lib.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // Copyright 2019-2022 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Internal runtime between Tauri and the underlying webview runtime.
  5. #![cfg_attr(doc_cfg, feature(doc_cfg))]
  6. use raw_window_handle::RawDisplayHandle;
  7. use serde::Deserialize;
  8. use std::{fmt::Debug, sync::mpsc::Sender};
  9. use tauri_utils::Theme;
  10. use uuid::Uuid;
  11. pub mod http;
  12. /// Create window and system tray menus.
  13. pub mod menu;
  14. /// Types useful for interacting with a user's monitors.
  15. pub mod monitor;
  16. pub mod webview;
  17. pub mod window;
  18. use monitor::Monitor;
  19. use webview::WindowBuilder;
  20. use window::{
  21. dpi::{PhysicalPosition, PhysicalSize, Position, Size},
  22. CursorIcon, DetachedWindow, PendingWindow, WindowEvent,
  23. };
  24. use crate::http::{
  25. header::{InvalidHeaderName, InvalidHeaderValue},
  26. method::InvalidMethod,
  27. status::InvalidStatusCode,
  28. InvalidUri,
  29. };
  30. #[cfg(all(desktop, feature = "system-tray"))]
  31. use std::fmt;
  32. pub type TrayId = u16;
  33. pub type TrayEventHandler = dyn Fn(&SystemTrayEvent) + Send + 'static;
  34. #[cfg(all(desktop, feature = "system-tray"))]
  35. #[non_exhaustive]
  36. pub struct SystemTray {
  37. pub id: TrayId,
  38. pub icon: Option<Icon>,
  39. pub menu: Option<menu::SystemTrayMenu>,
  40. #[cfg(target_os = "macos")]
  41. pub icon_as_template: bool,
  42. #[cfg(target_os = "macos")]
  43. pub menu_on_left_click: bool,
  44. #[cfg(target_os = "macos")]
  45. pub title: Option<String>,
  46. pub on_event: Option<Box<TrayEventHandler>>,
  47. }
  48. #[cfg(all(desktop, feature = "system-tray"))]
  49. impl fmt::Debug for SystemTray {
  50. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  51. let mut d = f.debug_struct("SystemTray");
  52. d.field("id", &self.id)
  53. .field("icon", &self.icon)
  54. .field("menu", &self.menu);
  55. #[cfg(target_os = "macos")]
  56. {
  57. d.field("icon_as_template", &self.icon_as_template)
  58. .field("menu_on_left_click", &self.menu_on_left_click)
  59. .field("title", &self.title);
  60. }
  61. d.finish()
  62. }
  63. }
  64. #[cfg(all(desktop, feature = "system-tray"))]
  65. impl Clone for SystemTray {
  66. fn clone(&self) -> Self {
  67. Self {
  68. id: self.id,
  69. icon: self.icon.clone(),
  70. menu: self.menu.clone(),
  71. on_event: None,
  72. #[cfg(target_os = "macos")]
  73. icon_as_template: self.icon_as_template,
  74. #[cfg(target_os = "macos")]
  75. menu_on_left_click: self.menu_on_left_click,
  76. #[cfg(target_os = "macos")]
  77. title: self.title.clone(),
  78. }
  79. }
  80. }
  81. #[cfg(all(desktop, feature = "system-tray"))]
  82. impl Default for SystemTray {
  83. fn default() -> Self {
  84. Self {
  85. id: rand::random(),
  86. icon: None,
  87. menu: None,
  88. #[cfg(target_os = "macos")]
  89. icon_as_template: false,
  90. #[cfg(target_os = "macos")]
  91. menu_on_left_click: false,
  92. #[cfg(target_os = "macos")]
  93. title: None,
  94. on_event: None,
  95. }
  96. }
  97. }
  98. #[cfg(all(desktop, feature = "system-tray"))]
  99. impl SystemTray {
  100. /// Creates a new system tray that only renders an icon.
  101. pub fn new() -> Self {
  102. Default::default()
  103. }
  104. pub fn menu(&self) -> Option<&menu::SystemTrayMenu> {
  105. self.menu.as_ref()
  106. }
  107. /// Sets the tray id.
  108. #[must_use]
  109. pub fn with_id(mut self, id: TrayId) -> Self {
  110. self.id = id;
  111. self
  112. }
  113. /// Sets the tray icon.
  114. #[must_use]
  115. pub fn with_icon(mut self, icon: Icon) -> Self {
  116. self.icon.replace(icon);
  117. self
  118. }
  119. /// Sets the tray icon as template.
  120. #[cfg(target_os = "macos")]
  121. #[must_use]
  122. pub fn with_icon_as_template(mut self, is_template: bool) -> Self {
  123. self.icon_as_template = is_template;
  124. self
  125. }
  126. /// Sets whether the menu should appear when the tray receives a left click. Defaults to `true`.
  127. #[cfg(target_os = "macos")]
  128. #[must_use]
  129. pub fn with_menu_on_left_click(mut self, menu_on_left_click: bool) -> Self {
  130. self.menu_on_left_click = menu_on_left_click;
  131. self
  132. }
  133. #[cfg(target_os = "macos")]
  134. #[must_use]
  135. pub fn with_title(mut self, title: &str) -> Self {
  136. self.title = Some(title.to_owned());
  137. self
  138. }
  139. /// Sets the menu to show when the system tray is right clicked.
  140. #[must_use]
  141. pub fn with_menu(mut self, menu: menu::SystemTrayMenu) -> Self {
  142. self.menu.replace(menu);
  143. self
  144. }
  145. #[must_use]
  146. pub fn on_event<F: Fn(&SystemTrayEvent) + Send + 'static>(mut self, f: F) -> Self {
  147. self.on_event.replace(Box::new(f));
  148. self
  149. }
  150. }
  151. /// Type of user attention requested on a window.
  152. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  153. #[serde(tag = "type")]
  154. pub enum UserAttentionType {
  155. /// ## Platform-specific
  156. /// - **macOS:** Bounces the dock icon until the application is in focus.
  157. /// - **Windows:** Flashes both the window and the taskbar button until the application is in focus.
  158. Critical,
  159. /// ## Platform-specific
  160. /// - **macOS:** Bounces the dock icon once.
  161. /// - **Windows:** Flashes the taskbar button until the application is in focus.
  162. Informational,
  163. }
  164. #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
  165. #[serde(tag = "type")]
  166. pub enum DeviceEventFilter {
  167. /// Always filter out device events.
  168. Always,
  169. /// Filter out device events while the window is not focused.
  170. Unfocused,
  171. /// Report all device events regardless of window focus.
  172. Never,
  173. }
  174. impl Default for DeviceEventFilter {
  175. fn default() -> Self {
  176. Self::Unfocused
  177. }
  178. }
  179. #[derive(Debug, thiserror::Error)]
  180. #[non_exhaustive]
  181. pub enum Error {
  182. /// Failed to create webview.
  183. #[error("failed to create webview: {0}")]
  184. CreateWebview(Box<dyn std::error::Error + Send + Sync>),
  185. /// Failed to create window.
  186. #[error("failed to create window")]
  187. CreateWindow,
  188. /// The given window label is invalid.
  189. #[error("Window labels must only include alphanumeric characters, `-`, `/`, `:` and `_`.")]
  190. InvalidWindowLabel,
  191. /// Failed to send message to webview.
  192. #[error("failed to send message to the webview")]
  193. FailedToSendMessage,
  194. /// Failed to receive message from webview.
  195. #[error("failed to receive message from webview")]
  196. FailedToReceiveMessage,
  197. /// Failed to serialize/deserialize.
  198. #[error("JSON error: {0}")]
  199. Json(#[from] serde_json::Error),
  200. /// Encountered an error creating the app system tray.
  201. #[cfg(all(desktop, feature = "system-tray"))]
  202. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  203. #[error("error encountered during tray setup: {0}")]
  204. SystemTray(Box<dyn std::error::Error + Send + Sync>),
  205. /// Failed to load window icon.
  206. #[error("invalid icon: {0}")]
  207. InvalidIcon(Box<dyn std::error::Error + Send + Sync>),
  208. /// Failed to get monitor on window operation.
  209. #[error("failed to get monitor")]
  210. FailedToGetMonitor,
  211. /// Global shortcut error.
  212. #[cfg(all(desktop, feature = "global-shortcut"))]
  213. #[error(transparent)]
  214. GlobalShortcut(Box<dyn std::error::Error + Send + Sync>),
  215. #[error("Invalid header name: {0}")]
  216. InvalidHeaderName(#[from] InvalidHeaderName),
  217. #[error("Invalid header value: {0}")]
  218. InvalidHeaderValue(#[from] InvalidHeaderValue),
  219. #[error("Invalid uri: {0}")]
  220. InvalidUri(#[from] InvalidUri),
  221. #[error("Invalid status code: {0}")]
  222. InvalidStatusCode(#[from] InvalidStatusCode),
  223. #[error("Invalid method: {0}")]
  224. InvalidMethod(#[from] InvalidMethod),
  225. #[error("Infallible error, something went really wrong: {0}")]
  226. Infallible(#[from] std::convert::Infallible),
  227. #[error("the event loop has been closed")]
  228. EventLoopClosed,
  229. }
  230. /// Result type.
  231. pub type Result<T> = std::result::Result<T, Error>;
  232. /// Window icon.
  233. #[derive(Debug, Clone)]
  234. pub struct Icon {
  235. /// RGBA bytes of the icon.
  236. pub rgba: Vec<u8>,
  237. /// Icon width.
  238. pub width: u32,
  239. /// Icon height.
  240. pub height: u32,
  241. }
  242. /// A type that can be used as an user event.
  243. pub trait UserEvent: Debug + Clone + Send + 'static {}
  244. impl<T: Debug + Clone + Send + 'static> UserEvent for T {}
  245. /// Event triggered on the event loop run.
  246. #[non_exhaustive]
  247. pub enum RunEvent<T: UserEvent> {
  248. /// Event loop is exiting.
  249. Exit,
  250. /// Event loop is about to exit
  251. ExitRequested {
  252. tx: Sender<ExitRequestedEventAction>,
  253. },
  254. /// An event associated with a window.
  255. WindowEvent {
  256. /// The window label.
  257. label: String,
  258. /// The detailed event.
  259. event: WindowEvent,
  260. },
  261. /// Application ready.
  262. Ready,
  263. /// Sent if the event loop is being resumed.
  264. Resumed,
  265. /// Emitted when all of the event loop’s input events have been processed and redraw processing is about to begin.
  266. ///
  267. /// This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the “main body” of your event loop.
  268. MainEventsCleared,
  269. /// A custom event defined by the user.
  270. UserEvent(T),
  271. }
  272. /// Action to take when the event loop is about to exit
  273. #[derive(Debug)]
  274. pub enum ExitRequestedEventAction {
  275. /// Prevent the event loop from exiting
  276. Prevent,
  277. }
  278. /// A system tray event.
  279. #[derive(Debug)]
  280. pub enum SystemTrayEvent {
  281. MenuItemClick(u16),
  282. LeftClick {
  283. position: PhysicalPosition<f64>,
  284. size: PhysicalSize<f64>,
  285. },
  286. RightClick {
  287. position: PhysicalPosition<f64>,
  288. size: PhysicalSize<f64>,
  289. },
  290. DoubleClick {
  291. position: PhysicalPosition<f64>,
  292. size: PhysicalSize<f64>,
  293. },
  294. }
  295. /// Metadata for a runtime event loop iteration on `run_iteration`.
  296. #[derive(Debug, Clone, Default)]
  297. pub struct RunIteration {
  298. pub window_count: usize,
  299. }
  300. /// Application's activation policy. Corresponds to NSApplicationActivationPolicy.
  301. #[cfg(target_os = "macos")]
  302. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  303. #[non_exhaustive]
  304. pub enum ActivationPolicy {
  305. /// Corresponds to NSApplicationActivationPolicyRegular.
  306. Regular,
  307. /// Corresponds to NSApplicationActivationPolicyAccessory.
  308. Accessory,
  309. /// Corresponds to NSApplicationActivationPolicyProhibited.
  310. Prohibited,
  311. }
  312. /// A [`Send`] handle to the runtime.
  313. pub trait RuntimeHandle<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  314. type Runtime: Runtime<T, Handle = Self>;
  315. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  316. fn create_proxy(&self) -> <Self::Runtime as Runtime<T>>::EventLoopProxy;
  317. /// Create a new webview window.
  318. fn create_window(
  319. &self,
  320. pending: PendingWindow<T, Self::Runtime>,
  321. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  322. /// Run a task on the main thread.
  323. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  324. /// Adds an icon to the system tray with the specified menu items.
  325. #[cfg(all(desktop, feature = "system-tray"))]
  326. #[cfg_attr(doc_cfg, doc(cfg(all(desktop, feature = "system-tray"))))]
  327. fn system_tray(
  328. &self,
  329. system_tray: SystemTray,
  330. ) -> Result<<Self::Runtime as Runtime<T>>::TrayHandler>;
  331. fn raw_display_handle(&self) -> RawDisplayHandle;
  332. /// Shows the application, but does not automatically focus it.
  333. #[cfg(target_os = "macos")]
  334. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  335. fn show(&self) -> Result<()>;
  336. /// Hides the application.
  337. #[cfg(target_os = "macos")]
  338. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  339. fn hide(&self) -> Result<()>;
  340. }
  341. /// A global shortcut manager.
  342. #[cfg(all(desktop, feature = "global-shortcut"))]
  343. pub trait GlobalShortcutManager: Debug + Clone + Send + Sync {
  344. /// Whether the application has registered the given `accelerator`.
  345. fn is_registered(&self, accelerator: &str) -> Result<bool>;
  346. /// Register a global shortcut of `accelerator`.
  347. fn register<F: Fn() + Send + 'static>(&mut self, accelerator: &str, handler: F) -> Result<()>;
  348. /// Unregister all accelerators registered by the manager instance.
  349. fn unregister_all(&mut self) -> Result<()>;
  350. /// Unregister the provided `accelerator`.
  351. fn unregister(&mut self, accelerator: &str) -> Result<()>;
  352. }
  353. /// Clipboard manager.
  354. #[cfg(feature = "clipboard")]
  355. pub trait ClipboardManager: Debug + Clone + Send + Sync {
  356. /// Writes the text into the clipboard as plain text.
  357. fn write_text<T: Into<String>>(&mut self, text: T) -> Result<()>;
  358. /// Read the content in the clipboard as plain text.
  359. fn read_text(&self) -> Result<Option<String>>;
  360. }
  361. pub trait EventLoopProxy<T: UserEvent>: Debug + Clone + Send + Sync {
  362. fn send_event(&self, event: T) -> Result<()>;
  363. }
  364. /// The webview runtime interface.
  365. pub trait Runtime<T: UserEvent>: Debug + Sized + 'static {
  366. /// The message dispatcher.
  367. type Dispatcher: Dispatch<T, Runtime = Self>;
  368. /// The runtime handle type.
  369. type Handle: RuntimeHandle<T, Runtime = Self>;
  370. /// The global shortcut manager type.
  371. #[cfg(all(desktop, feature = "global-shortcut"))]
  372. type GlobalShortcutManager: GlobalShortcutManager;
  373. /// The clipboard manager type.
  374. #[cfg(feature = "clipboard")]
  375. type ClipboardManager: ClipboardManager;
  376. /// The tray handler type.
  377. #[cfg(all(desktop, feature = "system-tray"))]
  378. type TrayHandler: menu::TrayHandle;
  379. /// The proxy type.
  380. type EventLoopProxy: EventLoopProxy<T>;
  381. /// Creates a new webview runtime. Must be used on the main thread.
  382. fn new() -> Result<Self>;
  383. /// Creates a new webview runtime on any thread.
  384. #[cfg(any(windows, target_os = "linux"))]
  385. #[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "linux"))))]
  386. fn new_any_thread() -> Result<Self>;
  387. /// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
  388. fn create_proxy(&self) -> Self::EventLoopProxy;
  389. /// Gets a runtime handle.
  390. fn handle(&self) -> Self::Handle;
  391. /// Gets the global shortcut manager.
  392. #[cfg(all(desktop, feature = "global-shortcut"))]
  393. fn global_shortcut_manager(&self) -> Self::GlobalShortcutManager;
  394. /// Gets the clipboard manager.
  395. #[cfg(feature = "clipboard")]
  396. fn clipboard_manager(&self) -> Self::ClipboardManager;
  397. /// Create a new webview window.
  398. fn create_window(&self, pending: PendingWindow<T, Self>) -> Result<DetachedWindow<T, Self>>;
  399. /// Adds the icon to the system tray with the specified menu items.
  400. #[cfg(all(desktop, feature = "system-tray"))]
  401. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  402. fn system_tray(&self, system_tray: SystemTray) -> Result<Self::TrayHandler>;
  403. /// Registers a system tray event handler.
  404. #[cfg(all(desktop, feature = "system-tray"))]
  405. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  406. fn on_system_tray_event<F: Fn(TrayId, &SystemTrayEvent) + Send + 'static>(&mut self, f: F);
  407. /// Sets the activation policy for the application. It is set to `NSApplicationActivationPolicyRegular` by default.
  408. #[cfg(target_os = "macos")]
  409. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  410. fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);
  411. /// Shows the application, but does not automatically focus it.
  412. #[cfg(target_os = "macos")]
  413. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  414. fn show(&self);
  415. /// Hides the application.
  416. #[cfg(target_os = "macos")]
  417. #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
  418. fn hide(&self);
  419. /// Change the device event filter mode.
  420. ///
  421. /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
  422. /// will ignore them by default for unfocused windows on Windows. This method allows changing
  423. /// the filter to explicitly capture them again.
  424. ///
  425. /// ## Platform-specific
  426. ///
  427. /// - ** Linux / macOS / iOS / Android**: Unsupported.
  428. ///
  429. /// [`tao`]: https://crates.io/crates/tao
  430. fn set_device_event_filter(&mut self, filter: DeviceEventFilter);
  431. /// Runs the one step of the webview runtime event loop and returns control flow to the caller.
  432. #[cfg(desktop)]
  433. fn run_iteration<F: Fn(RunEvent<T>) + 'static>(&mut self, callback: F) -> RunIteration;
  434. /// Run the webview runtime.
  435. fn run<F: FnMut(RunEvent<T>) + 'static>(self, callback: F);
  436. }
  437. /// Webview dispatcher. A thread-safe handle to the webview API.
  438. pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static {
  439. /// The runtime this [`Dispatch`] runs under.
  440. type Runtime: Runtime<T>;
  441. /// The window builder type.
  442. type WindowBuilder: WindowBuilder;
  443. /// Run a task on the main thread.
  444. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()>;
  445. /// Registers a window event handler.
  446. fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) -> Uuid;
  447. /// Registers a window event handler.
  448. fn on_menu_event<F: Fn(&window::MenuEvent) + Send + 'static>(&self, f: F) -> Uuid;
  449. /// Open the web inspector which is usually called devtools.
  450. #[cfg(any(debug_assertions, feature = "devtools"))]
  451. fn open_devtools(&self);
  452. /// Close the web inspector which is usually called devtools.
  453. #[cfg(any(debug_assertions, feature = "devtools"))]
  454. fn close_devtools(&self);
  455. /// Gets the devtools window's current open state.
  456. #[cfg(any(debug_assertions, feature = "devtools"))]
  457. fn is_devtools_open(&self) -> Result<bool>;
  458. // GETTERS
  459. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  460. fn scale_factor(&self) -> Result<f64>;
  461. /// Returns the position of the top-left hand corner of the window's client area relative to the top-left hand corner of the desktop.
  462. fn inner_position(&self) -> Result<PhysicalPosition<i32>>;
  463. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  464. fn outer_position(&self) -> Result<PhysicalPosition<i32>>;
  465. /// Returns the physical size of the window's client area.
  466. ///
  467. /// The client area is the content of the window, excluding the title bar and borders.
  468. fn inner_size(&self) -> Result<PhysicalSize<u32>>;
  469. /// Returns the physical size of the entire window.
  470. ///
  471. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  472. fn outer_size(&self) -> Result<PhysicalSize<u32>>;
  473. /// Gets the window's current fullscreen state.
  474. fn is_fullscreen(&self) -> Result<bool>;
  475. /// Gets the window's current minimized state.
  476. fn is_minimized(&self) -> Result<bool>;
  477. /// Gets the window's current maximized state.
  478. fn is_maximized(&self) -> Result<bool>;
  479. /// Gets the window’s current decoration state.
  480. fn is_decorated(&self) -> Result<bool>;
  481. /// Gets the window’s current resizable state.
  482. fn is_resizable(&self) -> Result<bool>;
  483. /// Gets the window's current visibility state.
  484. fn is_visible(&self) -> Result<bool>;
  485. /// Gets the window's current title.
  486. fn title(&self) -> Result<String>;
  487. /// Gets the window menu current visibility state.
  488. fn is_menu_visible(&self) -> Result<bool>;
  489. /// Returns the monitor on which the window currently resides.
  490. ///
  491. /// Returns None if current monitor can't be detected.
  492. fn current_monitor(&self) -> Result<Option<Monitor>>;
  493. /// Returns the primary monitor of the system.
  494. ///
  495. /// Returns None if it can't identify any monitor as a primary one.
  496. fn primary_monitor(&self) -> Result<Option<Monitor>>;
  497. /// Returns the list of all the monitors available on the system.
  498. fn available_monitors(&self) -> Result<Vec<Monitor>>;
  499. /// Returns the `ApplicationWindow` from gtk crate that is used by this window.
  500. #[cfg(any(
  501. target_os = "linux",
  502. target_os = "dragonfly",
  503. target_os = "freebsd",
  504. target_os = "netbsd",
  505. target_os = "openbsd"
  506. ))]
  507. fn gtk_window(&self) -> Result<gtk::ApplicationWindow>;
  508. fn raw_window_handle(&self) -> Result<raw_window_handle::RawWindowHandle>;
  509. /// Returns the current window theme.
  510. fn theme(&self) -> Result<Theme>;
  511. // SETTERS
  512. /// Centers the window.
  513. fn center(&self) -> Result<()>;
  514. /// Opens the dialog to prints the contents of the webview.
  515. fn print(&self) -> Result<()>;
  516. /// Requests user attention to the window.
  517. ///
  518. /// Providing `None` will unset the request for user attention.
  519. fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()>;
  520. /// Create a new webview window.
  521. fn create_window(
  522. &mut self,
  523. pending: PendingWindow<T, Self::Runtime>,
  524. ) -> Result<DetachedWindow<T, Self::Runtime>>;
  525. /// Updates the window resizable flag.
  526. fn set_resizable(&self, resizable: bool) -> Result<()>;
  527. /// Updates the window title.
  528. fn set_title<S: Into<String>>(&self, title: S) -> Result<()>;
  529. /// Maximizes the window.
  530. fn maximize(&self) -> Result<()>;
  531. /// Unmaximizes the window.
  532. fn unmaximize(&self) -> Result<()>;
  533. /// Minimizes the window.
  534. fn minimize(&self) -> Result<()>;
  535. /// Unminimizes the window.
  536. fn unminimize(&self) -> Result<()>;
  537. /// Shows the window menu.
  538. fn show_menu(&self) -> Result<()>;
  539. /// Hides the window menu.
  540. fn hide_menu(&self) -> Result<()>;
  541. /// Shows the window.
  542. fn show(&self) -> Result<()>;
  543. /// Hides the window.
  544. fn hide(&self) -> Result<()>;
  545. /// Closes the window.
  546. fn close(&self) -> Result<()>;
  547. /// Updates the hasDecorations flag.
  548. fn set_decorations(&self, decorations: bool) -> Result<()>;
  549. /// Updates the window alwaysOnTop flag.
  550. fn set_always_on_top(&self, always_on_top: bool) -> Result<()>;
  551. /// Prevents the window contents from being captured by other apps.
  552. fn set_content_protected(&self, protected: bool) -> Result<()>;
  553. /// Resizes the window.
  554. fn set_size(&self, size: Size) -> Result<()>;
  555. /// Updates the window min size.
  556. fn set_min_size(&self, size: Option<Size>) -> Result<()>;
  557. /// Updates the window max size.
  558. fn set_max_size(&self, size: Option<Size>) -> Result<()>;
  559. /// Updates the window position.
  560. fn set_position(&self, position: Position) -> Result<()>;
  561. /// Updates the window fullscreen state.
  562. fn set_fullscreen(&self, fullscreen: bool) -> Result<()>;
  563. /// Bring the window to front and focus.
  564. fn set_focus(&self) -> Result<()>;
  565. /// Updates the window icon.
  566. fn set_icon(&self, icon: Icon) -> Result<()>;
  567. /// Whether to hide the window icon from the taskbar or not.
  568. fn set_skip_taskbar(&self, skip: bool) -> Result<()>;
  569. /// Grabs the cursor, preventing it from leaving the window.
  570. ///
  571. /// There's no guarantee that the cursor will be hidden. You should
  572. /// hide it by yourself if you want so.
  573. fn set_cursor_grab(&self, grab: bool) -> Result<()>;
  574. /// Modifies the cursor's visibility.
  575. ///
  576. /// If `false`, this will hide the cursor. If `true`, this will show the cursor.
  577. fn set_cursor_visible(&self, visible: bool) -> Result<()>;
  578. // Modifies the cursor icon of the window.
  579. fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>;
  580. /// Changes the position of the cursor in window coordinates.
  581. fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>;
  582. /// Ignores the window cursor events.
  583. fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()>;
  584. /// Starts dragging the window.
  585. fn start_dragging(&self) -> Result<()>;
  586. /// Executes javascript on the window this [`Dispatch`] represents.
  587. fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
  588. /// Applies the specified `update` to the menu item associated with the given `id`.
  589. fn update_menu_item(&self, id: u16, update: menu::MenuUpdate) -> Result<()>;
  590. }