From 09dfee4dd7ea101f2945d56481ac99184a31a1bf Mon Sep 17 00:00:00 2001 From: FelixKratz Date: Mon, 13 Sep 2021 23:58:19 +0200 Subject: [PATCH] added configurable bar border --- README.md | 2 ++ src/bar.c | 4 ++-- src/bar_manager.c | 14 ++++++++++++++ src/bar_manager.h | 5 +++++ src/message.c | 9 +++++++++ src/misc/helpers.h | 12 +++++++----- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0cda4540..787dd592 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,8 @@ where the settings currently are: * *margin*: the screen padding around the bar itself * *y_offset*: the y-offset in pixels from the default position * *corner_radius*: the corner radius of the bar itself +* *border_width*: the width of the bars border +* *border_color*: the color of the bars border * *blur_radius*: the blur radius to be applied to the background of the bar itself * *padding_left*: padding on the left before first item * *padding_right*: just as padding_right diff --git a/src/bar.c b/src/bar.c index 4d6f16a6..9592b4da 100644 --- a/src/bar.c +++ b/src/bar.c @@ -148,14 +148,14 @@ void bar_draw_graph(struct bar* bar, struct bar_item* bar_item, uint32_t x, bool void bar_draw_item_background(struct bar* bar, struct bar_item* bar_item, uint32_t sid) { if (!bar_item->draws_background) return; CGRect draw_region = {{bar_item->bounding_rects[sid - 1]->origin.x - bar->origin.x, 0}, {bar_item->bounding_rects[sid - 1]->size.width, bar->frame.size.height}}; - draw_rect(bar->context, draw_region, bar_item->background_color, 0); + draw_rect(bar->context, draw_region, &bar_item->background_color, 0, 0, NULL); } void bar_redraw(struct bar* bar) { SLSDisableUpdate(g_connection); SLSOrderWindow(g_connection, bar->id, -1, 0); - draw_rect(bar->context, bar->frame, g_bar_manager.background_color, g_bar_manager.corner_radius); + draw_rect(bar->context, bar->frame, &g_bar_manager.background_color, g_bar_manager.corner_radius, g_bar_manager.border_width, &g_bar_manager.border_color); uint32_t did = bar->adid; uint32_t sid = bar->sid; diff --git a/src/bar_manager.c b/src/bar_manager.c index 478b671b..fd660e1b 100644 --- a/src/bar_manager.c +++ b/src/bar_manager.c @@ -1,6 +1,7 @@ #include "bar_manager.h" #include "bar.h" #include "bar_item.h" +#include "misc/helpers.h" #include <_types/_uint32_t.h> #include @@ -41,6 +42,16 @@ void bar_manager_set_background_color(struct bar_manager* bar_manager, uint32_t bar_manager_refresh(bar_manager, true); } +void bar_manager_set_border_color(struct bar_manager* bar_manager, uint32_t color) { + bar_manager->border_color = rgba_color_from_hex(color); + bar_manager_refresh(bar_manager, true); +} + +void bar_manager_set_border_width(struct bar_manager* bar_manager, uint32_t width) { + bar_manager->border_width = width; + bar_manager_refresh(bar_manager, true); +} + void bar_manager_set_position(struct bar_manager* bar_manager, char *pos) { bar_manager->position = pos; bar_manager_resize(bar_manager); @@ -137,6 +148,9 @@ void bar_manager_init(struct bar_manager* bar_manager) { bar_manager->window_level = NSFloatingWindowLevel; bar_manager->hidden = false; bar_manager->topmost = false; + bar_manager->border_width = 0; + bar_manager->border_color = rgba_color_from_hex(0xffff0000); + bar_manager->background_color = rgba_color_from_hex(0x44000000); bar_item_init(&bar_manager->default_item, NULL); custom_events_init(&bar_manager->custom_events); diff --git a/src/bar_manager.h b/src/bar_manager.h index 231aae2f..8a70c89a 100644 --- a/src/bar_manager.h +++ b/src/bar_manager.h @@ -29,7 +29,10 @@ struct bar_manager { uint32_t y_offset; uint32_t padding_left; uint32_t padding_right; + + uint32_t border_width; struct rgba_color background_color; + struct rgba_color border_color; struct custom_events custom_events; }; @@ -54,6 +57,8 @@ void bar_manager_set_padding_right(struct bar_manager *bar_manager, uint32_t pad void bar_manager_set_display(struct bar_manager *bar_manager, char *display); void bar_manager_set_hidden(struct bar_manager *bar_manager, bool hidden); void bar_manager_set_topmost(struct bar_manager *bar_manager, bool topmost); +void bar_manager_set_border_width(struct bar_manager* bar_manager, uint32_t width); +void bar_manager_set_border_color(struct bar_manager* bar_manager, uint32_t color); void bar_manager_freeze(struct bar_manager *bar_manager); void bar_manager_unfreeze(struct bar_manager *bar_manager); diff --git a/src/message.c b/src/message.c index 1bea9739..a2dc526a 100644 --- a/src/message.c +++ b/src/message.c @@ -75,6 +75,8 @@ extern bool g_verbose; #define DOMAIN_CONFIG "config" #define COMMAND_CONFIG_DEBUG_OUTPUT "debug_output" #define COMMAND_CONFIG_BAR_BACKGROUND "bar_color" +#define COMMAND_CONFIG_BAR_BORDER_COLOR "border_color" +#define COMMAND_CONFIG_BAR_BORDER_WIDTH "border_width" #define COMMAND_CONFIG_BAR_POSITION "position" #define COMMAND_CONFIG_BAR_HEIGHT "height" #define COMMAND_CONFIG_BAR_YOFFSET "y_offset" @@ -449,6 +451,13 @@ static void handle_domain_config(FILE *rsp, struct token domain, char *message) } else if (token_equals(command, COMMAND_CONFIG_BAR_HEIGHT)) { struct token token = get_token(&message); bar_manager_set_height(&g_bar_manager, atoi(token.text)); + } else if (token_equals(command, COMMAND_CONFIG_BAR_BORDER_WIDTH)) { + struct token token = get_token(&message); + bar_manager_set_border_width(&g_bar_manager, atoi(token.text)); + } else if (token_equals(command, COMMAND_CONFIG_BAR_BORDER_COLOR)) { + struct token token = get_token(&message); + uint32_t color = token_to_uint32t(token); + bar_manager_set_border_color(&g_bar_manager, color); } else if (token_equals(command, COMMAND_CONFIG_BAR_MARGIN)) { struct token token = get_token(&message); g_bar_manager.margin = token_to_uint32t(token); diff --git a/src/misc/helpers.h b/src/misc/helpers.h index 1648dcaa..911f15cc 100644 --- a/src/misc/helpers.h +++ b/src/misc/helpers.h @@ -46,14 +46,16 @@ static inline bool string_equals(const char *a, const char *b) { return a && b && strcmp(a, b) == 0; } -static inline void draw_rect(CGContextRef context, CGRect region, struct rgba_color color, uint32_t corner_radius) { - CGContextClearRect(context, region); - CGContextSetRGBFillColor(context, color.r, color.g, color.b, color.a); +static inline void draw_rect(CGContextRef context, CGRect region, struct rgba_color* fill_color, uint32_t corner_radius, uint32_t line_width, struct rgba_color* stroke_color ) { + CGContextSetLineWidth(context, line_width); + if (stroke_color) CGContextSetRGBStrokeColor(context, stroke_color->r, stroke_color->g, stroke_color->b, stroke_color->a); + CGContextSetRGBFillColor(context, fill_color->r, fill_color->g, fill_color->b, fill_color->a); + CGContextClearRect(context, region); CGMutablePathRef path = CGPathCreateMutable(); - CGPathAddRoundedRect(path, NULL, region, corner_radius, corner_radius); + CGPathAddRoundedRect(path, NULL, CGRectInset(region, line_width / 2, line_width / 2), corner_radius, corner_radius); CGContextAddPath(context, path); - CGContextFillPath(context); + CGContextDrawPath(context, kCGPathFillStroke); CFRelease(path); }