add slider component (#284) and stricter item type checking

This commit is contained in:
Felix Kratz 2022-12-31 18:17:23 +01:00
parent f6ce7fe832
commit 26f5504445
11 changed files with 430 additions and 175 deletions

View file

@ -6,7 +6,7 @@ SRC = src
_OBJ = alias.o background.o bar_item.o custom_events.o event.o graph.o \
image.o mouse.o shadow.o text.o message.o mouse.o bar.o window.o \
bar_manager.o display.o event_loop.o group.o mach.o popup.o \
animation.o workspace.om volume.o
animation.o workspace.om volume.o slider.o
OBJ = $(patsubst %, $(ODIR)/%, $(_OBJ))
.PHONY: all clean arm x86 profile leak universal

View file

@ -42,7 +42,7 @@ static void background_reset_clip(struct background* background) {
background->num_clips = 0;
}
static bool background_set_enabled(struct background* background, bool enabled) {
bool background_set_enabled(struct background* background, bool enabled) {
if (background->enabled == enabled) return false;
if (background_clips_bar(background)) {
@ -55,16 +55,7 @@ static bool background_set_enabled(struct background* background, bool enabled)
return true;
}
static bool background_set_clip(struct background* background, float clip) {
if (background->clip == clip) return false;
background->clip = clip;
g_bar_manager.bar_needs_update = true;
g_bar_manager.might_need_clipping = true;
if (clip > 0.f) background_set_enabled(background, true);
return true;
}
static bool background_set_color(struct background* background, uint32_t color) {
bool background_set_color(struct background* background, uint32_t color) {
struct rgba_color target_color = rgba_color_from_hex(color);
if (background->color.r == target_color.r
&& background->color.g == target_color.g
@ -75,6 +66,15 @@ static bool background_set_color(struct background* background, uint32_t color)
return true;
}
static bool background_set_clip(struct background* background, float clip) {
if (background->clip == clip) return false;
background->clip = clip;
g_bar_manager.bar_needs_update = true;
g_bar_manager.might_need_clipping = true;
if (clip > 0.f) background_set_enabled(background, true);
return true;
}
static bool background_set_border_color(struct background* background, uint32_t color) {
struct rgba_color target_color = rgba_color_from_hex(color);
if (background->border_color.r == target_color.r

View file

@ -27,6 +27,8 @@ struct background {
void background_init(struct background* background);
void background_calculate_bounds(struct background* background, uint32_t x, uint32_t y, uint32_t width, uint32_t height);
bool background_set_enabled(struct background* background, bool enabled);
bool background_set_color(struct background* background, uint32_t color);
bool background_set_height(struct background* background, uint32_t height);
void background_draw(struct background* background, CGContextRef context);

View file

@ -55,6 +55,7 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
popup_init(&bar_item->popup, bar_item);
graph_init(&bar_item->graph);
alias_init(&bar_item->alias);
slider_init(&bar_item->slider);
if (default_item) bar_item_inherit_from_item(bar_item, default_item);
}
@ -289,10 +290,22 @@ void bar_item_set_name(struct bar_item* bar_item, char* name) {
string_copy(name) );
}
void bar_item_set_type(struct bar_item* bar_item, char type) {
bar_item->type = type;
void bar_item_set_type(struct bar_item* bar_item, char* type) {
if (string_equals(type, TYPE_SPACE)) {
bar_item->type = BAR_COMPONENT_SPACE;
} else if (string_equals(type, TYPE_ALIAS)) {
bar_item->type = BAR_COMPONENT_ALIAS;
} else if (string_equals(type, TYPE_GROUP)) {
bar_item->type = BAR_COMPONENT_GROUP;
} else if (string_equals(type, TYPE_GRAPH)) {
bar_item->type = BAR_COMPONENT_GRAPH;
} else if (string_equals(type, TYPE_SLIDER)) {
bar_item->type = BAR_COMPONENT_SLIDER;
} else {
bar_item->type = BAR_ITEM;
}
if (type == BAR_COMPONENT_SPACE) {
if (bar_item->type == BAR_COMPONENT_SPACE) {
if (!bar_item->script) {
bar_item_set_script(bar_item,
string_copy("sketchybar -m --set $NAME icon.highlight=$SELECTED"));
@ -312,15 +325,18 @@ void bar_item_set_type(struct bar_item* bar_item, char type) {
string_copy("DID"),
string_copy("0") );
}
else if (type == BAR_COMPONENT_ALIAS) {
else if (bar_item->type == BAR_COMPONENT_ALIAS) {
bar_item->update_frequency = 1;
bar_item->has_alias = true;
bar_item->updates_only_when_shown = true;
}
else if (type == BAR_COMPONENT_GRAPH) {
else if (bar_item->type == BAR_COMPONENT_GRAPH) {
bar_item->has_graph = true;
}
else if (type == BAR_COMPONENT_GROUP) {
else if (bar_item->type == BAR_COMPONENT_SLIDER) {
bar_item->has_slider = true;
}
else if (bar_item->type == BAR_COMPONENT_GROUP) {
bar_item->group = group_create();
group_init(bar_item->group);
group_add_member(bar_item->group, bar_item);
@ -336,8 +352,9 @@ void bar_item_set_position(struct bar_item* bar_item, char position) {
static uint32_t bar_item_get_content_length(struct bar_item* bar_item) {
int length = text_get_length(&bar_item->icon, false)
+ text_get_length(&bar_item->label, false)
+ (bar_item->has_graph ? graph_get_length(&bar_item->graph) : 0)
+ (bar_item->has_alias ? alias_get_length(&bar_item->alias) : 0);
+ (bar_item->has_graph ? graph_get_length(&bar_item->graph) : 0)
+ (bar_item->has_slider ? slider_get_length(&bar_item->slider) : 0)
+ (bar_item->has_alias ? alias_get_length(&bar_item->alias) : 0);
return max(length, 0);
}
@ -474,8 +491,11 @@ uint32_t bar_item_calculate_bounds(struct bar_item* bar_item, uint32_t bar_heigh
label_position += graph_get_length(&bar_item->graph);
} else if (bar_item->has_alias) {
label_position += alias_get_length(&bar_item->alias);
} else if (bar_item->has_slider) {
label_position += slider_get_length(&bar_item->slider);
}
text_calculate_bounds(&bar_item->icon,
icon_position,
content_y + bar_item->y_offset);
@ -489,6 +509,11 @@ uint32_t bar_item_calculate_bounds(struct bar_item* bar_item, uint32_t bar_heigh
sandwich_position,
content_y + bar_item->y_offset);
if (bar_item->has_slider)
slider_calculate_bounds(&bar_item->slider,
sandwich_position,
content_y + bar_item->y_offset);
if (bar_item->has_graph) {
bar_item->graph.bounds.size.height = bar_item->background.enabled
? (bar_item->background.bounds.size.height
@ -520,8 +545,8 @@ bool bar_item_clip_needs_update_for_bar(struct bar_item* bar_item, struct bar* b
bool needs_update = false;
needs_update |= background_clip_needs_update(&bar_item->background, bar)
| background_clip_needs_update(&bar_item->icon.background, bar)
| background_clip_needs_update(&bar_item->label.background, bar);
|| background_clip_needs_update(&bar_item->icon.background, bar)
|| background_clip_needs_update(&bar_item->label.background, bar);
return needs_update;
}
@ -549,6 +574,8 @@ void bar_item_draw(struct bar_item* bar_item, CGContextRef context) {
alias_draw(&bar_item->alias, context);
if (bar_item->has_graph)
graph_draw(&bar_item->graph, context);
if (bar_item->has_slider)
slider_draw(&bar_item->slider, context);
}
void bar_item_change_space(struct bar_item* bar_item, uint64_t dsid, uint32_t adid) {
@ -668,6 +695,9 @@ void bar_item_serialize(struct bar_item* bar_item, FILE* rsp) {
case BAR_COMPONENT_GROUP:
snprintf(type, 32, "bracket");
break;
case BAR_COMPONENT_SLIDER:
snprintf(type, 32, "slider");
break;
case BAR_COMPONENT_GRAPH:
snprintf(type, 32, "graph");
break;
@ -787,6 +817,10 @@ void bar_item_serialize(struct bar_item* bar_item, FILE* rsp) {
fprintf(rsp, ",\n\t\"graph\": {\n");
graph_serialize(&bar_item->graph, "\t\t", rsp);
fprintf(rsp, "\n\t}");
} else if (bar_item->type == BAR_COMPONENT_SLIDER) {
fprintf(rsp, ",\n\t\"slider\": {\n");
slider_serialize(&bar_item->slider, "\t\t", rsp);
fprintf(rsp, "\n\t}");
}
fprintf(rsp, "\n}\n");
@ -800,44 +834,63 @@ void bar_item_parse_set_message(struct bar_item* bar_item, char* message, FILE*
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_ICON))
if (token_equals(subdom, SUB_DOMAIN_ICON)) {
needs_refresh = text_parse_sub_domain(&bar_item->icon,
rsp,
entry,
message );
else if (token_equals(subdom, SUB_DOMAIN_LABEL))
}
else if (token_equals(subdom, SUB_DOMAIN_LABEL)) {
needs_refresh = text_parse_sub_domain(&bar_item->label,
rsp,
entry,
message );
else if (token_equals(subdom, SUB_DOMAIN_BACKGROUND))
}
else if (token_equals(subdom, SUB_DOMAIN_BACKGROUND)) {
needs_refresh = background_parse_sub_domain(&bar_item->background,
rsp,
entry,
message );
else if (token_equals(subdom, SUB_DOMAIN_GRAPH))
needs_refresh = graph_parse_sub_domain(&bar_item->graph,
rsp,
entry,
message );
else if (token_equals(subdom, SUB_DOMAIN_POPUP))
}
else if (token_equals(subdom, SUB_DOMAIN_POPUP)) {
needs_refresh = popup_parse_sub_domain(&bar_item->popup,
rsp,
entry,
message );
else if (token_equals(subdom, SUB_DOMAIN_ALIAS))
needs_refresh = alias_parse_sub_domain(&bar_item->alias,
rsp,
entry,
message );
else
}
else if (token_equals(subdom, SUB_DOMAIN_GRAPH)) {
if (bar_item->has_graph) {
needs_refresh = graph_parse_sub_domain(&bar_item->graph,
rsp,
entry,
message );
} else {
respond(rsp, "[!] Item (%s): Trying to set a graph property on a non-graph item\n", bar_item->name);
}
}
else if (token_equals(subdom, SUB_DOMAIN_ALIAS)) {
if (bar_item->has_alias) {
needs_refresh = alias_parse_sub_domain(&bar_item->alias,
rsp,
entry,
message );
} else {
respond(rsp, "[!] Item (%s): Trying to set an alias property on a non-alias item\n", bar_item->name);
}
}
else if (token_equals(subdom, SUB_DOMAIN_SLIDER)) {
if (bar_item->has_slider) {
needs_refresh = slider_parse_sub_domain(&bar_item->slider,
rsp,
entry,
message );
} else {
respond(rsp, "[!] Item (%s): Trying to set a slider property on a non-slider item\n", bar_item->name);
}
}
else {
respond(rsp, "[!] Item (%s): Invalid subdomain '%s'\n", bar_item->name, subdom.text);
}
}
else if (token_equals(property, PROPERTY_ICON)) {
needs_refresh = text_set_string(&bar_item->icon,

View file

@ -8,6 +8,15 @@
#include "misc/helpers.h"
#include "popup.h"
#include "text.h"
#include "slider.h"
#define BAR_ITEM 'i'
#define BAR_COMPONENT_GRAPH 'g'
#define BAR_COMPONENT_SPACE 's'
#define BAR_COMPONENT_ALIAS 'a'
#define BAR_COMPONENT_GROUP 'b'
#define BAR_COMPONENT_SLIDER 't'
#define BAR_PLUGIN 'p'
struct bar_item {
char type;
@ -64,6 +73,10 @@ struct bar_item {
bool has_alias;
struct alias alias;
// Slider Properties
bool has_slider;
struct slider slider;
// Group Properties
struct group* group;
@ -103,7 +116,7 @@ void bar_item_remove_associated_bar(struct bar_item* bar_item, uint32_t adid);
void bar_item_reset_associated_bar(struct bar_item* bar_item);
void bar_item_set_name(struct bar_item* bar_item, char* name);
void bar_item_set_type(struct bar_item* bar_item, char type);
void bar_item_set_type(struct bar_item* bar_item, char* type);
void bar_item_set_position(struct bar_item* bar_item, char position);
uint32_t bar_item_get_length(struct bar_item* bar_item, bool ignore_override);

View file

@ -107,9 +107,9 @@ void graph_draw(struct graph* graph, CGContextRef context) {
}
void graph_serialize(struct graph* graph, char* indent, FILE* rsp) {
fprintf(rsp, "%s\"graph.color\": \"0x%x\",\n"
"%s\"graph.fill_color\": \"0x%x\",\n"
"%s\"graph.line_width\": \"%f\",\n"
fprintf(rsp, "%s\"color\": \"0x%x\",\n"
"%s\"fill_color\": \"0x%x\",\n"
"%s\"line_width\": \"%f\",\n"
"%s\"data\": [\n",
indent, hex_from_rgba_color(graph->line_color),
indent, hex_from_rgba_color(graph->fill_color),

View file

@ -178,7 +178,7 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
}
struct bar_item* bar_item = bar_manager_create_item(&g_bar_manager);
bar_item_set_type(bar_item, command.text[0]);
bar_item_set_type(bar_item, command.text);
bar_item_set_position(bar_item, position.text[0]);
bar_item_set_name(bar_item, token_to_string(name));
@ -187,6 +187,10 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
if (bar_item->type == BAR_COMPONENT_GRAPH) {
struct token width = get_token(&message);
graph_setup(&bar_item->graph, token_to_uint32t(width));
}
else if (bar_item->type == BAR_COMPONENT_SLIDER) {
struct token width = get_token(&message);
slider_setup(&bar_item->slider, token_to_uint32t(width));
}
else if (bar_item->type == BAR_COMPONENT_ALIAS) {
char* tmp_name = string_copy(name.text);
@ -200,6 +204,18 @@ static void handle_domain_add(FILE* rsp, struct token domain, char* message) {
free(tmp_name);
}
else if (bar_item->type == BAR_COMPONENT_SLIDER) {
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_setup(&bar_item->alias, token_to_string(name), NULL);
else
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) {
struct token member = position;
bool first = true;

View file

@ -7,6 +7,7 @@
#include "bar_manager.h"
#include "display.h"
#include "group.h"
#include "slider.h"
#include "mach.h"
#include "event.h"
#include "event_loop.h"

View file

@ -1,155 +1,157 @@
#pragma once
#define DOMAIN_ADD "--add"
#define COMMAND_ADD_ITEM "item"
#define COMMAND_ADD_COMPONENT "component"
#define COMMAND_ADD_EVENT "event"
#define DOMAIN_ADD "--add"
#define COMMAND_ADD_ITEM "item"
#define COMMAND_ADD_COMPONENT "component"
#define COMMAND_ADD_EVENT "event"
#define DOMAIN_UPDATE "--update"
#define DOMAIN_UPDATE "--update"
#define DOMAIN_PUSH "--push"
#define DOMAIN_PUSH "--push"
#define DOMAIN_TRIGGER "--trigger"
#define DOMAIN_TRIGGER "--trigger"
#define DOMAIN_DEFAULT "--default"
#define COMMAND_DEFAULT_RESET "reset"
#define DOMAIN_DEFAULT "--default"
#define COMMAND_DEFAULT_RESET "reset"
#define DOMAIN_CLONE "--clone"
#define DOMAIN_CLONE "--clone"
#define DOMAIN_RENAME "--rename"
#define DOMAIN_RENAME "--rename"
#define DOMAIN_REORDER "--reorder"
#define DOMAIN_REORDER "--reorder"
#define DOMAIN_REMOVE "--remove"
#define DOMAIN_REMOVE "--remove"
#define DOMAIN_MOVE "--move"
#define DOMAIN_MOVE "--move"
#define DOMAIN_SET "--set"
#define DOMAIN_SET "--set"
#define DOMAIN_ANIMATE "--animate"
#define DOMAIN_ANIMATE "--animate"
#define DOMAIN_EXIT "--exit"
#define DOMAIN_EXIT "--exit"
#define SUB_DOMAIN_ICON "icon"
#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"
#define SUB_DOMAIN_ICON "icon"
#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"
#define SUB_DOMAIN_KNOB "knob"
#define SUB_DOMAIN_SLIDER "slider"
#define PROPERTY_FONT "font"
#define PROPERTY_COLOR "color"
#define PROPERTY_HIGHLIGHT "highlight"
#define PROPERTY_HIGHLIGHT_COLOR "highlight_color"
#define PROPERTY_PADDING_LEFT "padding_left"
#define PROPERTY_PADDING_RIGHT "padding_right"
#define PROPERTY_HEIGHT "height"
#define PROPERTY_BORDER_COLOR "border_color"
#define PROPERTY_BORDER_WIDTH "border_width"
#define PROPERTY_CORNER_RADIUS "corner_radius"
#define PROPERTY_FILL_COLOR "fill_color"
#define PROPERTY_LINE_WIDTH "line_width"
#define PROPERTY_BLUR_RADIUS "blur_radius"
#define PROPERTY_DRAWING "drawing"
#define PROPERTY_CLIP "clip"
#define PROPERTY_DISTANCE "distance"
#define PROPERTY_ANGLE "angle"
#define PROPERTY_SCALE "scale"
#define PROPERTY_FONT "font"
#define PROPERTY_COLOR "color"
#define PROPERTY_HIGHLIGHT "highlight"
#define PROPERTY_HIGHLIGHT_COLOR "highlight_color"
#define PROPERTY_PADDING_LEFT "padding_left"
#define PROPERTY_PADDING_RIGHT "padding_right"
#define PROPERTY_HEIGHT "height"
#define PROPERTY_BORDER_COLOR "border_color"
#define PROPERTY_BORDER_WIDTH "border_width"
#define PROPERTY_CORNER_RADIUS "corner_radius"
#define PROPERTY_FILL_COLOR "fill_color"
#define PROPERTY_LINE_WIDTH "line_width"
#define PROPERTY_BLUR_RADIUS "blur_radius"
#define PROPERTY_DRAWING "drawing"
#define PROPERTY_CLIP "clip"
#define PROPERTY_DISTANCE "distance"
#define PROPERTY_ANGLE "angle"
#define PROPERTY_SCALE "scale"
#define PROPERTY_UPDATES "updates"
#define PROPERTY_POSITION "position"
#define PROPERTY_ASSOCIATED_DISPLAY "associated_display"
#define PROPERTY_ASSOCIATED_SPACE "associated_space"
#define PROPERTY_UPDATE_FREQ "update_freq"
#define PROPERTY_SCRIPT "script"
#define PROPERTY_CLICK_SCRIPT "click_script"
#define PROPERTY_ICON "icon"
#define PROPERTY_YOFFSET "y_offset"
#define PROPERTY_WIDTH "width"
#define PROPERTY_LABEL "label"
#define PROPERTY_CACHE_SCRIPTS "cache_scripts"
#define PROPERTY_LAZY "lazy"
#define PROPERTY_IGNORE_ASSOCIATION "ignore_association"
#define PROPERTY_EVENT_PORT "mach_helper"
#define PROPERTY_UPDATES "updates"
#define PROPERTY_POSITION "position"
#define PROPERTY_ASSOCIATED_DISPLAY "associated_display"
#define PROPERTY_ASSOCIATED_SPACE "associated_space"
#define PROPERTY_UPDATE_FREQ "update_freq"
#define PROPERTY_SCRIPT "script"
#define PROPERTY_CLICK_SCRIPT "click_script"
#define PROPERTY_ICON "icon"
#define PROPERTY_YOFFSET "y_offset"
#define PROPERTY_WIDTH "width"
#define PROPERTY_LABEL "label"
#define PROPERTY_CACHE_SCRIPTS "cache_scripts"
#define PROPERTY_LAZY "lazy"
#define PROPERTY_IGNORE_ASSOCIATION "ignore_association"
#define PROPERTY_EVENT_PORT "mach_helper"
#define PROPERTY_PERCENTAGE "percentage"
#define DOMAIN_BAR "--bar"
#define PROPERTY_POSITION "position"
#define PROPERTY_MARGIN "margin"
#define PROPERTY_DISPLAY "display"
#define PROPERTY_TOPMOST "topmost"
#define PROPERTY_STICKY "sticky"
#define PROPERTY_HIDDEN "hidden"
#define PROPERTY_FONT_SMOOTHING "font_smoothing"
#define PROPERTY_SHADOW "shadow"
#define PROPERTY_ALIGN "align"
#define PROPERTY_NOTCH_WIDTH "notch_width"
#define PROPERTY_NOTCH_OFFSET "notch_offset"
#define PROPERTY_HORIZONTAL "horizontal"
#define DOMAIN_BAR "--bar"
#define PROPERTY_POSITION "position"
#define PROPERTY_MARGIN "margin"
#define PROPERTY_DISPLAY "display"
#define PROPERTY_TOPMOST "topmost"
#define PROPERTY_STICKY "sticky"
#define PROPERTY_HIDDEN "hidden"
#define PROPERTY_FONT_SMOOTHING "font_smoothing"
#define PROPERTY_SHADOW "shadow"
#define PROPERTY_ALIGN "align"
#define PROPERTY_NOTCH_WIDTH "notch_width"
#define PROPERTY_NOTCH_OFFSET "notch_offset"
#define PROPERTY_HORIZONTAL "horizontal"
#define DOMAIN_SUBSCRIBE "--subscribe"
#define COMMAND_SUBSCRIBE_FRONT_APP_SWITCHED "front_app_switched"
#define COMMAND_SUBSCRIBE_SPACE_CHANGE "space_change"
#define COMMAND_SUBSCRIBE_DISPLAY_CHANGE "display_change"
#define COMMAND_SUBSCRIBE_SYSTEM_WOKE "system_woke"
#define COMMAND_SUBSCRIBE_SYSTEM_WILL_SLEEP "system_will_sleep"
#define COMMAND_SUBSCRIBE_VOLUME_CHANGE "volume_change"
#define COMMAND_SUBSCRIBE_BRIGHTNESS_CHANGE "brightness_change"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED "mouse.entered"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED "mouse.exited"
#define COMMAND_SUBSCRIBE_MOUSE_CLICKED "mouse.clicked"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED_GLOBAL "mouse.entered.global"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED_GLOBAL "mouse.exited.global"
#define DOMAIN_SUBSCRIBE "--subscribe"
#define COMMAND_SUBSCRIBE_FRONT_APP_SWITCHED "front_app_switched"
#define COMMAND_SUBSCRIBE_SPACE_CHANGE "space_change"
#define COMMAND_SUBSCRIBE_DISPLAY_CHANGE "display_change"
#define COMMAND_SUBSCRIBE_SYSTEM_WOKE "system_woke"
#define COMMAND_SUBSCRIBE_SYSTEM_WILL_SLEEP "system_will_sleep"
#define COMMAND_SUBSCRIBE_VOLUME_CHANGE "volume_change"
#define COMMAND_SUBSCRIBE_BRIGHTNESS_CHANGE "brightness_change"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED "mouse.entered"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED "mouse.exited"
#define COMMAND_SUBSCRIBE_MOUSE_CLICKED "mouse.clicked"
#define COMMAND_SUBSCRIBE_MOUSE_ENTERED_GLOBAL "mouse.entered.global"
#define COMMAND_SUBSCRIBE_MOUSE_EXITED_GLOBAL "mouse.exited.global"
#define DOMAIN_QUERY "--query"
#define COMMAND_QUERY_DEFAULT_ITEMS "default_menu_items"
#define COMMAND_QUERY_ITEM "item"
#define COMMAND_QUERY_DEFAULTS "defaults"
#define COMMAND_QUERY_BAR "bar"
#define COMMAND_QUERY_EVENTS "events"
#define DOMAIN_QUERY "--query"
#define COMMAND_QUERY_DEFAULT_ITEMS "default_menu_items"
#define COMMAND_QUERY_ITEM "item"
#define COMMAND_QUERY_DEFAULTS "defaults"
#define COMMAND_QUERY_BAR "bar"
#define COMMAND_QUERY_EVENTS "events"
#define ARGUMENT_COMMON_VAL_ON "on"
#define ARGUMENT_COMMON_VAL_NOT_OFF "!off"
#define ARGUMENT_COMMON_VAL_TRUE "true"
#define ARGUMENT_COMMON_VAL_NOT_FALSE "!false"
#define ARGUMENT_COMMON_VAL_ONE "1"
#define ARGUMENT_COMMON_VAL_NOT_ZERO "!0"
#define ARGUMENT_COMMON_VAL_YES "yes"
#define ARGUMENT_COMMON_VAL_NOT_NO "!no"
#define ARGUMENT_COMMON_VAL_OFF "off"
#define ARGUMENT_COMMON_VAL_NOT_ON "!on"
#define ARGUMENT_COMMON_VAL_FALSE "false"
#define ARGUMENT_COMMON_VAL_NOT_TRUE "!true"
#define ARGUMENT_COMMON_VAL_ZERO "0"
#define ARGUMENT_COMMON_VAL_NOT_ONE "!1"
#define ARGUMENT_COMMON_VAL_NO "no"
#define ARGUMENT_COMMON_VAL_NOT_YES "!yes"
#define ARGUMENT_COMMON_VAL_TOGGLE "toggle"
#define ARGUMENT_COMMON_VAL_BEFORE "before"
#define ARGUMENT_COMMON_VAL_AFTER "after"
#define ARGUMENT_COMMON_VAL_ON "on"
#define ARGUMENT_COMMON_VAL_NOT_OFF "!off"
#define ARGUMENT_COMMON_VAL_TRUE "true"
#define ARGUMENT_COMMON_VAL_NOT_FALSE "!false"
#define ARGUMENT_COMMON_VAL_ONE "1"
#define ARGUMENT_COMMON_VAL_NOT_ZERO "!0"
#define ARGUMENT_COMMON_VAL_YES "yes"
#define ARGUMENT_COMMON_VAL_NOT_NO "!no"
#define ARGUMENT_COMMON_VAL_OFF "off"
#define ARGUMENT_COMMON_VAL_NOT_ON "!on"
#define ARGUMENT_COMMON_VAL_FALSE "false"
#define ARGUMENT_COMMON_VAL_NOT_TRUE "!true"
#define ARGUMENT_COMMON_VAL_ZERO "0"
#define ARGUMENT_COMMON_VAL_NOT_ONE "!1"
#define ARGUMENT_COMMON_VAL_NO "no"
#define ARGUMENT_COMMON_VAL_NOT_YES "!yes"
#define ARGUMENT_COMMON_VAL_TOGGLE "toggle"
#define ARGUMENT_COMMON_VAL_BEFORE "before"
#define ARGUMENT_COMMON_VAL_AFTER "after"
#define ARGUMENT_UPDATES_WHEN_SHOWN "when_shown"
#define ARGUMENT_DYNAMIC "dynamic"
#define ARGUMENT_UPDATES_WHEN_SHOWN "when_shown"
#define ARGUMENT_DYNAMIC "dynamic"
#define POSITION_TOP 't'
#define POSITION_BOTTOM 'b'
#define POSITION_LEFT 'l'
#define POSITION_RIGHT 'r'
#define POSITION_CENTER 'c'
#define POSITION_POPUP 'p'
#define POSITION_CENTER_LEFT 'q'
#define POSITION_TOP 't'
#define POSITION_BOTTOM 'b'
#define POSITION_LEFT 'l'
#define POSITION_RIGHT 'r'
#define POSITION_CENTER 'c'
#define POSITION_POPUP 'p'
#define POSITION_CENTER_LEFT 'q'
#define POSITION_CENTER_RIGHT 'e'
#define DISPLAY_MAIN 'm'
#define DISPLAY_ALL 'a'
#define DISPLAY_ALL 'a'
#define TYPE_GRAPH "graph"
#define TYPE_SPACE "space"
#define TYPE_ALIAS "alias"
#define TYPE_GROUP "bracket"
#define TYPE_SLIDER "slider"
#define TYPE_ITEM "item"
#define BAR_ITEM 'i'
#define BAR_COMPONENT_GRAPH 'g'
#define BAR_COMPONENT_SPACE 's'
#define BAR_COMPONENT_ALIAS 'a'
#define BAR_COMPONENT_GROUP 'b'
#define BAR_PLUGIN 'p'
#define REGEX_DELIMITER '/'
#define REGEX_DELIMITER '/'

143
src/slider.c Normal file
View file

@ -0,0 +1,143 @@
#include "slider.h"
#include "bar_manager.h"
#include "animation.h"
static bool slider_set_percentage(struct slider* slider, uint32_t percentage) {
if (percentage == slider->percentage) return false;
slider->percentage = min(percentage, 100);
return true;
}
static bool slider_set_width(struct slider* slider, uint32_t width) {
if (width == slider->background.bounds.size.width) return false;
slider->background.bounds.size.width = width;
return true;
}
static bool slider_set_foreground_color(struct slider* slider, uint32_t color) {
if (slider->foreground_color == color) return false;
slider->foreground_color = color;
return background_set_color(&slider->foreground, color);
}
void slider_init(struct slider* slider) {
slider->percentage = 0;
slider->bounds = CGRectNull;
slider->foreground_color = 0xff0000ff;
text_init(&slider->knob);
background_init(&slider->background);
background_init(&slider->foreground);
background_set_color(&slider->background, 0xff000000);
background_set_color(&slider->foreground, slider->foreground_color);
}
void slider_setup(struct slider* slider, uint32_t width) {
slider->background.bounds.size.width = width;
background_set_enabled(&slider->background, true);
background_set_enabled(&slider->foreground, true);
}
uint32_t slider_get_length(struct slider* slider) {
return slider->background.bounds.size.width;
}
void slider_calculate_bounds(struct slider* slider, uint32_t x, uint32_t y) {
background_calculate_bounds(&slider->background,
x,
y,
slider->background.bounds.size.width,
slider->background.bounds.size.height);
background_calculate_bounds(&slider->foreground,
x,
y,
slider->background.bounds.size.width
* ((float)slider->percentage)/100.f,
slider->background.bounds.size.height);
text_calculate_bounds(&slider->knob,
x + ((float)slider->percentage)/100.f
* slider->background.bounds.size.width
- slider->knob.bounds.size.width / 2.,
y );
}
void slider_draw(struct slider* slider, CGContextRef context) {
background_draw(&slider->background, context);
background_draw(&slider->foreground, context);
text_draw(&slider->knob, context);
}
void slider_destroy(struct slider* slider) {
background_destroy(&slider->background);
background_destroy(&slider->foreground);
text_destroy(&slider->knob);
}
void slider_serialize(struct slider* slider, char* indent, FILE* rsp) {
fprintf(rsp, "%s\"highlight_color\": \"0x%x\",\n"
"%s\"percentage\": \"%d\",\n",
indent, slider->foreground_color,
indent, slider->percentage);
char deeper_indent[strlen(indent) + 2];
snprintf(deeper_indent, strlen(indent) + 2, "%s\t", indent);
fprintf(rsp, "%s\"background\": {\n", indent);
background_serialize(&slider->background, deeper_indent, rsp, false);
fprintf(rsp, "\n%s},\n", indent);
fprintf(rsp, "%s\"knob\": {\n", indent);
text_serialize(&slider->knob, deeper_indent, rsp);
fprintf(rsp, "\n%s}", indent);
}
bool slider_parse_sub_domain(struct slider* slider, FILE* rsp, struct token property, char* message) {
bool needs_refresh = false;
if (token_equals(property, PROPERTY_PERCENTAGE)) {
needs_refresh = slider_set_percentage(slider,
token_to_uint32t(get_token(&message)));
}
else if (token_equals(property, PROPERTY_HIGHLIGHT_COLOR)) {
struct token token = get_token(&message);
ANIMATE_BYTES(slider_set_foreground_color,
slider,
slider->foreground_color,
token_to_uint32t(token) );
}
else if (token_equals(property, PROPERTY_WIDTH)) {
struct token token = get_token(&message);
ANIMATE(slider_set_width,
slider,
slider->bounds.size.width,
token_to_uint32t(token) );
}
else if (token_equals(property, SUB_DOMAIN_KNOB)) {
needs_refresh = text_set_string(&slider->knob,
token_to_string(get_token(&message)),
false );
}
else {
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_BACKGROUND)) {
background_parse_sub_domain(&slider->foreground, rsp, entry, message);
background_set_color(&slider->foreground, slider->foreground_color);
return background_parse_sub_domain(&slider->background, rsp, entry, message);
}
else if (token_equals(subdom, SUB_DOMAIN_KNOB))
return text_parse_sub_domain(&slider->knob, rsp, entry, message);
else {
respond(rsp, "[!] Slider: Invalid subdomain '%s' \n", subdom.text);
}
}
else {
respond(rsp, "[!] Slider: Invalid property '%s'\n", property.text);
}
}
return needs_refresh;
}

25
src/slider.h Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include "background.h"
#include "text.h"
struct slider {
uint32_t percentage;
uint32_t foreground_color;
CGRect bounds;
struct text knob;
struct background background;
struct background foreground;
};
void slider_init(struct slider* slider);
void slider_setup(struct slider* slider, uint32_t width);
void slider_calculate_bounds(struct slider* slider, uint32_t x, uint32_t y);
void slider_draw(struct slider* slider, CGContextRef context);
uint32_t slider_get_length(struct slider* slider);
void slider_destroy(struct slider* slider);
void slider_serialize(struct slider* slider, char* indent, FILE* rsp);
bool slider_parse_sub_domain(struct slider* graph, FILE* rsp, struct token property, char* message);