From abacdaccb6a1ef55bd85b66e10caaa217458400b Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 17 Mar 2024 14:55:36 +0100 Subject: Add group shrink animation --- src/animate.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/animate.c') diff --git a/src/animate.c b/src/animate.c index 8bd09b5..313cfa7 100644 --- a/src/animate.c +++ b/src/animate.c @@ -129,8 +129,54 @@ Animation *animation_pulse_create(gint64 duration) pulse->type = ANIM_PULSE; pulse->before_func = (DrawFunc)pulse_func; pulse->duration = duration; + return pulse; +} + +static bool shrink_func(Animation *shrink, Layout *layout) +{ + g_assert_false(layout->btn->simple); + ButtonGroup *group = (gpointer)layout->btn; + + gint64 end = shrink->start + shrink->duration; + gint64 now = g_get_monotonic_time(); + + if (now > end) { + // NOTE: Actually remove the buttons from the group after the animation's end + // This part is quite rough around the edges... + if (group->children->next) { + g_list_free(g_list_remove_link(group->children, group->children)); + g_list_free(g_list_remove_link(layout->children, layout->children)); + layout->width = CAST(layout->children->data, Layout)->width; + return true; + } + + return false; + } + + double t = (double)(now - shrink->start) / (end - shrink->start); + double pos = smoothstep(t, 0, 1); - return (gpointer)pulse; + int target_w = CAST(layout->children->data, Layout)->width; + int width = layout->width - pos * (layout->width - target_w); + layout->width = width; + + for (GList *it = layout->children, *next = it->next; it; it = next, next = it ? it->next : NULL) { + Layout *child = it->data; + if (child->x + child->width > layout->x + width) + layout->children = g_list_delete_link(layout->children, it); + } + + return true; +} + +Animation *animation_group_shrink_create(gint64 duration) +{ + // Note the 0 initialization + Animation *shrink = g_malloc0(sizeof(Animation)); + shrink->type = ANIM_GROUP_SHRINK; + shrink->layout_func = (LayoutFunc)shrink_func; + shrink->duration = duration; + return shrink; } void animation_destroy(Animation *anim) -- cgit v1.2.3