1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

add actions for window controls

This commit is contained in:
SunDoge
2022-11-12 16:26:28 +08:00
parent f790e248e4
commit 634d506012
3 changed files with 67 additions and 11 deletions

View File

@@ -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<bool>,
/// Set to some bool to minimize or unminimize window.
#[cfg(not(target_arch = "wasm32"))]
pub minimized: Option<bool>,
/// Set to some bool to maximize or unmaximize window.
#[cfg(not(target_arch = "wasm32"))]
pub maximized: Option<bool>,
}
}

View File

@@ -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);
}
}
// ----------------------------------------------------------------------------

View File

@@ -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;