winrt-xaml 1.0.0

A Rust library for creating modern Windows UIs using WinRT and XAML with reactive data binding
Documentation
//! Simple window example - minimal demonstration.
//!
//! This example shows a basic window with minimal functionality using pure WinRT.

use winrt_xaml::error::Result;
use winrt_xaml::xaml_native::*;
use windows::core::w;
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
use windows::Win32::UI::WindowsAndMessaging::*;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::System::Com::{CoInitializeEx, CoUninitialize, COINIT_APARTMENTTHREADED};
use std::ptr;

fn create_host_window() -> Result<HWND> {
    unsafe {
        let class_name = w!("WinRT_SimpleWindow");
        let wc = WNDCLASSW {
            style: CS_HREDRAW | CS_VREDRAW,
            lpfnWndProc: Some(window_proc),
            hInstance: GetModuleHandleW(None)?.into(),
            lpszClassName: class_name,
            hCursor: LoadCursorW(None, IDC_ARROW)?,
            ..Default::default()
        };
        let _ = RegisterClassW(&wc);

        CreateWindowExW(
            WINDOW_EX_STYLE(0),
            class_name,
            w!("Simple WinRT-XAML Window"),
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
            None, None,
            GetModuleHandleW(None)?,
            Some(ptr::null()),
        ).map_err(|e| winrt_xaml::error::Error::window_creation(format!("{:?}", e)))
    }
}

unsafe extern "system" fn window_proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
    match msg {
        WM_SIZE => {
            if let Ok(child) = GetWindow(hwnd, GW_CHILD) {
                if !child.0.is_null() {
                    let width = (lparam.0 & 0xFFFF) as i32;
                    let height = ((lparam.0 >> 16) & 0xFFFF) as i32;
                    let _ = SetWindowPos(child, None, 0, 0, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
                }
            }
            LRESULT(0)
        }
        WM_DESTROY => {
            PostQuitMessage(0);
            LRESULT(0)
        }
        _ => DefWindowProcW(hwnd, msg, wparam, lparam),
    }
}

fn main() -> Result<()> {
    println!("╔═══════════════════════════════════════════════╗");
    println!("║        Simple WinRT-XAML Window             ║");
    println!("╚═══════════════════════════════════════════════╝\n");

    println!("Creating simple window...");

    // Initialize COM for WinRT
    unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED).ok(); }

    println!("✅ COM initialized");

    // Initialize XAML framework
    let _manager = XamlManager::new()?;
    println!("✅ XAML framework initialized");

    // Create host window
    let host_hwnd = create_host_window()?;
    println!("✅ Host window created");

    // Create XAML source and attach to window
    let mut xaml_source = XamlSource::new()?;
    let island_hwnd = xaml_source.attach_to_window(host_hwnd)?;
    println!("✅ XAML Island attached");

    // Create simple content
    // Create a styled panel container
    let panel = XamlStackPanel::new()?;
    panel.set_vertical(true)?;
    panel.set_spacing(15.0)?;
    panel.set_background(0xFF1A1A1A)?; // Dark theme
    panel.set_padding(40.0, 40.0, 40.0, 40.0)?;
    panel.set_corner_radius(14.0)?;

    // Add styled greeting text
    let textblock = XamlTextBlock::new()?;
    textblock.set_text("👋 Hello, WinRT-XAML!")?;
    textblock.set_font_size(36.0)?;
    textblock.set_font_weight(700)?; // Bold
    textblock.set_foreground(0xFF00FF9F)?; // Bright green
    panel.add_child(&textblock.as_uielement())?;

    // Add subtitle
    let subtitle = XamlTextBlock::new()?;
    subtitle.set_text("A simple, styled window example")?;
    subtitle.set_font_size(16.0)?;
    subtitle.set_foreground(0xFFAAAAAA)?; // Gray
    panel.add_child(&subtitle.as_uielement())?;

    // Set content
    xaml_source.set_content_element(&panel.as_uielement())?;

    // Show and size the island
    unsafe {
        use windows::Win32::UI::WindowsAndMessaging::*;
        use windows::Win32::Foundation::RECT;

        let _ = ShowWindow(island_hwnd, SW_SHOW);
        let mut rect = RECT::default();
        let _ = GetClientRect(host_hwnd, &mut rect);
        let _ = SetWindowPos(island_hwnd, None, 0, 0,
            rect.right - rect.left, rect.bottom - rect.top,
            SWP_NOZORDER | SWP_NOACTIVATE);
    }

    println!("✅ Window shown - starting message loop\n");

    // Run message loop
    unsafe {
        let mut msg = MSG::default();
        while GetMessageW(&mut msg, None, 0, 0).as_bool() {
            let _ = TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
        CoUninitialize();
    }

    Ok(())
}