use accesskit::NodeId;
use crate::props::{A11yNodeProps, CheckedState, TextSelection};
use crate::tree::{A11yNode, WidgetRole};
pub struct A11yNodeBuilder {
id: NodeId,
role: WidgetRole,
label: Option<String>,
props: A11yNodeProps,
children: Vec<NodeId>,
text_content: Option<String>,
}
impl A11yNodeBuilder {
pub fn new(id: NodeId, role: WidgetRole) -> Self {
Self {
id,
role,
label: None,
props: A11yNodeProps::default(),
children: Vec::new(),
text_content: None,
}
}
pub fn label(mut self, s: impl Into<String>) -> Self {
self.label = Some(s.into());
self
}
pub fn description(mut self, s: impl Into<String>) -> Self {
self.props.description = Some(s.into());
self
}
pub fn placeholder(mut self, s: impl Into<String>) -> Self {
self.props.placeholder = Some(s.into());
self
}
pub fn key_shortcut(mut self, s: impl Into<String>) -> Self {
self.props.key_shortcut = Some(s.into());
self
}
pub fn disabled(mut self) -> Self {
self.props.disabled = true;
self
}
pub fn expanded(mut self, v: bool) -> Self {
self.props.expanded = Some(v);
self
}
pub fn selected(mut self, v: bool) -> Self {
self.props.selected = Some(v);
self
}
pub fn checked(mut self, v: CheckedState) -> Self {
self.props.checked = Some(v);
self
}
pub fn value(mut self, now: f64, min: f64, max: f64, step: f64) -> Self {
self.props.value_now = Some(now);
self.props.value_min = Some(min);
self.props.value_max = Some(max);
self.props.value_step = Some(step);
self
}
pub fn text(mut self, v: impl Into<String>) -> Self {
self.text_content = Some(v.into());
self
}
pub fn text_selection(mut self, sel: TextSelection) -> Self {
self.props.text_selection = Some(sel);
self
}
pub fn labelled_by(mut self, ids: impl IntoIterator<Item = NodeId>) -> Self {
self.props.labelled_by = ids.into_iter().collect();
self
}
pub fn described_by(mut self, ids: impl IntoIterator<Item = NodeId>) -> Self {
self.props.described_by = ids.into_iter().collect();
self
}
pub fn controlled_by(mut self, ids: impl IntoIterator<Item = NodeId>) -> Self {
self.props.controlled_by = ids.into_iter().collect();
self
}
pub fn owns(mut self, ids: impl IntoIterator<Item = NodeId>) -> Self {
self.props.owns = ids.into_iter().collect();
self
}
pub fn tab_index(mut self, i: u32) -> Self {
self.props.tab_index = Some(i);
self
}
pub fn child(mut self, id: NodeId) -> Self {
self.children.push(id);
self
}
pub fn build(self) -> A11yNode {
A11yNode {
id: self.id,
role: self.role,
label: self.label,
children: Vec::new(),
props: self.props,
text_content: self.text_content,
}
}
pub fn build_with_children(self, children: Vec<A11yNode>) -> A11yNode {
A11yNode {
id: self.id,
role: self.role,
label: self.label,
children,
props: self.props,
text_content: self.text_content,
}
}
}