Skip to content

Commit

Permalink
eframe: add set_minimized and set_maximized (#2292)
Browse files Browse the repository at this point in the history
* add actions for window controls

* add maximized to WindowInfo
update button text
fix clippy

* add overlap icon when maximized

* remove argument `app`

* remove WindowInfo { maximized }

* Update minimum window size

* Double-click titlebar to toggle maximized state

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
  • Loading branch information
SunDoge and emilk authored Feb 4, 2023
1 parent fb5cb30 commit f0718a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
20 changes: 20 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,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`"]
Expand Down Expand Up @@ -1011,5 +1023,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>,
}
}
10 changes: 10 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ pub fn handle_app_output(
window_pos,
visible: _, // handled in post_present
always_on_top,
minimized,
maximized,
} = app_output;

if let Some(decorated) = decorated {
Expand Down Expand Up @@ -250,6 +252,14 @@ pub fn handle_app_output(
WindowLevel::Normal
});
}

if let Some(minimized) = minimized {
window.set_minimized(minimized);
}

if let Some(maximized) = maximized {
window.set_maximized(maximized);
}
}

// ----------------------------------------------------------------------------
Expand Down
43 changes: 38 additions & 5 deletions examples/custom_window_frame/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn main() -> Result<(), eframe::Error> {
decorated: false,
// To have rounded corners we need transparency:
transparent: true,
min_window_size: Some(egui::vec2(320.0, 100.0)),
initial_window_size: Some(egui::vec2(320.0, 240.0)),
min_window_size: Some(egui::vec2(400.0, 100.0)),
initial_window_size: Some(egui::vec2(400.0, 240.0)),
..Default::default()
};
eframe::run_native(
Expand All @@ -22,15 +22,17 @@ fn main() -> Result<(), eframe::Error> {
}

#[derive(Default)]
struct MyApp {}
struct MyApp {
maximized: bool,
}

impl eframe::App for MyApp {
fn clear_color(&self, _visuals: &egui::Visuals) -> [f32; 4] {
egui::Rgba::TRANSPARENT.to_array() // Make sure we don't paint anything behind the rounded corners
}

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:");
Expand All @@ -41,6 +43,7 @@ impl eframe::App for MyApp {
}

fn custom_window_frame(
app: &mut MyApp,
ctx: &egui::Context,
frame: &mut eframe::Frame,
title: &str,
Expand Down Expand Up @@ -92,7 +95,11 @@ fn custom_window_frame(
};
let title_bar_response =
ui.interact(title_bar_rect, Id::new("title_bar"), Sense::click());
if title_bar_response.is_pointer_button_down_on() {

if title_bar_response.double_clicked() {
app.maximized = !app.maximized;
frame.set_maximized(app.maximized);
} else if title_bar_response.is_pointer_button_down_on() {
frame.drag_window();
}

Expand All @@ -105,6 +112,32 @@ fn custom_window_frame(
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(if app.maximized { "🗗" } else { "🗖" }).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;
Expand Down

0 comments on commit f0718a6

Please sign in to comment.