diff --git a/crates/ui-contract/src/runtime/drag.rs b/crates/ui-contract/src/runtime/drag.rs index 884a870cf..51839744f 100644 --- a/crates/ui-contract/src/runtime/drag.rs +++ b/crates/ui-contract/src/runtime/drag.rs @@ -658,6 +658,13 @@ pub fn moves(ctx: &Context, viewport: ViewportId) -> bool { board(ctx, |board| board.session.as_ref().is_some_and(|session| session.moving.viewport == Some(viewport))) } +/// Whether `source` started the running session, which after its release lives until a target in another window has +/// taken the drop. +#[doc(hidden)] +pub fn sourced_by(ctx: &Context, source: Id) -> bool { + board(ctx, |board| board.session.as_ref().is_some_and(|session| session.source == source)) +} + /// Starts a session whose source is the container `source`, as the title row of a dock window does, with no payload /// of its own. For trials. #[cfg(feature = "testing")] diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 58539fbe3..c9690393c 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -1233,18 +1233,24 @@ pub fn shapes_at(ctx: &Context, at: Pos2) -> Vec<(Id, Option)> { }) } +/// The part of `owner` holding a press in this viewport that was open when the pass began: in the pass that ends the +/// press too, and in none after it. +pub fn pressed_part(ctx: &Context, owner: Id) -> Option { + PerViewport::::with(ctx, pass::current_viewport(ctx), |record| { + record.pressed_parts.iter().find_map(|(id, part)| (*id == owner).then_some(*part)) + }) +} + /// The part of `owner` holding the pointer's hover or an open press in this viewport. /// /// An open press keeps its part while the pointer moves away. A press open at the start of the pass keeps its part /// for that whole pass, so a child can still declare it after an enclosing arena has processed the release. pub fn active_part(ctx: &Context, owner: Id) -> Option { - let viewport = pass::current_viewport(ctx); - let pressed = - PerViewport::::with(ctx, viewport, |record| record.pressed_parts.iter().find_map(|(id, part)| (*id == owner).then_some(*part))); + let pressed = pressed_part(ctx, owner); if pressed.is_some() { return pressed; } - with_hover(ctx, viewport, |under, captured| { + with_hover(ctx, pass::current_viewport(ctx), |under, captured| { if captured { return None; } diff --git a/crates/ui-panels/src/docking/config.rs b/crates/ui-panels/src/docking/config.rs index 22309ca23..90cdca42d 100644 --- a/crates/ui-panels/src/docking/config.rs +++ b/crates/ui-panels/src/docking/config.rs @@ -111,7 +111,8 @@ pub struct DockConfig { pub tab_look: PanelTabLook, /// A floating window opens in an OS window of its own where the window host opens OS windows and reports where /// they are on the desktop (not on Wayland) and its panels allow it (`PanelSpec::allow_native`). Elsewhere, and - /// with this off, it shows in the app window. + /// with this off, it shows in the app window. A window that takes over a held press (`DockArea::continue_move`) + /// opens its OS window once that press ends. pub native_windows: bool, /// Docked groups' menus get Minimize and Move Group Up and Down. pub group_order_items: bool, diff --git a/crates/ui-panels/src/docking/native.rs b/crates/ui-panels/src/docking/native.rs index 0b54ab05d..2af914581 100644 --- a/crates/ui-panels/src/docking/native.rs +++ b/crates/ui-panels/src/docking/native.rs @@ -9,10 +9,10 @@ use super::{DockConfig, FloatingGroupOutput, PanelSpec}; use crate::runtime::windows::{WindowAction, WindowHost, WindowSpec}; -use crate::runtime::{drag, overlay}; -use craft_behavior::GesturePhase; +use crate::runtime::{drag, overlay, pointer}; use craft_behavior::dock::{Action, Floating}; use craft_behavior::placement::{Defaults, monitor_of, place}; +use craft_behavior::{Buttons, GesturePhase}; use craft_host::target::WindowKind; use egui::{Context, Pos2, Rect, Vec2, ViewportId}; @@ -29,6 +29,8 @@ pub struct Opening { pub rect: Rect, /// The OS window to open, or `None` where the group shows in the app window. pub spec: Option, + /// The group shows in the app window until the drag that moves it there ends, and in an OS window after. + pub waits: bool, } /// Whether the groups of panels with `specs` open OS windows: the dock's `config` turns them on, the platform opens @@ -89,17 +91,38 @@ pub fn restore

(ctx: &Context, window: &Floating

) -> Rect { /// at its own rect. `specs` are its panels' specs; `group` is the id of the group that shows it. A window that opens /// opens at its [`restore`]d rect; an open window stays at the placement the dock model recorded for it, so the /// monitor fit of `restore` never sends back a place the user gave it. +/// +/// A window opens no OS window while a drag moves it in the app window: the OS gives an OS window no press that went +/// down in another window, so the drag would end. `continues` says that the window takes over the held press in this +/// pass (`DockArea::continue_move`), which a window never placed on the desktop does in the app window. #[doc(hidden)] -pub fn opening

(ctx: &Context, config: &DockConfig, window: &Floating

, specs: &[PanelSpec], group: egui::Id) -> Opening { +pub fn opening

(ctx: &Context, config: &DockConfig, window: &Floating

, specs: &[PanelSpec], group: egui::Id, continues: bool) -> Opening { + let layer = Opening { rect: window.rect, spec: None, waits: false }; if !allowed(ctx, config, specs) { - return Opening { rect: window.rect, spec: None }; + return layer; } let open = WindowHost::of(ctx).viewport_of(group).is_some(); + if !open { + let takes_over = continues && window.native.is_none() && pointer::frame(ctx).buttons.contains(Buttons::PRIMARY); + if takes_over || pointer::pressed_part(ctx, group).is_some() || drag::sourced_by(ctx, group) { + return Opening { waits: true, ..layer }; + } + } let recorded = window.native.as_ref().filter(|_| open).and_then(|native| { let at = native.position()?; Some(Rect::from_min_size(Pos2::from(at), Vec2::from(native.normal.size))) }); - Opening { rect: recorded.unwrap_or_else(|| restore(ctx, window)), spec: Some(spec_for(specs)) } + Opening { rect: recorded.unwrap_or_else(|| restore(ctx, window)), spec: Some(spec_for(specs)), waits: false } +} + +/// Asks for the pass in which a group that waits to open its OS window ([`Opening::waits`]) opens it: the next one once +/// the press that moved it has ended, or the one after a target in another window had its chance to take the drop. +#[doc(hidden)] +pub fn waited(ctx: &Context, opening: &Opening, output: &FloatingGroupOutput) { + let ended = output.placed.is_some_and(|change| matches!(change.phase, GesturePhase::Commit | GesturePhase::Cancel)); + if opening.waits && (ended || pointer::pressed_part(ctx, output.response.id).is_none()) { + crate::runtime::frame::request_repaint(ctx); + } } /// The action that records where the OS put the native window `window`, shown in `viewport`: its placement, as an diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 3734ecc75..7a5e6d2f3 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -172,8 +172,9 @@ pub struct DockArea<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> { /// The floating window holding the panel takes over the held primary press in this pass, with the pointer at the /// grab point inside the window ([`FloatingGroup::continue_move`]): the window follows the pointer and docks where it /// would dock on release. A request of the pass: a host that floats a panel from a rail's `torn_off` keeps it for - /// the one pass after, when the window shows. Without a held press, or when no window holds the panel, it does - /// nothing. + /// the one pass after, when the window shows. Where the dock opens OS windows (`DockConfig::native_windows`), the + /// window shows in the app window until the press ends, and then opens its OS window where it was released. Without + /// a held press, when no window holds the panel, or when the window has shown in an OS window, it does nothing. #[prop(quantity = "panel and grab point")] continue_move: Option<(P, Vec2)>, /// Moves key focus to the group that shows the panel, as a press on the group's body does, and picks its tab, as a @@ -1725,7 +1726,8 @@ impl Pass<'_, P> { let rights: Vec = window.panels().map(|panel| scene.permissions(panel)).collect(); let dock = scene.dock.with(("floating", window.id)); let group_id = dock.with("window"); - let opening = native::opening(&ctx, scene.config, window, &specs, group_id); + let continued = scene.continue_move.filter(|(panel, _)| window.panels().any(|held| held == *panel)).map(|(_, grab)| grab); + let opening = native::opening(&ctx, scene.config, window, &specs, group_id, continued.is_some()); // An OS window lays out at its own monitor's scale, which sizes its chrome; one in the app window at the // main window's. let scale = opening @@ -1791,7 +1793,7 @@ impl Pass<'_, P> { if scene.focus.is_some_and(|focus| window.panels().any(|held| held == focus)) { group = group.raise(); } - if let Some((_, grab)) = scene.continue_move.filter(|(panel, _)| window.panels().any(|held| held == *panel)) { + if let Some(grab) = continued { group = group.continue_move(grab); } if scene.raise.is_some_and(|raised| window.panels().any(|held| held == raised)) { @@ -1827,6 +1829,7 @@ impl Pass<'_, P> { }); self.dock = area_dock; let output = shown.output; + native::waited(&ctx, &opening, &output); // The window's grips, then everything it drew, in the OS window it shows in, if it has one. let grips = output.grip.map(|(rect, node)| DockSource::new(SourceKind::WindowGrip(anchor.clone()), rect, node)); let resize = diff --git a/crates/ui-patterns/tests/it/docking/mod.rs b/crates/ui-patterns/tests/it/docking/mod.rs index 766343ee0..77bcc8212 100644 --- a/crates/ui-patterns/tests/it/docking/mod.rs +++ b/crates/ui-patterns/tests/it/docking/mod.rs @@ -35,6 +35,7 @@ pub(crate) mod tab_focus; pub(crate) mod workspace_dialogs; pub(crate) mod native; +pub(crate) mod native_drag; pub(crate) mod panes; /// The docking sizes of the default theme with the given divider gap, least pane and strip height. diff --git a/crates/ui-patterns/tests/it/docking/native_drag.rs b/crates/ui-patterns/tests/it/docking/native_drag.rs new file mode 100644 index 000000000..ec6547ac6 --- /dev/null +++ b/crates/ui-patterns/tests/it/docking/native_drag.rs @@ -0,0 +1,178 @@ +//! A host's own drag (a rail popup's title) that floats a panel and hands its press to the panel's window, in a dock +//! that opens its floats in OS windows. + +use super::Numbered; +use craft_behavior::dock::{Floating, Layout, Node, Workspace}; +use craft_ui::contract::{Bind, BodyCx, Role, Tracked}; +use craft_ui::docking::{DockConfig, DockPanels, GroupDrag}; +use craft_ui::patterns::dock::DockArea; +use craft_ui::runtime::drag; +use craft_ui::testing::input::key_press; +use craft_ui::testing::pointer::{moved, press, release}; +use craft_ui::testing::trial; +use craft_ui_test_kit::windows::{Out, Windows}; +use egui::{Event, Key, Modifiers, Pos2, Rect, Sense, Ui, Vec2, ViewportId, ViewportInfo}; +use std::sync::Arc; + +const DOCKED: usize = 1; +const FLOATED: usize = 2; +/// The main window on the desktop. +const MAIN_AT: Rect = Rect { min: Pos2::new(100.0, 100.0), max: Pos2::new(900.0, 700.0) }; +/// Where the host floats the panel, in the main window. +const WINDOW_AT: Rect = Rect { min: Pos2::new(130.0, 90.0), max: Pos2::new(330.0, 210.0) }; +/// The host's popup title, clear of the docked group's tab strip. +const TITLE_AT: Rect = Rect { min: Pos2::new(130.0, 230.0), max: Pos2::new(290.0, 254.0) }; +/// The grab point inside the window the host hands its press over at. +const GRAB: Vec2 = Vec2::new(40.0, 12.0); +const SLACK: f32 = 1.5; + +struct Empty; + +impl DockPanels for Empty { + fn body(&mut self, _: &usize, _: &mut Ui, _: &mut BodyCx<'_>) {} +} + +/// A dock over a host that shows a popup title, and the OS windows the dock opened. +struct Host { + windows: Windows, + workspace: Arc>, + specs: Tracked, + popup: bool, + hand_over: Option<(usize, Vec2)>, + /// Where the dock opened its first OS window on the desktop. + opened_at: Option, +} + +impl Host { + fn new(root: Option>) -> Self { + let windows = Windows::new(); + windows.root_info(info(MAIN_AT)); + windows.child_info(info(WINDOW_AT.translate(MAIN_AT.min.to_vec2()))); + let workspace = Arc::new(Workspace::new(Layout::new(root, Vec::new()))); + let mut host = Host { windows, workspace, specs: Tracked::new(Numbered), popup: true, hand_over: None, opened_at: None }; + for _ in 0..3 { + host.frame(vec![]); + } + host + } + + fn frame(&mut self, events: Vec) -> Out { + let Host { windows, workspace, specs, popup, hand_over, .. } = self; + let out = windows.frame(events, vec![], |ui| { + let mut panels = Empty; + let config = DockConfig::default().title_bar_docks(false); + let mut area = DockArea::new("Dock", Bind::bound(&mut *workspace), &*specs, &mut panels, config); + if let Some(request) = hand_over.take() { + area = area.continue_move(request); + } + area.show(ui); + if !*popup { + return; + } + egui::Area::new(egui::Id::unique("host popup")).order(egui::Order::Foreground).fixed_pos(TITLE_AT.min).show(ui.ctx(), |ui| { + let (_, title) = ui.allocate_exact_size(TITLE_AT.size(), Sense::drag()); + if title.drag_started() { + *popup = false; + let mut floated = workspace.layout().clone(); + floated.floating.push(Floating::single(100, vec![FLOATED], 0, WINDOW_AT)); + *workspace = Arc::new(Workspace::new(floated)); + *hand_over = Some((FLOATED, GRAB)); + } + }); + }); + if self.opened_at.is_none() { + self.opened_at = out.builders.iter().find(|(viewport, _)| *viewport != ViewportId::ROOT).and_then(|(_, builder)| builder.position); + } + out + } + + fn frames(&mut self, first: Vec) { + self.frame(first); + for _ in 0..3 { + self.frame(vec![]); + } + } + + /// Presses the popup's title and drags it to `to`, held there. + fn drag_to(&mut self, to: Pos2) { + let from = TITLE_AT.center(); + self.frame(vec![moved(from)]); + self.frame(vec![press(from)]); + for step in 1..=4 { + self.frame(vec![moved(from + (to - from) * (step as f32 / 4.0))]); + } + self.frame(vec![]); + } + + fn dragging(&self) -> bool { + drag::active::>(&self.windows.ctx).is_some() + } + + fn floats(&self) -> bool { + self.workspace.layout().floating.iter().any(|window| window.contains(&FLOATED)) + } +} + +fn info(rect: Rect) -> ViewportInfo { + ViewportInfo { + focused: Some(true), + inner_rect: Some(rect), + outer_rect: Some(rect), + native_pixels_per_point: Some(1.0), + ..ViewportInfo::default() + } +} + +/// A dock of one docked group, and a point on the free room of its tab strip. +fn docked() -> (Host, Pos2) { + let mut host = Host::new(Some(Node::tabs(vec![DOCKED], 0))); + let out = host.frame(vec![]); + let tree = &out.trees.iter().find(|(viewport, _)| *viewport == ViewportId::ROOT).expect("the main window ran a pass").1; + let tab = tree.with_role(Role::TAB).into_iter().find_map(|key| tree.bounds_of(key)).expect("the docked tab shows"); + (host, Pos2::new(tab.right() + 20.0, tab.center().y)) +} + +/// Dropped on a docked group's tab strip, the panel joins the group: the drag shows the dock's mark, and no OS window +/// opens at any point of it. +#[trial] +fn a_press_handed_to_a_float_docks_it_on_the_strip_it_is_released_on() { + let (mut host, strip) = docked(); + host.drag_to(strip); + assert_eq!(host.opened_at, None, "no OS window opens while the press is held"); + assert!(drag::mark(&host.windows.ctx).is_some(), "the strip under the pointer shows the drop mark"); + host.frames(vec![release(strip)]); + let layout = host.workspace.layout(); + assert!(layout.floating.is_empty() && layout.place(&FLOATED).is_some_and(|at| layout.place(&DOCKED) == Some(at)), "it joins: {layout:?}"); + assert_eq!(host.opened_at, None, "a docked panel opens no OS window"); +} + +/// Released where nothing takes it, the panel floats in an OS window at the place it was dropped. +#[trial] +fn a_press_handed_to_a_float_opens_its_os_window_where_it_is_released() { + let mut host = Host::new(None); + let to = Pos2::new(400.0, 300.0); + host.drag_to(to); + assert!(host.dragging(), "the panel's drag runs"); + assert_eq!(host.opened_at, None, "no OS window opens while the press is held"); + host.frames(vec![release(to)]); + let at = host.opened_at.expect("the OS window opens after the release"); + assert!(at.distance(MAIN_AT.min + (to - GRAB).to_vec2()) < SLACK, "where it was dropped: {at:?}"); + assert!(host.floats() && !host.dragging(), "the panel floats and no drag is left"); +} + +/// Escape, or the main window losing the focus, ends the drag: the panel's OS window opens where the host floated it, +/// and the button's later release drops nothing. +#[trial] +fn a_cancelled_press_handed_to_a_float_leaves_it_floating() { + for cancel in [key_press(Key::Escape, Modifiers::NONE), Event::WindowFocused(false)] { + let (mut host, strip) = docked(); + host.drag_to(strip); + assert!(host.dragging(), "{cancel:?}: the panel's drag runs"); + host.frames(vec![cancel.clone()]); + assert!(!host.dragging(), "{cancel:?} ends the drag"); + host.frames(vec![release(strip)]); + assert!(host.floats(), "{cancel:?}: the release docks nothing"); + let at = host.opened_at.expect("the panel's OS window opens"); + assert!(at.distance(MAIN_AT.min + WINDOW_AT.min.to_vec2()) < SLACK, "{cancel:?}: where the host floated it: {at:?}"); + } +}