]> git.mdlowis.com Git - proto/labwc.git/commitdiff
theme: simplify loading of xbm buttons
authorJohan Malm <jgm323@gmail.com>
Fri, 4 Aug 2023 21:30:16 +0000 (22:30 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 9 Aug 2023 06:38:17 +0000 (07:38 +0100)
No functional change.

include/theme.h
include/xbm/xbm.h
src/ssd/ssd_titlebar.c
src/theme.c
src/xbm/xbm.c

index c78afb26a46e14233afcdda672b5267c8fafcedd..47ef3b9f33b08b892aac5b934b27d04af3efd5ed 100644 (file)
@@ -76,15 +76,15 @@ struct theme {
        int osd_window_switcher_item_active_border_width;
 
        /* textures */
-       struct lab_data_buffer *xbm_close_active_unpressed;
-       struct lab_data_buffer *xbm_maximize_active_unpressed;
-       struct lab_data_buffer *xbm_iconify_active_unpressed;
-       struct lab_data_buffer *xbm_menu_active_unpressed;
-
-       struct lab_data_buffer *xbm_close_inactive_unpressed;
-       struct lab_data_buffer *xbm_maximize_inactive_unpressed;
-       struct lab_data_buffer *xbm_iconify_inactive_unpressed;
-       struct lab_data_buffer *xbm_menu_inactive_unpressed;
+       struct lab_data_buffer *button_close_active_unpressed;
+       struct lab_data_buffer *button_maximize_active_unpressed;
+       struct lab_data_buffer *button_iconify_active_unpressed;
+       struct lab_data_buffer *button_menu_active_unpressed;
+
+       struct lab_data_buffer *button_close_inactive_unpressed;
+       struct lab_data_buffer *button_maximize_inactive_unpressed;
+       struct lab_data_buffer *button_iconify_inactive_unpressed;
+       struct lab_data_buffer *button_menu_inactive_unpressed;
 
        struct lab_data_buffer *corner_top_left_active_normal;
        struct lab_data_buffer *corner_top_right_active_normal;
index ffcdaf085373c7f78a997d200e1e5729918768ed..8c36dfbc25599db1c2515f8a4c2415770e3bebbd 100644 (file)
@@ -9,6 +9,7 @@
 /**
  * xbm_load - load theme xbm files into global theme struct
  */
-void xbm_load(struct theme *theme);
+void xbm_load_button(const char *filename, struct lab_data_buffer **buffer,
+       char *fallback_button, float *rgba);
 
 #endif /* LABWC_XBM_H */
index ca286ba3c8dac43e8136659c859aebf1cf71a8ef..c12a48b47bda21e4f83539c13c678a72e8c1ef84 100644 (file)
@@ -43,18 +43,18 @@ 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->xbm_menu_active_unpressed->base;
-                       iconify_button_unpressed = &theme->xbm_iconify_active_unpressed->base;
-                       close_button_unpressed = &theme->xbm_close_active_unpressed->base;
-                       maximize_button_unpressed = &theme->xbm_maximize_active_unpressed->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;
                } 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->xbm_menu_inactive_unpressed->base;
-                       iconify_button_unpressed = &theme->xbm_iconify_inactive_unpressed->base;
-                       maximize_button_unpressed = &theme->xbm_maximize_inactive_unpressed->base;
-                       close_button_unpressed = &theme->xbm_close_inactive_unpressed->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;
+                       close_button_unpressed = &theme->button_close_inactive_unpressed->base;
                        wlr_scene_node_set_enabled(&parent->node, false);
                }
                wl_list_init(&subtree->parts);
index 43014460f672a00b61b787da5c3577cfab8a5787..5a3c489ce51fa219a9331f79177f6b94f426092d 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * Theme engine for labwc
  *
- * Copyright (C) Johan Malm 2020-2021
+ * Copyright (C) Johan Malm 2020-2023
  */
 
 #define _POSIX_C_SOURCE 200809L
 #include "buffer.h"
 #include "ssd.h"
 
+struct button {
+       const char *name;
+       char fallback_button[6];        /* built-in 6x6 button */
+       struct {
+               struct lab_data_buffer **buffer;
+               float *rgba;
+       } active, inactive;
+};
+
+static void
+load_buttons(struct theme *theme)
+{
+       struct button buttons[] = {
+               {
+                       "menu",
+                       { 0x00, 0x18, 0x3c, 0x3c, 0x18, 0x00 },
+                       {
+                               &theme->button_menu_active_unpressed,
+                               theme->window_active_button_menu_unpressed_image_color,
+                       },
+                       {
+                               &theme->button_menu_inactive_unpressed,
+                               theme->window_inactive_button_menu_unpressed_image_color,
+                       },
+               },
+               {
+                       "iconify",
+                       { 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f },
+                       {
+                               &theme->button_iconify_active_unpressed,
+                               theme->window_active_button_iconify_unpressed_image_color,
+                       },
+                       {
+                               &theme->button_iconify_inactive_unpressed,
+                               theme->window_inactive_button_iconify_unpressed_image_color,
+                       },
+               },
+               {
+                       "max",
+                       { 0x3f, 0x3f, 0x21, 0x21, 0x21, 0x3f },
+                       {
+                               &theme->button_maximize_active_unpressed,
+                               theme->window_active_button_max_unpressed_image_color,
+                       },
+                       {
+                               &theme->button_maximize_inactive_unpressed,
+                               theme->window_inactive_button_max_unpressed_image_color,
+                       },
+               },
+               {
+                       "close",
+                       { 0x33, 0x3f, 0x1e, 0x1e, 0x3f, 0x33 },
+                       {
+                               &theme->button_close_active_unpressed,
+                               theme->window_active_button_close_unpressed_image_color,
+                       },
+                       {
+                               &theme->button_close_inactive_unpressed,
+                               theme->window_inactive_button_close_unpressed_image_color,
+                       },
+               },
+       };
+
+       char filename[4096] = {0};
+       for (size_t i = 0; i < sizeof(buttons) / sizeof(buttons[0]); ++i) {
+               struct button *b = &buttons[i];
+               snprintf(filename, sizeof(filename), "%s.xbm", b->name);
+               xbm_load_button(filename, b->active.buffer, b->fallback_button, b->active.rgba);
+               xbm_load_button(filename, b->inactive.buffer, b->fallback_button, b->inactive.rgba);
+       }
+}
+
 static int
 hex_to_dec(char c)
 {
@@ -695,7 +767,7 @@ theme_init(struct theme *theme, const char *theme_name)
 
        post_processing(theme);
        create_corners(theme);
-       xbm_load(theme);
+       load_buttons(theme);
 }
 
 void
index 3e9e1aec3c4bc554d51252bfa03d2e423a0138fd..a19d50ff1c81936d0df410eb6758ae8022116c9d 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * Create wlr textures based on xbm data
  *
- * Copyright Johan Malm 2020
+ * Copyright Johan Malm 2020-2023
  */
 
 #include <stdio.h>
 #include "xbm/xbm.h"
 #include "buffer.h"
 
-/* built-in 6x6 buttons */
-char menu_button_normal[] = { 0x00, 0x18, 0x3c, 0x3c, 0x18, 0x00 };
-char iconify_button_normal[] = { 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f };
-char max_button_normal[] = { 0x3f, 0x3f, 0x21, 0x21, 0x21, 0x3f };
-char max_button_toggled[] = { 0x3e, 0x22, 0x2f, 0x29, 0x39, 0x0f };
-char close_button_normal[] = { 0x33, 0x3f, 0x1e, 0x1e, 0x3f, 0x33 };
-
 static char *
 xbm_path(const char *button)
 {
@@ -33,8 +26,9 @@ xbm_path(const char *button)
        return buffer;
 }
 
-static void
-load_button(const char *filename, struct lab_data_buffer **buffer, char *button)
+void
+xbm_load_button(const char *filename, struct lab_data_buffer **buffer,
+               char *fallback_button, float *rgba)
 {
        struct pixmap pixmap = {0};
        if (*buffer) {
@@ -42,6 +36,8 @@ load_button(const char *filename, struct lab_data_buffer **buffer, char *button)
                *buffer = NULL;
        }
 
+       parse_set_color(rgba);
+
        /* Read file into memory as it's easier to tokenzie that way */
        char *token_buffer = grab_file(xbm_path(filename));
        if (token_buffer) {
@@ -53,40 +49,10 @@ load_button(const char *filename, struct lab_data_buffer **buffer, char *button)
                }
        }
        if (!pixmap.data) {
-               pixmap = parse_xbm_builtin(button, 6);
+               pixmap = parse_xbm_builtin(fallback_button, 6);
        }
 
        /* Create buffer with free_on_destroy being true */
        *buffer = buffer_create_wrap(pixmap.data, pixmap.width, pixmap.height,
                pixmap.width * 4, true);
 }
-
-void
-xbm_load(struct theme *theme)
-{
-       parse_set_color(theme->window_active_button_menu_unpressed_image_color);
-       load_button("menu.xbm",
-               &theme->xbm_menu_active_unpressed, menu_button_normal);
-       parse_set_color(theme->window_active_button_iconify_unpressed_image_color);
-       load_button("iconify.xbm",
-               &theme->xbm_iconify_active_unpressed, iconify_button_normal);
-       parse_set_color(theme->window_active_button_max_unpressed_image_color);
-       load_button("max.xbm",
-               &theme->xbm_maximize_active_unpressed, max_button_normal);
-       parse_set_color(theme->window_active_button_close_unpressed_image_color);
-       load_button("close.xbm",
-               &theme->xbm_close_active_unpressed, close_button_normal);
-
-       parse_set_color(theme->window_inactive_button_menu_unpressed_image_color);
-       load_button("menu.xbm",
-               &theme->xbm_menu_inactive_unpressed, menu_button_normal);
-       parse_set_color(theme->window_inactive_button_iconify_unpressed_image_color);
-       load_button("iconify.xbm",
-               &theme->xbm_iconify_inactive_unpressed, iconify_button_normal);
-       parse_set_color(theme->window_inactive_button_max_unpressed_image_color);
-       load_button("max.xbm",
-               &theme->xbm_maximize_inactive_unpressed, max_button_normal);
-       parse_set_color(theme->window_inactive_button_close_unpressed_image_color);
-       load_button("close.xbm",
-               &theme->xbm_close_inactive_unpressed, close_button_normal);
-}