Skip to content

Commit

Permalink
fix: emit resize event on frame_did_change on macOS, closes #436 (#439)
Browse files Browse the repository at this point in the history
* On macOS, emit resize event on frame_did_change

When the window switches mode from normal to tabbed one, it doesn't
get resized, however the frame gets resized. This commit makes
winit to track resizes when frame changes instead of window.

Co-authored-by: Phillip Hellewell <sshock@users.noreply.github.com>
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) and sshock committed Jun 22, 2022
1 parent 5c9cc21 commit 54062ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/frame-did-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tao": patch
---

On macOS, `WindowEvent::Resized` is now emitted in `frameDidChange` instead of `windowDidResize`.

12 changes: 11 additions & 1 deletion src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use objc::{
};

use crate::{
dpi::LogicalPosition,
dpi::{LogicalPosition, LogicalSize},
event::{
DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent,
},
Expand Down Expand Up @@ -374,6 +374,16 @@ extern "C" fn frame_did_change(this: &Object, _sel: Sel, _event: id) {
assumeInside:NO
];

// Emit resize event here rather than from windowDidResize because:
// 1. When a new window is created as a tab, the frame size may change without a window resize occurring.
// 2. Even when a window resize does occur on a new tabbed window, it contains the wrong size (includes tab height).
let logical_size = LogicalSize::new(rect.size.width as f64, rect.size.height as f64);
let size = logical_size.to_physical::<u32>(state.get_scale_factor());
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::Resized(size),
}));

state.tracking_rect = Some(tracking_rect);
}
}
Expand Down
10 changes: 1 addition & 9 deletions src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@ impl WindowDelegateState {
AppState::queue_event(wrapper);
}

pub fn emit_resize_event(&mut self) {
let rect = unsafe { NSView::frame(*self.ns_view) };
let scale_factor = self.get_scale_factor();
let logical_size = LogicalSize::new(rect.size.width as f64, rect.size.height as f64);
let size = logical_size.to_physical(scale_factor);
self.emit_event(WindowEvent::Resized(size));
}

fn emit_move_event(&mut self) {
let rect = unsafe { NSWindow::frame(*self.ns_window) };
let x = rect.origin.x as f64;
Expand Down Expand Up @@ -335,7 +327,7 @@ extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) {
trace!("Triggered `windowDidResize:`");
with_state(this, |state| {
if !state.is_checking_zoomed_in {
state.emit_resize_event();
// NOTE: WindowEvent::Resized is reported in frameDidChange.
state.emit_move_event();
}
});
Expand Down

0 comments on commit 54062ca

Please sign in to comment.