diff --git a/crates/ui-panels/src/docking/group.rs b/crates/ui-panels/src/docking/group.rs index a3ffef3dc..2c95fdb41 100644 --- a/crates/ui-panels/src/docking/group.rs +++ b/crates/ui-panels/src/docking/group.rs @@ -17,7 +17,7 @@ use crate::display::{Shade, Text}; use crate::menu::{GroupItem, Menu, PanelMenu, PanelRow}; use crate::runtime::commands::{self, CommandSet as _, CommandState, Commands as _}; use crate::runtime::env::Label; -use crate::runtime::focus::{self, FocusTarget}; +use crate::runtime::focus::{self, FocusRequest, FocusTarget}; use crate::runtime::memory::sub; use crate::sections::FooterBar; use crate::style::{ComponentStyle, Corners, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceKind, TypeRole}; @@ -354,6 +354,8 @@ pub struct DockGroup<'a, P: Clone + Hash + Send + Sync + 'static> { /// single titled panel does: the panels are shown from a rail, not by tabs. #[prop(flag)] titled: bool, + /// The tab row takes key focus in this pass: [`take_focus`](DockGroup::take_focus). + take_focus: bool, /// The dock whose drop registry the group's spots go to. #[prop] dock: Option, @@ -407,6 +409,7 @@ impl<'a, P: Clone + Hash + Send + Sync + PartialEq + Debug + 'static> DockGroup< wheel_selects_tabs: false, tab_look: PanelTabLook::Plain, titled: false, + take_focus: false, dock: None, zones: ZoneRule::Fraction, in_window: false, @@ -431,6 +434,14 @@ impl<'a, P: Clone + Hash + Send + Sync + PartialEq + Debug + 'static> DockGroup< self } + /// The group takes key focus in this pass, as a click on its tabs gives it: its tab row's stop takes it while the + /// row runs, so the keys of the next pass route through the group; a titled header, which has no tab row, focuses + /// its drag band. A request of the pass. + #[must_use] + pub fn take_focus(self) -> Self { + DockGroup { take_focus: true, ..self } + } + /// Whether the panel menu button shows: it is asked for and the group has a menu. fn shows_menu_button(&self) -> bool { self.panel_menu_shown && self.menu.is_some() @@ -547,6 +558,10 @@ impl Component for popup.open(cx); } } + // A titled header has no tab row: its drag band is the group's stop. + if self.take_focus && self.titled && cx.part_if_declared(GRIP).is_some() { + focus::request(&cx.core.ctx, FocusTarget::Part(cx.core.id, GRIP.into())); + } if let Some(grip) = cx.part_if_declared(GRIP).map(|grip| grip.press) { self.reported.toggle_minimized |= grip.double_pressed(); if self.movable && matches!(grip.phase, PressPhase::DragStart { .. }) { @@ -671,6 +686,7 @@ impl Container for closable: self.closable, wheel_selects: self.wheel_selects_tabs, look: self.tab_look, + take_focus: self.take_focus, dock: self.dock, sent: Outbox::new(&mut sent), }; @@ -746,6 +762,9 @@ struct TabRow<'a, P> { closable: bool, wheel_selects: bool, look: PanelTabLook, + /// The strip takes key focus in this pass (`DockGroup::take_focus`): in the key, so the row runs and the strip + /// takes it while it runs. + take_focus: bool, dock: Option, sent: Outbox<'a, TabEvent>, } @@ -785,6 +804,10 @@ fn tab_row(row: &mu if row.wheel_selects { strip = strip.wheel_selects(); } + // The strip takes the focus while it runs, so it runs focused and the keys of the next pass route through it. + if row.take_focus { + strip = strip.request_focus(FocusRequest::Take); + } let marks = &row.marks; let strip = strip.show_items(ui, &source, &mut |key, _| { let panel = panels.iter().find(|panel| &panel.key == key); diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index c8f50a75a..d5943f5ad 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -68,7 +68,8 @@ pub struct DockAreaOutput

{ pub events: Vec>, /// The shown panel of the group that holds key focus: focus is on the group or on a control inside it. pub focused: Option

, - /// The panel whose tab the user clicked or picked this pass. + /// The panel whose tab the user clicked or picked this pass; a pick of the host's `DockArea::focus` is not the + /// user's and shows only in `actions`. pub picked: Option

, /// Why nothing was drawn, when the workspace is invalid. pub error: Option, @@ -107,6 +108,7 @@ impl

Outcome for DockAreaOutput

{ /// actions the workspace took, divider sizes and window moves included. A given workspace is not written. /// - Requests: while the pass runs, a body changes the workspace through /// [`request_actions`](crate::docking::request_actions); the output returns those actions unapplied in `requested`. +/// The host moves key focus to a panel's group with [`focus`](DockArea::focus). /// - Hiding: [`hide`](DockArea::hide), the shell's panel visibility and [`maximized`](DockArea::maximized) prune /// what shows; a pruned panel keeps its place for restore. /// - Drops: the area is the drop target of tab and group drags. Ctrl or Cmd held at the release floats the drop, and @@ -151,6 +153,13 @@ pub struct DockArea<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> { /// nothing. #[prop(quantity = "panel and grab point")] continue_move: Option<(P, Vec2)>, + /// Moves key focus to the group that shows the panel and picks its tab, as a click on the tab does: the group's + /// stop takes focus in this pass, and the pick is one of the pass's `actions`. A request of the pass: a host that + /// focuses a panel itself (it opened something in it) sets it for one pass. A panel the area does not show in this + /// pass (hidden, pruned by a maximize, in a closed flyout, or in a group the host's `group_view` draws) takes + /// neither; `DockAreaOutput::focused` tells the host where focus is. + #[prop] + focus: Option

, /// Adds the host's fixed trailing controls to the docked rail containing `panel`. The panel identifies the /// column as it moves between dock sides. The host draws the controls through [`DockPanels::rail_footer`]; its /// icons scroll above them when the rail is short. Has no effect in flyout mode or on floating columns. @@ -183,6 +192,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { locked: false, flyout: None, continue_move: None, + focus: None, rail_footer: None, top_inset: 0.0, } @@ -316,6 +326,8 @@ struct Scene<'s, P> { auto_collapse: bool, /// The panel whose window takes over the held press, with its grab point (`DockArea::continue_move`). continue_move: Option<(&'s P, Vec2)>, + /// The panel whose group takes key focus in this pass (`DockArea::focus`). + focus: Option<&'s P>, rtl: bool, /// The docked places floating panels left, for Dock Panel. floated: &'s [(P, Location

)], @@ -491,6 +503,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { locked, mut flyout, continue_move, + focus, rail_footer, top_inset, } = self; @@ -550,6 +563,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { maximized: maximized.as_ref(), auto_collapse, continue_move: continue_move.as_ref().map(|(panel, grab)| (panel, *grab)), + focus: focus.as_ref(), sizes, rtl, rail_footer: rail_footer.as_ref(), @@ -655,6 +669,7 @@ struct Input<'i, P> { maximized: Option<&'i P>, auto_collapse: bool, continue_move: Option<(&'i P, Vec2)>, + focus: Option<&'i P>, sizes: DockSizes, rtl: bool, rail_footer: Option<&'i P>, @@ -689,8 +704,24 @@ fn show_area( panels: &mut dyn DockPanels

, mut flyout: Option<&mut Bind<'_, Option

>>, ) -> Passed

{ - let Input { id, ctx, current, config, specs, keys, hide, model, maximized, auto_collapse, continue_move, sizes, rtl, rail_footer, top_inset } = - *input; + let Input { + id, + ctx, + current, + config, + specs, + keys, + hide, + model, + maximized, + auto_collapse, + continue_move, + focus, + sizes, + rtl, + rail_footer, + top_inset, + } = *input; let shell = body.shell(); let mut memory = body.memory::>().clone(); let protected = |panel: &P| rights.get(panel).is_some_and(|rights| rights.is_protected()); @@ -712,6 +743,7 @@ fn show_area( bounds, auto_collapse, continue_move, + focus, rtl, floated: current.floated(), rail_footer, @@ -1058,6 +1090,8 @@ impl Pass<'_, P> { return Some(height + grip); } let shown = &members[active]; + // The host's focus request, when it names a panel of this group. + let focus = scene.focus.filter(|panel| members.contains(panel)); let words = body.words::(); let ctx = ui.ctx().clone(); let panels = RefCell::new(&mut *self.panels); @@ -1148,7 +1182,11 @@ impl Pass<'_, P> { let taken = in_room(ui, view, |ui| panels.borrow_mut().group_view(members, active, ui, GroupAt::Show(view), body)); match taken { Some(_) => None, - None => Some(make(active, look.minimized).show_at(ui, rect, |ui, body| panel_body(&panels, shown, ui, body)).output), + None => { + let group = make(active, look.minimized); + let group = if focus.is_some() { group.take_focus() } else { group }; + Some(group.show_at(ui, rect, |ui, body| panel_body(&panels, shown, ui, body)).output) + } } }) .inner; @@ -1188,10 +1226,14 @@ impl Pass<'_, P> { let torn = torn.map(|(panel, pointer)| { float_drop(&DragUnit::Panel(panel.clone()), scene.space, pointer, Some(fitted(ui, panel)), None, scene.rtl).action }); - let picked = output.picked.and_then(|key| scene.panel(key)); + let clicked = output.picked.and_then(|key| scene.panel(key)); + if let Some(panel) = clicked { + self.report.picked = Some(panel.clone()); + } + // The host's focus request picks its panel's tab as a click does; the user's click of the pass wins. + let picked = clicked.or(focus.filter(|panel| *panel != shown)); if let Some(panel) = picked { self.report.actions.push(Action::activate(panel.clone())); - self.report.picked = Some(panel.clone()); } // A tab picked in this pass is the group's shown panel from now on, though this pass drew the one before. if focus_within(ui.ctx(), output.response.id) { diff --git a/crates/ui-patterns/tests/it/docking/focus.rs b/crates/ui-patterns/tests/it/docking/focus.rs new file mode 100644 index 000000000..69d0dff45 --- /dev/null +++ b/crates/ui-patterns/tests/it/docking/focus.rs @@ -0,0 +1,97 @@ +//! The host's focus request: `DockArea::focus` moves key focus to the group that shows a panel, as a click on its tab +//! does. + +use super::Numbered; +use craft_behavior::dock::{Action, Layout, Node, SplitSize, Workspace}; +use craft_behavior::{Axis, PartKey}; +use craft_ui::contract::{Bind, BodyCx, Tracked}; +use craft_ui::docking::{DockConfig, DockPanels}; +use craft_ui::patterns::dock::DockArea; +use craft_ui::testing::{Fixture, trial}; +use egui::{Key, Modifiers, Ui, Vec2}; +use std::cell::{Cell, RefCell}; +use std::rc::Rc; +use std::sync::Arc; + +struct Panels; + +impl DockPanels for Panels { + fn body(&mut self, _: &usize, _: &mut Ui, _: &mut BodyCx<'_>) {} +} + +/// What one pass of the area reported. +#[derive(Debug)] +struct Seen { + focused: Option, + picked: Option, + actions: Vec>, +} + +/// The fixture, the host's focus request for the next pass, and each pass's report. +type TwoGroups = (Fixture, Rc>>, Rc>>); + +/// A dock of group A (panel 0) beside group B (panels 1 and 2, 1 shown) with `config`; the host's focus request for the +/// next pass goes in the cell, and each pass's report is pushed to the list. +fn two_groups(config: DockConfig) -> TwoGroups { + let root = Node::split(Axis::X, SplitSize::Ratio(0.5), Node::tabs(vec![0], 0), Node::tabs(vec![1, 2], 0)); + let mut workspace = Arc::new(Workspace::new(Layout::new(Some(root), Vec::new()))); + let request = Rc::new(Cell::new(None)); + let seen = Rc::new(RefCell::new(Vec::new())); + let (asked, reported, specs) = (Rc::clone(&request), Rc::clone(&seen), Tracked::new(Numbered)); + let mut fixture = Fixture::new(move |ui| { + let mut panels = Panels; + let mut area = DockArea::new("Dock", Bind::bound(&mut workspace), &specs, &mut panels, config.clone()); + if let Some(panel) = asked.take() { + area = area.focus(panel); + } + let output = area.show(ui); + reported.borrow_mut().push(Seen { focused: output.focused, picked: output.picked, actions: output.actions }); + }); + fixture.room(Vec2::new(1200.0, 600.0)).settle(); + (fixture, request, seen) +} + +/// With the user's focus on group A's tabs, the host's request for panel 2 of group B moves key focus to B's stop (the +/// one a click on B's tabs focuses) and picks 2's tab; a key there reaches B. Guards against a request that picked the +/// tab but left key focus, and so the keys, on the tabs the user clicked last. +#[trial] +fn the_hosts_focus_request_moves_key_focus_to_the_panels_group_and_picks_its_tab() { + let (mut fixture, request, seen) = two_groups(DockConfig::default()); + let tab = |fixture: &Fixture, name: &str| fixture.by_label(name).expect("a group's tab"); + let b_tab = tab(&fixture, "Panel 1"); + fixture.click(&b_tab).idle(1); + let b_stop = fixture.focused_stop().expect("a click on B's tab focuses B's stop"); + let a_tab = tab(&fixture, "Panel 0"); + fixture.click(&a_tab).idle(1); + assert_ne!(fixture.focused_stop(), Some(b_stop), "a click on A's tab moves focus to A's stop"); + assert_eq!(seen.borrow().last().and_then(|seen| seen.focused), Some(0), "A holds the focus"); + seen.borrow_mut().clear(); + + request.set(Some(2)); + fixture.idle(2); + assert_eq!(fixture.focused_stop(), Some(b_stop), "the request moves key focus to B's stop"); + { + let seen = seen.borrow(); + assert!(seen.iter().any(|pass| pass.actions.contains(&Action::activate(2))), "the request picks 2's tab: {seen:?}"); + assert!(seen.iter().all(|pass| pass.picked.is_none()), "the host's pick is not the user's: {seen:?}"); + assert_eq!(seen.last().and_then(|pass| pass.focused), Some(2), "B holds the focus, with 2 shown: {seen:?}"); + } + seen.borrow_mut().clear(); + + fixture.key(Key::ArrowLeft, Modifiers::NONE).idle(1); + fixture.key(Key::Space, Modifiers::NONE).idle(1); + let picked = seen.borrow().iter().find_map(|pass| pass.picked); + assert!(matches!(picked, Some(1 | 2)), "the keys reach B's tabs, not A's: {picked:?}"); +} + +/// A titled header has no tab row: the request moves key focus to its drag band, the group's stop, and the group +/// holds the focus. Guards against a request that a titled group dropped. +#[trial] +fn the_hosts_focus_request_focuses_a_titled_groups_drag_band() { + let (mut fixture, request, seen) = two_groups(DockConfig::default().titled_headers(true)); + request.set(Some(2)); + fixture.idle(2); + let part = fixture.focused_stop().and_then(|(_, part)| part).map(|part| part.key); + assert_eq!(part, Some(PartKey::new("grip")), "the drag band holds key focus"); + assert_eq!(seen.borrow().last().and_then(|pass| pass.focused), Some(2), "B holds the focus, with 2 shown"); +} diff --git a/crates/ui-patterns/tests/it/docking/mod.rs b/crates/ui-patterns/tests/it/docking/mod.rs index 17d129e89..14fccdc59 100644 --- a/crates/ui-patterns/tests/it/docking/mod.rs +++ b/crates/ui-patterns/tests/it/docking/mod.rs @@ -6,6 +6,7 @@ use craft_ui::testing::style::{tokens, with_cx}; pub(crate) mod render; +pub(crate) mod focus; pub(crate) mod panel_menu; pub(crate) mod runs;