diff --git a/apps/pdfcraft/src/main.rs b/apps/pdfcraft/src/main.rs index 1f5e859ba4..cd68d32977 100644 --- a/apps/pdfcraft/src/main.rs +++ b/apps/pdfcraft/src/main.rs @@ -52,6 +52,8 @@ mod reopen; mod single_instance; mod startup; #[cfg(target_os = "windows")] +mod window_frame; +#[cfg(target_os = "windows")] mod window_placement; #[cfg(test)] #[path = "windows_manifest.rs"] @@ -640,6 +642,9 @@ fn app_creator<'a>( #[cfg(target_os = "macos")] apple_events: &'a apple_events::AppleEvents, ) -> eframe::AppCreator<'a> { let Launch { startup_time, files, options, control, create_images, combine, frame, lang, two_paged, nightly_watch } = launch; + // Whether the app's own bar is the title bar, so the window is undecorated. + #[cfg(target_os = "windows")] + let integrated = !matches!(frame.frame, craft::window::policy::Frame::Native); Box::new(move |cc| { started.set(true); startup_stage(startup_time, "renderer ready, installing fonts and runtime"); @@ -688,6 +693,11 @@ fn app_creator<'a>( window_placement::save(&window, storage); } })); + // The undecorated window must stay borderless, or the caption and border the toolkit gives it + // offset the drawn content from the pointer on some systems (#8845, #8793). + if integrated { + app.window_frame = window_frame::guard(window); + } set_taskbar_icon(window); } app.set_startup_update_check(!is_nightly()); diff --git a/apps/pdfcraft/src/window_frame.rs b/apps/pdfcraft/src/window_frame.rs new file mode 100644 index 0000000000..44d68c9e22 --- /dev/null +++ b/apps/pdfcraft/src/window_frame.rs @@ -0,0 +1,86 @@ +//! Keeps the custom (integrated) title bar's undecorated window truly borderless on Windows +//! (#8845, #8793, #9040). +//! +//! winit 0.30 gives an undecorated window the `WS_CAPTION | WS_BORDER` styles and hides the +//! non-client area by spreading the client rect over it (`WM_NCCALCSIZE`). On some systems the +//! desktop compositor still reserves that frame for the window surface: the content is drawn +//! offset by the caption and border, leaving a black bar at the top, while the pointer keeps the +//! un-offset client coordinates, so a click lands above the control that is drawn under it. +//! +//! PhotoCraft (storytold/photocraft#2401, #2246), VectorCraft, FigCraft and DesignCraft fixed the +//! same symptom the same way: strip `WS_CAPTION`/`WS_BORDER` and have Windows recalculate the +//! frame. winit puts the styles back whenever it changes the window flags (maximize, restore, +//! ...), so the guard runs every frame; once they are gone it only reads the style. +//! +//! The Windows calls go through safe `winsafe` wrappers; the workspace forbids `unsafe`, and +//! winsafe cannot wrap a raw handle without it, so the window is found among the UI thread's +//! windows by its handle, as `window_placement` does. + +use std::sync::Weak; + +use winit::raw_window_handle::{HasWindowHandle as _, RawWindowHandle}; +use winsafe::{HWND, HwndPlace, POINT, SIZE, co}; + +/// A guard that keeps `window` borderless; call it once per frame on the UI thread. `None` when +/// the window's handle is not a Win32 one. +pub(super) fn guard(window: &std::sync::Arc) -> Option> { + let RawWindowHandle::Win32(raw) = window.window_handle().ok()?.as_raw() else { return None }; + let target = raw.hwnd.get(); + let root: Weak = std::sync::Arc::downgrade(window); + let guard = move || { + // The window is gone once the app is closing. + if root.upgrade().is_none() { + return; + } + let _ = winsafe::EnumThreadWindows(winsafe::GetCurrentThreadId(), |hwnd| { + if hwnd.ptr() as isize == target { + strip(&hwnd); + } + true + }); + }; + guard(); + Some(Box::new(guard)) +} + +/// Removes `WS_CAPTION`/`WS_BORDER` and has Windows recalculate the frame. A no-op once they are +/// gone, so it is cheap to call every frame. +fn strip(hwnd: &HWND) { + let style = hwnd.style(); + let without = borderless(style); + if without.raw() == style.raw() { + return; + } + hwnd.set_style(without); + let _ = hwnd.SetWindowPos( + HwndPlace::None, + POINT::new(), + SIZE::new(), + co::SWP::FRAMECHANGED | co::SWP::NOMOVE | co::SWP::NOSIZE | co::SWP::NOZORDER | co::SWP::NOACTIVATE, + ); +} + +/// `style` without the OS title bar and frame (`WS_CAPTION`, which already includes `WS_BORDER`). +/// The sizing border (`WS_THICKFRAME`) stays, so edge resizing and Aero snap keep working. +fn borderless(style: co::WS) -> co::WS { + style & !(co::WS::CAPTION | co::WS::BORDER) +} + +#[cfg(test)] +mod tests { + use super::borderless; + use winsafe::co; + + #[test] + fn the_borderless_style_drops_the_caption_and_border_only() { + // `WS_CAPTION` is 0x00c0_0000 and already contains `WS_BORDER` (0x0080_0000). + let decorated = co::WS::CAPTION | co::WS::BORDER | co::WS::SYSMENU | co::WS::THICKFRAME | co::WS::MAXIMIZEBOX | co::WS::MINIMIZEBOX; + let bare = borderless(decorated); + assert!(!bare.has(co::WS::CAPTION), "caption gone"); + assert!(!bare.has(co::WS::BORDER), "border gone"); + assert!(bare.has(co::WS::THICKFRAME), "edge resizing kept"); + assert!(bare.has(co::WS::MAXIMIZEBOX) && bare.has(co::WS::SYSMENU), "window buttons kept"); + // Already borderless: unchanged, so the per-frame call does nothing. + assert_eq!(bare.raw(), borderless(bare).raw()); + } +} diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 4c1808d8cb..b119e391d7 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -377,6 +377,8 @@ impl PdfCraftApp { at_exit: None, #[cfg(target_os = "windows")] save_window_placement: None, + #[cfg(target_os = "windows")] + window_frame: None, startup_time: None, show_secured_notice: true, text_links: true, diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index a88a10c2a1..6d43665c62 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -9,6 +9,12 @@ impl PdfCraftApp { /// command states and host. eframe calls the hook once per frame, before the first pass, so /// a second pass of the same frame runs on what this call set and fires nothing twice. pub(super) fn prepare_raw_input(&mut self, ctx: &egui::Context, _raw: &mut egui::RawInput) { + // The toolkit gives the undecorated window its caption back whenever it changes the window's flags (maximize, + // restore, ...): take it off again before the frame is laid out. + #[cfg(target_os = "windows")] + if let Some(window_frame) = self.window_frame.as_mut() { + window_frame(); + } // Preferences and the control channel only change `title_bar_choice`; the window follows on the // next frame. Asking the policy again when nothing changed would re-read the platform each frame. if self.applied_title_bar != Some(self.title_bar_choice) { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 9819c53597..f7eb738d3e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -464,6 +464,10 @@ pub struct PdfCraftApp { /// Preserve the normal Win32 placement if eframe saved a minimized window. #[cfg(target_os = "windows")] pub save_window_placement: Option>, + /// Keeps the undecorated window's OS frame from offsetting its content and pointer (Windows, + /// integrated title bar). Called once per frame, before the frame's input is read. + #[cfg(target_os = "windows")] + pub window_frame: Option>, /// Native launch time, consumed after the first UI pass for startup diagnostics. pub startup_time: Option, /// Preferences: tell the user, above the pages, that a document is secured (its security