From 634d506012174124f2e0dcaf6e86bb1b96ae803f Mon Sep 17 00:00:00 2001 From: SunDoge <384813529@qq.com> Date: Sat, 12 Nov 2022 16:26:28 +0800 Subject: [PATCH] add actions for window controls --- crates/eframe/src/epi.rs | 20 +++++++++ crates/eframe/src/native/epi_integration.rs | 10 +++++ examples/custom_window_frame/src/main.rs | 48 ++++++++++++++++----- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index 372a8d2b3..e2eb2c717 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -711,6 +711,18 @@ impl Frame { self.output.close = true; } + /// Minimize or unminimize window. (native only) + #[cfg(not(target_arch = "wasm32"))] + pub fn set_minimized(&mut self, minimized: bool) { + self.output.minimized = Some(minimized); + } + + /// Maximize or unmaximize window. (native only) + #[cfg(not(target_arch = "wasm32"))] + pub fn set_maximized(&mut self, maximized: bool) { + self.output.maximized = Some(maximized); + } + /// Tell `eframe` to close the desktop window. #[cfg(not(target_arch = "wasm32"))] #[deprecated = "Renamed `close`"] @@ -1001,5 +1013,13 @@ pub(crate) mod backend { /// Set to some bool to tell the window always on top. #[cfg(not(target_arch = "wasm32"))] pub always_on_top: Option, + + /// Set to some bool to minimize or unminimize window. + #[cfg(not(target_arch = "wasm32"))] + pub minimized: Option, + + /// Set to some bool to maximize or unmaximize window. + #[cfg(not(target_arch = "wasm32"))] + pub maximized: Option, } } diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 9d9d51e03..e21a96f72 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -148,6 +148,8 @@ pub fn handle_app_output( window_pos, visible, always_on_top, + minimized, + maximized, } = app_output; if let Some(decorated) = decorated { @@ -190,6 +192,14 @@ pub fn handle_app_output( if let Some(always_on_top) = always_on_top { window.set_always_on_top(always_on_top); } + + if let Some(minimized) = minimized { + window.set_minimized(minimized) + } + + if let Some(maximized) = maximized { + window.set_maximized(maximized); + } } // ---------------------------------------------------------------------------- diff --git a/examples/custom_window_frame/src/main.rs b/examples/custom_window_frame/src/main.rs index 372fa7c77..fd2afd314 100644 --- a/examples/custom_window_frame/src/main.rs +++ b/examples/custom_window_frame/src/main.rs @@ -21,7 +21,9 @@ fn main() { } #[derive(Default)] -struct MyApp {} +struct MyApp { + maximized: bool, +} impl eframe::App for MyApp { fn clear_color(&self, _visuals: &egui::Visuals) -> egui::Rgba { @@ -29,7 +31,7 @@ impl eframe::App for MyApp { } fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { - custom_window_frame(ctx, frame, "egui with custom frame", |ui| { + custom_window_frame(self, ctx, frame, "egui with custom frame", |ui| { ui.label("This is just the contents of the window"); ui.horizontal(|ui| { ui.label("egui theme:"); @@ -40,6 +42,7 @@ impl eframe::App for MyApp { } fn custom_window_frame( + app: &mut MyApp, ctx: &egui::Context, frame: &mut eframe::Frame, title: &str, @@ -83,15 +86,6 @@ fn custom_window_frame( Stroke::new(1.0, text_color), ); - // Add the close button: - let close_response = ui.put( - Rect::from_min_size(rect.left_top(), Vec2::splat(height)), - Button::new(RichText::new("❌").size(height - 4.0)).frame(false), - ); - if close_response.clicked() { - frame.close(); - } - // Interact with the title bar (drag to move window): let title_bar_rect = { let mut rect = rect; @@ -104,6 +98,38 @@ fn custom_window_frame( frame.drag_window(); } + // Add the close button: + let close_response = ui.put( + Rect::from_min_size(rect.left_top(), Vec2::splat(height)), + Button::new(RichText::new("❌").size(height - 4.0)).frame(false), + ); + if close_response.clicked() { + frame.close(); + } + + let minimized_response = ui.put( + Rect::from_min_size( + rect.left_top() + vec2((height - 4.0) * 1.0, 0.0), + Vec2::splat(height), + ), + Button::new(RichText::new("--").size(height - 4.0)).frame(false), + ); + if minimized_response.clicked() { + frame.set_minimized(true); + } + + let maximized_response = ui.put( + Rect::from_min_size( + rect.left_top() + vec2((height - 4.0) * 2.0, 0.0), + Vec2::splat(height), + ), + Button::new(RichText::new("O").size(height - 4.0)).frame(false), + ); + if maximized_response.clicked() { + app.maximized = !app.maximized; + frame.set_maximized(app.maximized); + } + // Add the contents: let content_rect = { let mut rect = rect;