use super::*;
#[derive(Clone)]
pub struct WindowHandle {
pub(super) info: WindowInfo,
pub(super) window: Window,
}
impl WindowHandle {
#[cfg(feature = "mcp")]
pub(crate) fn dialog_window(&self) -> DialogWindow {
DialogWindow(Arc::downgrade(&self.window.inner))
}
pub fn info(&self) -> WindowInfo {
self.info
}
pub fn on_close_requested(&self, handler: impl Fn(CloseRequest) + Send + Sync + 'static) {
self.window.inner.write().unwrap().close_handler = Some(Arc::new(handler));
}
pub fn clear_close_requested_handler(&self) {
self.window.inner.write().unwrap().close_handler = None;
}
pub fn request_close(&self) {
self
.window
.push_command(WindowCommand::RequestClose(CloseRequestSource::App));
}
pub fn close(&self) {
self.window.push_command(WindowCommand::Close);
}
pub fn set_minimized(&self, minimized: bool) {
self.window.push_command(WindowCommand::SetMinimized(minimized));
}
pub fn set_maximized(&self, maximized: bool) {
self.window.push_command(WindowCommand::SetMaximized(maximized));
}
pub fn set_full_screen(&self, full_screen: bool) {
self.window.push_command(WindowCommand::SetFullScreen(full_screen));
}
pub fn set_decorated(&self, decorated: bool) {
self.window.push_command(WindowCommand::SetDecorated(decorated));
}
pub fn set_decorations(&self, decorations: bool) {
self.set_decorated(decorations);
}
pub fn title(&self) -> Option<String> {
self.window.inner.read().unwrap().title.clone()
}
pub fn set_title(&self, title: impl Into<String>) {
let title = title.into();
{
let mut inner = self.window.inner.write().unwrap();
if inner.title.as_deref() == Some(title.as_str()) {
return;
}
inner.title = Some(title.clone());
}
self.window.push_command(WindowCommand::SetTitle(title));
}
pub fn set_title_bar_color(&self, color: impl Into<Option<Color>>) {
self.window.push_command(WindowCommand::SetTitleBarColor(color.into()));
}
pub fn clear_title_bar_color(&self) {
self.set_title_bar_color(None);
}
pub fn border_color(&self) -> WindowBorderColor {
self.window.inner.read().unwrap().border_color
}
pub fn set_border_color(&self, color: WindowBorderColor) {
self.window.push_command(WindowCommand::SetBorderColor(color));
}
pub fn set_icon(&self, icon: impl Into<Option<WindowIcon>>) {
self.window.push_command(WindowCommand::SetIcon(icon.into()));
}
pub fn clear_icon(&self) {
self.set_icon(None);
}
pub fn set_corner_radius(&self, radius: WindowCornerRadius) {
self.window.push_command(WindowCommand::SetCornerRadius(radius));
}
pub fn set_rounded_corners(&self, rounded: bool) {
self.set_corner_radius(if rounded {
WindowCornerRadius::Rounded
} else {
WindowCornerRadius::None
});
}
pub fn reset_corner_radius(&self) {
self.set_corner_radius(WindowCornerRadius::Default);
}
pub fn r#move(&self, x: i32, y: i32) {
self.window.push_command(WindowCommand::Move { x, y });
}
pub fn move_to(&self, x: i32, y: i32) {
self.r#move(x, y);
}
pub fn resize(&self, width: u32, height: u32) {
self
.window
.push_command(WindowCommand::Resize(super::resize::ResizeRequest {
width,
height,
restore: super::resize::ResizeRestore::Minimized,
report: None,
}));
}
pub fn start_drag(&self) {
self.window.push_command(WindowCommand::StartDrag);
}
pub fn start_resize(&self, direction: WindowResizeDirection) {
self.window.push_command(WindowCommand::StartResize(direction));
}
pub fn stop_drag(&self) {
self.window.push_command(WindowCommand::StopDrag);
}
pub fn inject_input(&self, input: crate::app::synthetic_input::SyntheticInput) {
self.window.push_command(WindowCommand::InjectInput(input));
}
pub fn inject_inputs(&self, inputs: impl IntoIterator<Item = crate::app::synthetic_input::SyntheticInput>) {
for input in inputs {
self.inject_input(input);
}
}
#[cfg(feature = "screenshot")]
pub fn screenshot(&self, output_path: impl Into<std::path::PathBuf>) {
self
.window
.push_command(WindowCommand::Screenshot(output_path.into(), None));
}
#[cfg(feature = "screenshot")]
pub fn screenshot_region(&self, output_path: impl Into<std::path::PathBuf>, region: ScreenshotRegion) {
self
.window
.push_command(WindowCommand::Screenshot(output_path.into(), Some(region)));
}
#[cfg(feature = "screenshot")]
pub fn screenshot_node(&self, output_path: impl Into<std::path::PathBuf>, node: &crate::core::ElementRef) {
let bounds = node.bounds();
self.screenshot_region(
output_path,
ScreenshotRegion {
x: bounds.x,
y: bounds.y,
width: bounds.width,
height: bounds.height,
},
);
}
pub fn open_devtools(&self) {
self.window.push_command(WindowCommand::OpenDevtools);
}
pub fn close_devtools(&self) {
self.window.push_command(WindowCommand::CloseDevtools);
}
pub fn toggle_devtools(&self) {
self.window.push_command(WindowCommand::ToggleDevtools);
}
}
#[cfg(feature = "mcp")]
#[derive(Clone)]
pub(crate) struct DialogWindow(Weak<RwLock<WindowInner>>);
#[cfg(feature = "mcp")]
impl DialogWindow {
pub(crate) fn matches(&self, window: &Window) -> bool {
self.0.ptr_eq(&Arc::downgrade(&window.inner))
}
pub(crate) fn closing(&self) -> bool {
self
.0
.upgrade()
.is_none_or(|window| window.read().unwrap().accepted_close)
}
}
impl Deref for WindowHandle {
type Target = WindowInfo;
fn deref(&self) -> &Self::Target {
&self.info
}
}