use computeruse::{Desktop, SerializableUIElement};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("UIElement Serialization & Deserialization Example");
println!("==================================================");
let desktop = Desktop::new(false, false)?;
let root = desktop.root();
println!("=== Direct UIElement Serialization ===");
let json = serde_json::to_string_pretty(&root)?;
println!("Serialized UIElement:");
println!("{json}");
println!("\n=== SerializableUIElement Round-trip ===");
let serializable = root.to_serializable();
let serializable_json = serializable.to_json()?;
println!("Serialized SerializableUIElement:");
println!("{serializable_json}");
let deserialized = SerializableUIElement::from_json(&serializable_json)?;
println!("\nDeserialized SerializableUIElement:");
println!("Role: {}", deserialized.role);
println!("Name: {:?}", deserialized.name);
println!("Display Name: {}", deserialized.display_name());
println!("Bounds: {:?}", deserialized.bounds);
println!("\n=== Working with Collections ===");
if let Ok(children) = root.children() {
if !children.is_empty() {
let serializable_children: Vec<SerializableUIElement> = children
.iter()
.map(|child| child.to_serializable())
.collect();
let children_json = serde_json::to_string_pretty(&serializable_children)?;
println!("Serialized children collection:");
println!("{children_json}");
let deserialized_children: Vec<SerializableUIElement> =
serde_json::from_str(&children_json)?;
println!("\nDeserialized {} children:", deserialized_children.len());
for (i, child) in deserialized_children.iter().take(3).enumerate() {
println!(" {}: {} ({})", i + 1, child.display_name(), child.role);
}
}
}
println!("\n=== Creating SerializableUIElement from scratch ===");
let mut custom_element = SerializableUIElement::new("Button".to_string());
custom_element.name = Some("Click Me".to_string());
custom_element.bounds = Some((100.0, 200.0, 80.0, 30.0));
custom_element.window_and_application_name = Some("My App".to_string());
let custom_json = custom_element.to_json()?;
println!("Custom SerializableUIElement:");
println!("{custom_json}");
let custom_deserialized = SerializableUIElement::from_json(&custom_json)?;
println!(
"Round-trip successful: {}",
custom_deserialized.name == custom_element.name
);
Ok(())
}