mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
Merge pull request #3213 from storytold/design-port/pr-menus-popups
Menus and popups: keyboard opening and safer activation
This commit is contained in:
@@ -159,6 +159,11 @@ impl<'a> OverlayUi<'a> {
|
||||
};
|
||||
// An in-place editor is part of its owner: the owner's node reports no popup of it.
|
||||
let (kind, peers, reported) = (overlay.popup, overlay.opener.is_none(), overlay.role != LayerRole::InPlaceEditor);
|
||||
// A popup with no recorded opener hangs from this component: when the component goes disabled, its anchor is
|
||||
// gone, as for an explicit opener that goes disabled (`dismissal`).
|
||||
if open && peers && overlay.dismiss.anchor_gone && !self.resolved.enabled {
|
||||
crate::runtime::overlay::request_close(self.ctx, popup.id, CloseReason::AnchorGone);
|
||||
}
|
||||
let ctx = self.ctx.clone();
|
||||
with_popups(&ctx, |popups| {
|
||||
if reported {
|
||||
|
||||
@@ -1253,6 +1253,14 @@ pub fn keyboard_in_menu(ctx: &Context) -> bool {
|
||||
focused_rec(ctx).is_some_and(|rec| menu_scope(ctx, rec.scope).is_some()) && with(ctx, |state| state.visible)
|
||||
}
|
||||
|
||||
/// Whether the focused stop took the focus when its scope opened (a menu's first row as the menu showed) and has kept
|
||||
/// it since: no key, press or request (an AccessKit `Focus`, the app's [`request`]) has moved it. A menu row reads it
|
||||
/// to tell a key that reached the row its menu's opening focused, which may not be meant for it, from a key on a row
|
||||
/// the user or assistive technology chose.
|
||||
pub fn focus_came_with_open(ctx: &Context) -> bool {
|
||||
focused_rec(ctx).is_some_and(|rec| origin(ctx, &rec) == Some(FocusOrigin::Open))
|
||||
}
|
||||
|
||||
/// The innermost menu scope around the component whose body is running: the menu a row being shown sits in.
|
||||
#[doc(hidden)]
|
||||
pub fn running_menu(ctx: &Context) -> Option<Id> {
|
||||
|
||||
@@ -208,13 +208,17 @@ impl Group {
|
||||
|
||||
/// The innermost open `KeepAnchor` layer opened from the stop `opener`, the collection the layer names for its
|
||||
/// opener's list keys (`FocusPolicy::list`) and the layer's policy. `None` while no such layer is open, or it names no
|
||||
/// collection or one that registered no stop in the last pass (a list not shown leaves the keys with the opener).
|
||||
/// collection or one that registered no stop in the last pass (a list not shown leaves the keys with the opener). A
|
||||
/// layer playing its exit fade is closed: the opener keeps its keys, so an Enter right after Escape opens the list
|
||||
/// again instead of activating the row of the list that is leaving.
|
||||
pub(crate) fn dropdown_target(ctx: &Context, opener: StopKey) -> Option<(Id, Id, FocusPolicy)> {
|
||||
with(ctx, |state| {
|
||||
let layer =
|
||||
*state.last_scope_order.iter().rev().find(|id| {
|
||||
state.last_scopes.get(*id).is_some_and(|scope| scope.policy.initial == Initial::KeepAnchor && scope.opener == Some(opener))
|
||||
})?;
|
||||
let layer = *state.last_scope_order.iter().rev().find(|id| {
|
||||
state
|
||||
.last_scopes
|
||||
.get(*id)
|
||||
.is_some_and(|scope| scope.policy.initial == Initial::KeepAnchor && !scope.closing && scope.opener == Some(opener))
|
||||
})?;
|
||||
let policy = state.last_scopes.get(&layer)?.policy;
|
||||
let shown = |list: &Id| state.last_stops.iter().any(|rec| rec.stop.id == *list);
|
||||
policy.list.filter(shown).map(|list| (layer, list, policy))
|
||||
|
||||
@@ -70,12 +70,14 @@ pub struct MenuIconRow<'a> {
|
||||
common: Common,
|
||||
icons: &'a [MenuIcon<'a>],
|
||||
picked: Option<usize>,
|
||||
/// The row took a key activation it does not run: the first key in a menu the pointer opened (`RowInput::of`).
|
||||
withheld: bool,
|
||||
}
|
||||
|
||||
impl<'a> MenuIconRow<'a> {
|
||||
/// A row named `label` of `icons`.
|
||||
pub fn new(label: impl Into<Label>, icons: &'a [MenuIcon<'a>]) -> Self {
|
||||
MenuIconRow { common: Common::new(), icons, picked: None }.label(label)
|
||||
MenuIconRow { common: Common::new(), icons, picked: None, withheld: false }.label(label)
|
||||
}
|
||||
|
||||
fn part(at: usize) -> PartRef {
|
||||
@@ -160,8 +162,11 @@ impl Component for MenuIconRow<'_> {
|
||||
let runs = self.runs(enabled);
|
||||
let input = RowInput::of(cx, &level);
|
||||
let (moving, hovered) = (input.moving, input.hovered);
|
||||
// As a menu item: the pointer moving over the row, or the keyboard landing on it, makes it the active row.
|
||||
// As a menu item: the pointer moving over the row, or the keyboard landing on it, makes it the active row. The
|
||||
// first key in a menu the pointer opened picks no icon of a row the pointer did not light: `RowInput::of` runs
|
||||
// the lit row instead, or closes the menu.
|
||||
input.activate(&ctx, cx.id, &level, runs);
|
||||
self.withheld = input.withheld;
|
||||
// The pointer on the row closes another row's open submenu, unless it is on its way into it.
|
||||
if hovered && moving && !level.aimed(cx.id) && level.clear_submenu() {
|
||||
frame::request_repaint(&ctx);
|
||||
@@ -177,7 +182,7 @@ impl Component for MenuIconRow<'_> {
|
||||
self.picked = icons
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, icon)| runs && icon.enabled)
|
||||
.filter(|(_, icon)| runs && !input.withheld && icon.enabled)
|
||||
.map(|(at, _)| at)
|
||||
.find(|at| cx.part_activated(Self::part(*at)).is_some());
|
||||
if self.picked.is_some() {
|
||||
@@ -185,6 +190,11 @@ impl Component for MenuIconRow<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A key activation the row withheld does not activate it.
|
||||
fn activated(&self, cx: &Cx<'_, Self>) -> bool {
|
||||
!self.withheld && cx.activation().is_some()
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
let side = cx.style.glyph.side;
|
||||
let draw = ui.draw();
|
||||
|
||||
@@ -48,7 +48,7 @@ pub(crate) fn close_on_pick(ctx: &Context) {
|
||||
|
||||
/// What the pointer and the keyboard did to a menu row in this pass. Every row that can be the level's active row
|
||||
/// (`MenuItem`, `MenuIconRow`) reads it through [`RowInput::of`] and makes itself active by [`RowInput::activate`], so
|
||||
/// the rule lives once.
|
||||
/// the rules live once: which row is active, and what the first key in a menu the pointer opened does.
|
||||
pub(crate) struct RowInput {
|
||||
/// The pointer moved in this pass.
|
||||
pub moving: bool,
|
||||
@@ -56,6 +56,10 @@ pub(crate) struct RowInput {
|
||||
pub hovered: bool,
|
||||
/// The row holds the focus.
|
||||
pub focused: bool,
|
||||
/// The row took Enter or Space as the first key in a menu the pointer opened, holding the focus the menu gave it
|
||||
/// as it opened, while the pointer had not lit it: the row does not run on it and is not made active by it.
|
||||
/// [`RowInput::of`] answers the key instead.
|
||||
pub withheld: bool,
|
||||
keyboard: bool,
|
||||
gained: bool,
|
||||
started: bool,
|
||||
@@ -64,25 +68,48 @@ pub(crate) struct RowInput {
|
||||
impl RowInput {
|
||||
/// The row's input in this pass; the row's memory keeps the focus and keyboard facts of the pass before, to see
|
||||
/// when focus lands on the row or the keyboard starts driving the menu.
|
||||
///
|
||||
/// Enter or Space as the first key in a menu the pointer opened reaches the focused row, which the pointer may never
|
||||
/// have lit: the row withholds it, and the key runs the row the pointer lit, or with none lit only closes the menu,
|
||||
/// as a native menu does. A submenu row is no exception: the first key opens no submenu the pointer did not light.
|
||||
/// Only the focus the menu gave as it opened is withheld from: a row or part that a request focused (an AccessKit
|
||||
/// `Focus`, the app) was aimed at, and the key runs it.
|
||||
pub(crate) fn of<C: Component>(cx: &mut Cx<'_, C>, level: &MenuLevel) -> RowInput {
|
||||
let moving = cx.core.ctx.input(|input| input.pointer.delta() != Vec2::ZERO);
|
||||
let hovered = cx.response.hovered();
|
||||
let focused = cx.is_focused();
|
||||
let keyboard = level.keyboard();
|
||||
let by_key = cx.activation().is_some_and(|activation| activation.by_key);
|
||||
let id = cx.id;
|
||||
let rest = cx.memory::<Rest>();
|
||||
let gained = focused && !rest.focused;
|
||||
rest.focused = focused;
|
||||
let started = keyboard && !rest.keyboard;
|
||||
rest.keyboard = keyboard;
|
||||
RowInput { moving, hovered, focused, keyboard, gained, started }
|
||||
let opened = focused && crate::runtime::focus::focus_came_with_open(&cx.core.ctx);
|
||||
let lit = level.active_enabled();
|
||||
let withheld = started && by_key && opened && lit != Some(id);
|
||||
if withheld {
|
||||
match lit {
|
||||
Some(row) => level.run(row),
|
||||
None => {
|
||||
let ctx = &cx.core.ctx;
|
||||
if let Some(menu) = crate::runtime::focus::running_menu(ctx) {
|
||||
crate::runtime::focus::return_from_pick(ctx, crate::runtime::focus::PickedIn::Menus(menu));
|
||||
crate::runtime::overlay::request_close(ctx, menu, crate::runtime::overlay::CloseReason::Requested);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
RowInput { moving, hovered, focused, withheld, keyboard, gained, started }
|
||||
}
|
||||
|
||||
/// Makes the row `id` the level's active row when it `runs` and the keyboard landed on it. The pointer's row is
|
||||
/// made active by its `Menu` before the rows run (`MenuLevel::follow_pointer`); the focused row of a menu the
|
||||
/// pointer opened is not active until the keyboard lands on it.
|
||||
/// pointer opened is not active until the keyboard lands on it, and a withheld first key does not make it so.
|
||||
pub(crate) fn activate(&self, ctx: &Context, id: Id, level: &MenuLevel, runs: bool) {
|
||||
let landed = self.focused && self.keyboard && (self.gained || self.started);
|
||||
if runs && landed && level.activate_row(id) {
|
||||
if runs && !self.withheld && landed && level.activate_row(id) {
|
||||
frame::request_repaint(ctx);
|
||||
}
|
||||
}
|
||||
@@ -270,6 +297,8 @@ pub struct MenuItem<'a> {
|
||||
entry: Option<bool>,
|
||||
sample: Option<(crate::list::ThumbAspect, crate::primitives::FacePaint)>,
|
||||
ran: bool,
|
||||
/// The row took a key activation it does not run: the first key in a menu the pointer opened (`update`).
|
||||
withheld: bool,
|
||||
opens: bool,
|
||||
open: bool,
|
||||
active: bool,
|
||||
@@ -298,6 +327,7 @@ impl<'a> MenuItem<'a> {
|
||||
entry: None,
|
||||
sample: None,
|
||||
ran: false,
|
||||
withheld: false,
|
||||
opens: false,
|
||||
open: false,
|
||||
active: false,
|
||||
@@ -593,6 +623,8 @@ impl Component for MenuItem<'_> {
|
||||
}
|
||||
};
|
||||
let takes_over = hovered && (!heading || rested >= AIM_REST);
|
||||
// The first key in a menu the pointer opened, on a row the pointer did not light, is answered by `RowInput::of`.
|
||||
let first_key = input.withheld;
|
||||
// The focused row of a menu that the pointer opened is not active: the keyboard makes it so when focus lands on
|
||||
// it or the keyboard starts driving the menu.
|
||||
input.activate(&ctx, cx.id, &level, enabled);
|
||||
@@ -603,7 +635,8 @@ impl Component for MenuItem<'_> {
|
||||
frame::request_repaint_after(&ctx, AIM_REST - rested);
|
||||
}
|
||||
let typed = enabled && level.take_run(cx.id);
|
||||
let asked = enabled && (typed || cx.activation().is_some());
|
||||
self.withheld = first_key;
|
||||
let asked = enabled && (typed || (cx.activation().is_some() && !first_key));
|
||||
if self.submenu {
|
||||
self.opens = asked || (enabled && forward) || (enabled && takes_over);
|
||||
if self.opens {
|
||||
@@ -641,9 +674,10 @@ impl Component for MenuItem<'_> {
|
||||
self.ran = asked;
|
||||
}
|
||||
|
||||
/// A submenu row never runs: it opens its submenu, and the menus stay open.
|
||||
/// A submenu row never runs: it opens its submenu, and the menus stay open. Nor does a row whose key activation it
|
||||
/// withheld: the row the pointer lit runs instead, or none.
|
||||
fn activated(&self, cx: &Cx<'_, Self>) -> bool {
|
||||
!self.submenu && cx.activation().is_some()
|
||||
!self.submenu && !self.withheld && cx.activation().is_some()
|
||||
}
|
||||
|
||||
fn flags(&self) -> Flags {
|
||||
|
||||
@@ -326,6 +326,11 @@ impl MenuLevel {
|
||||
self.with(|state| state.active)
|
||||
}
|
||||
|
||||
/// The active row while it is enabled.
|
||||
pub(super) fn active_enabled(&self) -> Option<Id> {
|
||||
self.with(|state| state.active.filter(|active| state.shown.iter().any(|row| row.id == *active && row.enabled)))
|
||||
}
|
||||
|
||||
/// Moves the active row by `mv` among the enabled rows as the user last saw them, without moving the focus (the
|
||||
/// keys of a field that drives the menu): Up and Down wrap, Home and Page Up go to the first, End and Page Down to
|
||||
/// the last. Returns whether it moved.
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
|
||||
use craft_behavior::{IconId, PartRef};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _};
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _, Trigger};
|
||||
use craft_ui::menu::{Menu, MenuIcon, MenuIconRow, MenuItem};
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::testing::focus::AccessActionKind;
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
use egui::{Id, Key, Modifiers};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
@@ -111,3 +112,72 @@ fn an_icon_row_is_one_stop_in_the_menu_left_and_right_move_between_its_icons_and
|
||||
assert_eq!(*picks.borrow(), vec![2, 0], "a click picks the icon under it");
|
||||
assert!(fixture.by_label("Ungroup").is_none(), "the pick closes the menu");
|
||||
}
|
||||
|
||||
/// Regression: Return as the first key in a menu the pointer opened picked the first icon of a focused icon row the
|
||||
/// pointer never lit. With no row lit it only closes the menu; with a row lit under the pointer it runs that row.
|
||||
#[trial]
|
||||
fn return_in_a_pointer_opened_menu_whose_first_row_is_an_icon_row_runs_only_the_lit_row() {
|
||||
let picks = Rc::new(RefCell::new(Vec::new()));
|
||||
let ran = Rc::new(Cell::new(None::<&'static str>));
|
||||
let row_id = Rc::new(Cell::new(None::<Id>));
|
||||
let (picked, seen, shown) = (picks.clone(), ran.clone(), row_id.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let icons =
|
||||
[MenuIcon::new(IconId::new("align_edge_left"), "Align Left Edges"), MenuIcon::new(IconId::new("align_edge_right"), "Align Right Edges")];
|
||||
let opener = Button::new("Open").show(ui);
|
||||
Menu::new("Arrange").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
let row = MenuIconRow::new("Align", &icons).show(ui);
|
||||
shown.set(Some(row.response.id));
|
||||
picked.borrow_mut().extend(row.picked);
|
||||
for name in ["Group", "Ungroup"] {
|
||||
if MenuItem::new(name).show(ui).ran {
|
||||
seen.set(Some(name));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let open = |fixture: &mut Fixture| {
|
||||
let opener = fixture.by_label("Open").expect("the opener");
|
||||
fixture.advance(1.0).click(&opener).settle();
|
||||
assert!(fixture.by_label("Ungroup").is_some(), "a click on the opener opens the menu");
|
||||
assert_eq!(fixture.focused_stop().map(|(id, _)| id), row_id.get(), "the menu puts its first row, the icon row, in focus");
|
||||
};
|
||||
open(&mut fixture);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Ungroup").is_none(), "Return with no row lit closes the menu");
|
||||
assert!(picks.borrow().is_empty(), "and picks no icon");
|
||||
assert_eq!(ran.get(), None, "and runs no row");
|
||||
open(&mut fixture);
|
||||
let ungroup = fixture.by_label("Ungroup").expect("the Ungroup row").center();
|
||||
fixture.frame(vec![pointer::moved(ungroup)]).settle();
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!(ran.get(), Some("Ungroup"), "Return runs the row the pointer lit");
|
||||
assert!(picks.borrow().is_empty(), "and picks no icon of the focused row");
|
||||
assert!(fixture.by_label("Ungroup").is_none(), "and the run closes the menu");
|
||||
}
|
||||
|
||||
/// Regression: the first key in a menu the pointer opened was withheld from an icon an AccessKit `Focus` had moved to,
|
||||
/// so Return picked nothing. Only the focus the menu gave as it opened is withheld from; an icon aimed at is picked.
|
||||
#[trial]
|
||||
fn return_on_an_icon_accesskit_focused_in_a_pointer_opened_menu_picks_it() {
|
||||
let picks = Rc::new(RefCell::new(Vec::new()));
|
||||
let picked = picks.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let icons =
|
||||
[MenuIcon::new(IconId::new("align_edge_left"), "Align Left Edges"), MenuIcon::new(IconId::new("align_edge_right"), "Align Right Edges")];
|
||||
let opener = Button::new("Open").show(ui);
|
||||
Menu::new("Arrange").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
picked.borrow_mut().extend(MenuIconRow::new("Align", &icons).show(ui).picked);
|
||||
MenuItem::new("Group").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let opener = fixture.by_label("Open").expect("the opener");
|
||||
fixture.advance(1.0).click(&opener).settle();
|
||||
let right = fixture.by_label("Align Right Edges").expect("the menu shows its icons");
|
||||
fixture.access(AccessActionKind::Focus, &right).settle();
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!(*picks.borrow(), vec![1], "Return picks the icon AccessKit focused");
|
||||
assert!(fixture.by_label("Group").is_none(), "and the pick closes the menu");
|
||||
}
|
||||
|
||||
@@ -301,3 +301,81 @@ fn a_menu_paste_reads_the_clipboard_when_the_app_asks() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Space on a focused icon button that opens a menu opens the menu, as a click does.
|
||||
#[trial]
|
||||
fn space_on_a_focused_menu_opener_opens_its_menu() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let opener = IconButton::new(IconId::new("menu")).popup(PopupKind::Menu).label("Panel menu").show(ui);
|
||||
Menu::new("Panel menu").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
MenuItem::new("Clear All").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
fixture.key(egui::Key::Tab, egui::Modifiers::NONE).settle();
|
||||
fixture.key(egui::Key::Space, egui::Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Clear All").is_some(), "Space on the focused opener opens the menu");
|
||||
}
|
||||
|
||||
/// A menu opened from the keyboard is driven by the keyboard: Space on the focused opener opens it with its first row
|
||||
/// lit, and Return then runs that row and closes the menu.
|
||||
#[trial]
|
||||
fn return_in_a_menu_opened_by_space_runs_its_first_row() {
|
||||
let ran = Rc::new(Cell::new(None::<&'static str>));
|
||||
let seen = ran.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let opener = IconButton::new(IconId::new("menu")).popup(PopupKind::Menu).label("Panel menu").show(ui);
|
||||
Menu::new("Panel menu").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
for row in ["Clear All", "Reset"] {
|
||||
if MenuItem::new(row).show(ui).ran {
|
||||
seen.set(Some(row));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
fixture.key(egui::Key::Tab, egui::Modifiers::NONE).settle();
|
||||
fixture.key(egui::Key::Space, egui::Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Clear All").is_some(), "Space on the focused opener opens the menu");
|
||||
fixture.key(egui::Key::Enter, egui::Modifiers::NONE).settle();
|
||||
assert_eq!(ran.get(), Some("Clear All"), "Return runs the menu's first row");
|
||||
assert!(menu_rect(&fixture).is_none(), "running the row closes the menu");
|
||||
}
|
||||
|
||||
/// A menu the pointer opened puts its first row in focus without lighting it; when that row opens a submenu, Return
|
||||
/// with no row lit only closes the menu and opens no submenu, and with a row lit under the pointer it runs that row.
|
||||
#[trial]
|
||||
fn return_in_a_pointer_opened_menu_whose_first_row_opens_a_submenu() {
|
||||
let ran = Rc::new(Cell::new(None::<&'static str>));
|
||||
let seen = ran.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let opener = Button::new("Open").show(ui);
|
||||
Menu::new("Main").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
|
||||
Menu::submenu_row(ui, "Recent", |ui, _| {
|
||||
MenuItem::new("Report.txt").show(ui);
|
||||
});
|
||||
for row in ["Clear All", "Reset"] {
|
||||
if MenuItem::new(row).show(ui).ran {
|
||||
seen.set(Some(row));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let open = |fixture: &mut Fixture| {
|
||||
let opener = fixture.by_label("Open").expect("the opener");
|
||||
fixture.advance(1.0).click(&opener).settle();
|
||||
assert!(fixture.by_label("Clear All").is_some(), "a click on the opener opens the menu");
|
||||
};
|
||||
open(&mut fixture);
|
||||
fixture.key(egui::Key::Enter, egui::Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Clear All").is_none(), "Return with no row lit closes the menu");
|
||||
assert!(fixture.by_label("Report.txt").is_none(), "and opens no submenu");
|
||||
assert_eq!(ran.get(), None, "and runs no row");
|
||||
open(&mut fixture);
|
||||
let reset = fixture.by_label("Reset").expect("the Reset row").center();
|
||||
fixture.frame(vec![pointer::moved(reset)]).settle();
|
||||
fixture.key(egui::Key::Enter, egui::Modifiers::NONE).settle();
|
||||
assert_eq!(ran.get(), Some("Reset"), "Return runs the row the pointer lit");
|
||||
assert!(fixture.by_label("Clear All").is_none(), "and the run closes the menu");
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ pub(crate) mod combo_list;
|
||||
pub(crate) mod date;
|
||||
pub(crate) mod icon_picker;
|
||||
pub(crate) mod select_arrow;
|
||||
pub(crate) mod select_disabled_owner;
|
||||
pub(crate) mod select_list;
|
||||
pub(crate) mod select_measure;
|
||||
pub(crate) mod select_steps;
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
//! A disabled implicit Select owner naturally retires its held popup before the pointer is released.
|
||||
|
||||
use craft_behavior::EditEvent;
|
||||
use craft_ui::contract::{Bind, Entry, Props as _, Role, Show as _};
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::testing::{Fixture, Room, Story, StoryData, StoryOutput, trial};
|
||||
use egui::{Key, Modifiers, Ui};
|
||||
|
||||
fn select(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let parent_enabled = *data.value("parent_enabled", true);
|
||||
let control_enabled = *data.value("control_enabled", true);
|
||||
ui.add_enabled_ui(parent_enabled, |ui| {
|
||||
let entries = [
|
||||
Entry::new(1u8, "One"),
|
||||
Entry::new(2u8, "Two"),
|
||||
Entry::new(3u8, "Three"),
|
||||
Entry::new(4u8, "Four"),
|
||||
Entry::new(5u8, "Five"),
|
||||
Entry::new(6u8, "Six"),
|
||||
Entry::new(7u8, "Seven"),
|
||||
Entry::new(8u8, "Eight"),
|
||||
Entry::new(9u8, "Nine"),
|
||||
Entry::new(10u8, "Ten"),
|
||||
Entry::new(11u8, "Eleven"),
|
||||
Entry::new(12u8, "Twelve"),
|
||||
];
|
||||
let out = Select::new("Pick", Bind::bound(data.value("value", 1u8)), &entries).enabled(control_enabled).show(ui);
|
||||
data.note("terminal_edits", Vec::<&'static str>::new()).extend(out.events.iter().filter_map(|event| match event {
|
||||
EditEvent::Commit { .. } => Some("commit"),
|
||||
EditEvent::Set { .. } => Some("set"),
|
||||
_ => None,
|
||||
}));
|
||||
out.into()
|
||||
})
|
||||
.inner
|
||||
}
|
||||
|
||||
fn value(fixture: &mut Fixture) -> u8 {
|
||||
*fixture.data().value("value", 1u8)
|
||||
}
|
||||
|
||||
fn terminal_edits(fixture: &mut Fixture) -> usize {
|
||||
fixture.data().note("terminal_edits", Vec::<&'static str>::new()).len()
|
||||
}
|
||||
|
||||
fn held_popup_retires(disabled: &'static str) {
|
||||
let mut fixture = Fixture::story(&Story::new("disabled_implicit_select", select));
|
||||
fixture.room(Room::Window.size()).settle();
|
||||
let trigger = *fixture.by_role(Role::COMBO_BOX).first().expect("the Select");
|
||||
fixture.click(&trigger).settle();
|
||||
assert_eq!(fixture.tree().with_role(Role::LIST_BOX_OPTION).len(), 12, "the twelve options are open before the owner is disabled");
|
||||
let option = fixture.by_label("Two").expect("the real second option");
|
||||
fixture.press(&option);
|
||||
assert!(fixture.ctx().input(|input| input.pointer.primary_down()), "the real option press is still physically held");
|
||||
assert_eq!(terminal_edits(&mut fixture), 0, "the held option has not committed or set a value");
|
||||
|
||||
*fixture.data().value(disabled, true) = false;
|
||||
fixture.idle(2);
|
||||
let current = *fixture.by_role(Role::COMBO_BOX).first().expect("the same disabled Select is still installed");
|
||||
assert_eq!(current.node, trigger.node, "disabling does not substitute the owner's identity");
|
||||
assert!(fixture.tree().is_disabled_key(current.node), "the real disabled state reached the current owner");
|
||||
assert!(fixture.ctx().input(|input| input.pointer.primary_down()), "retirement is checked before any pointer release");
|
||||
assert_eq!(
|
||||
fixture.tree().with_role(Role::LIST_BOX_OPTION).len(),
|
||||
0,
|
||||
"a disabled implicit owner retires all popup options after two ordinary idle frames, before release"
|
||||
);
|
||||
assert!(fixture.tree().with_role(Role::LIST_BOX).is_empty(), "the popup shell closes too");
|
||||
assert_eq!(value(&mut fixture), 1, "disable preserves the original bound value");
|
||||
assert_eq!(terminal_edits(&mut fixture), 0, "retirement produces no Commit or Set");
|
||||
|
||||
fixture.release(&option).idle(2);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(2);
|
||||
assert_eq!(value(&mut fixture), 1, "late release and Enter cannot commit the retired option");
|
||||
assert_eq!(terminal_edits(&mut fixture), 0, "late inputs emit no terminal edit");
|
||||
*fixture.data().value(disabled, true) = true;
|
||||
fixture.idle(2);
|
||||
assert!(fixture.tree().with_role(Role::LIST_BOX_OPTION).is_empty(), "re-enabling does not resurrect the old popup");
|
||||
let recovered = *fixture.by_role(Role::COMBO_BOX).first().expect("the re-enabled owner");
|
||||
assert_eq!(recovered.node, trigger.node, "normal recovery keeps the same owner identity");
|
||||
fixture.click(&recovered).settle();
|
||||
let next = fixture.by_label("Two").expect("a newly opened, real option");
|
||||
fixture.click(&next).settle();
|
||||
assert_eq!(value(&mut fixture), 2, "a later pick still works");
|
||||
assert_eq!(terminal_edits(&mut fixture), 1, "only the new complete pick produces a terminal edit");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_parent_disabled_select_retires_its_held_popup_before_release() {
|
||||
held_popup_retires("parent_enabled");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn an_explicitly_disabled_select_retires_its_held_popup_before_release() {
|
||||
held_popup_retires("control_enabled");
|
||||
}
|
||||
@@ -61,6 +61,23 @@ fn keys_in_the_open_list_move_the_value_and_escape_puts_it_back() {
|
||||
assert_eq!(value(&mut fixture), 1, "and puts back the value from before the list opened");
|
||||
}
|
||||
|
||||
/// The list that Escape closed is closed for its field's keys while it still fades out: the Enter that follows at once
|
||||
/// is the field's own and opens the list again, rather than choosing a row of the list that is leaving.
|
||||
#[trial]
|
||||
fn enter_on_the_field_right_after_escape_opens_the_list_again_during_its_exit_fade() {
|
||||
let mut fixture = focused();
|
||||
fixture.key(Key::ArrowDown, Modifiers::ALT);
|
||||
fixture.settle();
|
||||
assert!(list_open(&mut fixture), "Alt+Down opens the list");
|
||||
fixture.key(Key::ArrowDown, Modifiers::NONE);
|
||||
fixture.key(Key::Escape, Modifiers::NONE);
|
||||
assert_eq!(value(&mut fixture), 1, "Escape puts back the value from before the list opened");
|
||||
fixture.key(Key::Enter, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(list_open(&mut fixture), "Enter on the focused field opens the list again before the old one has faded out");
|
||||
assert_eq!(value(&mut fixture), 1, "and picks no row of the list that was leaving");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn typing_on_the_closed_select_opens_the_list_on_the_first_match() {
|
||||
let mut fixture = focused();
|
||||
|
||||
@@ -101,6 +101,44 @@ fn escape_closes_the_edit_menu_and_the_field_keeps_its_typing_run() {
|
||||
assert_eq!(*rig.text.borrow(), "hello", "the field's own Escape still restores the value from before its run");
|
||||
}
|
||||
|
||||
/// Return in an edit menu the pointer opened, before the pointer lit a row, runs no row (it ran the first one, Undo,
|
||||
/// and took back the typing): it closes the menu and the field keeps its typing run, as a native menu does.
|
||||
#[trial]
|
||||
fn return_in_the_edit_menu_with_no_row_lit_only_closes_it() {
|
||||
let mut rig = rig();
|
||||
rig.fixture.type_text("abc");
|
||||
rig.fixture.settle();
|
||||
rig.right_click_in_the_field();
|
||||
assert!(rig.menu_open(), "the edit menu is open");
|
||||
rig.fixture.key(Key::Enter, Modifiers::NONE);
|
||||
rig.fixture.settle();
|
||||
assert!(!rig.menu_open(), "Return closes the menu");
|
||||
assert_eq!(*rig.text.borrow(), "helloabc", "and runs none of its rows");
|
||||
assert!(rig.focus_in_the_field(), "the focus is back in the field");
|
||||
rig.fixture.type_text("d");
|
||||
rig.fixture.settle();
|
||||
assert_eq!(*rig.text.borrow(), "helloabcd", "typing goes on in the field");
|
||||
}
|
||||
|
||||
/// Return in an edit menu the pointer opened runs the row the pointer lit, not the first row the menu focused.
|
||||
#[trial]
|
||||
fn return_in_the_edit_menu_runs_the_row_the_pointer_lit() {
|
||||
let mut rig = rig();
|
||||
rig.fixture.type_text("abc");
|
||||
rig.fixture.settle();
|
||||
rig.right_click_in_the_field();
|
||||
let select_all = rig.fixture.by_label("Select All").expect("the Select All row").center();
|
||||
rig.fixture.frame(vec![pointer::moved(select_all)]);
|
||||
rig.fixture.settle();
|
||||
rig.fixture.key(Key::Enter, Modifiers::NONE);
|
||||
rig.fixture.settle();
|
||||
assert!(!rig.menu_open(), "Return closes the menu");
|
||||
assert_eq!(*rig.text.borrow(), "helloabc", "Undo, the first row, did not run");
|
||||
rig.fixture.type_text("x");
|
||||
rig.fixture.settle();
|
||||
assert_eq!(*rig.text.borrow(), "x", "Select All ran: typing replaced the whole text");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn an_edit_command_fired_from_elsewhere_reaches_the_focused_field() {
|
||||
let mut rig = rig();
|
||||
|
||||
+286
-83
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user