From 8c145fb1b4fa81c0334dae13ed47ab71981c3705 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Mon, 8 Jun 2026 22:57:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(bar):=20=E5=AE=9E=E7=8E=B0=20bar=5Fdraw=20?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E9=AA=A8=E6=9E=B6=E5=8F=8A=E6=96=87=E5=AD=97?= =?UTF-8?q?=E6=8E=92=E7=89=88=E7=BB=98=E5=88=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bar/bar.c | 30 ++++++++++++++++++ src/bar/bar.h | 4 +++ src/bar/text.c | 72 +++++++++++++++++++++++++++++++++++++++++++ src/bar/text.h | 18 +++++++++++ src/runtime/runtime.c | 2 ++ 5 files changed, 126 insertions(+) diff --git a/src/bar/bar.c b/src/bar/bar.c index 2604e2a..19f9b44 100644 --- a/src/bar/bar.c +++ b/src/bar/bar.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -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); + } + } + } +} diff --git a/src/bar/bar.h b/src/bar/bar.h index cb13aa9..1936a9b 100644 --- a/src/bar/bar.h +++ b/src/bar/bar.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -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); diff --git a/src/bar/text.c b/src/bar/text.c index 18c219a..14afe81 100644 --- a/src/bar/text.c +++ b/src/bar/text.c @@ -1,5 +1,7 @@ #include "bar/text.h" +#include +#include #include #include #include @@ -7,6 +9,7 @@ #include #include #include +#include #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); +} diff --git a/src/bar/text.h b/src/bar/text.h index 0f74479..a55d672 100644 --- a/src/bar/text.h +++ b/src/bar/text.h @@ -1,9 +1,27 @@ #pragma once +#include #include +#include + +#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); diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 9484d0e..efbee58 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -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);