Skip to main content

glory_render/
semantics.rs

1//! Finite composite-widget metadata. Stable names are explicit document keys,
2//! independent of NodeId lifetimes and snapshot rekeying.
3
4use crate::{Error, Navigation, Props, Role};
5
6/// Structural form content, fixed for a node's lifetime on HTML adapters.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum FieldPart {
9    Form,
10    Label,
11    Fieldset,
12    Legend,
13}
14
15/// The host captures requests; application policy owns validation and transport.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum FormAction {
18    Submit,
19    Reset,
20}
21
22/// Submission metadata is separate from the accessible name. Text input values
23/// come from InputProps; Check/Button may supply an explicit successful value.
24#[derive(Clone, Debug, PartialEq, Eq)]
25pub struct FormControl {
26    pub name: String,
27    pub value: Option<String>,
28}
29
30/// Native links may use their current context or request a separate one.
31/// Opening a system browser remains an outer native host service.
32#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
33pub enum LinkOpen {
34    #[default]
35    Current,
36    NewContext,
37}
38
39/// The current location in a related set, independent of selection or visits.
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum Current {
42    Page,
43    Step,
44    Location,
45    Date,
46}
47/// The popup role is one typed value, never an independent boolean flag.
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub enum PopupKind {
50    Dialog,
51    Menu,
52    Listbox,
53    Tree,
54}
55impl PopupKind {
56    pub fn as_str(self) -> &'static str {
57        match self {
58            Self::Dialog => "dialog",
59            Self::Menu => "menu",
60            Self::Listbox => "listbox",
61            Self::Tree => "tree",
62        }
63    }
64}
65#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66pub enum MenuItemKind {
67    Action,
68    Checkbox,
69    Radio,
70}
71/// Finite intrinsic-control refinement. Owner is a named Menu View, also used
72/// to keep menu check controls outside any enclosing successful form controls.
73#[derive(Clone, Debug, PartialEq, Eq)]
74pub struct MenuItemSemantics {
75    pub kind: MenuItemKind,
76    pub owner: SemanticId,
77}
78
79/// Host-managed top layer. Presence declares a focus scope; `hidden` controls
80/// whether it participates in the stack. Relations resolve only live names.
81#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
82pub enum LayerFocus {
83    #[default]
84    Manage,
85    Preserve,
86}
87#[derive(Clone, Debug, PartialEq)]
88pub struct Layer {
89    pub sheet: Option<crate::SheetConfig>,
90    pub modal: bool,
91    pub escape: bool,
92    pub outside: bool,
93    pub focus: LayerFocus,
94    pub placement: LayerPlacement,
95    pub anchor: Option<SemanticId>,
96    pub initial_focus: Option<SemanticId>,
97    pub return_focus: Option<SemanticId>,
98}
99impl Default for Layer {
100    fn default() -> Self {
101        Self {
102            sheet: None,
103            modal: true,
104            escape: true,
105            outside: true,
106            focus: LayerFocus::Manage,
107            placement: LayerPlacement::Center,
108            anchor: None,
109            initial_focus: None,
110            return_focus: None,
111        }
112    }
113}
114#[derive(Clone, Copy, Debug, Default, PartialEq)]
115pub enum LayerPlacement {
116    #[default]
117    Center,
118    Left,
119    Right,
120    Top,
121    Bottom,
122    Anchor(crate::AnchorPlacement),
123    /// A context request in viewport CSS pixels, belonging to `Layer::anchor`.
124    ContextPoint(crate::ContextPoint),
125    Viewport(crate::ViewportPlacement),
126}
127impl LayerPlacement {
128    pub fn as_str(self) -> &'static str {
129        match self {
130            Self::Center => "center",
131            Self::Left => "left",
132            Self::Right => "right",
133            Self::Top => "top",
134            Self::Bottom => "bottom",
135            Self::Anchor(_) => "anchor",
136            Self::ContextPoint(_) => "context-point",
137            Self::Viewport(_) => "viewport",
138        }
139    }
140}
141#[derive(Clone, Copy, Debug, PartialEq, Eq)]
142pub enum DismissReason {
143    Gesture,
144    Escape,
145    Outside,
146    /// The declared anchor is no longer a live visible semantic box.
147    AnchorLost,
148}
149
150#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
151pub struct SemanticId(String);
152impl SemanticId {
153    /// Names are bounded ASCII tokens. Applications must choose distinct widget
154    /// namespaces when multiple Apps share one host document.
155    pub fn new(value: impl Into<String>) -> Result<Self, Error> {
156        let value = value.into();
157        if value.is_empty()
158            || value.len() > 256
159            || !value
160                .bytes()
161                .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
162        {
163            return Err(Error::InvalidProps(
164                "semantic id must be a bounded ASCII token",
165            ));
166        }
167        Ok(Self(value))
168    }
169    pub fn as_str(&self) -> &str {
170        &self.0
171    }
172}
173
174#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175pub enum Orientation {
176    Horizontal,
177    Vertical,
178}
179#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
180pub enum Direction {
181    #[default]
182    Ltr,
183    Rtl,
184}
185impl Orientation {
186    pub fn key_with_direction(
187        self,
188        direction: Direction,
189        input: crate::KeyboardInput<'_>,
190    ) -> Option<Navigation> {
191        let command = self.key(input)?;
192        Some(match (self, direction, input.key, command) {
193            (Self::Horizontal, Direction::Rtl, "ArrowLeft" | "ArrowRight", Navigation::Next) => {
194                Navigation::Previous
195            }
196            (
197                Self::Horizontal,
198                Direction::Rtl,
199                "ArrowLeft" | "ArrowRight",
200                Navigation::Previous,
201            ) => Navigation::Next,
202            _ => command,
203        })
204    }
205    pub fn as_str(self) -> &'static str {
206        match self {
207            Self::Horizontal => "horizontal",
208            Self::Vertical => "vertical",
209        }
210    }
211    /// Shared direction mapping. Tab, editing keys, modified/composing keys and
212    /// the perpendicular arrows remain host-native. Repeats may navigate.
213    pub fn key(self, input: crate::KeyboardInput<'_>) -> Option<Navigation> {
214        if input.composing
215            || input.editable
216            || input.control
217            || input.meta
218            || input.alt
219            || input.shift
220        {
221            return None;
222        }
223        match (self, input.key) {
224            (Self::Horizontal, "ArrowLeft") | (Self::Vertical, "ArrowUp") => {
225                Some(Navigation::Previous)
226            }
227            (Self::Horizontal, "ArrowRight") | (Self::Vertical, "ArrowDown") => {
228                Some(Navigation::Next)
229            }
230            (_, "Home") => Some(Navigation::First),
231            (_, "End") => Some(Navigation::Last),
232            _ => None,
233        }
234    }
235}
236
237/// Optional and finite: ordinary nodes allocate none of this metadata. Named
238/// relations may refer forward or to an unmounted target; adapters resolve only
239/// live names in their document. Removing a target retains no owning reference.
240#[derive(Clone, Debug, Default, PartialEq)]
241pub struct Semantics {
242    /// Explicit inherited text direction/language on an ordinary boxed View.
243    pub environment: Option<crate::Environment>,
244    /// A focus-only boundary. It has no role, top-layer paint or inert backdrop.
245    pub focus_scope: Option<crate::FocusScopeConfig>,
246    /// Nonphysical source read from the accepted semantic tree only when a
247    /// typed clipboard action is admitted. It is not duplicated into HTML.
248    pub clipboard: Option<crate::ClipboardText>,
249    /// Present only on a structural column header; actions remain intrinsic buttons.
250    pub sort: Option<crate::SortDirection>,
251    /// Replace ordinary host ValueChanged with one bounded post-edit path.
252    pub input_edit: Option<crate::InputEditCapture>,
253    /// Finite paint-only motion on an inert, independently boxed View.
254    pub translation: Option<crate::TranslationMotion>,
255    /// Finite single-line input hints; no automatic code or credential retrieval.
256    pub input_assist: Option<crate::InputAssist>,
257    /// Optional bounded presentation hint on Input, never its accessible name,
258    /// stored value, default value or composition state.
259    pub placeholder: Option<String>,
260    /// Optional intrinsic-height presentation with immediate logical hiding.
261    pub collapse: Option<crate::CollapseConfig>,
262    /// Optional finite opacity/transform presence; logical availability is immediate.
263    pub presence: Option<Box<crate::PresenceConfig>>,
264    /// Immediate primary-pointer capture, in viewport CSS pixels.
265    pub drag_capture: Option<crate::DragCapture>,
266    /// Axis intent capture preserves orthogonal native panning.
267    pub swipe_capture: Option<crate::SwipeCapture>,
268    /// An explicit context region. Touch long-press remains host-native.
269    pub context_menu: Option<crate::ContextEditPolicy>,
270    /// Opted-in free tag entry policy; retains the Input's intrinsic textbox.
271    pub tags_input: Option<crate::TagSeparators>,
272    pub progress: Option<crate::ProgressValue>,
273    pub splitter: Option<crate::RangeValue>,
274    pub slider: Option<crate::SliderValue>,
275    pub spinbutton: Option<crate::SpinValue>,
276    /// Opt-in keyboard commands only while this viewport itself owns focus.
277    pub scroll_keyboard: Option<crate::ScrollAxes>,
278    /// An application-owned pending state on a boxed semantic node.
279    pub busy: Option<bool>,
280    pub current: Option<Current>,
281    pub separator_axis: Option<Orientation>,
282    pub link_open: Option<LinkOpen>,
283    /// Capture activation as an application request before default navigation.
284    pub link_request: bool,
285    pub document_part: Option<crate::DocumentPart>,
286    pub table: Option<crate::TableMetadata>,
287    pub form_action: Option<FormAction>,
288    pub form_control: Option<FormControl>,
289    pub field_part: Option<FieldPart>,
290    pub label_for: Option<SemanticId>,
291    pub error_message: Option<SemanticId>,
292    pub required: bool,
293    pub readonly: bool,
294    pub invalid: bool,
295    pub id: Option<SemanticId>,
296    pub controls: Option<SemanticId>,
297    pub labelled_by: Option<SemanticId>,
298    pub described_by: Option<SemanticId>,
299    pub popup: Option<PopupKind>,
300    pub combobox: Option<crate::ComboboxKind>,
301    pub option: Option<crate::OptionSemantics>,
302    pub active_descendant: Option<SemanticId>,
303    pub listbox_multiple: bool,
304    pub menu_item: Option<MenuItemSemantics>,
305    /// Single-line search editor controlling its nearest named Menu. Matching,
306    /// highlight and activation remain owned by the component.
307    pub menu_search: Option<SemanticId>,
308    /// An intrinsic action trigger belonging to a structural Menubar.
309    pub menubar_item: bool,
310    pub layer: Option<Layer>,
311    /// Passive grip hit area; the nearest live sheet owns pointer gestures.
312    pub sheet_grip: bool,
313    pub expanded: Option<bool>,
314    pub selected: Option<bool>,
315    /// A toggle button's application-owned state; independent from disclosure.
316    pub pressed: Option<bool>,
317    /// Refine a Button's intrinsic interaction to a tab, retaining activation.
318    pub tab: bool,
319    /// Keep a control focusable, while suppressing activation (e.g. the sole
320    /// expanded header in an accordion that cannot collapse completely).
321    pub disabled: bool,
322    /// Hide a View subtree without destroying its state or removing its names.
323    pub hidden: bool,
324    /// Retain a View's content and layout while suppressing subtree interaction
325    /// and accessibility exposure. This is not a scroll lock or a modal scope.
326    pub inert: bool,
327    /// Explicit collection boundary, independent of virtual-list navigation.
328    pub navigation: Option<Orientation>,
329    /// A two-dimensional physical Button navigation boundary, independent of
330    /// the linear collection navigation protocol.
331    pub grid_navigation: Option<Direction>,
332    /// A Grid that permits more than one selected cell.
333    pub grid_multiple: bool,
334    pub tree_navigation: Option<crate::TreeNavigation>,
335    pub tree_item: Option<crate::TreeItem>,
336    /// A checkable tree item, independent from list selection and Check props.
337    pub tree_checked: Option<crate::TreeCheckState>,
338    pub navigation_item: bool,
339    pub navigation_panel: Option<crate::NavigationPanel>,
340    /// Native radio association; not an accessible name or a submission value.
341    /// HTML uses the same attribute for native grouping and successful names.
342    pub check_group: Option<SemanticId>,
343    pub navigation_direction: Direction,
344    /// Opt in to bounded character search on the collection's mounted members.
345    pub typeahead: bool,
346}
347impl Semantics {
348    pub(crate) fn validate(&self, d: crate::DescriptorView<'_>) -> Result<(), Error> {
349        if let Some(placeholder) = &self.placeholder {
350            let Props::Input(input) = d.props else {
351                return Err(Error::InvalidProps("placeholder requires an Input"));
352            };
353            crate::validate_input_placeholder(placeholder, input.multiline)?;
354        }
355        if let Some(environment) = &self.environment {
356            if !matches!(d.props, Props::View) {
357                return Err(Error::InvalidProps("environment metadata belongs to View"));
358            }
359            if self.navigation.is_some() && self.navigation_direction != environment.direction
360                || self
361                    .grid_navigation
362                    .is_some_and(|direction| direction != environment.direction)
363                || self
364                    .tree_navigation
365                    .is_some_and(|tree| tree.direction != environment.direction)
366            {
367                return Err(Error::InvalidProps(
368                    "environment and navigation directions disagree",
369                ));
370            }
371        }
372        if self.focus_scope.is_some()
373            && (!matches!(d.props, Props::View)
374                || d.focus != crate::FocusMode::Programmatic
375                || !matches!(d.role, None | Some(Role::Group))
376                || self.layer.is_some())
377        {
378            return Err(Error::InvalidProps(
379                "FocusScope requires an independent programmatic View",
380            ));
381        }
382        if self.tree_checked.is_some()
383            && (d.role != Some(Role::TreeItem)
384                || self.tree_item.is_none()
385                || self.selected.is_some())
386        {
387            return Err(Error::InvalidProps(
388                "tree checked state requires a TreeItem with coordinates, without selected state",
389            ));
390        }
391        if self.tree_navigation.is_some()
392            && (d.role != Some(Role::Tree)
393                || !matches!(d.props, Props::View)
394                || self.navigation.is_some()
395                || self.grid_navigation.is_some()
396                || self.navigation_item
397                || self.tree_item.is_some()
398                || self.typeahead)
399        {
400            return Err(Error::InvalidProps(
401                "tree navigation requires an independent Tree View",
402            ));
403        }
404        if let Some(item) = self.tree_item {
405            item.validate()?;
406            if d.role != Some(Role::TreeItem)
407                || !matches!(d.props, Props::View)
408                || self.navigation.is_some()
409                || self.grid_navigation.is_some()
410                || self.navigation_item
411                || self.table.is_some()
412                || self.typeahead
413            {
414                return Err(Error::InvalidProps(
415                    "tree coordinates require an independent TreeItem View",
416                ));
417            }
418        }
419        if self.clipboard.is_some()
420            && (!matches!(d.props, Props::Button(_))
421                || self.form_action.is_some()
422                || self.tab
423                || self.menu_item.is_some())
424        {
425            return Err(Error::InvalidProps(
426                "clipboard source requires an independent intrinsic Button",
427            ));
428        }
429        if self.grid_multiple && (d.role != Some(Role::Grid) || !matches!(d.props, Props::View)) {
430            return Err(Error::InvalidProps(
431                "multiple grid selection requires a Grid View",
432            ));
433        }
434        if self.grid_navigation.is_some()
435            && (d.role != Some(Role::Grid)
436                || !matches!(d.props, Props::View)
437                || self.navigation.is_some()
438                || self.navigation_item
439                || self.typeahead)
440        {
441            return Err(Error::InvalidProps(
442                "grid navigation requires an independent Grid View",
443            ));
444        }
445        if let Some(capture) = self.input_edit {
446            capture.validate()?;
447            if !matches!(d.props,Props::Input(p) if !p.multiline&&p.value.len()<=capture.max_bytes)
448                || self.tags_input.is_some()
449                || self.combobox.is_some()
450            {
451                return Err(Error::InvalidProps(
452                    "input edit capture requires a bounded single-line Input or SpinButton",
453                ));
454            }
455        }
456        if let Some(translation) = self.translation {
457            translation.validate()?;
458            if !matches!(d.props, Props::View)
459                || !self.inert
460                || d.role.is_some()
461                || d.name.is_some()
462                || d.focus != crate::FocusMode::Auto
463                || self.collapse.is_some()
464                || self.presence.is_some()
465                || self.layer.is_some()
466                || self.drag_capture.is_some()
467                || self.document_part.is_some()
468                || self.field_part.is_some()
469            {
470                return Err(Error::InvalidProps(
471                    "translation requires an independent inert unnamed View without role, focus, layer, collapse or drag capture",
472                ));
473            }
474        }
475        if self.input_assist.is_some() && !matches!(d.props, Props::Input(p) if !p.multiline) {
476            return Err(Error::InvalidProps(
477                "input assist requires a single-line Input",
478            ));
479        }
480        if let Some(presence) = &self.presence {
481            presence.validate()?;
482            presence.validate_style(d.style)?;
483            if !matches!(d.props, Props::View)
484                || self.layer.is_some()
485                || self.collapse.is_some()
486                || self.translation.is_some()
487                || self.document_part.is_some()
488                || self.field_part.is_some()
489                || self.hidden == presence.open
490                || self.inert == presence.open
491            {
492                return Err(Error::InvalidProps(
493                    "presence requires an independent View and hidden/inert opposite to open",
494                ));
495            }
496        }
497        if let Some(collapse) = &self.collapse {
498            collapse.validate()?;
499            collapse.validate_style(d.style)?;
500            if !matches!(d.props, Props::View)
501                || self.presence.is_some()
502                || self.layer.is_some()
503                || self.document_part.is_some()
504                || self.field_part.is_some()
505                || self.hidden == collapse.open
506                || self.inert == collapse.open
507            {
508                return Err(Error::InvalidProps(
509                    "collapse requires an independent View and hidden/inert opposite to open",
510                ));
511            }
512        }
513        if self.swipe_capture.is_some()
514            && (!matches!(d.props, Props::View)
515                || d.role.is_some_and(|role| role != Role::Group)
516                || self.document_part.is_some()
517                || self.field_part.is_some()
518                || self.drag_capture.is_some()
519                || self.slider.is_some()
520                || self.splitter.is_some()
521                || self.sheet_grip
522                || self.layer.is_some()
523                || self.collapse.is_some()
524                || self.presence.is_some())
525        {
526            return Err(Error::InvalidProps(
527                "swipe capture requires an independent boxed View",
528            ));
529        }
530        if self.drag_capture.is_some()
531            && (!matches!(d.props, Props::View)
532                || self.slider.is_some()
533                || self.splitter.is_some()
534                || self.sheet_grip)
535        {
536            return Err(Error::InvalidProps(
537                "press drag capture requires an independent boxed View",
538            ));
539        }
540        if self.drag_capture == Some(crate::DragCapture::PressFocus)
541            && d.focus == crate::FocusMode::Auto
542        {
543            return Err(Error::InvalidProps(
544                "focus capture requires an explicit View focus mode",
545            ));
546        }
547        if d.role == Some(Role::Menubar)
548            && (self.id.is_none()
549                || self.navigation != Some(Orientation::Horizontal)
550                || d.focus != crate::FocusMode::Auto
551                || self.layer.is_some())
552        {
553            return Err(Error::InvalidProps(
554                "menubar requires a named horizontal container with roving child focus",
555            ));
556        }
557        if self.menubar_item
558            && (!matches!(d.props, Props::Button(_))
559                || !self
560                    .menu_item
561                    .as_ref()
562                    .is_some_and(|item| item.kind == MenuItemKind::Action)
563                || self.popup != Some(PopupKind::Menu)
564                || self.controls.is_none()
565                || self.id.is_none()
566                || !self.navigation_item
567                || d.focus == crate::FocusMode::Auto)
568        {
569            return Err(Error::InvalidProps(
570                "menubar root requires an intrinsic menuitem popup button with explicit roving focus",
571            ));
572        }
573        if self.context_menu.is_some()
574            && (!matches!(d.props, Props::View)
575                || d.effective_role() != Some(Role::Group)
576                || self.id.is_none()
577                || self.layer.is_some()
578                || self.popup.is_some()
579                || self.expanded.is_some()
580                || self.navigation.is_some())
581        {
582            return Err(Error::InvalidProps(
583                "context policy requires an independent named group region",
584            ));
585        }
586        if self.tags_input.is_some()
587            && (!matches!(d.props,crate::Props::Input(input) if !input.multiline)
588                || self.combobox.is_some()
589                || self.popup.is_some()
590                || self.controls.is_some()
591                || self.active_descendant.is_some()
592                || self.form_control.is_some())
593        {
594            return Err(Error::InvalidProps(
595                "tags policy requires an independent single-line search input",
596            ));
597        }
598        if self.sheet_grip
599            && (!matches!(d.props, Props::View)
600                || d.role.is_some()
601                || d.focus != crate::FocusMode::Auto
602                || self.layer.is_some()
603                || self.navigation.is_some())
604        {
605            return Err(Error::InvalidProps(
606                "sheet grip requires a passive generic view",
607            ));
608        }
609        if let Some(panel) = self.navigation_panel {
610            let valid = match panel {
611                crate::NavigationPanel::Trigger => {
612                    matches!(d.props, Props::Button(_))
613                        && self.navigation_item
614                        && self.id.is_some()
615                        && self.controls.is_some()
616                        && self.controls != self.id
617                        && self.expanded.is_some()
618                        && self.popup.is_none()
619                        && self.menu_item.is_none()
620                        && !self.tab
621                        && self.pressed.is_none()
622                        && self.form_action.is_none()
623                }
624                crate::NavigationPanel::Content => {
625                    matches!(d.props, Props::View)
626                        && d.role == Some(Role::Group)
627                        && d.focus == crate::FocusMode::Auto
628                        && self.id.is_some()
629                        && self.navigation == Some(Orientation::Vertical)
630                        && self.menu_item.is_none()
631                        && self.popup.is_none()
632                        && self.field_part.is_none()
633                        && self.document_part.is_none()
634                        && self.scroll_keyboard.is_none()
635                }
636            };
637            if !valid {
638                return Err(Error::InvalidProps(
639                    "navigation panels require an independent disclosure button or physical group content",
640                ));
641            }
642        }
643        if self.inert && !matches!(d.props, Props::View) {
644            return Err(Error::InvalidProps("inert regions require a view"));
645        }
646        self.validate_choice(d)?;
647        self.validate_menu_search(d)?;
648        if let Some(progress) = &self.progress {
649            progress.validate()?;
650            if d.role != Some(Role::ProgressBar)
651                || !matches!(d.props, Props::View)
652                || self.navigation.is_some()
653                || self.layer.is_some()
654                || self.field_part.is_some()
655                || self.document_part.is_some()
656                || self.splitter.is_some()
657                || self.menu_item.is_some()
658                || self.popup.is_some()
659                || self.pressed.is_some()
660                || self.expanded.is_some()
661                || self.scroll_keyboard.is_some()
662            {
663                return Err(Error::InvalidProps(
664                    "progress metadata requires an independent progress bar view",
665                ));
666            }
667        }
668        if let Some(spin) = &self.spinbutton {
669            spin.validate()?;
670            if !matches!(d.props, Props::Input(p) if !p.multiline && matches!(p.purpose, crate::Purpose::Text | crate::Purpose::Number))
671                || self.combobox.is_some()
672                || self.tags_input.is_some()
673                || self.popup.is_some()
674                || self.navigation.is_some()
675                || self.option.is_some()
676                || self.slider.is_some()
677                || self.splitter.is_some()
678            {
679                return Err(Error::InvalidProps(
680                    "spinbutton requires an independent single-line numeric text input",
681                ));
682            }
683            if (self.disabled || self.readonly || !d.props.enabled())
684                && (spin.can_increase || spin.can_decrease)
685            {
686                return Err(Error::InvalidProps(
687                    "disabled or readonly spinbuttons cannot advertise adjustment",
688                ));
689            }
690            if spin.value.is_some_and(|v| {
691                spin.min.is_some_and(|min| v < min) || spin.max.is_some_and(|max| v > max)
692            }) && !self.invalid
693            {
694                return Err(Error::InvalidProps(
695                    "outside spinbutton coordinates require invalid metadata",
696                ));
697            }
698        }
699        if let Some(slider) = &self.slider {
700            slider.validate()?;
701            if slider.range.min == slider.range.max && !self.readonly {
702                return Err(Error::InvalidProps(
703                    "a fixed slider interval requires readonly semantics",
704                ));
705            }
706            if !matches!(d.props, Props::View)
707                || d.role != Some(Role::Slider)
708                || d.focus == crate::FocusMode::Auto
709                || self.splitter.is_some()
710                || self.progress.is_some()
711                || self.separator_axis.is_some()
712                || self.navigation.is_some()
713                || self.navigation_item
714                || self.layer.is_some()
715                || self.field_part.is_some()
716                || self.document_part.is_some()
717                || self.tab
718                || self.pressed.is_some()
719                || self.expanded.is_some()
720                || self.popup.is_some()
721                || self.scroll_keyboard.is_some()
722                || self.menu_item.is_some()
723                || self.combobox.is_some()
724                || self.option.is_some()
725            {
726                return Err(Error::InvalidProps(
727                    "slider requires an independent focused range view",
728                ));
729            }
730        }
731        if let Some(range) = self.splitter {
732            range.validate()?;
733            if !matches!(d.props, Props::View)
734                || d.role != Some(Role::Separator)
735                || d.focus == crate::FocusMode::Auto
736                || self.separator_axis.is_none()
737                || self.controls.is_none()
738                || self.controls == self.id
739                || self.navigation.is_some()
740            {
741                return Err(Error::InvalidProps(
742                    "splitter requires a focused separator, orientation and distinct controlled pane",
743                ));
744            }
745        }
746        if self.scroll_keyboard.is_some()
747            && (!matches!(d.props, Props::View)
748                || self.navigation.is_some()
749                || self.navigation_item
750                || self.layer.is_some()
751                || self.field_part.is_some()
752                || self.document_part.is_some()
753                || d.role == Some(Role::Separator))
754        {
755            return Err(Error::InvalidProps(
756                "scroll keyboard requires an independent viewport view",
757            ));
758        }
759        if let Some(item) = &self.menu_item {
760            let valid = match (&d.props, item.kind) {
761                (Props::Button(_), MenuItemKind::Action) => {
762                    !self.tab && self.pressed.is_none() && self.form_action.is_none()
763                }
764                (Props::Check(check), MenuItemKind::Checkbox) => matches!(
765                    check.mode,
766                    crate::CheckMode::Checkbox | crate::CheckMode::Mixed
767                ),
768                (Props::Check(check), MenuItemKind::Radio) => {
769                    check.mode == crate::CheckMode::Radio && self.check_group.is_some()
770                }
771                _ => false,
772            };
773            if !valid
774                || self.form_control.is_some()
775                || self.id.as_ref() == Some(&item.owner)
776                || self.popup.is_some_and(|popup| popup != PopupKind::Menu)
777            {
778                return Err(Error::InvalidProps(
779                    "menu item requires a matching intrinsic control and distinct menu owner",
780                ));
781            }
782        }
783        if self.current.is_some()
784            && !matches!(d.props, Props::Link(_) | Props::Button(_) | Props::Text(_))
785        {
786            return Err(Error::InvalidProps("current requires link, button or text"));
787        }
788        if (self.typeahead || self.navigation_direction != Direction::Ltr)
789            && self.navigation.is_none()
790            && self.combobox != Some(crate::ComboboxKind::Select)
791        {
792            return Err(Error::InvalidProps(
793                "direction and typeahead require a navigation boundary",
794            ));
795        }
796        if self.separator_axis.is_some() && d.role != Some(Role::Separator) {
797            return Err(Error::InvalidProps(
798                "separator orientation requires a separator role",
799            ));
800        }
801        if (self.link_open.is_some() || self.link_request) && !matches!(d.props, Props::Link(_)) {
802            return Err(Error::InvalidProps("link context requires a link"));
803        }
804        if let Some(table) = &self.table {
805            table.validate(d)?;
806        }
807        if self.sort.is_some()
808            && (!matches!(d.props, Props::View)
809                || !(self.document_part == Some(crate::DocumentPart::ColumnHeader)
810                    || d.role == Some(Role::ColumnHeader)))
811        {
812            return Err(Error::InvalidProps(
813                "sort direction requires a structural column header",
814            ));
815        }
816        if self.document_part.is_some()
817            && (!matches!(d.props, Props::View)
818                || d.role.is_some()
819                || d.focus != crate::FocusMode::Auto
820                || self.field_part.is_some()
821                || self.layer.is_some()
822                || self.navigation.is_some())
823        {
824            return Err(Error::InvalidProps(
825                "document parts require an unfocused structural view",
826            ));
827        }
828        if let Some(group) = &self.check_group
829            && (!matches!(&d.props, Props::Check(check) if check.mode == crate::CheckMode::Radio)
830                || self
831                    .form_control
832                    .as_ref()
833                    .is_some_and(|control| control.name != group.as_str()))
834        {
835            return Err(Error::InvalidProps(
836                "radio association requires a radio and matching successful-control name",
837            ));
838        }
839        if self.form_action.is_some()
840            && (!matches!(d.props, Props::Button(_))
841                || self.tab
842                || self.popup.is_some()
843                || self.expanded.is_some()
844                || self.pressed.is_some())
845        {
846            return Err(Error::InvalidProps("form actions require ordinary buttons"));
847        }
848        if let Some(control) = &self.form_control
849            && (!matches!(
850                d.props,
851                Props::Input(_) | Props::Select(_) | Props::Check(_) | Props::Button(_)
852            ) && d.role != Some(Role::Slider)
853                || control.name.len() > 256
854                || control.name.contains(['\0', '\r', '\n'])
855                || control
856                    .value
857                    .as_ref()
858                    .is_some_and(|value| value.contains(['\0', '\r']))
859                || control.value.is_some() && matches!(d.props, Props::Input(_) | Props::Select(_)))
860        {
861            return Err(Error::InvalidProps("invalid successful-control metadata"));
862        }
863        if self.field_part.is_some()
864            && (!matches!(d.props, Props::View)
865                || d.role.is_some()
866                || d.focus != crate::FocusMode::Auto
867                || self.layer.is_some()
868                || self.navigation.is_some())
869        {
870            return Err(Error::InvalidProps(
871                "form parts require an unfocused structural view",
872            ));
873        }
874        if self.label_for.is_some() && self.field_part != Some(FieldPart::Label) {
875            return Err(Error::InvalidProps(
876                "label association requires a label part",
877            ));
878        }
879        if self.label_for.is_some() && self.label_for == self.id {
880            return Err(Error::InvalidProps("a label cannot target itself"));
881        }
882        if self.readonly && matches!(d.props, Props::Select(_)) {
883            return Err(Error::Unsupported("native select has no readonly state"));
884        }
885        let input_field = self.combobox.is_some()
886            || matches!(
887                d.props,
888                Props::Input(_) | Props::Select(_) | Props::Check(_) | Props::RichEditor(_)
889            )
890            || matches!(d.role, Some(Role::RadioGroup | Role::Slider));
891        if (self.required && !input_field)
892            || (self.readonly && !input_field && d.role != Some(Role::Grid))
893            || ((self.invalid || self.error_message.is_some())
894                && !input_field
895                && self.field_part != Some(FieldPart::Fieldset))
896        {
897            return Err(Error::InvalidProps(
898                "field states require an input or check",
899            ));
900        }
901        if self.popup.is_some()
902            && ((!matches!(d.props, Props::Button(_)) && self.combobox.is_none()) || self.tab)
903        {
904            return Err(Error::InvalidProps(
905                "popup triggers require a non-tab button",
906            ));
907        }
908        if self.pressed.is_some()
909            && (!matches!(d.props, Props::Button(_)) || self.tab || self.popup.is_some())
910        {
911            return Err(Error::InvalidProps(
912                "pressed requires a non-tab toggle button",
913            ));
914        }
915        if let Some(layer) = &self.layer {
916            if let LayerPlacement::Viewport(placement) = layer.placement {
917                placement.validate()?;
918                if d.role != Some(Role::Dialog) || layer.anchor.is_some() || layer.sheet.is_some() {
919                    return Err(Error::InvalidProps(
920                        "viewport positioning requires an unanchored dialog",
921                    ));
922                }
923            }
924            if let LayerPlacement::Anchor(placement) = layer.placement {
925                placement.validate()?;
926            }
927            if let LayerPlacement::ContextPoint(point) = layer.placement {
928                point
929                    .validate()
930                    .map_err(|_| Error::InvalidProps("context layer point must be finite"))?;
931                if d.role != Some(Role::Menu) {
932                    return Err(Error::InvalidProps(
933                        "context point positioning requires a menu",
934                    ));
935                }
936            }
937            if let Some(sheet) = &layer.sheet {
938                sheet.validate()?;
939                if d.style
940                    .position
941                    .is_some_and(|p| p != crate::style::Position::Fixed)
942                {
943                    return Err(Error::InvalidProps("sheet owns fixed viewport positioning"));
944                }
945                if d.role != Some(Role::Dialog)
946                    || !crate::sheet::edge(layer.placement)
947                    || layer.focus != LayerFocus::Manage
948                    || layer.anchor.is_some()
949                {
950                    return Err(Error::InvalidProps(
951                        "sheet requires an edge dialog with managed focus",
952                    ));
953                }
954            }
955            if !matches!(d.props, Props::View)
956                || !matches!(
957                    d.role,
958                    Some(
959                        Role::Dialog
960                            | Role::AlertDialog
961                            | Role::Menu
962                            | Role::Tooltip
963                            | Role::Listbox
964                            | Role::Tree
965                            | Role::Group
966                    )
967                )
968                || self.id.is_none()
969                || (!matches!(d.role, Some(Role::Tooltip | Role::Listbox))
970                    && d.focus == crate::FocusMode::Auto
971                    && d.role != Some(Role::Group))
972                || (layer.focus == LayerFocus::Preserve
973                    && (layer.modal
974                        || layer.initial_focus.is_some()
975                        || layer.return_focus.is_some()))
976                || (d.role == Some(Role::Tooltip)
977                    && (layer.focus != LayerFocus::Preserve
978                        || d.focus != crate::FocusMode::Auto
979                        || self.navigation.is_some()
980                        || self.popup.is_some()
981                        || self.expanded.is_some()))
982                || (matches!(d.role, Some(Role::Dialog | Role::AlertDialog))
983                    && self.navigation.is_some())
984                || (d.role == Some(Role::AlertDialog)
985                    && (!layer.modal
986                        || layer.focus != LayerFocus::Manage
987                        || layer.initial_focus.is_none()))
988                || (d.role == Some(Role::Listbox)
989                    && (layer.modal
990                        || layer.focus != LayerFocus::Preserve
991                        || d.focus != crate::FocusMode::Auto
992                        || self.navigation != Some(Orientation::Vertical)))
993                || (d.role == Some(Role::Tree)
994                    && (layer.modal
995                        || !matches!(layer.focus, LayerFocus::Manage | LayerFocus::Preserve)
996                        || self.tree_navigation.is_none()
997                        || d.focus == crate::FocusMode::Auto))
998                || (d.role == Some(Role::Menu)
999                    && (layer.modal
1000                        || layer.focus != LayerFocus::Manage
1001                        || self.navigation != Some(Orientation::Vertical)))
1002                || (d.role == Some(Role::Group)
1003                    && (self.navigation_panel != Some(crate::NavigationPanel::Content)
1004                        || layer.modal
1005                        || layer.focus != LayerFocus::Preserve
1006                        || d.focus != crate::FocusMode::Auto))
1007            {
1008                return Err(Error::InvalidProps(
1009                    "layers require a valid named dialog, alert decision, menu or passive tooltip focus policy",
1010                ));
1011            }
1012            if matches!(
1013                layer.placement,
1014                LayerPlacement::Anchor(_) | LayerPlacement::ContextPoint(_)
1015            ) != layer.anchor.is_some()
1016                || layer
1017                    .anchor
1018                    .as_ref()
1019                    .is_some_and(|id| Some(id) == self.id.as_ref())
1020            {
1021                return Err(Error::InvalidProps(
1022                    "anchored layers require a distinct anchor",
1023                ));
1024            }
1025        }
1026        if matches!(d.props, Props::Group) {
1027            return Err(Error::InvalidProps(
1028                "group has no composite widget metadata",
1029            ));
1030        }
1031        if (self.tab
1032            || (self.expanded.is_some() && d.role != Some(Role::TreeItem))
1033            || (self.disabled
1034                && self.splitter.is_none()
1035                && self.slider.is_none()
1036                && self.context_menu.is_none()
1037                && self.drag_capture.is_none()
1038                && self.swipe_capture.is_none()
1039                && d.role != Some(Role::Menubar)
1040                && !matches!(d.role, Some(Role::Tree | Role::TreeItem))
1041                && !matches!(self.field_part, Some(FieldPart::Fieldset | FieldPart::Form))))
1042            && !matches!(d.props, Props::Button(_))
1043            && self.combobox.is_none()
1044        {
1045            return Err(Error::InvalidProps(
1046                "composite trigger metadata requires a button",
1047            ));
1048        }
1049        if self.navigation_item
1050            && !matches!(&d.props, Props::Button(_) | Props::Link(_))
1051            && !matches!(&d.props, Props::Check(check) if check.mode == crate::CheckMode::Radio)
1052            && self.menu_item.is_none()
1053        {
1054            return Err(Error::InvalidProps(
1055                "navigation items require a button, link or radio",
1056            ));
1057        }
1058        if self.selected.is_some()
1059            && !self.tab
1060            && self.option.is_none()
1061            && d.role != Some(Role::GridCell)
1062            && d.role != Some(Role::TreeItem)
1063            || self.tab && (self.expanded.is_some() || self.selected.is_none())
1064        {
1065            return Err(Error::InvalidProps(
1066                "selected belongs to tabs; tabs cannot be disclosures",
1067            ));
1068        }
1069        if self.navigation.is_some() && !matches!(d.props, Props::View) {
1070            return Err(Error::InvalidProps(
1071                "hidden panels and navigation boundaries require a view",
1072            ));
1073        }
1074        if self.hidden && !matches!(d.props, Props::View) && self.option.is_none() {
1075            return Err(Error::InvalidProps(
1076                "hidden content requires a view or intrinsic option",
1077            ));
1078        }
1079        if self.labelled_by.is_some()
1080            && (matches!(&d.props, Props::Text(p) if !matches!(p.kind, crate::TextKind::Heading(_)))
1081                || matches!(&d.props, Props::Image(p) if p.alternative.is_none())
1082                || matches!(d.props, Props::Graphic(p) if p.alternative.is_none()))
1083        {
1084            return Err(Error::InvalidProps(
1085                "this node cannot have a naming relation",
1086            ));
1087        }
1088        if self.controls.is_some() && self.id == self.controls {
1089            return Err(Error::InvalidProps("a control cannot control itself"));
1090        }
1091        if self.labelled_by.is_some() && self.id == self.labelled_by {
1092            return Err(Error::InvalidProps("a node cannot label itself"));
1093        }
1094        if self.navigation.is_some()
1095            && !matches!(
1096                d.role,
1097                None | Some(
1098                    Role::Group
1099                        | Role::TabList
1100                        | Role::RadioGroup
1101                        | Role::Toolbar
1102                        | Role::Menu
1103                        | Role::Menubar
1104                        | Role::Listbox
1105                )
1106            )
1107        {
1108            return Err(Error::InvalidProps(
1109                "navigation requires a generic group or tab list",
1110            ));
1111        }
1112        Ok(())
1113    }
1114}