hdy-status-page.c 11.4 KB
Newer Older
Andrei Lișiță's avatar
Andrei Lișiță committed
1
2
3
/*
 * Copyright (C) 2020 Andrei Lișiță <andreii.lisita@gmail.com>
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
Andrei Lișiță's avatar
Andrei Lișiță committed
5
6
7
8
9
10
11
12
 */

#include "config.h"
#include <glib/gi18n-lib.h>

#include "hdy-status-page.h"

/**
13
 * HdyStatusPage:
Andrei Lișiță's avatar
Andrei Lișiță committed
14
 *
15
16
17
 * A page used for empty/error states and similar use-cases.
 *
 * The `HdyStatusPage` widget can have an icon, a title, a description and a
Andrei Lișiță's avatar
Andrei Lișiță committed
18
19
 * custom widget which is displayed below them.
 *
20
 * ## CSS nodes
Andrei Lișiță's avatar
Andrei Lișiță committed
21
 *
22
 * `HdyStatusPage` has a main CSS node with name `statuspage`.
Andrei Lișiță's avatar
Andrei Lișiță committed
23
 *
Adrien Plazas's avatar
Adrien Plazas committed
24
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
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
 */

enum {
  PROP_0,
  PROP_ICON_NAME,
  PROP_TITLE,
  PROP_DESCRIPTION,
  LAST_PROP,
};

static GParamSpec *props[LAST_PROP];

struct _HdyStatusPage
{
  GtkBin parent_instance;

  GtkWidget *scrolled_window;
  GtkBox *toplevel_box;
  GtkImage *image;
  gchar *icon_name;
  GtkLabel *title_label;
  GtkLabel *description_label;

  GtkWidget *user_widget;
};

G_DEFINE_TYPE (HdyStatusPage, hdy_status_page, GTK_TYPE_BIN)

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static void
update_title_visibility (HdyStatusPage *self)
{
  gtk_widget_set_visible (GTK_WIDGET (self->title_label),
                          gtk_label_get_text (self->title_label) != NULL &&
                          g_strcmp0 (gtk_label_get_text (self->title_label), "") != 0);
}

static void
update_description_visibility (HdyStatusPage *self)
{
  gtk_widget_set_visible (GTK_WIDGET (self->description_label),
                          gtk_label_get_text (self->description_label) != NULL &&
                          g_strcmp0 (gtk_label_get_text (self->description_label), "") != 0);
}

Andrei Lișiță's avatar
Andrei Lișiță committed
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
static void
hdy_status_page_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (object);

  switch (prop_id) {
  case PROP_ICON_NAME:
    g_value_set_string (value, hdy_status_page_get_icon_name (self));
    break;

  case PROP_TITLE:
    g_value_set_string (value, hdy_status_page_get_title (self));
    break;

  case PROP_DESCRIPTION:
    g_value_set_string (value, hdy_status_page_get_description (self));
    break;

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

static void
hdy_status_page_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (object);

  switch (prop_id) {
  case PROP_ICON_NAME:
    hdy_status_page_set_icon_name (self, g_value_get_string (value));
    break;

  case PROP_TITLE:
    hdy_status_page_set_title (self, g_value_get_string (value));
    break;

  case PROP_DESCRIPTION:
    hdy_status_page_set_description (self, g_value_get_string (value));
    break;

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

static void
hdy_status_page_finalize (GObject *object)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (object);

  g_clear_pointer (&self->icon_name, g_free);

  G_OBJECT_CLASS (hdy_status_page_parent_class)->finalize (object);
}

static void
hdy_status_page_destroy (GtkWidget *widget)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (widget);

  if (self->scrolled_window) {
    gtk_container_remove (GTK_CONTAINER (self), self->scrolled_window);
    self->toplevel_box = NULL;
    self->image = NULL;
    self->title_label = NULL;
    self->description_label = NULL;
    self->user_widget = NULL;
  }

  GTK_WIDGET_CLASS (hdy_status_page_parent_class)->destroy (widget);
}

static void
hdy_status_page_add (GtkContainer *container,
                     GtkWidget    *child)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (container);

  if (!self->scrolled_window) {
    GTK_CONTAINER_CLASS (hdy_status_page_parent_class)->add (container, child);
  } else if (!self->user_widget) {
    gtk_container_add (GTK_CONTAINER (self->toplevel_box), child);
    self->user_widget = child;
  } else {
    g_warning ("Attempting to add a second child to a HdyStatusPage, but a HdyStatusPage can only have one child");
  }
}

static void
hdy_status_page_remove (GtkContainer *container,
                        GtkWidget    *child)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (container);

  if (child == self->scrolled_window) {
    GTK_CONTAINER_CLASS (hdy_status_page_parent_class)->remove (container, child);
  } else if (child == self->user_widget) {
    gtk_container_remove (GTK_CONTAINER (self->toplevel_box), child);
    self->user_widget = NULL;
  } else {
    g_return_if_reached ();
  }
}

static void
hdy_status_page_forall (GtkContainer *container,
                        gboolean      include_internals,
                        GtkCallback   callback,
                        gpointer      callback_data)
{
  HdyStatusPage *self = HDY_STATUS_PAGE (container);

  if (include_internals)
    GTK_CONTAINER_CLASS (hdy_status_page_parent_class)->forall (container,
                                                                include_internals,
                                                                callback,
                                                                callback_data);
  else if (self->user_widget)
    callback (self->user_widget, callback_data);
}

static void
hdy_status_page_class_init (HdyStatusPageClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  object_class->get_property = hdy_status_page_get_property;
  object_class->set_property = hdy_status_page_set_property;
  object_class->finalize = hdy_status_page_finalize;
  widget_class->destroy = hdy_status_page_destroy;
  container_class->add = hdy_status_page_add;
  container_class->remove = hdy_status_page_remove;
  container_class->forall = hdy_status_page_forall;

  /**
213
   * HdyStatusPage:icon-name: (attributes org.gtk.Property.get=hdy_status_page_get_icon_name org.gtk.Property.set=hdy_status_page_set_icon_name)
Andrei Lișiță's avatar
Andrei Lișiță committed
214
215
216
   *
   * The name of the icon to be used.
   *
Adrien Plazas's avatar
Adrien Plazas committed
217
   * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
218
219
220
221
222
223
224
225
226
   */
  props[PROP_ICON_NAME] =
    g_param_spec_string ("icon-name",
                         _("Icon name"),
                         _("The name of the icon to be used"),
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  /**
227
   * HdyStatusPage:title: (attributes org.gtk.Property.get=hdy_status_page_get_title org.gtk.Property.set=hdy_status_page_set_title)
Andrei Lișiță's avatar
Andrei Lișiță committed
228
229
230
   *
   * The title to be displayed below the icon.
   *
Adrien Plazas's avatar
Adrien Plazas committed
231
   * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
232
233
234
235
236
237
238
239
240
   */
  props[PROP_TITLE] =
    g_param_spec_string ("title",
                         _("Title"),
                         _("The title to be displayed below the icon"),
                         "",
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  /**
241
   * HdyStatusPage:description: (attributes org.gtk.Property.get=hdy_status_page_get_description org.gtk.Property.set=hdy_status_page_set_description)
Andrei Lișiță's avatar
Andrei Lișiță committed
242
243
244
   *
   * The description to be displayed below the title.
   *
Adrien Plazas's avatar
Adrien Plazas committed
245
   * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
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
   */
  props[PROP_DESCRIPTION] =
    g_param_spec_string ("description",
                         _("Description"),
                         _("The description to be displayed below the title"),
                         "",
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/sm/puri/handy/ui/hdy-status-page.ui");
  gtk_widget_class_bind_template_child (widget_class, HdyStatusPage, scrolled_window);
  gtk_widget_class_bind_template_child (widget_class, HdyStatusPage, toplevel_box);
  gtk_widget_class_bind_template_child (widget_class, HdyStatusPage, image);
  gtk_widget_class_bind_template_child (widget_class, HdyStatusPage, title_label);
  gtk_widget_class_bind_template_child (widget_class, HdyStatusPage, description_label);

  gtk_widget_class_set_css_name (widget_class, "statuspage");
}

static void
hdy_status_page_init (HdyStatusPage *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
271
272
273

  update_title_visibility (self);
  update_description_visibility (self);
Andrei Lișiță's avatar
Andrei Lișiță committed
274
275
276
277
278
}

/**
 * hdy_status_page_new:
 *
279
 * Creates a new `HdyStatusPage`.
Andrei Lișiță's avatar
Andrei Lișiță committed
280
 *
281
 * Returns: the newly created `HdyStatusPage`
Andrei Lișiță's avatar
Andrei Lișiță committed
282
 *
Adrien Plazas's avatar
Adrien Plazas committed
283
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
284
285
286
287
288
289
290
291
 */
GtkWidget *
hdy_status_page_new (void)
{
  return g_object_new (HDY_TYPE_STATUS_PAGE, NULL);
}

/**
292
293
 * hdy_status_page_get_icon_name: (attributes org.gtk.Method.get_property=icon-name)
 * @self: a status page
Andrei Lișiță's avatar
Andrei Lișiță committed
294
295
296
 *
 * Gets the icon name for @self.
 *
297
 * Returns: (transfer none) (nullable): the icon name for @self
Andrei Lișiță's avatar
Andrei Lișiță committed
298
 *
Adrien Plazas's avatar
Adrien Plazas committed
299
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
300
301
302
303
304
305
306
307
 */
const gchar *
hdy_status_page_get_icon_name (HdyStatusPage *self)
{
  return self->icon_name;
}

/**
308
309
 * hdy_status_page_set_icon_name: (attributes org.gtk.Method.set_property=icon-name)
 * @self: a status page
Andrei Lișiță's avatar
Andrei Lișiță committed
310
311
312
313
 * @icon_name: (nullable): the icon name
 *
 * Sets the icon name for @self.
 *
Adrien Plazas's avatar
Adrien Plazas committed
314
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
 */
void
hdy_status_page_set_icon_name (HdyStatusPage *self,
                               const gchar   *icon_name)
{
  g_return_if_fail (HDY_IS_STATUS_PAGE (self));

  if (g_strcmp0 (self->icon_name, icon_name) == 0)
    return;

  g_free (self->icon_name);
  self->icon_name = g_strdup (icon_name);

  if (!icon_name)
    g_object_set (G_OBJECT (self->image), "icon-name", "image-missing", NULL);
  else
    g_object_set (G_OBJECT (self->image), "icon-name", icon_name, NULL);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ICON_NAME]);
}

/**
337
338
 * hdy_status_page_get_title: (attributes org.gtk.Method.get_property=title)
 * @self: a status page
Andrei Lișiță's avatar
Andrei Lișiță committed
339
340
341
 *
 * Gets the title for @self.
 *
342
 * Returns: (transfer none) (nullable): the title for @self
Andrei Lișiță's avatar
Andrei Lișiță committed
343
 *
Adrien Plazas's avatar
Adrien Plazas committed
344
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
345
346
347
348
349
350
351
352
353
354
 */
const gchar *
hdy_status_page_get_title (HdyStatusPage *self)
{
  g_return_val_if_fail (HDY_IS_STATUS_PAGE (self), NULL);

  return gtk_label_get_label (self->title_label);
}

/**
355
356
 * hdy_status_page_set_title: (attributes org.gtk.Method.set_property=title)
 * @self: a status page
Andrei Lișiță's avatar
Andrei Lișiță committed
357
358
359
360
 * @title: (nullable): the title
 *
 * Sets the title for @self.
 *
Adrien Plazas's avatar
Adrien Plazas committed
361
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
362
363
364
365
366
367
368
369
370
371
372
 */
void
hdy_status_page_set_title (HdyStatusPage *self,
                           const gchar   *title)
{
  g_return_if_fail (HDY_IS_STATUS_PAGE (self));

  if (g_strcmp0 (title, hdy_status_page_get_title (self)) == 0)
    return;

  gtk_label_set_label (self->title_label, title);
373
  update_title_visibility (self);
Andrei Lișiță's avatar
Andrei Lișiță committed
374
375
376
377
378

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TITLE]);
}

/**
379
380
 * hdy_status_page_get_description: (attributes org.gtk.Method.get_property=description)
 * @self: a status page
Andrei Lișiță's avatar
Andrei Lișiță committed
381
382
383
 *
 * Gets the description for @self.
 *
384
 * Returns: (transfer none) (nullable): the description for @self
Andrei Lișiță's avatar
Andrei Lișiță committed
385
 *
Adrien Plazas's avatar
Adrien Plazas committed
386
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
387
388
389
390
391
392
393
394
395
396
 */
const gchar *
hdy_status_page_get_description (HdyStatusPage *self)
{
  g_return_val_if_fail (HDY_IS_STATUS_PAGE (self), NULL);

  return gtk_label_get_label (self->description_label);
}

/**
397
398
 * hdy_status_page_set_description: (attributes org.gtk.Method.set_property=description)
 * @self: a status page
Andrei Lișiță's avatar
Andrei Lișiță committed
399
400
401
402
 * @description: (nullable): the description
 *
 * Sets the description for @self.
 *
Adrien Plazas's avatar
Adrien Plazas committed
403
 * Since: 1.2
Andrei Lișiță's avatar
Andrei Lișiță committed
404
405
406
407
408
409
410
411
412
413
414
 */
void
hdy_status_page_set_description (HdyStatusPage *self,
                                 const gchar   *description)
{
  g_return_if_fail (HDY_IS_STATUS_PAGE (self));

  if (g_strcmp0 (description, hdy_status_page_get_description (self)) == 0)
    return;

  gtk_label_set_label (self->description_label, description);
415
  update_description_visibility (self);
Andrei Lișiță's avatar
Andrei Lișiță committed
416
417
418

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_DESCRIPTION]);
}