mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
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
This commit is contained in:
@@ -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<RowPressed>,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
@@ -169,6 +169,9 @@ pub(super) struct Drags<K> {
|
||||
pub(super) slow: Option<SlowClick<K>>,
|
||||
/// 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<K>,
|
||||
}
|
||||
|
||||
/// 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<K> Default for Drags<K> {
|
||||
marquee: None,
|
||||
slow: None,
|
||||
part_press: None,
|
||||
menu: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -440,6 +440,7 @@ impl<C: Collection> ShapeHooks<C> 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<C: Collection> ShapeHooks<C> 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<C: Collection> ShapeHooks<C> 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<C: Collection> ShapeHooks<C> 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));
|
||||
|
||||
@@ -168,7 +168,7 @@ impl<C: Collection> 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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -205,7 +205,7 @@ impl<C: Collection> 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<C: Collection> 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<C: Collection> 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<C::Key>) {
|
||||
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<ItemOpener>) -> Vec<(ItemOpener, egui::Response)> {
|
||||
|
||||
@@ -535,9 +535,10 @@ pub trait ItemOutput<E>: 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<egui::Pos2> {
|
||||
/// 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<egui::Rect> {
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -240,6 +240,9 @@ pub struct ItemFacts {
|
||||
/// Where the collection holds a press on the row (`RowState::pressed`).
|
||||
#[doc(hidden)]
|
||||
pub pressed: Option<crate::contract::RowPressed>,
|
||||
/// The row's menu is open (`RowState::menu_open`).
|
||||
#[doc(hidden)]
|
||||
pub menu_open: bool,
|
||||
}
|
||||
|
||||
impl Scoped {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<RowPressed>,
|
||||
/// 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<LeadPaintFn<'a>>,
|
||||
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<ThumbPaint<'a>>,
|
||||
pub(super) extras: Vec<Thumb<'a>>,
|
||||
/// 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<bool>,
|
||||
pub(super) event: Option<RowEvent>,
|
||||
pub(super) open: Option<OpenChange<()>>,
|
||||
/// The row's menu button was clicked: where the menu hangs.
|
||||
pub(super) menu: Option<egui::Pos2>,
|
||||
/// The row's menu button was clicked: its rect, where the menu hangs.
|
||||
pub(super) menu: Option<Rect>,
|
||||
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
|
||||
|
||||
@@ -69,7 +69,7 @@ pub struct RowOutput {
|
||||
/// `CollectionEvent::Open`, as it does the keys.
|
||||
pub open: Option<OpenChange<()>>,
|
||||
/// The row's menu button was clicked: where the menu hangs, in the row's layer.
|
||||
pub menu: Option<egui::Pos2>,
|
||||
pub menu: Option<Rect>,
|
||||
/// Where the label and detail are drawn, the full height of the row.
|
||||
pub label: Rect,
|
||||
}
|
||||
@@ -91,7 +91,7 @@ impl ItemOutput<RowEvent> for RowOutput {
|
||||
self.open.clone()
|
||||
}
|
||||
|
||||
fn menu_request(&self) -> Option<egui::Pos2> {
|
||||
fn menu_request(&self) -> Option<Rect> {
|
||||
self.menu
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -187,6 +187,7 @@ impl ListRow<'_> {
|
||||
if self.pressed.is_none() {
|
||||
self.pressed.clone_from(&facts.pressed);
|
||||
}
|
||||
self.menu_open |= facts.menu_open;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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::<usize>::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::<usize>);
|
||||
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)
|
||||
|
||||
@@ -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::<usize>));
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user