diff --git a/apps/photocraft/src/cursor.rs b/apps/photocraft/src/cursor.rs index 72548ca638..55efd13005 100644 --- a/apps/photocraft/src/cursor.rs +++ b/apps/photocraft/src/cursor.rs @@ -13,10 +13,15 @@ //! [`service`] builds the pointer-position service for the app's window, `None` where it can't //! be read. -pub use platform::service; +pub use platform::{primary_down_service, service}; #[cfg(target_os = "macos")] mod platform { + /// Not read here: the title-bar move of a floating document window reaches the app as a press and a release. + pub fn primary_down_service() -> Option { + None + } + use objc2::MainThreadMarker; use objc2_app_kit::{NSApplication, NSEvent, NSWindow}; use photocraft_ui_egui::CursorPosFn; @@ -45,6 +50,11 @@ mod platform { #[cfg(target_os = "linux")] mod platform { + /// Not read here: the title-bar move of a floating document window reaches the app as a press and a release. + pub fn primary_down_service() -> Option { + None + } + use eframe::wgpu::rwh::{HasWindowHandle, RawWindowHandle}; use photocraft_ui_egui::CursorPosFn; use x11rb::protocol::xproto::ConnectionExt; @@ -81,7 +91,13 @@ mod platform { #[cfg(target_os = "windows")] mod platform { - use photocraft_ui_egui::CursorPosFn; + use photocraft_ui_egui::{CursorPosFn, PrimaryDownFn}; + + /// The physical left button (`GetAsyncKeyState`): a floating document window's title-bar move runs in the OS, which + /// tells the app nothing when it ends (#16415). + pub fn primary_down_service() -> Option { + Some(Box::new(|| winsafe::GetAsyncKeyState(winsafe::co::VK::LBUTTON))) + } pub fn service(_cc: &eframe::CreationContext<'_>) -> Option { Some(Box::new(|ctx: &egui::Context| { @@ -104,6 +120,11 @@ fn client_points(screen_px: egui::Pos2, inner_min: egui::Pos2, ppp: f32) -> egui #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))] mod platform { + /// Not read here: the title-bar move of a floating document window reaches the app as a press and a release. + pub fn primary_down_service() -> Option { + None + } + use photocraft_ui_egui::CursorPosFn; pub fn service(_cc: &eframe::CreationContext<'_>) -> Option { diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs index 38ff2c94ad..bb2cc284c4 100644 --- a/apps/photocraft/src/main.rs +++ b/apps/photocraft/src/main.rs @@ -713,6 +713,8 @@ fn main() -> eframe::Result { } // Where file drags and drops are (winit 0.30 doesn't say). app.services.cursor_pos = cursor::service(cc); + // The physical primary button: it ends a floating document window's OS move by its title bar (#16415). + app.services.primary_down = cursor::primary_down_service(); // The custom title bar's undecorated window must stay borderless, or its caption and // border offset the content and the pointer on some Windows systems (#2246). app.services.window_frame = window_frame::service(cc, integrated_frame); diff --git a/crates/ui-egui/src/canvas/document_area.rs b/crates/ui-egui/src/canvas/document_area.rs index e239c0b527..35cbe74429 100644 --- a/crates/ui-egui/src/canvas/document_area.rs +++ b/crates/ui-egui/src/canvas/document_area.rs @@ -130,11 +130,17 @@ pub(crate) fn apply_arranged(app: &mut PhotocraftApp, arranged: Arranged) { } } +/// The height of the band at the top of the document area that takes a floating window's drop where no tab strip +/// shows: a tab's height. +const DOCK_BAND: f32 = 32.0; + fn documents(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // The web build's drop path reads where the canvas is (`file_open::target_at`); set again when one shows. app.drop_canvas_rect = None; - // With no docked document the whole area takes a floating window's drop; the tab strip narrows it when it shows. - app.dock_drop_rect = Some(ui.max_rect()); + // A floating window's title bar docks on the tab strip (`tabs::strip` sets its rect); where no strip shows, on a + // band of a tab's height at the top of the area, where the strip would be. + let area = ui.max_rect(); + app.dock_drop_rect = Some(Rect::from_min_size(area.min, egui::vec2(area.width(), DOCK_BAND.min(area.height())))); retain_gpu_documents(app); publish_shown(app); crate::cas_transform::tick(app, ui.ctx()); diff --git a/crates/ui-egui/src/canvas/document_window_title_tests.rs b/crates/ui-egui/src/canvas/document_window_title_tests.rs index 4ec7978b70..82c880989d 100644 --- a/crates/ui-egui/src/canvas/document_window_title_tests.rs +++ b/crates/ui-egui/src/canvas/document_window_title_tests.rs @@ -2,7 +2,7 @@ //! can be told apart, moved, and closed from it. A stand-in for the window system runs each OS window's pass on the //! harness's context with its own input, as eframe does. -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::rc::Rc; @@ -250,3 +250,41 @@ fn a_floating_window_dropped_on_the_main_windows_tabs_docks_again() { assert_eq!(h.state().session.documents().len(), 1, "the document stays open"); assert_eq!(h.state().session.active_index(), Some(0), "the docked document is in front"); } + +/// #16415, Windows: the OS runs a title-bar move of the floating window (its hit test answers the caption), so the app +/// sees no press and no release. The window moving while the physical button is down, then the button going up with the +/// OS pointer on the bar over the main window's tabs, docks it; the same end with the pointer elsewhere keeps it. +#[test] +fn an_os_move_of_the_floating_window_ending_over_the_tabs_docks_it() { + use std::sync::Arc; + use std::sync::atomic::{AtomicBool, Ordering}; + let (mut h, host) = desktop(); + let id = window(&host); + let down = Arc::new(AtomicBool::new(false)); + let pointer = Rc::new(Cell::new(None::)); + { + let (down, pointer) = (Arc::clone(&down), Rc::clone(&pointer)); + let app = h.state_mut(); + app.services.primary_down = Some(Box::new(move || down.load(Ordering::SeqCst))); + app.services.cursor_pos = Some(Box::new(move |_| pointer.get())); + } + steps(&mut h, &host, id, 2); + let bar = title_bar(&host, id).expect("the bar"); + let grab = bar.left_center() + vec2(40.0, 0.0); + // The OS moves the window with the button held, then the button goes up: off the tabs, nothing docks. + let os_move = |h: &mut Harness<'static, App>, to: Rect| { + down.store(true, Ordering::SeqCst); + host.borrow_mut().moved = Some(to); + pointer.set(Some(to.min + grab.to_vec2())); + steps(h, &host, id, 2); + down.store(false, Ordering::SeqCst); + steps(h, &host, id, 2); + h.state().ui.document_windows.floating.len() + }; + let away = Rect::from_min_size(pos2(500.0, 400.0), window_rect().size()); + assert_eq!(os_move(&mut h, away), 1, "an OS move ending off the tabs keeps it floating"); + let target = h.state().dock_drop_rect.expect("the main window has a place to dock"); + let over = Rect::from_min_size(target.center() - grab.to_vec2(), window_rect().size()); + assert_eq!(os_move(&mut h, over), 0, "an OS move ending on the tabs docks it"); + assert_eq!(h.state().session.documents().len(), 1, "the document stays open"); +} diff --git a/crates/ui-egui/src/canvas/document_windows.rs b/crates/ui-egui/src/canvas/document_windows.rs index 3d1faa82c7..6e35acdf26 100644 --- a/crates/ui-egui/src/canvas/document_windows.rs +++ b/crates/ui-egui/src/canvas/document_windows.rs @@ -85,12 +85,14 @@ pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { }; let mut view = start.clone(); let mut dock = false; + let mut bar_rect = None; let inner = DocumentWindow::new(title).first_placed(rect).overlay(|o| o.size_limits(SizeLimits::at_least(MIN_SIZE))).show(ui, |ui, cx| { if let Some(bar) = window_title_bar(ui, bar_title.clone()) { if bar.close { cx.request_close(); } dock |= dropped_on_main(ui, id, bar.rect, dock_target); + bar_rect = Some(bar.rect); } // A second run of the body in one pass continues from the first run's view; the wrap rule // compares with the view the window started the pass with, so both runs file the same ask. @@ -105,6 +107,7 @@ pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { view = after; }); let output = inner.output; + dock |= os_move_dropped(app, ui.ctx(), id, output.response.id, bar_rect, dock_target); if dock { docked.push((id, doc)); continue; @@ -136,9 +139,46 @@ pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { for (id, doc) in docked { close_window(app, id); app.session.set_active(doc); + // The OS window closes in the next pass, the first that no longer shows it; nothing else may ask for one. + ui.ctx().request_repaint(); } } +/// Whether an OS move of the floating window `window` (the container `container`) by its title bar (`bar`, in the +/// window's points) ended this pass with the pointer on the bar over `target`, the main window's tab strip in its +/// points. Where the platform answers the window's hit test (Windows, the app-drawn frame), the OS runs the move and +/// the app sees neither the press nor the release: the window moving from one pass to the next starts it, the +/// physical primary button going up (`Services::primary_down`) ends it, and the OS pointer (`Services::cursor_pos`) +/// says where (#16415). +fn os_move_dropped(app: &mut PhotocraftApp, ctx: &egui::Context, window: u64, container: egui::Id, bar: Option, target: Option) -> bool { + let host = WindowHost::of(ctx); + let Some(facts) = host.viewport_of(container).and_then(|viewport| host.facts(viewport)) else { return false }; + let (last_key, moving_key) = (egui::Id::unique(("floating document place", window)), egui::Id::unique(("floating document moving", window))); + let previous = ctx.data(|d| d.get_temp::(last_key)); + ctx.data_mut(|d| d.insert_temp(last_key, facts.inner)); + // A move keeps the size; a resize or a wrap to the zoom is no move. + let moved = previous.is_some_and(|before| before.min != facts.inner.min && (before.size() - facts.inner.size()).length() < 0.5); + let Some(primary_down) = app.services.primary_down.as_ref() else { return false }; + let down = primary_down(); + // The OS may run the whole move between two of the app's passes (Windows draws no frame of the app while it + // moves a window), so a move first seen with the button up ends at once. + let moving = ctx.data(|d| d.get_temp::(moving_key).unwrap_or(false)) || moved; + if down || !moving { + ctx.data_mut(|d| d.insert_temp(moving_key, moving)); + // Nothing wakes the app when the OS move ends: look again soon while it lasts. + if moving { + ctx.request_repaint_after(std::time::Duration::from_millis(50)); + } + return false; + } + ctx.data_mut(|d| d.remove_temp::(moving_key)); + let (Some(bar), Some(target), Some(main)) = (bar, target, host.facts(egui::ViewportId::ROOT)) else { return false }; + let Some(pointer) = app.services.cursor_pos.as_mut().and_then(|cursor| cursor(ctx)) else { return false }; + // The pointer is in the main window's points; the release on the bar is where the move held the window. + let on_bar = bar.translate(facts.inner.min.to_vec2()).expand(2.0).contains(main.inner.min + pointer.to_vec2()); + on_bar && target.contains(pointer) +} + /// Whether this pass ends a move of the floating window `window` by its title bar (`bar`, in the window's points) with /// the pointer over `target`, the main window's tab strip in its points (`PhotocraftApp::dock_drop_rect`): the press on /// the bar notes where the window was, and the release, which the runtime adds once the OS move ends, docks when the diff --git a/crates/ui-egui/src/canvas/tabs.rs b/crates/ui-egui/src/canvas/tabs.rs index 4ff6b1353c..5ebe93e531 100644 --- a/crates/ui-egui/src/canvas/tabs.rs +++ b/crates/ui-egui/src/canvas/tabs.rs @@ -38,9 +38,8 @@ pub(super) fn strip(app: &mut PhotocraftApp, ui: &mut egui::Ui, cx: &mut BodyCx< let ctx = ui.ctx().clone(); let out = tabs(app, ui); let response = out.response.clone(); - // A floating document window's title bar dropped on the strip docks it (`document_windows`); with every document - // floating, the whole area keeps taking the drop. - if !docked_document_indices(app).is_empty() { + // A floating document window's title bar dropped on the strip docks it (`document_windows`). + if response.rect.is_positive() { app.dock_drop_rect = Some(response.rect); } if let Some(menu) = apply(app, &ctx, out) { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index ef7fb0af5c..b0c7014f6d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -240,8 +240,9 @@ pub use services::{ AdoptAutosaveFn, AppendTextFn, AutomationAuthorizeFn, AutomationCommandFn, AutomationLocateFn, AutomationOpenPcraftFn, AutomationReadFn, AutomationServices, AutomationWriteFn, AutosaveFn, AutosaveResultsFn, CapsLockFn, ClipboardGetFn, ClipboardGetSvgFn, ClipboardSetFn, ClipboardUnreadableFn, CursorPosFn, DiscardAutosaveFn, ExportFn, ExportSettings, ExternalSvgEditor, ExternalSvgSession, ExternalVectorSave, ExternalVectorSource, FileStamp, - FileStampFn, FormatChoices, ImportFn, Inbox, LoadTextFn, OpenExternalSvgFn, OpenUrlFn, OsEventsFn, PenEndFn, PhotoshopShortcutsFn, PlaceInbox, RecoverFn, - Recoverable, RightButtonDownFn, SameFileFn, SaveFileFn, SaveTextFn, Services, SystemThemeFn, TabletPollFn, WindowFrameFn, WriteFn, + FileStampFn, FormatChoices, ImportFn, Inbox, LoadTextFn, OpenExternalSvgFn, OpenUrlFn, OsEventsFn, PenEndFn, PhotoshopShortcutsFn, PlaceInbox, + PrimaryDownFn, RecoverFn, Recoverable, RightButtonDownFn, SameFileFn, SaveFileFn, SaveTextFn, Services, SystemThemeFn, TabletPollFn, WindowFrameFn, + WriteFn, }; pub(crate) use services::{opaque_layer, saves_flat_in_place}; pub use text::cjk_fonts; @@ -426,7 +427,7 @@ pub struct PhotocraftApp { /// start screen or an opening file's card): files dropped here are placed as layers. pub(crate) drop_canvas_rect: Option, /// Where a floating document window's title bar dropped docks its document, in the main window's points, as of - /// the last frame: the document tab strip, or the whole document area while no document is docked (#16415). + /// the last frame: the document tab strip, or a band of its height at the top of the document area (#16415). pub(crate) dock_drop_rect: Option, /// The window's one egui context, for code without one in hand: SAVED-tier reads and writes, /// the Frame tool's transforms from pointer events, and the mapping and panels that read the diff --git a/crates/ui-egui/src/services.rs b/crates/ui-egui/src/services.rs index e84c92bd47..6f53f98f52 100644 --- a/crates/ui-egui/src/services.rs +++ b/crates/ui-egui/src/services.rs @@ -493,6 +493,11 @@ pub type CapsLockFn = Box bool>; /// Called once per frame; `None` when the window is decorated or the platform needs no fixing. pub type WindowFrameFn = Box; +/// Whether the physical primary mouse button is down now, read from the OS. A move of a floating document window by +/// its title bar runs in the OS (its hit test answers the caption), so the app never sees the press or the release; +/// the button going up ends the move (#16415). +pub type PrimaryDownFn = Box bool + Send + Sync>; + /// Physical right mouse button or stylus barrel button down (Windows; read for brush resize). pub type RightButtonDownFn = Box bool + Send + Sync>; @@ -644,6 +649,8 @@ pub struct Services { pub window_frame: Option, /// Physical right mouse button or pen barrel button currently down. pub right_button_down: Option, + /// Physical primary mouse button currently down (Windows): ends an OS move of a floating document window. + pub primary_down: Option, /// Which end of the pen is in use, where the platform reports it outside the stylus feed /// (Windows: the feed carries no pen samples there). Read for the eraser-end tool switch. pub pen_end: Option,