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

Allow users to create viewports larger than monitor on Windows & macOS #4337

Merged
merged 9 commits into from
Apr 22, 2024
22 changes: 16 additions & 6 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ pub fn viewport_builder<E>(

let mut viewport_builder = native_options.viewport.clone();

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.
#[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 {
emilk marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand All @@ -37,10 +45,12 @@ pub fn viewport_builder<E>(
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 {
emilk marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
1 change: 1 addition & 0 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ pub fn create_winit_window_builder<T>(
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()
Expand Down
22 changes: 22 additions & 0 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ pub struct ViewportBuilder {
pub min_inner_size: Option<Vec2>,
pub max_inner_size: Option<Vec2>,

/// 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<bool>,

pub fullscreen: Option<bool>,
pub maximized: Option<bool>,
pub resizable: Option<bool>,
Expand Down Expand Up @@ -493,6 +498,15 @@ impl ViewportBuilder {
self
}

/// 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]
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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -740,6 +755,13 @@ impl ViewportBuilder {

let mut recreate_window = false;

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;
Expand Down
Loading