Skip to content

Commit

Permalink
feat: 'will-move' event for windows. (#14283)
Browse files Browse the repository at this point in the history
* feat: 'will-resize' window event (Windows only)

* documentation for 'will-move' event

* comment and line break fix in docs
  • Loading branch information
sgd2z authored and ckerr committed Aug 28, 2018
1 parent f1fe485 commit afdb6c5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions atom/browser/api/atom_api_top_level_window.cc
Expand Up @@ -205,6 +205,13 @@ void TopLevelWindow::OnWindowResize() {
Emit("resize");
}

void TopLevelWindow::OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) {
if (Emit("will-move", new_bounds)) {
*prevent_default = true;
}
}

void TopLevelWindow::OnWindowMove() {
Emit("move");
}
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/api/atom_api_top_level_window.h
Expand Up @@ -63,6 +63,8 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
void OnWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default) override;
void OnWindowResize() override;
void OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) override;
void OnWindowMove() override;
void OnWindowMoved() override;
void OnWindowScrollTouchBegin() override;
Expand Down
6 changes: 6 additions & 0 deletions atom/browser/native_window.cc
Expand Up @@ -468,6 +468,12 @@ void NativeWindow::NotifyWindowWillResize(const gfx::Rect& new_bounds,
observer.OnWindowWillResize(new_bounds, prevent_default);
}

void NativeWindow::NotifyWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowWillMove(new_bounds, prevent_default);
}

void NativeWindow::NotifyWindowResize() {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowResize();
Expand Down
1 change: 1 addition & 0 deletions atom/browser/native_window.h
Expand Up @@ -241,6 +241,7 @@ class NativeWindow : public base::SupportsUserData,
void NotifyWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default);
void NotifyWindowResize();
void NotifyWindowWillMove(const gfx::Rect& new_bounds, bool* prevent_default);
void NotifyWindowMoved();
void NotifyWindowScrollTouchBegin();
void NotifyWindowScrollTouchEnd();
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/native_window_observer.h
Expand Up @@ -70,6 +70,8 @@ class NativeWindowObserver {
virtual void OnWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default) {}
virtual void OnWindowResize() {}
virtual void OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) {}
virtual void OnWindowMove() {}
virtual void OnWindowMoved() {}
virtual void OnWindowScrollTouchBegin() {}
Expand Down
9 changes: 8 additions & 1 deletion atom/browser/native_window_views_win.cc
Expand Up @@ -207,9 +207,16 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
return false;
}
case WM_MOVING: {
if (!movable_)
bool prevent_default = false;
NotifyWindowWillMove(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
&prevent_default);
if (!movable_ || prevent_default) {
::GetWindowRect(GetAcceleratedWidget(),
reinterpret_cast<RECT*>(l_param));
return true; // Tells Windows that the Move is handled. If not true,
// frameless windows can be moved using
// -webkit-app-region: drag elements.
}
return false;
}
case WM_MOVE: {
Expand Down
11 changes: 11 additions & 0 deletions docs/api/browser-window.md
Expand Up @@ -505,6 +505,17 @@ 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_

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.

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: 'move'

Emitted when the window is being moved to a new position.
Expand Down

0 comments on commit afdb6c5

Please sign in to comment.