feat(bar): 实现 bar_draw 渲染骨架及文字排版绘制接口

This commit is contained in:
2026-06-08 22:57:34 +08:00
parent 62f6bab611
commit 8c145fb1b4
5 changed files with 126 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
#include <cairo.h>
#include <stddef.h>
#include <stdint.h>
#include <zdwm/bar.h>
#include <zdwm/types.h>
@@ -92,3 +93,32 @@ void bar_cleanup(bar_t *bar) {
p_delete(&bar->bars);
bar->count = 0;
}
static void bar_draw_item(
zdwm_bar_item_t *item,
text_context_t *context,
cairo_t *cr,
int32_t width,
int32_t height
) {
/* TODO: */
}
void bar_draw(bar_t *bar) {
auto ctx = bar->ctx;
for (size_t i = 0; i < bar->count; ++i) {
auto bar_output = &bar->bars[i];
auto cr = bar_output->cr;
auto width = bar_output->width;
auto height = bar_output->height;
for (size_t j = 0; j < ZDWM_BAR_SIDE_COUNT; ++j) {
auto side = &bar_output->sides[j];
for (size_t k = 0; k < side->count; ++k) {
auto item = &side->items[k];
bar_draw_item(item, ctx, cr, width, height);
}
}
}
}

View File

@@ -2,6 +2,7 @@
#include <cairo.h>
#include <stddef.h>
#include <stdint.h>
#include <zdwm/bar.h>
#include <zdwm/types.h>
@@ -13,6 +14,8 @@ typedef struct bar_output_t {
cairo_t *cr;
zdwm_window_id_t window_id;
zdwm_output_id_t output_id;
int32_t width;
int32_t height;
bar_side_t sides[ZDWM_BAR_SIDE_COUNT];
} bar_output_t;
@@ -33,3 +36,4 @@ typedef struct bar_t {
void bar_init(bar_t *bar);
void bar_cleanup(bar_t *bar);
void bar_draw(bar_t *bar);

View File

@@ -1,5 +1,7 @@
#include "bar/text.h"
#include <assert.h>
#include <cairo.h>
#include <glib-object.h>
#include <pango/pango-attributes.h>
#include <pango/pango-context.h>
@@ -7,6 +9,7 @@
#include <pango/pango-layout.h>
#include <pango/pango-types.h>
#include <pango/pangocairo.h>
#include <stdint.h>
#include "base/memory.h"
@@ -67,3 +70,72 @@ void text_context_destory(text_context_t *context) {
p_delete(&context);
}
static inline void
get_layout_size(PangoLayout *layout, int32_t *width, int32_t *height) {
pango_layout_get_pixel_size(layout, width, height);
}
void text_context_get_text_size(
text_context_t *context,
const char *text,
int32_t *width,
int32_t *height
) {
assert(context && text);
if (!width && !height) return;
auto layout = context->layout;
pango_layout_set_width(layout, -1);
pango_layout_set_height(layout, 0);
pango_layout_set_text(layout, text, -1);
get_layout_size(layout, width, height);
}
void draw_text(
cairo_t *cr,
text_context_t *context,
const char *text,
color_t *color,
zdwm_rect_t area
) {
assert(cr);
assert(context);
assert(text);
assert(color);
auto layout = context->layout;
pango_layout_set_width(layout, area.width * PANGO_SCALE);
pango_layout_set_text(layout, text, -1);
cairo_set_source_rgba(
cr,
color->red,
color->green,
color->blue,
color->alpha
);
int32_t height = 0;
get_layout_size(layout, nullptr, &height);
double offset_y = (area.height - height) / 2.0;
pango_cairo_update_layout(cr, layout);
cairo_move_to(cr, area.x, offset_y + area.y);
pango_cairo_show_layout(cr, layout);
}
void draw_background(cairo_t *cr, color_t *color, zdwm_rect_t area) {
cairo_move_to(cr, area.x, area.y);
cairo_set_source_rgba(
cr,
color->red,
color->green,
color->blue,
color->alpha
);
cairo_rectangle(cr, area.x, area.y, area.width, area.height);
cairo_fill(cr);
}

View File

@@ -1,9 +1,27 @@
#pragma once
#include <cairo.h>
#include <stdint.h>
#include <zdwm/types.h>
#include "base/color.h"
typedef struct text_context_t text_context_t;
text_context_t *
text_context_create(const char *family, uint32_t size, uint32_t dpi);
void text_context_destory(text_context_t *context);
void text_context_get_text_size(
text_context_t *context,
const char *text,
int32_t *width,
int32_t *height
);
void draw_text(
cairo_t *cr,
text_context_t *context,
const char *text,
color_t *color,
zdwm_rect_t area
);
void draw_background(cairo_t *cr, color_t *color, zdwm_rect_t area);

View File

@@ -226,6 +226,8 @@ static void runtime_init_bar(runtime_t *runtime) {
bar_output->cr = bar_window.cr;
bar_output->window_id = bar_window.window_id;
bar_output->output_id = output->id;
bar_output->width = bar_rect.width;
bar_output->height = bar_rect.height;
}
bar_init(&runtime->bar);