use super::*;
use crate::{MacOsVibrancy, MacOsVisualEffectState};
#[derive(Debug, Default)]
pub struct EventContext {
pub(crate) globals: GlobalStore,
pub(crate) app_info: Option<AppInfo>,
pub(crate) app_paths: Option<AppPaths>,
pub(crate) system_info: SystemInfo,
pub(crate) system_preferences: SystemPreferences,
pub(crate) window_registry: WindowRegistry,
pub(crate) foreground_tasks: Option<ForegroundTaskSpawner>,
pub(crate) clipboard: Option<ClipboardService>,
pub(crate) displays: Displays,
pub(crate) keyboard_layout: KeyboardLayout,
pub(crate) assets: Assets,
pub(crate) window: Option<WindowHandle>,
pub(crate) parent_window: Option<WindowHandle>,
pub(crate) popover_owner_window: Option<WindowHandle>,
pub(crate) popover_root_window: Option<WindowHandle>,
pub(crate) pointer_position: Option<Point>,
pub(crate) invalidate: bool,
pub(crate) exit: bool,
pub(crate) relaunch: Option<RelaunchRequest>,
pub(crate) focus: Option<Option<ElementId>>,
pub(crate) clear_text_selection: bool,
pub(crate) actions: Vec<AnyAction>,
pub(crate) targeted_actions: Vec<(WindowHandle, AnyAction)>,
pub(crate) menus: Option<Vec<Menu>>,
pub(crate) window_menus: Option<Option<Vec<Menu>>>,
pub(crate) native_popup_menus: Vec<NativePopupMenuRequest>,
pub(crate) propagate_action: bool,
pub(crate) stop_event_propagation: bool,
pub(crate) prevent_default: bool,
pub(crate) open_windows: Vec<WindowRequest>,
pub(crate) close_current_window: bool,
pub(crate) close_windows: Vec<WindowHandle>,
pub(crate) focus_windows: Vec<WindowHandle>,
pub(crate) invalidate_windows: Vec<WindowHandle>,
pub(crate) window_commands: Vec<WindowCommand>,
pub(crate) exit_code: Option<i32>,
pub(crate) constrained_size: Option<Size>,
pub(crate) constrained_position: Option<Point>,
pub(crate) platform_requests: Vec<PlatformRequest>,
pub(crate) prevent_close: bool,
pub(crate) prevent_quit: bool,
pub(crate) form_submissions: Vec<ElementId>,
pub(crate) entity_notifications: Vec<EntityId>,
pub(crate) notify_all_entities: bool,
pub(crate) entity_events: Vec<EntityEvent>,
pub(crate) global_notifications: Vec<TypeId>,
pub(crate) notify_all_globals: bool,
}
#[derive(Debug)]
pub(crate) struct NativePopupMenuRequest {
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub(crate) menu: Menu,
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub(crate) position: Option<Point>,
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct EventWindowContext {
pub(crate) window: Option<WindowHandle>,
pub(crate) parent: Option<WindowHandle>,
pub(crate) popover_owner: Option<WindowHandle>,
pub(crate) popover_root: Option<WindowHandle>,
pub(crate) pointer_position: Option<Point>,
}
pub(crate) struct EventRuntimeContext {
pub(crate) globals: GlobalStore,
pub(crate) foreground_tasks: ForegroundTaskSpawner,
pub(crate) clipboard: ClipboardService,
pub(crate) displays: Displays,
pub(crate) keyboard_layout: KeyboardLayout,
pub(crate) assets: Assets,
pub(crate) app_info: Option<AppInfo>,
pub(crate) app_paths: Option<AppPaths>,
pub(crate) system_info: SystemInfo,
pub(crate) system_preferences: SystemPreferences,
pub(crate) window_registry: WindowRegistry,
pub(crate) window: EventWindowContext,
}
impl EventContext {
pub(crate) fn with_runtime(runtime: EventRuntimeContext) -> Self {
Self {
globals: runtime.globals,
app_info: runtime.app_info,
app_paths: runtime.app_paths,
system_info: runtime.system_info,
system_preferences: runtime.system_preferences,
window_registry: runtime.window_registry,
foreground_tasks: Some(runtime.foreground_tasks),
clipboard: Some(runtime.clipboard),
displays: runtime.displays,
keyboard_layout: runtime.keyboard_layout,
assets: runtime.assets,
window: runtime.window.window,
parent_window: runtime.window.parent,
popover_owner_window: runtime.window.popover_owner,
popover_root_window: runtime.window.popover_root,
pointer_position: runtime.window.pointer_position,
invalidate: false,
exit: false,
relaunch: None,
focus: None,
clear_text_selection: false,
actions: Vec::new(),
targeted_actions: Vec::new(),
menus: None,
window_menus: None,
native_popup_menus: Vec::new(),
propagate_action: false,
stop_event_propagation: false,
prevent_default: false,
open_windows: Vec::new(),
close_current_window: false,
close_windows: Vec::new(),
focus_windows: Vec::new(),
invalidate_windows: Vec::new(),
window_commands: Vec::new(),
exit_code: None,
constrained_size: None,
constrained_position: None,
platform_requests: Vec::new(),
prevent_close: false,
prevent_quit: false,
form_submissions: Vec::new(),
entity_notifications: Vec::new(),
notify_all_entities: false,
entity_events: Vec::new(),
global_notifications: Vec::new(),
notify_all_globals: false,
}
}
pub fn displays(&self) -> &[Display] {
self.displays.all()
}
pub const fn display_snapshot(&self) -> &Displays {
&self.displays
}
pub fn primary_display(&self) -> Option<&Display> {
self.displays.primary()
}
pub fn find_display(&self, id: DisplayId) -> Option<&Display> {
self.displays.find(id)
}
pub fn keyboard_layout(&self) -> &KeyboardLayout {
&self.keyboard_layout
}
pub fn assets(&self) -> &Assets {
&self.assets
}
pub fn asset_source(&self) -> &Assets {
self.assets()
}
pub fn app_info(&self) -> Option<&AppInfo> {
self.app_info.as_ref()
}
pub fn app_paths(&self) -> Option<&AppPaths> {
self.app_paths.as_ref()
}
pub fn system_info(&self) -> &SystemInfo {
&self.system_info
}
pub const fn system_preferences(&self) -> SystemPreferences {
self.system_preferences
}
pub fn window_registry(&self) -> &WindowRegistry {
&self.window_registry
}
pub fn windows(&self) -> &[WindowHandle] {
self.window_registry.windows()
}
pub const fn active_window(&self) -> Option<WindowHandle> {
self.window_registry.active_window()
}
pub fn cursor_screen_position(&self) -> Result<Point, PlatformError> {
crate::runtime::cursor_screen_position(&self.displays)
}
pub fn read_from_clipboard(&self) -> Result<Option<ClipboardItem>, ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.read(ClipboardTarget::General)
}
pub fn write_to_clipboard(&self, item: ClipboardItem) -> Result<(), ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.write(ClipboardTarget::General, item)
}
#[cfg(target_os = "linux")]
pub fn read_from_selection_clipboard(&self) -> Result<Option<ClipboardItem>, ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.read(ClipboardTarget::Selection)
}
#[cfg(target_os = "linux")]
pub fn write_to_selection_clipboard(&self, item: ClipboardItem) -> Result<(), ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.write(ClipboardTarget::Selection, item)
}
#[cfg(target_os = "macos")]
pub fn read_from_find_pasteboard(&self) -> Result<Option<ClipboardItem>, ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.read(ClipboardTarget::Find)
}
#[cfg(target_os = "macos")]
pub fn write_to_find_pasteboard(&self, item: ClipboardItem) -> Result<(), ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.write(ClipboardTarget::Find, item)
}
pub fn spawn<V, Build, Fut, R>(&self, build: Build) -> Result<Task<R>, ForegroundTaskSpawnError>
where
V: 'static,
Build: FnOnce(AsyncViewContext<V>) -> Fut,
Fut: Future<Output = R> + 'static,
R: 'static,
{
let foreground_tasks = self
.foreground_tasks
.as_ref()
.ok_or(ForegroundTaskSpawnError::Unavailable)?;
let window = self.window.ok_or(ForegroundTaskSpawnError::Unavailable)?;
foreground_tasks.spawn::<V, _, _, _>(window, build)
}
pub fn invalidate(&mut self) {
self.invalidate = true;
}
pub fn notify<T>(&mut self, entity: &Entity<T>) {
if self.notify_all_entities || self.entity_notifications.contains(&entity.id()) {
return;
}
if self.entity_notifications.len() == MAX_ENTITY_NOTIFICATIONS_PER_EVENT {
self.entity_notifications.clear();
self.notify_all_entities = true;
return;
}
self.entity_notifications.push(entity.id());
}
pub fn emit<T, E>(&mut self, entity: &Entity<T>, event: E) -> bool
where
T: EventEmitter<E>,
E: Any,
{
if self.entity_events.len() == MAX_ENTITY_EVENTS_PER_CALLBACK {
return false;
}
self.entity_events.push(EntityEvent::new(entity, event));
true
}
pub fn has_global<G: Global>(&self) -> bool {
self.globals.has::<G>()
}
pub fn global<G: Global>(&self) -> Ref<'_, G> {
self.globals.get::<G>()
}
pub fn try_global<G: Global>(&self) -> Option<Ref<'_, G>> {
self.globals.try_get::<G>()
}
pub fn global_mut<G: Global>(&mut self) -> RefMut<'_, G> {
self.note_global_changed(TypeId::of::<G>());
self.globals.get_mut::<G>()
}
pub fn default_global<G: Global + Default>(&mut self) -> RefMut<'_, G> {
self.note_global_changed(TypeId::of::<G>());
self.globals.default_mut::<G>()
}
pub fn set_global<G: Global>(&mut self, global: G) {
self.globals.set(global);
self.note_global_changed(TypeId::of::<G>());
}
pub fn update_global<G: Global, R>(&mut self, update: impl FnOnce(&mut G) -> R) -> R {
self.note_global_changed(TypeId::of::<G>());
let mut global = self.globals.get_mut::<G>();
update(&mut global)
}
pub fn remove_global<G: Global>(&mut self) -> G {
let global = self.globals.remove::<G>();
self.note_global_changed(TypeId::of::<G>());
global
}
pub(super) fn note_global_changed(&mut self, global_type: TypeId) {
if self.notify_all_globals || self.global_notifications.contains(&global_type) {
return;
}
if self.global_notifications.len() == MAX_GLOBAL_NOTIFICATIONS_PER_EVENT {
self.global_notifications.clear();
self.notify_all_globals = true;
return;
}
self.global_notifications.push(global_type);
}
pub fn exit(&mut self) {
self.exit = true;
}
pub fn relaunch(&mut self) -> Result<(), SystemIntegrationError> {
self.relaunch_with(RelaunchOptions::default())
}
pub fn relaunch_with(
&mut self,
options: RelaunchOptions,
) -> Result<(), SystemIntegrationError> {
self.relaunch = Some(options.prepare()?);
self.exit = true;
Ok(())
}
pub fn open_window<V: View>(&mut self, options: WindowOptions, view: V) -> WindowHandle {
let request = WindowRequest::with_parent(view, options, self.window);
let handle = request.handle;
self.open_windows.push(request);
handle
}
pub fn open_system_popover<V: View>(
&mut self,
anchor: impl Into<ElementId>,
options: WindowOptions,
view: V,
) -> Result<WindowHandle, WindowCommandError> {
if self.window.is_none() {
return Err(WindowCommandError::Unavailable);
}
if options.kind != crate::WindowKind::SystemPopover || options.popover.is_none() {
return Err(WindowCommandError::InvalidPopoverConfiguration);
}
let mut request = WindowRequest::with_parent(view, options, self.window);
request.popover_anchor_element = Some(anchor.into());
let handle = request.handle;
self.open_windows.push(request);
Ok(handle)
}
pub fn window_handle(&self) -> Option<WindowHandle> {
self.window
}
pub const fn pointer_position(&self) -> Option<Point> {
self.pointer_position
}
fn current_window_handle(&self) -> Result<WindowHandle, WindowCommandError> {
self.window.ok_or(WindowCommandError::Unavailable)
}
fn push_window_command(&mut self, command: WindowCommand) -> Result<(), WindowCommandError> {
if self.window_commands.len() == crate::MAX_WINDOW_COMMANDS_PER_EVENT {
return Err(WindowCommandError::QueueFull);
}
self.window_commands.push(command);
Ok(())
}
fn push_platform_request(&mut self, request: PlatformRequest) -> Result<(), PlatformError> {
if self.platform_requests.len() == crate::MAX_PLATFORM_REQUESTS_PER_EVENT {
return Err(PlatformError::QueueFull);
}
self.platform_requests.push(request);
Ok(())
}
fn ensure_platform_capacity(&self) -> Result<(), PlatformError> {
if self.platform_requests.len() == crate::MAX_PLATFORM_REQUESTS_PER_EVENT {
Err(PlatformError::QueueFull)
} else {
Ok(())
}
}
pub fn prompt(
&mut self,
level: PromptLevel,
message: impl Into<Arc<str>>,
detail: Option<&str>,
buttons: &[PromptButton],
) -> Result<PlatformResponse<usize>, PlatformError> {
self.ensure_platform_capacity()?;
let window = self.window.ok_or(PlatformError::Unavailable)?;
let (request, response) =
PlatformRequest::prompt(window, level, message, detail.map(Arc::from), buttons)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn prompt_for_paths(
&mut self,
options: PathPromptOptions,
) -> Result<PathPromptResponse, PlatformError> {
self.ensure_platform_capacity()?;
let window = self.window.ok_or(PlatformError::Unavailable)?;
let (request, response) = PlatformRequest::open_paths(window, options)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn prompt_for_new_path(
&mut self,
options: SavePathOptions,
) -> Result<SavePathResponse, PlatformError> {
self.ensure_platform_capacity()?;
let window = self.window.ok_or(PlatformError::Unavailable)?;
let (request, response) = PlatformRequest::save_path(window, options)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn message_box(
&mut self,
options: MessageBoxOptions,
) -> Result<MessageBoxResponseFuture, PlatformError> {
self.ensure_platform_capacity()?;
let (request, response) = match self.window {
Some(window) => PlatformRequest::message_box(window, options)?,
None => PlatformRequest::application_message_box(options)?,
};
self.push_platform_request(request)?;
Ok(response)
}
pub fn preview_file(
&mut self,
path: impl Into<PathBuf>,
display_name: Option<&str>,
) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().file_previews {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
let request = PlatformRequest::preview_file(path, display_name.map(Arc::from))?;
self.push_platform_request(request)
}
pub fn preview_file_response(
&mut self,
path: impl Into<PathBuf>,
display_name: Option<&str>,
) -> Result<ShellResponse, PlatformError> {
self.ensure_platform_capacity()?;
let (request, response) =
PlatformRequest::preview_file_response(path, display_name.map(Arc::from))?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn close_file_preview(&mut self) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().file_previews {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::close_file_preview())
}
pub fn close_file_preview_response(&mut self) -> Result<ShellResponse, PlatformError> {
self.ensure_platform_capacity()?;
let (request, response) = PlatformRequest::close_file_preview_response();
self.push_platform_request(request)?;
Ok(response)
}
pub fn show_color_panel(
&mut self,
initial: Color,
mode: ColorPanelMode,
) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().color_panel {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::show_color_panel(initial, mode))
}
pub fn close_color_panel(&mut self) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().color_panel {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::close_color_panel())
}
pub fn show_font_panel(&mut self, font: Font) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().font_panel {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::show_font_panel(font))
}
pub fn share_items(&mut self, items: &[ShareItem], anchor: Rect) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().share_sheet {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
let window = Some(self.window.ok_or(PlatformError::Unavailable)?);
self.push_platform_request(PlatformRequest::share_items(window, items, anchor)?)
}
pub fn share_items_response(
&mut self,
items: &[ShareItem],
anchor: Rect,
) -> Result<ShellResponse, PlatformError> {
self.ensure_platform_capacity()?;
let window = Some(self.window.ok_or(PlatformError::Unavailable)?);
let (request, response) = PlatformRequest::share_items_response(window, items, anchor)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn authenticate_with_biometrics(
&mut self,
reason: impl Into<Arc<str>>,
) -> Result<PlatformResponse<bool>, PlatformError> {
if !crate::DesktopIntegrationSupport::current().biometric_authentication {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
let (request, response) = PlatformRequest::authenticate_with_biometrics(reason)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn show_system_notification(
&mut self,
notification: SystemNotification,
) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::show_system_notification(notification)?;
#[cfg(target_os = "windows")]
crate::runtime::validate_windows_notification_app_info(self.app_info.as_ref())?;
self.push_platform_request(request)
}
pub fn dismiss_system_notification(
&mut self,
tag: impl Into<Arc<str>>,
) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::dismiss_system_notification(tag)?;
#[cfg(target_os = "windows")]
crate::runtime::validate_windows_notification_app_info(self.app_info.as_ref())?;
self.push_platform_request(request)
}
pub fn notification_permission_status(
&mut self,
) -> Result<NotificationPermissionResponse, PlatformError> {
self.ensure_platform_capacity()?;
#[cfg(target_os = "windows")]
crate::runtime::validate_windows_notification_app_info(self.app_info.as_ref())?;
let (request, response) = PlatformRequest::notification_permission_status();
self.push_platform_request(request)?;
Ok(response)
}
pub fn request_notification_permission(
&mut self,
) -> Result<NotificationPermissionResponse, PlatformError> {
self.ensure_platform_capacity()?;
#[cfg(target_os = "windows")]
crate::runtime::validate_windows_notification_app_info(self.app_info.as_ref())?;
let (request, response) = PlatformRequest::request_notification_permission();
self.push_platform_request(request)?;
Ok(response)
}
pub fn open_url(&mut self, url: impl Into<Arc<str>>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::open_url(url)?;
#[cfg(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
self.push_platform_request(request)
}
#[cfg(not(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
)))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn open_path(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::open_path(path)?;
#[cfg(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
self.push_platform_request(request)
}
#[cfg(not(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
)))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn reveal_path(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::reveal_path(path)?;
#[cfg(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
self.push_platform_request(request)
}
#[cfg(not(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
)))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn trash_path(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::trash_path(path)?;
#[cfg(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
self.push_platform_request(request)
}
#[cfg(not(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
)))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn set_dock_badge(&mut self, value: impl Into<Arc<str>>) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().dock_badges {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
let value = value.into();
let request = PlatformRequest::set_dock_badge((!value.is_empty()).then_some(value))?;
self.push_platform_request(request)
}
pub fn clear_dock_badge(&mut self) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().dock_badges {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::set_dock_badge(None)?)
}
pub fn set_dock_icon(&mut self, icon: Image) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().dock_icons {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::set_dock_icon(Some(icon)))
}
pub fn clear_dock_icon(&mut self) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().dock_icons {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::set_dock_icon(None))
}
pub fn set_dock_menu(&mut self, menu: Menu) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().dock_menus {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::set_dock_menu(Some(menu))?)
}
pub fn clear_dock_menu(&mut self) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().dock_menus {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::set_dock_menu(None)?)
}
pub fn add_recent_document(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().recent_documents {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::add_recent_document(path)?)
}
pub fn clear_recent_documents(&mut self) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().recent_documents {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::ClearRecentDocuments)
}
pub fn show_about_panel(&mut self, options: AboutPanelOptions) -> Result<(), PlatformError> {
if !crate::DesktopIntegrationSupport::current().native_about_panel {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
self.push_platform_request(PlatformRequest::show_about_panel(options)?)
}
pub fn file_icon(
&mut self,
path: impl Into<PathBuf>,
size: FileIconSize,
) -> Result<FileIconResponse, PlatformError> {
if !crate::DesktopIntegrationSupport::current().file_icons {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
let (request, response) = PlatformRequest::get_file_icon(path, size)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn set_user_tasks(
&mut self,
tasks: impl IntoIterator<Item = UserTask>,
) -> Result<ShellResponse, PlatformError> {
if !crate::DesktopIntegrationSupport::current().user_tasks {
return Err(PlatformError::Unsupported);
}
self.ensure_platform_capacity()?;
let (request, response) = PlatformRequest::set_user_tasks(tasks.into_iter().collect())?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn clear_user_tasks(&mut self) -> Result<ShellResponse, PlatformError> {
self.set_user_tasks(std::iter::empty())
}
pub fn set_window_title(&mut self, title: impl Into<String>) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_title_handle(handle, title)
}
pub fn set_window_title_handle(
&mut self,
handle: WindowHandle,
title: impl Into<String>,
) -> Result<(), WindowCommandError> {
let title = title.into();
validate_window_title(&title)?;
self.push_window_command(WindowCommand::SetTitle(handle, title))
}
pub fn set_represented_file(
&mut self,
path: impl Into<PathBuf>,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_represented_file_handle(handle, path)
}
pub fn set_represented_file_handle(
&mut self,
handle: WindowHandle,
path: impl Into<PathBuf>,
) -> Result<(), WindowCommandError> {
let path = path.into();
validate_window_document_path(&path)?;
self.push_window_command(WindowCommand::SetRepresentedFile(handle, Some(path)))
}
pub fn set_document_path(&mut self, path: impl AsRef<Path>) -> Result<(), WindowCommandError> {
self.set_represented_file(path.as_ref().to_path_buf())
}
pub fn set_document_path_handle(
&mut self,
handle: WindowHandle,
path: impl AsRef<Path>,
) -> Result<(), WindowCommandError> {
self.set_represented_file_handle(handle, path.as_ref().to_path_buf())
}
pub fn clear_represented_file(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_represented_file_handle(handle)
}
pub fn clear_represented_file_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetRepresentedFile(handle, None))
}
pub fn set_window_edited(&mut self, edited: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_edited_handle(handle, edited)
}
pub fn set_window_edited_handle(
&mut self,
handle: WindowHandle,
edited: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetDocumentEdited(handle, edited))
}
pub fn set_document_edited(&mut self, edited: bool) -> Result<(), WindowCommandError> {
self.set_window_edited(edited)
}
pub fn show_character_palette(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.show_character_palette_handle(handle)
}
pub fn show_character_palette_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ShowCharacterPalette(handle))
}
pub fn show_definition_for_selection(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.show_definition_for_selection_handle(handle)
}
pub fn show_definition_for_selection_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::LookUpSelection(handle))
}
pub fn set_tabbing_identifier(
&mut self,
identifier: impl Into<String>,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_tabbing_identifier_handle(handle, identifier)
}
pub fn set_tabbing_identifier_handle(
&mut self,
handle: WindowHandle,
identifier: impl Into<String>,
) -> Result<(), WindowCommandError> {
let identifier = identifier.into();
validate_window_tabbing_identifier(&identifier)?;
self.push_window_command(WindowCommand::SetTabbingIdentifier(
handle,
Some(identifier),
))
}
pub fn clear_tabbing_identifier(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_tabbing_identifier_handle(handle)
}
pub fn clear_tabbing_identifier_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetTabbingIdentifier(handle, None))
}
pub fn select_next_tab(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.select_next_tab_handle(handle)
}
pub fn select_next_tab_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SelectNextTab(handle))
}
pub fn select_previous_tab(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.select_previous_tab_handle(handle)
}
pub fn select_previous_tab_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SelectPreviousTab(handle))
}
pub fn select_tab(&mut self, index: usize) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.select_tab_handle(handle, index)
}
pub fn select_tab_handle(
&mut self,
handle: WindowHandle,
index: usize,
) -> Result<(), WindowCommandError> {
if index >= MAX_SYSTEM_WINDOW_TABS {
return Err(WindowCommandError::InvalidTabIndex);
}
self.push_window_command(WindowCommand::SelectTab(handle, index))
}
pub fn merge_all_windows(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.merge_all_windows_handle(handle)
}
pub fn merge_all_windows_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::MergeAllWindows(handle))
}
pub fn move_tab_to_new_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.move_tab_to_new_window_handle(handle)
}
pub fn move_tab_to_new_window_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::MoveTabToNewWindow(handle))
}
pub fn toggle_tab_bar(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_tab_bar_handle(handle)
}
pub fn toggle_tab_bar_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleTabBar(handle))
}
pub fn toggle_tab_overview(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_tab_overview_handle(handle)
}
pub fn toggle_tab_overview_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleTabOverview(handle))
}
pub fn set_window_bounds(
&mut self,
bounds: crate::WindowBounds,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_bounds_handle(handle, bounds)
}
pub fn set_window_bounds_handle(
&mut self,
handle: WindowHandle,
bounds: crate::WindowBounds,
) -> Result<(), WindowCommandError> {
validate_window_bounds(bounds)?;
self.push_window_command(WindowCommand::SetBounds(handle, bounds))
}
pub fn move_window(&mut self, position: Point) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.move_window_handle(handle, position)
}
pub fn move_window_handle(
&mut self,
handle: WindowHandle,
position: Point,
) -> Result<(), WindowCommandError> {
validate_window_position(position)?;
self.push_window_command(WindowCommand::Move(handle, position))
}
pub fn resize_window(&mut self, size: Size) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.resize_window_handle(handle, size)
}
pub fn resize_window_handle(
&mut self,
handle: WindowHandle,
size: Size,
) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.push_window_command(WindowCommand::Resize(handle, size))
}
pub fn minimize_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.minimize_window_handle(handle)
}
pub fn minimize_window_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::Minimize(handle))
}
pub fn restore_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.restore_window_handle(handle)
}
pub fn restore_window_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::Restore(handle))
}
pub fn zoom_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.zoom_window_handle(handle)
}
pub fn zoom_window_handle(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::Zoom(handle))
}
pub fn toggle_fullscreen(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_fullscreen_handle(handle)
}
pub fn toggle_fullscreen_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleFullscreen(handle))
}
pub fn set_fullscreen(&mut self, fullscreen: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_fullscreen_handle(handle, fullscreen)
}
pub fn set_fullscreen_handle(
&mut self,
handle: WindowHandle,
fullscreen: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetFullscreen(handle, fullscreen))
}
pub fn show_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.show_window_handle(handle)
}
pub fn show_window_handle(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetVisible(handle, true))
}
pub fn hide_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.hide_window_handle(handle)
}
pub fn hide_window_handle(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetVisible(handle, false))
}
pub fn set_window_movable(&mut self, movable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_movable_handle(handle, movable)
}
pub fn set_window_movable_handle(
&mut self,
handle: WindowHandle,
movable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMovable(handle, movable))
}
pub fn set_window_resizable(&mut self, resizable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_resizable_handle(handle, resizable)
}
pub fn set_window_resizable_handle(
&mut self,
handle: WindowHandle,
resizable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetResizable(handle, resizable))
}
pub fn set_window_minimum_size(&mut self, size: Size) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_minimum_size_handle(handle, size)
}
pub fn set_window_minimum_size_handle(
&mut self,
handle: WindowHandle,
size: Size,
) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.push_window_command(WindowCommand::SetMinimumSize(handle, Some(size)))
}
pub fn clear_window_minimum_size(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_window_minimum_size_handle(handle)
}
pub fn clear_window_minimum_size_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMinimumSize(handle, None))
}
pub fn set_window_maximum_size(&mut self, size: Size) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_maximum_size_handle(handle, size)
}
pub fn set_window_maximum_size_handle(
&mut self,
handle: WindowHandle,
size: Size,
) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.push_window_command(WindowCommand::SetMaximumSize(handle, Some(size)))
}
pub fn clear_window_maximum_size(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_window_maximum_size_handle(handle)
}
pub fn clear_window_maximum_size_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMaximumSize(handle, None))
}
pub fn set_window_minimizable(&mut self, minimizable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_minimizable_handle(handle, minimizable)
}
pub fn set_window_minimizable_handle(
&mut self,
handle: WindowHandle,
minimizable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMinimizable(handle, minimizable))
}
pub fn set_window_maximizable(&mut self, maximizable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_maximizable_handle(handle, maximizable)
}
pub fn set_window_maximizable_handle(
&mut self,
handle: WindowHandle,
maximizable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMaximizable(handle, maximizable))
}
pub fn set_window_closable(&mut self, closable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_closable_handle(handle, closable)
}
pub fn set_window_closable_handle(
&mut self,
handle: WindowHandle,
closable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetClosable(handle, closable))
}
pub fn set_window_decorated(&mut self, decorated: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_decorated_handle(handle, decorated)
}
pub fn set_window_decorated_handle(
&mut self,
handle: WindowHandle,
decorated: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetDecorated(handle, decorated))
}
pub fn set_window_shadow(&mut self, shadow: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_shadow_handle(handle, shadow)
}
pub fn set_window_shadow_handle(
&mut self,
handle: WindowHandle,
shadow: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetShadow(handle, shadow))
}
pub fn set_window_content_protected(
&mut self,
protected: bool,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_content_protected_handle(handle, protected)
}
pub fn set_window_content_protected_handle(
&mut self,
handle: WindowHandle,
protected: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetContentProtected(handle, protected))
}
pub fn set_window_level(&mut self, level: WindowLevel) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_level_handle(handle, level)
}
pub fn set_window_level_handle(
&mut self,
handle: WindowHandle,
level: WindowLevel,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetWindowLevel(handle, Some(level)))
}
pub fn use_automatic_window_level(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.use_automatic_window_level_handle(handle)
}
pub fn use_automatic_window_level_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetWindowLevel(handle, None))
}
pub fn set_activation_policy(
&mut self,
policy: ActivationPolicy,
) -> Result<PlatformResponse<()>, PlatformError> {
let (request, response) = PlatformRequest::set_activation_policy(policy);
self.push_platform_request(request)?;
Ok(response)
}
pub fn activate_application(&mut self, force: bool) -> Result<(), PlatformError> {
self.push_platform_request(PlatformRequest::ActivateApplication { force })
}
pub fn hide_application(&mut self) -> Result<(), PlatformError> {
self.push_platform_request(PlatformRequest::HideApplication)
}
pub fn unhide_application(&mut self) -> Result<(), PlatformError> {
self.push_platform_request(PlatformRequest::UnhideApplication)
}
pub fn request_dock_attention(
&mut self,
attention: DockAttention,
) -> Result<PlatformResponse<DockAttentionRequest>, PlatformError> {
let (request, response) = PlatformRequest::request_dock_attention(attention);
self.push_platform_request(request)?;
Ok(response)
}
pub fn cancel_dock_attention(
&mut self,
request: DockAttentionRequest,
) -> Result<(), PlatformError> {
self.push_platform_request(PlatformRequest::CancelDockAttention(request))
}
pub fn set_dock_visible(
&mut self,
visible: bool,
) -> Result<PlatformResponse<()>, PlatformError> {
let (request, response) = PlatformRequest::set_dock_visible(visible);
self.push_platform_request(request)?;
Ok(response)
}
pub fn set_secure_keyboard_entry(&mut self, enabled: bool) -> Result<(), PlatformError> {
self.push_platform_request(PlatformRequest::SetSecureKeyboardEntry(enabled))
}
pub fn beep(&mut self) -> Result<(), PlatformError> {
self.push_platform_request(PlatformRequest::Beep)
}
pub fn applications_folder_support(&self) -> ApplicationsFolderSupport {
#[cfg(target_os = "macos")]
{
crate::macos_shell::applications_folder_support()
}
#[cfg(not(target_os = "macos"))]
{
ApplicationsFolderSupport::default()
}
}
pub fn move_to_applications_folder(&mut self) -> Result<PlatformResponse<bool>, PlatformError> {
let (request, response) = PlatformRequest::move_to_applications_folder();
self.push_platform_request(request)?;
Ok(response)
}
pub fn is_application_packaged(&self) -> bool {
crate::is_application_packaged()
}
pub fn exit_with_code(&mut self, code: i32) {
self.exit_code = Some(code);
self.exit();
}
pub fn move_window_top(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.move_window_top_handle(handle)
}
pub fn move_window_top_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::MoveToTop(handle))
}
pub fn move_window_above(&mut self, other: WindowHandle) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.move_window_above_handle(handle, other)
}
pub fn move_window_above_handle(
&mut self,
handle: WindowHandle,
other: WindowHandle,
) -> Result<(), WindowCommandError> {
if handle == other {
return Err(WindowCommandError::InvalidWindowOrder);
}
self.push_window_command(WindowCommand::MoveAbove(handle, other))
}
pub fn set_ignore_mouse_events(
&mut self,
ignore: bool,
forward: bool,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_ignore_mouse_events_handle(handle, ignore, forward)
}
pub fn set_ignore_mouse_events_handle(
&mut self,
handle: WindowHandle,
ignore: bool,
forward: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetIgnoreMouseEvents(
handle,
ignore,
ignore && forward,
))
}
pub fn set_window_enabled(&mut self, enabled: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_enabled_handle(handle, enabled)
}
pub fn set_window_enabled_handle(
&mut self,
handle: WindowHandle,
enabled: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetWindowEnabled(handle, enabled))
}
pub fn set_aspect_ratio(&mut self, ratio: Option<Size>) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_aspect_ratio_handle(handle, ratio)
}
pub fn set_aspect_ratio_handle(
&mut self,
handle: WindowHandle,
ratio: Option<Size>,
) -> Result<(), WindowCommandError> {
validate_window_aspect_ratio(ratio)?;
self.push_window_command(WindowCommand::SetAspectRatio(handle, ratio))
}
pub fn clear_aspect_ratio(&mut self) -> Result<(), WindowCommandError> {
self.set_aspect_ratio(None)
}
pub fn clear_aspect_ratio_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.set_aspect_ratio_handle(handle, None)
}
pub fn set_window_button_visibility(
&mut self,
visible: bool,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_button_visibility_handle(handle, visible)
}
pub fn set_window_button_visibility_handle(
&mut self,
handle: WindowHandle,
visible: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetWindowButtonVisibility(handle, visible))
}
pub fn constrain_resize(&mut self, size: Size) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.constrained_size = Some(size);
Ok(())
}
pub fn constrain_move(&mut self, position: Point) -> Result<(), WindowCommandError> {
validate_window_position(position)?;
self.constrained_position = Some(position);
Ok(())
}
pub fn set_window_always_on_top(
&mut self,
always_on_top: bool,
) -> Result<(), WindowCommandError> {
self.set_window_level(if always_on_top {
WindowLevel::AlwaysOnTop
} else {
WindowLevel::Normal
})
}
pub fn set_window_always_on_top_handle(
&mut self,
handle: WindowHandle,
always_on_top: bool,
) -> Result<(), WindowCommandError> {
self.set_window_level_handle(
handle,
if always_on_top {
WindowLevel::AlwaysOnTop
} else {
WindowLevel::Normal
},
)
}
pub fn set_window_focusable(&mut self, focusable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_focusable_handle(handle, focusable)
}
pub fn set_window_focusable_handle(
&mut self,
handle: WindowHandle,
focusable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetFocusable(handle, focusable))
}
pub fn set_window_skip_taskbar(&mut self, skip: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_skip_taskbar_handle(handle, skip)
}
pub fn set_window_skip_taskbar_handle(
&mut self,
handle: WindowHandle,
skip: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetSkipTaskbar(handle, skip))
}
pub fn set_window_visible_on_all_workspaces(
&mut self,
visible: bool,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_visible_on_all_workspaces_handle(handle, visible)
}
pub fn set_window_visible_on_all_workspaces_handle(
&mut self,
handle: WindowHandle,
visible: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetVisibleOnAllWorkspaces(handle, visible))
}
pub fn set_window_opacity(&mut self, opacity: f32) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_opacity_handle(handle, opacity)
}
pub fn set_window_opacity_handle(
&mut self,
handle: WindowHandle,
opacity: f32,
) -> Result<(), WindowCommandError> {
validate_window_opacity(opacity)?;
self.push_window_command(WindowCommand::SetOpacity(handle, opacity))
}
pub fn set_window_icon(&mut self, icon: Image) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_icon_handle(handle, icon)
}
pub fn set_window_icon_handle(
&mut self,
handle: WindowHandle,
icon: Image,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetIcon(handle, Some(icon)))
}
pub fn clear_window_icon(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_window_icon_handle(handle)
}
pub fn clear_window_icon_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetIcon(handle, None))
}
pub fn set_taskbar_progress(
&mut self,
state: TaskbarProgressState,
progress: f32,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_taskbar_progress_handle(handle, state, progress)
}
pub fn set_taskbar_progress_handle(
&mut self,
handle: WindowHandle,
state: TaskbarProgressState,
progress: f32,
) -> Result<(), WindowCommandError> {
validate_taskbar_progress(progress)?;
self.push_window_command(WindowCommand::SetTaskbarProgress(handle, state, progress))
}
pub fn set_taskbar_overlay_icon(
&mut self,
icon: Image,
description: impl Into<String>,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_taskbar_overlay_icon_handle(handle, icon, description)
}
pub fn set_taskbar_overlay_icon_handle(
&mut self,
handle: WindowHandle,
icon: Image,
description: impl Into<String>,
) -> Result<(), WindowCommandError> {
let description = description.into();
validate_taskbar_overlay_description(Some(&description))?;
self.push_window_command(WindowCommand::SetTaskbarOverlayIcon(
handle,
Some(icon),
Some(description),
))
}
pub fn clear_taskbar_overlay_icon(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_taskbar_overlay_icon_handle(handle)
}
pub fn clear_taskbar_overlay_icon_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetTaskbarOverlayIcon(handle, None, None))
}
pub fn set_cursor_visible(&mut self, visible: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_cursor_visible_handle(handle, visible)
}
pub fn set_cursor_visible_handle(
&mut self,
handle: WindowHandle,
visible: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetCursorVisible(handle, visible))
}
pub fn set_cursor_grab(&mut self, mode: CursorGrabMode) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_cursor_grab_handle(handle, mode)
}
pub fn set_cursor_grab_handle(
&mut self,
handle: WindowHandle,
mode: CursorGrabMode,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetCursorGrab(handle, mode))
}
pub fn set_cursor_hit_test(&mut self, hit_test: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_cursor_hit_test_handle(handle, hit_test)
}
pub fn set_cursor_hit_test_handle(
&mut self,
handle: WindowHandle,
hit_test: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetCursorHitTest(handle, hit_test))
}
pub fn set_cursor_position(&mut self, position: Point) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_cursor_position_handle(handle, position)
}
pub fn set_cursor_position_handle(
&mut self,
handle: WindowHandle,
position: Point,
) -> Result<(), WindowCommandError> {
validate_window_position(position)?;
self.push_window_command(WindowCommand::SetCursorPosition(handle, position))
}
pub fn set_window_appearance(
&mut self,
appearance: WindowAppearance,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_appearance_handle(handle, appearance)
}
pub fn set_window_appearance_handle(
&mut self,
handle: WindowHandle,
appearance: WindowAppearance,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetAppearance(handle, Some(appearance)))
}
pub fn follow_system_window_appearance(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.follow_system_window_appearance_handle(handle)
}
pub fn follow_system_window_appearance_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetAppearance(handle, None))
}
pub fn set_window_background_appearance(
&mut self,
appearance: WindowBackgroundAppearance,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_background_appearance_handle(handle, appearance)
}
pub fn set_window_background_appearance_handle(
&mut self,
handle: WindowHandle,
appearance: WindowBackgroundAppearance,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetBackgroundAppearance(handle, appearance))
}
pub fn set_macos_window_vibrancy(
&mut self,
vibrancy: Option<MacOsVibrancy>,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_macos_window_vibrancy_handle(handle, vibrancy)
}
pub fn set_macos_window_vibrancy_handle(
&mut self,
handle: WindowHandle,
vibrancy: Option<MacOsVibrancy>,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMacOsVibrancy(handle, vibrancy))
}
pub fn set_macos_visual_effect_state(
&mut self,
state: MacOsVisualEffectState,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_macos_visual_effect_state_handle(handle, state)
}
pub fn set_macos_visual_effect_state_handle(
&mut self,
handle: WindowHandle,
state: MacOsVisualEffectState,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMacOsVisualEffectState(handle, state))
}
#[cfg(feature = "inspector")]
pub fn set_inspector(&mut self, open: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_inspector_handle(handle, open)
}
#[cfg(feature = "inspector")]
pub fn set_inspector_handle(
&mut self,
handle: WindowHandle,
open: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetInspector(handle, open))
}
#[cfg(feature = "inspector")]
pub fn toggle_inspector(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_inspector_handle(handle)
}
#[cfg(feature = "inspector")]
pub fn toggle_inspector_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleInspector(handle))
}
pub fn request_window_attention(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.request_window_attention_handle(handle)
}
pub fn request_window_attention_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::RequestAttention(handle))
}
pub fn close_window(&mut self) {
self.close_current_window = true;
}
pub fn close_popover_chain(&mut self) -> bool {
let Some(root) = self.popover_root_window else {
return false;
};
if let Some(owner) = self.popover_owner_window
&& !self.focus_windows.contains(&owner)
{
self.focus_windows.push(owner);
}
if Some(root) == self.window {
self.close_current_window = true;
} else {
self.close_windows.push(root);
}
true
}
pub fn close_window_handle(&mut self, handle: WindowHandle) {
self.close_windows.push(handle);
}
pub fn focus_window(&mut self, handle: WindowHandle) {
self.focus_windows.push(handle);
}
pub fn invalidate_window(&mut self, handle: WindowHandle) {
self.invalidate_windows.push(handle);
}
pub fn prevent_close(&mut self) {
self.prevent_close = true;
}
pub fn prevent_quit(&mut self) {
self.prevent_quit = true;
}
pub fn focus(&mut self, handle: FocusHandle) {
self.focus = Some(Some(handle.id()));
}
pub fn blur(&mut self) {
self.focus = Some(None);
}
pub fn clear_text_selection(&mut self) {
self.clear_text_selection = true;
}
pub fn submit_form(&mut self, form: impl Into<ElementId>) -> bool {
let form = form.into();
if self.form_submissions.contains(&form) {
return true;
}
if self.form_submissions.len() == MAX_FORM_SUBMISSIONS_PER_EVENT {
return false;
}
self.form_submissions.push(form);
true
}
pub fn dispatch_action<A: Action>(&mut self, action: A) {
self.actions.push(AnyAction::new(action));
}
pub fn dispatch_any_action(&mut self, action: AnyAction) {
self.actions.push(action);
}
pub fn dispatch_action_to_window<A: Action>(
&mut self,
window: WindowHandle,
action: A,
) -> bool {
self.dispatch_any_action_to_window(window, AnyAction::new(action))
}
pub fn dispatch_any_action_to_window(
&mut self,
window: WindowHandle,
action: AnyAction,
) -> bool {
if self.targeted_actions.len() == MAX_TARGETED_ACTIONS_PER_EVENT {
return false;
}
self.targeted_actions.push((window, action));
true
}
pub fn dispatch_action_to_parent<A: Action>(&mut self, action: A) -> bool {
let Some(parent) = self.parent_window else {
return false;
};
self.dispatch_action_to_window(parent, action)
}
pub fn dispatch_any_action_to_parent(&mut self, action: AnyAction) -> bool {
let Some(parent) = self.parent_window else {
return false;
};
self.dispatch_any_action_to_window(parent, action)
}
pub const fn parent_window_handle(&self) -> Option<WindowHandle> {
self.parent_window
}
pub fn dispatch_action_to_popover_owner<A: Action>(&mut self, action: A) -> bool {
let Some(owner) = self.popover_owner_window else {
return false;
};
self.dispatch_action_to_window(owner, action)
}
pub fn dispatch_any_action_to_popover_owner(&mut self, action: AnyAction) -> bool {
let Some(owner) = self.popover_owner_window else {
return false;
};
self.dispatch_any_action_to_window(owner, action)
}
pub const fn popover_owner_window_handle(&self) -> Option<WindowHandle> {
self.popover_owner_window
}
pub const fn popover_root_window_handle(&self) -> Option<WindowHandle> {
self.popover_root_window
}
pub fn set_menus(&mut self, menus: impl IntoIterator<Item = Menu>) {
self.menus = Some(menus.into_iter().collect());
}
pub fn clear_menus(&mut self) {
self.menus = Some(Vec::new());
}
pub fn set_window_menus(&mut self, menus: impl IntoIterator<Item = Menu>) {
self.window_menus = Some(Some(menus.into_iter().collect()));
}
pub fn clear_window_menus(&mut self) {
self.window_menus = Some(Some(Vec::new()));
}
pub fn use_application_menus(&mut self) {
self.window_menus = Some(None);
}
pub fn show_native_popup_menu(
&mut self,
menu: Menu,
position: Option<Point>,
) -> Result<(), PlatformError> {
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
let _ = (menu, position);
return Err(PlatformError::Unsupported);
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
{
if self.window.is_none() {
return Err(PlatformError::Unavailable);
}
if self.native_popup_menus.len() == MAX_NATIVE_POPUP_MENUS_PER_EVENT {
return Err(PlatformError::QueueFull);
}
if position.is_some_and(|position| validate_window_position(position).is_err()) {
return Err(PlatformError::InvalidMenuPosition);
}
validate_menus(std::slice::from_ref(&menu)).map_err(|_| PlatformError::InvalidMenu)?;
self.native_popup_menus
.push(NativePopupMenuRequest { menu, position });
Ok(())
}
}
pub fn propagate(&mut self) {
self.propagate_action = true;
self.stop_event_propagation = false;
}
pub fn stop_propagation(&mut self) {
self.stop_event_propagation = true;
}
pub fn prevent_default(&mut self) {
self.prevent_default = true;
}
}