winrt_xaml/winrt/xaml/
application.rs

1//! Windows.UI.Xaml.Application bindings.
2
3use crate::winrt::{IInspectable, IActivationFactory};
4use crate::error::Result;
5use windows::core::IInspectable as CoreIInspectable;
6
7/// Windows.UI.Xaml.Application - The base class for UWP applications.
8#[derive(Clone)]
9pub struct XamlApplication {
10    inspectable: IInspectable,
11}
12
13impl XamlApplication {
14    /// Create a new Application instance using WinRT activation.
15    pub fn new() -> Result<Self> {
16        // Try to activate the Windows.UI.Xaml.Application runtime class
17        let factory = IActivationFactory::get("Windows.UI.Xaml.Application")
18            .map_err(|e| crate::error::Error::initialization(format!("Failed to get XAML Application factory: {}", e)))?;
19
20        let inspectable: CoreIInspectable = factory.activate_instance()
21            .map_err(|e| crate::error::Error::initialization(format!("Failed to activate XAML Application: {}", e)))?;
22
23        Ok(XamlApplication {
24            inspectable: IInspectable::from(inspectable),
25        })
26    }
27
28    /// Get the current Application instance (singleton pattern in XAML).
29    pub fn current() -> Result<Self> {
30        // For now, create a new instance
31        // In a real implementation, this would retrieve the current app instance
32        Self::new()
33    }
34
35    /// Run the application message loop.
36    pub fn run(&self) -> Result<()> {
37        // The WinRT Application.Run() method would be called here
38        // For now, fall back to Win32 message loop
39        let app = crate::app::Application::current()
40            .ok_or_else(|| crate::error::Error::application("No application instance available"))?;
41        app.run()
42    }
43
44    /// Exit the application with a code.
45    pub fn exit(&self, code: i32) -> Result<()> {
46        let app = crate::app::Application::current()
47            .ok_or_else(|| crate::error::Error::application("No application instance available"))?;
48        app.exit_with_code(code);
49        Ok(())
50    }
51
52    /// Get the underlying IInspectable.
53    pub fn as_inspectable(&self) -> &IInspectable {
54        &self.inspectable
55    }
56}
57
58impl Default for XamlApplication {
59    fn default() -> Self {
60        Self::new().expect("Failed to create XAML Application")
61    }
62}
63
64unsafe impl Send for XamlApplication {}
65unsafe impl Sync for XamlApplication {}
66