From 47ae4415b228ead1848cdb9bd50c208a3d3ddfc4 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 00:23:49 +0000 Subject: [PATCH] Row menu: the More button's click makes the context request a host's Menu::context on the list opens from, as the Menu key does, and the row whose menu the collection asked for keeps its button shown while that menu is open (RowState::menu_open); a List story shows row_menu with the host's menu, whose Rename opens the row's field --- .../src/contract/collection/props.rs | 3 ++ crates/ui-contract/src/contract/items/drag.rs | 4 ++ .../ui-contract/src/contract/items/hooks.rs | 13 +++-- crates/ui-contract/src/contract/items/keys.rs | 2 +- .../ui-contract/src/contract/items/measure.rs | 1 + .../ui-contract/src/contract/items/members.rs | 10 ++-- .../ui-contract/src/contract/items/press.rs | 11 +++- crates/ui-contract/src/contract/values.rs | 7 +-- crates/ui-contract/src/runtime/env.rs | 3 ++ crates/ui-contract/src/testing/story.rs | 1 + crates/ui-widgets/src/list/row.rs | 14 +++-- crates/ui-widgets/src/list/row/output.rs | 4 +- crates/ui-widgets/src/list/row/paint.rs | 1 + crates/ui-widgets/src/list/row/placement.rs | 1 + crates/ui-widgets/src/list/row/show.rs | 2 +- crates/ui-widgets/src/list/view_stories.rs | 38 +++++++++++++- crates/ui-widgets/tests/it/list.rs | 51 +++++++++++++++++++ 17 files changed, 136 insertions(+), 30 deletions(-) diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index 61e5dc235..a506655ba 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -585,4 +585,7 @@ pub struct RowState { pub hovered: bool, /// The collection holds a press on the row, which the item shows on the part it acts on. pub pressed: Option, + /// The collection asked for this row's menu and a menu hung from the collection is open: the row keeps its menu + /// button shown. + pub menu_open: bool, } diff --git a/crates/ui-contract/src/contract/items/drag.rs b/crates/ui-contract/src/contract/items/drag.rs index 6e990068c..b685f2b02 100644 --- a/crates/ui-contract/src/contract/items/drag.rs +++ b/crates/ui-contract/src/contract/items/drag.rs @@ -169,6 +169,9 @@ pub(super) struct Drags { pub(super) slow: Option>, /// The row a press an item part won landed on, and whether the row held the lead when the press began. pub(super) part_press: Option<(K, bool)>, + /// The row the collection last asked a menu for: it shows as the menu's row while a menu hung from the collection + /// is open. + pub(super) menu: Option, } /// A press that opens a rename when it ends as a click on the item's label and no second press follows within the @@ -198,6 +201,7 @@ impl Default for Drags { marquee: None, slow: None, part_press: None, + menu: None, } } } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index bee5686e4..574ec58bd 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -440,6 +440,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { // The pointer counts as over a row only while the sensing pass puts it on this collection, so a popup or another // widget above it takes the hover as it does for the rows' parts. let hover_at = crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id)).hover.and(ctx.pointer_latest_pos()); + let menu_open = self.drags.menu.is_some() && crate::contract::overlay::container_open::opened_from(&ctx, cx.core.id); let place_rows = |ui: &mut Ui| { // The items of a collection whose items are members of its run share one `Ui` under the collection's env. let members_ui = C::MEMBERS.then(|| { @@ -473,6 +474,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { open: self.source.has_children(at) && self.source.open(at), hovered: hover_at.is_some_and(|pos| row.contains(pos)), pressed: self.pressed.as_ref().filter(|(held, _)| *held == at).map(|(_, pressed)| pressed.clone()), + menu_open: self.drags.menu.as_ref() == Some(&key) && menu_open, ..RowState::default() }; let mut flags = crate::style::Flags::empty(); @@ -551,6 +553,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { toggles: toggle_keys, toggle_keys: self.header.toggle_keys(), pressed: state.pressed.clone(), + menu_open: state.menu_open, })); }), ); @@ -601,13 +604,9 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { if let Some(ask) = out.open_request() { self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all })); } - if let Some(at) = out.menu_request() { - self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { - key: key.clone(), - at: self.on_screen(at), - column: None, - part: None, - })); + if let Some(rect) = out.menu_request() { + crate::runtime::keys::request_context(&ctx, id, rect); + self.ask_menu(ContextMenuRequest { key: key.clone(), at: self.on_screen(rect.left_bottom()), column: None, part: None }); } if let Some(event) = out.into_event() { self.events.push(CollectionEvent::Item(key.clone(), event)); diff --git a/crates/ui-contract/src/contract/items/keys.rs b/crates/ui-contract/src/contract/items/keys.rs index f0cbb89df..e553e76e8 100644 --- a/crates/ui-contract/src/contract/items/keys.rs +++ b/crates/ui-contract/src/contract/items/keys.rs @@ -168,7 +168,7 @@ impl ItemsHooks<'_, '_, C> { let rect = self.screen(layout, at.rect, row, at.offset, at.rtl); crate::runtime::keys::request_context(&cx.core.ctx, cx.core.id, rect); let place = self.on_screen(rect.left_bottom()); - self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { key: self.source.key(row), at: place, column: None, part: None })); + self.ask_menu(ContextMenuRequest { key: self.source.key(row), at: place, column: None, part: None }); } } diff --git a/crates/ui-contract/src/contract/items/measure.rs b/crates/ui-contract/src/contract/items/measure.rs index b9f12f8eb..034f840e7 100644 --- a/crates/ui-contract/src/contract/items/measure.rs +++ b/crates/ui-contract/src/contract/items/measure.rs @@ -113,6 +113,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { toggles: toggles.clone(), toggle_keys: Vec::new(), pressed: None, + menu_open: false, }; let installed = Env::install_for( ui, diff --git a/crates/ui-contract/src/contract/items/members.rs b/crates/ui-contract/src/contract/items/members.rs index ddaae2223..5d7ae8550 100644 --- a/crates/ui-contract/src/contract/items/members.rs +++ b/crates/ui-contract/src/contract/items/members.rs @@ -86,13 +86,9 @@ impl<'r, C: Collection> ItemsHooks<'_, 'r, C> { if let Some(ask) = output.open_request() { self.events.push(CollectionEvent::Open(OpenChange { key: key.clone(), open: ask.open, all: ask.all })); } - if let Some(at) = output.menu_request() { - self.events.push(CollectionEvent::ContextMenu(ContextMenuRequest { - key: key.clone(), - at: self.on_screen(at), - column: None, - part: None, - })); + if let Some(rect) = output.menu_request() { + crate::runtime::keys::request_context(ctx, id, rect); + self.ask_menu(ContextMenuRequest { key: key.clone(), at: self.on_screen(rect.left_bottom()), column: None, part: None }); } if let Some(event) = output.into_event() { self.events.push(CollectionEvent::Item(key.clone(), event)); diff --git a/crates/ui-contract/src/contract/items/press.rs b/crates/ui-contract/src/contract/items/press.rs index 3ac6f1ab4..23e57a865 100644 --- a/crates/ui-contract/src/contract/items/press.rs +++ b/crates/ui-contract/src/contract/items/press.rs @@ -205,7 +205,7 @@ impl ItemsHooks<'_, '_, C> { && let Some(row) = layout.hit(local(pos), offset).filter(|&row| self.source.enabled(row) && layout.shown(row)) { let request = self.menu_request(layout, Placement { rect, offset, rtl }, row, local(pos)); - self.events.push(CollectionEvent::ContextMenu(request)); + self.ask_menu(request); } match self.drags.slow.as_mut() { Some(slow) if click && slow.on_label => slow.released = Some(frame::clock(&ctx)), @@ -274,7 +274,7 @@ impl ItemsHooks<'_, '_, C> { self.move_lead(at, LeadSource::Pointer); let mut request = self.menu_request(layout, Placement { rect, offset, rtl }, at, local(pos)); request.part = hover.filter(|hit| hit.owner != cx.core.id).and_then(|hit| hit.part); - self.events.push(CollectionEvent::ContextMenu(request)); + self.ask_menu(request); } self.slow_click_due(comp, cx); } @@ -315,6 +315,13 @@ impl ItemsHooks<'_, '_, C> { ContextMenuRequest { key: self.source.key(row), at: place, column, part: None } } + /// Reports a request for the menu of a row; the row keeps its menu button shown while a menu hung from the + /// collection is open (`RowState::menu_open`). + pub(super) fn ask_menu(&mut self, request: ContextMenuRequest) { + self.drags.menu = Some(request.key.clone()); + self.events.push(CollectionEvent::ContextMenu(request)); + } + /// The response of each part an item asked to open a menu from (`ItemCx::part_opener`): the part's click this pass /// and its rect, the rect the press was hit-tested against. pub(super) fn item_openers(&self, ui: &Ui, ctx: &Context, id: Id, enabled: bool, openers: Vec) -> Vec<(ItemOpener, egui::Response)> { diff --git a/crates/ui-contract/src/contract/values.rs b/crates/ui-contract/src/contract/values.rs index 2c4e13a65..f8d53be9b 100644 --- a/crates/ui-contract/src/contract/values.rs +++ b/crates/ui-contract/src/contract/values.rs @@ -535,9 +535,10 @@ pub trait ItemOutput: Outcome { None } - /// The item asks for its menu from one of its parts (a row's "More" button): where the menu hangs, in the item's - /// layer. The collection reports it as `CollectionEvent::ContextMenu`, as it does a secondary click on the row. - fn menu_request(&self) -> Option { + /// The item asks for its menu from one of its parts (a row's "More" button): the part's rect, in the item's layer. + /// The collection makes the context request a `Trigger::Context` opener reads at that rect and reports + /// `CollectionEvent::ContextMenu` at its bottom start corner, as it does for the Menu key. + fn menu_request(&self) -> Option { None } diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 6c3e77d56..8cbb4fc44 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -240,6 +240,9 @@ pub struct ItemFacts { /// Where the collection holds a press on the row (`RowState::pressed`). #[doc(hidden)] pub pressed: Option, + /// The row's menu is open (`RowState::menu_open`). + #[doc(hidden)] + pub menu_open: bool, } impl Scoped { diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index f3c79722f..9c8097406 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -541,6 +541,7 @@ where toggles: Vec::new(), toggle_keys: Vec::new(), pressed: None, + menu_open: false, }; let installed = crate::runtime::env::Env::install_for( ui, diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index 45e3c2f02..0d443a497 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -150,6 +150,8 @@ pub struct ListRow<'a> { pub(super) renaming: bool, /// Where the collection holds a press on the row, from its row state: the toggle cell it acts on shows it. pub(super) pressed: Option, + /// The collection asked for this row's menu and it is open, from its row state: a revealed part stays shown. + pub(super) menu_open: bool, /// The thumbnails that drag instead of the row, each with what starts its drag session. pub(super) drags: Vec<(ThumbTarget, ThumbPayload)>, /// The icon or thumbnails come before the disclosure column instead of after it. @@ -197,10 +199,10 @@ pub struct ListRow<'a> { /// replaces the row's icon; thumbnails replace it in turn. #[prop(boxed = "FnOnce(&ContentPainterCx, Rect) + 'a", name = "lead_painter")] pub(super) lead_paint: Option>, - pub(super) thumb: ThumbSize, - pub(super) thumb_aspect: ThumbAspect, /// The leading text and the widest text its slot is sized for (`lead_text`). pub(super) lead_text: Option<(Label, Label)>, + pub(super) thumb: ThumbSize, + pub(super) thumb_aspect: ThumbAspect, pub(super) first: Option>, pub(super) extras: Vec>, /// The thumbnail the row's editing is aimed at: framed with corner brackets whether or not the row is selected. @@ -224,8 +226,8 @@ pub struct ListRow<'a> { pub(super) check: Option, pub(super) event: Option, pub(super) open: Option>, - /// The row's menu button was clicked: where the menu hangs. - pub(super) menu: Option, + /// The row's menu button was clicked: its rect, where the menu hangs. + pub(super) menu: Option, pub(super) label_rect: Rect, } @@ -251,6 +253,7 @@ impl<'a> ListRow<'a> { rule: false, renaming: false, pressed: None, + menu_open: false, drags: Vec::new(), leading_first: false, detail: None, @@ -269,10 +272,10 @@ impl<'a> ListRow<'a> { texts: Vec::new(), icon: None, lead_paint: None, + lead_text: None, thumb: ThumbSize::None, thumb_aspect: ThumbAspect::SQUARE, first: None, - lead_text: None, extras: Vec::new(), target: None, trailing: Vec::new(), @@ -310,6 +313,7 @@ impl<'a> ListRow<'a> { pub fn state(mut self, row: RowState) -> Self { self.renaming = row.renaming; self.pressed = row.pressed; + self.menu_open |= row.menu_open; self.striped = row.striped; self.highlight |= row.emphasized; self diff --git a/crates/ui-widgets/src/list/row/output.rs b/crates/ui-widgets/src/list/row/output.rs index 9f0c33532..04517032b 100644 --- a/crates/ui-widgets/src/list/row/output.rs +++ b/crates/ui-widgets/src/list/row/output.rs @@ -69,7 +69,7 @@ pub struct RowOutput { /// `CollectionEvent::Open`, as it does the keys. pub open: Option>, /// The row's menu button was clicked: where the menu hangs, in the row's layer. - pub menu: Option, + pub menu: Option, /// Where the label and detail are drawn, the full height of the row. pub label: Rect, } @@ -91,7 +91,7 @@ impl ItemOutput for RowOutput { self.open.clone() } - fn menu_request(&self) -> Option { + fn menu_request(&self) -> Option { self.menu } diff --git a/crates/ui-widgets/src/list/row/paint.rs b/crates/ui-widgets/src/list/row/paint.rs index 52a37d552..23867446c 100644 --- a/crates/ui-widgets/src/list/row/paint.rs +++ b/crates/ui-widgets/src/list/row/paint.rs @@ -44,6 +44,7 @@ impl ListRow<'_> { let shown = !*revealed || cx.response.hovered() || selected + || self.menu_open || cx.part_state(key).flags.contains(Flags::FOCUSED) || item_part_open(&cx.ctx, cx.id, key); if !shown { diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 1948f2fbf..7f6aa24d3 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -187,6 +187,7 @@ impl ListRow<'_> { if self.pressed.is_none() { self.pressed.clone_from(&facts.pressed); } + self.menu_open |= facts.menu_open; } } diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index 9ff1dfe51..a4d8a6c5a 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -320,7 +320,7 @@ impl Component for ListRow<'_> { let key = TRAILING.at(u64::from(part.0)); if let Some(source) = cx.part_activated(key) { if *part == TrailingPart::MENU { - self.menu = Some(cx.part(key).rect.left_bottom()); + self.menu = Some(cx.part(key).rect); } else { let clicks = part_clicks(source, cx.part(key).press.phase); let was_lead = match (source, &self.pressed) { diff --git a/crates/ui-widgets/src/list/view_stories.rs b/crates/ui-widgets/src/list/view_stories.rs index 526fbc146..b50d903b9 100644 --- a/crates/ui-widgets/src/list/view_stories.rs +++ b/crates/ui-widgets/src/list/view_stories.rs @@ -5,9 +5,10 @@ use super::{ EventLevel, EventList, EventLog, EventLogState, Finding, Findings, FindingsList, FindingsSummary, List, ListRow, LogEvent, Severity, UndoList, UndoMark, UndoRows, }; -use crate::contract::{Bind, Show as _, ShowCollection as _, WordSetCx as _}; +use crate::contract::{Bind, CollectionEvent, Entry, Show as _, ShowCollection as _, WordSetCx as _, request_rename}; +use crate::menu::Menu; use crate::testing::{Drive, StoryData, StoryOutput, story}; -use craft_behavior::{DisplayOrder, ItemSource}; +use craft_behavior::{DisplayOrder, ItemSource, Selection}; use egui::Ui; /// A list of names keyed by position. @@ -47,6 +48,39 @@ fn list_many(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { list(ui, 40) } +/// Rows with the "More" button of `row_menu`, shown on the hovered and the selected row: a click on it opens the host's +/// row menu, whose Rename opens the row's rename field. +#[story(List<'static, usize>, covers = [], drive = Drive::Never)] +fn list_row_menu(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let names = names(4); + let out = List::::new("Layers") + .rename(true) + .row_menu() + .selection(Bind::bound(data.value("selection", Selection::single(1_usize)))) + .show_items(ui, &Names(&names), &mut |key, _| ListRow::new(names[*key].clone())); + let menu_row = data.value("menu", None::); + for event in &out.events { + if let CollectionEvent::ContextMenu(request) = event { + *menu_row = Some(request.key); + } + } + if let Some(row) = *menu_row { + let entries = [ + Entry::new(String::from("rename"), "Rename"), + Entry::new(String::from("duplicate"), "Duplicate"), + Entry::new(String::from("delete"), "Delete"), + ]; + let menu = Menu::context("Layer", &out.response).pick(ui, &entries); + if menu.picked.as_deref() == Some("rename") { + request_rename(ui.ctx(), out.response.id, &row); + } + if menu.closed.is_some() { + *menu_row = None; + } + } + out.into() +} + #[story(List<'static, usize>, covers = [One], drive = Drive::Never)] fn list_one(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { list(ui, 1) diff --git a/crates/ui-widgets/tests/it/list.rs b/crates/ui-widgets/tests/it/list.rs index 1dfeaaada..b150ce51d 100644 --- a/crates/ui-widgets/tests/it/list.rs +++ b/crates/ui-widgets/tests/it/list.rs @@ -236,3 +236,54 @@ fn the_leading_text_takes_its_double_click_and_a_part_click_tells_whether_its_ro fixture.click_at(options).settle(); assert_eq!(was_lead(&seen), Some(true), "the press moved the lead there, so the next click finds it the lead"); } + +/// A click on a row's "More" button opens the host's context menu at the button; the button stays shown while the +/// menu is open with the pointer away from the row; the menu's Rename opens that row's rename field. +#[trial] +fn the_row_menu_opens_the_host_menu_keeps_its_button_and_renames_the_row() { + use craft_ui::contract::{Entry, Role}; + use craft_ui::menu::Menu; + let seen = Rc::new(RefCell::new(Vec::new())); + let held = seen.clone(); + let menu_row = Rc::new(RefCell::new(None::)); + let mut fixture = Fixture::new(move |ui| { + let keys = [0_usize, 1, 2]; + let source = keyed(&keys, |key: &usize| *key).with_text(|key| std::borrow::Cow::Borrowed(NAMES[*key])); + let out = List::new("Artboards").rename(true).row_menu().show_items(ui, &source, &mut |key, _| ListRow::new(NAMES[*key])); + for event in &out.events { + if let CollectionEvent::ContextMenu(request) = event { + *menu_row.borrow_mut() = Some(request.key); + } + } + let row = *menu_row.borrow(); + if let Some(row) = row { + let entries = [Entry::new(String::from("rename"), "Rename"), Entry::new(String::from("delete"), "Delete")]; + let menu = Menu::context("Artboard", &out.response).pick(ui, &entries); + if menu.picked.as_deref() == Some("rename") { + request_rename(ui.ctx(), out.response.id, &row); + } + if menu.closed.is_some() { + *menu_row.borrow_mut() = None; + } + } + held.borrow_mut().extend(out.events); + }); + fixture.room(Vec2::new(300.0, 200.0)); + fixture.settle(); + let button = fixture.by_label(&format!("More ({})", NAMES[0])).expect("the row has its menu button").center(); + fixture.click_at(button).settle(); + assert!(!fixture.by_role(Role::MENU).is_empty(), "the host's menu opens from the button"); + let away = fixture.by_label(NAMES[2]).expect("the last row shows").rect.center_bottom() + Vec2::new(0.0, 40.0); + fixture.frame(vec![craft_ui::testing::pointer::moved(away)]); + let shapes = fixture.frame_shapes(); + assert!(drawn_at(&shapes, button) > 0, "the button stays shown while its menu is open"); + let rename = fixture.by_label("Rename").expect("the menu has Rename"); + fixture.click(&rename).settle(); + assert!( + seen.borrow() + .iter() + .any(|event| matches!(event, CollectionEvent::Rename(EditEvent::Begin { before: Mixed::Same(rename), .. }) if rename.key == 0)), + "Rename opens the row's rename field: {:?}", + seen.borrow() + ); +}