1use crate::{Error, Navigation, Props, Role};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum FieldPart {
9 Form,
10 Label,
11 Fieldset,
12 Legend,
13}
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum FormAction {
18 Submit,
19 Reset,
20}
21
22#[derive(Clone, Debug, PartialEq, Eq)]
25pub struct FormControl {
26 pub name: String,
27 pub value: Option<String>,
28}
29
30#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
33pub enum LinkOpen {
34 #[default]
35 Current,
36 NewContext,
37}
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum Current {
42 Page,
43 Step,
44 Location,
45 Date,
46}
47#[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#[derive(Clone, Debug, PartialEq, Eq)]
74pub struct MenuItemSemantics {
75 pub kind: MenuItemKind,
76 pub owner: SemanticId,
77}
78
79#[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 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 AnchorLost,
148}
149
150#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
151pub struct SemanticId(String);
152impl SemanticId {
153 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 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#[derive(Clone, Debug, Default, PartialEq)]
241pub struct Semantics {
242 pub environment: Option<crate::Environment>,
244 pub focus_scope: Option<crate::FocusScopeConfig>,
246 pub clipboard: Option<crate::ClipboardText>,
249 pub sort: Option<crate::SortDirection>,
251 pub input_edit: Option<crate::InputEditCapture>,
253 pub translation: Option<crate::TranslationMotion>,
255 pub input_assist: Option<crate::InputAssist>,
257 pub placeholder: Option<String>,
260 pub collapse: Option<crate::CollapseConfig>,
262 pub presence: Option<Box<crate::PresenceConfig>>,
264 pub drag_capture: Option<crate::DragCapture>,
266 pub swipe_capture: Option<crate::SwipeCapture>,
268 pub context_menu: Option<crate::ContextEditPolicy>,
270 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 pub scroll_keyboard: Option<crate::ScrollAxes>,
278 pub busy: Option<bool>,
280 pub current: Option<Current>,
281 pub separator_axis: Option<Orientation>,
282 pub link_open: Option<LinkOpen>,
283 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 pub menu_search: Option<SemanticId>,
308 pub menubar_item: bool,
310 pub layer: Option<Layer>,
311 pub sheet_grip: bool,
313 pub expanded: Option<bool>,
314 pub selected: Option<bool>,
315 pub pressed: Option<bool>,
317 pub tab: bool,
319 pub disabled: bool,
322 pub hidden: bool,
324 pub inert: bool,
327 pub navigation: Option<Orientation>,
329 pub grid_navigation: Option<Direction>,
332 pub grid_multiple: bool,
334 pub tree_navigation: Option<crate::TreeNavigation>,
335 pub tree_item: Option<crate::TreeItem>,
336 pub tree_checked: Option<crate::TreeCheckState>,
338 pub navigation_item: bool,
339 pub navigation_panel: Option<crate::NavigationPanel>,
340 pub check_group: Option<SemanticId>,
343 pub navigation_direction: Direction,
344 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(_) | Props::Link(_), 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 && !matches!(d.props, Props::Link(_))
1035 && self.splitter.is_none()
1036 && self.slider.is_none()
1037 && self.context_menu.is_none()
1038 && self.drag_capture.is_none()
1039 && self.swipe_capture.is_none()
1040 && d.role != Some(Role::Menubar)
1041 && !matches!(d.role, Some(Role::Tree | Role::TreeItem))
1042 && !matches!(self.field_part, Some(FieldPart::Fieldset | FieldPart::Form))))
1043 && !matches!(d.props, Props::Button(_))
1044 && self.combobox.is_none()
1045 {
1046 return Err(Error::InvalidProps(
1047 "composite trigger metadata requires a button",
1048 ));
1049 }
1050 if self.navigation_item
1051 && !matches!(&d.props, Props::Button(_) | Props::Link(_))
1052 && !matches!(&d.props, Props::Check(check) if check.mode == crate::CheckMode::Radio)
1053 && self.menu_item.is_none()
1054 {
1055 return Err(Error::InvalidProps(
1056 "navigation items require a button, link or radio",
1057 ));
1058 }
1059 if self.selected.is_some()
1060 && !self.tab
1061 && self.option.is_none()
1062 && d.role != Some(Role::GridCell)
1063 && d.role != Some(Role::TreeItem)
1064 || self.tab && (self.expanded.is_some() || self.selected.is_none())
1065 {
1066 return Err(Error::InvalidProps(
1067 "selected belongs to tabs; tabs cannot be disclosures",
1068 ));
1069 }
1070 if self.navigation.is_some() && !matches!(d.props, Props::View) {
1071 return Err(Error::InvalidProps(
1072 "hidden panels and navigation boundaries require a view",
1073 ));
1074 }
1075 if self.hidden && !matches!(d.props, Props::View) && self.option.is_none() {
1076 return Err(Error::InvalidProps(
1077 "hidden content requires a view or intrinsic option",
1078 ));
1079 }
1080 if self.labelled_by.is_some()
1081 && (matches!(&d.props, Props::Text(p) if !matches!(p.kind, crate::TextKind::Heading(_)))
1082 || matches!(&d.props, Props::Image(p) if p.alternative.is_none())
1083 || matches!(d.props, Props::Graphic(p) if p.alternative.is_none()))
1084 {
1085 return Err(Error::InvalidProps(
1086 "this node cannot have a naming relation",
1087 ));
1088 }
1089 if self.controls.is_some() && self.id == self.controls {
1090 return Err(Error::InvalidProps("a control cannot control itself"));
1091 }
1092 if self.labelled_by.is_some() && self.id == self.labelled_by {
1093 return Err(Error::InvalidProps("a node cannot label itself"));
1094 }
1095 if self.navigation.is_some()
1096 && !matches!(
1097 d.role,
1098 None | Some(
1099 Role::Group
1100 | Role::TabList
1101 | Role::RadioGroup
1102 | Role::Toolbar
1103 | Role::Menu
1104 | Role::Menubar
1105 | Role::Listbox
1106 )
1107 )
1108 {
1109 return Err(Error::InvalidProps(
1110 "navigation requires a generic group or tab list",
1111 ));
1112 }
1113 Ok(())
1114 }
1115}