Browse Source

feat(core): allow disabling file drop handler, closes #2014 (#2030)

Lucas Fernandes Nogueira 4 years ago
parent
commit
9cd10df4d5

+ 5 - 0
.changes/drag-and-drop-config.md

@@ -0,0 +1,5 @@
+---
+"tauri-utils": patch
+---
+
+Adds `file_drop_enabled` flag on `WindowConfig`.

+ 5 - 0
.changes/fix-drag-and-drop.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Allow disabling the webview file drop handler (required to use drag and drop on the frontend on Windows) using the `tauri.conf.json > tauri > windows > fileDropEnabled` flag or the `WebviewAttributes#disable_file_drop_handler` method.

+ 8 - 0
core/tauri-runtime/src/webview.rs

@@ -27,6 +27,7 @@ pub struct WebviewAttributes {
   pub initialization_scripts: Vec<String>,
   pub data_directory: Option<PathBuf>,
   pub uri_scheme_protocols: HashMap<String, Box<UriSchemeProtocol>>,
+  pub file_drop_handler_enabled: bool,
 }
 
 impl WebviewAttributes {
@@ -37,6 +38,7 @@ impl WebviewAttributes {
       initialization_scripts: Vec::new(),
       data_directory: None,
       uri_scheme_protocols: Default::default(),
+      file_drop_handler_enabled: true,
     }
   }
 
@@ -80,6 +82,12 @@ impl WebviewAttributes {
       .insert(uri_scheme, Box::new(move |data| (protocol)(data)));
     self
   }
+
+  /// Disables the file drop handler. This is required to use drag and drop APIs on the front end on Windows.
+  pub fn disable_file_drop_handler(mut self) -> Self {
+    self.file_drop_handler_enabled = false;
+    self
+  }
 }
 
 /// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).

+ 13 - 0
core/tauri-utils/src/config.rs

@@ -42,6 +42,11 @@ pub struct WindowConfig {
   /// The window webview URL.
   #[serde(default)]
   pub url: WindowUrl,
+  /// Whether the file drop is enabled or not on the webview. By default it is enabled.
+  ///
+  /// Disabling it is required to use drag and drop on the frontend on Windows.
+  #[serde(default = "default_file_drop_enabled")]
+  pub file_drop_enabled: bool,
   /// Center the window.
   #[serde(default)]
   pub center: bool,
@@ -123,11 +128,16 @@ fn default_title() -> String {
   "Tauri App".to_string()
 }
 
+fn default_file_drop_enabled() -> bool {
+  true
+}
+
 impl Default for WindowConfig {
   fn default() -> Self {
     Self {
       label: default_window_label(),
       url: WindowUrl::default(),
+      file_drop_enabled: default_file_drop_enabled(),
       center: false,
       x: None,
       y: None,
@@ -653,6 +663,7 @@ mod build {
     fn to_tokens(&self, tokens: &mut TokenStream) {
       let label = str_lit(&self.label);
       let url = &self.url;
+      let file_drop_enabled = self.file_drop_enabled;
       let center = self.center;
       let x = opt_lit(self.x.as_ref());
       let y = opt_lit(self.y.as_ref());
@@ -678,6 +689,7 @@ mod build {
         WindowConfig,
         label,
         url,
+        file_drop_enabled,
         center,
         x,
         y,
@@ -943,6 +955,7 @@ mod test {
       windows: vec![WindowConfig {
         label: "main".to_string(),
         url: WindowUrl::default(),
+        file_drop_enabled: true,
         center: false,
         x: None,
         y: None,

+ 7 - 1
core/tauri/src/app.rs

@@ -680,14 +680,20 @@ where
     // set up all the windows defined in the config
     for config in manager.config().tauri.windows.clone() {
       let url = config.url.clone();
+      let file_drop_enabled = config.file_drop_enabled;
       let label = config
         .label
         .parse()
         .unwrap_or_else(|_| panic!("bad label found in config: {}", config.label));
 
+      let mut webview_attributes = WebviewAttributes::new(url);
+      if !file_drop_enabled {
+        webview_attributes = webview_attributes.disable_file_drop_handler();
+      }
+
       self.pending_windows.push(PendingWindow::with_config(
         config,
-        WebviewAttributes::new(url),
+        webview_attributes,
         label,
       ));
     }

+ 3 - 1
core/tauri/src/manager.rs

@@ -630,7 +630,9 @@ impl<P: Params> WindowManager<P> {
       pending.rpc_handler = Some(self.prepare_rpc_handler());
     }
 
-    pending.file_drop_handler = Some(self.prepare_file_drop());
+    if pending.webview_attributes.file_drop_handler_enabled {
+      pending.file_drop_handler = Some(self.prepare_file_drop());
+    }
     pending.url = url;
 
     Ok(pending)

+ 1 - 0
docs/api/config.md

@@ -252,6 +252,7 @@ It's composed of the following properties:
       <Properties anchorRoot="tauri.windows" rows={[
         { property: "label", type: "string", description: `Window id to reference on the codebase.` },
         { property: "url", type: "string", description: `URL to load on the webview.` },
+        { property: "fileDropEnabled", type: "boolean", description: `Whether the file drop handler is enabled or not on the webview. Disabling it is required to use drag and drop on the frontend on Windows.` },
         { property: "center", type: "boolean", description: `Show window in the center of the screen.` },
         { property: "x", type: "number", description: `The horizontal position of the window's top left corner.` },
         { property: "y", type: "number", description: `The vertical position of the window's top left corner.` },

+ 9 - 0
tooling/cli.rs/config_definition.rs

@@ -263,6 +263,11 @@ pub struct WindowConfig {
   pub label: Option<String>,
   /// The window webview URL.
   pub url: Option<String>,
+  /// Whether the file drop is enabled or not on the webview. By default it is enabled.
+  ///
+  /// Disabling it is required to use drag and drop on the frontend on Windows.
+  #[serde(default = "default_file_drop_enabled")]
+  pub file_drop_enabled: bool,
   /// The horizontal position of the window's top left corner
   pub x: Option<f64>,
   /// The vertical position of the window's top left corner
@@ -312,6 +317,10 @@ fn default_decorations() -> bool {
   true
 }
 
+fn default_file_drop_enabled() -> bool {
+  true
+}
+
 #[skip_serializing_none]
 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
 #[serde(rename_all = "camelCase", deny_unknown_fields)]

+ 5 - 0
tooling/cli.rs/schema.json

@@ -1104,6 +1104,11 @@
           "default": true,
           "type": "boolean"
         },
+        "fileDropEnabled": {
+          "description": "Whether the file drop is enabled or not on the webview. By default it is enabled.\n\nDisabling it is required to use drag and drop on the frontend on Windows.",
+          "default": true,
+          "type": "boolean"
+        },
         "fullscreen": {
           "description": "Whether the window starts as fullscreen or not.",
           "default": false,