Skip to content

Commit

Permalink
added configurable bar border
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Sep 13, 2021
1 parent aa31ba6 commit 09dfee4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
@@ -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 <string.h>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions src/misc/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 09dfee4

Please sign in to comment.