Explorar el Código

feat: add `clipboard` flag to `WebviewAttributes` [TRI-032] (#12)

Lucas Fernandes Nogueira hace 3 años
padre
commit
d42ccfb34f

+ 6 - 0
.changes/webview-attributes-clipboard.md

@@ -0,0 +1,6 @@
+---
+"tauri": patch
+"tauri-runtime": patch
+---
+
+Added `clipboard` field on the `WebviewAttributes` struct, which must be set to `true` to enable clipboard access on the webview.

+ 5 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -3149,6 +3149,11 @@ fn create_webview(
       vacant.insert(web_context)
     }
   };
+
+  if webview_attributes.clipboard {
+    webview_builder.webview.clipboard = true;
+  }
+
   let webview = webview_builder
     .with_web_context(web_context)
     .build()

+ 12 - 11
core/tauri-runtime/src/webview.rs

@@ -18,22 +18,13 @@ use windows::Win32::Foundation::HWND;
 use std::{fmt, path::PathBuf};
 
 /// The attributes used to create an webview.
+#[derive(Debug)]
 pub struct WebviewAttributes {
   pub url: WindowUrl,
   pub initialization_scripts: Vec<String>,
   pub data_directory: Option<PathBuf>,
   pub file_drop_handler_enabled: bool,
-}
-
-impl fmt::Debug for WebviewAttributes {
-  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-    f.debug_struct("WebviewAttributes")
-      .field("url", &self.url)
-      .field("initialization_scripts", &self.initialization_scripts)
-      .field("data_directory", &self.data_directory)
-      .field("file_drop_handler_enabled", &self.file_drop_handler_enabled)
-      .finish()
-  }
+  pub clipboard: bool,
 }
 
 impl WebviewAttributes {
@@ -44,6 +35,7 @@ impl WebviewAttributes {
       initialization_scripts: Vec::new(),
       data_directory: None,
       file_drop_handler_enabled: true,
+      clipboard: false,
     }
   }
 
@@ -64,6 +56,15 @@ impl WebviewAttributes {
     self.file_drop_handler_enabled = false;
     self
   }
+
+  /// Enables clipboard access for the page rendered on **Linux** and **Windows**.
+  ///
+  /// **macOS** doesn't provide such method and is always enabled by default,
+  /// but you still need to add menu item accelerators to use shortcuts.
+  pub fn enable_clipboard_access(mut self) -> Self {
+    self.clipboard = true;
+    self
+  }
 }
 
 /// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).