Skip to content

Commit c946681

Browse files
committed
feat: Implement FileSavePicker for Gtk
1 parent 0140f0d commit c946681

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

doc/articles/features/windows-storage-pickers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Legend
1212
| Picker | UWP | WebAssembly | Android | iOS | macOS | WPF | GTK |
1313
|----------------|-------|-------------|---------|-------|-------|-----|-----|
1414
| FileOpenPicker | ✔️ | ✔️ (1) | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
15-
| FileSavePicker | ✔️ | ✔️ (1) | ✔️ | ✔️ | ✔️ | ✔️ | |
15+
| FileSavePicker | ✔️ | ✔️ (1) | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
1616
| FolderPicker | ✔️ | ✔️ | ✔️ | 💬 (2)| ✔️ || ✔️ |
1717

1818
*(1) - Multiple implementations supported - see WebAssembly section below*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#nullable enable
2+
3+
using Gtk;
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Uno.UI.Runtime.Skia;
8+
using Windows.Storage;
9+
using Windows.Storage.Pickers;
10+
11+
namespace Uno.Extensions.Storage.Pickers
12+
{
13+
internal class FileSavePickerExtension : IFileSavePickerExtension
14+
{
15+
private readonly FileSavePicker _picker;
16+
17+
public FileSavePickerExtension(FileSavePicker owner)
18+
{
19+
_picker = owner ?? throw new ArgumentNullException(nameof(owner));
20+
}
21+
22+
public async Task<StorageFile?> PickSaveFileAsync(CancellationToken token)
23+
{
24+
string commitText = "Save File";
25+
if (!string.IsNullOrWhiteSpace(_picker.CommitButtonText))
26+
{
27+
commitText = _picker.CommitButtonText;
28+
}
29+
30+
FileChooserDialog dialog = new FileChooserDialog(
31+
"Save File",
32+
GtkHost.Window,
33+
FileChooserAction.Save,
34+
"Cancel", ResponseType.Cancel,
35+
commitText, ResponseType.Accept);
36+
37+
dialog.SelectMultiple = false;
38+
dialog.SetFilename(_picker.SuggestedFileName);
39+
dialog.SetCurrentFolder(PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation));
40+
41+
StorageFile? file = null;
42+
if (dialog.Run() == (int)ResponseType.Accept)
43+
{
44+
file = await StorageFile.GetFileFromPathAsync(dialog.Filename);
45+
}
46+
47+
dialog.Destroy();
48+
return file;
49+
}
50+
}
51+
}

src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void Run()
6464
ApiExtensibility.Register<FileOpenPicker>(typeof(IFileOpenPickerExtension), o => new FileOpenPickerExtension(o));
6565
ApiExtensibility.Register<FolderPicker>(typeof(IFolderPickerExtension), o => new FolderPickerExtension(o));
6666
ApiExtensibility.Register(typeof(IClipboardExtension), o => new ClipboardExtensions(o));
67+
ApiExtensibility.Register<FileSavePicker>(typeof(IFileSavePickerExtension), o => new FileSavePickerExtension(o));
6768

6869
_isDispatcherThread = true;
6970
_window = new Gtk.Window("Uno Host");

0 commit comments

Comments
 (0)