Skip to content

Commit

Permalink
feat(core): allow disabling file drop handler, closes #2014 (#2030)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Jun 21, 2021
1 parent b769c7f commit 9cd10df
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/drag-and-drop-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-utils": patch
---

Adds `file_drop_enabled` flag on `WindowConfig`.
5 changes: 5 additions & 0 deletions .changes/fix-drag-and-drop.md
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions core/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,6 +38,7 @@ impl WebviewAttributes {
initialization_scripts: Vec::new(),
data_directory: None,
uri_scheme_protocols: Default::default(),
file_drop_handler_enabled: true,
}
}

Expand Down Expand Up @@ -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).
Expand Down
13 changes: 13 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand All @@ -678,6 +689,7 @@ mod build {
WindowConfig,
label,
url,
file_drop_enabled,
center,
x,
y,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}
Expand Down
4 changes: 3 additions & 1 deletion core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.` },
Expand Down
9 changes: 9 additions & 0 deletions tooling/cli.rs/config_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions tooling/cli.rs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9cd10df

Please sign in to comment.