added option to comletely remove an item

This commit is contained in:
FelixKratz 2021-08-31 00:32:33 +02:00
parent e505222dfe
commit d88cda7dbb
8 changed files with 82 additions and 7 deletions

View file

@ -199,6 +199,12 @@ This pushes the data point into the graph with name *name*.
```bash
sketchybar -m update
```
### Completely remove an item
```bash
sketchybar -m remove item <name>
```
This also works for components, just reference it by name.
## Credits
yabai,

View file

@ -1,4 +1,5 @@
#include "bar_item.h"
#include "graph_data.h"
struct bar_item* bar_item_create() {
struct bar_item* bar_item = malloc(sizeof(struct bar_item));
@ -14,6 +15,7 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
bar_item->counter = 0;
bar_item->name = "";
bar_item->type = BAR_ITEM;
bar_item->identifier = "";
bar_item->update_frequency = 0;
bar_item->cache_scripts = false;
bar_item->script = "";
@ -23,10 +25,12 @@ 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 = "";
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 = "";
bar_item->label_spacing_left = 0;
bar_item->label_spacing_right = 0;
bar_item->label_color = rgba_color_from_hex(0xffffffff);
@ -182,3 +186,25 @@ void bar_item_set_bounding_rect_for_space(struct bar_item* bar_item, uint32_t si
bar_item->bounding_rects[sid - 1]->origin.y = rect.origin.y + bar_origin.y;
bar_item->bounding_rects[sid - 1]->size = rect.size;
}
void bar_item_destroy(struct bar_item* bar_item) {
if (bar_item->name) free(bar_item->name);
if (bar_item->script && !bar_item->cache_scripts) free(bar_item->script);
if (bar_item->on_click_script && !bar_item->cache_scripts) free(bar_item->on_click_script);
if (bar_item->icon) free(bar_item->icon);
if (bar_item->icon_font_name) free(bar_item->icon_font_name);
if (bar_item->label) free(bar_item->label);
if (bar_item->label_font_name) free(bar_item->label_font_name);
if (bar_item->identifier) free(bar_item->identifier);
if (bar_item->bounding_rects) {
for (int j = 0; j < bar_item->num_rects; j++) {
free(bar_item->bounding_rects[j]);
}
free(bar_item->bounding_rects);
}
if (bar_item->has_graph) {
graph_data_destroy(&bar_item->graph_data);
}
free(bar_item);
}

View file

@ -68,6 +68,7 @@ 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);

View file

@ -1,5 +1,7 @@
#include "bar_manager.h"
#include "bar_item.h"
#include <_types/_uint32_t.h>
#include <string.h>
extern struct event_loop g_event_loop;
@ -18,6 +20,15 @@ int bar_manager_get_item_index_for_name(struct bar_manager* bar_manager, char* n
return -1;
}
int bar_manager_get_item_index_by_address(struct bar_manager* bar_manager, struct bar_item* bar_item) {
for (int i = 0; i < bar_manager->bar_item_count; i++) {
if (bar_manager->bar_items[i] == bar_item) {
return i;
}
}
return -1;
}
void bar_manager_set_background_color(struct bar_manager *bar_manager, uint32_t color)
{
bar_manager->background_color = rgba_color_from_hex(color);
@ -87,6 +98,14 @@ struct bar_item* bar_manager_create_item(struct bar_manager* bar_manager) {
return bar_item;
}
void bar_manager_destroy_item(struct bar_manager* bar_manager, struct bar_item* bar_item) {
int index = bar_manager_get_item_index_by_address(bar_manager, bar_item);
memmove(bar_manager->bar_items + index, bar_manager->bar_items + index + 1, bar_manager->bar_item_count - index - 1);
bar_manager->bar_items = (struct bar_item**) realloc(bar_manager->bar_items, sizeof(struct bar_item*) * (bar_manager->bar_item_count - 1));
bar_manager->bar_item_count -= 1;
bar_item_destroy(bar_item);
}
void bar_manager_init(struct bar_manager *bar_manager)
{
bar_manager->bars = NULL;

View file

@ -30,6 +30,7 @@ void bar_manager_custom_events_trigger(struct bar_manager* bar_manager, char* na
struct bar_item* bar_manager_create_item(struct bar_manager* bar_manager);
void bar_manager_destroy_item(struct bar_manager* bar_manager, struct bar_item* 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);

View file

@ -9,7 +9,7 @@ void graph_data_init(struct graph_data* graph_data, uint32_t graph_width) {
graph_data->ready = true;
}
void graph_data_destruct(struct graph_data* graph_data) {
void graph_data_destroy(struct graph_data* graph_data) {
if (!graph_data->ready) return;
free(graph_data->y);
}

View file

@ -11,6 +11,7 @@ struct graph_data {
};
void graph_data_init(struct graph_data* graph_data, uint32_t graph_width);
void graph_data_destroy(struct graph_data* graph_data);
void graph_data_push_back(struct graph_data* graph_data, float y);
void graph_data_destruct(struct graph_data* graph_data);
float graph_data_get_y(struct graph_data* graph_data, uint32_t i);

View file

@ -1,5 +1,6 @@
#include "message.h"
#include "bar_item.h"
#include "bar_manager.h"
extern struct event_loop g_event_loop;
extern struct display_manager g_display_manager;
@ -17,6 +18,8 @@ extern bool g_verbose;
#define COMMAND_ADD_PLUGIN "plugin"
#define COMMAND_ADD_EVENT "event"
#define DOMAIN_REMOVE "remove"
#define DOMAIN_UPDATE "update"
#define DOMAIN_PUSH "push"
@ -247,6 +250,16 @@ static void handle_domain_push(FILE* rsp, struct token domain, char* message) {
bar_manager_refresh(&g_bar_manager);
}
static void handle_domain_remove(FILE* rsp, struct token domain, char* message) {
struct token command = get_token(&message);
if (token_equals(command, COMMAND_ADD_ITEM) || token_equals(command, COMMAND_ADD_COMPONENT)) {
struct token name = get_token(&message);
int index = bar_manager_get_item_index_for_name(&g_bar_manager, token_to_string(name));
if (index < 0) return;
bar_manager_destroy_item(&g_bar_manager, g_bar_manager.bar_items[index]);
}
bar_manager_refresh(&g_bar_manager);
}
// Syntax: sketchybar -m add <item|component|plugin> (<identifier>) <name> <position>
static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
struct token command = get_token(&message);
@ -302,6 +315,12 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
exit(1);
}
if (bar_manager_get_item_index_for_name(&g_bar_manager, token_to_string(name)) >= 0) {
bar_manager_destroy_item(&g_bar_manager, bar_item);
printf("Name already exists... skipping");
return;
}
bar_item->position = token_to_string(position)[0];
bar_item_set_name(bar_item, string_copy(token_to_string(name)));
@ -483,6 +502,8 @@ void handle_message(FILE *rsp, char *message)
handle_domain_config(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_ADD)){
handle_domain_add(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_REMOVE)){
handle_domain_remove(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_SET)){
handle_domain_set(rsp, domain, message);
} else if (token_equals(domain, DOMAIN_UPDATE)) {