diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 789a942391..c53d33e9a2 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -99,6 +99,7 @@ impl PhotocraftApp { last_canvas_layer: egui::LayerId::background(), ppp: 1.0, drop_canvas_rect: None, + dock_drop_rect: None, ctx, frame_preview: None, drop_places: Default::default(), diff --git a/crates/ui-egui/src/canvas/document_area.rs b/crates/ui-egui/src/canvas/document_area.rs index bfc1345247..e239c0b527 100644 --- a/crates/ui-egui/src/canvas/document_area.rs +++ b/crates/ui-egui/src/canvas/document_area.rs @@ -133,6 +133,8 @@ pub(crate) fn apply_arranged(app: &mut PhotocraftApp, arranged: Arranged) { 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()); 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 668053b686..4ec7978b70 100644 --- a/crates/ui-egui/src/canvas/document_window_title_tests.rs +++ b/crates/ui-egui/src/canvas/document_window_title_tests.rs @@ -209,3 +209,44 @@ fn a_moved_floating_window_stays_where_it_was_moved() { let placed = h.state().ui.document_windows.floating.first().map(|(_, rect)| *rect); assert_eq!(placed, Some(moved), "the app keeps the window's new place"); } + +/// Presses the floating window's title bar at its middle-left, lets the OS move the window to `to` (outer rect on the +/// desktop) and releases there, as an OS move by the title bar does (the runtime adds the release when it ends). +fn move_by_title_bar(h: &mut Harness<'static, App>, host: &RefCell, id: ViewportId, to: Option) { + let bar = title_bar(host, id).expect("the floating window has a title bar"); + let at = bar.left_center() + vec2(40.0, 0.0); + let press = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + host.borrow_mut().events.insert(id, vec![Event::PointerMoved(at), press(true)]); + steps(h, host, id, 1); + if let Some(to) = to { + host.borrow_mut().moved = Some(to); + steps(h, host, id, 1); + } + host.borrow_mut().events.insert(id, vec![press(false)]); + steps(h, host, id, 3); +} + +/// #16415: a floating document window dropped by its title bar on the main window's document tabs goes back into +/// them; dropped elsewhere it stays where it went, and a click on its bar docks nothing. +#[test] +fn a_floating_window_dropped_on_the_main_windows_tabs_docks_again() { + let (mut h, host) = desktop(); + let id = window(&host); + steps(&mut h, &host, id, 2); + // A click, no move: the window stays. + move_by_title_bar(&mut h, &host, id, None); + assert_eq!(h.state().ui.document_windows.floating.len(), 1, "a click on the bar docks nothing"); + // Dropped far off the main window: it stays floating there. + let away = Rect::from_min_size(pos2(2000.0, 1200.0), window_rect().size()); + move_by_title_bar(&mut h, &host, id, Some(away)); + assert_eq!(h.state().ui.document_windows.floating.len(), 1, "dropped off the main window it keeps floating"); + // Dropped with the pointer on the main window's tab area: it docks, its document in front. + let target = h.state().dock_drop_rect.expect("the main window has a place to dock"); + let bar = title_bar(&host, id).expect("the bar"); + let grab = bar.left_center() + vec2(40.0, 0.0); + let over = Rect::from_min_size(target.center() - grab.to_vec2(), window_rect().size()); + move_by_title_bar(&mut h, &host, id, Some(over)); + assert!(h.state().ui.document_windows.floating.is_empty(), "dropped on the tabs the window docks"); + 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"); +} diff --git a/crates/ui-egui/src/canvas/document_windows.rs b/crates/ui-egui/src/canvas/document_windows.rs index 88744c480a..3d1faa82c7 100644 --- a/crates/ui-egui/src/canvas/document_windows.rs +++ b/crates/ui-egui/src/canvas/document_windows.rs @@ -63,6 +63,8 @@ pub(crate) fn sync_documents(app: &mut PhotocraftApp, ids: &[DocId]) { pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let windows: Vec<(u64, Rect)> = app.ui.document_windows.floating.iter().map(|(g, rect)| (g.id, *rect)).collect(); let mut closed = Vec::new(); + let mut docked = Vec::new(); + let dock_target = app.dock_drop_rect; for (id, rect) in windows { let shown = app.ui.document_windows.floating.iter().find(|(g, _)| g.id == id).and_then(|(g, _)| g.shown.or_else(|| g.documents.first().copied())); let Some(doc) = shown.and_then(|d| crate::canvas::tabs::document_index(app, d)) else { continue }; @@ -82,9 +84,13 @@ pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { if app.session.documents()[doc].is_dirty() { Title::unsaved(text) } else { Title::new(text) } }; let mut view = start.clone(); + let mut dock = false; let inner = DocumentWindow::new(title).first_placed(rect).overlay(|o| o.size_limits(SizeLimits::at_least(MIN_SIZE))).show(ui, |ui, cx| { - if window_title_bar(ui, bar_title.clone()) { - cx.request_close(); + 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); } // 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. @@ -99,6 +105,10 @@ pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { view = after; }); let output = inner.output; + if dock { + docked.push((id, doc)); + continue; + } follow_os_window(app, ui.ctx(), id, output.response.id); if output.close_requested.is_some() { closed.push(id); @@ -122,6 +132,36 @@ pub fn extra_windows(app: &mut PhotocraftApp, ui: &mut egui::Ui) { for id in closed { close_window(app, id); } + // A window whose title bar was dropped on the main window's tabs goes back into them, its document in front. + for (id, doc) in docked { + close_window(app, id); + app.session.set_active(doc); + } +} + +/// 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 +/// window moved and the pointer is over the strip (#16415). A click on the bar, which moves nothing, docks nothing. +fn dropped_on_main(ui: &egui::Ui, window: u64, bar: Rect, target: Option) -> bool { + let ctx = ui.ctx(); + let host = WindowHost::of(ctx); + let Some(facts) = host.facts(ctx.viewport_id()) else { return false }; + let key = egui::Id::unique(("floating document press", window)); + let (pressed, released, pos) = ui.input(|i| (i.pointer.primary_pressed(), i.pointer.primary_released(), i.pointer.interact_pos())); + if pressed && pos.is_some_and(|p| bar.contains(p)) { + ctx.data_mut(|d| d.insert_temp(key, facts.inner.min)); + return false; + } + if !released { + return false; + } + let Some(start) = ctx.data_mut(|d| d.remove_temp::(key)) else { return false }; + let (Some(target), Some(main), Some(pos)) = (target, host.facts(egui::ViewportId::ROOT), pos) else { return false }; + if start == facts.inner.min { + return false; + } + target.translate(main.inner.min.to_vec2()).contains(facts.inner.min + pos.to_vec2()) } /// Keeps the rect the floating window `window` is placed at (`DocumentWindow::first_placed`, the container `container`) @@ -149,13 +189,22 @@ fn follow_os_window(app: &mut PhotocraftApp, ctx: &egui::Context, window: u64, c /// and Linux): the document's title, a move band and the caption buttons. `DocumentWindow` leaves that bar to its /// body there (#16415); without it the window could not be moved, minimized, maximized or closed. With the system's /// frame the OS draws the bar, and where OS windows are unavailable the fallback layer has its own title row, so -/// nothing is drawn then. Returns whether the bar's Close was clicked. -fn window_title_bar(ui: &mut egui::Ui, title: Title) -> bool { +/// nothing is drawn then. Returns the bar's rect and whether its Close was clicked, or `None` with no bar. +fn window_title_bar(ui: &mut egui::Ui, title: Title) -> Option { let host = WindowHost::of(ui.ctx()); if !host.viewports_available() || matches!(host.frame(), Frame::Native) { - return false; + return None; } - TitleBar::new().show_titled(ui, title, |_, _| {}, |_, _| {}).close_requested + let out = TitleBar::new().show_titled(ui, title, |_, _| {}, |_, _| {}); + Some(ShownBar { rect: out.response.rect, close: out.close_requested }) +} + +/// A floating document window's title bar as this pass showed it. +struct ShownBar { + /// Its rect, in the window's points. + rect: Rect, + /// Its Close was clicked. + close: bool, } /// The canvas of the document window `window` showing document `doc`, drawn in its own viewport. diff --git a/crates/ui-egui/src/canvas/tabs.rs b/crates/ui-egui/src/canvas/tabs.rs index 471d3d7ed7..4ff6b1353c 100644 --- a/crates/ui-egui/src/canvas/tabs.rs +++ b/crates/ui-egui/src/canvas/tabs.rs @@ -38,6 +38,11 @@ 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() { + app.dock_drop_rect = Some(response.rect); + } if let Some(menu) = apply(app, &ctx, out) { cx.keep::().0 = Some(menu); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 0927c80a3a..ef7fb0af5c 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -425,6 +425,9 @@ pub struct PhotocraftApp { /// The document area showing the active document's canvas last frame (not the tabs, the /// 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). + 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 /// canvas's zoom ease (`document_display::shown_camera`). Read it through [`Self::ctx`].