use crate::Rect;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SemanticsNumericValue {
pub value: f64,
pub min: f64,
pub max: f64,
pub step: f64,
}
#[derive(Clone, Debug)]
pub struct WidgetSemantics {
pub role: SemanticsRole,
pub label: Option<String>,
pub value: Option<String>,
pub toggled: Option<bool>,
pub expanded: Option<bool>,
pub selected: Option<bool>,
pub enabled: bool,
pub focusable: bool,
pub numeric: Option<SemanticsNumericValue>,
pub modal: bool,
pub live: Option<LiveRegion>,
pub virtual_items: Vec<VirtualSemanticItem>,
pub text_selection: Option<(usize, usize)>,
pub read_only: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VirtualSemanticItem {
pub id: u64,
pub role: SemanticsRole,
pub label: String,
pub selected: Option<bool>,
pub expanded: Option<bool>,
pub enabled: bool,
pub bounds: Rect,
pub children: Vec<VirtualSemanticItem>,
pub text_run: Option<VirtualTextRun>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VirtualTextRun {
pub character_lengths: Vec<u8>,
pub start_char: usize,
pub character_positions: Option<Vec<f32>>,
pub character_widths: Option<Vec<f32>>,
pub rtl: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LiveRegion {
Polite,
Assertive,
}
impl WidgetSemantics {
pub fn new(role: SemanticsRole) -> Self {
Self {
role,
label: None,
value: None,
toggled: None,
expanded: None,
selected: None,
enabled: true,
focusable: matches!(
role,
SemanticsRole::Button | SemanticsRole::CheckBox | SemanticsRole::Slider | SemanticsRole::TextInput
),
numeric: None,
modal: false,
live: None,
virtual_items: Vec::new(),
text_selection: None,
read_only: false,
}
}
pub fn with_read_only(mut self, read_only: bool) -> Self {
self.read_only = read_only;
self
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.value = Some(value.into());
self
}
pub fn with_toggled(mut self, v: bool) -> Self {
self.toggled = Some(v);
self
}
pub fn with_expanded(mut self, expanded: bool) -> Self {
self.expanded = Some(expanded);
self
}
pub fn with_selected(mut self, selected: bool) -> Self {
self.selected = Some(selected);
self
}
pub fn with_focusable(mut self, focusable: bool) -> Self {
self.focusable = focusable && self.enabled;
self
}
pub fn with_modal(mut self, modal: bool) -> Self {
self.modal = modal;
self
}
pub fn with_live(mut self, live: LiveRegion) -> Self {
self.live = Some(live);
self
}
pub fn with_virtual_items(mut self, items: Vec<VirtualSemanticItem>) -> Self {
self.virtual_items = items;
self
}
pub fn with_text_selection(mut self, anchor: usize, focus: usize) -> Self {
self.text_selection = Some((anchor, focus));
self
}
pub fn with_enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
if !enabled {
self.focusable = false;
}
self
}
pub fn with_numeric_value(mut self, value: f64, min: f64, max: f64, step: f64) -> Self {
self.numeric = Some(SemanticsNumericValue { value, min, max, step });
self
}
pub fn non_focusable(mut self) -> Self {
self.focusable = false;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SemanticsRole {
Button,
CheckBox,
Slider,
TextInput,
TextRun,
Label,
Image,
Container,
Unknown,
Dialog,
Tab,
TabList,
TabPanel,
List,
ListItem,
ListOption,
Grid,
GridRow,
GridCell,
Tree,
TreeItem,
Progress,
Toast,
Menu,
MenuItem,
ComboBox,
RadioGroup,
RadioButton,
Tooltip,
Splitter,
}
#[derive(Clone, Debug)]
pub struct SemanticsData {
pub role: SemanticsRole,
pub label: Option<String>,
pub value: Option<String>,
pub toggled: Option<bool>,
pub expanded: Option<bool>,
pub selected: Option<bool>,
pub modal: bool,
pub live: Option<LiveRegion>,
pub virtual_items: Vec<VirtualSemanticItem>,
pub text_selection: Option<(usize, usize)>,
pub read_only: bool,
pub enabled: bool,
pub bounds: Rect,
pub children: Vec<crate::id::ElementId>,
pub focusable: bool,
pub numeric: Option<SemanticsNumericValue>,
}
impl SemanticsData {
pub fn children(&self) -> &[crate::id::ElementId] {
&self.children
}
pub fn new(role: SemanticsRole, bounds: Rect) -> Self {
Self {
role,
label: None,
value: None,
toggled: None,
expanded: None,
selected: None,
modal: false,
live: None,
virtual_items: Vec::new(),
text_selection: None,
read_only: false,
enabled: true,
bounds,
children: Vec::new(),
focusable: matches!(
role,
SemanticsRole::Button | SemanticsRole::CheckBox | SemanticsRole::Slider | SemanticsRole::TextInput
),
numeric: None,
}
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
pub fn with_numeric_value(mut self, value: f64, min: f64, max: f64, step: f64) -> Self {
self.numeric = Some(SemanticsNumericValue { value, min, max, step });
self
}
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.value = Some(value.into());
self
}
pub fn with_toggled(mut self, v: bool) -> Self {
self.toggled = Some(v);
self
}
pub fn with_expanded(mut self, expanded: bool) -> Self {
self.expanded = Some(expanded);
self
}
pub fn with_selected(mut self, selected: bool) -> Self {
self.selected = Some(selected);
self
}
pub fn with_modal(mut self, modal: bool) -> Self {
self.modal = modal;
self
}
pub fn with_live(mut self, live: LiveRegion) -> Self {
self.live = Some(live);
self
}
pub fn with_children(mut self, children: Vec<crate::id::ElementId>) -> Self {
self.children = children;
self
}
pub fn with_text_selection(mut self, anchor: usize, focus: usize) -> Self {
self.text_selection = Some((anchor, focus));
self
}
}