Skip to content

Windows: Focus is broken when an invisible window is created with maximized #4586

Description

@rhysd

Description

Repro

  1. Apply the following patch
  2. Run cargo run --example window
  3. Before a window is opened, move focus to another window
  4. winit window is opened without focus
  5. Click somewhere on the winit window to move focus to the window
  6. Window focus changed: true is expected to be printed but actually it is not printed. The winit window is actually focused but its internal state is still focused == false.
diff --git a/winit/examples/window.rs b/winit/examples/window.rs
index 7d9fc59f..1e22f893 100644
--- a/winit/examples/window.rs
+++ b/winit/examples/window.rs
@@ -23,12 +23,16 @@ struct App {
 impl ApplicationHandler for App {
     fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
         #[cfg(not(web_platform))]
-        let window_attributes = WindowAttributes::default();
+        let window_attributes =
+            WindowAttributes::default().with_maximized(true).with_visible(false);
         #[cfg(web_platform)]
         let window_attributes = WindowAttributes::default()
             .with_platform_attributes(Box::new(WindowAttributesWeb::default().with_append(true)));
         self.window = match event_loop.create_window(window_attributes) {
-            Ok(window) => Some(window),
+            Ok(window) => {
+                window.set_visible(true);
+                Some(window)
+            },
             Err(err) => {
                 error!("error creating window: {err}");
                 event_loop.exit();
@@ -38,8 +42,11 @@ impl ApplicationHandler for App {
     }

     fn window_event(&mut self, event_loop: &dyn ActiveEventLoop, _: WindowId, event: WindowEvent) {
-        info!("{event:?}");
+        // info!("{event:?}");
         match event {
+            WindowEvent::Focused(focused) => {
+                println!("Window focus changed: {focused}");
+            },
             WindowEvent::CloseRequested => {
                 info!("Close was requested; stopping");
                 event_loop.exit();

Windows version

Microsoft Windows [Version 10.0.26200.8457]

Winit version

HEAD (c4afadb)

Metadata

Metadata

Assignees

No one assigned

    Labels

    B - bugDang, that shouldn't have happenedDS - win32Affects the Win32/Windows backend

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions