From 145b4fae94c2c46864f456bc4a7bbfb188864b9f Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Tue, 20 Aug 2019 07:53:43 -0700 Subject: [PATCH] feat: implement `will-move` event on macOS (#19641) --- docs/api/browser-window.md | 4 ++-- shell/browser/ui/cocoa/atom_ns_window_delegate.mm | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 12d8f3bd038e..66a3cdd6cc29 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -516,14 +516,14 @@ Note that this is only emitted when the window is being resized manually. Resizi Emitted after the window has been resized. -#### Event: 'will-move' _Windows_ +#### Event: 'will-move' _macOS_ _Windows_ Returns: * `event` Event * `newBounds` [`Rectangle`](structures/rectangle.md) - Location the window is being moved to. -Emitted before the window is moved. Calling `event.preventDefault()` will prevent the window from being moved. +Emitted before the window is moved. On Windows, calling `event.preventDefault()` will prevent the window from being moved. Note that this is only emitted when the window is being resized manually. Resizing the window with `setBounds`/`setSize` will not emit this event. diff --git a/shell/browser/ui/cocoa/atom_ns_window_delegate.mm b/shell/browser/ui/cocoa/atom_ns_window_delegate.mm index b67b10c7d3f6..c1dea1b67d28 100644 --- a/shell/browser/ui/cocoa/atom_ns_window_delegate.mm +++ b/shell/browser/ui/cocoa/atom_ns_window_delegate.mm @@ -138,6 +138,18 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle; shell_->NotifyWindowResize(); } +- (void)windowWillMove:(NSNotification*)notification { + NSWindow* window = [notification object]; + NSSize size = [[window contentView] frame].size; + NSRect new_bounds = NSMakeRect(window.frame.origin.x, window.frame.origin.y, + size.width, size.height); + bool prevent_default = false; + + // prevent_default has no effect + shell_->NotifyWindowWillMove(gfx::ScreenRectFromNSRect(new_bounds), + &prevent_default); +} + - (void)windowDidMove:(NSNotification*)notification { [super windowDidMove:notification]; // TODO(zcbenz): Remove the alias after figuring out a proper