aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-03-15 00:35:34 +0100
committerFederico Angelilli <code@fedang.net>2024-03-15 00:35:34 +0100
commit74a29b60cdcff2906db89707c1059ee1cc840e49 (patch)
tree402e7989d41205e2e7c3dda3928b1d193fc72367
parent430def6e39315b6cee807aba3e4126134b93b164 (diff)
Move animation removal to handler
-rw-r--r--src/draw.c39
-rw-r--r--src/state.c26
2 files changed, 35 insertions, 30 deletions
diff --git a/src/draw.c b/src/draw.c
index 1a8fe5c..873d08e 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -67,12 +67,13 @@ void draw_paint(Drawer *draw, Window *win)
for (GList *it = draw->layouts; it; it = it->next) {
Layout *layout = it->data;
-
- int radius = layout->height / 2;
+ Button *btn = layout->btn;
cairo_push_group(cr);
- Color color = layout->btn->color;
+ int radius = layout->height / 2;
+
+ Color color = btn->color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_new_sub_path(cr);
cairo_arc(cr, layout->x + radius, layout->y + radius, radius, 90 * degree, 270 * degree);
@@ -80,7 +81,7 @@ void draw_paint(Drawer *draw, Window *win)
cairo_close_path(cr);
cairo_fill(cr);
- color = layout->btn->line_color;
+ color = btn->line_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_new_sub_path(cr);
cairo_arc(cr, layout->x + radius, layout->y + radius, radius - line_w , 90 * degree, 270 * degree);
@@ -88,37 +89,29 @@ void draw_paint(Drawer *draw, Window *win)
cairo_close_path(cr);
cairo_stroke(cr);
- if (layout->btn->simple) {
+ if (btn->simple) {
int text_x = layout->x + (layout->width / 2) - (layout->text_w / 2);
int text_y = layout->y + (layout->height / 2) - (layout->text_h / 2);
- color = CAST(layout->btn, ButtonSimple)->text_color;
+ color = CAST(btn, ButtonSimple)->text_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_move_to(cr, text_x, text_y);
// NOTE: This works only if the text didn't change in size after the layouting
- pango_layout_set_text(layout->pl, CAST(layout->btn, ButtonSimple)->text, -1);
+ pango_layout_set_text(layout->pl, CAST(btn, ButtonSimple)->text, -1);
pango_cairo_update_layout(cr, layout->pl);
pango_cairo_show_layout(cr, layout->pl);
}
- if (layout->btn->anim != NULL) {
- if (layout->btn->anim->paint_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 (!layout->btn->anim->paint_func(layout->btn->anim, cr, layout))
- 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;
- log_debug("Removing animation [button=%p]", layout->btn);
+ if (btn->anim != NULL && btn->anim->paint_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->paint_func(btn->anim, cr, layout))
+ btn->anim->paint_func = NULL;
}
cairo_pop_group_to_source(cr);
diff --git a/src/state.c b/src/state.c
index f35faa2..d9ecaeb 100644
--- a/src/state.c
+++ b/src/state.c
@@ -67,16 +67,29 @@ void state_request_redraw(State *state, bool relayout)
static gboolean anim_handler(State *state)
{
- bool anim = false;
+ bool done = true;
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;
+ bool draw = btn->anim->paint_func != NULL;
+ bool layout = btn->anim->layout_func != NULL;
+
+ // Remove animation if both paint_func and layout_func finished
+ if (!draw && !layout) {
+ log_debug("Removing animation [type=%d, end=%ld, button=%p]",
+ btn->anim->type, btn->anim->start + btn->anim->duration, btn);
+
+ animation_destroy(btn->anim);
+ btn->anim = NULL;
+ } else {
+ done = false;
+ state->relayout |= layout;
+ }
}
}
- log_debug("Animating [relayout=%d]", state->relayout);
+ log_debug("Redrawing animation [relayout=%d, done=%d]", state->relayout, done);
if (state->relayout) {
draw_compute_layout(state->draw, state->win, state->btns);
@@ -84,10 +97,9 @@ static gboolean anim_handler(State *state)
}
draw_paint(state->draw, state->win);
- if (anim) return G_SOURCE_CONTINUE;
- state->anim_id = 0;
- return G_SOURCE_REMOVE;
+ if (done) state->anim_id = 0;
+ return !done;
}
void state_request_animation(State *state)