]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/menu.c: move some post_processing() and menu_update_width()
authortokyo4j <hrak1529@gmail.com>
Sun, 10 Nov 2024 09:51:48 +0000 (18:51 +0900)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 13 Nov 2024 20:08:15 +0000 (20:08 +0000)
src/menu/menu.c

index 6e6199f1019a3b9f4a49bfdab8d126306638deae..68c744f3893b7a659fcce56406e5b69c9807e273 100644 (file)
@@ -104,85 +104,6 @@ menu_get_by_id(struct server *server, const char *id)
        return NULL;
 }
 
-static void
-menu_update_width(struct menu *menu)
-{
-       struct menuitem *item;
-       struct theme *theme = menu->server->theme;
-       int max_width = theme->menu_min_width;
-
-       /* Get widest menu item, clamped by menu_max_width */
-       wl_list_for_each(item, &menu->menuitems, link) {
-               if (item->native_width > max_width) {
-                       max_width = item->native_width < theme->menu_max_width
-                               ? item->native_width : theme->menu_max_width;
-               }
-       }
-       menu->size.width = max_width + 2 * theme->menu_items_padding_x;
-
-       /*
-        * TODO: This function is getting a bit unwieldy. Consider calculating
-        * the menu-window width up-front to avoid this post_processing() and
-        * second-bite-of-the-cherry stuff
-        */
-
-       /* Update all items for the new size */
-       wl_list_for_each(item, &menu->menuitems, link) {
-               wlr_scene_rect_set_size(
-                       wlr_scene_rect_from_node(item->normal.background),
-                       menu->size.width, item->height);
-
-               /*
-                * Separator lines are special because they change width with
-                * the menu.
-                */
-               if (item->type == LAB_MENU_SEPARATOR_LINE) {
-                       int width = menu->size.width
-                               - 2 * theme->menu_separator_padding_width;
-                       wlr_scene_rect_set_size(
-                               wlr_scene_rect_from_node(item->normal.text),
-                               width, theme->menu_separator_line_thickness);
-               } else if (item->type == LAB_MENU_TITLE) {
-                       if (item->native_width > max_width) {
-                               scaled_font_buffer_set_max_width(item->normal.buffer,
-                                       max_width);
-                       }
-                       if (theme->menu_title_text_justify == LAB_JUSTIFY_CENTER) {
-                               int x, y;
-                               x = (menu->size.width - item->native_width) / 2;
-                               x = x < 0 ? 0 : x;
-                               y = (theme->menu_header_height - item->normal.buffer->height) / 2;
-                               wlr_scene_node_set_position(item->normal.text, x, y);
-                       }
-               }
-
-               if (item->selectable) {
-                       /* Only selectable items have item->selected.background */
-                       wlr_scene_rect_set_size(
-                               wlr_scene_rect_from_node(item->selected.background),
-                               menu->size.width, item->height);
-               }
-
-               if (item->native_width > max_width || item->submenu || item->execute) {
-                       scaled_font_buffer_set_max_width(item->normal.buffer,
-                               max_width);
-                       if (item->selectable) {
-                               scaled_font_buffer_set_max_width(item->selected.buffer,
-                                       max_width);
-                       }
-               }
-       }
-}
-
-static void
-post_processing(struct server *server)
-{
-       struct menu *menu;
-       wl_list_for_each(menu, &server->menus, link) {
-               menu_update_width(menu);
-       }
-}
-
 static void
 validate_menu(struct menu *menu)
 {
@@ -370,6 +291,85 @@ separator_create(struct menu *menu, const char *label)
        return menuitem;
 }
 
+static void
+menu_update_width(struct menu *menu)
+{
+       struct menuitem *item;
+       struct theme *theme = menu->server->theme;
+       int max_width = theme->menu_min_width;
+
+       /* Get widest menu item, clamped by menu_max_width */
+       wl_list_for_each(item, &menu->menuitems, link) {
+               if (item->native_width > max_width) {
+                       max_width = item->native_width < theme->menu_max_width
+                               ? item->native_width : theme->menu_max_width;
+               }
+       }
+       menu->size.width = max_width + 2 * theme->menu_items_padding_x;
+
+       /*
+        * TODO: This function is getting a bit unwieldy. Consider calculating
+        * the menu-window width up-front to avoid this post_processing() and
+        * second-bite-of-the-cherry stuff
+        */
+
+       /* Update all items for the new size */
+       wl_list_for_each(item, &menu->menuitems, link) {
+               wlr_scene_rect_set_size(
+                       wlr_scene_rect_from_node(item->normal.background),
+                       menu->size.width, item->height);
+
+               /*
+                * Separator lines are special because they change width with
+                * the menu.
+                */
+               if (item->type == LAB_MENU_SEPARATOR_LINE) {
+                       int width = menu->size.width
+                               - 2 * theme->menu_separator_padding_width;
+                       wlr_scene_rect_set_size(
+                               wlr_scene_rect_from_node(item->normal.text),
+                               width, theme->menu_separator_line_thickness);
+               } else if (item->type == LAB_MENU_TITLE) {
+                       if (item->native_width > max_width) {
+                               scaled_font_buffer_set_max_width(item->normal.buffer,
+                                       max_width);
+                       }
+                       if (theme->menu_title_text_justify == LAB_JUSTIFY_CENTER) {
+                               int x, y;
+                               x = (menu->size.width - item->native_width) / 2;
+                               x = x < 0 ? 0 : x;
+                               y = (theme->menu_header_height - item->normal.buffer->height) / 2;
+                               wlr_scene_node_set_position(item->normal.text, x, y);
+                       }
+               }
+
+               if (item->selectable) {
+                       /* Only selectable items have item->selected.background */
+                       wlr_scene_rect_set_size(
+                               wlr_scene_rect_from_node(item->selected.background),
+                               menu->size.width, item->height);
+               }
+
+               if (item->native_width > max_width || item->submenu || item->execute) {
+                       scaled_font_buffer_set_max_width(item->normal.buffer,
+                               max_width);
+                       if (item->selectable) {
+                               scaled_font_buffer_set_max_width(item->selected.buffer,
+                                       max_width);
+                       }
+               }
+       }
+}
+
+static void
+post_processing(struct server *server)
+{
+       struct menu *menu;
+       wl_list_for_each(menu, &server->menus, link) {
+               menu_update_width(menu);
+       }
+}
+
 /*
  * Handle the following:
  * <item label="">