From 430e910e4bce749980d4366582f193e80e6cb415 Mon Sep 17 00:00:00 2001 From: lopo Date: Mon, 8 Apr 2024 11:49:28 +0800 Subject: [PATCH 1/9] allow users to create windows larger than monitor on windows & macos --- crates/eframe/src/native/epi_integration.rs | 22 +++++++++++++----- crates/egui-winit/src/lib.rs | 1 + crates/egui/src/viewport.rs | 25 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index fbf7b6dc078..9ce0ea9d0c2 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -20,14 +20,22 @@ pub fn viewport_builder( let mut viewport_builder = native_options.viewport.clone(); + // Allow users to create windows larger than monitor on windows & macos + let with_constraint: bool = !cfg!(any(target_os = "windows", target_os = "macos")) + || viewport_builder.clamp_size_to_monitor_size.unwrap_or(true); + // Always use the default window size / position on iOS. Trying to restore the previous position // causes the window to be shown too small. #[cfg(not(target_os = "ios"))] let inner_size_points = if let Some(mut window_settings) = window_settings { // Restore pos/size from previous session - window_settings - .clamp_size_to_sane_values(largest_monitor_point_size(egui_zoom_factor, event_loop)); + if with_constraint { + window_settings.clamp_size_to_sane_values(largest_monitor_point_size( + egui_zoom_factor, + event_loop, + )); + } window_settings.clamp_position_to_monitors(egui_zoom_factor, event_loop); viewport_builder = window_settings.initialize_viewport_builder(viewport_builder); @@ -37,10 +45,12 @@ pub fn viewport_builder( viewport_builder = viewport_builder.with_position(pos); } - if let Some(initial_window_size) = viewport_builder.inner_size { - let initial_window_size = initial_window_size - .at_most(largest_monitor_point_size(egui_zoom_factor, event_loop)); - viewport_builder = viewport_builder.with_inner_size(initial_window_size); + if with_constraint { + if let Some(initial_window_size) = viewport_builder.inner_size { + let initial_window_size = initial_window_size + .at_most(largest_monitor_point_size(egui_zoom_factor, event_loop)); + viewport_builder = viewport_builder.with_inner_size(initial_window_size); + } } viewport_builder.inner_size diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 5abfa0f6898..94db145dbb2 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -1563,6 +1563,7 @@ pub fn create_winit_window_builder( window_type: _window_type, mouse_passthrough: _, // handled in `apply_viewport_builder_to_window` + .. } = viewport_builder; let mut window_builder = winit::window::WindowBuilder::new() diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index aed8d35165e..6de08d248a8 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -275,6 +275,9 @@ pub struct ViewportBuilder { pub min_inner_size: Option, pub max_inner_size: Option, + /// Whether clamp the window's size to monitor's size. Default to `true` + pub clamp_size_to_monitor_size: Option, + pub fullscreen: Option, pub maximized: Option, pub resizable: Option, @@ -493,6 +496,17 @@ impl ViewportBuilder { self } + /// Sets whether clamp the window's size to monitor's size. + /// + /// This only works on windows & macos, otherwise will be dismissed. + /// + /// Default to `true` + #[inline] + pub fn with_clamp_size_to_monitor_size(mut self, value: bool) -> Self { + self.clamp_size_to_monitor_size = Some(value); + self + } + /// Does not work on X11. #[inline] pub fn with_close_button(mut self, value: bool) -> Self { @@ -606,6 +620,7 @@ impl ViewportBuilder { inner_size: new_inner_size, min_inner_size: new_min_inner_size, max_inner_size: new_max_inner_size, + clamp_size_to_monitor_size: new_clamp_size_to_monitor_size, fullscreen: new_fullscreen, maximized: new_maximized, resizable: new_resizable, @@ -740,6 +755,16 @@ impl ViewportBuilder { let mut recreate_window = false; + // TODO(lopo12123): Rebuild the window directly? + // Or should we get the size of each monitor somewhere + // and then compare and adjust the window size(if need) + if new_clamp_size_to_monitor_size.is_some() + && self.clamp_size_to_monitor_size != new_clamp_size_to_monitor_size + { + self.clamp_size_to_monitor_size = new_clamp_size_to_monitor_size; + recreate_window = true; + } + if new_active.is_some() && self.active != new_active { self.active = new_active; recreate_window = true; From de71328724e94ccc44351fc802869b087640e336 Mon Sep 17 00:00:00 2001 From: lopo Date: Mon, 8 Apr 2024 12:04:13 +0800 Subject: [PATCH 2/9] chore: remove todo (select the "recreate" one, because the initial size of the window may be used in the application to make certain judgments.) --- crates/egui/src/viewport.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index 6de08d248a8..e08559b6ad7 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -755,9 +755,6 @@ impl ViewportBuilder { let mut recreate_window = false; - // TODO(lopo12123): Rebuild the window directly? - // Or should we get the size of each monitor somewhere - // and then compare and adjust the window size(if need) if new_clamp_size_to_monitor_size.is_some() && self.clamp_size_to_monitor_size != new_clamp_size_to_monitor_size { From 2bc0b6e558d2c116f5982cd4bc36bf3f87e6a942 Mon Sep 17 00:00:00 2001 From: lopo <59609929+lopo12123@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:00:02 +0800 Subject: [PATCH 3/9] Update crates/egui-winit/src/lib.rs Co-authored-by: Emil Ernerfeldt --- crates/egui-winit/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 94db145dbb2..24390de0c14 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -1563,7 +1563,7 @@ pub fn create_winit_window_builder( window_type: _window_type, mouse_passthrough: _, // handled in `apply_viewport_builder_to_window` - .. + clamp_size_to_monitor_size: _, // Handled in `viewport_builder` in `epi_integration.rs` } = viewport_builder; let mut window_builder = winit::window::WindowBuilder::new() From dfd6ec52b8eb6ef8a662c2f9a19bef3679c01df3 Mon Sep 17 00:00:00 2001 From: lopo <59609929+lopo12123@users.noreply.github.com> Date: Tue, 9 Apr 2024 21:51:34 +0800 Subject: [PATCH 4/9] docs: add note about 'clamp_size_to_monitor_size' --- crates/egui/src/viewport.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index e08559b6ad7..c42feba15bc 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -275,7 +275,9 @@ pub struct ViewportBuilder { pub min_inner_size: Option, pub max_inner_size: Option, - /// Whether clamp the window's size to monitor's size. Default to `true` + /// Whether clamp the window's size to monitor's size. Default to `true`. + /// + /// Note: On some Linux systems, a window size larger than the monitor causes crashes pub clamp_size_to_monitor_size: Option, pub fullscreen: Option, @@ -496,11 +498,9 @@ impl ViewportBuilder { self } - /// Sets whether clamp the window's size to monitor's size. - /// - /// This only works on windows & macos, otherwise will be dismissed. + /// Sets whether clamp the window's size to monitor's size. Default to `true`. /// - /// Default to `true` + /// Note: On some Linux systems, a window size larger than the monitor causes crashes #[inline] pub fn with_clamp_size_to_monitor_size(mut self, value: bool) -> Self { self.clamp_size_to_monitor_size = Some(value); From c58a56f63b1125f775d63af4723d9f339a57d2b5 Mon Sep 17 00:00:00 2001 From: lopo <59609929+lopo12123@users.noreply.github.com> Date: Tue, 9 Apr 2024 21:52:19 +0800 Subject: [PATCH 5/9] Update crates/eframe/src/native/epi_integration.rs Co-authored-by: Emil Ernerfeldt --- crates/eframe/src/native/epi_integration.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 9ce0ea9d0c2..717dd03a323 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -20,9 +20,9 @@ pub fn viewport_builder( let mut viewport_builder = native_options.viewport.clone(); - // Allow users to create windows larger than monitor on windows & macos - let with_constraint: bool = !cfg!(any(target_os = "windows", target_os = "macos")) - || viewport_builder.clamp_size_to_monitor_size.unwrap_or(true); + let clamp_size_to_monitor_size = viewport_builder.clamp_size_to_monitor_size.unwrap_or( + // On some Linux systems, a window size larger than the monitor causes crashes + cfg!(target_os = "linux")); // Always use the default window size / position on iOS. Trying to restore the previous position // causes the window to be shown too small. From 6cd14a9bc94a9ca3e875b64e8b3f1291eb40073b Mon Sep 17 00:00:00 2001 From: lopo <59609929+lopo12123@users.noreply.github.com> Date: Sun, 21 Apr 2024 20:36:02 +0800 Subject: [PATCH 6/9] docs: update note of 'clamp_size_to_monitor_size' --- crates/egui/src/viewport.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index c42feba15bc..dd45b84da37 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -275,7 +275,7 @@ pub struct ViewportBuilder { pub min_inner_size: Option, pub max_inner_size: Option, - /// Whether clamp the window's size to monitor's size. Default to `true`. + /// Whether clamp the window's size to monitor's size. The default is `true` on linux, otherwise it is `false`. /// /// Note: On some Linux systems, a window size larger than the monitor causes crashes pub clamp_size_to_monitor_size: Option, @@ -498,7 +498,7 @@ impl ViewportBuilder { self } - /// Sets whether clamp the window's size to monitor's size. Default to `true`. + /// Sets whether clamp the window's size to monitor's size. The default is `true` on linux, otherwise it is `false`. /// /// Note: On some Linux systems, a window size larger than the monitor causes crashes #[inline] From 3d65cfe2a247f5e3e11f50e74d88fe2fa7931d1b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 21 Apr 2024 19:01:09 +0200 Subject: [PATCH 7/9] Fix compilation 1/2 --- crates/eframe/src/native/epi_integration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 717dd03a323..2fe7efbb378 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -30,7 +30,7 @@ pub fn viewport_builder( let inner_size_points = if let Some(mut window_settings) = window_settings { // Restore pos/size from previous session - if with_constraint { + if clamp_size_to_monitor_size { window_settings.clamp_size_to_sane_values(largest_monitor_point_size( egui_zoom_factor, event_loop, From 6735e22051b7044fdab3358bb4e6f533109d4a1e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 21 Apr 2024 19:01:15 +0200 Subject: [PATCH 8/9] Fix compilation 2/2 --- crates/eframe/src/native/epi_integration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 2fe7efbb378..9015c9c999b 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -45,7 +45,7 @@ pub fn viewport_builder( viewport_builder = viewport_builder.with_position(pos); } - if with_constraint { + if clamp_size_to_monitor_size { if let Some(initial_window_size) = viewport_builder.inner_size { let initial_window_size = initial_window_size .at_most(largest_monitor_point_size(egui_zoom_factor, event_loop)); From 05d77b19c546f12a639071260fa4713f57690682 Mon Sep 17 00:00:00 2001 From: lopo Date: Mon, 22 Apr 2024 10:26:17 +0800 Subject: [PATCH 9/9] chore: fmt --- crates/eframe/src/native/epi_integration.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 9015c9c999b..b09f0f0e0c7 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -22,7 +22,8 @@ pub fn viewport_builder( let clamp_size_to_monitor_size = viewport_builder.clamp_size_to_monitor_size.unwrap_or( // On some Linux systems, a window size larger than the monitor causes crashes - cfg!(target_os = "linux")); + cfg!(target_os = "linux"), + ); // Always use the default window size / position on iOS. Trying to restore the previous position // causes the window to be shown too small.