use std::rc::Rc;
use waft_embedder::{Locale, TextDirection};
use waft_foundation::App;
use crate::object::{RenderHandle, RenderObject};
use crate::semantics::{AttributedString, SemanticsProperties};
#[derive(Debug, Default)]
pub struct SemanticsAnnotationsData {
properties: Option<Rc<SemanticsProperties>>,
container: bool,
explicit_child_nodes: bool,
exclude_semantics: bool,
block_user_actions: bool,
locale_for_subtree: Option<Locale>,
attributed_label: Option<AttributedString>,
attributed_value: Option<AttributedString>,
attributed_increased_value: Option<AttributedString>,
attributed_decreased_value: Option<AttributedString>,
attributed_hint: Option<AttributedString>,
text_direction: Option<TextDirection>,
}
impl SemanticsAnnotationsData {
pub fn new() -> SemanticsAnnotationsData {
SemanticsAnnotationsData::default()
}
}
pub trait SemanticsAnnotationsMixin: RenderObject {
fn semantics_annotations_data(self: RenderHandle<Self>, app: &App)
-> &SemanticsAnnotationsData;
fn semantics_annotations_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut SemanticsAnnotationsData;
#[allow(clippy::too_many_arguments)]
fn init_semantics_annotations(
self: RenderHandle<Self>,
app: &mut App,
properties: Rc<SemanticsProperties>,
container: bool,
explicit_child_nodes: bool,
exclude_semantics: bool,
block_user_actions: bool,
locale_for_subtree: Option<Locale>,
text_direction: Option<TextDirection>,
) {
let data = self.semantics_annotations_data_mut(app);
data.properties = Some(properties.clone());
data.container = container;
data.explicit_child_nodes = explicit_child_nodes;
data.exclude_semantics = exclude_semantics;
data.block_user_actions = block_user_actions;
data.locale_for_subtree = locale_for_subtree;
data.text_direction = text_direction;
self.update_attributed_fields(app, &properties);
}
fn properties(self: RenderHandle<Self>, app: &App) -> Rc<SemanticsProperties> {
self.semantics_annotations_data(app)
.properties
.clone()
.expect("set by init_semantics_annotations")
}
fn set_properties(self: RenderHandle<Self>, app: &mut App, value: Rc<SemanticsProperties>) {
if Rc::ptr_eq(&self.properties(app), &value) {
return;
}
self.semantics_annotations_data_mut(app).properties = Some(value.clone());
self.update_attributed_fields(app, &value);
}
fn container(self: RenderHandle<Self>, app: &App) -> bool {
self.semantics_annotations_data(app).container
}
fn set_container(self: RenderHandle<Self>, app: &mut App, value: bool) {
if self.container(app) == value {
return;
}
self.semantics_annotations_data_mut(app).container = value;
}
fn explicit_child_nodes(self: RenderHandle<Self>, app: &App) -> bool {
self.semantics_annotations_data(app).explicit_child_nodes
}
fn set_explicit_child_nodes(self: RenderHandle<Self>, app: &mut App, value: bool) {
if self.semantics_annotations_data(app).explicit_child_nodes == value {
return;
}
self.semantics_annotations_data_mut(app)
.explicit_child_nodes = value;
}
fn exclude_semantics(self: RenderHandle<Self>, app: &App) -> bool {
self.semantics_annotations_data(app).exclude_semantics
}
fn set_exclude_semantics(self: RenderHandle<Self>, app: &mut App, value: bool) {
if self.semantics_annotations_data(app).exclude_semantics == value {
return;
}
self.semantics_annotations_data_mut(app).exclude_semantics = value;
}
fn block_user_actions(self: RenderHandle<Self>, app: &App) -> bool {
self.semantics_annotations_data(app).block_user_actions
}
fn set_block_user_actions(self: RenderHandle<Self>, app: &mut App, value: bool) {
if self.semantics_annotations_data(app).block_user_actions == value {
return;
}
self.semantics_annotations_data_mut(app).block_user_actions = value;
}
fn locale_for_subtree(self: RenderHandle<Self>, app: &App) -> Option<Locale> {
self.semantics_annotations_data(app)
.locale_for_subtree
.clone()
}
fn set_locale_for_subtree(self: RenderHandle<Self>, app: &mut App, value: Option<Locale>) {
if self.semantics_annotations_data(app).locale_for_subtree == value {
return;
}
self.semantics_annotations_data_mut(app).locale_for_subtree = value;
}
fn update_attributed_fields(
self: RenderHandle<Self>,
app: &mut App,
value: &SemanticsProperties,
) {
let attributed_label = effective_attributed_label(value);
let attributed_value = effective_attributed_value(value);
let attributed_increased_value = effective_attributed_increased_value(value);
let attributed_decreased_value =
effective_attributed_decreased_value(&self.properties(app), value);
let attributed_hint = effective_attributed_hint(value);
let data = self.semantics_annotations_data_mut(app);
data.attributed_label = attributed_label;
data.attributed_value = attributed_value;
data.attributed_increased_value = attributed_increased_value;
data.attributed_decreased_value = attributed_decreased_value;
data.attributed_hint = attributed_hint;
}
fn attributed_label(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
self.semantics_annotations_data(app)
.attributed_label
.clone()
}
fn attributed_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
self.semantics_annotations_data(app)
.attributed_value
.clone()
}
fn attributed_increased_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
self.semantics_annotations_data(app)
.attributed_increased_value
.clone()
}
fn attributed_decreased_value(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
self.semantics_annotations_data(app)
.attributed_decreased_value
.clone()
}
fn attributed_hint(self: RenderHandle<Self>, app: &App) -> Option<AttributedString> {
self.semantics_annotations_data(app).attributed_hint.clone()
}
fn text_direction(self: RenderHandle<Self>, app: &App) -> Option<TextDirection> {
self.semantics_annotations_data(app).text_direction
}
fn set_text_direction(self: RenderHandle<Self>, app: &mut App, value: Option<TextDirection>) {
if self.text_direction(app) == value {
return;
}
self.semantics_annotations_data_mut(app).text_direction = value;
}
}
fn effective_attributed_label(value: &SemanticsProperties) -> Option<AttributedString> {
value
.attributed_label
.clone()
.or_else(|| value.label.clone().map(AttributedString::new))
}
fn effective_attributed_value(value: &SemanticsProperties) -> Option<AttributedString> {
value
.attributed_value
.clone()
.or_else(|| value.value.clone().map(AttributedString::new))
}
fn effective_attributed_increased_value(value: &SemanticsProperties) -> Option<AttributedString> {
value
.attributed_increased_value
.clone()
.or_else(|| value.increased_value.clone().map(AttributedString::new))
}
fn effective_attributed_decreased_value(
properties: &SemanticsProperties,
value: &SemanticsProperties,
) -> Option<AttributedString> {
properties
.attributed_decreased_value
.clone()
.or_else(|| value.decreased_value.clone().map(AttributedString::new))
}
fn effective_attributed_hint(value: &SemanticsProperties) -> Option<AttributedString> {
value
.attributed_hint
.clone()
.or_else(|| value.hint.clone().map(AttributedString::new))
}