feat(bar): bar item 扩展钩子功能 bar cell 添加 fixed_width 属性与操作接口

- item type 加入可为空的 after_layout 和 after_draw 钩子
- cell 加 fixed_width 属性, cell api 添加 cell_set_fixed_width 和
  cell_get_region 两个接口
- cell api 改为 const 避免外部修改
This commit is contained in:
2026-06-28 02:04:20 +08:00
parent 27aab6f0e9
commit 6b75cda128
5 changed files with 134 additions and 32 deletions

View File

@@ -212,14 +212,16 @@ static void bar_output_layout(bar_output_t *bar_output, text_context_t *ctx) {
item->region.start = start;
for (size_t j = 0; j < item->count; ++j) {
auto cell = &item->cells[j];
auto fixed_width = cell->fixed_width;
int32_t width = 0;
text_context_get_text_size(ctx, cell->text, &width, nullptr);
bar_x_region_t region = {
.start = start,
.end = start + width + item->cell_padding * 2,
};
bar_x_region_t region = {.start = start, .end = start};
if (fixed_width == 0) {
int32_t width = 0;
text_context_get_text_size(ctx, cell->text, &width, nullptr);
region.end += width + 2 * item->cell_padding;
} else if (fixed_width > 0) {
region.end += fixed_width;
}
bar_cell_set_region(item, j, region);
start = region.end;
@@ -240,14 +242,16 @@ static void bar_output_layout(bar_output_t *bar_output, text_context_t *ctx) {
for (size_t j = item->count; j > 0; --j) {
auto cell_index = j - 1;
auto cell = &item->cells[cell_index];
auto fixed_width = cell->fixed_width;
int32_t width = 0;
text_context_get_text_size(ctx, cell->text, &width, nullptr);
bar_x_region_t region = {
.start = end - width - item->cell_padding * 2,
.end = end,
};
bar_x_region_t region = {.start = end, .end = end};
if (fixed_width == 0) {
int32_t width = 0;
text_context_get_text_size(ctx, cell->text, &width, nullptr);
region.start -= width + item->cell_padding * 2;
} else if (fixed_width > 0) {
region.start -= fixed_width;
}
bar_cell_set_region(item, cell_index, region);
end = region.start;
@@ -263,12 +267,30 @@ static void bar_output_layout(bar_output_t *bar_output, text_context_t *ctx) {
if (center->count == 0) return;
center->region = (bar_x_region_t){.start = start, .end = end};
auto width = (end - start) / (int32_t)center->count;
auto center_width = end - start;
auto center_count = center->count;
for (size_t i = 0; i < center->count; ++i) {
bar_x_region_t region = {
.start = start,
.end = start + width + center->cell_padding * 2,
};
auto cell = &center->cells[i];
auto fixed_width = cell->fixed_width;
if (fixed_width > 0) {
center_width -= fixed_width;
} else if (fixed_width < 0) {
/* 隐藏的 cell 不占任何空间,平分宽度时不参与 */
--center_count;
}
}
auto width = center_width / (int32_t)center_count;
for (size_t i = 0; i < center->count; ++i) {
auto cell = &center->cells[i];
auto fixed_width = cell->fixed_width;
bar_x_region_t region = {.start = start, .end = start};
if (fixed_width == 0) {
region.end += width + center->cell_padding * 2;
} else if (fixed_width > 0) {
region.end += fixed_width;
}
start = region.end;
bar_cell_set_region(center, i, region);
}
@@ -386,6 +408,42 @@ bar_output_draw(bar_output_t *bar_output, text_context_t *ctx, color_t *bg) {
bar_item_draw(cr, &bar_output->center, ctx, bar_output->height);
}
static inline void bar_item_run_hook(
zdwm_bar_item_t *item,
void (*hook)(const zdwm_bar_item_hook_params_t *params)
) {
if (!hook) return;
zdwm_bar_item_hook_params_t params = {
.item = item,
.cell_api = &bar_cell_api,
.region = item->region,
.state = item->state
};
hook(&params);
}
static void visitor_after_layout(zdwm_bar_item_t *item) {
bar_item_run_hook(item, item->api.after_layout);
}
static void visitor_after_draw(zdwm_bar_item_t *item) {
bar_item_run_hook(item, item->api.after_draw);
}
typedef void (*bar_item_visitor_t)(zdwm_bar_item_t *item);
static void
bar_output_foreach_item(bar_output_t *bar_output, bar_item_visitor_t visit) {
for (size_t i = 0; i < bar_output->left.count; ++i) {
visit(&bar_output->left.items[i]);
}
visit(&bar_output->center);
for (size_t i = 0; i < bar_output->right.count; ++i) {
visit(&bar_output->right.items[i]);
}
}
bool bar_draw(bar_t *bar) {
auto ctx = bar->ctx;
@@ -396,7 +454,9 @@ bool bar_draw(bar_t *bar) {
if (!bar_output_is_dirty(bar_output)) continue;
bar_output_layout(bar_output, ctx);
bar_output_foreach_item(bar_output, visitor_after_layout);
bar_output_draw(bar_output, ctx, &bar->palette.bg);
bar_output_foreach_item(bar_output, visitor_after_draw);
changed = true;
}

View File

@@ -2,6 +2,7 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <zdwm/types.h>
@@ -119,15 +120,37 @@ bar_cell_set_indicator(zdwm_bar_item_t *item, size_t index, bool show) {
item->dirty = true;
}
static void
bar_cell_set_fixed_width(zdwm_bar_item_t *item, size_t index, int32_t width) {
if (index >= item->count) return;
auto cell = &item->cells[index];
if (cell->fixed_width == width) return;
cell->fixed_width = width;
cell->dirty = true;
item->dirty = true;
}
static bar_x_region_t bar_cell_get_region(zdwm_bar_item_t *item, size_t index) {
if (index >= item->count) return (bar_x_region_t){0};
auto cell = &item->cells[index];
return cell->region;
}
/* clang-format off */
zdwm_bar_cell_api_t bar_cell_api = {
.get_cell_count = bar_cell_get_count,
.set_cell_count = bar_cell_set_count,
.cell_set_text = bar_cell_set_text,
.cell_set_bg = bar_cell_set_bg,
.cell_set_fg = bar_cell_set_fg,
.cell_set_icon = bar_cell_set_icon,
.cell_set_indicator = bar_cell_set_indicator,
const zdwm_bar_cell_api_t bar_cell_api = {
.get_cell_count = bar_cell_get_count,
.set_cell_count = bar_cell_set_count,
.cell_set_text = bar_cell_set_text,
.cell_set_bg = bar_cell_set_bg,
.cell_set_fg = bar_cell_set_fg,
.cell_set_icon = bar_cell_set_icon,
.cell_set_indicator = bar_cell_set_indicator,
.cell_set_fixed_width = bar_cell_set_fixed_width,
.cell_get_region = bar_cell_get_region,
};
/* clang-format on */

View File

@@ -12,4 +12,4 @@ void bar_cell_set_region(
bar_x_region_t region
);
extern zdwm_bar_cell_api_t bar_cell_api;
extern const zdwm_bar_cell_api_t bar_cell_api;

View File

@@ -7,10 +7,7 @@
#include "base/color.h"
typedef struct bar_x_region_t {
int32_t start;
int32_t end;
} bar_x_region_t;
typedef struct zdwm_bar_x_region_t bar_x_region_t;
typedef struct bar_cell_t {
zdwm_icon_t icon;
@@ -20,6 +17,7 @@ typedef struct bar_cell_t {
color_t fg;
color_t bg;
bar_x_region_t region;
int32_t fixed_width;
bool show_indicator;
bool dirty;
} bar_cell_t;