Skip to content

Commit 9cd10df

Browse files
authored
feat(core): allow disabling file drop handler, closes #2014 (#2030)
1 parent b769c7f commit 9cd10df

9 files changed

Lines changed: 56 additions & 2 deletions

File tree

.changes/drag-and-drop-config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": patch
3+
---
4+
5+
Adds `file_drop_enabled` flag on `WindowConfig`.

.changes/fix-drag-and-drop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
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.

core/tauri-runtime/src/webview.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct WebviewAttributes {
2727
pub initialization_scripts: Vec<String>,
2828
pub data_directory: Option<PathBuf>,
2929
pub uri_scheme_protocols: HashMap<String, Box<UriSchemeProtocol>>,
30+
pub file_drop_handler_enabled: bool,
3031
}
3132

3233
impl WebviewAttributes {
@@ -37,6 +38,7 @@ impl WebviewAttributes {
3738
initialization_scripts: Vec::new(),
3839
data_directory: None,
3940
uri_scheme_protocols: Default::default(),
41+
file_drop_handler_enabled: true,
4042
}
4143
}
4244

@@ -80,6 +82,12 @@ impl WebviewAttributes {
8082
.insert(uri_scheme, Box::new(move |data| (protocol)(data)));
8183
self
8284
}
85+
86+
/// Disables the file drop handler. This is required to use drag and drop APIs on the front end on Windows.
87+
pub fn disable_file_drop_handler(mut self) -> Self {
88+
self.file_drop_handler_enabled = false;
89+
self
90+
}
8391
}
8492

8593
/// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).

core/tauri-utils/src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ pub struct WindowConfig {
4242
/// The window webview URL.
4343
#[serde(default)]
4444
pub url: WindowUrl,
45+
/// Whether the file drop is enabled or not on the webview. By default it is enabled.
46+
///
47+
/// Disabling it is required to use drag and drop on the frontend on Windows.
48+
#[serde(default = "default_file_drop_enabled")]
49+
pub file_drop_enabled: bool,
4550
/// Center the window.
4651
#[serde(default)]
4752
pub center: bool,
@@ -123,11 +128,16 @@ fn default_title() -> String {
123128
"Tauri App".to_string()
124129
}
125130

131+
fn default_file_drop_enabled() -> bool {
132+
true
133+
}
134+
126135
impl Default for WindowConfig {
127136
fn default() -> Self {
128137
Self {
129138
label: default_window_label(),
130139
url: WindowUrl::default(),
140+
file_drop_enabled: default_file_drop_enabled(),
131141
center: false,
132142
x: None,
133143
y: None,
@@ -653,6 +663,7 @@ mod build {
653663
fn to_tokens(&self, tokens: &mut TokenStream) {
654664
let label = str_lit(&self.label);
655665
let url = &self.url;
666+
let file_drop_enabled = self.file_drop_enabled;
656667
let center = self.center;
657668
let x = opt_lit(self.x.as_ref());
658669
let y = opt_lit(self.y.as_ref());
@@ -678,6 +689,7 @@ mod build {
678689
WindowConfig,
679690
label,
680691
url,
692+
file_drop_enabled,
681693
center,
682694
x,
683695
y,
@@ -943,6 +955,7 @@ mod test {
943955
windows: vec![WindowConfig {
944956
label: "main".to_string(),
945957
url: WindowUrl::default(),
958+
file_drop_enabled: true,
946959
center: false,
947960
x: None,
948961
y: None,

core/tauri/src/app.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,20 @@ where
680680
// set up all the windows defined in the config
681681
for config in manager.config().tauri.windows.clone() {
682682
let url = config.url.clone();
683+
let file_drop_enabled = config.file_drop_enabled;
683684
let label = config
684685
.label
685686
.parse()
686687
.unwrap_or_else(|_| panic!("bad label found in config: {}", config.label));
687688

689+
let mut webview_attributes = WebviewAttributes::new(url);
690+
if !file_drop_enabled {
691+
webview_attributes = webview_attributes.disable_file_drop_handler();
692+
}
693+
688694
self.pending_windows.push(PendingWindow::with_config(
689695
config,
690-
WebviewAttributes::new(url),
696+
webview_attributes,
691697
label,
692698
));
693699
}

core/tauri/src/manager.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,9 @@ impl<P: Params> WindowManager<P> {
630630
pending.rpc_handler = Some(self.prepare_rpc_handler());
631631
}
632632

633-
pending.file_drop_handler = Some(self.prepare_file_drop());
633+
if pending.webview_attributes.file_drop_handler_enabled {
634+
pending.file_drop_handler = Some(self.prepare_file_drop());
635+
}
634636
pending.url = url;
635637

636638
Ok(pending)

docs/api/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ It's composed of the following properties:
252252
<Properties anchorRoot="tauri.windows" rows={[
253253
{ property: "label", type: "string", description: `Window id to reference on the codebase.` },
254254
{ property: "url", type: "string", description: `URL to load on the webview.` },
255+
{ 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.` },
255256
{ property: "center", type: "boolean", description: `Show window in the center of the screen.` },
256257
{ property: "x", type: "number", description: `The horizontal position of the window's top left corner.` },
257258
{ property: "y", type: "number", description: `The vertical position of the window's top left corner.` },

tooling/cli.rs/config_definition.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ pub struct WindowConfig {
263263
pub label: Option<String>,
264264
/// The window webview URL.
265265
pub url: Option<String>,
266+
/// Whether the file drop is enabled or not on the webview. By default it is enabled.
267+
///
268+
/// Disabling it is required to use drag and drop on the frontend on Windows.
269+
#[serde(default = "default_file_drop_enabled")]
270+
pub file_drop_enabled: bool,
266271
/// The horizontal position of the window's top left corner
267272
pub x: Option<f64>,
268273
/// The vertical position of the window's top left corner
@@ -312,6 +317,10 @@ fn default_decorations() -> bool {
312317
true
313318
}
314319

320+
fn default_file_drop_enabled() -> bool {
321+
true
322+
}
323+
315324
#[skip_serializing_none]
316325
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
317326
#[serde(rename_all = "camelCase", deny_unknown_fields)]

tooling/cli.rs/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@
11041104
"default": true,
11051105
"type": "boolean"
11061106
},
1107+
"fileDropEnabled": {
1108+
"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.",
1109+
"default": true,
1110+
"type": "boolean"
1111+
},
11071112
"fullscreen": {
11081113
"description": "Whether the window starts as fullscreen or not.",
11091114
"default": false,

0 commit comments

Comments
 (0)