Skip to content

Commit

Permalink
On macOS, fallback resize event for NSWindow to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-Wei Wu committed Jul 12, 2022
1 parent 9d97e4a commit ab2e57e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/resize-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

On macOS, fallback resize event for NSWindow to handle.
12 changes: 1 addition & 11 deletions 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, LogicalSize},
dpi::LogicalPosition,
event::{
DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent,
},
Expand Down Expand Up @@ -374,16 +374,6 @@ 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: 9 additions & 1 deletion src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ 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 @@ -327,7 +335,7 @@ extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) {
trace!("Triggered `windowDidResize:`");
with_state(this, |state| {
if !state.is_checking_zoomed_in {
// NOTE: WindowEvent::Resized is reported in frameDidChange.
state.emit_resize_event();
state.emit_move_event();
}
});
Expand Down

0 comments on commit ab2e57e

Please sign in to comment.