From 3be83aefdf5fc938731c9805f217c633b63fcb83 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Tue, 16 Jun 2026 09:14:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(bar):=20=E4=BF=AE=E5=A4=8D=E9=A2=9C?= =?UTF-8?q?=E8=89=B2=E7=BC=93=E5=AD=98=E9=80=A0=E6=88=90=E7=9A=84=E9=A2=9C?= =?UTF-8?q?=E8=89=B2=E9=87=8E=E6=8C=87=E9=92=88=E4=BB=8E=E8=80=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=20cell=20=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bar/bar.c | 10 +++---- src/bar/cell.c | 69 ++++++++++++------------------------------------- src/bar/cell.h | 3 --- src/bar/types.h | 6 +++-- 4 files changed, 25 insertions(+), 63 deletions(-) diff --git a/src/bar/bar.c b/src/bar/bar.c index a4b05d4..dd5768d 100644 --- a/src/bar/bar.c +++ b/src/bar/bar.c @@ -42,6 +42,8 @@ static void bar_item_cleanup(zdwm_bar_item_t *item) { for (size_t i = 0; i < item->count; ++i) { auto cell = &item->cells[i]; p_delete(&cell->text); + p_delete(&cell->fg_text); + p_delete(&cell->bg_text); } p_delete(&item->cells); @@ -141,8 +143,6 @@ void bar_cleanup(bar_t *bar) { } p_delete(&bar->bars); bar->count = 0; - - bar_cell_clean_color_cache(); } static inline void bar_item_update(zdwm_bar_item_t *item) { @@ -258,7 +258,7 @@ static void bar_item_draw( continue; } - draw_background(cr, cell->bg, cell_area); + draw_background(cr, &cell->bg, cell_area); if (cell->show_indicator) { auto size = MIN(item->indicator_width, cell_area.width); @@ -271,7 +271,7 @@ static void bar_item_draw( .width = size, .height = size, }; - draw_background(cr, cell->fg, indicator_area); + draw_background(cr, &cell->fg, indicator_area); } } @@ -282,7 +282,7 @@ static void bar_item_draw( .height = height, }; if (text_area.width > 0) { - draw_text(cr, ctx, cell->text, cell->fg, text_area); + draw_text(cr, ctx, cell->text, &cell->fg, text_area); } cell->dirty = false; diff --git a/src/bar/cell.c b/src/bar/cell.c index 05aa1cc..46cd928 100644 --- a/src/bar/cell.c +++ b/src/bar/cell.c @@ -2,63 +2,25 @@ #include #include +#include #include #include #include "bar/types.h" -#include "base/array.h" #include "base/color.h" #include "base/memory.h" -typedef struct color_cache_item_t { - const char *text; - color_t color; -} color_cache_item_t; - -typedef struct color_cache_t { - color_cache_item_t *items; - size_t count; - size_t capacity; -} color_cache_t; - -static color_cache_t cache = {}; - -static color_t *find_or_insert_color(const char *text) { - assert(text); - for (size_t i = 0; i < cache.count; ++i) { - auto item = &cache.items[i]; - if (text == item->text || strcasecmp(text, item->text) == 0) { - return &item->color; - } - } - - auto slot = array_push(cache.items, cache.count, cache.capacity); - slot->text = p_strdup(text); - color_parse(text, &slot->color); - return &slot->color; -} - -void bar_cell_reset_color_cache(void) { - for (size_t i = 0; i < cache.count; ++i) { - auto item = &cache.items[i]; - p_delete(&item->text); - } - p_clear(cache.items, cache.count); - cache.count = 0; -} - -void bar_cell_clean_color_cache(void) { - bar_cell_reset_color_cache(); - p_delete(&cache.items); - cache.capacity = 0; -} - static size_t bar_cell_get_count(zdwm_bar_item_t *item) { return item->count; } static void bar_cell_set_count(zdwm_bar_item_t *item, size_t count) { if (item->count == count) return; - for (size_t i = 0; i < item->count; ++i) p_delete(&item->cells[i].text); + for (size_t i = 0; i < item->count; ++i) { + auto cell = &item->cells[i]; + p_delete(cell->text); + p_delete(&cell->fg_text); + p_delete(&cell->bg_text); + } item->count = count; p_realloc(&item->cells, count); @@ -85,11 +47,12 @@ static void bar_cell_set_bg(zdwm_bar_item_t *item, size_t index, const char *color) { if (index >= item->count) return; - auto cell = &item->cells[index]; - auto cached_color = find_or_insert_color(color); - if (cell->bg == cached_color) return; + auto cell = &item->cells[index]; - cell->bg = cached_color; + if (cell->bg_text && strcmp(color, cell->bg_text) == 0) return; + + cell->bg_text = p_strdup(color); + color_parse(color, &cell->bg); cell->dirty = true; item->dirty = true; @@ -99,11 +62,11 @@ static void bar_cell_set_fg(zdwm_bar_item_t *item, size_t index, const char *color) { if (index >= item->count) return; - auto cell = &item->cells[index]; - auto cached_color = find_or_insert_color(color); - if (cell->fg == cached_color) return; + auto cell = &item->cells[index]; + if (cell->fg_text && strcmp(color, cell->fg_text) == 0) return; - cell->fg = cached_color; + cell->fg_text = p_strdup(color); + color_parse(color, &cell->fg); cell->dirty = true; item->dirty = true; diff --git a/src/bar/cell.h b/src/bar/cell.h index 814efa3..2763168 100644 --- a/src/bar/cell.h +++ b/src/bar/cell.h @@ -6,9 +6,6 @@ #include "bar/types.h" -void bar_cell_reset_color_cache(void); -void bar_cell_clean_color_cache(void); - void bar_cell_set_region( zdwm_bar_item_t *item, size_t index, diff --git a/src/bar/types.h b/src/bar/types.h index 9effc7c..23e0450 100644 --- a/src/bar/types.h +++ b/src/bar/types.h @@ -15,8 +15,10 @@ typedef struct bar_x_region_t { typedef struct bar_cell_t { zdwm_icon_t icon; char *text; /* 持有内存,避免野指针问题 */ - const color_t *fg; - const color_t *bg; + char *fg_text; + char *bg_text; + color_t fg; + color_t bg; bar_x_region_t region; bool show_indicator; bool dirty;