fixes preventing clicks when dragging

This commit is contained in:
Gellert Hegyi 2024-11-03 18:21:14 +01:00
parent c54fdbc68d
commit 70830104b9

View file

@ -7,7 +7,8 @@
static IMP g_originalHitTest; static IMP g_originalHitTest;
static IMP g_originalMouseEvent; static IMP g_originalMouseEvent;
static char kAssociatedObjectKey; static char kIsDraggableKey;
static char kIsDraggingKey;
NSView* viewUnderneathPoint(NSView* self, NSPoint point) { NSView* viewUnderneathPoint(NSView* self, NSPoint point) {
NSView *contentView = self.window.contentView; NSView *contentView = self.window.contentView;
@ -21,6 +22,7 @@ NSView* viewUnderneathPoint(NSView* self, NSPoint point) {
} }
} }
} }
return nil; return nil;
} }
@ -28,11 +30,10 @@ NSView* swizzledHitTest(id obj, SEL sel, NSPoint point) {
NSView* originalReturn = NSView* originalReturn =
((NSView*(*) (id, SEL, NSPoint))g_originalHitTest)(obj, sel, point); ((NSView*(*) (id, SEL, NSPoint))g_originalHitTest)(obj, sel, point);
NSNumber* isDraggable = @(originalReturn == nil);
objc_setAssociatedObject(obj, objc_setAssociatedObject(obj,
&kAssociatedObjectKey, &kIsDraggableKey,
isDraggable, @(originalReturn == nil),
OBJC_ASSOCIATION_COPY_NONATOMIC); OBJC_ASSOCIATION_COPY_NONATOMIC);
NSView* viewUnderPoint = viewUnderneathPoint(obj, point); 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 swizzledMouseEvent(id obj, SEL sel, NSEvent* theEvent) {
((void(*) (id, SEL, NSEvent*))g_originalMouseEvent)(obj, sel, theEvent);
NSView* view = obj; NSView* view = obj;
NSNumber* isDragging = objc_getAssociatedObject(view.window.contentView, NSNumber* isDraggable = objc_getAssociatedObject(view.window.contentView,
&kAssociatedObjectKey); &kIsDraggableKey);
NSNumber* isDragging = objc_getAssociatedObject(obj,
&kIsDraggingKey);
if ([theEvent type] == NSEventTypeLeftMouseDown && isDragging.boolValue) { if ([theEvent type] == NSEventTypeLeftMouseDown && isDraggable.boolValue) {
NSView* self = obj; NSView* self = obj;
[self.window performWindowDragWithEvent:theEvent]; [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) { void Setup(const Napi::CallbackInfo &info) {