1
0
Fork 0

Only send resize event on macOS on actual change

To follow the same behavior as on Linux and Windows.
This commit is contained in:
Robbert van der Helm 2022-12-01 18:32:35 +01:00
parent 537c303ee5
commit 80fb0a00b8
2 changed files with 31 additions and 7 deletions

View file

@ -240,12 +240,17 @@ extern "C" fn view_did_change_backing_properties(this: &Object, _: Sel, _: id) {
let bounds: NSRect = msg_send![this, bounds];
let window_info = WindowInfo::from_logical_size(
let new_window_info = WindowInfo::from_logical_size(
Size::new(bounds.size.width, bounds.size.height),
scale_factor,
);
state.trigger_event(Event::Window(WindowEvent::Resized(window_info)));
// Only send the event when the window's size has actually changed to be in line with the
// other platform implementations
if new_window_info.physical_size() != state.window_info.physical_size() {
state.window_info = new_window_info;
state.trigger_event(Event::Window(WindowEvent::Resized(new_window_info)));
}
}
}
@ -364,6 +369,6 @@ extern "C" fn scroll_wheel(this: &Object, _: Sel, event: id) {
state.trigger_event(Event::Mouse(MouseEvent::WheelScrolled {
delta,
modifiers: make_modifiers(modifiers)
modifiers: make_modifiers(modifiers),
}));
}

View file

@ -124,6 +124,13 @@ impl Window {
{
let pool = unsafe { NSAutoreleasePool::new(nil) };
let scaling = match options.scale {
WindowScalePolicy::ScaleFactor(scale) => scale,
WindowScalePolicy::SystemScaleFactor => 1.0,
};
let window_info = WindowInfo::from_logical_size(options.size, scaling);
let handle = if let RawWindowHandle::AppKit(handle) = parent.raw_window_handle() {
handle
} else {
@ -144,7 +151,7 @@ impl Window {
.map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)),
};
let window_handle = Self::init(true, window, build);
let window_handle = Self::init(true, window, window_info, build);
unsafe {
let _: id = msg_send![handle.ns_view as *mut Object, addSubview: ns_view];
@ -164,6 +171,13 @@ impl Window {
{
let pool = unsafe { NSAutoreleasePool::new(nil) };
let scaling = match options.scale {
WindowScalePolicy::ScaleFactor(scale) => scale,
WindowScalePolicy::SystemScaleFactor => 1.0,
};
let window_info = WindowInfo::from_logical_size(options.size, scaling);
let ns_view = unsafe { create_view(&options) };
let window = Window {
@ -178,7 +192,7 @@ impl Window {
.map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)),
};
let window_handle = Self::init(true, window, build);
let window_handle = Self::init(true, window, window_info, build);
unsafe {
let () = msg_send![pool, drain];
@ -254,7 +268,7 @@ impl Window {
.map(|gl_config| Self::create_gl_context(Some(ns_window), ns_view, gl_config)),
};
let _ = Self::init(false, window, build);
let _ = Self::init(false, window, window_info, build);
unsafe {
ns_window.setContentView_(ns_view);
@ -266,7 +280,9 @@ impl Window {
}
}
fn init<H, B>(parented: bool, mut window: Window, build: B) -> WindowHandle
fn init<H, B>(
parented: bool, mut window: Window, window_info: WindowInfo, build: B,
) -> WindowHandle
where
H: WindowHandler + 'static,
B: FnOnce(&mut crate::Window) -> H,
@ -285,6 +301,7 @@ impl Window {
keyboard_state: KeyboardState::new(),
frame_timer: None,
retain_count_after_build,
window_info,
_parent_handle: parent_handle,
}));
@ -325,6 +342,8 @@ pub(super) struct WindowState {
frame_timer: Option<CFRunLoopTimer>,
_parent_handle: Option<ParentHandle>,
pub retain_count_after_build: usize,
/// The last known window info for this window.
pub window_info: WindowInfo,
}
impl WindowState {