Skip to content

Commit

Permalink
override alias colors
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Feb 13, 2022
1 parent 49209b5 commit 657a2b0
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 24 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,6 @@ this draws a white background below all my space components. I plan to expand th
### Item Alias -- Mirror items of the original macOS status bar into sketchybar
It is possible to create an alias for default menu bar items (such as MeetingBar, etc.) in sketchybar. The default menu bar can be set to autohide and this should still work.

Important: <br>
I highly recommend setting a wallpaper on all spaces that makes the default menu bar items appear in either the light or the dark theme consitently.

It is now possible to create an alias of a default menu bar item with the following syntax:
```bash
sketchybar --add alias <application_name> <position>
Expand All @@ -383,6 +380,10 @@ All further default menu items currently available on your system can be found v
```bash
sketchybar --query default_menu_items
```
You can override the color of an alias via the property (HEAD only):
```bash
sketchybar --set <name> alias.color=<rgba_hex>
```

## Popup Menus
<img src="https://user-images.githubusercontent.com/22680421/146688291-b8bc5e77-e6a2-42ee-bd9f-b3709c63d936.png" width="300"> <br>
Expand Down
54 changes: 49 additions & 5 deletions src/alias.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "alias.h"
#include "misc/helpers.h"

extern void SLSCaptureWindowsContentsToRectWithOptions(uint32_t cid, uint32_t* wid, bool meh, CGRect bounds, uint32_t flags, CGImageRef* image);
extern void SLSCaptureWindowsContentsToRectWithOptions(uint32_t cid, uint64_t* wid, bool meh, CGRect bounds, uint32_t flags, CGImageRef* image);
extern int SLSGetScreenRectForWindow(uint32_t cid, uint32_t wid, CGRect* out);

void print_all_menu_items(FILE* rsp) {
Expand Down Expand Up @@ -58,11 +58,19 @@ void alias_get_permission(struct alias* alias) {
#endif
}

void alias_init(struct alias* alias, char* owner, char* name) {
alias->name = name;
alias->owner = owner;
void alias_init(struct alias* alias) {
alias->name = NULL;
alias->owner = NULL;
alias->wid = 0;
alias->color_override = false;
alias->color = rgba_color_from_hex(0xffff0000);

image_init(&alias->image);
}

void alias_setup(struct alias* alias, char* owner, char* name) {
alias->name = name;
alias->owner = owner;
alias_get_permission(alias);
alias_update_image(alias);
}
Expand Down Expand Up @@ -137,15 +145,51 @@ bool alias_update_image(struct alias* alias) {
}

void alias_draw(struct alias* alias, CGContextRef context) {
image_draw(&alias->image, context);
if (alias->color_override) {
CGContextSaveGState(context);
image_draw(&alias->image, context);
CGContextClipToMask(context, alias->image.bounds, alias->image.image_ref);
CGContextSetRGBFillColor(context, alias->color.r, alias->color.g, alias->color.b, alias->color.a);
CGContextFillRect(context, alias->image.bounds);
CGContextRestoreGState(context);
}
else {
image_draw(&alias->image, context);
}
}

void alias_destroy(struct alias* alias) {
image_destroy(&alias->image);
if (alias->name) free(alias->name);
if (alias->owner) free(alias->owner);
alias->name = NULL;
alias->owner = NULL;
}

void alias_calculate_bounds(struct alias* alias, uint32_t x, uint32_t y) {
image_calculate_bounds(&alias->image, x, y);
}

static bool alias_parse_sub_domain(struct alias* alias, FILE* rsp, struct token property, char* message) {
struct key_value_pair key_value_pair = get_key_value_pair(property.text, '.');
if (key_value_pair.key && key_value_pair.value) {
struct token subdom = { key_value_pair.key, strlen(key_value_pair.key) };
struct token entry = { key_value_pair.value, strlen(key_value_pair.value) };
if (token_equals(subdom, SUB_DOMAIN_SHADOW))
return shadow_parse_sub_domain(&alias->image.shadow, rsp, entry, message);
else {
fprintf(rsp, "Invalid subdomain: %s \n", subdom.text);
printf("Invalid subdomain: %s \n", subdom.text);
}
}
else if (token_equals(property, PROPERTY_COLOR)) {
alias->color = rgba_color_from_hex(token_to_uint32t(get_token(&message)));
alias->color_override = true;
return true;
} else {
fprintf(rsp, "Unknown property: %s \n", property.text);
printf("Unknown property: %s \n", property.text);
}
return false;
}

10 changes: 8 additions & 2 deletions src/alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ struct alias {
char* owner;

uint64_t pid;
uint32_t wid;
uint64_t wid;

bool color_override;
struct rgba_color color;
struct image image;

};

void alias_init(struct alias* alias, char* owner, char* name);
void alias_init(struct alias* alias);
void alias_setup(struct alias* alias, char* owner, char* name);
bool alias_update_image(struct alias* alias);
void alias_find_window(struct alias* alias);
uint32_t alias_get_length(struct alias* alias);
Expand All @@ -27,4 +31,6 @@ void alias_destroy(struct alias* alias);

void print_all_menu_items(FILE* rsp);

static bool alias_parse_sub_domain(struct alias* alias, FILE* rsp, struct token property, char* message);

#endif
2 changes: 2 additions & 0 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
background_init(&bar_item->background);
env_vars_init(&bar_item->signal_args.env_vars);
popup_init(&bar_item->popup);
graph_init(&bar_item->graph);
alias_init(&bar_item->alias);

if (default_item) bar_item_inherit_from_item(bar_item, default_item);
}
Expand Down
14 changes: 9 additions & 5 deletions src/graph.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#include "graph.h"

void graph_init(struct graph* graph, uint32_t width) {
graph->width = width;
graph->y = malloc(sizeof(float) * width);
memset(graph->y, 0, sizeof(float) * width);
void graph_init(struct graph* graph) {
graph->width = 0;
graph->cursor = 0;

graph->line_color = rgba_color_from_hex(0xcccccc);
graph->fill_color = rgba_color_from_hex(0xcccccc);
graph->line_width = 0.5;
graph->line_width = 0.5;
graph->fill = true;
graph->overrides_fill_color = false;
graph->enabled = true;
}

void graph_setup(struct graph* graph, uint32_t width) {
graph->width = width;
graph->y = malloc(sizeof(float) * width);
memset(graph->y, 0, sizeof(float) * width);
}

float graph_get_y(struct graph* graph, uint32_t i) {
if (!graph->enabled) return 0.f;
return graph->y[ (graph->cursor + i)%graph->width ];
Expand Down
3 changes: 2 additions & 1 deletion src/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct graph {
struct rgba_color fill_color;
};

void graph_init(struct graph* graph, uint32_t width);
void graph_init(struct graph* graph);
void graph_setup(struct graph* graph, uint32_t width);
void graph_push_back(struct graph* graph, float y);
float graph_get_y(struct graph* graph, uint32_t i);
uint32_t graph_get_length(struct graph* graph);
Expand Down
14 changes: 14 additions & 0 deletions src/image.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "image.h"
#include "misc/helpers.h"
#include "shadow.h"

void image_init(struct image* image) {
image->enabled = false;
Expand All @@ -8,6 +9,8 @@ void image_init(struct image* image) {
image->bounds = CGRectNull;
image->size = CGSizeZero;
image->scale = 1.0;

shadow_init(&image->shadow);
}

bool image_set_enabled(struct image* image, bool enabled) {
Expand Down Expand Up @@ -91,6 +94,17 @@ void image_calculate_bounds(struct image* image, uint32_t x, uint32_t y) {

void image_draw(struct image* image, CGContextRef context) {
if (!image->image_ref) return;

// if (image->shadow.enabled) {
// CGContextSaveGState(context);
// CGRect sbounds = shadow_get_bounds(&image->shadow, image->bounds);
// CGContextDrawImage(context, sbounds, image->image_ref);
// CGContextClipToMask(context, sbounds, image->image_ref);
// CGContextSetRGBFillColor(context, image->shadow.color.r, image->shadow.color.g, image->shadow.color.b, image->shadow.color.a);
// CGContextFillRect(context, sbounds);
// CGContextRestoreGState(context);
// }

CGContextDrawImage(context, image->bounds, image->image_ref);
}

Expand Down
3 changes: 3 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct image {

CGImageRef image_ref;
CFDataRef data_ref;

struct shadow shadow;
};

void image_init(struct image* image);
Expand All @@ -24,4 +26,5 @@ void image_draw(struct image* image, CGContextRef context);
void image_clear_pointers(struct image* image);
void image_destroy(struct image* image);

static bool image_parse_sub_domain(struct image* image, FILE* rsp, struct token property, char* message);
#endif
8 changes: 5 additions & 3 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
} else if (command.length > 0) {
if (bar_item->type == BAR_COMPONENT_GRAPH) {
struct token width = get_token(&message);
graph_init(&bar_item->graph, token_to_uint32t(width));
graph_setup(&bar_item->graph, token_to_uint32t(width));
}
else if (bar_item->type == BAR_COMPONENT_ALIAS) {
char* tmp_name = string_copy(name.text);
struct key_value_pair key_value_pair = get_key_value_pair(tmp_name, ',');
if (!key_value_pair.key || !key_value_pair.value)
alias_init(&bar_item->alias, token_to_string(name), NULL);
alias_setup(&bar_item->alias, token_to_string(name), NULL);
else
alias_init(&bar_item->alias, string_copy(key_value_pair.key), string_copy(key_value_pair.value));
alias_setup(&bar_item->alias, string_copy(key_value_pair.key), string_copy(key_value_pair.value));
free(tmp_name);
}
else if (bar_item->type == BAR_COMPONENT_GROUP) {
Expand Down Expand Up @@ -205,6 +205,8 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
needs_update = graph_parse_sub_domain(&bar_item->graph, rsp, entry, message);
else if (token_equals(subdom, SUB_DOMAIN_POPUP))
needs_update = popup_parse_sub_domain(&bar_item->popup, rsp, entry, message);
else if (token_equals(subdom, SUB_DOMAIN_ALIAS))
needs_update = alias_parse_sub_domain(&bar_item->alias, rsp, entry, message);
else {
fprintf(rsp, "Invalid subdomain: %s \n", subdom.text);
printf("Invalid subdomain: %s \n", subdom.text);
Expand Down
1 change: 1 addition & 0 deletions src/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define SUB_DOMAIN_LABEL "label"
#define SUB_DOMAIN_BACKGROUND "background"
#define SUB_DOMAIN_GRAPH "graph"
#define SUB_DOMAIN_ALIAS "alias"
#define SUB_DOMAIN_POPUP "popup"
#define SUB_DOMAIN_SHADOW "shadow"
#define SUB_DOMAIN_IMAGE "image"
Expand Down
5 changes: 0 additions & 5 deletions src/misc/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ struct signal_args {
};

struct rgba_color {
bool is_valid;

float r;
float g;
float b;
float a;
uint32_t p;
};

struct token {
Expand All @@ -41,8 +38,6 @@ static uint32_t hex_from_rgba_color(struct rgba_color rgba_color) {

static struct rgba_color rgba_color_from_hex(uint32_t color) {
struct rgba_color result;
result.is_valid = true;
result.p = color;
result.r = ((color >> 16) & 0xff) / 255.0;
result.g = ((color >> 8) & 0xff) / 255.0;
result.b = ((color >> 0) & 0xff) / 255.0;
Expand Down

0 comments on commit 657a2b0

Please sign in to comment.