eframe: read the state of native window's input focus and request it (#2900)
* Add ability to read the native window's focus state * Add `eframe::Frame::focus()` for requesting the native window's focus * rename the output field `active` → `focused` for consistency
This commit is contained in:
parent
b80c0e6ff6
commit
6f1e66731e
|
|
@ -814,6 +814,15 @@ impl Frame {
|
||||||
self.output.minimized = Some(minimized);
|
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)
|
/// Maximize or unmaximize window. (native only)
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub fn set_maximized(&mut self, maximized: bool) {
|
pub fn set_maximized(&mut self, maximized: bool) {
|
||||||
|
|
@ -938,6 +947,9 @@ pub struct WindowInfo {
|
||||||
/// Are we maximized?
|
/// Are we maximized?
|
||||||
pub maximized: bool,
|
pub maximized: bool,
|
||||||
|
|
||||||
|
/// Is the window focused and able to receive input?
|
||||||
|
pub focused: bool,
|
||||||
|
|
||||||
/// Window inner size in egui points (logical pixels).
|
/// Window inner size in egui points (logical pixels).
|
||||||
pub size: egui::Vec2,
|
pub size: egui::Vec2,
|
||||||
|
|
||||||
|
|
@ -1129,6 +1141,10 @@ pub(crate) mod backend {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub maximized: Option<bool>,
|
pub maximized: Option<bool>,
|
||||||
|
|
||||||
|
/// Set to some bool to focus window.
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub focus: Option<bool>,
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub screenshot_requested: bool,
|
pub screenshot_requested: bool,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ pub fn read_window_info(
|
||||||
fullscreen: window.fullscreen().is_some(),
|
fullscreen: window.fullscreen().is_some(),
|
||||||
minimized: window_state.minimized,
|
minimized: window_state.minimized,
|
||||||
maximized: window_state.maximized,
|
maximized: window_state.maximized,
|
||||||
|
focused: window.has_focus(),
|
||||||
size: egui::Vec2 {
|
size: egui::Vec2 {
|
||||||
x: size.width,
|
x: size.width,
|
||||||
y: size.height,
|
y: size.height,
|
||||||
|
|
@ -231,6 +232,7 @@ pub fn handle_app_output(
|
||||||
screenshot_requested: _, // handled by the rendering backend,
|
screenshot_requested: _, // handled by the rendering backend,
|
||||||
minimized,
|
minimized,
|
||||||
maximized,
|
maximized,
|
||||||
|
focus,
|
||||||
} = app_output;
|
} = app_output;
|
||||||
|
|
||||||
if let Some(decorated) = decorated {
|
if let Some(decorated) = decorated {
|
||||||
|
|
@ -284,6 +286,10 @@ pub fn handle_app_output(
|
||||||
window.set_maximized(maximized);
|
window.set_maximized(maximized);
|
||||||
window_state.maximized = maximized;
|
window_state.maximized = maximized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if focus == Some(true) {
|
||||||
|
window.focus_window();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue