summaryrefslogtreecommitdiff
path: root/endless/eostopbar.c
blob: 9433ef7ca9e586a9315d15ab9c3a2aecb9874497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/* Copyright 2013 Endless Mobile, Inc. */

#include "config.h"
#include "eostopbar-private.h"

#include <glib-object.h>
#include <gtk/gtk.h>

/*
 * SECTION:topbar
 * @short_description: The top bar of the window, above the main area of your
 * application.
 * @title: TopBar
 * 
 * The #EosTopBar has three different areas that can be managed through this 
 * class: a left widget, center widget, and action buttons area.
 * 
 * The action buttons area contain "minimize", "maximize" and "close" buttons.
 */
#define _EOS_STYLE_CLASS_TOP_BAR "top-bar"
#define _EOS_STYLE_CLASS_UNMAXIMIZED "unmaximized"
#define _EOS_TOP_BAR_HEIGHT_PX 36
#define _EOS_TOP_BAR_BUTTON_PADDING_PX 4
#define _EOS_TOP_BAR_ICON_SIZE_PX 16
#define _EOS_TOP_BAR_HORIZONTAL_BUTTON_MARGIN_PX 7
#define _EOS_TOP_BAR_BUTTON_SEPARATION_PX 8
#define _EOS_TOP_BAR_VERTICAL_BUTTON_MARGIN_PX 6
#define _EOS_TOP_BAR_MINIMIZE_ICON_NAME "window-minimize-symbolic"
#define _EOS_TOP_BAR_MAXIMIZE_ICON_NAME "window-maximize-symbolic"
#define _EOS_TOP_BAR_UNMAXIMIZE_ICON_NAME "window-unmaximize-symbolic"
#define _EOS_TOP_BAR_CLOSE_ICON_NAME "window-close-symbolic"
#define _EOS_TOP_BAR_CREDITS_ICON_NAME "user-info-symbolic"

typedef struct {
  GtkWidget *actions_grid;
  GtkWidget *left_top_bar_attach;
  GtkWidget *center_top_bar_attach;

  GtkWidget *left_top_bar_widget;
  GtkWidget *center_top_bar_widget;

  GtkWidget *minimize_button;
  GtkWidget *minimize_icon;
  GtkWidget *maximize_button;
  GtkWidget *maximize_icon;
  GtkWidget *close_button;
  GtkWidget *close_icon;
  GtkWidget *credits_button;
  GtkWidget *credits_icon;
  GtkWidget *credits_stack;

  gboolean show_credits_button;
  guint credits_enter_handler;
  guint credits_leave_handler;
} EosTopBarPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (EosTopBar, eos_top_bar, GTK_TYPE_EVENT_BOX)

enum {
  CLOSE_CLICKED,
  MINIMIZE_CLICKED,
  MAXIMIZE_CLICKED,
  CREDITS_CLICKED,
  LAST_SIGNAL
};

static guint top_bar_signals[LAST_SIGNAL] = { 0 };

enum {
  PROP_0,
  PROP_SHOW_CREDITS_BUTTON,
  NPROPS
};

static GParamSpec *eos_top_bar_props[NPROPS] = { NULL, };

static void
eos_top_bar_get_property (GObject    *object,
                          guint       property_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  EosTopBar *self = EOS_TOP_BAR (object);

  switch (property_id)
    {
    case PROP_SHOW_CREDITS_BUTTON:
      g_value_set_boolean (value, eos_top_bar_get_show_credits_button (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
eos_top_bar_set_property (GObject      *object,
                          guint         property_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  EosTopBar *self = EOS_TOP_BAR (object);

  switch (property_id)
    {
    case PROP_SHOW_CREDITS_BUTTON:
      eos_top_bar_set_show_credits_button (self, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
eos_top_bar_get_preferred_height (GtkWidget *widget,
                                  int *minimum,
                                  int *natural)
{
  if (minimum != NULL)
    *minimum = _EOS_TOP_BAR_HEIGHT_PX;
  if (natural != NULL)
    *natural = _EOS_TOP_BAR_HEIGHT_PX;
}

/* Draw the edge finishing on the two lines inside the topbar; see
after_draw_cb() in eoswindow.c for the two lines outside the topbar */
static gboolean
eos_top_bar_draw (GtkWidget *self_widget,
                  cairo_t   *cr)
{
  GTK_WIDGET_CLASS (eos_top_bar_parent_class)->draw (self_widget, cr);

  gint width = gtk_widget_get_allocated_width (self_widget);
  cairo_set_line_width (cr, 1.0);
  /* Highlight: #ffffff, opacity 5% */
  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.05);
  cairo_move_to (cr, 0, _EOS_TOP_BAR_HEIGHT_PX - 1.5);
  cairo_rel_line_to (cr, width, 0);
  cairo_stroke (cr);
  /* Baseline: #0a0a0a, opacity 100% */
  cairo_set_source_rgb (cr, 0.039, 0.039, 0.039);
  cairo_move_to (cr, 0, _EOS_TOP_BAR_HEIGHT_PX - 0.5);
  cairo_rel_line_to (cr, width, 0);
  cairo_stroke (cr);

  return GDK_EVENT_PROPAGATE;
}

static void
eos_top_bar_class_init (EosTopBarClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->get_property = eos_top_bar_get_property;
  object_class->set_property = eos_top_bar_set_property;
  widget_class->get_preferred_height = eos_top_bar_get_preferred_height;
  widget_class->draw = eos_top_bar_draw;

  /*
   * Emitted when the minimize button has been activated.
   */
  top_bar_signals[MINIMIZE_CLICKED] =
      g_signal_new ("minimize-clicked",
                    G_OBJECT_CLASS_TYPE (object_class),
                    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                    0,
                    NULL, NULL, NULL,
                    G_TYPE_NONE, 0);

  /*
   * Emitted when the maximize button has been activated.
   */
  top_bar_signals[MAXIMIZE_CLICKED] =
      g_signal_new ("maximize-clicked",
                    G_OBJECT_CLASS_TYPE (object_class),
                    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                    0,
                    NULL, NULL, NULL,
                    G_TYPE_NONE, 0);

  /*
   * Emitted when the close button has been activated.
   */
  top_bar_signals[CLOSE_CLICKED] =
      g_signal_new ("close-clicked",
                    G_OBJECT_CLASS_TYPE (object_class),
                    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                    0,
                    NULL, NULL, NULL,
                    G_TYPE_NONE, 0);

  top_bar_signals[CREDITS_CLICKED] =
    g_signal_new ("credits-clicked", G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, 0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  eos_top_bar_props[PROP_SHOW_CREDITS_BUTTON] =
    g_param_spec_boolean ("show-credits-button", "Show credits button",
                         "Whether the credits button is discoverable",
                         FALSE,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, NPROPS, eos_top_bar_props);
}

static void
on_minimize_clicked_cb (GtkButton *button,
                        gpointer user_data)
{
  EosTopBar *self = EOS_TOP_BAR (user_data);
  g_signal_emit (self, top_bar_signals[MINIMIZE_CLICKED], 0);
}

static void
on_maximize_clicked_cb (GtkButton *button,
                        gpointer   user_data)
{
  EosTopBar *self = EOS_TOP_BAR (user_data);
  g_signal_emit (self, top_bar_signals[MAXIMIZE_CLICKED], 0);
}

static void
on_close_clicked_cb (GtkButton *button,
                     gpointer user_data)
{
  EosTopBar *self = EOS_TOP_BAR (user_data);
  g_signal_emit (self, top_bar_signals[CLOSE_CLICKED], 0);
}

static gboolean
on_stack_hover (GtkStack *stack,
                GdkEvent *event,
                gpointer  data)
{
  gboolean show = GPOINTER_TO_INT (data);
  gtk_stack_set_visible_child_name (stack, show ? "button" : "blank");
  return GDK_EVENT_PROPAGATE;
}

static void
on_credits_clicked (GtkButton *button,
                    EosTopBar *self)
{
  g_signal_emit (self, top_bar_signals[CREDITS_CLICKED], 0);
}

static void
eos_top_bar_init (EosTopBar *self)
{
  GtkStyleContext *context;
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

  context = gtk_widget_get_style_context (GTK_WIDGET (self));
  gtk_style_context_add_class (context, _EOS_STYLE_CLASS_TOP_BAR);

  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);

  priv->actions_grid =
    g_object_new (GTK_TYPE_GRID,
                  "orientation", GTK_ORIENTATION_HORIZONTAL,
                  "hexpand", TRUE,
                  "halign", GTK_ALIGN_FILL,
                  "column-spacing", _EOS_TOP_BAR_BUTTON_SEPARATION_PX,
                  "margin-top", _EOS_TOP_BAR_VERTICAL_BUTTON_MARGIN_PX,
                  "margin-bottom", _EOS_TOP_BAR_VERTICAL_BUTTON_MARGIN_PX,
                  "margin-start", _EOS_TOP_BAR_HORIZONTAL_BUTTON_MARGIN_PX,
                  "margin-end", _EOS_TOP_BAR_HORIZONTAL_BUTTON_MARGIN_PX,
                  NULL);

  priv->left_top_bar_attach = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
  priv->center_top_bar_attach = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
  gtk_widget_set_hexpand (priv->center_top_bar_attach, TRUE);
  gtk_widget_set_halign (priv->center_top_bar_attach, GTK_ALIGN_CENTER);

  /* TODO implement adding actions and widgets to the actions_grid */

  priv->minimize_button =
    g_object_new (GTK_TYPE_BUTTON,
                  "can-focus", FALSE,
                  "halign", GTK_ALIGN_END,
                  "valign", GTK_ALIGN_CENTER,
                  NULL);
  priv->minimize_icon =
    gtk_image_new_from_icon_name (_EOS_TOP_BAR_MINIMIZE_ICON_NAME,
                                  GTK_ICON_SIZE_SMALL_TOOLBAR);
  g_object_set(priv->minimize_icon,
               "pixel-size", _EOS_TOP_BAR_ICON_SIZE_PX,
               "margin", _EOS_TOP_BAR_BUTTON_PADDING_PX,
               NULL);
  gtk_container_add (GTK_CONTAINER (priv->minimize_button),
                     priv->minimize_icon);

  priv->maximize_button =
    g_object_new (GTK_TYPE_BUTTON,
                  "can-focus", FALSE,
                  "halign", GTK_ALIGN_END,
                  "valign", GTK_ALIGN_CENTER,
                  NULL);
  priv->maximize_icon = gtk_image_new ();
  eos_top_bar_update_window_maximized (self, TRUE);
  g_object_set(priv->maximize_icon,
               "pixel-size", _EOS_TOP_BAR_ICON_SIZE_PX,
               "margin", _EOS_TOP_BAR_BUTTON_PADDING_PX,
               NULL);
  gtk_container_add (GTK_CONTAINER (priv->maximize_button),
                     priv->maximize_icon);

  priv->close_button =
    g_object_new (GTK_TYPE_BUTTON,
                  "can-focus", FALSE,
                  "halign", GTK_ALIGN_END,
                  "valign", GTK_ALIGN_CENTER,
                  NULL);
  priv->close_icon =
      gtk_image_new_from_icon_name (_EOS_TOP_BAR_CLOSE_ICON_NAME,
                                    GTK_ICON_SIZE_SMALL_TOOLBAR);
  g_object_set(priv->close_icon,
               "pixel-size", _EOS_TOP_BAR_ICON_SIZE_PX,
               "margin", _EOS_TOP_BAR_BUTTON_PADDING_PX,
               NULL);
  gtk_container_add (GTK_CONTAINER (priv->close_button),
                     priv->close_icon);

  /* This works like a revealer but it's really a GtkStack so that it takes up
  space and presents a target even when it's not shown. */
  priv->credits_stack = gtk_stack_new ();
  gtk_stack_set_transition_type (GTK_STACK (priv->credits_stack),
                                 GTK_STACK_TRANSITION_TYPE_CROSSFADE);
  gtk_widget_add_events (priv->credits_stack,
                         GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
  gtk_stack_add_named (GTK_STACK (priv->credits_stack),
                       gtk_event_box_new (), "blank");
  priv->credits_button = g_object_new (GTK_TYPE_BUTTON,
                                       "can-focus", FALSE,
                                       "halign", GTK_ALIGN_END,
                                       "valign", GTK_ALIGN_CENTER,
                                       NULL);
  priv->credits_icon =
    gtk_image_new_from_icon_name (_EOS_TOP_BAR_CREDITS_ICON_NAME,
                                  GTK_ICON_SIZE_SMALL_TOOLBAR);
  g_object_set (priv->credits_icon,
                "pixel-size", _EOS_TOP_BAR_ICON_SIZE_PX,
                "margin", _EOS_TOP_BAR_BUTTON_PADDING_PX,
                NULL);
  gtk_container_add (GTK_CONTAINER (priv->credits_button), priv->credits_icon);
  gtk_stack_add_named (GTK_STACK (priv->credits_stack),
                       priv->credits_button, "button");

  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->left_top_bar_attach);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->center_top_bar_attach);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid), priv->credits_stack);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->minimize_button);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->maximize_button);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->close_button);

  gtk_container_add (GTK_CONTAINER (self), priv->actions_grid);

  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
  gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_FILL);

  g_signal_connect (priv->minimize_button, "clicked",
                    G_CALLBACK (on_minimize_clicked_cb), self);
  g_signal_connect (priv->maximize_button, "clicked",
                    G_CALLBACK (on_maximize_clicked_cb), self);
  g_signal_connect (priv->close_button, "clicked",
                    G_CALLBACK (on_close_clicked_cb), self);
  priv->credits_enter_handler =
    g_signal_connect (priv->credits_stack, "enter-notify-event",
                      G_CALLBACK (on_stack_hover), GINT_TO_POINTER (TRUE));
  priv->credits_leave_handler =
    g_signal_connect (priv->credits_stack, "leave-notify-event",
                      G_CALLBACK (on_stack_hover), GINT_TO_POINTER (FALSE));
  g_signal_handler_block (priv->credits_stack, priv->credits_enter_handler);
  g_signal_handler_block (priv->credits_stack, priv->credits_leave_handler);
  g_signal_connect (priv->credits_button, "clicked",
                    G_CALLBACK (on_credits_clicked), self);
}

GtkWidget *
eos_top_bar_new (void)
{
  return GTK_WIDGET (g_object_new (EOS_TYPE_TOP_BAR, NULL));
}

/*
 * eos_top_bar_set_left_widget:
 * @self: the top bar
 * @left_top_bar_widget: the left top bar widget to be set
 * 
 * Sets the left widget in the top bar.
 */
void
eos_top_bar_set_left_widget (EosTopBar *self,
                             GtkWidget *left_top_bar_widget)
{
  g_return_if_fail (EOS_IS_TOP_BAR (self));
  g_return_if_fail (left_top_bar_widget == NULL || GTK_IS_WIDGET (left_top_bar_widget));

  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

  if (priv->left_top_bar_widget == left_top_bar_widget)
    return;

  if (priv->left_top_bar_widget)
    gtk_container_remove (GTK_CONTAINER (priv->left_top_bar_attach),
                       priv->left_top_bar_widget);

  priv->left_top_bar_widget = left_top_bar_widget;
  if (left_top_bar_widget)
    {
      gtk_container_add (GTK_CONTAINER (priv->left_top_bar_attach),
                         priv->left_top_bar_widget);
    }
}

/*
 * eos_top_bar_set_center_widget:
 * @self: the top bar
 * @center_top_bar_widget: the center top bar widget to be set
 * 
 * Sets the center widget in the top bar.
 */
void
eos_top_bar_set_center_widget (EosTopBar *self,
                               GtkWidget *center_top_bar_widget)
{
  g_return_if_fail (EOS_IS_TOP_BAR (self));
  g_return_if_fail (center_top_bar_widget == NULL || GTK_IS_WIDGET (center_top_bar_widget));

  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

  if (priv->center_top_bar_widget == center_top_bar_widget)
    return;

  if (priv->center_top_bar_widget)
    gtk_container_remove (GTK_CONTAINER (priv->center_top_bar_attach),
                          priv->center_top_bar_widget);

  priv->center_top_bar_widget = center_top_bar_widget;
  if (center_top_bar_widget)
    {
      gtk_container_add (GTK_CONTAINER (priv->center_top_bar_attach),
                         priv->center_top_bar_widget);
    }
}

/*
 * eos_top_bar_update_window_maximized:
 * @self: the top bar
 * @is_maximized: whether the window is currently maximized
 *
 * Private method for eos_window to update the topbar on the window maximized
 * state. The top bar will flip the asset of the maximized button depending on
 * the state
 */
void
eos_top_bar_update_window_maximized (EosTopBar *self,
                                     gboolean is_maximized)
{
  g_return_if_fail (EOS_IS_TOP_BAR (self));
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

  gchar *icon_name = is_maximized ? _EOS_TOP_BAR_UNMAXIMIZE_ICON_NAME : _EOS_TOP_BAR_MAXIMIZE_ICON_NAME;
  gtk_image_set_from_icon_name (GTK_IMAGE (priv->maximize_icon),
                                icon_name,
                                GTK_ICON_SIZE_SMALL_TOOLBAR);

  GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
  if (!is_maximized)
    gtk_style_context_add_class (context, _EOS_STYLE_CLASS_UNMAXIMIZED);
  else
    gtk_style_context_remove_class (context, _EOS_STYLE_CLASS_UNMAXIMIZED);
}

/*
 * eos_top_bar_get_show_credits_button:
 * @self: the top bar
 *
 * See eos_top_bar_set_show_credits_button().
 *
 * Returns: %TRUE if credits button should be discoverable, %FALSE if not.
 */
gboolean
eos_top_bar_get_show_credits_button (EosTopBar *self)
{
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);
  return priv->show_credits_button;
}

/*
 * eos_top_bar_set_show_credits_button:
 * @self: the top bar
 * @show_credits_button: whether the credits button should be discoverable.
 *
 * Gets whether the credits button should be discoverable.
 * Note that the credits button is not visible as such, but when the mouse
 * hovers over it, it becomes visible if this is set to %TRUE.
 * If this is %FALSE, the button never becomes visible.
 */
void
eos_top_bar_set_show_credits_button (EosTopBar *self,
                                     gboolean   show_credits_button)
{
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);
  if (priv->show_credits_button == show_credits_button)
    return;

  priv->show_credits_button = show_credits_button;
  if (show_credits_button)
    {
      g_signal_handler_unblock (priv->credits_stack,
                                priv->credits_enter_handler);
      g_signal_handler_unblock (priv->credits_stack,
                                priv->credits_leave_handler);
    }
  else
    {
      gtk_stack_set_visible_child_name (GTK_STACK (priv->credits_stack),
                                        "blank");
      g_signal_handler_block (priv->credits_stack, priv->credits_enter_handler);
      g_signal_handler_block (priv->credits_stack, priv->credits_leave_handler);
    }
  g_object_notify_by_pspec (G_OBJECT (self),
                            eos_top_bar_props[PROP_SHOW_CREDITS_BUTTON]);
}