diff options
| author | Federico Angelilli <code@fedang.net> | 2024-03-15 21:49:36 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-03-15 21:49:36 +0100 |
| commit | bccbbedf91365a4aa6e23958d9f1713723a1f1bd (patch) | |
| tree | db96b0898e84164d4805dd4e56dde3c3ce386092 | |
| parent | 056dfd80b930abe56e151d4ebafae248fb0fa5a0 (diff) | |
Make button click detection work with the new layouts
| -rw-r--r-- | src/connect.c | 72 | ||||
| -rw-r--r-- | src/draw.c | 3 |
2 files changed, 46 insertions, 29 deletions
diff --git a/src/connect.c b/src/connect.c index 97deca5..8e2578c 100644 --- a/src/connect.c +++ b/src/connect.c @@ -77,49 +77,63 @@ static inline bool in_capsule(int px, int py, int x, int y, int w, int h) || in_rect(px, py, x + radius, y, w - 2 * radius, h); } -static void button_action(State *state, const char *event, int x, int y) +static bool button_action_find(State *state, GList *layouts, int x, int y) { - log_debug("Checking %s event [x=%d, y=%d]", event, x, y); - - for (GList *it = state->draw->layouts; it != NULL; it = it->next) { - Layout *layout = it->data; + for (GList *it = layouts; it; it = it->next) { + const Layout *layout = it->data; // Skip if (layout->x + layout->width < x) continue; if (layout->x > x) break; - if (layout->btn->simple) { - log_debug("Button layout [x=%d, y=%d, w=%d, h=%d]", - layout->x, layout->y, layout->width, layout->height); + // Bound check click coordinates + int w = layout->width - 2 * layout->x_pad; + int h = layout->height - 2 * layout->y_pad; - if (in_capsule(x, y, layout->x, layout->y, layout->width, layout->height)) { - Button *btn = layout->btn; - ButtonAction action = button_simple_get_action(btn); + if (!in_capsule(x, y, layout->x + layout->x_pad, layout->y + layout->y_pad, w, h)) + continue; - // TODO: Button labels - log_debug("Triggering action for button [button=%p]", btn); + Button *btn = layout->btn; + if (!layout->btn->simple) { + log_debug("Button group layout [x=%d, y=%d, w=%d, h=%d, button=%p]", + layout->x, layout->y, layout->width, layout->height, btn); - if (action != NULL) { - // NOTE: Animations may change layout! - //Animation *anim = animation_shine_create(300 * G_TIME_SPAN_MILLISECOND); - Animation *anim = animation_pulse_create(200 * G_TIME_SPAN_MILLISECOND); + // Check the group children + return button_action_find(state, layout->children, x, y); + } - if (button_set_animation(btn, anim)) - state_request_animation(state); - else - animation_destroy(anim); + log_debug("Button layout [x=%d, y=%d, w=%d, h=%d, button=%p]", + layout->x, layout->y, layout->width, layout->height, btn); - action(btn); - } - return; - } - } else { - log_debug("Button group layout [x=%d, y=%d, w=%d, h=%d]", - layout->x, layout->y, layout->width, layout->height); + ButtonAction action = button_simple_get_action(btn); + if (action == NULL) { + // TODO: Button labels + log_debug("Ignoring button without action [button=%p]", btn); + return true; } + + // NOTE: Animations may change the layouts! + //Animation *anim = animation_shine_create(300 * G_TIME_SPAN_MILLISECOND); + Animation *anim = animation_pulse_create(200 * G_TIME_SPAN_MILLISECOND); + + if (button_set_animation(btn, anim)) + state_request_animation(state); + else + animation_destroy(anim); + + log_info("Triggering action for button [text=%s, button=%p]", button_simple_get_text(btn), btn); + action(btn); + return true; } - log_debug("Ignoring %s", event); + return false; +} + +static void button_action(State *state, const char *event, int x, int y) +{ + log_debug("Checking %s event [x=%d, y=%d]", event, x, y); + if (!button_action_find(state, state->draw->layouts, x, y)) + log_debug("Ignoring %s", event); } typedef struct { @@ -265,6 +265,9 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns) layout->width = x - layout->x; layout->height = height; + // Otherwise button_action_find will not work! + layout->children = g_list_reverse(layout->children); + // FIXME: Temporary solution to make the antialiasing (or the circles not overlapping correctly) problem less noticeable bool fix_left = CAST(g_list_first(group->children)->data, Button)->x_pad == 0; |
