new modifier to execute scripts only when the item is shown

This commit is contained in:
FelixKratz 2021-10-01 12:37:37 +02:00
parent b9421137aa
commit b761163536
8 changed files with 64 additions and 35 deletions

View file

@ -267,7 +267,7 @@ A list of properties is listed below:
* *update_freq*: time in seconds between script executions
* *click_script*: script to run when left clicking on item
* *cache_scripts*: If the scripts should be cached in RAM or read from disc every time (values: *on*, *off*, *toggle*, default: *off*)
* *updates*: If the item updates e.g. via scripts (turning this off disables the script execution) (values: *on*, *off*, *toggle*, default: *on*)
* *updates*: If and when the item updates e.g. via script execution (values: *on*, *off*, *toggle*, *when_shown*, default: *on*)
* *drawing*: If the item should be drawn into the bar (values: *on*, *off*, *toggle*, default: *on*)
* *lazy*: Changes do not trigger a redraw of the bar, item is refreshed when the bar is redrawn anyways (values: *on*, *off*, *toggle*, default: *off*)
@ -299,7 +299,7 @@ this currently works for the properties:
* *y_offset*
* *update_freq*
* *cache_scripts*
* *scripting*
* *updates*
* *drawing*
* *lazy*

View file

@ -102,38 +102,39 @@ void alias_find_window(struct alias* alias) {
CFRelease(window_list);
}
/*
void alias_click(struct alias* alias) {
printf("Click \n");
CGEventSourceRef event_source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
CGEventRef click_event = CGEventCreateMouseEvent(event_source, kCGEventLeftMouseDown, CGPointMake(alias->bounds.origin.x + 5,alias->bounds.origin.y + 5), kCGMouseButtonLeft);
CGEventPostToPid(alias->pid, click_event);
//CGEventPost(kCGHIDEventTap, click_event);
CGEventSetType(click_event, kCGEventLeftMouseUp);
CGEventPostToPid(alias->pid, click_event);
//CGEventPost(kCGHIDEventTap, click_event);
CFRelease(click_event);
CFRelease(event_source);
}*/
bool alias_update_image(struct alias* alias) {
if (alias->wid == 0) alias_find_window(alias);
if (alias->wid == 0) {
alias->image_ref = NULL;
return false;
}
// Capture Bar Item with SkyLight private framework
CGImageRef tmp_ref = NULL;
/*CFArrayRef image_refs = SLSHWCaptureWindowList(g_connection, &alias->wid, 1, 1 << 8 | 1 << 11);
if (image_refs && CFArrayGetCount(image_refs) > 0) {
tmp_ref = (CGImageRef) CFArrayGetValueAtIndex(image_refs, 0);
}
else {
tmp_ref = NULL;
}*/
SLSGetScreenRectForWindow(g_connection, alias->wid, &alias->bounds);
CGImageRef tmp_ref = NULL;
SLSCaptureWindowsContentsToRectWithOptions(g_connection, &alias->wid, true, CGRectNull, 1 << 8, &tmp_ref);
// alias_click(alias);
//CGImageRef tmp_ref = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow,
// alias->wid, kCGWindowImageBestResolution | kCGWindowImageBoundsIgnoreFraming);
if (!tmp_ref) { alias->wid = 0; return false;}
if (!tmp_ref) { alias->wid = 0; return false; }
CGImageRelease(alias->image_ref);
alias->image_ref = tmp_ref;
// Bar Item Cropping
/* alias->size.x = CGImageGetWidth(raw_image_ref) - 2*MENU_ITEM_CROP;
alias->size.y = CGImageGetHeight(raw_image_ref);
CGRect crop = {{ MENU_ITEM_CROP, 0 }, { alias->size.x, alias->size.y }};
alias->image_ref = CGImageCreateWithImageInRect(raw_image_ref, crop);
CGImageRelease(raw_image_ref); */
return true;
}

View file

@ -298,6 +298,10 @@ void bar_set_font_smoothing(struct bar* bar, bool smoothing) {
CGContextSetAllowsFontSmoothing(bar->context, smoothing);
}
void bar_set_blur_radius(struct bar* bar) {
SLSSetWindowBackgroundBlurRadius(g_connection, bar->id, g_bar_manager.blur_radius);
}
void bar_create_window(struct bar* bar) {
uint32_t set_tags[2] = {
kCGSStickyTagBit |
@ -316,11 +320,11 @@ void bar_create_window(struct bar* bar) {
SLSNewWindow(g_connection, 2, bar->origin.x, bar->origin.y, frame_region, &bar->id);
CFRelease(frame_region);
SLSSetWindowResolution(g_connection, bar->id, 2.0f);
//SLSSetWindowResolution(g_connection, bar->id, 2.0f);
SLSSetWindowTags(g_connection, bar->id, set_tags, 64);
SLSClearWindowTags(g_connection, bar->id, clear_tags, 64);
SLSSetWindowOpacity(g_connection, bar->id, 0);
SLSSetWindowBackgroundBlurRadius(g_connection, bar->id, g_bar_manager.blur_radius);
bar_set_blur_radius(bar);
SLSSetMouseEventEnableFlags(g_connection, bar->id, false);
SLSSetWindowLevel(g_connection, bar->id, g_bar_manager.window_level);
bar->context = SLWindowContextCreate(g_connection, bar->id, 0);

View file

@ -58,5 +58,6 @@ void bar_close_window(struct bar* bar);
void bar_destroy(struct bar* bar);
void bar_set_hidden(struct bar* bar, bool hidden);
void bar_set_font_smoothing(struct bar* bar, bool smoothing);
void bar_set_blur_radius(struct bar* bar);
#endif

View file

@ -14,6 +14,7 @@ struct bar_item* bar_item_create() {
void bar_item_inherit_from_item(struct bar_item* bar_item, struct bar_item* ancestor) {
bar_item->lazy = ancestor->lazy;
bar_item->updates = ancestor->updates;
bar_item->updates_only_when_shown = ancestor->updates_only_when_shown;
bar_item->drawing = ancestor->drawing;
text_destroy(&bar_item->icon);
@ -40,6 +41,7 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
bar_item->lazy = false;
bar_item->drawing = true;
bar_item->updates = true;
bar_item->updates_only_when_shown = false;
bar_item->nospace = false;
bar_item->selected = false;
bar_item->counter = 0;
@ -112,7 +114,11 @@ void bar_item_reset_associated_bar(struct bar_item* bar_item) {
bool bar_item_update(struct bar_item* bar_item, bool forced) {
if (!bar_item->updates || (bar_item->update_frequency == 0 && !forced)) return false;
bar_item->counter++;
if (bar_item->update_frequency <= bar_item->counter || forced) {
bool scheduled_update_needed = bar_item->update_frequency <= bar_item->counter;
bool should_update = bar_item->updates_only_when_shown ? bar_item_is_shown(bar_item) : true;
if ((scheduled_update_needed && should_update) || forced) {
bar_item->counter = 0;
// Script Update
@ -121,7 +127,7 @@ bool bar_item_update(struct bar_item* bar_item, bool forced) {
}
// Alias Update
if (bar_item->has_alias && bar_item_is_shown(bar_item)) {
if (bar_item->has_alias) {
alias_update_image(&bar_item->alias);
bar_item_needs_update(bar_item);
return true;
@ -158,6 +164,7 @@ void bar_item_set_type(struct bar_item* bar_item, char type) {
else if (type == BAR_COMPONENT_ALIAS) {
bar_item->update_frequency = 1;
bar_item->has_alias = true;
bar_item->updates_only_when_shown = true;
}
}

View file

@ -19,14 +19,20 @@
struct bar_item {
char* name;
bool needs_update;
bool lazy;
bool drawing;
bool updates;
bool nospace;
bool selected;
int counter;
char type;
// Update Modifiers
uint32_t counter;
bool needs_update;
bool updates;
bool updates_only_when_shown;
bool lazy;
bool selected;
// Drawing Modifiers
bool drawing;
bool nospace;
// These are 32bit masks where the ith bit represents the ith screen/display/bar association
uint32_t associated_bar;

View file

@ -34,7 +34,7 @@ int bar_manager_get_item_index_by_address(struct bar_manager* bar_manager, struc
void bar_manager_set_background_blur(struct bar_manager* bar_manager, uint32_t radius) {
bar_manager->blur_radius = radius;
for (int i = 0; i < bar_manager->bar_count; i++) {
SLSSetWindowBackgroundBlurRadius(g_connection, bar_manager->bars[i]->id, radius);
bar_set_blur_radius(bar_manager->bars[i]);
}
}

View file

@ -121,6 +121,8 @@ extern bool g_verbose;
#define ARGUMENT_COMMON_VAL_TOGGLE "toggle"
#define ARGUMENT_COMMON_NO_SPACE "nospace"
#define ARGUMENT_UPDATES_WHEN_SHOWN "when_shown"
#define BAR_DISPLAY_MAIN_ONLY "main"
#define BAR_DISPLAY_ALL "all"
@ -377,7 +379,15 @@ static void message_parse_set_message_for_bar_item(FILE* rsp, struct bar_item* b
} else if (token_equals(property, COMMAND_SET_ICON_COLOR)) {
needs_update = text_set_color(&bar_item->icon, token_to_uint32t(get_token(&message)));
} else if (token_equals(property, COMMAND_SET_SCRIPTING) || token_equals(property, COMMAND_SET_UPDATES)) {
bar_item->updates = evaluate_boolean_state(get_token(&message), bar_item->updates);
struct token token = get_token(&message);
if (token_equals(token, ARGUMENT_UPDATES_WHEN_SHOWN)) {
bar_item->updates = true;
bar_item->updates_only_when_shown = true;
}
else {
bar_item->updates = evaluate_boolean_state(token, bar_item->updates);
bar_item->updates_only_when_shown = false;
}
} else if (token_equals(property, COMMAND_SET_DRAWING)) {
bar_item_set_drawing(bar_item, evaluate_boolean_state(get_token(&message), bar_item->drawing));
} else if (token_equals(property, COMMAND_SET_LABEL_HIGHLIGHT)) {