lib.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2019-2021 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 std::{fmt::Debug, hash::Hash, path::PathBuf};
  7. use serde::Serialize;
  8. use tauri_utils::assets::Assets;
  9. use uuid::Uuid;
  10. /// Create window and system tray menus.
  11. #[cfg(any(feature = "menu", feature = "system-tray"))]
  12. #[cfg_attr(doc_cfg, doc(cfg(any(feature = "menu", feature = "system-tray"))))]
  13. pub mod menu;
  14. /// Types useful for interacting with a user's monitors.
  15. pub mod monitor;
  16. pub mod tag;
  17. pub mod webview;
  18. pub mod window;
  19. use monitor::Monitor;
  20. use tag::Tag;
  21. use webview::WindowBuilder;
  22. use window::{
  23. dpi::{PhysicalPosition, PhysicalSize, Position, Size},
  24. DetachedWindow, PendingWindow, WindowEvent,
  25. };
  26. /// A type that can be derived into a menu id.
  27. pub trait MenuId: Serialize + Hash + Eq + Debug + Clone + Send + Sync + 'static {}
  28. impl<T> MenuId for T where T: Serialize + Hash + Eq + Debug + Clone + Send + Sync + 'static {}
  29. #[derive(Debug, thiserror::Error)]
  30. #[non_exhaustive]
  31. pub enum Error {
  32. /// Failed to create webview.
  33. #[error("failed to create webview: {0}")]
  34. CreateWebview(Box<dyn std::error::Error + Send>),
  35. /// Failed to create window.
  36. #[error("failed to create window")]
  37. CreateWindow,
  38. /// Failed to send message to webview.
  39. #[error("failed to send message to the webview")]
  40. FailedToSendMessage,
  41. /// Failed to serialize/deserialize.
  42. #[error("JSON error: {0}")]
  43. Json(#[from] serde_json::Error),
  44. /// Encountered an error creating the app system tray.
  45. #[cfg(feature = "system-tray")]
  46. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  47. #[error("error encountered during tray setup: {0}")]
  48. SystemTray(Box<dyn std::error::Error + Send>),
  49. /// Failed to load window icon.
  50. #[error("invalid icon: {0}")]
  51. InvalidIcon(Box<dyn std::error::Error + Send>),
  52. }
  53. /// Result type.
  54. pub type Result<T> = std::result::Result<T, Error>;
  55. #[doc(hidden)]
  56. pub mod private {
  57. pub trait ParamsBase {}
  58. }
  59. /// Types associated with the running Tauri application.
  60. pub trait Params: private::ParamsBase + 'static {
  61. /// The event type used to create and listen to events.
  62. type Event: Tag;
  63. /// The type used to determine the name of windows.
  64. type Label: Tag;
  65. /// The type used to determine window menu ids.
  66. type MenuId: MenuId;
  67. /// The type used to determine system tray menu ids.
  68. type SystemTrayMenuId: MenuId;
  69. /// Assets that Tauri should serve from itself.
  70. type Assets: Assets;
  71. /// The underlying webview runtime used by the Tauri application.
  72. type Runtime: Runtime;
  73. }
  74. /// A icon definition.
  75. #[derive(Debug, Clone)]
  76. #[non_exhaustive]
  77. pub enum Icon {
  78. /// Icon from file path.
  79. File(PathBuf),
  80. /// Icon from raw bytes.
  81. Raw(Vec<u8>),
  82. }
  83. /// A system tray event.
  84. pub struct SystemTrayEvent {
  85. pub menu_item_id: u32,
  86. }
  87. /// The webview runtime interface.
  88. pub trait Runtime: Sized + 'static {
  89. /// The message dispatcher.
  90. type Dispatcher: Dispatch<Runtime = Self>;
  91. /// Creates a new webview runtime.
  92. fn new() -> crate::Result<Self>;
  93. /// Create a new webview window.
  94. fn create_window<P: Params<Runtime = Self>>(
  95. &self,
  96. pending: PendingWindow<P>,
  97. ) -> crate::Result<DetachedWindow<P>>;
  98. /// Adds the icon to the system tray with the specified menu items.
  99. #[cfg(feature = "system-tray")]
  100. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  101. fn system_tray<I: MenuId>(
  102. &self,
  103. icon: Icon,
  104. menu: Vec<menu::SystemTrayMenuItem<I>>,
  105. ) -> crate::Result<()>;
  106. /// Registers a system tray event handler.
  107. #[cfg(feature = "system-tray")]
  108. #[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
  109. fn on_system_tray_event<F: Fn(&SystemTrayEvent) + Send + 'static>(&mut self, f: F) -> Uuid;
  110. /// Run the webview runtime.
  111. fn run(self);
  112. }
  113. /// Webview dispatcher. A thread-safe handle to the webview API.
  114. pub trait Dispatch: Clone + Send + Sized + 'static {
  115. /// The runtime this [`Dispatch`] runs under.
  116. type Runtime: Runtime;
  117. /// The winoow builder type.
  118. type WindowBuilder: WindowBuilder + Clone;
  119. /// Run a task on the main thread.
  120. fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()>;
  121. /// Registers a window event handler.
  122. fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) -> Uuid;
  123. /// Registers a window event handler.
  124. #[cfg(feature = "menu")]
  125. #[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
  126. fn on_menu_event<F: Fn(&window::MenuEvent) + Send + 'static>(&self, f: F) -> Uuid;
  127. // GETTERS
  128. /// Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
  129. fn scale_factor(&self) -> crate::Result<f64>;
  130. /// 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.
  131. fn inner_position(&self) -> crate::Result<PhysicalPosition<i32>>;
  132. /// Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
  133. fn outer_position(&self) -> crate::Result<PhysicalPosition<i32>>;
  134. /// Returns the physical size of the window's client area.
  135. ///
  136. /// The client area is the content of the window, excluding the title bar and borders.
  137. fn inner_size(&self) -> crate::Result<PhysicalSize<u32>>;
  138. /// Returns the physical size of the entire window.
  139. ///
  140. /// These dimensions include the title bar and borders. If you don't want that (and you usually don't), use inner_size instead.
  141. fn outer_size(&self) -> crate::Result<PhysicalSize<u32>>;
  142. /// Gets the window's current fullscreen state.
  143. fn is_fullscreen(&self) -> crate::Result<bool>;
  144. /// Gets the window's current maximized state.
  145. fn is_maximized(&self) -> crate::Result<bool>;
  146. /// Returns the monitor on which the window currently resides.
  147. ///
  148. /// Returns None if current monitor can't be detected.
  149. fn current_monitor(&self) -> crate::Result<Option<Monitor>>;
  150. /// Returns the primary monitor of the system.
  151. ///
  152. /// Returns None if it can't identify any monitor as a primary one.
  153. fn primary_monitor(&self) -> crate::Result<Option<Monitor>>;
  154. /// Returns the list of all the monitors available on the system.
  155. fn available_monitors(&self) -> crate::Result<Vec<Monitor>>;
  156. // SETTERS
  157. /// Opens the dialog to prints the contents of the webview.
  158. fn print(&self) -> crate::Result<()>;
  159. /// Create a new webview window.
  160. fn create_window<P: Params<Runtime = Self::Runtime>>(
  161. &mut self,
  162. pending: PendingWindow<P>,
  163. ) -> crate::Result<DetachedWindow<P>>;
  164. /// Updates the window resizable flag.
  165. fn set_resizable(&self, resizable: bool) -> crate::Result<()>;
  166. /// Updates the window title.
  167. fn set_title<S: Into<String>>(&self, title: S) -> crate::Result<()>;
  168. /// Maximizes the window.
  169. fn maximize(&self) -> crate::Result<()>;
  170. /// Unmaximizes the window.
  171. fn unmaximize(&self) -> crate::Result<()>;
  172. /// Minimizes the window.
  173. fn minimize(&self) -> crate::Result<()>;
  174. /// Unminimizes the window.
  175. fn unminimize(&self) -> crate::Result<()>;
  176. /// Shows the window.
  177. fn show(&self) -> crate::Result<()>;
  178. /// Hides the window.
  179. fn hide(&self) -> crate::Result<()>;
  180. /// Closes the window.
  181. fn close(&self) -> crate::Result<()>;
  182. /// Updates the hasDecorations flag.
  183. fn set_decorations(&self, decorations: bool) -> crate::Result<()>;
  184. /// Updates the window alwaysOnTop flag.
  185. fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()>;
  186. /// Resizes the window.
  187. fn set_size(&self, size: Size) -> crate::Result<()>;
  188. /// Updates the window min size.
  189. fn set_min_size(&self, size: Option<Size>) -> crate::Result<()>;
  190. /// Updates the window max size.
  191. fn set_max_size(&self, size: Option<Size>) -> crate::Result<()>;
  192. /// Updates the window position.
  193. fn set_position(&self, position: Position) -> crate::Result<()>;
  194. /// Updates the window fullscreen state.
  195. fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()>;
  196. /// Updates the window icon.
  197. fn set_icon(&self, icon: Icon) -> crate::Result<()>;
  198. /// Starts dragging the window.
  199. fn start_dragging(&self) -> crate::Result<()>;
  200. /// Executes javascript on the window this [`Dispatch`] represents.
  201. fn eval_script<S: Into<String>>(&self, script: S) -> crate::Result<()>;
  202. }