webview.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Items specific to the [`Runtime`](crate::Runtime)'s webview.
  5. use crate::{menu::Menu, window::DetachedWindow, Icon};
  6. #[cfg(target_os = "macos")]
  7. use tauri_utils::TitleBarStyle;
  8. use tauri_utils::{
  9. config::{WindowConfig, WindowEffectsConfig, WindowUrl},
  10. Theme,
  11. };
  12. #[cfg(windows)]
  13. use windows::Win32::Foundation::HWND;
  14. use std::{fmt, path::PathBuf};
  15. /// The attributes used to create an webview.
  16. #[derive(Debug, Clone)]
  17. pub struct WebviewAttributes {
  18. pub url: WindowUrl,
  19. pub user_agent: Option<String>,
  20. pub initialization_scripts: Vec<String>,
  21. pub data_directory: Option<PathBuf>,
  22. pub file_drop_handler_enabled: bool,
  23. pub clipboard: bool,
  24. pub accept_first_mouse: bool,
  25. pub additional_browser_args: Option<String>,
  26. pub window_effects: Option<WindowEffectsConfig>,
  27. pub incognito: bool,
  28. }
  29. impl From<&WindowConfig> for WebviewAttributes {
  30. fn from(config: &WindowConfig) -> Self {
  31. let mut builder = Self::new(config.url.clone());
  32. builder = builder.incognito(config.incognito);
  33. builder = builder.accept_first_mouse(config.accept_first_mouse);
  34. if !config.file_drop_enabled {
  35. builder = builder.disable_file_drop_handler();
  36. }
  37. if let Some(user_agent) = &config.user_agent {
  38. builder = builder.user_agent(user_agent);
  39. }
  40. if let Some(additional_browser_args) = &config.additional_browser_args {
  41. builder = builder.additional_browser_args(additional_browser_args);
  42. }
  43. if let Some(effects) = &config.window_effects {
  44. builder = builder.window_effects(effects.clone());
  45. }
  46. builder
  47. }
  48. }
  49. impl WebviewAttributes {
  50. /// Initializes the default attributes for a webview.
  51. pub fn new(url: WindowUrl) -> Self {
  52. Self {
  53. url,
  54. user_agent: None,
  55. initialization_scripts: Vec::new(),
  56. data_directory: None,
  57. file_drop_handler_enabled: true,
  58. clipboard: false,
  59. accept_first_mouse: false,
  60. additional_browser_args: None,
  61. window_effects: None,
  62. incognito: false,
  63. }
  64. }
  65. /// Sets the user agent
  66. #[must_use]
  67. pub fn user_agent(mut self, user_agent: &str) -> Self {
  68. self.user_agent = Some(user_agent.to_string());
  69. self
  70. }
  71. /// Sets the init script.
  72. #[must_use]
  73. pub fn initialization_script(mut self, script: &str) -> Self {
  74. self.initialization_scripts.push(script.to_string());
  75. self
  76. }
  77. /// Data directory for the webview.
  78. #[must_use]
  79. pub fn data_directory(mut self, data_directory: PathBuf) -> Self {
  80. self.data_directory.replace(data_directory);
  81. self
  82. }
  83. /// Disables the file drop handler. This is required to use drag and drop APIs on the front end on Windows.
  84. #[must_use]
  85. pub fn disable_file_drop_handler(mut self) -> Self {
  86. self.file_drop_handler_enabled = false;
  87. self
  88. }
  89. /// Enables clipboard access for the page rendered on **Linux** and **Windows**.
  90. ///
  91. /// **macOS** doesn't provide such method and is always enabled by default,
  92. /// but you still need to add menu item accelerators to use shortcuts.
  93. #[must_use]
  94. pub fn enable_clipboard_access(mut self) -> Self {
  95. self.clipboard = true;
  96. self
  97. }
  98. /// Sets whether clicking an inactive window also clicks through to the webview.
  99. #[must_use]
  100. pub fn accept_first_mouse(mut self, accept: bool) -> Self {
  101. self.accept_first_mouse = accept;
  102. self
  103. }
  104. /// Sets additional browser arguments. **Windows Only**
  105. #[must_use]
  106. pub fn additional_browser_args(mut self, additional_args: &str) -> Self {
  107. self.additional_browser_args = Some(additional_args.to_string());
  108. self
  109. }
  110. /// Sets window effects
  111. #[must_use]
  112. pub fn window_effects(mut self, effects: WindowEffectsConfig) -> Self {
  113. self.window_effects = Some(effects);
  114. self
  115. }
  116. /// Enable or disable incognito mode for the WebView.
  117. #[must_use]
  118. pub fn incognito(mut self, incognito: bool) -> Self {
  119. self.incognito = incognito;
  120. self
  121. }
  122. }
  123. /// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
  124. ///
  125. /// This trait is separate from [`WindowBuilder`] to prevent "accidental" implementation.
  126. pub trait WindowBuilderBase: fmt::Debug + Clone + Sized {}
  127. /// A builder for all attributes related to a single webview.
  128. ///
  129. /// This trait is only meant to be implemented by a custom [`Runtime`](crate::Runtime)
  130. /// and not by applications.
  131. pub trait WindowBuilder: WindowBuilderBase {
  132. /// Initializes a new window attributes builder.
  133. fn new() -> Self;
  134. /// Initializes a new webview builder from a [`WindowConfig`]
  135. fn with_config(config: WindowConfig) -> Self;
  136. /// Sets the menu for the window.
  137. #[must_use]
  138. fn menu(self, menu: Menu) -> Self;
  139. /// Show window in the center of the screen.
  140. #[must_use]
  141. fn center(self) -> Self;
  142. /// The initial position of the window's.
  143. #[must_use]
  144. fn position(self, x: f64, y: f64) -> Self;
  145. /// Window size.
  146. #[must_use]
  147. fn inner_size(self, width: f64, height: f64) -> Self;
  148. /// Window min inner size.
  149. #[must_use]
  150. fn min_inner_size(self, min_width: f64, min_height: f64) -> Self;
  151. /// Window max inner size.
  152. #[must_use]
  153. fn max_inner_size(self, max_width: f64, max_height: f64) -> Self;
  154. /// Whether the window is resizable or not.
  155. /// When resizable is set to false, native window's maximize button is automatically disabled.
  156. #[must_use]
  157. fn resizable(self, resizable: bool) -> Self;
  158. /// Whether the window's native maximize button is enabled or not.
  159. /// If resizable is set to false, this setting is ignored.
  160. ///
  161. /// ## Platform-specific
  162. ///
  163. /// - **macOS:** Disables the "zoom" button in the window titlebar, which is also used to enter fullscreen mode.
  164. /// - **Linux / iOS / Android:** Unsupported.
  165. #[must_use]
  166. fn maximizable(self, maximizable: bool) -> Self;
  167. /// Whether the window's native minimize button is enabled or not.
  168. ///
  169. /// ## Platform-specific
  170. ///
  171. /// - **Linux / iOS / Android:** Unsupported.
  172. #[must_use]
  173. fn minimizable(self, minimizable: bool) -> Self;
  174. /// Whether the window's native close button is enabled or not.
  175. ///
  176. /// ## Platform-specific
  177. ///
  178. /// - **Linux:** "GTK+ will do its best to convince the window manager not to show a close button.
  179. /// Depending on the system, this function may not have any effect when called on a window that is already visible"
  180. /// - **iOS / Android:** Unsupported.
  181. #[must_use]
  182. fn closable(self, closable: bool) -> Self;
  183. /// The title of the window in the title bar.
  184. #[must_use]
  185. fn title<S: Into<String>>(self, title: S) -> Self;
  186. /// Whether to start the window in fullscreen or not.
  187. #[must_use]
  188. fn fullscreen(self, fullscreen: bool) -> Self;
  189. /// Whether the window will be initially focused or not.
  190. #[must_use]
  191. fn focused(self, focused: bool) -> Self;
  192. /// Whether the window should be maximized upon creation.
  193. #[must_use]
  194. fn maximized(self, maximized: bool) -> Self;
  195. /// Whether the window should be immediately visible upon creation.
  196. #[must_use]
  197. fn visible(self, visible: bool) -> Self;
  198. /// Whether the window should be transparent. If this is true, writing colors
  199. /// with alpha values different than `1.0` will produce a transparent window.
  200. #[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
  201. #[cfg_attr(
  202. doc_cfg,
  203. doc(cfg(any(not(target_os = "macos"), feature = "macos-private-api")))
  204. )]
  205. #[must_use]
  206. fn transparent(self, transparent: bool) -> Self;
  207. /// Whether the window should have borders and bars.
  208. #[must_use]
  209. fn decorations(self, decorations: bool) -> Self;
  210. /// Whether the window should always be on top of other windows.
  211. #[must_use]
  212. fn always_on_top(self, always_on_top: bool) -> Self;
  213. /// Prevents the window contents from being captured by other apps.
  214. #[must_use]
  215. fn content_protected(self, protected: bool) -> Self;
  216. /// Sets the window icon.
  217. fn icon(self, icon: Icon) -> crate::Result<Self>;
  218. /// Sets whether or not the window icon should be added to the taskbar.
  219. #[must_use]
  220. fn skip_taskbar(self, skip: bool) -> Self;
  221. /// Sets whether or not the window has shadow.
  222. ///
  223. /// ## Platform-specific
  224. ///
  225. /// - **Windows:**
  226. /// - `false` has no effect on decorated window, shadows are always ON.
  227. /// - `true` will make ndecorated window have a 1px white border,
  228. /// and on Windows 11, it will have a rounded corners.
  229. /// - **Linux:** Unsupported.
  230. #[must_use]
  231. fn shadow(self, enable: bool) -> Self;
  232. /// Sets a parent to the window to be created.
  233. ///
  234. /// A child window has the WS_CHILD style and is confined to the client area of its parent window.
  235. ///
  236. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#child-windows>
  237. #[cfg(windows)]
  238. #[must_use]
  239. fn parent_window(self, parent: HWND) -> Self;
  240. /// Sets a parent to the window to be created.
  241. ///
  242. /// A child window has the WS_CHILD style and is confined to the client area of its parent window.
  243. ///
  244. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#child-windows>
  245. #[cfg(target_os = "macos")]
  246. #[must_use]
  247. fn parent_window(self, parent: *mut std::ffi::c_void) -> Self;
  248. /// Set an owner to the window to be created.
  249. ///
  250. /// From MSDN:
  251. /// - An owned window is always above its owner in the z-order.
  252. /// - The system automatically destroys an owned window when its owner is destroyed.
  253. /// - An owned window is hidden when its owner is minimized.
  254. ///
  255. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
  256. #[cfg(windows)]
  257. #[must_use]
  258. fn owner_window(self, owner: HWND) -> Self;
  259. /// Hide the titlebar. Titlebar buttons will still be visible.
  260. #[cfg(target_os = "macos")]
  261. #[must_use]
  262. fn title_bar_style(self, style: TitleBarStyle) -> Self;
  263. /// Hide the window title.
  264. #[cfg(target_os = "macos")]
  265. #[must_use]
  266. fn hidden_title(self, hidden: bool) -> Self;
  267. /// Defines the window [tabbing identifier] for macOS.
  268. ///
  269. /// Windows with matching tabbing identifiers will be grouped together.
  270. /// If the tabbing identifier is not set, automatic tabbing will be disabled.
  271. ///
  272. /// [tabbing identifier]: <https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier>
  273. #[cfg(target_os = "macos")]
  274. #[must_use]
  275. fn tabbing_identifier(self, identifier: &str) -> Self;
  276. /// Forces a theme or uses the system settings if None was provided.
  277. fn theme(self, theme: Option<Theme>) -> Self;
  278. /// Whether the icon was set or not.
  279. fn has_icon(&self) -> bool;
  280. /// Gets the window menu.
  281. fn get_menu(&self) -> Option<&Menu>;
  282. }
  283. /// IPC handler.
  284. pub type WebviewIpcHandler<T, R> = Box<dyn Fn(DetachedWindow<T, R>, String) + Send>;