summaryrefslogtreecommitdiffstats
path: root/src/gs-progress-button.c
blob: 13fb782af82c8be0c8594c486c97ae382f96cf02 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2013-2014 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2015 Kalev Lember <klember@redhat.com>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include "config.h"

#include "gs-app.h"
#include "gs-progress-button.h"

struct _GsProgressButton
{
	GtkButton	 parent_instance;

	GtkWidget	*image;
	GtkWidget	*label;
	GtkWidget	*stack;

	GtkCssProvider	*css_provider;
	char		*label_text;
	char		*icon_name;
	gboolean	 show_icon;
};

G_DEFINE_TYPE (GsProgressButton, gs_progress_button, GTK_TYPE_BUTTON)

typedef enum {
	PROP_ICON_NAME = 1,
	PROP_SHOW_ICON,
	/* Overrides: */
	PROP_LABEL,
} GsProgressButtonProperty;

static GParamSpec *obj_props[PROP_SHOW_ICON + 1] = { NULL, };

void
gs_progress_button_set_progress (GsProgressButton *button, guint percentage)
{
	gchar tmp[64]; /* Large enough to hold the string below. */
	const gchar *css;

	if (percentage == GS_APP_PROGRESS_UNKNOWN) {
		css = ".install-progress {\n"
		      "  background-size: 25%;\n"
		      "  animation: install-progress-unknown-move infinite linear 2s;\n"
		      "}\n";
	} else {
		percentage = MIN (percentage, 100); /* No need to clamp an unsigned to 0, it produces errors. */
		g_assert ((gsize) g_snprintf (tmp, sizeof (tmp), ".install-progress { background-size: %u%%; }", percentage) < sizeof (tmp));
		css = tmp;
	}

	gtk_css_provider_load_from_data (button->css_provider, css, -1);
}

void
gs_progress_button_set_show_progress (GsProgressButton *button, gboolean show_progress)
{
	GtkStyleContext *context;

	context = gtk_widget_get_style_context (GTK_WIDGET (button));
	if (show_progress)
		gtk_style_context_add_class (context, "install-progress");
	else
		gtk_style_context_remove_class (context, "install-progress");
}

/**
 * gs_progress_button_get_label:
 * @button: a #GsProgressButton
 *
 * Get the label of @button.
 *
 * It should be used rather than gtk_button_get_label() as it can only retrieve
 * the text from the label set by gtk_button_set_label(), which also cannot be
 * used.
 *
 * Returns: the label of @button
 *
 * Since: 41
 */
const gchar *
gs_progress_button_get_label (GsProgressButton *button)
{
	g_return_val_if_fail (GS_IS_PROGRESS_BUTTON (button), NULL);

	return button->label_text;
}

/**
 * gs_progress_button_set_label:
 * @button: a #GsProgressButton
 * @label: a string
 *
 * Set the label of @button.
 *
 * It should be used rather than gtk_button_set_label() as it will replace the
 * content of @button by a new label, breaking it.
 *
 * Since: 41
 */
void
gs_progress_button_set_label (GsProgressButton *button, const gchar *label)
{
	g_return_if_fail (GS_IS_PROGRESS_BUTTON (button));

	if (g_strcmp0 (button->label_text, label) == 0)
		return;

	g_free (button->label_text);
	button->label_text = g_strdup (label);

	g_object_notify (G_OBJECT (button), "label");
}

/**
 * gs_progress_button_get_icon_name:
 * @button: a #GsProgressButton
 *
 * Get the value of #GsProgressButton:icon-name.
 *
 * Returns: (nullable): the name of the icon
 *
 * Since: 41
 */
const gchar *
gs_progress_button_get_icon_name (GsProgressButton *button)
{
	g_return_val_if_fail (GS_IS_PROGRESS_BUTTON (button), NULL);

	return button->icon_name;
}

/**
 * gs_progress_button_set_icon_name:
 * @button: a #GsProgressButton
 * @icon_name: (nullable): the name of the icon
 *
 * Set the value of #GsProgressButton:icon-name.
 *
 * Since: 41
 */
void
gs_progress_button_set_icon_name (GsProgressButton *button, const gchar *icon_name)
{
	g_return_if_fail (GS_IS_PROGRESS_BUTTON (button));

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

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

	g_object_notify_by_pspec (G_OBJECT (button), obj_props[PROP_ICON_NAME]);
}

/**
 * gs_progress_button_get_show_icon:
 * @button: a #GsProgressButton
 *
 * Get the value of #GsProgressButton:show-icon.
 *
 * Returns: %TRUE if the icon is shown, %FALSE if the label is shown
 *
 * Since: 41
 */
gboolean
gs_progress_button_get_show_icon (GsProgressButton *button)
{
	g_return_val_if_fail (GS_IS_PROGRESS_BUTTON (button), FALSE);

	return button->show_icon;
}

/**
 * gs_progress_button_set_show_icon:
 * @button: a #GsProgressButton
 * @show_icon: %TRUE to set show the icon, %FALSE to show the label
 *
 * Set the value of #GsProgressButton:show-icon.
 *
 * Since: 41
 */
void
gs_progress_button_set_show_icon (GsProgressButton *button, gboolean show_icon)
{
	GtkStyleContext *style;

	g_return_if_fail (GS_IS_PROGRESS_BUTTON (button));

	show_icon = !!show_icon;

	if (button->show_icon == show_icon)
		return;

	button->show_icon = show_icon;

	style = gtk_widget_get_style_context (GTK_WIDGET (button));
	if (show_icon) {
		gtk_stack_set_visible_child (GTK_STACK (button->stack), button->image);
		gtk_style_context_remove_class (style, "text-button");
		gtk_style_context_add_class (style, "image-button");
	} else {
		gtk_stack_set_visible_child (GTK_STACK (button->stack), button->label);
		gtk_style_context_remove_class (style, "image-button");
		gtk_style_context_add_class (style, "text-button");
	}

	g_object_notify_by_pspec (G_OBJECT (button), obj_props[PROP_SHOW_ICON]);
}

/**
 * gs_progress_button_set_size_groups:
 * @button: a #GsProgressButton
 * @label: the #GtkSizeGroup for the label
 * @image: the #GtkSizeGroup for the image
 *
 * Groups the size of different buttons while keeping adaptiveness.
 *
 * Since: 41
 */
void
gs_progress_button_set_size_groups (GsProgressButton *button, GtkSizeGroup *label, GtkSizeGroup *image)
{
	g_return_if_fail (GS_IS_PROGRESS_BUTTON (button));

	if (label != NULL)
		gtk_size_group_add_widget (label, button->label);
	if (image != NULL)
		gtk_size_group_add_widget (image, button->image);
}

static void
gs_progress_button_page_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	GsProgressButton *self = GS_PROGRESS_BUTTON (object);

	switch ((GsProgressButtonProperty) prop_id) {
	case PROP_LABEL:
		g_value_set_string (value, gs_progress_button_get_label (self));
		break;
	case PROP_ICON_NAME:
		g_value_set_string (value, gs_progress_button_get_icon_name (self));
		break;
	case PROP_SHOW_ICON:
		g_value_set_boolean (value, gs_progress_button_get_show_icon (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_progress_button_page_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	GsProgressButton *self = GS_PROGRESS_BUTTON (object);

	switch ((GsProgressButtonProperty) prop_id) {
	case PROP_LABEL:
		gs_progress_button_set_label (self, g_value_get_string (value));
		break;
	case PROP_ICON_NAME:
		gs_progress_button_set_icon_name (self, g_value_get_string (value));
		break;
	case PROP_SHOW_ICON:
		gs_progress_button_set_show_icon (self, g_value_get_boolean (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_progress_button_dispose (GObject *object)
{
	GsProgressButton *button = GS_PROGRESS_BUTTON (object);

	g_clear_object (&button->css_provider);

	G_OBJECT_CLASS (gs_progress_button_parent_class)->dispose (object);
}

static void
gs_progress_button_finalize (GObject *object)
{
	GsProgressButton *button = GS_PROGRESS_BUTTON (object);

	g_clear_pointer (&button->label_text, g_free);
	g_clear_pointer (&button->icon_name, g_free);

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

static void
gs_progress_button_init (GsProgressButton *button)
{
	gtk_widget_init_template (GTK_WIDGET (button));

	button->css_provider = gtk_css_provider_new ();
	gtk_style_context_add_provider (gtk_widget_get_style_context (GTK_WIDGET (button)),
					GTK_STYLE_PROVIDER (button->css_provider),
					GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}

static void
gs_progress_button_class_init (GsProgressButtonClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	object_class->get_property = gs_progress_button_page_get_property;
	object_class->set_property = gs_progress_button_page_set_property;
	object_class->dispose = gs_progress_button_dispose;
	object_class->finalize = gs_progress_button_finalize;

	/**
	 * GsProgressButton:icon-name: (nullable):
	 *
	 * The name of the icon for the button.
	 *
	 * Since: 41
	 */
	obj_props[PROP_ICON_NAME] =
		g_param_spec_string ("icon-name", NULL, NULL,
				     NULL,
				     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * GsProgressButton:show-icon:
	 *
	 * Whether to show the icon in place of the label.
	 *
	 * Since: 41
	 */
	obj_props[PROP_SHOW_ICON] =
		g_param_spec_boolean ("show-icon", NULL, NULL,
				      FALSE,
				      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties (object_class, G_N_ELEMENTS (obj_props), obj_props);

	g_object_class_override_property (object_class, PROP_LABEL, "label");

	gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-progress-button.ui");

	gtk_widget_class_bind_template_child (widget_class, GsProgressButton, image);
	gtk_widget_class_bind_template_child (widget_class, GsProgressButton, label);
	gtk_widget_class_bind_template_child (widget_class, GsProgressButton, stack);
}

GtkWidget *
gs_progress_button_new (void)
{
	return g_object_new (GS_TYPE_PROGRESS_BUTTON, NULL);
}