rust-windowing / rust-windowing/winit

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

Open
#4,586 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

B - bug DS - win32
Dominant language
Rust
Stars
6.2k
Forks
1.3k
Avg merge
2d 19h
Merged PRs (30d)
9

Description

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 (c4afadbfabf7b1e7989b40b493db1a4c7bd8ff4e)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with winit/examples/window.rs and reproduce the report using cargo run --example window with an invisible maximized window. Trace the Windows focus handling around create_window, set_visible(true), and WindowEvent::Focused; done means clicking the opened window emits Window focus changed: true and updates the internal focus state.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.