ui-contract: a control given selected outside a collection reports it as pressed, so the rail's current tool and the other tools say whether they are the tool

This commit is contained in:
bflatastic
2026-11-23 00:49:50 +07:00
parent 7e5b603174
commit f9cbb0a723
3 changed files with 34 additions and 7 deletions
@@ -195,6 +195,7 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>
shortcut,
display: facts.input == craft_behavior::Input::Display,
flags,
selected: resolved.selected,
enabled: resolved.enabled,
read_only: resolved.read_only,
focusable,
@@ -322,6 +323,7 @@ pub(crate) fn report_inert(core: &CxCore<'_>, facts: &TypeFacts) {
shortcut: None,
display: false,
flags: Flags::empty(),
selected: None,
enabled: core.resolved.enabled,
read_only: core.resolved.read_only,
focusable: false,
+12 -5
View File
@@ -32,6 +32,8 @@ pub(crate) struct Own<'a> {
/// A display component describes itself with its tooltip.
pub display: bool,
pub flags: Flags,
/// `Common.selected`: the component's chosen state outside a collection.
pub selected: Option<bool>,
pub enabled: bool,
pub read_only: bool,
pub focusable: bool,
@@ -124,15 +126,20 @@ fn expanded_of(flags: Flags, explicit: Option<bool>) -> Option<bool> {
explicit.or_else(|| flags.intersects(Flags::OPEN | Flags::EXPANDED).then_some(true))
}
/// The state flags of a node.
fn write_states(spec: &mut NodeSpec, flags: Flags, enabled: bool, read_only: bool) {
/// The state flags of a node, and `selected`, the `Common.selected` of a component's own node. A node that is neither
/// checked nor selected by its role, and shows no checked value, reports `selected` as pressed: a tool's button is down
/// while it is the tool, and up otherwise.
fn write_states(spec: &mut NodeSpec, flags: Flags, selected: Option<bool>, enabled: bool, read_only: bool) {
spec.disabled = !enabled;
let pressed = selected.filter(|_| !is_checkable(spec.role) && !is_selectable(spec.role) && !flags.intersects(Flags::CHECKED | Flags::MIXED));
if flags.contains(Flags::MIXED) {
spec.toggled = Some(Toggle::Mixed);
} else if flags.contains(Flags::CHECKED) || is_checkable(spec.role) {
spec.toggled = Some(if flags.contains(Flags::CHECKED) { Toggle::On } else { Toggle::Off });
} else if let Some(on) = pressed {
spec.toggled = Some(if on { Toggle::On } else { Toggle::Off });
}
if flags.contains(Flags::SELECTED) || is_selectable(spec.role) {
if pressed.is_none() && (flags.contains(Flags::SELECTED) || is_selectable(spec.role)) {
spec.selected = Some(flags.contains(Flags::SELECTED));
}
spec.invalid = flags.contains(Flags::INVALID);
@@ -287,7 +294,7 @@ pub(crate) fn report(ctx: &Context, own: &Own<'_>) {
column_index: spec.grid.column_index.or(access.grid.column_index),
column_count: spec.grid.column_count.or(access.grid.column_count),
};
write_states(&mut spec, own.flags, own.enabled, own.read_only);
write_states(&mut spec, own.flags, own.selected, own.enabled, own.read_only);
spec.ltr_axis = own.ltr_axis;
spec.busy |= access.busy;
if let Some(expanded) = expanded {
@@ -389,7 +396,7 @@ pub fn report_sub(ctx: &Context, decl: &SubDecl<'_>) {
spec.grid = node.grid;
spec.sort = node.sort;
spec.sortable = node.sortable;
write_states(&mut spec, flags, decl.enabled, false);
write_states(&mut spec, flags, None, decl.enabled, false);
declare_actions(&mut spec, decl.focusable, decl.clickable, numeric);
if node.steps && numeric.is_none() {
spec.actions.extend([NodeAction::Increment, NodeAction::Decrement]);
+20 -2
View File
@@ -3,9 +3,9 @@
pub(crate) mod drawer;
use craft_behavior::GesturePhase;
use craft_behavior::{GesturePhase, Mixed};
use craft_ui::contract::overlay::{PlacementChange, PlacementKind};
use craft_ui::contract::{Role, Show as _};
use craft_ui::contract::{Props as _, Role, Show as _};
use craft_ui::docking::{FloatingGroup, GroupDrag};
use craft_ui::rails::{FlyoutButton, RailTool};
use craft_ui::runtime::commands::EditCommands;
@@ -130,6 +130,24 @@ fn the_flyout_of_a_low_tool_slides_up_to_stay_in_the_window() {
assert!(flyout.bottom() > target.rect.bottom() + 1.0, "it slides: its bottom {} is below the tool's {}", flyout.bottom(), target.rect.bottom());
}
/// A rail button tells assistive technology which tool is the current one: the current tool's button is pressed and
/// every other tool's button is not, as it paints.
#[trial]
fn the_current_tools_button_reports_pressed_and_the_others_not() {
let (undo, cut) = (tools(&[UNDO]), tools(&[CUT]));
let mut fixture = Fixture::new(move |ui| {
FlyoutButton::new(&undo, &UNDO).id_salt("undo").selected(true).show(ui);
FlyoutButton::new(&cut, &CUT).id_salt("cut").selected(false).show(ui);
});
fixture.settle();
let pressed = |label: &str| {
let button = fixture.by_label(label).unwrap_or_else(|| panic!("the {label} button shows"));
fixture.tree().toggled_of(button.node)
};
assert_eq!(pressed("Undo"), Some(Mixed::Same(true)), "the current tool's button is pressed");
assert_eq!(pressed("Cut"), Some(Mixed::Same(false)), "another tool's button reports that it is not");
}
/// With a click command a click, Enter or Space fires it and selects nothing; a hold, a drag away and a right click
/// list the tools, and the release that ends a hold fires nothing.
#[trial]