implement a faster method to get the window id from a CGEvent and clean up mouse event handling

This commit is contained in:
Felix Kratz 2023-06-28 21:56:39 +02:00
parent 5b820d3b5d
commit cde3bc2fd4
7 changed files with 30 additions and 24 deletions

View file

@ -19,7 +19,6 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
bar_item->updates = true;
bar_item->updates_only_when_shown = false;
bar_item->selected = false;
bar_item->mouse_over = false;
bar_item->ignore_association = false;
bar_item->overrides_association = false;
bar_item->counter = 0;
@ -243,16 +242,13 @@ void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t mouse_
}
void bar_item_mouse_entered(struct bar_item* bar_item) {
bar_item->mouse_over = true;
if (bar_item->update_mask & UPDATE_MOUSE_ENTERED)
bar_item_update(bar_item, COMMAND_SUBSCRIBE_MOUSE_ENTERED, true, NULL);
}
void bar_item_mouse_exited(struct bar_item* bar_item) {
if (bar_item->mouse_over && (bar_item->update_mask & UPDATE_MOUSE_EXITED))
if (bar_item->update_mask & UPDATE_MOUSE_EXITED)
bar_item_update(bar_item, COMMAND_SUBSCRIBE_MOUSE_EXITED, true, NULL);
bar_item->mouse_over = false;
}
static bool bar_item_set_drawing(struct bar_item* bar_item, bool state) {

View file

@ -782,7 +782,7 @@ void bar_manager_handle_mouse_exited_global(struct bar_manager* bar_manager) {
}
void bar_manager_handle_mouse_entered(struct bar_manager* bar_manager, struct bar_item* bar_item) {
if (!bar_item || bar_item->mouse_over) return;
if (!bar_item) return;
bar_item_mouse_entered(bar_item);
}

View file

@ -100,7 +100,7 @@ EVENT_CALLBACK(EVENT_HANDLER_MACH_MESSAGE) {
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP) {
CGPoint point = CGEventGetLocation(context);
uint32_t wid = get_window_id_from_cg_event(context);
uint32_t wid = get_wid_from_cg_event(context);
CGEventType type = CGEventGetType(context);
uint32_t mouse_button_code = CGEventGetIntegerValueField(context, kCGMouseEventButtonNumber);
uint32_t modifier_keys = CGEventGetFlags(context);
@ -110,6 +110,13 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP) {
wid,
adid );
struct bar* bar = bar_manager_get_bar_by_wid(&g_bar_manager, wid);
struct popup* popup = bar_manager_get_popup_by_wid(&g_bar_manager, wid);
if (!bar_item && !popup && !bar) {
CFRelease(context);
return EVENT_SUCCESS;
}
if (!bar_item || bar_item->type == BAR_COMPONENT_GROUP) {
bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, adid);
}
@ -135,7 +142,7 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP) {
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DRAGGED) {
CGPoint point = CGEventGetLocation(context);
uint32_t wid = get_window_id_from_cg_event(context);
uint32_t wid = get_wid_from_cg_event(context);
uint32_t adid = display_arrangement(display_active_display_id());
struct bar_item* bar_item = bar_manager_get_item_by_wid(&g_bar_manager,
@ -167,7 +174,7 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_DRAGGED) {
}
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_ENTERED) {
uint32_t wid = get_window_id_from_cg_event(context);
uint32_t wid = get_wid_from_cg_event(context);
uint32_t adid = display_arrangement(display_active_display_id());
@ -212,7 +219,7 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_ENTERED) {
EVENT_CALLBACK(EVENT_HANDLER_MOUSE_EXITED) {
uint32_t adid = display_arrangement(display_active_display_id());
uint32_t wid = get_window_id_from_cg_event(context);
uint32_t wid = get_wid_from_cg_event(context);
struct bar* bar,* bar_target;
struct popup* popup,* popup_target;
@ -267,8 +274,6 @@ EVENT_CALLBACK(EVENT_HANDLER_MOUSE_EXITED) {
&& bar_manager_get_popup_by_point(&g_bar_manager,
point) != &bar_item->popup)) {
bar_manager_handle_mouse_exited(&g_bar_manager, bar_item);
} else if (bar_item) {
bar_item->mouse_over = false;
}
CFRelease(context);

View file

@ -6,8 +6,6 @@
struct event_loop;
extern uint32_t get_window_id_from_cg_event(CGEventRef cgevent);
#define EVENT_CALLBACK(name) uint32_t name(void *context)
typedef EVENT_CALLBACK(event_callback);

View file

@ -548,3 +548,20 @@ static inline void error(const char *format, ...) {
exit(EXIT_FAILURE);
}
static inline int get_wid_from_cg_event(CGEventRef event) {
switch (CGEventGetType(event)) {
case kCGEventLeftMouseUp:
case kCGEventRightMouseUp:
case kCGEventOtherMouseUp:
case kCGEventLeftMouseDragged:
case kCGEventRightMouseDragged:
case kCGEventOtherMouseDragged:
return CGEventGetIntegerValueField(event, 0x5b);
case 0x8:
case 0x9:
case kCGEventScrollWheel:
return CGEventGetIntegerValueField(event, 0x33);
default:
return 0;
}
}

View file

@ -16,4 +16,3 @@ void workspace_event_handler_end(void *context);
int workspace_display_notch_height(uint32_t did);
CGImageRef workspace_icon_for_app(char* app);
uint32_t get_window_id_from_cg_event(CGEventRef cgevent);

View file

@ -43,15 +43,6 @@ int workspace_display_notch_height(uint32_t did)
return height;
}
uint32_t get_window_id_from_cg_event(CGEventRef cgevent) {
uint32_t wid;
@autoreleasepool {
NSEvent *nsEvent = [NSEvent eventWithCGEvent:cgevent];
wid = [nsEvent windowNumber];
}
return wid;
}
void forced_front_app_event() {
@autoreleasepool {
NSString* name = [[[NSWorkspace sharedWorkspace] frontmostApplication] localizedName];