Skip to content

Commit cfa8f02

Browse files
authored
AppKit/UIKit: Do not emit default state events upon window creation (#3912)
Do not emit redundant ScaleFactorChanged, SurfaceResized and Focused.
1 parent 2e5db75 commit cfa8f02

File tree

4 files changed

+13
-34
lines changed

4 files changed

+13
-34
lines changed

src/changelog/unreleased.md

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ changelog entry.
161161
- In the same spirit rename `DeviceEvent::MouseMotion` to `PointerMotion`.
162162
- Remove `Force::Calibrated::altitude_angle`.
163163
- On X11, use bottom-right corner for IME hotspot in `Window::set_ime_cursor_area`.
164+
- On macOS and iOS, no longer emit `ScaleFactorChanged` upon window creation.
165+
- On macOS, no longer emit `Focused` upon window creation.
164166

165167
### Removed
166168

src/event.rs

+11
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ pub enum WindowEvent {
153153
/// Contains the new dimensions of the surface (can also be retrieved with
154154
/// [`Window::surface_size`]).
155155
///
156+
/// This event will not necessarily be emitted upon window creation, query
157+
/// [`Window::surface_size`] if you need to determine the surface's initial size.
158+
///
156159
/// [`Window::surface_size`]: crate::window::Window::surface_size
157160
SurfaceResized(PhysicalSize<u32>),
158161

@@ -193,6 +196,9 @@ pub enum WindowEvent {
193196
/// The window gained or lost focus.
194197
///
195198
/// The parameter is true if the window has gained focus, and false if it has lost focus.
199+
///
200+
/// Windows are unfocused upon creation, but will usually be focused by the system soon
201+
/// afterwards.
196202
Focused(bool),
197203

198204
/// An event from the keyboard has been received.
@@ -420,7 +426,12 @@ pub enum WindowEvent {
420426
/// To update the window size, use the provided [`SurfaceSizeWriter`] handle. By default, the
421427
/// window is resized to the value suggested by the OS, but it can be changed to any value.
422428
///
429+
/// This event will not necessarily be emitted upon window creation, query
430+
/// [`Window::scale_factor`] if you need to determine the window's initial scale factor.
431+
///
423432
/// For more information about DPI in general, see the [`dpi`] crate.
433+
///
434+
/// [`Window::scale_factor`]: crate::window::Window::scale_factor
424435
ScaleFactorChanged {
425436
scale_factor: f64,
426437
/// Handle to update surface size during scale changes.

src/platform_impl/apple/appkit/window_delegate.rs

-10
Original file line numberDiff line numberDiff line change
@@ -765,12 +765,6 @@ impl WindowDelegate {
765765
});
766766
let delegate: Retained<WindowDelegate> = unsafe { msg_send_id![super(delegate), init] };
767767

768-
if scale_factor != 1.0 {
769-
let delegate = delegate.clone();
770-
RunLoop::main(mtm).queue_closure(move || {
771-
delegate.handle_scale_factor_changed(scale_factor);
772-
});
773-
}
774768
window.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
775769

776770
// Listen for theme change event.
@@ -801,10 +795,6 @@ impl WindowDelegate {
801795

802796
delegate.set_cursor(attrs.cursor);
803797

804-
// XXX Send `Focused(false)` right after creating the window delegate, so we won't
805-
// obscure the real focused events on the startup.
806-
delegate.queue_event(WindowEvent::Focused(false));
807-
808798
// Set fullscreen mode after we setup everything
809799
delegate.set_fullscreen(attrs.fullscreen.map(Into::into));
810800

src/platform_impl/apple/uikit/window.rs

-24
Original file line numberDiff line numberDiff line change
@@ -517,30 +517,6 @@ impl Window {
517517
let window = WinitUIWindow::new(mtm, &window_attributes, frame, &view_controller);
518518
window.makeKeyAndVisible();
519519

520-
// Like the Windows and macOS backends, we send a `ScaleFactorChanged` and `SurfaceResized`
521-
// event on window creation if the DPI factor != 1.0
522-
let scale_factor = view.contentScaleFactor();
523-
let scale_factor = scale_factor as f64;
524-
if scale_factor != 1.0 {
525-
let frame = view.frame();
526-
let size =
527-
LogicalSize { width: frame.size.width as f64, height: frame.size.height as f64 };
528-
app_state::handle_nonuser_events(
529-
mtm,
530-
std::iter::once(EventWrapper::ScaleFactorChanged(app_state::ScaleFactorChanged {
531-
window: window.clone(),
532-
scale_factor,
533-
suggested_size: size.to_physical(scale_factor),
534-
}))
535-
.chain(std::iter::once(EventWrapper::StaticEvent(
536-
Event::WindowEvent {
537-
window_id: window.id(),
538-
event: WindowEvent::SurfaceResized(size.to_physical(scale_factor)),
539-
},
540-
))),
541-
);
542-
}
543-
544520
let inner = Inner { window, view_controller, view, gl_or_metal_backed };
545521
Ok(Window { inner: MainThreadBound::new(inner, mtm) })
546522
}

0 commit comments

Comments
 (0)