diff --git a/crates/ui-patterns/src/patterns/dock/panes.rs b/crates/ui-patterns/src/patterns/dock/panes.rs index 0cb160961e..28a51f2d27 100644 --- a/crates/ui-patterns/src/patterns/dock/panes.rs +++ b/crates/ui-patterns/src/patterns/dock/panes.rs @@ -25,7 +25,7 @@ use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; use craft_behavior::dock::{ZoneSize, zones}; use craft_behavior::{DockSide, GesturePhase, Metrics, Sense}; use egui::{Id, Pos2, Rect, Response, Ui, Vec2}; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::fmt::Debug; use std::hash::Hash; @@ -105,7 +105,9 @@ pub enum PaneRequest

{ Close(P), /// The user moved the pane to a new place. Place(P, PanePlace), - /// The user moved or resized the floating frame to this rect. It arrives every pass a gesture changes the rect. + /// The user moved or resized the floating frame to this rect. It arrives every pass a gesture changes the rect, + /// and after the `Place` of a Float with the rect the pane floats in (its kept float rect, else where the area + /// places it). FloatRect(P, Rect), /// The user minimized the floating pane (`true`) or restored it (`false`). Minimize(P, bool), @@ -329,8 +331,32 @@ fn outermost

(panes: &[Shown

], policy: SidePolicy, edge: PaneEdge, dragged: } } -/// The requests of a pane command acting on `pane`. -fn command_requests(panes: &[Shown

], policy: SidePolicy, pane: PanelKey, command: PaneCommands) -> Vec> { +/// Where floating panes are placed: the shell's float bounds and the cascade step of a pane with no float rect. +#[derive(Clone, Copy, Debug)] +struct FloatPlace { + bounds: Rect, + inset: f32, +} + +/// The rect `pane` floats in: its state's float rect, else its float size at the bounds' start, one cascade step on +/// for each floating pane before it. The area draws a floating pane there, and a Float reports it. +fn float_rect_of

(panes: &[Shown

], pane: PanelKey, float: FloatPlace) -> Rect { + let Some(at) = panes.iter().position(|s| s.key == pane) else { return Rect::from_min_size(float.bounds.min, Vec2::ZERO) }; + let shown = &panes[at]; + shown.state.float_rect.unwrap_or_else(|| { + let before = panes[..at].iter().filter(|s| s.state.place == PanePlace::Floating).count(); + Rect::from_min_size(float.bounds.min + Vec2::splat(before as f32 * float.inset), shown.spec.float_size) + }) +} + +/// The requests of a pane command acting on `pane`. `float` places a pane the command floats. +fn command_requests( + panes: &[Shown

], + policy: SidePolicy, + pane: PanelKey, + command: PaneCommands, + float: Option, +) -> Vec> { let Some(shown) = panes.iter().find(|s| s.key == pane) else { return Vec::new() }; let dock = |edge| dock_requests(panes, pane, edge, outermost(panes, policy, edge, pane)); match command { @@ -338,8 +364,14 @@ fn command_requests(panes: &[Shown

], policy: SidePolicy, pane: Pane PaneCommands::MoveToEnd if shown.spec.movable => dock(PaneEdge::End), PaneCommands::MoveToTop if shown.spec.movable => dock(PaneEdge::Top), PaneCommands::MoveToBottom if shown.spec.movable => dock(PaneEdge::Bottom), + // The rect it will float in goes with the place, so an app that keeps the float rect has it from the Float on, + // not only after a move or resize. PaneCommands::Float if shown.spec.movable && shown.state.place != PanePlace::Floating => { - vec![PaneRequest::Place(shown.pane.clone(), PanePlace::Floating)] + let mut requests = vec![PaneRequest::Place(shown.pane.clone(), PanePlace::Floating)]; + if let Some(float) = float { + requests.push(PaneRequest::FloatRect(shown.pane.clone(), float_rect_of(panes, pane, float))); + } + requests } PaneCommands::Close if shown.spec.closable => vec![PaneRequest::Close(shown.pane.clone())], _ => Vec::new(), @@ -640,6 +672,8 @@ struct Pass<'a, P> { content: RefCell<&'a mut dyn PaneContent

>, sink: RefCell>>, focused: RefCell>, + /// Where floating panes go, set at the start of the area's body. + float: Cell>, } /// A drop taker with the signature a container's `update` gives it. @@ -665,8 +699,15 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> PaneArea<'a, P> { }) .collect(); let rtl = crate::runtime::env::Env::of(ui).direction.is_rtl(); - let pass = - Pass { panes: &shown, policy, rtl, content: RefCell::new(&mut *content), sink: RefCell::new(Vec::new()), focused: RefCell::new(None) }; + let pass = Pass { + panes: &shown, + policy, + rtl, + content: RefCell::new(&mut *content), + sink: RefCell::new(Vec::new()), + focused: RefCell::new(None), + float: Cell::new(None), + }; let root = DockRoot::new(|cx| { let band = cx.style.sizes().edge_band; let target = PaneDrop { panes: pass.panes, policy, rtl, zone: Zone::Edges { bounds: cx.rect, band } }; @@ -678,6 +719,8 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> PaneArea<'a, P> { let Some(sizes) = cx.style::>>>().map(|style| *style.sizes()) else { return }; let (docks, floats) = cx.shell().as_ref().map_or((true, true), |shell| (shell.dock_visible(), shell.floating_visible())); let bounds = cx.shell().map_or(cx.rect, |shell| shell.float_bounds()); + let float = FloatPlace { bounds, inset: sizes.cascade_inset }; + pass.float.set(Some(float)); let mut order = [PaneEdge::Bottom, PaneEdge::Top, PaneEdge::Start, PaneEdge::End]; if rtl { order.swap(2, 3); @@ -698,12 +741,8 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> PaneArea<'a, P> { } centre(ui, cx); if floats { - let inset = sizes.cascade_inset; - let mut cascade = 0.0; for pane in shown.iter().filter(|s| s.state.place == PanePlace::Floating) { - let rect = pane.state.float_rect.unwrap_or_else(|| Rect::from_min_size(bounds.min + Vec2::splat(cascade), pane.spec.float_size)); - cascade += inset; - show_floating(ui, &pass, pane, rect, bounds); + show_floating(ui, &pass, pane, float_rect_of(&shown, pane.key, float), bounds); } } let held = pass.focused.borrow().as_ref().map(PanelKey::of); @@ -818,7 +857,7 @@ fn queue_action(action: Option, pass: &Pass<'_, P>, pane (Some(HeaderAction::Float), _) => PaneCommands::Float, _ => return, }; - pass.sink.borrow_mut().extend(command_requests(pass.panes, pass.policy, pane.key, command)); + pass.sink.borrow_mut().extend(command_requests(pass.panes, pass.policy, pane.key, command, pass.float.get())); } /// Whether the pane offers the move `command`: a movable pane offers Float and a Move to an edge it may dock at. @@ -840,7 +879,7 @@ fn take_commands(cx: &mut BodyCx<'_>, pass: &Pass<'_, P>, pane: &Shown } cx.publish(PaneCommands::Close, CommandState::new().enabled(pane.spec.closable)); while let Some((command, _)) = cx.take_command::() { - pass.sink.borrow_mut().extend(command_requests(pass.panes, pass.policy, pane.key, command)); + pass.sink.borrow_mut().extend(command_requests(pass.panes, pass.policy, pane.key, command, pass.float.get())); } } diff --git a/crates/ui-patterns/src/patterns/panels/palette.rs b/crates/ui-patterns/src/patterns/panels/palette.rs index 2334b2df52..e60ca671a3 100644 --- a/crates/ui-patterns/src/patterns/panels/palette.rs +++ b/crates/ui-patterns/src/patterns/panels/palette.rs @@ -95,7 +95,8 @@ impl crate::contract::Outcome for PaletteOutput { /// runs, so its own modal layer blocks none. A disabled command is listed and does not run. /// - Faces: [`PaletteFace::Dialog`] is a centred modal box. [`PaletteFace::InPlace`] is a search box where it is laid /// out, showing the `placeholder`; it opens to the field with the key focus in the pass that opens it and lists the -/// results in a popup under the field. +/// results in a popup under the field. The in-place field is the palette's face, so it is named by the palette's +/// `label`, closed and open; the dialog face's field, inside a box named `label`, keeps the generic search name. /// - Empty query: with `suggested`, its heading and those commands; else the commands last run, newest first (the /// `recents` binding), else the first entries. /// - Fallback: with `fallback`, a last row follows the matches of a query, or stands alone when nothing matches. Picking @@ -227,7 +228,7 @@ impl<'a> CommandPalette<'a> { // The closed face is the search field's own look at its own size, so what sits beside it does not shift on open. let closed_face = !is_open && clicked( - &TextField::search(words.palette_search.clone(), Bind::given(String::new())) + &TextField::search(label.clone(), Bind::given(String::new())) .placeholder(face_hint.clone()) .id(field_id) .show(ui) @@ -252,7 +253,7 @@ impl<'a> CommandPalette<'a> { focus::request(&ctx, FocusTarget::Id(field_id)); } let session = inner.memory::(); - let field = TextField::search(words.palette_search.clone(), Bind::bound(&mut session.query)) + let field = TextField::search(label.clone(), Bind::bound(&mut session.query)) .placeholder(face_hint.clone()) .id(field_id) .show(ui) diff --git a/crates/ui-patterns/tests/it/docking/panes.rs b/crates/ui-patterns/tests/it/docking/panes.rs index 3690095c83..a60656a0dd 100644 --- a/crates/ui-patterns/tests/it/docking/panes.rs +++ b/crates/ui-patterns/tests/it/docking/panes.rs @@ -171,3 +171,44 @@ fn escape_during_a_pane_title_drag_cancels_it_and_reaches_nothing_else() { assert_eq!(bed.state.get().float_rect, Some(FLOAT), "the pane is back where the drag began"); assert_eq!(bed.window_cancels.get(), 0, "the Escape went to the drag alone"); } + +/// The float rects a pane's requests since the last call reported. +fn float_rects(requests: &[PaneRequest]) -> Vec { + requests + .iter() + .filter_map(|request| match request { + PaneRequest::FloatRect(_, rect) => Some(*rect), + _ => None, + }) + .collect() +} + +/// Float reports the rect the pane floats in with its place, so an app that keeps the float rect (as WordCraft keeps +/// the Styles pane's) has it from the Float on, not only after a move or resize: a pane with no kept rect reports +/// where the area draws it, one with a kept rect reports that rect. +#[trial] +fn float_reports_the_rect_the_pane_floats_in() { + let mut bed = bed(PaneEdges::ALL, PaneState::docked(PaneEdge::Start, 200.0)); + bed.take(); + assert!(fire(bed.fixture.ctx(), PaneCommands::Float, FireSource::Key), "Float runs"); + bed.fixture.idle(2); + let requests = bed.take(); + assert!(requests.contains(&PaneRequest::Place(0, PanePlace::Floating)), "{requests:?}"); + let reported = float_rects(&requests); + assert_eq!(reported.len(), 1, "the Float reports one rect: {requests:?}"); + assert_eq!(reported[0].size(), FLOAT.size(), "at the pane's float size"); + assert_eq!(bed.state.get().float_rect, Some(reported[0]), "the app kept it by applying the requests"); + let frame = bed.fixture.by_label("Pane 0").expect("the floating pane").rect; + assert!((frame.min - reported[0].min).length() < 1.0, "the pane floats where the rect says: {frame:?} against {:?}", reported[0]); + + // A docked pane that keeps a float rect reports that rect. + let mut start = PaneState::docked(PaneEdge::Start, 200.0); + start.float_rect = Some(FLOAT); + let mut kept = self::bed(PaneEdges::ALL, start); + kept.take(); + assert!(fire(kept.fixture.ctx(), PaneCommands::Float, FireSource::Key), "Float runs"); + kept.fixture.idle(2); + assert_eq!(float_rects(&kept.take()), vec![FLOAT], "the kept rect is reported"); + let frame = kept.fixture.by_label("Pane 0").expect("the floating pane").rect; + assert!((frame.min - FLOAT.min).length() < 1.0, "the pane floats in its kept rect: {frame:?}"); +} diff --git a/crates/ui-patterns/tests/it/panels/palette.rs b/crates/ui-patterns/tests/it/panels/palette.rs index 73fbdc50db..1e3f38a41f 100644 --- a/crates/ui-patterns/tests/it/panels/palette.rs +++ b/crates/ui-patterns/tests/it/panels/palette.rs @@ -276,3 +276,19 @@ fn a_palette_opened_in_a_modal_dialog_lists_what_the_dialog_blocks_as_disabled() assert!(disabled(&fixture, "Rotate"), "the dialog under the palette blocks a default-rule app command"); assert!(!disabled(&fixture, "Zoom"), "a command that runs whatever is open stays enabled"); } + +/// The in-place face's field is the palette's face in the window (a title-bar search box), so a screen reader names it +/// by the palette's label, closed and open; the generic "Search commands" would drop the app's own name for it. +#[trial] +fn the_in_place_field_is_named_by_the_palettes_label_closed_and_open() { + let (mut fixture, open, ..) = faced(PaletteFace::InPlace, trial_entries(), None, None, false); + fixture.settle(); + for state in ["closed", "open"] { + let tree = fixture.tree(); + let names: Vec = + fixture.by_role(Role::TEXT_INPUT).iter().map(|field| tree.name_of(field.node).unwrap_or_default().to_owned()).collect(); + assert_eq!(names, vec!["Commands".to_owned()], "{state}: one search field, named by the palette's label"); + open.set(true); + fixture.settle(); + } +} diff --git a/crates/ui-widgets/src/buttons/split_button.rs b/crates/ui-widgets/src/buttons/split_button.rs index 59f212c0bc..0e88ffb3a5 100644 --- a/crates/ui-widgets/src/buttons/split_button.rs +++ b/crates/ui-widgets/src/buttons/split_button.rs @@ -2,7 +2,7 @@ use super::face::{self, Arrangement, FaceLook, FaceSpec}; use crate::contract::{ - Access, BodyCx, Common, Component, Cx, Entry, Focus as _, Hover, MeasureCx, Name, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, + Access, Bind, BodyCx, Common, Component, Cx, Entry, Focus as _, Hover, MeasureCx, Name, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, Props, Role, ShowContainer as _, Styled as _, WordSetCx as _, }; use crate::menu::{self, Menu}; @@ -231,16 +231,24 @@ impl Component for SplitButton<'_, T> { /// The menu: one `MenuItem` row per entry, a rule for a separator, or the caller's body. Running an entry's row /// reports its value and closes the menu; a disabled entry takes neither focus-run nor a pick. A body's rows run - /// their own commands, which close the layer as any row's run does. + /// their own commands, which close the layer as any row's run does; the body's `request_close` (a palette's pick) + /// closes the layer too. fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) { let popup = cx.popup("menu"); let name = cx.label(); let entries = match &mut self.menu { SplitMenu::Entries(entries) => *entries, SplitMenu::Body(body) => { - ui.show(popup, menu::layer(false), |ui, _| { - let _ = Menu::inline(name.clone()).show(ui, |ui, cx| body(ui, cx)); + // The body's `request_close` (a swatch pick) and a row's run close the inline menu; it is open while + // the popup is, so its close is the popup's. Its own open slot would stay closed and leave the layer + // open around an empty body. Its focus scope ignores Escape, so the binding takes no Escape from the + // layer's own rule. + let closed = ui.show(popup, menu::layer(false), |ui, _| { + Menu::inline(name.clone()).open(Bind::given(true)).show(ui, |ui, cx| body(ui, cx)).output.closed }); + if closed.flatten().is_some() { + popup.close(cx); + } return; } }; diff --git a/crates/ui-widgets/tests/it/buttons.rs b/crates/ui-widgets/tests/it/buttons.rs index 3f5b256aa9..84a3cbc443 100644 --- a/crates/ui-widgets/tests/it/buttons.rs +++ b/crates/ui-widgets/tests/it/buttons.rs @@ -540,3 +540,38 @@ fn a_side_menus_selected_row_shows_its_hover_band() { assert_ne!(on.rest, fill.off.rest, "the shown page's row is marked"); assert!(plain.expect("a ghost button has a surface").fill.on.is_none(), "a selected ghost button elsewhere keeps its look"); } + +/// A split button's body that closes itself (a colour palette's swatch pick) closes the split's menu layer: before, +/// the close reached only the inline menu inside the layer, which stayed open around an empty body. Opened again, the +/// body shows its rows. +#[trial] +fn a_split_bodys_request_close_closes_the_menu_layer_and_it_opens_again_with_its_body() { + let picked = Rc::new(Cell::new(0)); + let count = Rc::clone(&picked); + let mut fixture = Fixture::new(move |ui| { + let count = Rc::clone(&count); + let _ = SplitButton::::body("Shading", move |ui, cx| { + if craft_ui::contract::clicked(&Button::new("Red").show(ui).response) { + count.set(count.get() + 1); + cx.request_close(); + } + }) + .arrow_label("Shading colours") + .show(ui); + }); + fixture.settle(); + let open_menu = |fixture: &mut Fixture| { + let arrow = fixture.by_label("Shading colours").expect("the arrow part"); + fixture.click(&arrow).settle(); + assert!(!fixture.by_role(Role::MENU).is_empty(), "the arrow opened the menu"); + }; + open_menu(&mut fixture); + let swatch = fixture.by_label("Red").expect("the body's swatch is shown"); + fixture.click(&swatch).settle(); + assert_eq!(picked.get(), 1, "the swatch ran once"); + assert!(fixture.by_role(Role::MENU).is_empty(), "the body's close closed the menu"); + // An arrow click on a layer left open around an empty body would only close it: the layer itself must have closed + // for the click to open the menu with its body. + open_menu(&mut fixture); + assert!(fixture.by_label("Red").is_some(), "opened again, the menu shows its body"); +}