mod.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. use crate::{
  2. api::{assets::Assets, config::Config},
  3. event::{Event, EventHandler},
  4. runtime::{
  5. webview::{Attributes, AttributesPrivate, Icon, WindowConfig},
  6. window::{DetachedWindow, PendingWindow, Window},
  7. },
  8. };
  9. use serde::Serialize;
  10. use std::convert::TryFrom;
  11. pub(crate) mod app;
  12. pub mod flavor;
  13. pub(crate) mod manager;
  14. pub(crate) mod tag;
  15. #[cfg(feature = "updater")]
  16. pub(crate) mod updater;
  17. pub(crate) mod webview;
  18. pub(crate) mod window;
  19. pub use self::tag::Tag;
  20. use std::collections::HashMap;
  21. /// Important configurable items required by Tauri.
  22. pub struct Context<A: Assets> {
  23. /// The config the application was prepared with.
  24. pub config: Config,
  25. /// The assets to be served directly by Tauri.
  26. pub assets: A,
  27. /// The default window icon Tauri should use when creating windows.
  28. pub default_window_icon: Option<Vec<u8>>,
  29. /// Package information.
  30. pub package_info: tauri_api::PackageInfo,
  31. }
  32. /// The webview runtime interface.
  33. pub trait Runtime: Sized + 'static {
  34. /// The message dispatcher.
  35. type Dispatcher: Dispatch<Runtime = Self>;
  36. /// Creates a new webview runtime.
  37. fn new() -> crate::Result<Self>;
  38. /// Creates a new webview window.
  39. fn create_window<M: Params<Runtime = Self>>(
  40. &mut self,
  41. pending: PendingWindow<M>,
  42. ) -> crate::Result<DetachedWindow<M>>;
  43. /// Run the webview runtime.
  44. fn run(self);
  45. }
  46. /// Webview dispatcher. A thread-safe handle to the webview API.
  47. pub trait Dispatch: Clone + Send + Sized + 'static {
  48. /// The runtime this [`Dispatch`] runs under.
  49. type Runtime: Runtime;
  50. /// Representation of a window icon.
  51. type Icon: TryFrom<Icon, Error = crate::Error>;
  52. /// The webview builder type.
  53. type Attributes: Attributes<Icon = Self::Icon>
  54. + AttributesPrivate
  55. + From<WindowConfig>
  56. + Clone
  57. + Send;
  58. /// Creates a new webview window.
  59. fn create_window<M: Params<Runtime = Self::Runtime>>(
  60. &mut self,
  61. pending: PendingWindow<M>,
  62. ) -> crate::Result<DetachedWindow<M>>;
  63. /// Updates the window resizable flag.
  64. fn set_resizable(&self, resizable: bool) -> crate::Result<()>;
  65. /// Updates the window title.
  66. fn set_title<S: Into<String>>(&self, title: S) -> crate::Result<()>;
  67. /// Maximizes the window.
  68. fn maximize(&self) -> crate::Result<()>;
  69. /// Unmaximizes the window.
  70. fn unmaximize(&self) -> crate::Result<()>;
  71. /// Minimizes the window.
  72. fn minimize(&self) -> crate::Result<()>;
  73. /// Unminimizes the window.
  74. fn unminimize(&self) -> crate::Result<()>;
  75. /// Shows the window.
  76. fn show(&self) -> crate::Result<()>;
  77. /// Hides the window.
  78. fn hide(&self) -> crate::Result<()>;
  79. /// Closes the window.
  80. fn close(&self) -> crate::Result<()>;
  81. /// Updates the hasDecorations flag.
  82. fn set_decorations(&self, decorations: bool) -> crate::Result<()>;
  83. /// Updates the window alwaysOnTop flag.
  84. fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()>;
  85. /// Updates the window width.
  86. fn set_width(&self, width: f64) -> crate::Result<()>;
  87. /// Updates the window height.
  88. fn set_height(&self, height: f64) -> crate::Result<()>;
  89. /// Resizes the window.
  90. fn resize(&self, width: f64, height: f64) -> crate::Result<()>;
  91. /// Updates the window min size.
  92. fn set_min_size(&self, min_width: f64, min_height: f64) -> crate::Result<()>;
  93. /// Updates the window max size.
  94. fn set_max_size(&self, max_width: f64, max_height: f64) -> crate::Result<()>;
  95. /// Updates the X position.
  96. fn set_x(&self, x: f64) -> crate::Result<()>;
  97. /// Updates the Y position.
  98. fn set_y(&self, y: f64) -> crate::Result<()>;
  99. /// Updates the window position.
  100. fn set_position(&self, x: f64, y: f64) -> crate::Result<()>;
  101. /// Updates the window fullscreen state.
  102. fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()>;
  103. /// Updates the window icon.
  104. fn set_icon(&self, icon: Self::Icon) -> crate::Result<()>;
  105. /// Executes javascript on the window this [`Dispatch`] represents.
  106. fn eval_script<S: Into<String>>(&self, script: S) -> crate::Result<()>;
  107. }
  108. /// Prevent implementation details from leaking out of the [`Manager`] and [`Managed`] traits.
  109. pub(crate) mod sealed {
  110. use super::Params;
  111. use crate::{
  112. api::{config::Config, PackageInfo},
  113. event::{Event, EventHandler},
  114. hooks::{InvokeMessage, PageLoadPayload},
  115. runtime::{
  116. window::{DetachedWindow, PendingWindow, Window},
  117. RuntimeOrDispatch,
  118. },
  119. };
  120. use serde::Serialize;
  121. use std::collections::{HashMap, HashSet};
  122. use uuid::Uuid;
  123. /// private manager api
  124. pub trait ParamsPrivate<M: Params>: Clone + Send + Sized + 'static {
  125. /// Pass messages not handled by modules or plugins to the running application
  126. fn run_invoke_handler(&self, message: InvokeMessage<M>);
  127. /// Ran once for every window when the page is loaded.
  128. fn run_on_page_load(&self, window: Window<M>, payload: PageLoadPayload);
  129. /// Pass a message to be handled by a plugin that expects the command.
  130. fn extend_api(&self, command: String, message: InvokeMessage<M>);
  131. /// Initialize all the plugins attached to the [`Manager`].
  132. fn initialize_plugins(&self) -> crate::Result<()>;
  133. /// Prepare a [`PendingWindow`] to be created by the [`Runtime`].
  134. ///
  135. /// The passed labels should represent either all the windows in the manager. If the application
  136. /// has not yet been started, the passed labels should represent all windows that will be
  137. /// created before starting.
  138. fn prepare_window(
  139. &self,
  140. pending: PendingWindow<M>,
  141. labels: &[M::Label],
  142. ) -> crate::Result<PendingWindow<M>>;
  143. /// Attach a detached window to the manager.
  144. fn attach_window(&self, window: DetachedWindow<M>) -> Window<M>;
  145. /// Emit an event to javascript windows that pass the predicate.
  146. fn emit_filter_internal<S: Serialize + Clone, F: Fn(&Window<Self>) -> bool>(
  147. &self,
  148. event: String,
  149. payload: Option<S>,
  150. filter: F,
  151. ) -> crate::Result<()>;
  152. /// Emit an event to javascript windows that pass the predicate.
  153. fn emit_filter<S: Serialize + Clone, F: Fn(&Window<M>) -> bool>(
  154. &self,
  155. event: M::Event,
  156. payload: Option<S>,
  157. predicate: F,
  158. ) -> crate::Result<()>;
  159. /// All current window labels existing.
  160. fn labels(&self) -> HashSet<M::Label>;
  161. /// The configuration the [`Manager`] was built with.
  162. fn config(&self) -> &Config;
  163. /// App package information.
  164. fn package_info(&self) -> &PackageInfo;
  165. /// Remove the specified event handler.
  166. fn unlisten(&self, handler_id: EventHandler);
  167. /// Trigger an event.
  168. fn trigger(&self, event: M::Event, window: Option<M::Label>, data: Option<String>);
  169. /// Set up a listener to an event.
  170. fn listen<F: Fn(Event) + Send + 'static>(
  171. &self,
  172. event: M::Event,
  173. window: Option<M::Label>,
  174. handler: F,
  175. ) -> EventHandler;
  176. /// Set up a listener to and event that is automatically removed after called once.
  177. fn once<F: Fn(Event) + Send + 'static>(
  178. &self,
  179. event: M::Event,
  180. window: Option<M::Label>,
  181. handler: F,
  182. );
  183. fn event_listeners_object_name(&self) -> String;
  184. fn event_queue_object_name(&self) -> String;
  185. fn event_emit_function_name(&self) -> String;
  186. /// Generate a random salt and store it in the manager
  187. fn generate_salt(&self) -> Uuid;
  188. /// Verify that the passed salt is a valid salt in the manager.
  189. fn verify_salt(&self, salt: String) -> bool;
  190. /// Get a single managed window.
  191. fn get_window(&self, label: &M::Label) -> Option<Window<M>>;
  192. /// Get all managed windows.
  193. fn windows(&self) -> HashMap<M::Label, Window<M>>;
  194. }
  195. /// Represents a managed handle to the application runner.
  196. pub trait ManagerPrivate<M: Params> {
  197. /// The manager behind the [`Managed`] item.
  198. fn manager(&self) -> &M;
  199. /// The runtime or runtime dispatcher of the [`Managed`] item.
  200. fn runtime(&mut self) -> RuntimeOrDispatch<'_, M>;
  201. }
  202. }
  203. /// Represents either a [`Runtime`] or its dispatcher.
  204. pub enum RuntimeOrDispatch<'m, M: Params> {
  205. /// Mutable reference to the [`Runtime`].
  206. Runtime(&'m mut M::Runtime),
  207. /// Copy of the [`Runtime`]'s dispatcher.
  208. Dispatch(<M::Runtime as Runtime>::Dispatcher),
  209. }
  210. /// Represents a managed handle to the application runner
  211. pub trait Manager<M: Params>: sealed::ManagerPrivate<M> {
  212. /// The [`Config`] the manager was created with.
  213. fn config(&self) -> &Config {
  214. self.manager().config()
  215. }
  216. /// Emits a event to all windows.
  217. fn emit_all<S: Serialize + Clone>(
  218. &self,
  219. event: M::Event,
  220. payload: Option<S>,
  221. ) -> crate::Result<()> {
  222. self.manager().emit_filter(event, payload, |_| true)
  223. }
  224. /// Emits an event to a window with the specified label.
  225. fn emit_to<S: Serialize + Clone>(
  226. &self,
  227. label: &M::Label,
  228. event: M::Event,
  229. payload: Option<S>,
  230. ) -> crate::Result<()> {
  231. self
  232. .manager()
  233. .emit_filter(event, payload, |w| w.label() == label)
  234. }
  235. /// Creates a new [`Window`] on the [`Runtime`] and attaches it to the [`Manager`].
  236. fn create_window(&mut self, pending: PendingWindow<M>) -> crate::Result<Window<M>> {
  237. let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
  238. let pending = self.manager().prepare_window(pending, &labels)?;
  239. match self.runtime() {
  240. RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
  241. RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending),
  242. }
  243. .map(|window| self.manager().attach_window(window))
  244. }
  245. /// Listen to a global event.
  246. fn listen_global<F>(&self, event: M::Event, handler: F) -> EventHandler
  247. where
  248. F: Fn(Event) + Send + 'static,
  249. {
  250. self.manager().listen(event, None, handler)
  251. }
  252. /// Listen to a global event only once.
  253. fn once_global<F>(&self, event: M::Event, handler: F)
  254. where
  255. F: Fn(Event) + Send + 'static,
  256. {
  257. self.manager().once(event, None, handler)
  258. }
  259. /// Trigger a global event.
  260. fn trigger_global(&self, event: M::Event, data: Option<String>) {
  261. self.manager().trigger(event, None, data)
  262. }
  263. /// Remove an event listener.
  264. fn unlisten(&self, handler_id: EventHandler) {
  265. self.manager().unlisten(handler_id)
  266. }
  267. /// Fetch a single window from the manager.
  268. fn get_window(&self, label: &M::Label) -> Option<Window<M>> {
  269. self.manager().get_window(label)
  270. }
  271. /// Fetch all managed windows.
  272. fn windows(&self) -> HashMap<M::Label, Window<M>> {
  273. self.manager().windows()
  274. }
  275. }
  276. /// Types that the manager needs to have passed in by the application.
  277. pub trait Params: sealed::ParamsPrivate<Self> {
  278. /// The event type used to create and listen to events.
  279. type Event: Tag;
  280. /// The type used to determine the name of windows.
  281. type Label: Tag;
  282. /// Assets that Tauri should serve from itself.
  283. type Assets: Assets;
  284. /// The underlying webview runtime used by the Tauri application.
  285. type Runtime: Runtime;
  286. }