diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index 6f3d8d85..76e7636f 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -814,6 +814,15 @@ impl Frame { self.output.minimized = Some(minimized); } + /// Bring the window into focus (native only). Has no effect on Wayland, or if the window is minimized or invisible. + /// + /// This method puts the window on top of other applications and takes input focus away from them, + /// which, if unexpected, will disturb the user. + #[cfg(not(target_arch = "wasm32"))] + pub fn focus(&mut self) { + self.output.focus = Some(true); + } + /// Maximize or unmaximize window. (native only) #[cfg(not(target_arch = "wasm32"))] pub fn set_maximized(&mut self, maximized: bool) { @@ -938,6 +947,9 @@ pub struct WindowInfo { /// Are we maximized? pub maximized: bool, + /// Is the window focused and able to receive input? + pub focused: bool, + /// Window inner size in egui points (logical pixels). pub size: egui::Vec2, @@ -1129,6 +1141,10 @@ pub(crate) mod backend { #[cfg(not(target_arch = "wasm32"))] pub maximized: Option, + /// Set to some bool to focus window. + #[cfg(not(target_arch = "wasm32"))] + pub focus: Option, + #[cfg(not(target_arch = "wasm32"))] pub screenshot_requested: bool, } diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 800943b6..6ebffddf 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -61,6 +61,7 @@ pub fn read_window_info( fullscreen: window.fullscreen().is_some(), minimized: window_state.minimized, maximized: window_state.maximized, + focused: window.has_focus(), size: egui::Vec2 { x: size.width, y: size.height, @@ -231,6 +232,7 @@ pub fn handle_app_output( screenshot_requested: _, // handled by the rendering backend, minimized, maximized, + focus, } = app_output; if let Some(decorated) = decorated { @@ -284,6 +286,10 @@ pub fn handle_app_output( window.set_maximized(maximized); window_state.maximized = maximized; } + + if focus == Some(true) { + window.focus_window(); + } } // ----------------------------------------------------------------------------