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 4 commits
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
23 changes: 23 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 @@ -824,6 +836,9 @@ pub struct WindowInfo {

/// Current monitor size in egui points (logical pixels)
pub monitor_size: Option<egui::Vec2>,

/// Are we in maximize mode?
pub maximized: bool,
}

/// Information about the URL.
Expand Down Expand Up @@ -1001,5 +1016,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>,
}
}
11 changes: 11 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn read_window_info(window: &winit::window::Window, pixels_per_point: f32) -
y: size.height,
},
monitor_size,
maximized: window.is_maximized(),
}
}

Expand Down Expand Up @@ -148,6 +149,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 +193,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
42 changes: 33 additions & 9 deletions examples/custom_window_frame/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,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 +95,39 @@ 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 is_maximized = frame.info().window_info.maximized;
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 is_maximized { "🗗" } else { "🗖" }).size(height - 4.0))
.frame(false),
);
if maximized_response.clicked() {
frame.set_maximized(!is_maximized);
}

// Add the contents:
let content_rect = {
let mut rect = rect;
Expand Down