diff --git a/crates/ui-contract/src/runtime/drag.rs b/crates/ui-contract/src/runtime/drag.rs index bfe0156945..099e17c232 100644 --- a/crates/ui-contract/src/runtime/drag.rs +++ b/crates/ui-contract/src/runtime/drag.rs @@ -925,14 +925,26 @@ pub(crate) struct Ghost { pub mark: Option<(DropMark, Arc)>, } -/// The running session's ghost data. +/// The running session's ghost data for the running viewport. The mark paints in the window whose target registered +/// it, and also in the window the session moves, mapped to the same desktop place: that window lies over the mark, +/// which would otherwise show only through it. pub(crate) fn ghost(ctx: &Context) -> Option { let viewport = pass::current_viewport(ctx); - board(ctx, |board| { - board.session.as_ref().map(|session| Ghost { - preview: session.preview.clone(), - env: session.env.clone(), - mark: session.mark.as_ref().filter(|shown| shown.viewport == viewport).map(|shown| (shown.mark, shown.tokens.clone())), - }) - }) + let (preview, env, mark, moves_this) = board(ctx, |board| { + board + .session + .as_ref() + .map(|session| (session.preview.clone(), session.env.clone(), session.mark.clone(), session.moving.viewport == Some(viewport))) + })?; + let mark = mark.and_then(|shown| { + let offset = if shown.viewport == viewport { + Vec2::ZERO + } else if moves_this { + WindowHost::of(ctx).offset_between(shown.viewport, viewport)? + } else { + return None; + }; + Some((shown.mark.mapped(|point| point + offset), shown.tokens)) + }); + Some(Ghost { preview, env, mark }) } diff --git a/crates/ui-contract/src/runtime/windows/host.rs b/crates/ui-contract/src/runtime/windows/host.rs index 0d747a7260..a6002db479 100644 --- a/crates/ui-contract/src/runtime/windows/host.rs +++ b/crates/ui-contract/src/runtime/windows/host.rs @@ -477,9 +477,20 @@ impl WindowHost { /// The desktop pointer in the coordinates of `viewport`, when it lies inside that window. pub fn pointer_in(&self, viewport: ViewportId) -> Option { let desktop = self.desktop_pointer()?; - let inner = PerViewport::::with(&self.ctx, viewport, |record| record.facts.and_then(|facts| facts.inner))?; + let inner = self.inner_on_desktop(viewport)?; inner.contains(desktop).then(|| Pos2::ZERO + (desktop - inner.min)) } + + /// What a point of `from`'s window adds to land at the same desktop place in `to`'s window, by the inner rects + /// the windows last reported, as [`Self::pointer_in`] maps the desktop pointer. + pub(crate) fn offset_between(&self, from: ViewportId, to: ViewportId) -> Option { + Some(self.inner_on_desktop(from)?.min - self.inner_on_desktop(to)?.min) + } + + /// The inner rect of `viewport`'s window on the desktop, as the window last reported it. + fn inner_on_desktop(&self, viewport: ViewportId) -> Option { + PerViewport::::with(&self.ctx, viewport, |record| record.facts.and_then(|facts| facts.inner)) + } } /// The frame of `viewport`'s window as its chrome and the caption buttons it was opened with tell it, for a window diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index aa82dee486..81f0909430 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -25,7 +25,9 @@ use craft_ui::testing::trial; use craft_ui::testing::windows::{FakeSystem, HeldButtons}; use craft_ui::text::NumberField; use craft_ui_test_kit::windows::{AppKitWindow, FilesOver, Out, Windows}; -use egui::{Event, HoveredFile, Key, Modifiers, Pos2, Rect, Ui, Vec2, ViewportBuilder, ViewportCommand, ViewportEvent, ViewportId, ViewportInfo}; +use egui::{ + Event, HoveredFile, Key, Modifiers, Pos2, Rect, Shape, Ui, Vec2, ViewportBuilder, ViewportCommand, ViewportEvent, ViewportId, ViewportInfo, +}; use std::sync::Arc; /// The root window and the palette overlap; the palette is the larger. @@ -270,6 +272,8 @@ struct NativeDock { palette: Option, /// The tree the palette window's pass produced in the last frame. palette_tree: Option, + /// The shapes the palette window's pass painted in the last frame. + palette_shapes: Vec, /// Whether Auto-Collapse Iconic Panels is on in the frames of a collapsed palette. auto_collapse: bool, } @@ -293,7 +297,17 @@ impl NativeDock { windows.child_info(info(true, FLOAT_AT)); let workspace = Arc::new(Workspace::new(Layout::new(Some(Node::tabs(vec![2], 0)), vec![palette]))); let specs = Tracked::new(Specs { rights: Permissions::ALL, least: false, stepped }); - let mut dock = NativeDock { windows, workspace, stepped, specs, builder: None, palette: None, palette_tree: None, auto_collapse: false }; + let mut dock = NativeDock { + windows, + workspace, + stepped, + specs, + builder: None, + palette: None, + palette_tree: None, + palette_shapes: Vec::new(), + auto_collapse: false, + }; for _ in 0..3 { dock.frame(vec![]); } @@ -320,6 +334,7 @@ impl NativeDock { } let palette = self.palette; self.palette_tree = out.trees.into_iter().find(|(viewport, _)| Some(*viewport) == palette).map(|(_, tree)| tree); + self.palette_shapes = out.shapes.into_iter().find(|(viewport, _)| Some(*viewport) == palette).map(|(_, shapes)| shapes).unwrap_or_default(); for (_, builder) in out.builders.into_iter().filter(|(viewport, _)| Some(*viewport) == palette) { match self.builder.as_mut() { Some(kept) => { @@ -384,6 +399,37 @@ fn dragging_a_native_palette_by_its_title_row_docks_it_where_the_app_moves_windo assert!(!docked, "a cancelled move docks nothing"); } +/// A palette moved by its title row over the main window's group lies over the mark the group shows: its own window +/// paints the mark too, at the same desktop place by the windows' inner rects, so the mark shows over the palette and +/// not only through its faded window. +#[trial] +fn a_native_palette_moved_over_a_drop_target_paints_the_mark_over_itself() { + let mut dock = NativeDock::on(Os::Linux, Arc::new(FakeSystem::new())); + let step = (OVER_GROUP - (FLOAT_AT.min + ROW_AT.to_vec2())) / 3.0; + for events in [vec![moved(ROW_AT)], vec![press(ROW_AT)]] { + dock.frame(events); + } + for _ in 0..3 { + dock.frame(vec![moved(ROW_AT + step)]); + } + dock.frame(vec![]); + let area = match drag::mark(&dock.windows.ctx) { + Some(DropMark::Region { area, .. } | DropMark::Into(area)) => area, + mark => panic!("the main window's group marks the region the palette takes: {mark:?}"), + }; + dock.frame(vec![]); + let palette = dock.palette.expect("the palette has its window"); + let inner = dock.windows.facts(palette).inner_rect.expect("the palette reports its inner rect"); + let mapped = area.translate(MAIN_AT.min - inner.min); + let shows = dock.palette_shapes.iter().any( + |shape| matches!(shape, Shape::Rect(rect) if (rect.rect.min - mapped.min).length() < 0.5 && (rect.rect.max - mapped.max).length() < 0.5), + ); + assert!(shows, "the palette's window paints the mark at {mapped:?}, where the main window's {area:?} lies under it"); + for events in [vec![key_press(Key::Escape, Modifiers::NONE)], vec![release(ROW_AT)], vec![]] { + dock.frame(events); + } +} + /// Where the app moves windows itself and the platform tells the system pointer (Windows): the window follows the /// system pointer, not the pass's pointer event, which can be an older sample than the window's current place (the /// window moves between passes). Measured from such an event the window lands a step behind the pointer each pass and diff --git a/crates/ui-test-kit/src/windows.rs b/crates/ui-test-kit/src/windows.rs index d54eafd75c..bbda539b5c 100644 --- a/crates/ui-test-kit/src/windows.rs +++ b/crates/ui-test-kit/src/windows.rs @@ -36,6 +36,8 @@ thread_local! { static COMMANDS: RefCell> = const { RefCell::new(Vec::new()) }; /// The AccessKit tree each viewport's last pass produced, in pass order. static TREES: RefCell> = const { RefCell::new(Vec::new()) }; + /// The shapes each pass of the frame painted, nested shapes flattened. + static SHAPES: RefCell)>> = const { RefCell::new(Vec::new()) }; /// The time every pass of the frame runs at. static NOW: Cell = const { Cell::new(0.0) }; /// Each child's builder as its window has it: a later builder patches it, and what changed goes to the window as @@ -205,6 +207,18 @@ fn render_child(ctx: &Context, viewport: egui::ImmediateViewport<'_>) { fn keep_tree(output: &mut FullOutput, viewport: ViewportId) { let tree = tree_of(output); TREES.with(|kept| kept.borrow_mut().push((viewport, tree))); + let mut shapes = Vec::new(); + for clipped in &output.shapes { + flatten(&clipped.shape, &mut shapes); + } + SHAPES.with(|kept| kept.borrow_mut().push((viewport, shapes))); +} + +fn flatten(shape: &egui::Shape, into: &mut Vec) { + match shape { + egui::Shape::Vec(shapes) => shapes.iter().for_each(|shape| flatten(shape, into)), + shape => into.push(shape.clone()), + } } /// An OS window as macOS runs egui's window commands on it through winit, in desktop points with y down: a position @@ -256,6 +270,8 @@ pub struct Out { /// The AccessKit tree each viewport's pass produced, in pass order: the children's first, as they run inside the /// root's pass. pub trees: Vec<(ViewportId, AccessTree)>, + /// The shapes each viewport's pass painted, nested shapes flattened, in the order of `trees`. + pub shapes: Vec<(ViewportId, Vec)>, } /// A context with the runtime installed whose child viewports are OS windows. @@ -343,6 +359,7 @@ impl Windows { root_dropped.into_iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect(); CHILD_FILES.with(|files| *files.borrow_mut() = child_files); TREES.with(|kept| kept.borrow_mut().clear()); + SHAPES.with(|kept| kept.borrow_mut().clear()); set_input(&self.ctx, FrameInput::new(self.root.borrow().clone())); let mut input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, ROOT_SIZE)), @@ -373,6 +390,7 @@ impl Windows { commands: COMMANDS.with(|kept| std::mem::take(&mut *kept.borrow_mut())), builders, trees: TREES.with(|kept| std::mem::take(&mut *kept.borrow_mut())), + shapes: SHAPES.with(|kept| std::mem::take(&mut *kept.borrow_mut())), } } }