From 430def6e39315b6cee807aba3e4126134b93b164 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Fri, 15 Mar 2024 00:21:01 +0100 Subject: Fix layout animation and animation handler --- src/animate.c | 2 +- src/connect.c | 18 +++++++++++------- src/draw.c | 26 ++++++++++++++------------ src/state.c | 18 +++++++++++------- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/animate.c b/src/animate.c index 73ca0eb..9928daa 100644 --- a/src/animate.c +++ b/src/animate.c @@ -48,7 +48,7 @@ typedef struct { } AnimationShine; // FIXME: Not working for button group -bool shine_paint(AnimationShine *shine, cairo_t *cr, const Layout *layout) +static bool shine_paint(AnimationShine *shine, cairo_t *cr, const Layout *layout) { gint64 end = shine->anim.start + shine->anim.duration; gint64 now = g_get_monotonic_time(); diff --git a/src/connect.c b/src/connect.c index 71552da..f6c439b 100644 --- a/src/connect.c +++ b/src/connect.c @@ -93,20 +93,24 @@ static void button_action(State *state, const char *event, int x, int y) layout->x, layout->y, layout->width, layout->height); if (in_capsule(x, y, layout->x, layout->y, layout->width, layout->height)) { - log_debug("Triggering action for button"); - ButtonAction action = button_simple_get_action(layout->btn); + Button *btn = layout->btn; + ButtonAction action = button_simple_get_action(btn); + + // TODO: Button labels + log_debug("Triggering action for button [button=%p]", btn); if (action != NULL) { - Animation *shine = animation_shine_create(300 * G_TIME_SPAN_MILLISECOND); + // NOTE: Animations may change layout! + //Animation *anim = animation_shine_create(300 * G_TIME_SPAN_MILLISECOND); + Animation *anim = animation_pulse_create(1000 * G_TIME_SPAN_MILLISECOND); - if (button_set_animation(layout->btn, shine)) + if (button_set_animation(btn, anim)) state_request_animation(state); else - animation_destroy(shine); + animation_destroy(anim); - action(layout->btn); + action(btn); } - return; } } else { diff --git a/src/draw.c b/src/draw.c index 5171321..1a8fe5c 100644 --- a/src/draw.c +++ b/src/draw.c @@ -113,7 +113,7 @@ void draw_paint(Drawer *draw, Window *win) if (!layout->btn->anim->paint_func(layout->btn->anim, cr, layout)) layout->btn->anim->paint_func = NULL; - } else if (layout->btn->anim->paint_func == NULL) { + } else if (layout->btn->anim->layout_func == NULL) { // Remove animation if both paint_func and layout_func finished animation_destroy(layout->btn->anim); layout->btn->anim = NULL; @@ -245,20 +245,22 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns) } } - if (btn->anim != NULL && btn->anim->layout_func != NULL) { - if (layout->btn->anim->start == 0) { - layout->btn->anim->start = g_get_monotonic_time(); - log_debug("Starting animation [start=%ld, duration=%ld, button=%p]", - layout->btn->anim->start, layout->btn->anim->duration, layout->btn); - } - - if (!btn->anim->layout_func(btn->anim, layout)) - btn->anim->layout_func = NULL; - } - if (layout->btn->simple) { layout->pl = pango_cairo_create_layout(window_get_context(win)); layout_text(layout, draw->desc, height, radius); + + // TODO: make it work for groups + if (btn->anim != NULL && btn->anim->layout_func != NULL) { + if (btn->anim->start == 0) { + btn->anim->start = g_get_monotonic_time(); + log_debug("Starting animation [type=%d, start=%ld, duration=%ld, button=%p]", + btn->anim->type, btn->anim->start, btn->anim->duration, btn); + } + + if (!btn->anim->layout_func(btn->anim, layout)) + btn->anim->layout_func = NULL; + } + x += layout->width + line_w * 2; layout->width -= 2 * btn->padding; diff --git a/src/state.c b/src/state.c index 0f17695..f35faa2 100644 --- a/src/state.c +++ b/src/state.c @@ -67,7 +67,16 @@ void state_request_redraw(State *state, bool relayout) static gboolean anim_handler(State *state) { - //log_debug("Animating [relayout=%d]", state->relayout); + bool anim = false; + for (GList *it = state->btns; it != NULL; it = it->next) { + Button *btn = it->data; + if (btn->anim != NULL) { + anim = true; + state->relayout |= btn->anim->layout_func != NULL; + } + } + + log_debug("Animating [relayout=%d]", state->relayout); if (state->relayout) { draw_compute_layout(state->draw, state->win, state->btns); @@ -75,12 +84,7 @@ static gboolean anim_handler(State *state) } draw_paint(state->draw, state->win); - - for (GList *it = state->btns; it != NULL; it = it->next) { - Button *btn = it->data; - if (btn->anim != NULL) - return G_SOURCE_CONTINUE; - } + if (anim) return G_SOURCE_CONTINUE; state->anim_id = 0; return G_SOURCE_REMOVE; -- cgit v1.2.3