feat(dialog): allow setting `canCreateDirectories` on macOS (#978)

* feat(dialog): allow setting `canCreateDirectories` on macOS

closes #949

* Discard changes to plugins/log/src/api-iife.js

* Discard changes to plugins/store/src/api-iife.js

* Discard changes to plugins/window-state/src/api-iife.js

* Update plugins/dialog/src/commands.rs

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>

* Update plugins/dialog/src/commands.rs

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>

* Update plugins/dialog/src/lib.rs

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>

* Update plugins/dialog/guest-js/index.ts

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>

* Update plugins/dialog/guest-js/index.ts

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>

---------

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
pull/950/head
Amr Bashir 1 year ago committed by GitHub
parent ae56b13a4d
commit aa25c91bb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,6 @@
---
"dialog": "patch"
"dialog-js": "patch"
---
Allow configuring `canCreateDirectories` for open and save dialogs on macOS, if not configured, it will be set to `true` by default.

@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_PLUGIN_DEEPLINK__=function(e){"use strict";function n(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}async function t(e,n={},t){return window.__TAURI_INTERNALS__.invoke(e,n,t)}var r;async function i(e,r,i){const _="string"==typeof i?.target?{kind:"AnyLabel",label:i.target}:i?.target??{kind:"Any"};return t("plugin:event|listen",{event:e,target:_,handler:n(r)}).then((n=>async()=>async function(e,n){await t("plugin:event|unlisten",{event:e,eventId:n})}(e,n)))}async function _(){return await t("plugin:deep-link|get_current")}return"function"==typeof SuppressedError&&SuppressedError,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WEBVIEW_CREATED="tauri://webview-created",e.FILE_DROP="tauri://file-drop",e.FILE_DROP_HOVER="tauri://file-drop-hover",e.FILE_DROP_CANCELLED="tauri://file-drop-cancelled"}(r||(r={})),e.getCurrent=_,e.onOpenUrl=async function(e){const n=await _();return null!=n&&e(n),await i("deep-link://new-url",(n=>e(n.payload)))},e}({});Object.defineProperty(window.__TAURI__,"deepLink",{value:__TAURI_PLUGIN_DEEPLINK__})}
if("__TAURI__"in window){var __TAURI_PLUGIN_DEEPLINK__=function(e){"use strict";function n(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}async function t(e,n={},t){return window.__TAURI_INTERNALS__.invoke(e,n,t)}var r;async function _(e,r,_){const i="string"==typeof _?.target?{kind:"AnyLabel",label:_.target}:_?.target??{kind:"Any"};return t("plugin:event|listen",{event:e,target:i,handler:n(r)}).then((n=>async()=>async function(e,n){await t("plugin:event|unlisten",{event:e,eventId:n})}(e,n)))}async function i(){return await t("plugin:deep-link|get_current")}return"function"==typeof SuppressedError&&SuppressedError,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WEBVIEW_CREATED="tauri://webview-created",e.WEBVIEW_FILE_DROP="tauri://file-drop",e.WEBVIEW_FILE_DROP_HOVER="tauri://file-drop-hover",e.WEBVIEW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled"}(r||(r={})),e.getCurrent=i,e.onOpenUrl=async function(e){const n=await i();return null!=n&&e(n),await _("deep-link://new-url",(n=>e(n.payload)))},e}({});Object.defineProperty(window.__TAURI__,"deepLink",{value:__TAURI_PLUGIN_DEEPLINK__})}

@ -55,6 +55,8 @@ interface OpenDialogOptions {
* Defines whether subdirectories will be allowed on the scope or not.
*/
recursive?: boolean;
/** Whether to allow creating directories in the dialog. Enabled by default. **macOS Only** */
canCreateDirectories?: boolean;
}
/**
@ -73,6 +75,8 @@ interface SaveDialogOptions {
* If it's not an existing directory, the file name will be set to the dialog's file name input and the dialog will be set to the parent folder.
*/
defaultPath?: string;
/** Whether to allow creating directories in the dialog. Enabled by default. **macOS Only** */
canCreateDirectories?: boolean;
}
/**

@ -51,6 +51,8 @@ pub struct OpenDialogOptions {
#[serde(default)]
#[cfg_attr(mobile, allow(dead_code))]
recursive: bool,
/// Whether to allow creating directories in the dialog **macOS Only**
can_create_directories: Option<bool>,
}
/// The options for the save dialog API.
@ -65,6 +67,8 @@ pub struct SaveDialogOptions {
filters: Vec<DialogFilter>,
/// The initial path of the dialog.
default_path: Option<PathBuf>,
/// Whether to allow creating directories in the dialog **macOS Only**
can_create_directories: Option<bool>,
}
fn set_default_path<R: Runtime>(
@ -105,6 +109,9 @@ pub(crate) async fn open<R: Runtime>(
if let Some(default_path) = options.default_path {
dialog_builder = set_default_path(dialog_builder, default_path);
}
if let Some(can) = options.can_create_directories {
dialog_builder = dialog_builder.set_can_create_directories(can);
}
for filter in options.filters {
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
dialog_builder = dialog_builder.add_filter(filter.name, &extensions);
@ -185,6 +192,9 @@ pub(crate) async fn save<R: Runtime>(
if let Some(default_path) = options.default_path {
dialog_builder = set_default_path(dialog_builder, default_path);
}
if let Some(can) = options.can_create_directories {
dialog_builder = dialog_builder.set_can_create_directories(can);
}
for filter in options.filters {
let extensions: Vec<&str> = filter.extensions.iter().map(|s| &**s).collect();
dialog_builder = dialog_builder.add_filter(filter.name, &extensions);

@ -127,6 +127,8 @@ impl<R: Runtime> From<FileDialogBuilder<R>> for FileDialog {
builder = builder.set_parent(&WindowHandle(parent));
}
builder = builder.set_can_create_directories(d.can_create_directories.unwrap_or(true));
builder
}
}

@ -267,6 +267,7 @@ pub struct FileDialogBuilder<R: Runtime> {
pub(crate) starting_directory: Option<PathBuf>,
pub(crate) file_name: Option<String>,
pub(crate) title: Option<String>,
pub(crate) can_create_directories: Option<bool>,
#[cfg(desktop)]
pub(crate) parent: Option<raw_window_handle::RawWindowHandle>,
}
@ -291,6 +292,7 @@ impl<R: Runtime> FileDialogBuilder<R> {
starting_directory: None,
file_name: None,
title: None,
can_create_directories: None,
#[cfg(desktop)]
parent: None,
}
@ -345,6 +347,12 @@ impl<R: Runtime> FileDialogBuilder<R> {
self
}
/// Set whether it should be possible to create new directories in the dialog. Enabled by default. **macOS only**.
pub fn set_can_create_directories(mut self, can: bool) -> Self {
self.can_create_directories.replace(can);
self
}
/// Shows the dialog to select a single file.
/// This is not a blocking operation,
/// and should be used when running on the main thread to avoid deadlocks with the event loop.

Loading…
Cancel
Save