SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Windows: a restored window that is out of reach is fitted to the monitor's work area (#4299)
bherbrucke570c0b4eb6f
diff --git a/apps/solidcraft/Cargo.toml b/apps/solidcraft/Cargo.tomlindex 859dba6c39..0fcde99f5c 100644--- a/apps/solidcraft/Cargo.toml+++ b/apps/solidcraft/Cargo.toml@@ -52,6 +52,7 @@ raw-window-handle = "0.6"# The window's resize border and DPI size (src/native_frame.rs): the window procedure, hit test, DPI.windows-sys = { version = "0.61", features = ["Win32_Foundation",+ "Win32_Graphics_Gdi","Win32_Security","Win32_Storage_FileSystem","Win32_System_Diagnostics_Debug",diff --git a/apps/solidcraft/src/main.rs b/apps/solidcraft/src/main.rsindex 4c5df386f9..5097ebba5f 100644--- a/apps/solidcraft/src/main.rs+++ b/apps/solidcraft/src/main.rs@@ -28,10 +28,37 @@ mod open_documents;#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]mod space_mouse;mod startup;+#[cfg(any(target_os = "windows", test))]+mod window_fit;use solidcraft_engine::{Session, io};use solidcraft_ui_egui::{Services, SolveApp};+/// Windows: put the restored window back inside the monitor's work area if it is out of reach+/// (a gone monitor, another scale, a taskbar edge). Sizes are in points, the work area in pixels.+#[cfg(target_os = "windows")]+fn fit_window_to_work_area(ctx: &egui::Context, frame: &eframe::Frame) {+ use window_fit::Area;+ let Some((x, y, w, h)) = native_frame::work_area(frame) else { return };+ let (outer, inner, scale, maximized) = ctx.input(|i| {+ let v = i.viewport();+ (v.outer_rect, v.inner_rect, v.native_pixels_per_point, v.maximized.unwrap_or(false) || v.fullscreen.unwrap_or(false))+ });+ let (Some(outer), Some(inner), Some(scale)) = (outer, inner, scale.filter(|s| s.is_finite() && *s > 0.0)) else { return };+ if maximized {+ return;+ }+ let work = Area::new(x as f32 / scale, y as f32 / scale, w as f32 / scale, h as f32 / scale);+ let now = Area::new(outer.min.x, outer.min.y, outer.width(), outer.height());+ let fitted = window_fit::fit(now, work, (960.0, 600.0));+ if fitted != now {+ // The frame (title bar, borders) keeps its size; only the inner size follows.+ let inner_size = egui::vec2((inner.width() - (now.w - fitted.w)).max(1.0), (inner.height() - (now.h - fitted.h)).max(1.0));+ ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(inner_size));+ ctx.send_viewport_cmd(egui::ViewportCommand::OuterPosition(egui::pos2(fitted.x, fitted.y)));+ }+}+/// `SOLIDCRAFT_PROFILE_STARTUP=1` prints the time since launch at each startup step.fn mark(step: &str) {static START: std::sync::OnceLock<(std::time::Instant, bool)> = std::sync::OnceLock::new();@@ -57,6 +84,11 @@ impl eframe::App for App {fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {if self.frames < 2 {self.frames += 1;+ // Before the window is shown: its title bar must be within reach (window_fit.rs).+ #[cfg(target_os = "windows")]+ if self.frames == 1 {+ fit_window_to_work_area(ctx, _frame);+ }mark(if self.frames == 1 { "first frame begins" } else { "second frame begins (first presented)" });// A second frame right away, which means the first was presented: the app is up.ctx.request_repaint();diff --git a/apps/solidcraft/src/native_frame.rs b/apps/solidcraft/src/native_frame.rsindex d6fb511714..a43c1c37e5 100644--- a/apps/solidcraft/src/native_frame.rs+++ b/apps/solidcraft/src/native_frame.rs@@ -26,6 +26,7 @@ use std::sync::atomic::{AtomicIsize, Ordering};use raw_window_handle::{HasWindowHandle, RawWindowHandle};use solidcraft_ui_egui::titlebar;use windows_sys::Win32::Foundation::{HWND, LPARAM, LRESULT, RECT, SIZE, WPARAM};+use windows_sys::Win32::Graphics::Gdi::{GetMonitorInfoW, MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromWindow};use windows_sys::Win32::UI::HiDpi::{GetDpiForWindow, GetSystemMetricsForDpi};use windows_sys::Win32::UI::WindowsAndMessaging::{CallWindowProcW, GWL_STYLE, GWLP_WNDPROC, GetClientRect, GetWindowLongPtrW, GetWindowRect, HTBOTTOM, HTBOTTOMLEFT, HTBOTTOMRIGHT, HTLEFT,@@ -33,6 +34,26 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{WM_NCDESTROY, WM_NCHITTEST, WNDPROC, WS_THICKFRAME,};+/// The work area (the monitor without the taskbar) of the monitor nearest the window, in physical+/// pixels: left, top, width, height.+pub fn work_area(window: &impl HasWindowHandle) -> Option<(i32, i32, i32, i32)> {+ let handle = window.window_handle().ok()?;+ let RawWindowHandle::Win32(w) = handle.as_raw() else { return None };+ let hwnd = w.hwnd.get() as HWND;+ // SAFETY: `hwnd` is the live main window; `info` is a MONITORINFO with its size set, as+ // GetMonitorInfoW requires, and the monitor handle comes from the call before it.+ let rc = unsafe {+ let monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);+ let mut info: MONITORINFO = std::mem::zeroed();+ info.cbSize = std::mem::size_of::<MONITORINFO>() as u32;+ if monitor.is_null() || GetMonitorInfoW(monitor, &mut info) == 0 {+ return None;+ }+ info.rcWork+ };+ Some((rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top))+}+/// The window procedure ours replaced (winit's), as an address; 0 before [`install`].static PREVIOUS: AtomicIsize = AtomicIsize::new(0);diff --git a/apps/solidcraft/src/window_fit.rs b/apps/solidcraft/src/window_fit.rsnew file mode 100644index 0000000000..ba9db87b43--- /dev/null+++ b/apps/solidcraft/src/window_fit.rs@@ -0,0 +1,117 @@+//! Keeping the restored window reachable.+//!+//! The window comes back where it was left. After a monitor was unplugged, the scale changed or+//! the settings came from a larger screen, that can put its title bar above the top edge or+//! under the taskbar, out of reach of the mouse. [`fit`] pulls a window back into the monitor's+//! work area (the screen without the taskbar), in whatever unit the caller measures both, as+//! long as it is the same one.++/// A rectangle: left, top, width, height.+#[derive(Clone, Copy, Debug, PartialEq)]+pub struct Area {+ pub x: f32,+ pub y: f32,+ pub w: f32,+ pub h: f32,+}++impl Area {+ pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self {+ Area { x, y, w, h }+ }+ fn right(self) -> f32 {+ self.x + self.w+ }+ fn bottom(self) -> f32 {+ self.y + self.h+ }+ fn sane(self) -> bool {+ [self.x, self.y, self.w, self.h].iter().all(|v| v.is_finite()) && self.w > 0.0 && self.h > 0.0+ }+}++/// How much of the title bar must stay over the work area at least, to grab it.+pub const REACHABLE: f32 = 100.0;+/// The size a window gets when its saved geometry is unusable, before it is fitted to the screen.+pub const DEFAULT_SIZE: (f32, f32) = (1600.0, 1000.0);++/// `window` moved and shrunk so that it can be reached on `work`: no larger than the work area+/// (but not below `min`), the title bar not above the work area's top, the window not below its+/// bottom, and at least [`REACHABLE`] of it over the work area horizontally. A window that is+/// fine is returned unchanged. Geometry that makes no sense (not finite, empty), or a work+/// area that does not, gives the default size centred on the work area.+pub fn fit(window: Area, work: Area, min: (f32, f32)) -> Area {+ if !work.sane() {+ return window;+ }+ let min = (min.0.min(work.w), min.1.min(work.h));+ if !window.sane() {+ let w = DEFAULT_SIZE.0.min(work.w * 0.9).max(min.0);+ let h = DEFAULT_SIZE.1.min(work.h * 0.9).max(min.1);+ return Area::new(work.x + (work.w - w) / 2.0, work.y + (work.h - h) / 2.0, w, h);+ }+ let w = window.w.clamp(min.0, work.w);+ let h = window.h.clamp(min.1, work.h);+ let reach = REACHABLE.min(w);+ // A window that had to shrink goes fully onto the screen; one that fits may keep straddling two.+ let x = if w < window.w { window.x.max(work.x).min(work.right() - w) } else { window.x.max(work.x + reach - w).min(work.right() - reach) };+ let y = window.y.min(work.bottom() - h).max(work.y);+ Area::new(x, y, w, h)+}++#[cfg(test)]+mod tests {+ use super::*;++ const WORK: Area = Area::new(0.0, 0.0, 2560.0, 1400.0);+ const MIN: (f32, f32) = (960.0, 600.0);++ #[test]+ fn a_window_on_screen_is_left_alone() {+ let w = Area::new(300.0, 120.0, 1600.0, 1000.0);+ assert_eq!(fit(w, WORK, MIN), w);+ }++ #[test]+ fn a_title_bar_above_the_top_or_under_the_taskbar_comes_back() {+ let fitted = fit(Area::new(300.0, -400.0, 1600.0, 1000.0), WORK, MIN);+ assert_eq!((fitted.y, fitted.h), (0.0, 1000.0));+ let fitted = fit(Area::new(300.0, 1300.0, 1600.0, 1000.0), WORK, MIN);+ assert_eq!(fitted.y + fitted.h, 1400.0);+ // A taskbar at the top moves the work area down.+ let below_bar = Area::new(0.0, 48.0, 2560.0, 1352.0);+ assert_eq!(fit(Area::new(300.0, 0.0, 1600.0, 1000.0), below_bar, MIN).y, 48.0);+ }++ #[test]+ fn a_window_from_a_monitor_that_is_gone_keeps_a_grabbable_part_on_screen() {+ let right = fit(Area::new(5000.0, 100.0, 1600.0, 1000.0), WORK, MIN);+ assert_eq!(right.x, 2560.0 - REACHABLE);+ let left = fit(Area::new(-4000.0, 100.0, 1600.0, 1000.0), WORK, MIN);+ assert_eq!(left.x + left.w, REACHABLE);+ }++ #[test]+ fn a_window_larger_than_the_screen_shrinks_to_it() {+ let small = Area::new(0.0, 0.0, 1280.0, 720.0);+ let fitted = fit(Area::new(-20.0, -30.0, 3200.0, 2000.0), small, MIN);+ assert_eq!(fitted, small);+ // Never below the minimum, unless the screen itself is smaller.+ assert_eq!(fit(Area::new(0.0, 0.0, 100.0, 100.0), WORK, MIN).w, 960.0);+ assert_eq!(fit(Area::new(0.0, 0.0, 100.0, 100.0), Area::new(0.0, 0.0, 800.0, 500.0), MIN).h, 500.0);+ }++ #[test]+ fn unusable_geometry_is_centred_at_a_sensible_size() {+ for bad in [Area::new(f32::NAN, 0.0, 1600.0, 1000.0), Area::new(0.0, 0.0, 0.0, 0.0), Area::new(0.0, 0.0, f32::INFINITY, 10.0)] {+ let fitted = fit(bad, WORK, MIN);+ assert_eq!(fitted, Area::new(480.0, 200.0, 1600.0, 1000.0));+ }+ let small = Area::new(100.0, 50.0, 1366.0, 728.0);+ let fitted = fit(Area::new(f32::NAN, 0.0, 1.0, 1.0), small, MIN);+ assert!(fitted.w <= small.w && fitted.h <= small.h && fitted.x >= small.x && fitted.y >= small.y);+ // No work area to go by: nothing to change.+ let w = Area::new(1.0, 2.0, 3.0, 4.0);+ assert_eq!(fit(w, Area::new(0.0, 0.0, 0.0, 0.0), MIN), w);+ }+}