From 70830104b9cab06eb35185c595448f098feae9a4 Mon Sep 17 00:00:00 2001 From: Gellert Hegyi Date: Sun, 3 Nov 2024 18:21:14 +0100 Subject: [PATCH] fixes preventing clicks when dragging --- electron_drag_click.mm | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/electron_drag_click.mm b/electron_drag_click.mm index 5d6269a1..ffe3047b 100644 --- a/electron_drag_click.mm +++ b/electron_drag_click.mm @@ -7,7 +7,8 @@ static IMP g_originalHitTest; static IMP g_originalMouseEvent; -static char kAssociatedObjectKey; +static char kIsDraggableKey; +static char kIsDraggingKey; NSView* viewUnderneathPoint(NSView* self, NSPoint point) { NSView *contentView = self.window.contentView; @@ -21,6 +22,7 @@ NSView* viewUnderneathPoint(NSView* self, NSPoint point) { } } } + return nil; } @@ -28,11 +30,10 @@ NSView* swizzledHitTest(id obj, SEL sel, NSPoint point) { NSView* originalReturn = ((NSView*(*) (id, SEL, NSPoint))g_originalHitTest)(obj, sel, point); - NSNumber* isDraggable = @(originalReturn == nil); objc_setAssociatedObject(obj, - &kAssociatedObjectKey, - isDraggable, - OBJC_ASSOCIATION_COPY_NONATOMIC); + &kIsDraggableKey, + @(originalReturn == nil), + OBJC_ASSOCIATION_COPY_NONATOMIC); NSView* viewUnderPoint = viewUnderneathPoint(obj, point); @@ -40,16 +41,32 @@ NSView* swizzledHitTest(id obj, SEL sel, NSPoint point) { } void swizzledMouseEvent(id obj, SEL sel, NSEvent* theEvent) { - ((void(*) (id, SEL, NSEvent*))g_originalMouseEvent)(obj, sel, theEvent); - NSView* view = obj; - NSNumber* isDragging = objc_getAssociatedObject(view.window.contentView, - &kAssociatedObjectKey); + NSNumber* isDraggable = objc_getAssociatedObject(view.window.contentView, + &kIsDraggableKey); + NSNumber* isDragging = objc_getAssociatedObject(obj, + &kIsDraggingKey); - if ([theEvent type] == NSEventTypeLeftMouseDown && isDragging.boolValue) { + if ([theEvent type] == NSEventTypeLeftMouseDown && isDraggable.boolValue) { NSView* self = obj; [self.window performWindowDragWithEvent:theEvent]; } + + if ([theEvent type] == NSEventTypeLeftMouseDragged && isDraggable.boolValue) { + objc_setAssociatedObject(obj, + &kIsDraggingKey, + @(YES), + OBJC_ASSOCIATION_COPY_NONATOMIC); + } + + if ([theEvent type] == NSEventTypeLeftMouseUp && isDragging.boolValue) { + objc_setAssociatedObject(obj, + &kIsDraggingKey, + @(NO), + OBJC_ASSOCIATION_COPY_NONATOMIC); + } else { + ((void(*) (id, SEL, NSEvent*))g_originalMouseEvent)(obj, sel, theEvent); + } } void Setup(const Napi::CallbackInfo &info) {