use super::Context;
use tauri::{
CursorIcon, Icon, Monitor, PhysicalPosition, PhysicalSize, Position, Result, Size, Theme,
UserAttentionType,
};
use tauri_runtime::{Error, UserEvent};
use tauri_runtime_wry::{Message, MonitorHandleWrapper, WebviewId, WindowMessage, WryIcon};
use std::sync::mpsc::channel;
pub struct Window<T: UserEvent> {
pub(crate) id: WebviewId,
pub(crate) context: Context<T>,
}
macro_rules! window_getter {
($self: ident, $message: expr) => {{
let (tx, rx) = channel();
$self.context.inner.run_threaded(|main_thread| {
let message = Message::Window($self.id, $message(tx));
if main_thread.is_some() {
super::handle_user_message(&message, &$self.context.main_thread.windows);
Ok(())
} else {
$self
.context
.inner
.proxy
.send_event(message)
.map_err(|_| Error::EventLoopClosed)
.map_err(tauri::Error::from)
}
})?;
rx.recv()
.map_err(|_| Error::FailedToReceiveMessage)
.map_err(tauri::Error::from)
}};
}
impl<T: UserEvent> Window<T> {
pub fn scale_factor(&self) -> Result<f64> {
window_getter!(self, WindowMessage::ScaleFactor)
}
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>> {
window_getter!(self, WindowMessage::InnerPosition)?.map_err(Into::into)
}
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>> {
window_getter!(self, WindowMessage::OuterPosition)?.map_err(Into::into)
}
pub fn inner_size(&self) -> Result<PhysicalSize<u32>> {
window_getter!(self, WindowMessage::InnerSize)
}
pub fn outer_size(&self) -> Result<PhysicalSize<u32>> {
window_getter!(self, WindowMessage::OuterSize)
}
pub fn is_fullscreen(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsFullscreen)
}
pub fn is_maximized(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsMaximized)
}
pub fn is_decorated(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsDecorated)
}
pub fn is_resizable(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsResizable)
}
pub fn is_visible(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsVisible)
}
pub fn current_monitor(&self) -> Result<Option<Monitor>> {
Ok(
window_getter!(self, WindowMessage::CurrentMonitor)?
.map(|m| MonitorHandleWrapper(m).into())
.map(|m: tauri_runtime::monitor::Monitor| m.into()),
)
}
pub fn primary_monitor(&self) -> Result<Option<Monitor>> {
Ok(
window_getter!(self, WindowMessage::PrimaryMonitor)?
.map(|m| MonitorHandleWrapper(m).into())
.map(|m: tauri_runtime::monitor::Monitor| m.into()),
)
}
pub fn available_monitors(&self) -> Result<Vec<Monitor>> {
Ok(
window_getter!(self, WindowMessage::AvailableMonitors)?
.into_iter()
.map(|m| MonitorHandleWrapper(m).into())
.map(|m: tauri_runtime::monitor::Monitor| m.into())
.collect(),
)
}
pub fn theme(&self) -> Result<Theme> {
window_getter!(self, WindowMessage::Theme)
}
}
impl<T: UserEvent> Window<T> {
fn send_event(&self, message: WindowMessage) -> Result<()> {
self.context.inner.run_threaded(|main_thread| {
let message = Message::Window(self.id, message);
if main_thread.is_some() {
super::handle_user_message(&message, &self.context.main_thread.windows);
Ok(())
} else {
self
.context
.inner
.proxy
.send_event(message)
.map_err(|_| Error::EventLoopClosed.into())
}
})
}
pub fn center(&self) -> Result<()> {
self.send_event(WindowMessage::Center)
}
pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()> {
self.send_event(WindowMessage::RequestUserAttention(
request_type.map(Into::into),
))
}
pub fn set_resizable(&self, resizable: bool) -> Result<()> {
self.send_event(WindowMessage::SetResizable(resizable))
}
pub fn set_title(&self, title: &str) -> Result<()> {
self.send_event(WindowMessage::SetTitle(title.into()))
}
pub fn maximize(&self) -> Result<()> {
self.send_event(WindowMessage::Maximize)
}
pub fn unmaximize(&self) -> Result<()> {
self.send_event(WindowMessage::Unmaximize)
}
pub fn minimize(&self) -> Result<()> {
self.send_event(WindowMessage::Minimize)
}
pub fn unminimize(&self) -> Result<()> {
self.send_event(WindowMessage::Unminimize)
}
pub fn show(&self) -> Result<()> {
self.send_event(WindowMessage::Show)
}
pub fn hide(&self) -> Result<()> {
self.send_event(WindowMessage::Hide)
}
pub fn close(&self) -> Result<()> {
self.send_event(WindowMessage::Close)
}
pub fn set_decorations(&self, decorations: bool) -> Result<()> {
self.send_event(WindowMessage::SetDecorations(decorations))
}
pub fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
self.send_event(WindowMessage::SetAlwaysOnTop(always_on_top))
}
pub fn set_size<S: Into<Size>>(&self, size: S) -> Result<()> {
self.send_event(WindowMessage::SetSize(size.into()))
}
pub fn set_min_size<S: Into<Size>>(&self, size: Option<S>) -> Result<()> {
self.send_event(WindowMessage::SetMinSize(size.map(|s| s.into())))
}
pub fn set_max_size<S: Into<Size>>(&self, size: Option<S>) -> Result<()> {
self.send_event(WindowMessage::SetMaxSize(size.map(|s| s.into())))
}
pub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()> {
self.send_event(WindowMessage::SetPosition(position.into()))
}
pub fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
self.send_event(WindowMessage::SetFullscreen(fullscreen))
}
pub fn set_focus(&self) -> Result<()> {
self.send_event(WindowMessage::SetFocus)
}
pub fn set_icon(&self, icon: Icon) -> Result<()> {
let icon: tauri_runtime::Icon = icon.try_into()?;
self.send_event(WindowMessage::SetIcon(
WryIcon::try_from(icon)
.map_err(tauri_runtime::Error::from)?
.0,
))
}
pub fn set_skip_taskbar(&self, skip: bool) -> Result<()> {
self.send_event(WindowMessage::SetSkipTaskbar(skip))
}
pub fn set_cursor_grab(&self, grab: bool) -> Result<()> {
self.send_event(WindowMessage::SetCursorGrab(grab))
}
pub fn set_cursor_visible(&self, visible: bool) -> Result<()> {
self.send_event(WindowMessage::SetCursorVisible(visible))
}
pub fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()> {
self.send_event(WindowMessage::SetCursorIcon(icon))
}
pub fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()> {
self.send_event(WindowMessage::SetCursorPosition(position.into()))
}
pub fn start_dragging(&self) -> Result<()> {
self.send_event(WindowMessage::DragWindow)
}
}