Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eframe: Read and request window focus #2900

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -1129,6 +1141,10 @@ pub(crate) mod backend {
#[cfg(not(target_arch = "wasm32"))]
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"))]
pub screenshot_requested: bool,
}
Expand Down
6 changes: 6 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -284,6 +286,10 @@ pub fn handle_app_output(
window.set_maximized(maximized);
window_state.maximized = maximized;
}

if focus == Some(true) {
window.focus_window();
}
}

// ----------------------------------------------------------------------------
Expand Down