use crate::winrt::{IInspectable, IActivationFactory, PropertyAccess, WinRTEvent, WinRTClickEventArgs};
use crate::error::Result;
use windows::core::IInspectable as CoreIInspectable;
#[derive(Clone)]
pub struct XamlButton {
inspectable: IInspectable,
click_event: WinRTEvent<WinRTClickEventArgs>,
}
impl XamlButton {
pub fn new() -> Result<Self> {
let factory = IActivationFactory::get("Windows.UI.Xaml.Controls.Button")
.map_err(|e| crate::error::Error::control_creation(format!("Failed to get Button factory: {}", e)))?;
let inspectable: CoreIInspectable = factory.activate_instance()
.map_err(|e| crate::error::Error::control_creation(format!("Failed to activate Button: {}", e)))?;
Ok(XamlButton {
inspectable: IInspectable::from(inspectable),
click_event: WinRTEvent::new(),
})
}
pub fn as_inspectable(&self) -> &IInspectable {
&self.inspectable
}
pub fn set_content(&self, content: &str) -> Result<()> {
self.set_string_property("Content", content)
}
pub fn get_content(&self) -> Result<String> {
self.get_string_property("Content")
}
pub fn set_width(&self, width: f64) -> Result<()> {
self.set_numeric_property("Width", width)
}
pub fn set_height(&self, height: f64) -> Result<()> {
self.set_numeric_property("Height", height)
}
pub fn click(&self) -> &WinRTEvent<WinRTClickEventArgs> {
&self.click_event
}
pub fn trigger_click(&self) -> Result<()> {
let args = WinRTClickEventArgs::new("XamlButton");
self.click_event.invoke(&args)
}
}
impl PropertyAccess for XamlButton {
fn inspectable(&self) -> &IInspectable {
&self.inspectable
}
}
impl Default for XamlButton {
fn default() -> Self {
Self::new().expect("Failed to create XAML Button")
}
}
unsafe impl Send for XamlButton {}
unsafe impl Sync for XamlButton {}