Skip to content

Commit 6ed4b5b

Browse files
committed
feat: dialog add destroy_path method
1 parent 67bb7f4 commit 6ed4b5b

File tree

11 files changed

+162
-14
lines changed

11 files changed

+162
-14
lines changed

plugins/dialog/build.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
const COMMANDS: &[&str] = &["open", "save", "message", "ask", "confirm"];
5+
const COMMANDS: &[&str] = &[
6+
"open",
7+
"save",
8+
"destroy_path",
9+
"message",
10+
"ask",
11+
"confirm",
12+
];
613

714
fn main() {
815
let result = tauri_plugin::Builder::new(COMMANDS)

plugins/dialog/guest-js/index.ts

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@
44

55
import { invoke } from '@tauri-apps/api/core'
66

7+
class Path {
8+
public path: string
9+
constructor(path: string) {
10+
this.path = path
11+
}
12+
13+
destroy() {
14+
return invoke('plugin:dialog|destroy_path', { path: this.path })
15+
}
16+
17+
toPath() {
18+
return this.path
19+
}
20+
21+
toString() {
22+
return this.toPath()
23+
}
24+
25+
toJSON() {
26+
return {
27+
path: this.path
28+
}
29+
}
30+
}
31+
732
/**
833
* Extension filters for the file dialog.
934
*
@@ -224,13 +249,7 @@ interface ConfirmDialogOptions {
224249
cancelLabel?: string
225250
}
226251

227-
type OpenDialogReturn<T extends OpenDialogOptions> = T['directory'] extends true
228-
? T['multiple'] extends true
229-
? string[] | null
230-
: string | null
231-
: T['multiple'] extends true
232-
? string[] | null
233-
: string | null
252+
type OpenDialogReturn<T extends OpenDialogOptions> = T['multiple'] extends true ? Path[] | null : Path | null
234253

235254
/**
236255
* Open a file/directory selection dialog.
@@ -295,7 +314,17 @@ async function open<T extends OpenDialogOptions>(
295314
Object.freeze(options)
296315
}
297316

298-
return await invoke('plugin:dialog|open', { options })
317+
const path = await invoke<string[] | string | null>('plugin:dialog|open', { options })
318+
319+
if (Array.isArray(path)) {
320+
return path.map((p) => new Path(p))
321+
}
322+
323+
if (!path) {
324+
return null
325+
}
326+
327+
return new Path(path)
299328
}
300329

301330
/**
@@ -326,12 +355,18 @@ async function open<T extends OpenDialogOptions>(
326355
*
327356
* @since 2.0.0
328357
*/
329-
async function save(options: SaveDialogOptions = {}): Promise<string | null> {
358+
async function save(options: SaveDialogOptions = {}): Promise<Path | null> {
330359
if (typeof options === 'object') {
331360
Object.freeze(options)
332361
}
333362

334-
return await invoke('plugin:dialog|save', { options })
363+
const path = await invoke<string | null>('plugin:dialog|save', { options })
364+
365+
if (!path) {
366+
return null
367+
}
368+
369+
return new Path(path)
335370
}
336371

337372
/**

plugins/dialog/ios/Sources/DialogPlugin.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct SaveFileDialogOptions: Decodable {
3939
var defaultPath: String?
4040
}
4141

42+
struct StopAccessingPathOptions: Decodable {
43+
var path: URL
44+
}
45+
4246
class DialogPlugin: Plugin {
4347

4448
var filePickerController: FilePickerController!
@@ -75,6 +79,7 @@ class DialogPlugin: Plugin {
7579
onFilePickerResult = { (event: FilePickerEvent) -> Void in
7680
switch event {
7781
case .selected(let urls):
82+
urls.forEach { $0.startAccessingSecurityScopedResource() }
7883
invoke.resolve(["files": urls])
7984
case .cancelled:
8085
invoke.resolve(["files": nil])
@@ -150,6 +155,8 @@ class DialogPlugin: Plugin {
150155
onFilePickerResult = { (event: FilePickerEvent) -> Void in
151156
switch event {
152157
case .selected(let urls):
158+
Logger.info("picked file to save: \(urls.first!)")
159+
urls.first!.startAccessingSecurityScopedResource()
153160
invoke.resolve(["file": urls.first!])
154161
case .cancelled:
155162
invoke.resolve(["file": nil])
@@ -169,6 +176,12 @@ class DialogPlugin: Plugin {
169176
}
170177
}
171178

179+
@objc public func stopAccessingPath(_ invoke: Invoke) throws {
180+
let args = try invoke.parseArgs(StopAccessingPathOptions.self)
181+
args.path.stopAccessingSecurityScopedResource()
182+
invoke.resolve()
183+
}
184+
172185
private func presentViewController(_ viewControllerToPresent: UIViewController) {
173186
self.manager.viewController?.present(viewControllerToPresent, animated: true, completion: nil)
174187
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Automatically generated - DO NOT EDIT!
2+
3+
"$schema" = "../../schemas/schema.json"
4+
5+
[[permission]]
6+
identifier = "allow-destroy-path"
7+
description = "Enables the destroy_path command without any pre-configured scope."
8+
commands.allow = ["destroy_path"]
9+
10+
[[permission]]
11+
identifier = "deny-destroy-path"
12+
description = "Denies the destroy_path command without any pre-configured scope."
13+
commands.deny = ["destroy_path"]

plugins/dialog/permissions/autogenerated/reference.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All dialog types are enabled.
1414
- `allow-message`
1515
- `allow-save`
1616
- `allow-open`
17+
- `allow-destroy-path`
1718

1819
## Permission Table
1920

@@ -79,6 +80,32 @@ Denies the confirm command without any pre-configured scope.
7980
<tr>
8081
<td>
8182

83+
`dialog:allow-destroy-path`
84+
85+
</td>
86+
<td>
87+
88+
Enables the destroy_path command without any pre-configured scope.
89+
90+
</td>
91+
</tr>
92+
93+
<tr>
94+
<td>
95+
96+
`dialog:deny-destroy-path`
97+
98+
</td>
99+
<td>
100+
101+
Denies the destroy_path command without any pre-configured scope.
102+
103+
</td>
104+
</tr>
105+
106+
<tr>
107+
<td>
108+
82109
`dialog:allow-message`
83110

84111
</td>

plugins/dialog/permissions/default.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ permissions = [
1717
"allow-message",
1818
"allow-save",
1919
"allow-open",
20+
"allow-destroy-path"
2021
]

plugins/dialog/permissions/schemas/schema.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,18 @@
318318
"const": "deny-confirm",
319319
"markdownDescription": "Denies the confirm command without any pre-configured scope."
320320
},
321+
{
322+
"description": "Enables the destroy_path command without any pre-configured scope.",
323+
"type": "string",
324+
"const": "allow-destroy-path",
325+
"markdownDescription": "Enables the destroy_path command without any pre-configured scope."
326+
},
327+
{
328+
"description": "Denies the destroy_path command without any pre-configured scope.",
329+
"type": "string",
330+
"const": "deny-destroy-path",
331+
"markdownDescription": "Denies the destroy_path command without any pre-configured scope."
332+
},
321333
{
322334
"description": "Enables the message command without any pre-configured scope.",
323335
"type": "string",
@@ -355,10 +367,10 @@
355367
"markdownDescription": "Denies the save command without any pre-configured scope."
356368
},
357369
{
358-
"description": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-ask`\n- `allow-confirm`\n- `allow-message`\n- `allow-save`\n- `allow-open`",
370+
"description": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-ask`\n- `allow-confirm`\n- `allow-message`\n- `allow-save`\n- `allow-open`\n- `allow-destroy-path`",
359371
"type": "string",
360372
"const": "default",
361-
"markdownDescription": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-ask`\n- `allow-confirm`\n- `allow-message`\n- `allow-save`\n- `allow-open`"
373+
"markdownDescription": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-ask`\n- `allow-confirm`\n- `allow-message`\n- `allow-save`\n- `allow-open`\n- `allow-destroy-path`"
362374
}
363375
]
364376
}

plugins/dialog/src/commands.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ pub(crate) async fn save<R: Runtime>(
241241
Ok(path.map(|p| p.simplified()))
242242
}
243243

244+
#[command]
245+
pub fn destroy_path(_path: String) -> bool {
246+
true
247+
}
248+
244249
fn message_dialog<R: Runtime>(
245250
#[allow(unused_variables)] window: Window<R>,
246251
dialog: State<'_, Dialog<R>>,

plugins/dialog/src/desktop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ pub fn save_file<R: Runtime, F: FnOnce(Option<FilePath>) + Send + 'static>(
211211
});
212212
}
213213

214+
pub fn destroy_path<R: Runtime>(_dialog: FileDialogBuilder<R>, _path: String) -> bool {
215+
true
216+
}
217+
214218
/// Shows a message dialog
215219
pub fn show_message_dialog<R: Runtime, F: FnOnce(MessageDialogResult) + Send + 'static>(
216220
dialog: MessageDialogBuilder<R>,

plugins/dialog/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
180180
.invoke_handler(tauri::generate_handler![
181181
commands::open,
182182
commands::save,
183+
commands::destroy_path,
183184
commands::message,
184185
commands::ask,
185186
commands::confirm
@@ -611,6 +612,10 @@ impl<R: Runtime> FileDialogBuilder<R> {
611612
pub fn save_file<F: FnOnce(Option<FilePath>) + Send + 'static>(self, f: F) {
612613
save_file(self, f)
613614
}
615+
616+
pub fn destroy_path(self, path: String) -> bool {
617+
destroy_path(self, path)
618+
}
614619
}
615620

616621
/// Blocking APIs.
@@ -723,4 +728,8 @@ impl<R: Runtime> FileDialogBuilder<R> {
723728
pub fn blocking_save_file(self) -> Option<FilePath> {
724729
blocking_fn!(self, save_file)
725730
}
731+
732+
pub fn blocking_destroy_path(self, path: String) -> bool {
733+
self.destroy_path(path)
734+
}
726735
}

0 commit comments

Comments
 (0)