mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
Open menus from keyboard and accessibility activation
This commit is contained in:
@@ -18,7 +18,7 @@ pub enum Trigger {
|
||||
/// completes the click is part of that opening.
|
||||
Click,
|
||||
/// A primary press on the opener toggles, and a secondary click inside it toggles too, hanging the layer from the
|
||||
/// opener as a press does (a pop-up menu button).
|
||||
/// opener as a press does (a pop-up menu button). Keyboard and accessibility activation toggle it too.
|
||||
Press,
|
||||
/// A context request (right click, the Menu key, AccessKit `ShowContextMenu`) opens.
|
||||
Context,
|
||||
@@ -96,6 +96,9 @@ impl Opener {
|
||||
let clicked = trigger == Trigger::Click && frame.release == Some(PointerButton::Primary) && response::pointer_clicked(anchor);
|
||||
(fired, opening, pressed_on && pass.primary_down, clicked)
|
||||
});
|
||||
// Keyboard and accessibility clicks have no pointer press; their activation must still open the layer.
|
||||
let activated = anchor.enabled() && response::clicked(anchor) && !response::pointer_clicked(anchor);
|
||||
let fired = fired || (activated && matches!(trigger, Trigger::Click | Trigger::Press));
|
||||
Opener {
|
||||
id: anchor.id,
|
||||
rect: anchor.rect,
|
||||
|
||||
@@ -382,3 +382,53 @@ fn rail_icons_stay_beside_the_expanded_column_and_the_open_panels_icon_collapses
|
||||
assert!(iconic(), "the icon of the panel in front collapses the column");
|
||||
assert!(!shown(&fixture, "Alpha body"), "and its panel no longer shows");
|
||||
}
|
||||
|
||||
struct RailWithMenu;
|
||||
|
||||
impl DockPanels<u32> for RailWithMenu {
|
||||
fn spec(&self, panel: &u32) -> PanelSpec {
|
||||
PanelSpec::new(format!("Panel {panel}"))
|
||||
}
|
||||
|
||||
fn body(&mut self, _: &u32, _: &mut Ui, _: &mut BodyCx<'_>) {}
|
||||
|
||||
fn rail_footer(&mut self, _: &u32, ui: &mut Ui, _: &mut BodyCx<'_>) {
|
||||
use craft_ui::buttons::IconButton;
|
||||
use craft_ui::contract::{PopupKind, Props as _, ShowContainer as _, Trigger};
|
||||
use craft_ui::menu::{Menu, MenuItem};
|
||||
let opener = IconButton::new(craft_behavior::IconId::new("settings")).label("Rail menu").popup(PopupKind::Menu).show(ui);
|
||||
Menu::new("Rail options").opened_by(&opener.response, Trigger::Press).show(ui, |ui, _| {
|
||||
MenuItem::new("Rail option").show(ui);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// A popup opened from the fixed rail footer remains interactive at the edge of the window.
|
||||
#[trial]
|
||||
fn a_menu_opens_from_the_rail_footer() {
|
||||
let side = Column::new(vec![Group::new(vec![10, 11], 0)]).with_iconic(true);
|
||||
let root = Node::split(Axis::X, SplitSize::Ratio(0.6), Node::tabs(vec![1], 0), Node::dock(DockEdge::End, vec![side]));
|
||||
let workspace = Workspace::new(Layout::new(Some(root), Vec::new()));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
DockArea::new("Rail dock", Bind::given(workspace.clone()), &mut RailWithMenu, DockConfig::default().iconic_mode(IconicMode::Rail))
|
||||
.rail_footer(10)
|
||||
.show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(900.0, 500.0)).settle();
|
||||
let opener = fixture.by_label("Rail menu").expect("the rail menu button");
|
||||
fixture.access(craft_ui::testing::focus::AccessActionKind::Click, &opener).settle();
|
||||
assert!(fixture.by_label("Rail option").is_some(), "the footer button opens its menu through accessibility");
|
||||
fixture.key(egui::Key::Escape, egui::Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Rail option").is_none(), "Escape closes the menu");
|
||||
fixture.click(&opener).settle();
|
||||
assert!(fixture.by_label("Rail option").is_some(), "a pointer press still opens it");
|
||||
fixture.key(egui::Key::Escape, egui::Modifiers::NONE).settle();
|
||||
fixture.access(craft_ui::testing::focus::AccessActionKind::Focus, &opener).settle();
|
||||
fixture.key(egui::Key::Enter, egui::Modifiers::NONE).settle();
|
||||
assert!(fixture.by_label("Rail option").is_some(), "Enter opens the focused menu");
|
||||
fixture.key(egui::Key::Escape, egui::Modifiers::NONE).settle();
|
||||
let at = opener.center();
|
||||
fixture.frame(vec![pointer::moved(egui::pos2(100.0, 100.0))]);
|
||||
fixture.frame(vec![pointer::moved(at), pointer::press(at), pointer::release(at)]).settle();
|
||||
assert!(fixture.by_label("Rail option").is_some(), "a same-frame click opens the menu");
|
||||
}
|
||||
|
||||
@@ -206,3 +206,20 @@ fn a_menu_button_hangs_its_arrow_under_the_end_of_its_glyph() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Both click and press openers accept a screen reader's activation, not only a pointer press.
|
||||
#[trial]
|
||||
fn a_menu_opener_accepts_accessibility_activation() {
|
||||
for trigger in [Trigger::Click, Trigger::Press] {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let opener = IconButton::new(IconId::new("settings")).label("Options").popup(PopupKind::Menu).show(ui);
|
||||
Menu::new("Options menu").opened_by(&opener.response, trigger).show(ui, |ui, _| {
|
||||
MenuItem::new("An option").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let opener = fixture.by_label("Options").expect("the opener");
|
||||
fixture.access(craft_ui::testing::focus::AccessActionKind::Click, &opener).settle();
|
||||
assert!(fixture.by_label("An option").is_some(), "{trigger:?} opens through accessibility");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user