]> git.mdlowis.com Git - proto/labwc.git/commitdiff
refactor ssd_titlebar_create
authorTobias Bengfort <tobias.bengfort@posteo.de>
Sun, 18 Aug 2024 08:20:24 +0000 (10:20 +0200)
committerTobias Bengfort <tobias.bengfort@posteo.de>
Tue, 20 Aug 2024 21:04:10 +0000 (23:04 +0200)
src/ssd/ssd-titlebar.c

index 7326a707986759f73add6a09d9a74b59e76d39be..292810bdffca82b1cdf57f9c40e383a523961633 100644 (file)
 static void set_squared_corners(struct ssd *ssd, bool enable);
 static void set_maximize_alt_icon(struct ssd *ssd, bool enable);
 
+static void
+add_button(struct ssd *ssd, struct ssd_sub_tree *subtree, enum ssd_part_type type, int x)
+{
+       struct view *view = ssd->view;
+       struct theme *theme = view->server->theme;
+       struct wlr_scene_tree *parent = subtree->tree;
+       bool active = subtree == &ssd->titlebar.active;
+
+       struct ssd_part *btn_max_root;
+       struct ssd_button *btn_max;
+
+       switch (type) {
+       case LAB_SSD_BUTTON_WINDOW_MENU:
+               add_scene_button(&subtree->parts, type, parent,
+                       active ? &theme->button_menu_active_unpressed->base
+                               : &theme->button_menu_inactive_unpressed->base,
+                       active ? &theme->button_menu_active_hover->base
+                               : &theme->button_menu_inactive_hover->base,
+                       x, view);
+               break;
+       case LAB_SSD_BUTTON_ICONIFY:
+               add_scene_button(&subtree->parts, type, parent,
+                       active ? &theme->button_iconify_active_unpressed->base
+                               : &theme->button_iconify_inactive_unpressed->base,
+                       active ? &theme->button_iconify_active_hover->base
+                               : &theme->button_iconify_inactive_hover->base,
+                       x, view);
+               break;
+       case LAB_SSD_BUTTON_MAXIMIZE:
+               /* Maximize button has an alternate state when maximized */
+               btn_max_root = add_scene_button(&subtree->parts, type, parent,
+                       active ? &theme->button_maximize_active_unpressed->base
+                               : &theme->button_maximize_inactive_unpressed->base,
+                       active ? &theme->button_maximize_active_hover->base
+                               : &theme->button_maximize_inactive_hover->base,
+                       x, view);
+               btn_max = node_ssd_button_from_node(btn_max_root->node);
+               add_toggled_icon(btn_max, &subtree->parts, LAB_SSD_BUTTON_MAXIMIZE,
+                       active ? &theme->button_restore_active_unpressed->base
+                               : &theme->button_restore_inactive_unpressed->base,
+                       active ? &theme->button_restore_active_hover->base
+                               : &theme->button_restore_inactive_hover->base);
+               break;
+       case LAB_SSD_BUTTON_CLOSE:
+               add_scene_button(&subtree->parts, type, parent,
+                       active ? &theme->button_close_active_unpressed->base
+                               : &theme->button_close_inactive_unpressed->base,
+                       active ? &theme->button_close_active_hover->base
+                               : &theme->button_close_inactive_hover->base,
+                       x, view);
+               break;
+       default:
+               assert(false && "invalid titlebar part");
+               wlr_log(WLR_ERROR, "invalid titlebar type");
+               abort();
+       }
+}
+
 void
 ssd_titlebar_create(struct ssd *ssd)
 {
@@ -33,18 +91,6 @@ ssd_titlebar_create(struct ssd *ssd)
        struct wlr_buffer *corner_top_left;
        struct wlr_buffer *corner_top_right;
 
-       struct wlr_buffer *menu_button_unpressed;
-       struct wlr_buffer *iconify_button_unpressed;
-       struct wlr_buffer *maximize_button_unpressed;
-       struct wlr_buffer *restore_button_unpressed;
-       struct wlr_buffer *close_button_unpressed;
-
-       struct wlr_buffer *menu_button_hover;
-       struct wlr_buffer *iconify_button_hover;
-       struct wlr_buffer *maximize_button_hover;
-       struct wlr_buffer *restore_button_hover;
-       struct wlr_buffer *close_button_hover;
-
        ssd->titlebar.tree = wlr_scene_tree_create(ssd->tree);
 
        struct ssd_sub_tree *subtree;
@@ -56,33 +102,10 @@ ssd_titlebar_create(struct ssd *ssd)
                        color = theme->window_active_title_bg_color;
                        corner_top_left = &theme->corner_top_left_active_normal->base;
                        corner_top_right = &theme->corner_top_right_active_normal->base;
-                       menu_button_unpressed = &theme->button_menu_active_unpressed->base;
-                       iconify_button_unpressed = &theme->button_iconify_active_unpressed->base;
-                       close_button_unpressed = &theme->button_close_active_unpressed->base;
-                       maximize_button_unpressed = &theme->button_maximize_active_unpressed->base;
-                       restore_button_unpressed = &theme->button_restore_active_unpressed->base;
-
-                       menu_button_hover = &theme->button_menu_active_hover->base;
-                       iconify_button_hover = &theme->button_iconify_active_hover->base;
-                       close_button_hover = &theme->button_close_active_hover->base;
-                       maximize_button_hover = &theme->button_maximize_active_hover->base;
-                       restore_button_hover = &theme->button_restore_active_hover->base;
                } else {
                        color = theme->window_inactive_title_bg_color;
                        corner_top_left = &theme->corner_top_left_inactive_normal->base;
                        corner_top_right = &theme->corner_top_right_inactive_normal->base;
-                       menu_button_unpressed = &theme->button_menu_inactive_unpressed->base;
-                       iconify_button_unpressed = &theme->button_iconify_inactive_unpressed->base;
-                       maximize_button_unpressed =
-                               &theme->button_maximize_inactive_unpressed->base;
-                       restore_button_unpressed = &theme->button_restore_inactive_unpressed->base;
-                       close_button_unpressed = &theme->button_close_inactive_unpressed->base;
-
-                       menu_button_hover = &theme->button_menu_inactive_hover->base;
-                       iconify_button_hover = &theme->button_iconify_inactive_hover->base;
-                       close_button_hover = &theme->button_close_inactive_hover->base;
-                       maximize_button_hover = &theme->button_maximize_inactive_hover->base;
-                       restore_button_hover = &theme->button_restore_inactive_hover->base;
 
                        wlr_scene_node_set_enabled(&parent->node, false);
                }
@@ -99,81 +122,17 @@ ssd_titlebar_create(struct ssd *ssd)
                        -rc.theme->border_width);
 
                /* Buttons */
-               int x = 0;
-               struct ssd_part *btn_max_root;
-               struct ssd_button *btn_max;
                struct title_button *b;
+               int x = 0;
                wl_list_for_each(b, &rc.title_buttons_left, link) {
-                       switch (b->type) {
-                       case LAB_SSD_BUTTON_WINDOW_MENU:
-                               add_scene_button(&subtree->parts, LAB_SSD_BUTTON_WINDOW_MENU,
-                                       parent, menu_button_unpressed, menu_button_hover, x,
-                                       view);
-                               x += theme->window_button_width;
-                               continue;
-                       case LAB_SSD_BUTTON_ICONIFY:
-                               add_scene_button(&subtree->parts, LAB_SSD_BUTTON_ICONIFY, parent,
-                                       iconify_button_unpressed, iconify_button_hover, x,
-                                       view);
-                               x += theme->window_button_width;
-                               continue;
-                       case LAB_SSD_BUTTON_MAXIMIZE:
-                               /* Maximize button has an alternate state when maximized */
-                               btn_max_root = add_scene_button(
-                                       &subtree->parts, LAB_SSD_BUTTON_MAXIMIZE, parent,
-                                       maximize_button_unpressed, maximize_button_hover, x, view);
-                               btn_max = node_ssd_button_from_node(btn_max_root->node);
-                               add_toggled_icon(btn_max, &subtree->parts, LAB_SSD_BUTTON_MAXIMIZE,
-                                       restore_button_unpressed, restore_button_hover);
-                               x += theme->window_button_width;
-                               continue;
-                       case LAB_SSD_BUTTON_CLOSE:
-                               add_scene_button(&subtree->parts, LAB_SSD_BUTTON_CLOSE, parent,
-                                       close_button_unpressed, close_button_hover, x, view);
-                               x += theme->window_button_width;
-                               continue;
-                       default:
-                               assert(false && "invalid titlebar part");
-                               wlr_log(WLR_ERROR, "invalid titlebar type");
-                               abort();
-                       }
+                       add_button(ssd, subtree, b->type, x);
+                       x += theme->window_button_width;
                }
 
-               x = width - theme->window_button_width;
+               x = width;
                wl_list_for_each_reverse(b, &rc.title_buttons_right, link) {
-                       switch (b->type) {
-                       case LAB_SSD_BUTTON_WINDOW_MENU:
-                               add_scene_button(&subtree->parts, LAB_SSD_BUTTON_WINDOW_MENU,
-                                       parent, menu_button_unpressed, menu_button_hover, x,
-                                       view);
-                               x -= theme->window_button_width;
-                               continue;
-                       case LAB_SSD_BUTTON_ICONIFY:
-                               add_scene_button(&subtree->parts, LAB_SSD_BUTTON_ICONIFY, parent,
-                                       iconify_button_unpressed, iconify_button_hover, x,
-                                       view);
-                               x -= theme->window_button_width;
-                               continue;
-                       case LAB_SSD_BUTTON_MAXIMIZE:
-                               /* Maximize button has an alternate state when maximized */
-                               btn_max_root = add_scene_button(
-                                       &subtree->parts, LAB_SSD_BUTTON_MAXIMIZE, parent,
-                                       maximize_button_unpressed, maximize_button_hover, x, view);
-                               btn_max = node_ssd_button_from_node(btn_max_root->node);
-                               add_toggled_icon(btn_max, &subtree->parts, LAB_SSD_BUTTON_MAXIMIZE,
-                                       restore_button_unpressed, restore_button_hover);
-                               x -= theme->window_button_width;
-                               continue;
-                       case LAB_SSD_BUTTON_CLOSE:
-                               add_scene_button(&subtree->parts, LAB_SSD_BUTTON_CLOSE, parent,
-                                       close_button_unpressed, close_button_hover, x, view);
-                               x -= theme->window_button_width;
-                               continue;
-                       default:
-                               assert(false && "invalid titlebar part");
-                               wlr_log(WLR_ERROR, "invalid titlebar type");
-                               abort();
-                       }
+                       x -= theme->window_button_width;
+                       add_button(ssd, subtree, b->type, x);
                }
        } FOR_EACH_END