diff options
| author | Federico Angelilli <code@fedang.net> | 2024-03-17 17:00:27 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-03-17 17:00:27 +0100 |
| commit | 635b960629ef17daed73d742fb6cb78ea6b1d4a7 (patch) | |
| tree | 2e2806b1ee2c61c65960d0aa048bfb7bc75bb4af /src | |
| parent | 44a9ccb0ca18314016ef10b4b1b1c7c958d9eabc (diff) | |
Fix memory leaks and refactor menu button
Diffstat (limited to 'src')
| -rw-r--r-- | src/animate.c | 14 | ||||
| -rw-r--r-- | src/comet.c | 28 | ||||
| -rw-r--r-- | src/draw.c | 2 | ||||
| -rw-r--r-- | src/draw.h | 2 |
4 files changed, 30 insertions, 16 deletions
diff --git a/src/animate.c b/src/animate.c index 387c9b8..2f4be8d 100644 --- a/src/animate.c +++ b/src/animate.c @@ -145,7 +145,7 @@ static bool shrink_func(Animation *shrink, Layout *layout) // 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)); + g_list_free_full(g_list_remove_link(layout->children, layout->children), (GDestroyNotify)layout_destroy); layout->width = CAST(layout->children->data, Layout)->width; return true; } @@ -162,8 +162,10 @@ static bool shrink_func(Animation *shrink, Layout *layout) 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); + if (child->x + child->width <= layout->x + width) continue; + + layout_destroy(it->data); + layout->children = g_list_delete_link(layout->children, it); } return true; @@ -197,8 +199,10 @@ static bool grow_func(Animation *grow, Layout *layout) 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); + if (child->x + child->width <= layout->x + width) continue; + + layout_destroy(it->data); + layout->children = g_list_delete_link(layout->children, it); } return true; diff --git a/src/comet.c b/src/comet.c index b6c40fd..fd50aeb 100644 --- a/src/comet.c +++ b/src/comet.c @@ -199,18 +199,18 @@ static void menu_action(Button *btn) ButtonGroup *group; char *strings[2]; GList *toggled; + bool closed; } *menu_ctx = CAST(btn, ButtonSimple)->data; - bool to_open = menu_ctx->group->children->next == NULL; - - if (to_open) + if (menu_ctx->closed) { + g_list_free(menu_ctx->group->children); menu_ctx->group->children = g_list_copy(menu_ctx->toggled); + } - log_debug("%s menu", to_open ? "Opened" : "Closed"); - - button_simple_set_text(btn, g_strdup(menu_ctx->strings[to_open]), CAST(btn, ButtonSimple)->text_color); + char *text = g_strdup(menu_ctx->strings[menu_ctx->closed]); + button_simple_set_text(btn, text, CAST(btn, ButtonSimple)->text_color); - Animation *anim = to_open + Animation *anim = menu_ctx->closed ? animation_group_grow_create(MILLIS(600)) : animation_group_shrink_create(MILLIS(600)); @@ -218,6 +218,9 @@ static void menu_action(Button *btn) state_request_animation(menu_ctx->state); else animation_destroy(anim); + + log_debug("%s menu", menu_ctx->closed ? "Opened" : "Closed"); + menu_ctx->closed = !menu_ctx->closed; } static void register_buttons(State *state, Color color, Color text_color, Color line_color, int line_w) @@ -371,7 +374,8 @@ int main(int argc, char **argv) ButtonGroup *group; char *strings[2]; GList *toggled; - } menu_ctx = { state, (gpointer)group, {"", ""}, NULL }; + bool closed; + } menu_ctx = { state, (gpointer)group, {"", ""}, NULL, true }; Button *menu = button_simple_create(PANGO_ALIGN_LEFT, color); button_simple_set_text(menu, g_strdup(menu_ctx.strings[0]), text_color); @@ -407,9 +411,13 @@ int main(int argc, char **argv) g_source_remove(source_int); // NOTE: Skip the first element (the open/close button) - //g_list_free_full(menu_ctx.toggled->next, (GDestroyNotify)button_destroy); - //g_free(menu_ctx.toggled); + if (menu_ctx.closed) + g_list_free_full(g_list_delete_link(menu_ctx.toggled, menu_ctx.toggled), (GDestroyNotify)button_destroy); + else + g_list_free(menu_ctx.toggled); + timer_delete(date_ctx.timer); + g_object_unref(context); // NOTE: Buttons are freed by state_destroy dwm_destroy(dwm); @@ -138,7 +138,7 @@ void draw_paint(Drawer *draw, Window *win) cairo_surface_destroy(surface); } -static void layout_destroy(Layout *layout) +void layout_destroy(Layout *layout) { if (layout->pl != NULL) g_object_unref(layout->pl); @@ -45,6 +45,8 @@ Drawer *draw_create(); void draw_paint(Drawer *draw, Window *win); +void layout_destroy(Layout *layout); + // TODO: Rework the api so that we don't need to pass the window void draw_compute_layout(Drawer *draw, Window *win, GList *btns); |
