From 6aefe4346fb61f2b08651a10a1cc6d1965e4c5d9 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 11 Sep 2024 15:30:22 +0200 Subject: [PATCH 1/2] Do not emit ScaleFactorChanged upon window creation --- src/changelog/unreleased.md | 1 + src/event.rs | 8 +++++ .../apple/appkit/window_delegate.rs | 6 ---- src/platform_impl/apple/uikit/window.rs | 30 ------------------- 4 files changed, 9 insertions(+), 36 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 6f55ff7f2c..98861a7972 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -124,6 +124,7 @@ changelog entry. - `Window::set_max_inner_size` to `set_max_surface_size`. To migrate, you can probably just replace all instances of `inner_size` with `surface_size` in your codebase. +- On macOS and iOS, no longer emit `ScaleFactorChanged` upon window creation. ### Removed diff --git a/src/event.rs b/src/event.rs index 223992db8f..c801c49b91 100644 --- a/src/event.rs +++ b/src/event.rs @@ -153,6 +153,9 @@ pub enum WindowEvent { /// Contains the new dimensions of the surface (can also be retrieved with /// [`Window::surface_size`]). /// + /// This event will not necessarily be emitted upon window creation, query + /// [`Window::surface_size`] if you need to determine the surface's initial size. + /// /// [`Window::surface_size`]: crate::window::Window::surface_size SurfaceResized(PhysicalSize), @@ -368,7 +371,12 @@ pub enum WindowEvent { /// To update the window size, use the provided [`SurfaceSizeWriter`] handle. By default, the /// window is resized to the value suggested by the OS, but it can be changed to any value. /// + /// This event will not necessarily be emitted upon window creation, query + /// [`Window::scale_factor`] if you need to determine the window's initial scale factor. + /// /// For more information about DPI in general, see the [`dpi`] crate. + /// + /// [`Window::scale_factor`]: crate::window::Window::scale_factor ScaleFactorChanged { scale_factor: f64, /// Handle to update surface size during scale changes. diff --git a/src/platform_impl/apple/appkit/window_delegate.rs b/src/platform_impl/apple/appkit/window_delegate.rs index fdad0c515e..a4667a573f 100644 --- a/src/platform_impl/apple/appkit/window_delegate.rs +++ b/src/platform_impl/apple/appkit/window_delegate.rs @@ -740,12 +740,6 @@ impl WindowDelegate { }); let delegate: Retained = unsafe { msg_send_id![super(delegate), init] }; - if scale_factor != 1.0 { - let delegate = delegate.clone(); - RunLoop::main(mtm).queue_closure(move || { - delegate.handle_scale_factor_changed(scale_factor); - }); - } window.setDelegate(Some(ProtocolObject::from_ref(&*delegate))); // Listen for theme change event. diff --git a/src/platform_impl/apple/uikit/window.rs b/src/platform_impl/apple/uikit/window.rs index b4ca0bc721..2c7be6edbe 100644 --- a/src/platform_impl/apple/uikit/window.rs +++ b/src/platform_impl/apple/uikit/window.rs @@ -510,36 +510,6 @@ impl Window { let window = WinitUIWindow::new(mtm, &window_attributes, frame, &view_controller); window.makeKeyAndVisible(); - // Like the Windows and macOS backends, we send a `ScaleFactorChanged` and `SurfaceResized` - // event on window creation if the DPI factor != 1.0 - let scale_factor = view.contentScaleFactor(); - let scale_factor = scale_factor as f64; - if scale_factor != 1.0 { - let bounds = view.bounds(); - let screen = window.screen(); - let screen_space = screen.coordinateSpace(); - let screen_frame = view.convertRect_toCoordinateSpace(bounds, &screen_space); - let size = LogicalSize { - width: screen_frame.size.width as f64, - height: screen_frame.size.height as f64, - }; - let window_id = CoreWindowId(window.id()); - app_state::handle_nonuser_events( - mtm, - std::iter::once(EventWrapper::ScaleFactorChanged(app_state::ScaleFactorChanged { - window: window.clone(), - scale_factor, - suggested_size: size.to_physical(scale_factor), - })) - .chain(std::iter::once(EventWrapper::StaticEvent( - Event::WindowEvent { - window_id, - event: WindowEvent::SurfaceResized(size.to_physical(scale_factor)), - }, - ))), - ); - } - let inner = Inner { window, view_controller, view, gl_or_metal_backed }; Ok(Window { inner: MainThreadBound::new(inner, mtm) }) } From cd8a06285beecffd1accd830f4bef7947f6a1c26 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 11 Sep 2024 16:58:09 +0200 Subject: [PATCH 2/2] Do not emit Focused upon window creation --- src/changelog/unreleased.md | 1 + src/event.rs | 3 +++ src/platform_impl/apple/appkit/window_delegate.rs | 4 ---- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 98861a7972..840e155d34 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -125,6 +125,7 @@ changelog entry. To migrate, you can probably just replace all instances of `inner_size` with `surface_size` in your codebase. - On macOS and iOS, no longer emit `ScaleFactorChanged` upon window creation. +- On macOS, no longer emit `Focused` upon window creation. ### Removed diff --git a/src/event.rs b/src/event.rs index c801c49b91..e9ca08d997 100644 --- a/src/event.rs +++ b/src/event.rs @@ -193,6 +193,9 @@ pub enum WindowEvent { /// The window gained or lost focus. /// /// The parameter is true if the window has gained focus, and false if it has lost focus. + /// + /// Windows are unfocused upon creation, but will usually be focused by the system soon + /// afterwards. Focused(bool), /// An event from the keyboard has been received. diff --git a/src/platform_impl/apple/appkit/window_delegate.rs b/src/platform_impl/apple/appkit/window_delegate.rs index a4667a573f..24b23fc425 100644 --- a/src/platform_impl/apple/appkit/window_delegate.rs +++ b/src/platform_impl/apple/appkit/window_delegate.rs @@ -770,10 +770,6 @@ impl WindowDelegate { delegate.set_cursor(attrs.cursor); - // XXX Send `Focused(false)` right after creating the window delegate, so we won't - // obscure the real focused events on the startup. - delegate.queue_event(WindowEvent::Focused(false)); - // Set fullscreen mode after we setup everything delegate.set_fullscreen(attrs.fullscreen.map(Into::into));