#![allow(dead_code)]
use plushie::automation::Element;
use plushie::event::{Event, EventType, WidgetEvent};
use plushie_core::ScopedId;
use plushie_core::protocol::{Props, TreeNode};
use serde_json::{Value, json};
pub fn click_event(id: &str) -> Event {
Event::Widget(WidgetEvent {
event_type: EventType::Click,
scoped_id: ScopedId::new(id, vec![], Some("main".to_string())),
value: Value::Null,
})
}
pub fn input_event(id: &str, text: &str) -> Event {
Event::Widget(WidgetEvent {
event_type: EventType::Input,
scoped_id: ScopedId::new(id, vec![], Some("main".to_string())),
value: json!(text),
})
}
pub fn toggle_event(id: &str, checked: bool) -> Event {
Event::Widget(WidgetEvent {
event_type: EventType::Toggle,
scoped_id: ScopedId::new(id, vec![], Some("main".to_string())),
value: json!(checked),
})
}
pub fn slide_event(id: &str, value: f64) -> Event {
Event::Widget(WidgetEvent {
event_type: EventType::Slide,
scoped_id: ScopedId::new(id, vec![], Some("main".to_string())),
value: json!(value),
})
}
pub fn scoped_click(id: &str, scope: Vec<&str>) -> Event {
Event::Widget(WidgetEvent {
event_type: EventType::Click,
scoped_id: ScopedId::new(
id,
scope.into_iter().map(String::from).collect(),
Some("main".to_string()),
),
value: Value::Null,
})
}
pub fn text_node(id: &str, content: &str) -> TreeNode {
let mut props = serde_json::Map::new();
props.insert("content".to_string(), json!(content));
TreeNode {
id: id.to_string(),
type_name: "text".to_string(),
props: Props::from_json(Value::Object(props)),
children: vec![],
}
}
pub fn button_node(id: &str, label: &str) -> TreeNode {
let mut props = serde_json::Map::new();
props.insert("label".to_string(), json!(label));
TreeNode {
id: id.to_string(),
type_name: "button".to_string(),
props: Props::from_json(Value::Object(props)),
children: vec![],
}
}
pub fn container_node(id: &str, children: Vec<TreeNode>) -> TreeNode {
TreeNode {
id: id.to_string(),
type_name: "column".to_string(),
props: Props::from_json(Value::Object(Default::default())),
children,
}
}
pub fn window_node(id: &str, children: Vec<TreeNode>) -> TreeNode {
TreeNode {
id: id.to_string(),
type_name: "window".to_string(),
props: Props::from_json(Value::Object(Default::default())),
children,
}
}
pub fn a11y_node(id: &str, type_name: &str, role: &str) -> TreeNode {
let mut props = serde_json::Map::new();
props.insert("a11y".to_string(), json!({"role": role}));
TreeNode {
id: id.to_string(),
type_name: type_name.to_string(),
props: Props::from_json(Value::Object(props)),
children: vec![],
}
}
pub fn element(node: &TreeNode) -> Element<'_> {
Element::new(node)
}