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: add set_minimized and set_maximized #2292

Merged
merged 9 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`"]
Expand Down Expand Up @@ -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>,
}
}
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 @@ -148,6 +148,8 @@ pub fn handle_app_output(
window_pos,
visible,
always_on_top,
minimized,
maximized,
} = app_output;

if let Some(decorated) = decorated {
Expand Down Expand Up @@ -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);
}
}

// ----------------------------------------------------------------------------
Expand Down
48 changes: 37 additions & 11 deletions examples/custom_window_frame/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ fn main() {
}

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

impl eframe::App for MyApp {
fn clear_color(&self, _visuals: &egui::Visuals) -> egui::Rgba {
egui::Rgba::TRANSPARENT // 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 @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down