use super::DesktopWindowXamlSource;
use crate::error::Result;
use crate::window::Window;
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::WindowsAndMessaging::{SetWindowPos, ShowWindow, SWP_NOACTIVATE, SWP_NOZORDER, SW_SHOW, SW_HIDE};
#[derive(Clone)]
pub struct XamlIslandHost {
source: DesktopWindowXamlSource,
#[allow(dead_code)]
parent_hwnd: isize,
island_hwnd: isize,
}
impl XamlIslandHost {
pub fn new(window: &Window) -> Result<Self> {
let parent_hwnd = window.hwnd();
let source = DesktopWindowXamlSource::new()?;
let island_hwnd = source.attach_to_window(parent_hwnd)?;
println!("✅ XAML Island attached to window");
println!(" Parent HWND: {:?}", parent_hwnd);
println!(" Island HWND: {:?}", island_hwnd);
Ok(XamlIslandHost {
source,
parent_hwnd: parent_hwnd.0 as isize,
island_hwnd: island_hwnd.0 as isize,
})
}
pub fn source(&self) -> &DesktopWindowXamlSource {
&self.source
}
pub fn island_hwnd(&self) -> HWND {
HWND(self.island_hwnd as *mut _)
}
pub fn update_size(&self, width: i32, height: i32) -> Result<()> {
let hwnd = self.island_hwnd();
unsafe {
SetWindowPos(
hwnd,
HWND(std::ptr::null_mut()),
0,
0,
width,
height,
SWP_NOZORDER | SWP_NOACTIVATE,
)?;
}
Ok(())
}
pub fn show(&self) -> Result<()> {
let hwnd = self.island_hwnd();
unsafe {
let _ = ShowWindow(hwnd, SW_SHOW);
}
Ok(())
}
pub fn hide(&self) -> Result<()> {
let hwnd = self.island_hwnd();
unsafe {
let _ = ShowWindow(hwnd, SW_HIDE);
}
Ok(())
}
}