summaryrefslogtreecommitdiff
path: root/src/widgets/control-center/control-slider.c
blob: 9c69190f068a790895a2ec47d4b70be75943a980 (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
#include "widgets/control-center/control-slider.h"

//
// FoobarControlSlider:
//
// A slider in the "controls" section of the control center with a title and icon, as well as an optional secondary
// button to toggle a details view.
//

struct _FoobarControlSlider
{
	GtkWidget parent_instance;
	gchar*    icon_name;
	gchar*    label;
	gboolean  can_expand;
	gboolean  is_expanded;
	gint      percentage;
};

enum
{
	PROP_ICON_NAME = 1,
	PROP_LABEL,
	PROP_CAN_EXPAND,
	PROP_IS_EXPANDED,
	PROP_PERCENTAGE,
	N_PROPS,
};

static GParamSpec* props[N_PROPS] = { 0 };

static void foobar_control_slider_class_init           ( FoobarControlSliderClass* klass );
static void foobar_control_slider_init                 ( FoobarControlSlider*      self );
static void foobar_control_slider_get_property         ( GObject*                  object,
                                                         guint                     prop_id,
                                                         GValue*                   value,
                                                         GParamSpec*               pspec );
static void foobar_control_slider_set_property         ( GObject*                  object,
                                                         guint                     prop_id,
                                                         GValue const*             value,
                                                         GParamSpec*               pspec );
static void foobar_control_slider_dispose              ( GObject*                  object );
static void foobar_control_slider_finalize             ( GObject*                  object );
static void foobar_control_slider_handle_expand_clicked( GtkButton*                button,
                                                         gpointer                  userdata );

G_DEFINE_FINAL_TYPE( FoobarControlSlider, foobar_control_slider, GTK_TYPE_WIDGET )

// ---------------------------------------------------------------------------------------------------------------------
// Widget Implementation
// ---------------------------------------------------------------------------------------------------------------------

//
// Static initialization for control sliders.
//
void foobar_control_slider_class_init( FoobarControlSliderClass* klass )
{
	GtkWidgetClass* widget_klass = GTK_WIDGET_CLASS( klass );
	gtk_widget_class_set_css_name( widget_klass, "control-slider" );
	gtk_widget_class_set_layout_manager_type( widget_klass, GTK_TYPE_BOX_LAYOUT );

	GObjectClass* object_klass = G_OBJECT_CLASS( klass );
	object_klass->get_property = foobar_control_slider_get_property;
	object_klass->set_property = foobar_control_slider_set_property;
	object_klass->dispose = foobar_control_slider_dispose;
	object_klass->finalize = foobar_control_slider_finalize;

	props[PROP_ICON_NAME] = g_param_spec_string(
		"icon-name",
		"Icon Name",
		"Name of the icon to be displayed next to the label.",
		NULL,
		G_PARAM_READWRITE );
	props[PROP_LABEL] = g_param_spec_string(
		"label",
		"Label",
		"The slider's main label.",
		NULL,
		G_PARAM_READWRITE );
	props[PROP_CAN_EXPAND] = g_param_spec_boolean(
		"can-expand",
		"Can Expand",
		"Indicates whether some details are hidden and can be revealed by a toggle button next to the slider.",
		FALSE,
		G_PARAM_READWRITE );
	props[PROP_IS_EXPANDED] = g_param_spec_boolean(
		"is-expanded",
		"Is Expanded",
		"Indicates whether additional details should be shown.",
		FALSE,
		G_PARAM_READWRITE );
	props[PROP_PERCENTAGE] = g_param_spec_int(
		"percentage",
		"Percentage",
		"Value shown by the slider.",
		0,
		100,
		0,
		G_PARAM_READWRITE );
	g_object_class_install_properties( object_klass, N_PROPS, props );
}

//
// Instance initialization for control sliders.
//
void foobar_control_slider_init( FoobarControlSlider* self )
{
	GtkWidget* icon = gtk_image_new( );
	gtk_widget_set_valign( icon, GTK_ALIGN_CENTER );
	g_object_bind_property(
		self,
		"icon-name",
		icon,
		"icon-name",
		G_BINDING_SYNC_CREATE );

	GtkWidget* label = gtk_label_new( NULL );
	gtk_widget_set_valign( label, GTK_ALIGN_CENTER );
	gtk_widget_set_hexpand( label, TRUE );
	gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT );
	gtk_label_set_xalign( GTK_LABEL( label ), 0 );
	g_object_bind_property(
		self,
		"label",
		label,
		"label",
		G_BINDING_SYNC_CREATE );

	GtkWidget* header = gtk_box_new( GTK_ORIENTATION_HORIZONTAL, 0 );
	gtk_box_append( GTK_BOX( header ), icon );
	gtk_box_append( GTK_BOX( header ), label );

	GtkWidget* scale = gtk_scale_new_with_range( GTK_ORIENTATION_HORIZONTAL, 0, 100, 2 );
	g_object_bind_property(
		self,
		"percentage",
		gtk_range_get_adjustment( GTK_RANGE( scale ) ),
		"value",
		G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL );

	GtkWidget* primary_content = gtk_box_new( GTK_ORIENTATION_VERTICAL, 0 );
	gtk_widget_add_css_class( primary_content, "primary" );
	gtk_box_append( GTK_BOX( primary_content ), header );
	gtk_box_append( GTK_BOX( primary_content ), scale );

	GtkWidget* expand_button = gtk_button_new_from_icon_name( "fluent-chevron-right-symbolic" );
	gtk_widget_add_css_class( expand_button, "expand" );
	g_object_bind_property(
		self,
		"can-expand",
		expand_button,
		"visible",
		G_BINDING_SYNC_CREATE );
	g_signal_connect( expand_button, "clicked", G_CALLBACK( foobar_control_slider_handle_expand_clicked ), self );

	GtkLayoutManager* layout = gtk_widget_get_layout_manager( GTK_WIDGET( self ) );
	gtk_orientable_set_orientation( GTK_ORIENTABLE( layout ), GTK_ORIENTATION_HORIZONTAL );
	gtk_widget_insert_before( primary_content, GTK_WIDGET( self ), NULL );
	gtk_widget_insert_before( expand_button, GTK_WIDGET( self ), NULL );
}

//
// Property getter implementation, mapping a property id to a method.
//
void foobar_control_slider_get_property(
	GObject*    object,
	guint       prop_id,
	GValue*     value,
	GParamSpec* pspec )
{
	FoobarControlSlider* self = (FoobarControlSlider*)object;

	switch ( prop_id )
	{
		case PROP_ICON_NAME:
			g_value_set_string( value, foobar_control_slider_get_icon_name( self ) );
			break;
		case PROP_LABEL:
			g_value_set_string( value, foobar_control_slider_get_label( self ) );
			break;
		case PROP_CAN_EXPAND:
			g_value_set_boolean( value, foobar_control_slider_can_expand( self ) );
			break;
		case PROP_IS_EXPANDED:
			g_value_set_boolean( value, foobar_control_slider_is_expanded( self ) );
			break;
		case PROP_PERCENTAGE:
			g_value_set_int( value, foobar_control_slider_get_percentage( self ) );
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
			break;
	}
}

//
// Property setter implementation, mapping a property id to a method.
//
void foobar_control_slider_set_property(
	GObject*      object,
	guint         prop_id,
	GValue const* value,
	GParamSpec*   pspec )
{
	FoobarControlSlider* self = (FoobarControlSlider*)object;

	switch ( prop_id )
	{
		case PROP_ICON_NAME:
			foobar_control_slider_set_icon_name( self, g_value_get_string( value ) );
			break;
		case PROP_LABEL:
			foobar_control_slider_set_label( self, g_value_get_string( value ) );
			break;
		case PROP_CAN_EXPAND:
			foobar_control_slider_set_can_expand( self, g_value_get_boolean( value ) );
			break;
		case PROP_IS_EXPANDED:
			foobar_control_slider_set_expanded( self, g_value_get_boolean( value ) );
			break;
		case PROP_PERCENTAGE:
			foobar_control_slider_set_percentage( self, g_value_get_int( value ) );
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
			break;
	}
}

//
// Instance de-initialization for control sliders.
//
void foobar_control_slider_dispose( GObject* object )
{
	FoobarControlSlider* self = (FoobarControlSlider*)object;

	GtkWidget* child;
	while ( ( child = gtk_widget_get_first_child( GTK_WIDGET( self ) ) ) )
	{
		gtk_widget_unparent( child );
	}

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

//
// Instance cleanup for control sliders.
//
void foobar_control_slider_finalize( GObject* object )
{
	FoobarControlSlider* self = (FoobarControlSlider*)object;

	g_clear_pointer( &self->icon_name, g_free );
	g_clear_pointer( &self->label, g_free );

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

// ---------------------------------------------------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------------------------------------------------

//
// Create a new control slider instance.
//
GtkWidget* foobar_control_slider_new( void )
{
	return g_object_new( FOOBAR_TYPE_CONTROL_SLIDER, NULL );
}

//
// Get the name of the icon to be displayed next to the label.
//
gchar const* foobar_control_slider_get_icon_name( FoobarControlSlider* self )
{
	g_return_val_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ), NULL );
	return self->icon_name;
}

//
// Get the slider's main label.
//
gchar const* foobar_control_slider_get_label( FoobarControlSlider* self )
{
	g_return_val_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ), NULL );
	return self->label;
}

//
// Check whether some details are hidden and can be revealed by a toggle button next to the slider.
//
gboolean foobar_control_slider_can_expand( FoobarControlSlider* self )
{
	g_return_val_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ), FALSE );
	return self->can_expand;
}

//
// Check whether additional details should currently be shown.
//
gboolean foobar_control_slider_is_expanded( FoobarControlSlider* self )
{
	g_return_val_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ), FALSE );
	return self->is_expanded;
}

//
// Get the value shown by the slider.
//
gint foobar_control_slider_get_percentage( FoobarControlSlider* self )
{
	g_return_val_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ), FALSE );
	return self->percentage;
}

//
// Update the name of the icon to be displayed next to the label.
//
void foobar_control_slider_set_icon_name(
	FoobarControlSlider* self,
	gchar const*         value )
{
	g_return_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ) );

	if ( g_strcmp0( self->icon_name, value ) )
	{
		g_clear_pointer( &self->icon_name, g_free );
		self->icon_name = g_strdup( value );
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_ICON_NAME] );
	}
}

//
// Update the slider's main label.
//
void foobar_control_slider_set_label(
	FoobarControlSlider* self,
	gchar const*         value )
{
	g_return_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ) );

	if ( g_strcmp0( self->label, value ) )
	{
		g_clear_pointer( &self->label, g_free );
		self->label = g_strdup( value );
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_LABEL] );
	}
}

//
// Update whether some details are hidden and can be revealed by a toggle button next to the slider.
//
void foobar_control_slider_set_can_expand(
	FoobarControlSlider* self,
	gboolean             value )
{
	g_return_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ) );

	if ( !value ) { foobar_control_slider_set_expanded( self, FALSE ); }

	value = !!value;
	if ( self->can_expand != value )
	{
		self->can_expand = value;
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_CAN_EXPAND] );
	}
}

//
// Update whether additional details are currently shown.
//
void foobar_control_slider_set_expanded(
	FoobarControlSlider* self,
	gboolean             value )
{
	g_return_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ) );

	value = value && self->can_expand;
	if ( self->is_expanded != value )
	{
		self->is_expanded = value;
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_IS_EXPANDED] );

		if ( self->is_expanded )
		{
			gtk_widget_add_css_class( GTK_WIDGET( self ), "expanded" );
		}
		else
		{
			gtk_widget_remove_css_class( GTK_WIDGET( self ), "expanded" );
		}
	}
}

//
// Update the value shown by the slider.
//
void foobar_control_slider_set_percentage( FoobarControlSlider* self, gint value )
{
	g_return_if_fail( FOOBAR_IS_CONTROL_SLIDER( self ) );

	value = CLAMP( value, 0, 100 );
	if ( self->percentage != value )
	{
		self->percentage = value;
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_PERCENTAGE] );
	}
}

// ---------------------------------------------------------------------------------------------------------------------
// Signal Handlers
// ---------------------------------------------------------------------------------------------------------------------

//
// Called when the expand button was clicked.
//
// If allowed, this will toggle the "expanded" state of the slider.
//
void foobar_control_slider_handle_expand_clicked( GtkButton* button, gpointer userdata )
{
	(void)button;
	FoobarControlSlider* self = (FoobarControlSlider*)userdata;

	if ( foobar_control_slider_can_expand( self ) )
	{
		foobar_control_slider_set_expanded( self, !foobar_control_slider_is_expanded( self ) );
	}
}