diff --git a/atom/browser/api/atom_api_top_level_window.cc b/atom/browser/api/atom_api_top_level_window.cc index 365f97f0d8fb..a8c49390eefb 100644 --- a/atom/browser/api/atom_api_top_level_window.cc +++ b/atom/browser/api/atom_api_top_level_window.cc @@ -194,6 +194,13 @@ void TopLevelWindow::OnWindowRestore() { Emit("restore"); } +void TopLevelWindow::OnWindowWillResize(const gfx::Rect& new_bounds, + bool* prevent_default) { + if (Emit("will-resize", new_bounds)) { + *prevent_default = true; + } +} + void TopLevelWindow::OnWindowResize() { Emit("resize"); } diff --git a/atom/browser/api/atom_api_top_level_window.h b/atom/browser/api/atom_api_top_level_window.h index fe7c8b3b95ab..f4870cd7e223 100644 --- a/atom/browser/api/atom_api_top_level_window.h +++ b/atom/browser/api/atom_api_top_level_window.h @@ -60,6 +60,8 @@ class TopLevelWindow : public mate::TrackableObject, void OnWindowUnmaximize() override; void OnWindowMinimize() override; void OnWindowRestore() override; + void OnWindowWillResize(const gfx::Rect& new_bounds, + bool* prevent_default) override; void OnWindowResize() override; void OnWindowMove() override; void OnWindowMoved() override; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index a339a2d23a33..4ff2bcfcabfd 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -458,6 +458,12 @@ void NativeWindow::NotifyWindowRestore() { observer.OnWindowRestore(); } +void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds, + bool* prevent_default) { + for (NativeWindowObserver& observer : observers_) + observer.OnWindowWillResize(new_bounds, prevent_default); +} + void NativeWindow::NotifyWindowResize() { for (NativeWindowObserver& observer : observers_) observer.OnWindowResize(); diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index d28a387b8372..35b67e2dd230 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -236,6 +236,8 @@ class NativeWindow : public base::SupportsUserData, void NotifyWindowMinimize(); void NotifyWindowRestore(); void NotifyWindowMove(); + void NotifyWindowWillResize(const gfx::Rect& new_bounds, + bool* prevent_default); void NotifyWindowResize(); void NotifyWindowMoved(); void NotifyWindowScrollTouchBegin(); diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index 33e9eb643254..c2a86ea6b65c 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -16,6 +16,10 @@ #include #endif +namespace gfx { +class Rect; +} + namespace atom { class NativeWindowObserver { @@ -63,6 +67,8 @@ class NativeWindowObserver { virtual void OnWindowUnmaximize() {} virtual void OnWindowMinimize() {} virtual void OnWindowRestore() {} + virtual void OnWindowWillResize(const gfx::Rect& new_bounds, + bool* prevent_default) {} virtual void OnWindowResize() {} virtual void OnWindowMove() {} virtual void OnWindowMoved() {} diff --git a/atom/browser/native_window_views_win.cc b/atom/browser/native_window_views_win.cc index 0d2c9b8c7131..0e61d18b9159 100644 --- a/atom/browser/native_window_views_win.cc +++ b/atom/browser/native_window_views_win.cc @@ -187,6 +187,16 @@ bool NativeWindowViews::PreHandleMSG(UINT message, if (HIWORD(w_param) == THBN_CLICKED) return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param)); return false; + case WM_SIZING: { + bool prevent_default = false; + NotifyWindowWillResize(gfx::Rect(*reinterpret_cast(l_param)), + &prevent_default); + if (prevent_default) { + ::GetWindowRect(GetAcceleratedWidget(), + reinterpret_cast(l_param)); + } + return false; + } case WM_SIZE: { // Handle window state change. HandleSizeEvent(w_param, l_param); @@ -198,7 +208,8 @@ bool NativeWindowViews::PreHandleMSG(UINT message, } case WM_MOVING: { if (!movable_) - ::GetWindowRect(GetAcceleratedWidget(), (LPRECT)l_param); + ::GetWindowRect(GetAcceleratedWidget(), + reinterpret_cast(l_param)); return false; } case WM_MOVE: { diff --git a/atom/browser/ui/cocoa/atom_ns_window_delegate.mm b/atom/browser/ui/cocoa/atom_ns_window_delegate.mm index 78bfe1fcd7b5..c58da546dc8d 100644 --- a/atom/browser/ui/cocoa/atom_ns_window_delegate.mm +++ b/atom/browser/ui/cocoa/atom_ns_window_delegate.mm @@ -112,6 +112,15 @@ extraHeightPlusFrame); } + { + bool prevent_default = false; + gfx::Rect new_bounds(gfx::Point(sender.frame.origin), gfx::Size(newSize)); + shell_->NotifyWindowWillResize(new_bounds, &prevent_default); + if (prevent_default) { + return sender.frame.size; + } + } + return newSize; } diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index f03c0cc91e42..8e3276935412 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -490,9 +490,20 @@ Emitted when the window is minimized. Emitted when the window is restored from a minimized state. +#### Event: 'will-resize' _macOS_ _Windows_ + +Returns: + +* `event` Event +* `newBounds` [`Rectangle`](structures/rectangle.md) - Size the window is being resized to. + +Emitted before the window is resized. Calling `event.preventDefault()` will prevent the window from being resized. + +Note that this is only emitted when the window is being resized manually. Resizing the window with `setBounds`/`setSize` will not emit this event. + #### Event: 'resize' -Emitted when the window is being resized. +Emitted after the window has been resized. #### Event: 'move'