Skip to content

Commit

Permalink
make the space component more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Sep 4, 2021
1 parent 582fa05 commit eebd5eb
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 31 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,18 @@ A list of properties is listed below:
* *label*: the label of the item
* *label_font*: the font for the label
* *label_color*: the color of the label
* *label_highlight_color*: the highlight color of the label (e.g. for active space icon)
* *label_padding_left*: left padding of label (default: 0)
* *label_padding_right*: right padding of label (default: 0)
* *icon_highlight*: wether the icon is highlighted with the *icon_highlight_color* (values: *on*, *off*, default: *off*)

* *icon*: the icon of the item
* *icon_font*: the font for the icon
* *icon_color*: the color of the icon
* *icon_highlight_color*: the highlight color of the icon (e.g. for active space icon)
* *icon_padding_left*: left padding of icon (default: 0)
* *icon_padding_right*: right padding of icon (default: 0)
* *label_highlight*: wether the label is highlighted with the *label_highlight_color* (values: *on*, *off*, default: *off*)

* *graph_color*: color of the associated graph

Expand All @@ -186,11 +189,13 @@ sketchybar -m default <property> <value>
this currently works for the properties:
* *label_font*
* *label_color*
* *label_highlight_color*
* *label_padding_left*
* *label_padding_right*

* *icon_font*
* *icon_color*
* *icon_highlight_color*
* *icon_padding_left*
* *icon_padding_right*
* *update_freq*
Expand Down Expand Up @@ -255,6 +260,28 @@ This stops the redrawing of the bar entirely and "freezes" it. Can be used durin
create a cleaner startup by freezing the bar at the beginning of the configuration and unfreezing it after the
setup is done.

## Scripting
The bar supports scripts where ever possible to make it as customizable and versatile as possible.
When an item invokes a script, the script has access to some environment variables, such as:
```bash
$NAME
```
Which is the name of the item that has invoked the script.
The space component has an additional variable
```bash
$SELECTED
```
which has the value *true* if the associated space is selected and *false* if the selected space is not selected.
By default the space component invokes the script:
```bash
if [ "$SELECTED" = "true" ]; then
sketchybar -m set $NAME icon_highlight on
else
sketchybar -m set $NAME icon_highlight off
fi
```
which you can freely configure to your liking by supplying a different script to the space component.
I plan on increasing the available environment variables in scripting step by step but if you have a suggestion let me know in the issues.

## Credits
yabai,
Expand Down
9 changes: 9 additions & 0 deletions plugins/space.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

if [ "$SELECTED" = "true" ]; then
sketchybar -m set $NAME icon_highlight on
sketchybar -m set $NAME label_highlight on
else
sketchybar -m set $NAME icon_highlight off
sketchybar -m set $NAME label_highlight off
fi
14 changes: 13 additions & 1 deletion src/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ bool bar_draw_graphs(struct bar* bar, struct bar_item* bar_item, uint32_t x, boo
return false;
}

void bar_refresh_components(struct bar *bar) {
uint32_t did = display_arrangement(bar->did);
uint32_t sid = mission_control_index(display_space_id(bar->did));
if (sid == 0) return;
for (int i = 0; i < g_bar_manager.bar_item_count; i++) {
struct bar_item* bar_item = g_bar_manager.bar_items[i];
if(strcmp(bar_item->identifier, BAR_COMPONENT_SPACE)== 0 && (bar_item->associated_space & (1 << sid)) && (1 << did) & bar_item->associated_display) bar_item->selected = true;
else bar_item->selected = false;
}
}

void bar_refresh(struct bar *bar) {
SLSDisableUpdate(g_connection);
SLSOrderWindow(g_connection, bar->id, -1, 0);
Expand All @@ -166,7 +177,6 @@ void bar_refresh(struct bar *bar) {
uint32_t did = display_arrangement(bar->did);
uint32_t sid = mission_control_index(display_space_id(bar->did));
if (sid == 0) return;
bar_manager_update_components(&g_bar_manager, did, sid);

int bar_left_final_item_x = g_bar_manager.padding_left;
int bar_right_first_item_x = bar->frame.size.width - g_bar_manager.padding_right;
Expand All @@ -179,6 +189,8 @@ void bar_refresh(struct bar *bar) {

if(bar_item->associated_display > 0 && !(bar_item->associated_display & (1 << did))) continue;
if((strcmp(bar_item->identifier, BAR_COMPONENT_SPACE) != 0) && bar_item->associated_space > 0 && !(bar_item->associated_space & (1 << sid))) continue;
if(strcmp(bar_item->identifier, BAR_COMPONENT_SPACE)== 0 && (bar_item->associated_space & (1 << sid))) bar_item->selected = true;
else bar_item->selected = false;

struct bar_line* label = &bar_item->label_line;
struct bar_line* icon = &bar_item->icon_line;
Expand Down
1 change: 1 addition & 0 deletions src/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct bar {
CGPoint origin;
};

void bar_refresh_components(struct bar *bar);
void bar_refresh(struct bar *bar);
void bar_resize(struct bar *bar);
struct bar *bar_create(uint32_t did);
Expand Down
37 changes: 17 additions & 20 deletions src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
bar_item->scripting = true;
bar_item->is_shown = false;
bar_item->nospace = false;
bar_item->selected = false;
bar_item->counter = 0;
bar_item->name = "";
bar_item->type = BAR_ITEM;
Expand All @@ -25,15 +26,18 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
bar_item->associated_space = 0;
bar_item->icon_font_name = "Hack Nerd Font:Bold:14.0";
bar_item->label_font_name = "Hack Nerd Font:Bold:14.0";
bar_item->icon_highlight = false;
bar_item->icon = "";
bar_item->icon_spacing_left = 0;
bar_item->icon_spacing_right = 0;
bar_item->icon_color = rgba_color_from_hex(0xffffffff);
bar_item->icon_highlight_color = rgba_color_from_hex(0xffffffff);
bar_item->label_highlight = false;
bar_item->label = "";
bar_item->label_spacing_left = 0;
bar_item->label_spacing_right = 0;
bar_item->label_color = rgba_color_from_hex(0xffffffff);
bar_item->label_highlight_color = rgba_color_from_hex(0xffffffff);
bar_item->has_graph = false;
bar_item->num_rects = 0;
bar_item->bounding_rects = NULL;
Expand All @@ -51,44 +55,37 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
bar_item->label_spacing_right = default_item->label_spacing_right;
bar_item->update_frequency = default_item->update_frequency;
bar_item->cache_scripts = default_item->cache_scripts;
bar_item->icon_highlight_color = default_item->icon_highlight_color;
bar_item->label_highlight_color = default_item->label_highlight_color;
}

bar_item_set_icon(bar_item, string_copy(""), bar_item->icon_color);
bar_item_set_icon(bar_item, string_copy(""));
bar_item_set_icon_font(bar_item, string_copy(bar_item->icon_font_name));
bar_item_set_label_font(bar_item, string_copy(bar_item->label_font_name));
bar_item_set_label(bar_item, string_copy(""));
}

void bar_item_script_update(struct bar_item* bar_item, bool forced) {
if (!bar_item->scripting || (bar_item->update_frequency == 0 && !forced)) return;
if (strcmp(bar_item->script, "") != 0) {
if (strlen(bar_item->script) > 0) {
bar_item->counter++;
if (bar_item->update_frequency <= bar_item->counter || forced) {
bar_item->counter = 0;
strncpy(&bar_item->signal_args.name[1][0], "SELECTED", 255);
strncpy(&bar_item->signal_args.value[1][0], bar_item->selected ? "true" : "false", 255);
fork_exec(bar_item->script, &bar_item->signal_args);
}
}
}

void bar_item_update_component(struct bar_item* bar_item, uint32_t did, uint32_t sid) {
if (bar_item->type == BAR_COMPONENT) {
if (strcmp(bar_item->identifier, BAR_COMPONENT_SPACE) == 0) {
if ((1 << sid) & bar_item->associated_space && (1 << did) & bar_item->associated_display)
bar_item_set_icon(bar_item, bar_item->icon, bar_item->icon_highlight_color);
else
bar_item_set_icon(bar_item, bar_item->icon, bar_item->icon_color);
}
}
}

void bar_item_set_name(struct bar_item* bar_item, char* name) {
if (!name) return;
if (name != bar_item->name && !bar_item->name) {
free(bar_item->name);
}
bar_item->name = name;
strncpy(bar_item->signal_args.name[0], "NAME", 255);
strncpy(bar_item->signal_args.value[0], name, 255);
strncpy(&bar_item->signal_args.name[0][0], "NAME", 255);
strncpy(&bar_item->signal_args.value[0][0], name, 255);
}

void bar_item_set_script(struct bar_item* bar_item, char* script) {
Expand All @@ -111,18 +108,18 @@ void bar_item_set_click_script(struct bar_item* bar_item, char* script) {
bar_item->click_script = script;
}

void bar_item_set_icon(struct bar_item* bar_item, char* icon, struct rgba_color color) {
void bar_item_set_icon(struct bar_item* bar_item, char* icon) {
if (bar_item->icon_line.line)
bar_destroy_line(bar_item->icon_line);
if (icon != bar_item->icon && !bar_item->icon)
free(bar_item->icon);
bar_item->icon = icon;
bar_item->icon_line = bar_prepare_line(bar_item->icon_font, bar_item->icon, color);
bar_item->icon_line = bar_prepare_line(bar_item->icon_font, bar_item->icon, bar_item->icon_highlight ? bar_item->icon_highlight_color : bar_item->icon_color);
}

void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color) {
bar_item->icon_color = rgba_color_from_hex(color);
bar_item_set_icon(bar_item, bar_item->icon, bar_item->icon_color);
bar_item_set_icon(bar_item, bar_item->icon);
}

void bar_item_set_label(struct bar_item* bar_item, char* label) {
Expand All @@ -132,7 +129,7 @@ void bar_item_set_label(struct bar_item* bar_item, char* label) {
if (label != bar_item->label && !bar_item->label)
free(bar_item->label);
bar_item->label = label;
bar_item->label_line = bar_prepare_line(bar_item->label_font, bar_item->label, bar_item->label_color);
bar_item->label_line = bar_prepare_line(bar_item->label_font, bar_item->label, bar_item->label_highlight ? bar_item->label_highlight_color : bar_item->label_color);
}

void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color) {
Expand Down Expand Up @@ -161,7 +158,7 @@ void bar_item_on_click(struct bar_item* bar_item) {
if (!bar_item) return;
if (!bar_item->scripting) return;
if (bar_item && strlen(bar_item->click_script) > 0)
fork_exec(bar_item->click_script, NULL);
fork_exec(bar_item->click_script, &bar_item->signal_args);
}

CGRect bar_item_construct_bounding_rect(struct bar_item* bar_item) {
Expand Down
6 changes: 4 additions & 2 deletions src/bar_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct bar_item {
bool scripting;
bool is_shown;
bool nospace;
bool selected;
int counter;
char type;
char* identifier;
Expand All @@ -37,6 +38,7 @@ struct bar_item {
char position;

// Icon properties
bool icon_highlight;
struct bar_line icon_line;
char* icon;
char* icon_font_name;
Expand All @@ -47,6 +49,7 @@ struct bar_item {
struct rgba_color icon_highlight_color;

// Label properties
bool label_highlight;
struct bar_line label_line;
char* label;
char* label_font_name;
Expand All @@ -71,14 +74,13 @@ struct bar_item {
struct bar_item* bar_item_create();
void bar_item_destroy(struct bar_item* bar_item);
void bar_item_script_update(struct bar_item* bar_item, bool forced);
void bar_item_update_component(struct bar_item* bar_item, uint32_t did, uint32_t sid);
void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item);
void bar_item_set_name(struct bar_item* bar_item, char* name);
void bar_item_set_script(struct bar_item* bar_item, char* script);
void bar_item_set_click_script(struct bar_item* bar_item, char* script);
void bar_item_set_padding_left(struct bar_item* bar_item, uint32_t pad);
void bar_item_set_padding_right(struct bar_item* bar_item, uint32_t pad);
void bar_item_set_icon(struct bar_item* bar_item, char* icon, struct rgba_color color);
void bar_item_set_icon(struct bar_item* bar_item, char* icon);
void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color);
void bar_item_set_label(struct bar_item* bar_item, char* label);
void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color);
Expand Down
8 changes: 4 additions & 4 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ void bar_manager_init(struct bar_manager *bar_manager) {
CFRunLoopAddTimer(CFRunLoopGetMain(), bar_manager->shell_refresh_timer, kCFRunLoopCommonModes);
}

void bar_manager_update_components(struct bar_manager* bar_manager, uint32_t did, uint32_t sid) {
for (int i = 0; i < bar_manager->bar_item_count; i++) {
struct bar_item* bar_item = bar_manager->bar_items[i];
bar_item_update_component(bar_item, did, sid);
void bar_manager_update_components(struct bar_manager* bar_manager) {
for (int i = 0; i < bar_manager->bar_count; i++) {
bar_refresh_components(bar_manager->bars[i]);
}
}

Expand Down Expand Up @@ -190,6 +189,7 @@ void bar_manager_handle_front_app_switch(struct bar_manager* bar_manager) {
}

void bar_manager_handle_space_change(struct bar_manager* bar_manager) {
bar_manager_update_components(bar_manager);
bar_manager_check_bar_items_for_update_pattern(bar_manager, UPDATE_SPACE_CHANGE);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void bar_manager_destroy_item(struct bar_manager* bar_manager, struct bar_item*
void bar_manager_handle_notification(struct bar_manager* bar_manager, char* context);

void bar_manager_script_update(struct bar_manager* bar_manager, bool forced);
void bar_manager_update_components(struct bar_manager* bar_manager, uint32_t did, uint32_t sid);
void bar_manager_update_components(struct bar_manager* bar_manager);
void bar_manager_set_background_blur(struct bar_manager* bar_manager, uint32_t radius);
void bar_manager_set_background_color(struct bar_manager *bar_manager, uint32_t color);
void bar_manager_set_position(struct bar_manager *bar_manager, char *pos);
Expand Down
28 changes: 25 additions & 3 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ extern bool g_verbose;
#define COMMAND_SET_ICON_FONT "icon_font"
#define COMMAND_SET_ICON_COLOR "icon_color"
#define COMMAND_SET_ICON_HIGHLIGHT_COLOR "icon_highlight_color"
#define COMMAND_SET_ICON_HIGHLIGHT "icon_highlight"
#define COMMAND_SET_GRAPH_COLOR "graph_color"
#define COMMAND_SET_LABEL "label"
#define COMMAND_SET_LABEL_COLOR "label_color"
#define COMMAND_SET_LABEL_FONT "label_font"
#define COMMAND_SET_LABEL_HIGHLIGHT_COLOR "label_highlight_color"
#define COMMAND_SET_LABEL_HIGHLIGHT "label_highlight"
#define COMMAND_SET_CACHE_SCRIPTS "cache_scripts"

#define DOMAIN_SUBSCRIBE "subscribe"
Expand Down Expand Up @@ -230,6 +233,12 @@ static void handle_domain_default(FILE* rsp, struct token domain, char* message)
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
struct token value = get_token(&message);
bar_item->label_spacing_right = token_to_uint32t(value);
} else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT_COLOR)) {
struct token value = get_token(&message);
bar_item->icon_highlight_color = rgba_color_from_hex(token_to_uint32t(value));
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT_COLOR)) {
struct token value = get_token(&message);
bar_item->label_highlight_color = rgba_color_from_hex(token_to_uint32t(value));
} else if (token_equals(property, COMMAND_SET_CACHE_SCRIPTS)) {
struct token value = get_token(&message);
bar_item->cache_scripts = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
Expand Down Expand Up @@ -293,7 +302,6 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
bar_item->identifier = token_to_string(name);
} else if (token_equals(command, COMMAND_ADD_COMPONENT)) {
struct token identifier = get_token(&message);
if (token_equals(identifier, "title")) printf("Title component deprecated please use windowTitle.sh script");
name = get_token(&message);
position = get_token(&message);

Expand All @@ -310,6 +318,10 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
}
bar_item->has_graph = true;
}
else if (strcmp(bar_item->identifier, BAR_COMPONENT_SPACE) == 0) {
bar_item->script = "if [ \"$SELECTED\" = \"true\" ]; then sketchybar -m set $NAME icon_highlight on; else sketchybar -m set $NAME icon_highlight off; fi";
bar_item->update_mask |= UPDATE_SPACE_CHANGE;
}
} else if (token_equals(command, COMMAND_ADD_PLUGIN)) {
struct token identifier = get_token(&message);
name = get_token(&message);
Expand All @@ -329,7 +341,6 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
return;
}
bar_item_set_name(bar_item, string_copy(token_to_string(name)));

bar_manager_refresh(&g_bar_manager);
}

Expand All @@ -346,7 +357,7 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];

if (token_equals(property, COMMAND_SET_ICON)) {
bar_item_set_icon(bar_item, string_copy(message), bar_item->icon_color);
bar_item_set_icon(bar_item, string_copy(message));
} else if (token_equals(property, COMMAND_SET_ICON_FONT)) {
bar_item_set_icon_font(bar_item, string_copy(message));
} else if (token_equals(property, COMMAND_SET_LABEL)) {
Expand All @@ -372,6 +383,9 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
} else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT_COLOR)) {
struct token value = get_token(&message);
bar_item->icon_highlight_color = rgba_color_from_hex(token_to_uint32t(value));
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT_COLOR)) {
struct token value = get_token(&message);
bar_item->label_highlight_color = rgba_color_from_hex(token_to_uint32t(value));
} else if (token_equals(property, COMMAND_SET_POSITION)) {
struct token value = get_token(&message);
bar_item->position = token_to_string(value)[0];
Expand Down Expand Up @@ -402,6 +416,14 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
} else if (token_equals(property, COMMAND_SET_DRAWING)) {
struct token value = get_token(&message);
bar_item->drawing = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT)) {
struct token value = get_token(&message);
bar_item->label_highlight = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
bar_item_set_label(bar_item, bar_item->label);
} else if (token_equals(property, COMMAND_SET_ICON_HIGHLIGHT)) {
struct token value = get_token(&message);
bar_item->icon_highlight = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
bar_item_set_icon(bar_item, bar_item->icon);
}
// DEPRECATED
else if (token_equals(property, COMMAND_SET_ENABLED)) {
Expand Down

0 comments on commit eebd5eb

Please sign in to comment.