From da2bba866de1d1685517b33fa633730ffe04d0e6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 12 Oct 2020 11:29:17 -0700 Subject: [PATCH] window: macos: trigger resize event when screen resolution changes The easiest repro for this is dragging a window between monitors. refs: #161 --- window/src/os/macos/window.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index d435cdf9d..dbdf12d0f 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -298,6 +298,7 @@ impl Window { callbacks, view_id: None, window_id, + screen_changed: false, #[cfg(feature = "opengl")] gl_context_pair: None, text_cursor_position: Rect::new(Point::new(0, 0), Size::new(0, 0)), @@ -598,6 +599,7 @@ struct Inner { callbacks: Box, view_id: Option, window_id: usize, + screen_changed: bool, #[cfg(feature = "opengl")] gl_context_pair: Option, text_cursor_position: Rect, @@ -1146,6 +1148,17 @@ impl WindowView { } */ + extern "C" fn did_change_screen(this: &mut Object, _sel: Sel, _notification: id) { + log::trace!("did_change_screen"); + if let Some(this) = Self::get_this(this) { + // Just set a flag; we don't want to react immediately + // as this even fires as part of a live move and the + // resize flow may try to re-position the window to + // the wrong place. + this.inner.borrow_mut().screen_changed = true; + } + } + extern "C" fn did_resize(this: &mut Object, _sel: Sel, _notification: id) { #[cfg(feature = "opengl")] { @@ -1181,6 +1194,18 @@ impl WindowView { if let Some(this) = Self::get_this(view) { let mut inner = this.inner.borrow_mut(); + if inner.screen_changed { + // If the screen resolution changed (which can also + // happen if the window was dragged to another monitor + // with different dpi), then we treat this as a resize + // event that will in turn trigger an invalidation + // and a repaint. + inner.screen_changed = false; + drop(inner); + Self::did_resize(view, sel, nil); + return; + } + #[cfg(feature = "opengl")] { if let Some(gl_context_pair) = inner.gl_context_pair.as_ref() { @@ -1314,6 +1339,10 @@ impl WindowView { sel!(windowDidResize:), Self::did_resize as extern "C" fn(&mut Object, Sel, id), ); + cls.add_method( + sel!(windowDidChangeScreen:), + Self::did_change_screen as extern "C" fn(&mut Object, Sel, id), + ); cls.add_method( sel!(windowDidBecomeKey:),