Repository navigation
How to request window focus in Bevy 0.19 #25594
|
Is there some kind of function available to have the window "request focus"? I'm honestly remembering back to time in Java where I would need to call that at window initialization. Is there anything I can do like that in Bevy? I was referencing the Window Settings example on the website, so I liked the delayed visibility of the window but I would also like to have the window request focus once it becomes visible. |
Replies: 1 comment
|
Yes. In Bevy 0.19, changing There are two details worth handling in the delayed-visibility example:
Start the window unfocused, show it, then request focus on the next update: primary_window: Some(Window {
visible: false,
focused: false,
..default()
}),fn make_visible_and_focus(
mut window: Single<&mut Window>,
frames: Res<FrameCount>,
) {
match frames.0 {
3 => window.visible = true,
4 => window.focused = true,
_ => {}
}
}If the platform focuses the window automatically when it becomes visible, the focus event may already have changed the component to This is platform-dependent rather than guaranteed everywhere. Bevy 0.19 documents post-creation focus as unsupported on iOS, Android, Web, and Wayland. On supported desktop backends, it is still a request and the window manager may apply its own focus-stealing policy. References in the 0.19 source: |
Yes. In Bevy 0.19, changing
Window::focusedfromfalsetotruemakesbevy_winitcall winit'sfocus_window().There are two details worth handling in the delayed-visibility example:
focuseddefaults totrue, so setting it totruelater would not be a change and would not issue a new request.Start the window unfocused, show it, then request focus on the next update: