use super::*;
impl AppRunner {
pub fn exit(&mut self) -> bool {
if !matches!(self.status, AppRunStatus::Continue) || self.runtime.exit_requested {
return false;
}
if self
.runtime
.event_proxy
.send_event(RuntimeEvent::ExternalCommandsReady)
.is_err()
{
return false;
}
self.runtime.exit_requested = true;
true
}
pub fn relaunch(&mut self) -> std::result::Result<bool, crate::SystemIntegrationError> {
self.relaunch_with(RelaunchOptions::default())
}
pub fn relaunch_with(
&mut self,
options: RelaunchOptions,
) -> std::result::Result<bool, crate::SystemIntegrationError> {
let request = options.prepare()?;
if !matches!(self.status, AppRunStatus::Continue) || self.runtime.exit_requested {
return Ok(false);
}
self.runtime
.event_proxy
.send_event(RuntimeEvent::ExternalCommandsReady)
.map_err(|_| {
crate::SystemIntegrationError::Platform(Arc::from(
"the application event loop is closed",
))
})?;
self.runtime.relaunch_request = Some(request);
self.runtime.exit_requested = true;
Ok(true)
}
pub fn displays(&self) -> Displays {
self.runtime.displays.clone()
}
pub fn keyboard_layout(&self) -> KeyboardLayout {
self.runtime.keyboard.layout().clone()
}
pub fn window_state(&self, handle: WindowHandle) -> Option<WindowState> {
self.runtime.window_state_for(handle)
}
pub fn read_from_clipboard(&self) -> Result<Option<ClipboardItem>, crate::ClipboardError> {
self.runtime.clipboard.read(ClipboardTarget::General)
}
pub fn write_to_clipboard(&self, item: ClipboardItem) -> Result<(), crate::ClipboardError> {
self.runtime.clipboard.write(ClipboardTarget::General, item)
}
#[cfg(target_os = "linux")]
pub fn read_from_selection_clipboard(
&self,
) -> Result<Option<ClipboardItem>, crate::ClipboardError> {
self.runtime.clipboard.read(ClipboardTarget::Selection)
}
#[cfg(target_os = "linux")]
pub fn write_to_selection_clipboard(
&self,
item: ClipboardItem,
) -> Result<(), crate::ClipboardError> {
self.runtime
.clipboard
.write(ClipboardTarget::Selection, item)
}
pub fn open_external(
&mut self,
url: impl Into<Arc<str>>,
) -> Result<ShellResponse, PlatformError> {
let (request, response) = PlatformRequest::open_url_response(url)?;
self.queue_platform_response(request)?;
Ok(response)
}
pub fn open_path(&mut self, path: impl Into<PathBuf>) -> Result<ShellResponse, PlatformError> {
let (request, response) = PlatformRequest::open_path_response(path)?;
self.queue_platform_response(request)?;
Ok(response)
}
pub fn reveal_path(
&mut self,
path: impl Into<PathBuf>,
) -> Result<ShellResponse, PlatformError> {
let (request, response) = PlatformRequest::reveal_path_response(path)?;
self.queue_platform_response(request)?;
Ok(response)
}
pub fn trash_path(&mut self, path: impl Into<PathBuf>) -> Result<ShellResponse, PlatformError> {
let (request, response) = PlatformRequest::trash_path_response(path)?;
self.queue_platform_response(request)?;
Ok(response)
}
pub fn show_system_notification(
&mut self,
notification: SystemNotification,
) -> Result<(), PlatformError> {
let request = PlatformRequest::show_system_notification(notification)?;
#[cfg(target_os = "windows")]
validate_windows_notification_app_info(self.runtime.app_info.as_ref())?;
self.queue_platform_request(request)
}
pub fn dismiss_system_notification(
&mut self,
tag: impl Into<Arc<str>>,
) -> Result<(), PlatformError> {
let request = PlatformRequest::dismiss_system_notification(tag)?;
#[cfg(target_os = "windows")]
validate_windows_notification_app_info(self.runtime.app_info.as_ref())?;
self.queue_platform_request(request)
}
pub fn notification_permission_status(
&mut self,
) -> Result<NotificationPermissionResponse, PlatformError> {
#[cfg(target_os = "windows")]
validate_windows_notification_app_info(self.runtime.app_info.as_ref())?;
let (request, response) = PlatformRequest::notification_permission_status();
self.queue_platform_response(request)?;
Ok(response)
}
pub fn request_notification_permission(
&mut self,
) -> Result<NotificationPermissionResponse, PlatformError> {
#[cfg(target_os = "windows")]
validate_windows_notification_app_info(self.runtime.app_info.as_ref())?;
let (request, response) = PlatformRequest::request_notification_permission();
self.queue_platform_response(request)?;
Ok(response)
}
pub fn set_dock_badge(&mut self, value: impl Into<Arc<str>>) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().dock_badges {
return Err(PlatformError::Unsupported);
}
let value = value.into();
self.queue_platform_request(PlatformRequest::set_dock_badge(
(!value.is_empty()).then_some(value),
)?)
}
pub fn clear_dock_badge(&mut self) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().dock_badges {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::set_dock_badge(None)?)
}
pub fn dock_badge(&self) -> Option<&str> {
#[cfg(target_os = "macos")]
{
self.runtime.dock_badge.as_deref()
}
#[cfg(not(target_os = "macos"))]
{
None
}
}
pub fn set_dock_icon(&mut self, icon: Image) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().dock_icons {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::set_dock_icon(Some(icon)))
}
pub fn clear_dock_icon(&mut self) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().dock_icons {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::set_dock_icon(None))
}
pub fn set_dock_menu(&mut self, menu: Menu) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().dock_menus {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::set_dock_menu(Some(menu))?)
}
pub fn clear_dock_menu(&mut self) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().dock_menus {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::set_dock_menu(None)?)
}
pub fn dock_menu(&self) -> Option<&Menu> {
#[cfg(target_os = "macos")]
{
self.runtime.dock_menu.as_ref()
}
#[cfg(not(target_os = "macos"))]
{
None
}
}
pub fn add_recent_document(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().recent_documents {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::add_recent_document(path)?)
}
pub fn clear_recent_documents(&mut self) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().recent_documents {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::ClearRecentDocuments)
}
pub fn show_about_panel(&mut self, options: AboutPanelOptions) -> Result<(), PlatformError> {
if !DesktopIntegrationSupport::current().native_about_panel {
return Err(PlatformError::Unsupported);
}
self.queue_platform_request(PlatformRequest::show_about_panel(options)?)
}
pub fn file_icon(
&mut self,
path: impl Into<PathBuf>,
size: FileIconSize,
) -> Result<FileIconResponse, PlatformError> {
if !DesktopIntegrationSupport::current().file_icons {
return Err(PlatformError::Unsupported);
}
let (request, response) = PlatformRequest::get_file_icon(path, size)?;
self.queue_platform_response(request)?;
Ok(response)
}
pub fn set_user_tasks(
&mut self,
tasks: impl IntoIterator<Item = UserTask>,
) -> Result<ShellResponse, PlatformError> {
if !DesktopIntegrationSupport::current().user_tasks {
return Err(PlatformError::Unsupported);
}
let (request, response) = PlatformRequest::set_user_tasks(tasks.into_iter().collect())?;
self.queue_platform_response(request)?;
Ok(response)
}
pub fn clear_user_tasks(&mut self) -> Result<ShellResponse, PlatformError> {
self.set_user_tasks(std::iter::empty())
}
pub fn set_application_menus(
&mut self,
menus: impl IntoIterator<Item = Menu>,
) -> Result<(), PlatformError> {
let menus = menus.into_iter().collect::<Vec<_>>();
crate::menu::validate_menus(&menus).map_err(|_| PlatformError::InvalidMenu)?;
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
let _ = menus;
return Err(PlatformError::Unsupported);
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
{
if !matches!(self.status, AppRunStatus::Continue) {
return Err(PlatformError::Unavailable);
}
self.runtime
.event_proxy
.send_event(RuntimeEvent::ExternalCommandsReady)
.map_err(|_| PlatformError::Unavailable)?;
self.runtime.external_menus = Some(menus);
Ok(())
}
}
pub fn clear_application_menus(&mut self) -> Result<(), PlatformError> {
self.set_application_menus(std::iter::empty())
}
pub fn set_window_title(
&mut self,
handle: WindowHandle,
title: impl Into<String>,
) -> Result<(), WindowCommandError> {
let title = title.into();
validate_window_title(&title)?;
self.queue_window_command(handle, WindowCommand::SetTitle(handle, title))
}
pub fn set_window_bounds(
&mut self,
handle: WindowHandle,
bounds: WindowBounds,
) -> Result<(), WindowCommandError> {
validate_window_bounds(bounds)?;
self.queue_window_command(handle, WindowCommand::SetBounds(handle, bounds))
}
pub fn move_window(
&mut self,
handle: WindowHandle,
position: Point,
) -> Result<(), WindowCommandError> {
validate_window_position(position)?;
self.queue_window_command(handle, WindowCommand::Move(handle, position))
}
pub fn resize_window(
&mut self,
handle: WindowHandle,
size: Size,
) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.queue_window_command(handle, WindowCommand::Resize(handle, size))
}
pub fn minimize_window(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::Minimize(handle))
}
pub fn restore_window(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::Restore(handle))
}
pub fn maximize_window(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
if self
.window_state(handle)
.is_some_and(|state| state.maximized)
{
return Ok(());
}
self.queue_window_command(handle, WindowCommand::Zoom(handle))
}
pub fn set_window_fullscreen(
&mut self,
handle: WindowHandle,
fullscreen: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetFullscreen(handle, fullscreen))
}
pub fn set_window_visible(
&mut self,
handle: WindowHandle,
visible: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetVisible(handle, visible))
}
pub fn set_window_resizable(
&mut self,
handle: WindowHandle,
resizable: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetResizable(handle, resizable))
}
pub fn set_window_movable(
&mut self,
handle: WindowHandle,
movable: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetMovable(handle, movable))
}
pub fn set_window_minimum_size(
&mut self,
handle: WindowHandle,
minimum: Option<Size>,
) -> Result<(), WindowCommandError> {
if let Some(minimum) = minimum {
validate_window_size(minimum)?;
}
self.queue_window_command(handle, WindowCommand::SetMinimumSize(handle, minimum))
}
pub fn set_window_maximum_size(
&mut self,
handle: WindowHandle,
maximum: Option<Size>,
) -> Result<(), WindowCommandError> {
if let Some(maximum) = maximum {
validate_window_size(maximum)?;
}
self.queue_window_command(handle, WindowCommand::SetMaximumSize(handle, maximum))
}
pub fn set_window_minimizable(
&mut self,
handle: WindowHandle,
minimizable: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetMinimizable(handle, minimizable))
}
pub fn set_window_maximizable(
&mut self,
handle: WindowHandle,
maximizable: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetMaximizable(handle, maximizable))
}
pub fn set_window_closable(
&mut self,
handle: WindowHandle,
closable: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetClosable(handle, closable))
}
pub fn set_window_decorated(
&mut self,
handle: WindowHandle,
decorated: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetDecorated(handle, decorated))
}
pub fn set_window_shadow(
&mut self,
handle: WindowHandle,
shadow: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetShadow(handle, shadow))
}
pub fn set_window_content_protected(
&mut self,
handle: WindowHandle,
protected: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(
handle,
WindowCommand::SetContentProtected(handle, protected),
)
}
pub fn set_window_level(
&mut self,
handle: WindowHandle,
level: Option<WindowLevel>,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetWindowLevel(handle, level))
}
pub fn set_window_focusable(
&mut self,
handle: WindowHandle,
focusable: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetFocusable(handle, focusable))
}
pub fn set_window_skip_taskbar(
&mut self,
handle: WindowHandle,
skip: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetSkipTaskbar(handle, skip))
}
pub fn set_window_visible_on_all_workspaces(
&mut self,
handle: WindowHandle,
visible: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(
handle,
WindowCommand::SetVisibleOnAllWorkspaces(handle, visible),
)
}
pub fn set_window_opacity(
&mut self,
handle: WindowHandle,
opacity: f32,
) -> Result<(), WindowCommandError> {
validate_window_opacity(opacity)?;
self.queue_window_command(handle, WindowCommand::SetOpacity(handle, opacity))
}
pub fn set_window_icon(
&mut self,
handle: WindowHandle,
icon: Option<Image>,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetIcon(handle, icon))
}
pub fn set_cursor_visible(
&mut self,
handle: WindowHandle,
visible: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetCursorVisible(handle, visible))
}
pub fn set_cursor_grab(
&mut self,
handle: WindowHandle,
mode: CursorGrabMode,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetCursorGrab(handle, mode))
}
pub fn set_cursor_hit_test(
&mut self,
handle: WindowHandle,
hit_test: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetCursorHitTest(handle, hit_test))
}
pub fn set_cursor_position(
&mut self,
handle: WindowHandle,
position: Point,
) -> Result<(), WindowCommandError> {
validate_window_position(position)?;
self.queue_window_command(handle, WindowCommand::SetCursorPosition(handle, position))
}
pub fn set_taskbar_progress(
&mut self,
handle: WindowHandle,
state: TaskbarProgressState,
progress: f32,
) -> Result<(), WindowCommandError> {
validate_taskbar_progress(progress)?;
self.queue_window_command(
handle,
WindowCommand::SetTaskbarProgress(handle, state, progress),
)
}
pub fn set_taskbar_overlay_icon(
&mut self,
handle: WindowHandle,
icon: Image,
description: impl Into<String>,
) -> Result<(), WindowCommandError> {
let description = description.into();
validate_taskbar_overlay_description(Some(&description))?;
self.queue_window_command(
handle,
WindowCommand::SetTaskbarOverlayIcon(handle, Some(icon), Some(description)),
)
}
pub fn clear_taskbar_overlay_icon(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.queue_window_command(
handle,
WindowCommand::SetTaskbarOverlayIcon(handle, None, None),
)
}
pub fn focus_window(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.ensure_window_command_target(handle)?;
if self.runtime.focus_requests.len() == MAX_PENDING_WINDOW_COMMANDS {
return Err(WindowCommandError::QueueFull);
}
self.wake_for_external_command()?;
if !self.runtime.focus_requests.contains(&handle) {
self.runtime.focus_requests.push(handle);
}
Ok(())
}
pub fn request_window_attention(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::RequestAttention(handle))
}
pub fn set_window_represented_file(
&mut self,
handle: WindowHandle,
path: Option<PathBuf>,
) -> Result<(), WindowCommandError> {
if let Some(path) = &path {
validate_window_document_path(path)?;
}
self.queue_window_command(handle, WindowCommand::SetRepresentedFile(handle, path))
}
pub fn set_window_document_edited(
&mut self,
handle: WindowHandle,
edited: bool,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetDocumentEdited(handle, edited))
}
pub fn set_window_appearance(
&mut self,
handle: WindowHandle,
appearance: Option<WindowAppearance>,
) -> Result<(), WindowCommandError> {
self.queue_window_command(handle, WindowCommand::SetAppearance(handle, appearance))
}
pub fn set_window_background_appearance(
&mut self,
handle: WindowHandle,
appearance: WindowBackgroundAppearance,
) -> Result<(), WindowCommandError> {
self.queue_window_command(
handle,
WindowCommand::SetBackgroundAppearance(handle, appearance),
)
}
fn queue_window_command(
&mut self,
handle: WindowHandle,
command: WindowCommand,
) -> Result<(), WindowCommandError> {
self.ensure_window_command_target(handle)?;
if self.runtime.window_commands.len() == MAX_PENDING_WINDOW_COMMANDS {
return Err(WindowCommandError::QueueFull);
}
self.wake_for_external_command()?;
self.runtime.window_commands.push(command);
Ok(())
}
fn queue_platform_response(&mut self, request: PlatformRequest) -> Result<(), PlatformError> {
self.queue_platform_request(request)
}
fn queue_platform_request(&mut self, request: PlatformRequest) -> Result<(), PlatformError> {
if !matches!(self.status, AppRunStatus::Continue) {
return Err(PlatformError::Unavailable);
}
if self.runtime.platform_requests.len() == crate::MAX_PENDING_PLATFORM_REQUESTS {
return Err(PlatformError::PendingQueueFull);
}
self.runtime
.event_proxy
.send_event(RuntimeEvent::ExternalCommandsReady)
.map_err(|_| PlatformError::Unavailable)?;
self.runtime.platform_requests.push_back(request);
Ok(())
}
fn ensure_window_command_target(&self, handle: WindowHandle) -> Result<(), WindowCommandError> {
if !matches!(self.status, AppRunStatus::Continue)
|| !(self.runtime.window_handles.contains_key(&handle)
|| self.runtime.current_handle() == Some(handle))
{
Err(WindowCommandError::Unavailable)
} else {
Ok(())
}
}
fn wake_for_external_command(&self) -> Result<(), WindowCommandError> {
self.runtime
.event_proxy
.send_event(RuntimeEvent::ExternalCommandsReady)
.map_err(|_| WindowCommandError::Unavailable)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn external_window_commands_keep_public_validation() {
assert_eq!(
validate_window_title(&"x".repeat(MAX_WINDOW_TITLE_BYTES + 1)),
Err(WindowCommandError::TitleTooLong)
);
assert_eq!(
validate_window_document_path(Path::new("")),
Err(WindowCommandError::InvalidDocumentPath)
);
}
}