Skip to content

Commit

Permalink
fix: swipe event emission on macOS (#37965)
Browse files Browse the repository at this point in the history
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Apr 14, 2023
1 parent 31500cc commit 56122f1
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions shell/browser/ui/cocoa/electron_ns_window.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ - (NSImage*)_cornerMask;
- (int64_t)_resizeDirectionForMouseLocation:(CGPoint)location;
@end

#if IS_MAS_BUILD()
// See components/remote_cocoa/app_shim/native_widget_mac_nswindow.mm
@interface NSView (CRFrameViewAdditions)
- (void)cr_mouseDownOnFrameView:(NSEvent*)event;
Expand All @@ -40,10 +39,10 @@ - (void)cr_mouseDownOnFrameView:(NSEvent*)event;

// This class is never instantiated, it's just a container for our swizzled
// mouseDown method.
@interface SwizzledMouseDownHolderClass : NSView
@interface SwizzledMethodsClass : NSView
@end

@implementation SwizzledMouseDownHolderClass
@implementation SwizzledMethodsClass
- (void)swiz_nsthemeframe_mouseDown:(NSEvent*)event {
if ([self.window respondsToSelector:@selector(shell)]) {
electron::NativeWindowMac* shell =
Expand All @@ -64,22 +63,53 @@ - (void)swiz_nsnextstepframe_mouseDown:(NSEvent*)event {
g_nsnextstepframe_mousedown(self, @selector(mouseDown:), event);
}
}

- (void)swiz_nsview_swipeWithEvent:(NSEvent*)event {
if ([self.window respondsToSelector:@selector(shell)]) {
electron::NativeWindowMac* shell =
(electron::NativeWindowMac*)[(id)self.window shell];
if (shell) {
if (event.deltaY == 1.0) {
shell->NotifyWindowSwipe("up");
} else if (event.deltaX == -1.0) {
shell->NotifyWindowSwipe("right");
} else if (event.deltaY == -1.0) {
shell->NotifyWindowSwipe("down");
} else if (event.deltaX == 1.0) {
shell->NotifyWindowSwipe("left");
}
}
}
}
@end

namespace {
#if IS_MAS_BUILD()
void SwizzleMouseDown(NSView* frame_view,
SEL swiz_selector,
MouseDownImpl* orig_impl) {
Method original_mousedown =
class_getInstanceMethod([frame_view class], @selector(mouseDown:));
*orig_impl = (MouseDownImpl)method_getImplementation(original_mousedown);
Method new_mousedown = class_getInstanceMethod(
[SwizzledMouseDownHolderClass class], swiz_selector);
Method new_mousedown =
class_getInstanceMethod([SwizzledMethodsClass class], swiz_selector);
method_setImplementation(original_mousedown,
method_getImplementation(new_mousedown));
}
#else
// components/remote_cocoa/app_shim/bridged_content_view.h overrides
// swipeWithEvent, so we can't just override the implementation
// in ElectronNSWindow like we do with for ex. rotateWithEvent.
void SwizzleSwipeWithEvent(NSView* view, SEL swiz_selector) {
Method original_swipe_with_event =
class_getInstanceMethod([view class], @selector(swipeWithEvent:));
Method new_swipe_with_event =
class_getInstanceMethod([SwizzledMethodsClass class], swiz_selector);
method_setImplementation(original_swipe_with_event,
method_getImplementation(new_swipe_with_event));
}
#endif
} // namespace
#endif // IS_MAS_BUILD

@implementation ElectronNSWindow

Expand Down Expand Up @@ -125,8 +155,10 @@ - (id)initWithShell:(electron::NativeWindowMac*)shell
&g_nsnextstepframe_mousedown);
}
}
#else
NSView* view = [[self contentView] superview];
SwizzleSwipeWithEvent(view, @selector(swiz_nsview_swipeWithEvent:));
#endif // IS_MAS_BUILD

shell_ = shell;
}
return self;
Expand Down Expand Up @@ -156,18 +188,6 @@ - (NSTouchBar*)makeTouchBar {

// NSWindow overrides.

- (void)swipeWithEvent:(NSEvent*)event {
if (event.deltaY == 1.0) {
shell_->NotifyWindowSwipe("up");
} else if (event.deltaX == -1.0) {
shell_->NotifyWindowSwipe("right");
} else if (event.deltaY == -1.0) {
shell_->NotifyWindowSwipe("down");
} else if (event.deltaX == 1.0) {
shell_->NotifyWindowSwipe("left");
}
}

- (void)rotateWithEvent:(NSEvent*)event {
shell_->NotifyWindowRotateGesture(event.rotation);
}
Expand Down

0 comments on commit 56122f1

Please sign in to comment.