webview.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2019-2021 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::{window::DetachedWindow, Icon};
  6. use crate::menu::Menu;
  7. use serde::Deserialize;
  8. use serde_json::Value as JsonValue;
  9. use tauri_utils::config::{WindowConfig, WindowUrl};
  10. #[cfg(windows)]
  11. use windows::Win32::Foundation::HWND;
  12. use std::{fmt, path::PathBuf};
  13. /// The attributes used to create an webview.
  14. #[derive(Debug)]
  15. pub struct WebviewAttributes {
  16. pub url: WindowUrl,
  17. pub initialization_scripts: Vec<String>,
  18. pub data_directory: Option<PathBuf>,
  19. pub file_drop_handler_enabled: bool,
  20. pub clipboard: bool,
  21. }
  22. impl WebviewAttributes {
  23. /// Initializes the default attributes for a webview.
  24. pub fn new(url: WindowUrl) -> Self {
  25. Self {
  26. url,
  27. initialization_scripts: Vec::new(),
  28. data_directory: None,
  29. file_drop_handler_enabled: true,
  30. clipboard: false,
  31. }
  32. }
  33. /// Sets the init script.
  34. pub fn initialization_script(mut self, script: &str) -> Self {
  35. self.initialization_scripts.push(script.to_string());
  36. self
  37. }
  38. /// Data directory for the webview.
  39. pub fn data_directory(mut self, data_directory: PathBuf) -> Self {
  40. self.data_directory.replace(data_directory);
  41. self
  42. }
  43. /// Disables the file drop handler. This is required to use drag and drop APIs on the front end on Windows.
  44. pub fn disable_file_drop_handler(mut self) -> Self {
  45. self.file_drop_handler_enabled = false;
  46. self
  47. }
  48. /// Enables clipboard access for the page rendered on **Linux** and **Windows**.
  49. ///
  50. /// **macOS** doesn't provide such method and is always enabled by default,
  51. /// but you still need to add menu item accelerators to use shortcuts.
  52. pub fn enable_clipboard_access(mut self) -> Self {
  53. self.clipboard = true;
  54. self
  55. }
  56. }
  57. /// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
  58. ///
  59. /// This trait is separate from [`WindowBuilder`] to prevent "accidental" implementation.
  60. pub trait WindowBuilderBase: fmt::Debug + Sized {}
  61. /// A builder for all attributes related to a single webview.
  62. ///
  63. /// This trait is only meant to be implemented by a custom [`Runtime`](crate::Runtime)
  64. /// and not by applications.
  65. pub trait WindowBuilder: WindowBuilderBase {
  66. /// Initializes a new window attributes builder.
  67. fn new() -> Self;
  68. /// Initializes a new webview builder from a [`WindowConfig`]
  69. fn with_config(config: WindowConfig) -> Self;
  70. /// Sets the menu for the window.
  71. fn menu(self, menu: Menu) -> Self;
  72. /// Show window in the center of the screen.
  73. fn center(self) -> Self;
  74. /// The initial position of the window's.
  75. fn position(self, x: f64, y: f64) -> Self;
  76. /// Window size.
  77. fn inner_size(self, min_width: f64, min_height: f64) -> Self;
  78. /// Window min inner size.
  79. fn min_inner_size(self, min_width: f64, min_height: f64) -> Self;
  80. /// Window max inner size.
  81. fn max_inner_size(self, max_width: f64, max_height: f64) -> Self;
  82. /// Whether the window is resizable or not.
  83. fn resizable(self, resizable: bool) -> Self;
  84. /// The title of the window in the title bar.
  85. fn title<S: Into<String>>(self, title: S) -> Self;
  86. /// Whether to start the window in fullscreen or not.
  87. fn fullscreen(self, fullscreen: bool) -> Self;
  88. /// Whether the window will be initially hidden or focused.
  89. fn focus(self) -> Self;
  90. /// Whether the window should be maximized upon creation.
  91. fn maximized(self, maximized: bool) -> Self;
  92. /// Whether the window should be immediately visible upon creation.
  93. fn visible(self, visible: bool) -> Self;
  94. /// Whether the the window should be transparent. If this is true, writing colors
  95. /// with alpha values different than `1.0` will produce a transparent window.
  96. #[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
  97. #[cfg_attr(
  98. doc_cfg,
  99. doc(cfg(any(not(target_os = "macos"), feature = "macos-private-api")))
  100. )]
  101. fn transparent(self, transparent: bool) -> Self;
  102. /// Whether the window should have borders and bars.
  103. fn decorations(self, decorations: bool) -> Self;
  104. /// Whether the window should always be on top of other windows.
  105. fn always_on_top(self, always_on_top: bool) -> Self;
  106. /// Sets the window icon.
  107. fn icon(self, icon: Icon) -> crate::Result<Self>;
  108. /// Sets whether or not the window icon should be added to the taskbar.
  109. fn skip_taskbar(self, skip: bool) -> Self;
  110. /// Sets a parent to the window to be created.
  111. ///
  112. /// A child window has the WS_CHILD style and is confined to the client area of its parent window.
  113. ///
  114. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#child-windows>
  115. #[cfg(windows)]
  116. fn parent_window(self, parent: HWND) -> Self;
  117. /// Set an owner to the window to be created.
  118. ///
  119. /// From MSDN:
  120. /// - An owned window is always above its owner in the z-order.
  121. /// - The system automatically destroys an owned window when its owner is destroyed.
  122. /// - An owned window is hidden when its owner is minimized.
  123. ///
  124. /// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
  125. #[cfg(windows)]
  126. fn owner_window(self, owner: HWND) -> Self;
  127. /// Whether the icon was set or not.
  128. fn has_icon(&self) -> bool;
  129. /// Gets the window menu.
  130. fn get_menu(&self) -> Option<&Menu>;
  131. }
  132. /// The file drop event payload.
  133. #[derive(Debug, Clone)]
  134. #[non_exhaustive]
  135. pub enum FileDropEvent {
  136. /// The file(s) have been dragged onto the window, but have not been dropped yet.
  137. Hovered(Vec<PathBuf>),
  138. /// The file(s) have been dropped onto the window.
  139. Dropped(Vec<PathBuf>),
  140. /// The file drop was aborted.
  141. Cancelled,
  142. }
  143. /// IPC handler.
  144. pub type WebviewIpcHandler<R> = Box<dyn Fn(DetachedWindow<R>, String) + Send>;
  145. /// File drop handler callback
  146. /// Return `true` in the callback to block the OS' default behavior of handling a file drop.
  147. pub type FileDropHandler<R> = Box<dyn Fn(FileDropEvent, DetachedWindow<R>) -> bool + Send>;
  148. #[derive(Debug, Deserialize)]
  149. pub struct InvokePayload {
  150. pub command: String,
  151. #[serde(rename = "__tauriModule")]
  152. pub tauri_module: Option<String>,
  153. pub callback: String,
  154. pub error: String,
  155. #[serde(rename = "__invokeKey")]
  156. pub key: u32,
  157. #[serde(flatten)]
  158. pub inner: JsonValue,
  159. }