added basic item querying

This commit is contained in:
FelixKratz 2021-09-26 19:46:13 +02:00
parent db8266e272
commit 906ec7a1aa
5 changed files with 131 additions and 37 deletions

View file

@ -132,9 +132,9 @@ static int bar_get_center_length(struct bar_manager* bar_manager) {
for (int i = 0; i < bar_manager->bar_item_count; i++) {
struct bar_item* bar_item = bar_manager->bar_items[i];
if (bar_item->position == BAR_POSITION_CENTER) {
total_length += bar_item->label_line.bounds.size.width + bar_item->icon_line.bounds.size.width + bar_item->icon_spacing_right + bar_item->label_spacing_left + (bar_item->has_graph ? bar_item->graph_data.graph_width : 0);
total_length += bar_item->label_line.bounds.size.width + bar_item->icon_line.bounds.size.width + bar_item->icon_padding_right + bar_item->label_padding_left + (bar_item->has_graph ? bar_item->graph_data.graph_width : 0);
if (i > 0) {
total_length += bar_manager->bar_items[i-1]->label_spacing_right + bar_item->icon_spacing_left;
total_length += bar_manager->bar_items[i-1]->label_padding_right + bar_item->icon_padding_left;
}
}
}
@ -190,13 +190,13 @@ void bar_redraw(struct bar* bar) {
bool graph_rtl = false;
if (bar_item->position == BAR_POSITION_LEFT) {
icon_position.x = bar_left_final_item_x + bar_item->icon_spacing_left + bar_item->background_padding_left;
label_position.x = icon_position.x + icon->bounds.size.width + bar_item->icon_spacing_right + bar_item->label_spacing_left;
icon_position.x = bar_left_final_item_x + bar_item->icon_padding_left + bar_item->background_padding_left;
label_position.x = icon_position.x + icon->bounds.size.width + bar_item->icon_padding_right + bar_item->label_padding_left;
if (!bar_item->nospace)
bar_left_final_item_x = label_position.x + label->bounds.size.width + bar_item->label_spacing_right + bar_item->background_padding_right;
bar_left_final_item_x = label_position.x + label->bounds.size.width + bar_item->label_padding_right + bar_item->background_padding_right;
if (bar_item->has_graph) {
graph_x = bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label_spacing_right + bar_item->background_padding_right : bar_left_final_item_x;
graph_x = bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label_padding_right + bar_item->background_padding_right : bar_left_final_item_x;
if (!bar_item->nospace)
bar_left_final_item_x += bar_item->graph_data.graph_width;
}
@ -205,13 +205,13 @@ void bar_redraw(struct bar* bar) {
}
}
else if (bar_item->position == BAR_POSITION_RIGHT) {
label_position.x = bar_right_first_item_x - label->bounds.size.width - bar_item->label_spacing_right - bar_item->background_padding_right;
icon_position.x = label_position.x - icon->bounds.size.width - bar_item->icon_spacing_right - bar_item->label_spacing_left + 1;
label_position.x = bar_right_first_item_x - label->bounds.size.width - bar_item->label_padding_right - bar_item->background_padding_right;
icon_position.x = label_position.x - icon->bounds.size.width - bar_item->icon_padding_right - bar_item->label_padding_left + 1;
if (!bar_item->nospace)
bar_right_first_item_x = icon_position.x - bar_item->icon_spacing_left - bar_item->background_padding_left;
bar_right_first_item_x = icon_position.x - bar_item->icon_padding_left - bar_item->background_padding_left;
if (bar_item->has_graph) {
graph_x = bar_item->nospace ? icon_position.x - bar_item->icon_spacing_left - bar_item->background_padding_left : bar_right_first_item_x;
graph_x = bar_item->nospace ? icon_position.x - bar_item->icon_padding_left - bar_item->background_padding_left : bar_right_first_item_x;
graph_rtl = true;
if (!bar_item->nospace)
bar_right_first_item_x -= bar_item->graph_data.graph_width;
@ -222,13 +222,13 @@ void bar_redraw(struct bar* bar) {
}
}
else if (bar_item->position == BAR_POSITION_CENTER) {
icon_position.x = bar_center_first_item_x + bar_item->icon_spacing_left + bar_item->background_padding_left;
label_position.x = icon_position.x + icon->bounds.size.width + bar_item->icon_spacing_right + bar_item->label_spacing_left;
icon_position.x = bar_center_first_item_x + bar_item->icon_padding_left + bar_item->background_padding_left;
label_position.x = icon_position.x + icon->bounds.size.width + bar_item->icon_padding_right + bar_item->label_padding_left;
if (!bar_item->nospace)
bar_center_first_item_x = label_position.x + label->bounds.size.width + bar_item->label_spacing_right + bar_item->background_padding_right;
bar_center_first_item_x = label_position.x + label->bounds.size.width + bar_item->label_padding_right + bar_item->background_padding_right;
if (bar_item->has_graph) {
graph_x = bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label_spacing_right + bar_item->background_padding_right : bar_center_first_item_x;
graph_x = bar_item->nospace ? label_position.x + label->bounds.size.width + bar_item->label_padding_right + bar_item->background_padding_right : bar_center_first_item_x;
if (!bar_item->nospace)
bar_center_first_item_x += bar_item->graph_data.graph_width;
}

View file

@ -19,10 +19,10 @@ void bar_item_inherit_from_item(struct bar_item* bar_item, struct bar_item* ance
bar_item->icon_font_name = ancestor->icon_font_name;
bar_item->label_color = ancestor->label_color;
bar_item->label_font_name = ancestor->label_font_name;
bar_item->icon_spacing_left = ancestor->icon_spacing_left;
bar_item->icon_spacing_right = ancestor->icon_spacing_right;
bar_item->label_spacing_left = ancestor->label_spacing_left;
bar_item->label_spacing_right = ancestor->label_spacing_right;
bar_item->icon_padding_left = ancestor->icon_padding_left;
bar_item->icon_padding_right = ancestor->icon_padding_right;
bar_item->label_padding_left = ancestor->label_padding_left;
bar_item->label_padding_right = ancestor->label_padding_right;
bar_item->update_frequency = ancestor->update_frequency;
bar_item->cache_scripts = ancestor->cache_scripts;
bar_item->icon_highlight_color = ancestor->icon_highlight_color;
@ -58,14 +58,14 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
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_padding_left = 0;
bar_item->icon_padding_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_padding_left = 0;
bar_item->label_padding_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;
@ -336,11 +336,11 @@ void bar_item_set_yoffset(struct bar_item* bar_item, int offset) {
CGRect bar_item_construct_bounding_rect(struct bar_item* bar_item) {
CGRect bounding_rect;
bounding_rect.origin = bar_item->icon_line.bounds.origin;
bounding_rect.origin.x -= bar_item->icon_spacing_left;
bounding_rect.origin.x -= bar_item->icon_padding_left;
bounding_rect.origin.y = bar_item->icon_line.bounds.origin.y < bar_item->label_line.bounds.origin.y ? bar_item->icon_line.bounds.origin.y : bar_item->label_line.bounds.origin.y;
bounding_rect.size.width = bar_item->label_line.bounds.size.width + bar_item->icon_line.bounds.size.width
+ bar_item->icon_spacing_left + bar_item->icon_spacing_right
+ bar_item->label_spacing_right + bar_item->label_spacing_left;
+ bar_item->icon_padding_left + bar_item->icon_padding_right
+ bar_item->label_padding_right + bar_item->label_padding_left;
bounding_rect.size.height = bar_item->label_line.bounds.size.height > bar_item->icon_line.bounds.size.height ? bar_item->label_line.bounds.size.height : bar_item->icon_line.bounds.size.height;
return bounding_rect;
}
@ -381,3 +381,78 @@ void bar_item_destroy(struct bar_item* bar_item) {
}
free(bar_item);
}
void bar_item_serialize(struct bar_item* bar_item, FILE* rsp) {
fprintf(rsp, "{\n"
"\t\"name\": \"%s\",\n"
"\t\"type\": \"%c\",\n"
"\t\"text\": {\n"
"\t\t\"icon\": \"%s\",\n"
"\t\t\"label\": \"%s\",\n"
"\t\t\"icon_font\": \"%s\",\n"
"\t\t\"label_font\": \"%s\"\n"
"\t},\n"
"\t\"geometry\": {\n"
"\t\t\"position\": \"%c\",\n"
"\t\t\"background_padding_left\": %d,\n"
"\t\t\"background_padding_right\": %d,\n"
"\t\t\"icon_padding_left\": %d,\n"
"\t\t\"icon_padding_right\": %d,\n"
"\t\t\"label_padding_left\": %d,\n"
"\t\t\"label_padding_right\": %d\n"
"\t},\n"
"\t\"style\": {\n"
"\t\t\"icon_color:\": \"0x%x\",\n"
"\t\t\"icon_highlight_color:\": \"0x%x\",\n"
"\t\t\"label_color:\": \"0x%x\",\n"
"\t\t\"label_highlight_color:\": \"0x%x\",\n"
"\t\t\"draws_background\": %d,\n"
"\t\t\"background_height\": %u,\n"
"\t\t\"background_corner_radius\": %u,\n"
"\t\t\"background_border_width\": %u,\n"
"\t\t\"background_color:\": \"0x%x\",\n"
"\t\t\"background_border_color:\": \"0x%x\"\n"
"\t},\n"
"\t\"state\": {\n"
"\t\t\"drawing\": %d,\n"
"\t\t\"updates\": %d,\n"
"\t\t\"lazy\": %d,\n"
"\t\t\"chache_scripts\": %d,\n"
"\t\t\"associated_bar_mask\": %u,\n"
"\t\t\"associated_display_mask\": %u,\n"
"\t\t\"associated_space_mask\": %u,\n"
"\t\t\"update_mask\": %u\n"
"\t}\n"
"}\n",
bar_item->name,
bar_item->type,
bar_item->icon,
bar_item->label,
bar_item->icon_font_name,
bar_item->label_font_name,
bar_item->position,
bar_item->background_padding_left,
bar_item->background_padding_right,
bar_item->icon_padding_left,
bar_item->icon_padding_right,
bar_item->label_padding_left,
bar_item->label_padding_right,
hex_from_rgba_color(bar_item->icon_color),
hex_from_rgba_color(bar_item->icon_highlight_color),
hex_from_rgba_color(bar_item->label_color),
hex_from_rgba_color(bar_item->label_highlight_color),
bar_item->draws_background,
bar_item->background_height,
bar_item->background_corner_radius,
bar_item->background_border_width,
hex_from_rgba_color(bar_item->background_color),
hex_from_rgba_color(bar_item->background_border_color),
bar_item->drawing,
bar_item->updates,
bar_item->lazy,
bar_item->cache_scripts,
bar_item->associated_bar,
bar_item->associated_display,
bar_item->associated_space,
bar_item->update_mask);
}

View file

@ -18,6 +18,7 @@
#define UPDATE_SYSTEM_WOKE 1 << 3
struct bar_item {
char* name;
bool needs_update;
bool lazy;
bool drawing;
@ -33,15 +34,11 @@ struct bar_item {
uint32_t associated_space;
uint32_t update_frequency;
// Execute with exec_fork, callback from command via messages
bool cache_scripts;
char* script;
char* click_script;
struct signal_args signal_args;
// Name by which to refer to the bar_item in the configuration
char* name;
// The position in the bar: l,r,c
char position;
int y_offset;
@ -62,8 +59,8 @@ struct bar_item {
char* icon;
char* icon_font_name;
CTFontRef icon_font;
int icon_spacing_left;
int icon_spacing_right;
int icon_padding_left;
int icon_padding_right;
struct rgba_color icon_color;
struct rgba_color icon_highlight_color;
@ -73,8 +70,8 @@ struct bar_item {
char* label;
char* label_font_name;
CTFontRef label_font;
int label_spacing_left;
int label_spacing_right;
int label_padding_left;
int label_padding_right;
struct rgba_color label_color;
struct rgba_color label_highlight_color;
@ -98,6 +95,8 @@ struct bar_item* bar_item_create();
void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item);
void bar_item_destroy(struct bar_item* bar_item);
void bar_item_serialize(struct bar_item* bar_item, FILE* rsp);
bool bar_item_update(struct bar_item* bar_item, bool forced);
bool bar_item_is_shown(struct bar_item* bar_item);

View file

@ -436,16 +436,16 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
bar_item_append_associated_display(bar_item, 1 << strtoul(&token.text[sep + 1], NULL, 0));
}
} else if (token_equals(property, COMMAND_SET_ICON_PADDING_LEFT)) {
bar_item->icon_spacing_left = token_to_int(get_token(&message));
bar_item->icon_padding_left = token_to_int(get_token(&message));
bar_item_needs_update(bar_item);
} else if (token_equals(property, COMMAND_SET_ICON_PADDING_RIGHT)) {
bar_item->icon_spacing_right = token_to_int(get_token(&message));
bar_item->icon_padding_right = token_to_int(get_token(&message));
bar_item_needs_update(bar_item);
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_LEFT)) {
bar_item->label_spacing_left = token_to_int(get_token(&message));
bar_item->label_padding_left = token_to_int(get_token(&message));
bar_item_needs_update(bar_item);
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
bar_item->label_spacing_right = token_to_int(get_token(&message));
bar_item->label_padding_right = token_to_int(get_token(&message));
bar_item_needs_update(bar_item);
} else if (token_equals(property, COMMAND_SET_BACKGROUND_PADDING_LEFT)) {
bar_item->background_padding_left = token_to_int(get_token(&message));
@ -669,6 +669,16 @@ static void handle_domain_query(FILE* rsp, struct token domain, char* message) {
if (token_equals(token, COMMAND_QUERY_DEFAULT_ITEMS)) {
print_all_menu_items(rsp);
}
if (token_equals(token, COMMAND_QUERY_ITEM)) {
struct token name = get_token(&message);
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, name.text);
if (item_index_for_name < 0) {
fprintf(rsp, "Name: %s not found in bar items \n", name.text);
printf("Name: %s not found in bar items \n", name.text);
return;
}
bar_item_serialize(g_bar_manager.bar_items[item_index_for_name], rsp);
}
}
void handle_message(FILE *rsp, char *message) {

View file

@ -1,6 +1,7 @@
#ifndef HELPERS_H
#define HELPERS_H
#include <_types/_uint32_t.h>
#include <stdint.h>
#define array_count(a) (sizeof((a)) / sizeof(*(a)))
#define MAXLEN 512
@ -26,6 +27,15 @@ struct rgba_color {
float a;
};
static uint32_t hex_from_rgba_color(struct rgba_color rgba_color) {
uint32_t result = 0;
result += ((uint32_t)(rgba_color.a * 255.0)) << 24;
result += ((uint32_t)(rgba_color.r * 255.0)) << 16;
result += ((uint32_t)(rgba_color.g * 255.0)) << 8;
result += ((uint32_t)(rgba_color.b * 255.0)) << 0;
return result;
}
static struct rgba_color rgba_color_from_hex(uint32_t color) {
struct rgba_color result;
result.is_valid = true;