Debian Codegraph @

demo/pages/animations/adw-demo-page-animations.c

raw
1 #include "adw-demo-page-animations.h"
2
3 #include <glib/gi18n.h>
4
5 struct _AdwDemoPageAnimations
6 {
7 AdwBin parent_instance;
8
9 AdwViewStack *animation_preferences_stack;
10 AdwAnimation *timed_animation;
11 GtkWidget *timed_animation_sample;
12 GtkWidget *timed_animation_button_box;
13 AdwSpinRow *timed_animation_repeat_count;
14 AdwSwitchRow *timed_animation_reverse;
15 AdwSwitchRow *timed_animation_alternate;
16 AdwSpinRow *timed_animation_duration;
17 AdwComboRow *timed_animation_easing;
18 AdwAnimation *spring_animation;
19 AdwSpinRow *spring_animation_velocity;
20 AdwSpinRow *spring_animation_damping;
21 AdwSpinRow *spring_animation_mass;
22 AdwSpinRow *spring_animation_stiffness;
23 AdwSpinRow *spring_animation_epsilon;
24 AdwSwitchRow *spring_animation_clamp_switch;
25 };
26
27 enum {
28 PROP_0,
29 PROP_TIMED_ANIMATION,
30 PROP_SPRING_ANIMATION,
31 LAST_PROP,
32 };
33
34 static GParamSpec *props[LAST_PROP];
35
36 G_DEFINE_FINAL_TYPE (AdwDemoPageAnimations, adw_demo_page_animations, ADW_TYPE_BIN)
37
38 static AdwAnimation *
39 get_current_animation (AdwDemoPageAnimations *self)
40 {
41 const char *current_animation;
42
43 current_animation = adw_view_stack_get_visible_child_name (self->animation_preferences_stack);
44
45 if (!g_strcmp0 (current_animation, "Timed")) {
46 return self->timed_animation;
47 } else if (!g_strcmp0 (current_animation, "Spring")) {
48 return self->spring_animation;
49 } else {
50 g_assert_not_reached ();
51 }
52 }
53
54 static char *
55 animations_easing_name (AdwEnumListItem *value,
56 gpointer user_data)
57 {
58 switch (adw_enum_list_item_get_value (value)) {
59 case ADW_LINEAR:
60 return g_strdup (_("Linear"));
61 case ADW_EASE_IN_QUAD:
62 return g_strdup (_("Ease-in (Quadratic)"));
63 case ADW_EASE_OUT_QUAD:
64 return g_strdup (_("Ease-out (Quadratic)"));
65 case ADW_EASE_IN_OUT_QUAD:
66 return g_strdup (_("Ease-in-out (Quadratic)"));
67 case ADW_EASE_IN_CUBIC:
68 return g_strdup (_("Ease-in (Cubic)"));
69 case ADW_EASE_OUT_CUBIC:
70 return g_strdup (_("Ease-out (Cubic)"));
71 case ADW_EASE_IN_OUT_CUBIC:
72 return g_strdup (_("Ease-in-out (Cubic)"));
73 case ADW_EASE_IN_QUART:
74 return g_strdup (_("Ease-in (Quartic)"));
75 case ADW_EASE_OUT_QUART:
76 return g_strdup (_("Ease-out (Quartic)"));
77 case ADW_EASE_IN_OUT_QUART:
78 return g_strdup (_("Ease-in-out (Quartic)"));
79 case ADW_EASE_IN_QUINT:
80 return g_strdup (_("Ease-in (Quintic)"));
81 case ADW_EASE_OUT_QUINT:
82 return g_strdup (_("Ease-out (Quintic)"));
83 case ADW_EASE_IN_OUT_QUINT:
84 return g_strdup (_("Ease-in-out (Quintic)"));
85 case ADW_EASE_IN_SINE:
86 return g_strdup (_("Ease-in (Sine)"));
87 case ADW_EASE_OUT_SINE:
88 return g_strdup (_("Ease-out (Sine)"));
89 case ADW_EASE_IN_OUT_SINE:
90 return g_strdup (_("Ease-in-out (Sine)"));
91 case ADW_EASE_IN_EXPO:
92 return g_strdup (_("Ease-in (Exponential)"));
93 case ADW_EASE_OUT_EXPO:
94 return g_strdup (_("Ease-out (Exponential)"));
95 case ADW_EASE_IN_OUT_EXPO:
96 return g_strdup (_("Ease-in-out (Exponential)"));
97 case ADW_EASE_IN_CIRC:
98 return g_strdup (_("Ease-in (Circular)"));
99 case ADW_EASE_OUT_CIRC:
100 return g_strdup (_("Ease-out (Circular)"));
101 case ADW_EASE_IN_OUT_CIRC:
102 return g_strdup (_("Ease-in-out (Circular)"));
103 case ADW_EASE_IN_ELASTIC:
104 return g_strdup (_("Ease-in (Elastic)"));
105 case ADW_EASE_OUT_ELASTIC:
106 return g_strdup (_("Ease-out (Elastic)"));
107 case ADW_EASE_IN_OUT_ELASTIC:
108 return g_strdup (_("Ease-in-out (Elastic)"));
109 case ADW_EASE_IN_BACK:
110 return g_strdup (_("Ease-in (Back)"));
111 case ADW_EASE_OUT_BACK:
112 return g_strdup (_("Ease-out (Back)"));
113 case ADW_EASE_IN_OUT_BACK:
114 return g_strdup (_("Ease-in-out (Back)"));
115 case ADW_EASE_IN_BOUNCE:
116 return g_strdup (_("Ease-in (Bounce)"));
117 case ADW_EASE_OUT_BOUNCE:
118 return g_strdup (_("Ease-out (Bounce)"));
119 case ADW_EASE_IN_OUT_BOUNCE:
120 return g_strdup (_("Ease-in-out (Bounce)"));
121 case ADW_EASE:
122 return g_strdup (_("Ease"));
123 case ADW_EASE_IN:
124 return g_strdup (_("Ease-in"));
125 case ADW_EASE_OUT:
126 return g_strdup (_("Ease-out"));
127 case ADW_EASE_IN_OUT:
128 return g_strdup (_("Ease-in-out"));
129 default:
130 return NULL;
131 }
132 }
133
134 static void
135 timed_animation_measure (GtkWidget *widget,
136 GtkOrientation orientation,
137 int for_size,
138 int *minimum,
139 int *natural,
140 int *minimum_baseline,
141 int *natural_baseline)
142 {
143 GtkWidget *child = gtk_widget_get_first_child (widget);
144
145 if (!child)
146 return;
147
148 gtk_widget_measure (child, orientation, for_size, minimum, natural,
149 minimum_baseline, natural_baseline);
150 }
151
152 static void
153 timed_animation_allocate (GtkWidget *widget,
154 int width,
155 int height,
156 int baseline)
157 {
158 AdwDemoPageAnimations *self =
159 ADW_DEMO_PAGE_ANIMATIONS (gtk_widget_get_ancestor (widget, ADW_TYPE_DEMO_PAGE_ANIMATIONS));
160 GtkWidget *child = gtk_widget_get_first_child (widget);
161 AdwAnimation *animation = get_current_animation (self);
162 double progress;
163 int child_width, offset;
164
165 if (!child)
166 return;
167
168 progress = adw_animation_get_value (animation);
169
170 gtk_widget_measure (child, GTK_ORIENTATION_HORIZONTAL, -1,
171 &child_width, NULL, NULL, NULL);
172
173 offset = (int) ((width - child_width) * (progress - 0.5));
174
175 gtk_widget_allocate (child, width, height, baseline,
176 gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (offset, 0)));
177 }
178
179 static void
180 timed_animation_reset (AdwDemoPageAnimations *self)
181 {
182 adw_animation_reset (self->timed_animation);
183 adw_animation_reset (self->spring_animation);
184 }
185
186 static void
187 timed_animation_play_pause (AdwDemoPageAnimations *self)
188 {
189 AdwAnimation *animation = get_current_animation (self);
190
191 switch (adw_animation_get_state (animation)) {
192 case ADW_ANIMATION_IDLE:
193 case ADW_ANIMATION_FINISHED:
194 adw_animation_play (animation);
195 break;
196 case ADW_ANIMATION_PAUSED:
197 adw_animation_resume (animation);
198 break;
199 case ADW_ANIMATION_PLAYING:
200 adw_animation_pause (animation);
201 break;
202 default:
203 g_assert_not_reached ();
204 }
205 }
206
207 static void
208 timed_animation_skip (AdwDemoPageAnimations *self)
209 {
210 adw_animation_skip (self->timed_animation);
211 adw_animation_skip (self->spring_animation);
212 }
213
214 static char *
215 get_play_pause_icon_name (gpointer user_data,
216 AdwAnimationState timed_state,
217 AdwAnimationState spring_state)
218 {
219 gboolean playing = timed_state == ADW_ANIMATION_PLAYING ||
220 spring_state == ADW_ANIMATION_PLAYING;
221
222 return g_strdup (playing ? "media-playback-pause-symbolic" : "media-playback-start-symbolic");
223 }
224
225 static gboolean
226 timed_animation_can_reset (gpointer user_data,
227 AdwAnimationState timed_state,
228 AdwAnimationState spring_state)
229 {
230 return timed_state != ADW_ANIMATION_IDLE ||
231 spring_state != ADW_ANIMATION_IDLE;
232 }
233
234 static gboolean
235 timed_animation_can_skip (gpointer user_data,
236 AdwAnimationState timed_state,
237 AdwAnimationState spring_state)
238 {
239 return timed_state != ADW_ANIMATION_FINISHED &&
240 spring_state != ADW_ANIMATION_FINISHED;
241 }
242
243 static void
244 timed_animation_cb (double value,
245 GtkWidget *self)
246 {
247 gtk_widget_queue_allocate (self);
248 }
249
250 static void
251 notify_spring_params_change (AdwDemoPageAnimations *self)
252 {
253 AdwSpringParams *spring_params =
254 adw_spring_params_new_full (adw_spin_row_get_value (self->spring_animation_damping),
255 adw_spin_row_get_value (self->spring_animation_mass),
256 adw_spin_row_get_value (self->spring_animation_stiffness));
257
258 adw_spring_animation_set_spring_params (ADW_SPRING_ANIMATION (self->spring_animation), spring_params);
259
260 adw_spring_params_unref (spring_params);
261 }
262
263 static void
264 adw_demo_page_animations_get_property (GObject *object,
265 guint prop_id,
266 GValue *value,
267 GParamSpec *pspec)
268 {
269 AdwDemoPageAnimations *self = ADW_DEMO_PAGE_ANIMATIONS (object);
270
271 switch (prop_id) {
272 case PROP_TIMED_ANIMATION:
273 g_value_set_object (value, self->timed_animation);
274 break;
275
276 case PROP_SPRING_ANIMATION:
277 g_value_set_object (value, self->spring_animation);
278 break;
279
280 default:
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 }
283 }
284
285 static void
286 adw_demo_page_animations_set_property (GObject *object,
287 guint prop_id,
288 const GValue *value,
289 GParamSpec *pspec)
290 {
291 AdwDemoPageAnimations *self = ADW_DEMO_PAGE_ANIMATIONS (object);
292
293 switch (prop_id) {
294 case PROP_TIMED_ANIMATION:
295 g_set_object (&self->timed_animation, g_value_get_object (value));
296 break;
297
298 case PROP_SPRING_ANIMATION:
299 g_set_object (&self->spring_animation, g_value_get_object (value));
300 break;
301
302 default:
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 }
305 }
306
307 static void
308 adw_demo_page_animations_dispose (GObject *object)
309 {
310 AdwDemoPageAnimations *self = ADW_DEMO_PAGE_ANIMATIONS (object);
311
312 g_clear_object (&self->timed_animation);
313 g_clear_object (&self->spring_animation);
314
315 G_OBJECT_CLASS (adw_demo_page_animations_parent_class)->dispose (object);
316 }
317
318 static void
319 adw_demo_page_animations_class_init (AdwDemoPageAnimationsClass *klass)
320 {
321 GObjectClass *object_class = G_OBJECT_CLASS (klass);
322 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
323
324 object_class->set_property = adw_demo_page_animations_set_property;
325 object_class->get_property = adw_demo_page_animations_get_property;
326 object_class->dispose = adw_demo_page_animations_dispose;
327
328 props[PROP_TIMED_ANIMATION] =
329 g_param_spec_object ("timed-animation", NULL, NULL,
330 ADW_TYPE_ANIMATION,
331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
332
333 props[PROP_SPRING_ANIMATION] =
334 g_param_spec_object ("spring-animation", NULL, NULL,
335 ADW_TYPE_ANIMATION,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
337
338 g_object_class_install_properties (object_class, LAST_PROP, props);
339
340 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Adwaita1/Demo/ui/pages/animations/adw-demo-page-animations.ui");
341 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, animation_preferences_stack);
342 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_sample);
343 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_button_box);
344 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_repeat_count);
345 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_reverse);
346 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_alternate);
347 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_duration);
348 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, timed_animation_easing);
349 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, spring_animation_velocity);
350 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, spring_animation_damping);
351 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, spring_animation_mass);
352 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, spring_animation_stiffness);
353 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, spring_animation_epsilon);
354 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageAnimations, spring_animation_clamp_switch);
355 gtk_widget_class_bind_template_callback (widget_class, animations_easing_name);
356 gtk_widget_class_bind_template_callback (widget_class, timed_animation_reset);
357 gtk_widget_class_bind_template_callback (widget_class, timed_animation_play_pause);
358 gtk_widget_class_bind_template_callback (widget_class, timed_animation_skip);
359 gtk_widget_class_bind_template_callback (widget_class, get_play_pause_icon_name);
360 gtk_widget_class_bind_template_callback (widget_class, timed_animation_can_reset);
361 gtk_widget_class_bind_template_callback (widget_class, timed_animation_can_skip);
362 gtk_widget_class_bind_template_callback (widget_class, notify_spring_params_change);
363 }
364
365 static void
366 adw_demo_page_animations_init (AdwDemoPageAnimations *self)
367 {
368 GtkLayoutManager *manager;
369 AdwAnimationTarget *target;
370
371 gtk_widget_init_template (GTK_WIDGET (self));
372
373 target = adw_callback_animation_target_new ((AdwAnimationTargetFunc)
374 timed_animation_cb,
375 self->timed_animation_sample,
376 NULL);
377
378 self->timed_animation =
379 adw_timed_animation_new (GTK_WIDGET (self->timed_animation_sample),
380 0, 1, 100, g_object_ref (target));
381
382 self->spring_animation =
383 adw_spring_animation_new (GTK_WIDGET (self->timed_animation_sample), 0, 1,
384 adw_spring_params_new_full (10, 1, 100),
385 target);
386
387 notify_spring_params_change (self);
388
389 g_object_bind_property (self->timed_animation_repeat_count, "value",
390 self->timed_animation, "repeat-count",
391 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
392 g_object_bind_property (self->timed_animation_reverse, "active",
393 self->timed_animation, "reverse",
394 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
395 g_object_bind_property (self->timed_animation_alternate, "active",
396 self->timed_animation, "alternate",
397 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
398 g_object_bind_property (self->timed_animation_duration, "value",
399 self->timed_animation, "duration",
400 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
401 g_object_bind_property (self->timed_animation_easing, "selected",
402 self->timed_animation, "easing",
403 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
404
405 g_object_bind_property (self->spring_animation_velocity, "value",
406 self->spring_animation, "initial_velocity",
407 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
408 g_object_bind_property (self->spring_animation_epsilon, "value",
409 self->spring_animation, "epsilon",
410 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
411 g_object_bind_property (self->spring_animation_clamp_switch, "active",
412 self->spring_animation, "clamp",
413 G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
414
415 adw_timed_animation_set_easing (ADW_TIMED_ANIMATION (self->timed_animation),
416 ADW_EASE_IN_OUT_CUBIC);
417
418 adw_animation_set_follow_enable_animations_setting (self->timed_animation, FALSE);
419 adw_animation_set_follow_enable_animations_setting (self->spring_animation, FALSE);
420
421 g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TIMED_ANIMATION]);
422 g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SPRING_ANIMATION]);
423
424 manager = gtk_custom_layout_new (NULL, timed_animation_measure,
425 timed_animation_allocate);
426
427 gtk_widget_set_layout_manager (self->timed_animation_sample, manager);
428
429 gtk_widget_set_direction (self->timed_animation_button_box, GTK_TEXT_DIR_LTR);
430 }