Skip to content

Commit 84f3bbe

Browse files
committed
feat: macOS support for FileOpenPicker
1 parent 67d76a1 commit 84f3bbe

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

src/Uno.UWP/Generated/3.0.0.0/Windows.Storage.Pickers/FileOpenPicker.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#pragma warning disable 114 // new keyword hiding
33
namespace Windows.Storage.Pickers
44
{
5-
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || __MACOS__
5+
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false
66
[global::Uno.NotImplemented]
77
#endif
88
public partial class FileOpenPicker
@@ -93,7 +93,7 @@ public string CommitButtonText
9393
}
9494
}
9595
#endif
96-
#if false || false || NET461 || false || __NETSTD_REFERENCE__ || __MACOS__
96+
#if false || false || NET461 || false || __NETSTD_REFERENCE__ || false
9797
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__NETSTD_REFERENCE__", "__MACOS__")]
9898
public FileOpenPicker()
9999
{
@@ -116,7 +116,7 @@ public void PickMultipleFilesAndContinue()
116116
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Pickers.FileOpenPicker", "void FileOpenPicker.PickMultipleFilesAndContinue()");
117117
}
118118
#endif
119-
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || __MACOS__
119+
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false
120120
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__NETSTD_REFERENCE__", "__MACOS__")]
121121
public global::Windows.Foundation.IAsyncOperation<global::Windows.Storage.StorageFile> PickSingleFileAsync( string pickerOperationId)
122122
{
@@ -132,14 +132,14 @@ public void PickMultipleFilesAndContinue()
132132
// Forced skipping of method Windows.Storage.Pickers.FileOpenPicker.CommitButtonText.get
133133
// Forced skipping of method Windows.Storage.Pickers.FileOpenPicker.CommitButtonText.set
134134
// Forced skipping of method Windows.Storage.Pickers.FileOpenPicker.FileTypeFilter.get
135-
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || __MACOS__
135+
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false
136136
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__NETSTD_REFERENCE__", "__MACOS__")]
137137
public global::Windows.Foundation.IAsyncOperation<global::Windows.Storage.StorageFile> PickSingleFileAsync()
138138
{
139139
throw new global::System.NotImplementedException("The member IAsyncOperation<StorageFile> FileOpenPicker.PickSingleFileAsync() is not implemented in Uno.");
140140
}
141141
#endif
142-
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || __MACOS__
142+
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false
143143
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__NETSTD_REFERENCE__", "__MACOS__")]
144144
public global::Windows.Foundation.IAsyncOperation<global::System.Collections.Generic.IReadOnlyList<global::Windows.Storage.StorageFile>> PickMultipleFilesAsync()
145145
{

src/Uno.UWP/Storage/Pickers/FileOpenPicker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public string CommitButtonText
4747
set => _commitButtonText = value ?? throw new ArgumentNullException(nameof(value));
4848
}
4949

50-
#if __SKIA__ || __WASM__ || __IOS__ || __ANDROID__
50+
#if __SKIA__ || __WASM__ || __IOS__ || __ANDROID__ || __MACOS__
5151
public FileOpenPicker()
5252
{
5353
InitializePlatform();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#nullable enable
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using AppKit;
9+
using Foundation;
10+
11+
namespace Windows.Storage.Pickers
12+
{
13+
public partial class FileOpenPicker
14+
{
15+
private const int ModalResponseOk = 1;
16+
17+
private Task<StorageFile?> PickSingleFileTaskAsync(CancellationToken token)
18+
{
19+
var files = PickFiles(false, token);
20+
return Task.FromResult<StorageFile?>(files.FirstOrDefault());
21+
}
22+
23+
private Task<IReadOnlyList<StorageFile>> PickMultipleFilesTaskAsync(CancellationToken token) =>
24+
Task.FromResult<IReadOnlyList<StorageFile>>(PickFiles(true, token));
25+
26+
private FilePickerSelectedFilesArray PickFiles(bool pickMultiple, CancellationToken token)
27+
{
28+
var openPanel = new NSOpenPanel
29+
{
30+
CanChooseFiles = true,
31+
CanChooseDirectories = false,
32+
AllowsMultipleSelection = pickMultiple,
33+
AllowedFileTypes = FileTypeFilter.ToArray()
34+
};
35+
36+
var result = openPanel.RunModal();
37+
38+
if (result == ModalResponseOk)
39+
{
40+
if (openPanel.Urls != null)
41+
{
42+
var files = openPanel.Urls
43+
.Where(url => url?.Path != null)
44+
.Select(url => StorageFile.GetFileFromPath(url.Path))
45+
.ToArray();
46+
return new FilePickerSelectedFilesArray(files);
47+
}
48+
}
49+
return FilePickerSelectedFilesArray.Empty;
50+
}
51+
}
52+
}

src/Uno.UWP/Storage/Pickers/FileSavePicker.macOS.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Windows.Storage.Pickers
1212
{
1313
public partial class FileSavePicker
1414
{
15+
private const int ModalResponseOk = 1;
16+
1517
private async Task<StorageFile?> PickSaveFileTaskAsync(CancellationToken token)
1618
{
1719
var savePicker = new NSSavePanel();
@@ -21,7 +23,7 @@ public partial class FileSavePicker
2123
{
2224
savePicker.NameFieldStringValue = SuggestedFileName;
2325
}
24-
if (savePicker.RunModal() == 1)
26+
if (savePicker.RunModal() == ModalResponseOk)
2527
{
2628
return await StorageFile.GetFileFromPathAsync(savePicker.Url.Path);
2729
}

0 commit comments

Comments
 (0)