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

Position persistence and sane clamping to still-available monitors for Windows #2583

Merged
merged 20 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub fn window_builder<E>(
if let Some(mut window_settings) = window_settings {
// Restore pos/size from previous session
window_settings.clamp_to_sane_values(largest_monitor_point_size(event_loop));
#[cfg(windows)]
window_settings.clamp_window_to_sane_position(&event_loop);
window_builder = window_settings.initialize_window(window_builder);
} else {
if let Some(pos) = *initial_window_pos {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui-winit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ All notable changes to the `egui-winit` integration will be noted in this file.


## Unreleased

Fixed window position persistence for Windows ([#1469](https://github.com/emilk/egui/issues/1469)

## 0.20.1 - 2022-12-11
* Fix docs.rs build ([#2420](https://github.com/emilk/egui/pull/2420)).
Expand Down
71 changes: 61 additions & 10 deletions crates/egui-winit/src/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ impl WindowSettings {
// If the app last ran on two monitors and only one is now connected, then
// the given position is invalid.
// If this happens on Mac, the window is clamped into valid area.
// If this happens on Windows, the window is hidden and very difficult to find.
// So we don't restore window positions on Windows.
let try_restore_position = !cfg!(target_os = "windows");
if try_restore_position {
if let Some(pos) = self.position {
window = window.with_position(winit::dpi::PhysicalPosition {
x: pos.x as f64,
y: pos.y as f64,
});
}
// If this happens on Windows, the clamping behavior is managed by the function
// clamp_window_to_sane_position.
if let Some(pos) = self.position {
window = window.with_position(winit::dpi::PhysicalPosition {
x: pos.x as f64,
y: pos.y as f64,
});
}

if let Some(inner_size_points) = self.inner_size_points {
Expand Down Expand Up @@ -86,4 +83,58 @@ impl WindowSettings {
*size = size.at_most(max_size);
}
}

pub fn clamp_window_to_sane_position<E>(
&mut self,
event_loop: &winit::event_loop::EventLoopWindowTarget<E>,
) {
use std::ops::{Add, AddAssign, MulAssign, Sub};

if let (Some(position), Some(inner_size)) = (&mut self.position, &self.inner_size_points) {
Shel-M marked this conversation as resolved.
Show resolved Hide resolved
let monitors = event_loop.available_monitors();
// default to primary monitor, in case the correct monitor was disconnected.
let mut monitor: winit::monitor::MonitorHandle = match event_loop.primary_monitor() {
Some(m) => m,
// No primary monitor, grab index 0 instead
_ => {
match event_loop.available_monitors().nth(0) {
Some(m) => m,
None => return, // No monitors?
}
}
};
Shel-M marked this conversation as resolved.
Show resolved Hide resolved
for _monitor in monitors {
Shel-M marked this conversation as resolved.
Show resolved Hide resolved
let monitor_x_range =
_monitor.position().x..(_monitor.position().x + _monitor.size().width as i32);
let monitor_y_range =
_monitor.position().y..(_monitor.position().y + _monitor.size().height as i32);

if monitor_x_range.contains(&(position.x as i32))
&& monitor_y_range.contains(&(position.y as i32))
{
monitor = _monitor
}
}

let mut inner_size = inner_size.clone(); // Shadow inner_size, we don't want to modify the original
inner_size.mul_assign(monitor.scale_factor() as f32);
Shel-M marked this conversation as resolved.
Show resolved Hide resolved
// Add size of title bar. This is 32 px by default in Win 10/11.
#[cfg(windows)]
inner_size.add_assign(egui::Vec2::new(0.0, 32.0 * monitor.scale_factor() as f32));
Shel-M marked this conversation as resolved.
Show resolved Hide resolved
let monitor_position =
egui::Pos2::new(monitor.position().x as f32, monitor.position().y as f32);
let monitor_logical_size = monitor.size();
*position = position.clamp(
monitor_position,
// To get the maximum position, we get the rightmost corner of the display, then subtract
// the size of the window to get the bottom right most value window.position can have.
monitor_position
.add(egui::Vec2::new(
monitor_logical_size.width as f32,
monitor_logical_size.height as f32,
))
.sub(inner_size),
Shel-M marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
}