feat: add 'resized' event to BrowserWindow (#26216)

Also adds 'moved' event to BrowserWindow on Windows.
This commit is contained in:
Samuel Maddock 2020-11-11 19:27:24 -05:00 committed by GitHub
parent bb3fb548d8
commit 83d30c5c2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 59 additions and 3 deletions

View file

@ -544,6 +544,12 @@ Note that this is only emitted when the window is being resized manually. Resizi
Emitted after the window has been resized.
#### Event: 'resized' _macOS_ _Windows_
Emitted once when the window has finished being resized.
This is usually emitted when the window has been resized manually. On macOS, resizing the window with `setBounds`/`setSize` and setting the `animate` parameter to `true` will also emit this event once resizing has finished.
#### Event: 'will-move' _macOS_ _Windows_
Returns:
@ -559,12 +565,12 @@ Note that this is only emitted when the window is being resized manually. Resizi
Emitted when the window is being moved to a new position.
__Note__: On macOS this event is an alias of `moved`.
#### Event: 'moved' _macOS_
#### Event: 'moved' _macOS_ _Windows_
Emitted once when the window is moved to a new position.
__Note__: On macOS this event is an alias of `move`.
#### Event: 'enter-full-screen'
Emitted when the window enters a full-screen state.

View file

@ -222,6 +222,10 @@ void BaseWindow::OnWindowResize() {
Emit("resize");
}
void BaseWindow::OnWindowResized() {
Emit("resized");
}
void BaseWindow::OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) {
if (Emit("will-move", new_bounds)) {

View file

@ -63,6 +63,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
void OnWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default) override;
void OnWindowResize() override;
void OnWindowResized() override;
void OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) override;
void OnWindowMove() override;

View file

@ -497,6 +497,11 @@ void NativeWindow::NotifyWindowResize() {
observer.OnWindowResize();
}
void NativeWindow::NotifyWindowResized() {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowResized();
}
void NativeWindow::NotifyWindowMove() {
for (NativeWindowObserver& observer : observers_)
observer.OnWindowMove();

View file

@ -271,6 +271,7 @@ class NativeWindow : public base::SupportsUserData,
void NotifyWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default);
void NotifyWindowResize();
void NotifyWindowResized();
void NotifyWindowWillMove(const gfx::Rect& new_bounds, bool* prevent_default);
void NotifyWindowMoved();
void NotifyWindowScrollTouchBegin();

View file

@ -74,6 +74,7 @@ class NativeWindowObserver : public base::CheckedObserver {
virtual void OnWindowWillResize(const gfx::Rect& new_bounds,
bool* prevent_default) {}
virtual void OnWindowResize() {}
virtual void OnWindowResized() {}
virtual void OnWindowWillMove(const gfx::Rect& new_bounds,
bool* prevent_default) {}
virtual void OnWindowMove() {}

View file

@ -283,6 +283,12 @@ class NativeWindowViews : public NativeWindow,
// Whether we want to set window placement without side effect.
bool is_setting_window_placement_ = false;
// Whether the window is currently being resized.
bool is_resizing_ = false;
// Whether the window is currently being moved.
bool is_moving_ = false;
#endif
// Handles unhandled keyboard messages coming back from the renderer process.

View file

@ -259,6 +259,7 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param));
return false;
case WM_SIZING: {
is_resizing_ = true;
bool prevent_default = false;
NotifyWindowWillResize(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
&prevent_default);
@ -274,7 +275,19 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
HandleSizeEvent(w_param, l_param);
return false;
}
case WM_EXITSIZEMOVE: {
if (is_resizing_) {
NotifyWindowResized();
is_resizing_ = false;
}
if (is_moving_) {
NotifyWindowMoved();
is_moving_ = false;
}
return false;
}
case WM_MOVING: {
is_moving_ = true;
bool prevent_default = false;
NotifyWindowWillMove(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
&prevent_default);

View file

@ -201,6 +201,7 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle;
}
- (void)windowDidEndLiveResize:(NSNotification*)notification {
shell_->NotifyWindowResized();
if (is_zooming_) {
if (shell_->IsMaximized())
shell_->NotifyWindowMaximize();

View file

@ -855,6 +855,15 @@ describe('BrowserWindow module', () => {
const expectedBounds = Object.assign(fullBounds, boundsUpdate);
expectBoundsEqual(w.getBounds(), expectedBounds);
});
ifit(process.platform === 'darwin')('on macOS', () => {
it('emits \'resized\' event after animating', async () => {
const fullBounds = { x: 440, y: 225, width: 500, height: 400 };
w.setBounds(fullBounds, true);
await expect(emittedOnce(w, 'resized')).to.eventually.be.fulfilled();
});
});
});
describe('BrowserWindow.setSize(width, height)', () => {
@ -867,6 +876,15 @@ describe('BrowserWindow module', () => {
expectBoundsEqual(w.getSize(), size);
});
ifit(process.platform === 'darwin')('on macOS', () => {
it('emits \'resized\' event after animating', async () => {
const size = [300, 400];
w.setSize(size[0], size[1], true);
await expect(emittedOnce(w, 'resized')).to.eventually.be.fulfilled();
});
});
});
describe('BrowserWindow.setMinimum/MaximumSize(width, height)', () => {