A face and a collection member take READ_ONLY and SELECTED from their props as a run does: steps::value_flags is the one source of a component's own flags

This commit is contained in:
aiongg
2026-11-12 14:32:43 +00:00
parent af7478966a
commit 25cc4590b4
4 changed files with 111 additions and 35 deletions
+15 -27
View File
@@ -593,27 +593,21 @@ pub(crate) fn paint_face<C: Show>(ui: &mut Ui, env: &Env, source: &super::FaceSo
let rect = source.result.rect;
let flow = Flow::new(rect, env.direction.is_rtl());
let part_state = steps::part_state(source.result, step1.enabled && source.enabled, source.focus);
let mut flags = part_state.flags | face.flags() | source.flags;
// A face shows the value the step-6 peek found, as `steps::state` reads it for a component: a checked check box
// takes the on look.
if (shown.as_deref().map(|value| value as &dyn std::any::Any)).and_then(|value| value.downcast_ref::<bool>()) == Some(&true) {
flags |= crate::style::Flags::CHECKED;
}
if mixed {
flags |= crate::style::Flags::MIXED;
}
let on = f32::from(u8::from(flags.intersects(crate::style::Flags::ON)));
let state = StateSet::new(part_state.state, flags, crate::style::StateMix::new(part_state.t.hover, part_state.t.press, on));
let mut core = blank_core(env.view(), &resolved, &mut stage, ctx.clone(), env.ink, source.response.clone(), None);
core.face_mode = true;
core.rect = rect;
core.content_rect = flow.inset(StyleView::metrics(&*style).padding);
core.flow = flow;
core.state = state;
core.stated = true;
core.ink = steps::surface_ink(StyleView::surface(&*style), &state, env.ink);
core.visible = rect;
let cx = Cx::<C> { core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
let mut cx = Cx::<C> { core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
// A face shows its own inputs as `steps::state` reads them for a component: a checked check box takes the on
// look, a selected face the selected one.
let flags = part_state.flags | source.flags | steps::value_flags(&face, &cx);
let on = f32::from(u8::from(flags.intersects(crate::style::Flags::ON)));
let state = StateSet::new(part_state.state, flags, crate::style::StateMix::new(part_state.t.hover, part_state.t.press, on));
cx.core.state = state;
cx.core.stated = true;
cx.core.ink = steps::surface_ink(StyleView::surface(&*style), &state, env.ink);
steps::paint_surface(ui, &env, &cx.core, &*style);
{
let results = HashMap::new();
@@ -750,15 +744,6 @@ pub(crate) fn member_show<C: Show>(
held: false,
};
let part_state = steps::part_state(&result, enabled, None);
let mut flags = part_state.flags | item.flags();
if (shown.as_deref().map(|value| value as &dyn std::any::Any)).and_then(|value| value.downcast_ref::<bool>()) == Some(&true) {
flags |= Flags::CHECKED;
}
if mixed {
flags |= Flags::MIXED;
}
let on = f32::from(u8::from(flags.intersects(Flags::ON)));
let state = StateSet::new(part_state.state, flags, crate::style::StateMix::new(part_state.t.hover, part_state.t.press, on));
// A peek that opens no lifetime stage and is dropped without a write-back, as a face's.
let mut stage = MemoryStage::open_peek(&ctx, id);
stage.face = true;
@@ -767,9 +752,6 @@ pub(crate) fn member_show<C: Show>(
core.rect = row;
core.content_rect = flow.inset(StyleView::metrics(&*style).padding);
core.flow = flow;
core.state = state;
core.stated = true;
core.ink = steps::surface_ink(StyleView::surface(&*style), &state, env.ink);
core.visible = row.intersect(ui.clip_rect());
core.part_cursors = crate::primitives::sensing::hovered_cursor(decls.parts(), &sensed.parts).into_iter().collect();
core.results = sensed.parts;
@@ -777,6 +759,12 @@ pub(crate) fn member_show<C: Show>(
core.member_of = Some(node.owner);
core.intents = crate::runtime::keys::intents_of(&ctx, Some(id), rtl);
let mut cx = Cx::<C> { core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
let flags = part_state.flags | steps::value_flags(&item, &cx);
let on = f32::from(u8::from(flags.intersects(Flags::ON)));
let state = StateSet::new(part_state.state, flags, crate::style::StateMix::new(part_state.t.hover, part_state.t.press, on));
cx.core.state = state;
cx.core.stated = true;
cx.core.ink = steps::surface_ink(StyleView::surface(&*style), &state, env.ink);
item.update(&mut cx);
steps::paint_surface(ui, env, &cx.core, &*style);
steps::paint_part_layers(ui, env, &cx.core);
@@ -31,10 +31,17 @@ fn ease(p: &mut dyn ErasedPass<'_>, targets: [bool; 3]) -> StateMix {
StateMix::new(hover, press, on)
}
/// The flags of step 12 that the component's type knows: its own `flags`, CHECKED from a shown `bool`, MIXED from a
/// mixed binding and the binding's validity.
/// The flags of step 12 that the component's own inputs give, for a run, a face and a collection member alike: its
/// own `flags`, READ_ONLY and SELECTED from its props, CHECKED from a shown `bool`, MIXED from a mixed binding and the
/// binding's validity.
pub(crate) fn value_flags<C: Component>(comp: &C, cx: &Cx<'_, C>) -> Flags {
let mut flags = comp.flags();
if cx.core.resolved.read_only {
flags |= Flags::READ_ONLY;
}
if cx.core.resolved.selected == Some(true) {
flags |= Flags::SELECTED;
}
if cx.shown.as_deref().and_then(|value| (value as &dyn std::any::Any).downcast_ref::<bool>()) == Some(&true) {
flags |= Flags::CHECKED;
}
@@ -59,12 +66,6 @@ pub(crate) fn state(p: &mut dyn ErasedPass<'_>, style: &dyn StyleView, open: boo
if expanded {
flags |= Flags::EXPANDED;
}
if core.resolved.read_only {
flags |= Flags::READ_ONLY;
}
if core.resolved.selected == Some(true) {
flags |= Flags::SELECTED;
}
if core.command.as_deref().and_then(|command| command.checked) == Some(true) {
flags |= Flags::CHECKED;
}
@@ -0,0 +1,86 @@
//! A face paints and reports its own inputs as a component does: a face given `selected(true)` is a selected node.
use craft_behavior::PartKey;
use craft_ui::contract::{Common, Component, Cx, MeasureCx, PaintUi, PartsCx, Props as _, Role, Show as _};
use craft_ui::style::StyleCx;
use craft_ui::testing::{Fixture, trial};
use craft_ui_test_kit::contract::PlainStyle;
use egui::{Id, Rect, Vec2, pos2};
use std::cell::Cell;
use std::rc::Rc;
const FACE: PartKey = PartKey::new("face");
/// The face: a plain leaf.
#[derive(craft_ui::contract::Component)]
struct Mark {
#[common]
common: Common,
}
impl Component for Mark {
const ROLE: Role = Role::BUTTON;
type Style = PlainStyle;
type Output = egui::Response;
fn style(_: &StyleCx<'_>, _: MarkStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn finish(self, cx: Cx<'_, Self>) -> egui::Response {
cx.response.clone()
}
}
/// Two parts, each painted with a `Mark` face; the first face is selected.
#[derive(craft_ui::contract::Component)]
struct Pair {
#[common]
common: Common,
}
impl Component for Pair {
const ROLE: Role = Role::GROUP;
type Style = PlainStyle;
type Output = egui::Response;
fn style(_: &StyleCx<'_>, _: PairStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
Vec2::new(100.0, 30.0)
}
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
let half = rect.width() / 2.0;
for at in 0..2u64 {
let left = rect.min.x + half * at as f32;
cx.part(FACE.at(at), Rect::from_min_max(pos2(left, rect.min.y), pos2(left + half, rect.max.y)));
}
}
fn paint(&mut self, ui: &mut PaintUi<'_>, _: &Cx<'_, Self>) {
for at in 0..2u64 {
ui.face(FACE.at(at), Mark { common: Common::new() }.label("Mark").selected(at == 0));
}
}
fn finish(self, cx: Cx<'_, Self>) -> egui::Response {
cx.response.clone()
}
}
#[trial]
fn a_selected_face_reports_a_selected_node() {
let root = Rc::new(Cell::new(Id::NULL));
let shown = root.clone();
let mut fixture = Fixture::new(move |ui| {
shown.set(Pair { common: Common::new() }.label("Pair").show(ui).id);
});
fixture.settle();
let tree = fixture.tree();
let selected = |at: u64| fixture.part(root.get(), FACE.at(at)).map(|part| tree.selected_of(part.node));
assert_eq!(selected(0), Some(Some(true)), "the face given selected(true) is a selected node");
assert_ne!(selected(1), Some(Some(true)), "the other face is not");
}
@@ -1,5 +1,6 @@
//! The accessibility emitter: the label scope and merge rule, announcements, modal inertness, viewports.
mod face_state;
mod nodes;
mod scope;
mod sub_node_keys;