1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

eframe: ask if the window is minimized or maximized (#2672)

* eframe: ask if the window is minimized or maximized

* Improve note
This commit is contained in:
Emil Ernerfeldt
2023-02-04 16:05:23 +01:00
committed by GitHub
parent 430cbe541c
commit 660566c499
3 changed files with 84 additions and 43 deletions

View File

@@ -22,9 +22,7 @@ fn main() -> Result<(), eframe::Error> {
}
#[derive(Default)]
struct MyApp {
maximized: bool,
}
struct MyApp {}
impl eframe::App for MyApp {
fn clear_color(&self, _visuals: &egui::Visuals) -> [f32; 4] {
@@ -32,7 +30,7 @@ impl eframe::App for MyApp {
}
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
custom_window_frame(self, ctx, frame, "egui with custom frame", |ui| {
custom_window_frame(ctx, frame, "egui with custom frame", |ui| {
ui.label("This is just the contents of the window");
ui.horizontal(|ui| {
ui.label("egui theme:");
@@ -43,7 +41,6 @@ impl eframe::App for MyApp {
}
fn custom_window_frame(
app: &mut MyApp,
ctx: &egui::Context,
frame: &mut eframe::Frame,
title: &str,
@@ -55,6 +52,8 @@ fn custom_window_frame(
// Height of the title bar
let height = 28.0;
let button_height = 16.0;
CentralPanel::default()
.frame(Frame::none())
.show(ctx, |ui| {
@@ -97,46 +96,47 @@ fn custom_window_frame(
ui.interact(title_bar_rect, Id::new("title_bar"), Sense::click());
if title_bar_response.double_clicked() {
app.maximized = !app.maximized;
frame.set_maximized(app.maximized);
frame.set_maximized(!frame.info().window_info.maximized);
} else if title_bar_response.is_pointer_button_down_on() {
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();
}
ui.allocate_ui_at_rect(title_bar_rect, |ui| {
ui.horizontal_centered(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.visuals_mut().button_frame = false;
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 close_response = ui
.add(Button::new(RichText::new("").size(button_height)))
.on_hover_text("Close the window");
if close_response.clicked() {
frame.close();
}
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(if app.maximized { "🗗" } else { "🗖" }).size(height - 4.0),
)
.frame(false),
);
if maximized_response.clicked() {
app.maximized = !app.maximized;
frame.set_maximized(app.maximized);
}
let minimized_response = ui
.add(Button::new(RichText::new("🗕").size(button_height)))
.on_hover_text("Minimize the window");
if minimized_response.clicked() {
frame.set_minimized(true);
}
if frame.info().window_info.maximized {
let maximized_response = ui
.add(Button::new(RichText::new("🗖").size(button_height)))
.on_hover_text("Restore window");
if maximized_response.clicked() {
frame.set_maximized(false);
}
} else {
let maximized_response = ui
.add(Button::new(RichText::new("🗗").size(button_height)))
.on_hover_text("Maximize window");
if maximized_response.clicked() {
frame.set_maximized(true);
}
}
});
});
// Add the contents:
let content_rect = {