ui-contract: a disabled text part also disables its owner's node for routed requests

report_text marked the owner's AccessKit node disabled for a disabled text
part but left the owner's routing entry enabled, so ReplaceSelectedText,
Click or a custom action on that node still reached an enabled component.
The owner's entry is now marked disabled with the text part.
This commit is contained in:
dotCooCoo committed 2026-11-28 15:19:09 -08:00
1 parent 61d99fa436
commit 7892ab1318
2 files changed
+91 -2

No files matched your search

@@ -137,6 +137,10 @@ pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts, unit: Opt
}
});
adapter::write_text(ctx, owner, &TextNode { value: value.as_deref(), editable: facts.editable, disabled: facts.disabled, selection });
// The owner's node, registered with its common facts, is disabled with the text part it stands for.
if facts.disabled {
keys::register_disabled(ctx, owner);
}
Service::<Tables>::run(ctx, |tables| tables.0.insert(owner, entries));
}
@@ -2,10 +2,12 @@
//! context menu, step, value or custom action, on the component's own node or on its sub-nodes. The same requests on
//! an enabled component do.
use craft_behavior::{AccessAction, AccessData, Input, PartKey, Sense};
use craft_behavior::{AccessAction, AccessData, Input, PartKey, PlainText, Sense};
use craft_ui::contract::{
Access, Common, Component, Cx, Focus as _, MeasureCx, Name, Numeric, Overlays as _, PartsCx, Pointer as _, Props as _, Role, Show as _, Trigger,
Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Name, Numeric, Overlays as _, PartsCx,
Pointer as _, Props as _, Role, Show as _, Trigger,
};
use craft_ui::primitives::{TextKey, TextOptions};
use craft_ui::runtime::focus::KeyUse;
use craft_ui::style::StyleCx;
use craft_ui::testing::{Fixture, trial};
@@ -19,6 +21,7 @@ const CHEVRON: PartKey = PartKey::new("chevron");
const SECTION: PartKey = PartKey::new("section");
const KNOB: PartKey = PartKey::new("knob");
const NUDGE: PartKey = PartKey::new("nudge");
const TEXT: PartKey = PartKey::new("text");
/// What reached the components in their latest run.
#[derive(Default)]
@@ -270,3 +273,85 @@ fn a_disabled_component_takes_no_access_request_that_acts_on_it() {
}
assert!(wrong.is_empty(), "each request reaches the components only while they are enabled: {wrong:#?}");
}
/// An enabled field over a string whose text part is enabled or not, recording the access requests that reach it.
#[derive(craft_ui::contract::Component)]
struct Field<'a> {
#[common]
common: Common,
value: Bind<'a, String>,
text_enabled: bool,
requests: Rc<RefCell<Vec<AccessAction>>>,
}
impl Component for Field<'_> {
const ROLE: Role = Role::TEXT_INPUT;
const INPUT: Input = Input::Text;
type Style = PlainStyle;
type Output = EditResponse<String>;
fn style(_: &StyleCx<'_>, _: FieldStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn sense(&self) -> Sense {
Sense::CLICK | Sense::FOCUS | Sense::TAKE_FOCUS
}
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
Vec2::new(120.0, 24.0)
}
fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) {
let options = TextOptions { enabled: self.text_enabled, ..TextOptions::default() };
let (shown, rect) = (cx.value().cloned().unwrap_or_default(), cx.content_rect);
cx.text(TextKey::<String>::new(TEXT), rect, &PlainText, &shown, &options);
}
fn bind(&mut self) -> Option<BindMut<'_, String>> {
Some(self.value.reborrow())
}
fn update(&mut self, cx: &mut Cx<'_, Self>) {
while let Some(request) = cx.intents().take_access() {
self.requests.borrow_mut().push(request.action);
}
}
fn finish(self, cx: Cx<'_, Self>) -> EditResponse<String> {
cx.edit_response()
}
}
/// Shows the field with its text part enabled or not, sends `action` to the field's node and returns the access
/// requests that reached the field and its value.
fn send_to_field(text_enabled: bool, action: AccessAction, data: AccessData) -> (Vec<AccessAction>, String) {
let value = Rc::new(RefCell::new(String::from("Kept")));
let requests = Rc::new(RefCell::new(Vec::new()));
let id = Rc::new(Cell::new(Id::NULL));
let (held, taken, seen) = (value.clone(), requests.clone(), id.clone());
let mut fixture = Fixture::new(move |ui| {
let mut text = held.borrow_mut();
let field = Field { common: Common::new(), value: Bind::bound(&mut *text), text_enabled, requests: taken.clone() };
seen.set(field.label("Field").show(ui).response.id);
});
fixture.settle();
requests.borrow_mut().clear();
let request = ActionRequest { action, target_tree: TreeId::ROOT, target_node: id.get().accesskit_id(), data: Some(data) };
fixture.frame(vec![Event::AccessKitActionRequest(request)]).settle();
let reached = requests.borrow().clone();
let value = value.borrow().clone();
(reached, value)
}
/// The node of an enabled field stands for its text part and is disabled with it: a request that acts on the node
/// reaches the field, or sets its value, only while the text part is enabled.
#[trial]
fn a_disabled_text_part_disables_its_owners_node_for_requests_that_act_on_it() {
let replace = |enabled| send_to_field(enabled, AccessAction::ReplaceSelectedText, AccessData::Value("Typed".into())).0;
assert!(replace(true).contains(&AccessAction::ReplaceSelectedText), "an enabled text part takes ReplaceSelectedText");
assert!(replace(false).is_empty(), "a disabled text part takes no ReplaceSelectedText");
let set = |enabled| send_to_field(enabled, AccessAction::SetValue, AccessData::Value("Typed".into())).1;
assert_eq!(set(true), "Typed", "an enabled text part takes the value");
assert_eq!(set(false), "Kept", "a disabled text part keeps its value");
}