forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initial implementation of FileOpenPicker for Skia.Gtk
Add PickerHelpers to Skia.Gtk Update docs for pickers Use strongly typed extension registration Change test description to include Gtk
- Loading branch information
1 parent
f838557
commit 4cd398a
Showing
5 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Uno.UI.Runtime.Skia.Gtk/Extensions/Storage/Pickers/FileOpenPickerExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#nullable enable | ||
|
||
using Gtk; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Uno.UI.Runtime.Skia; | ||
using Windows.ApplicationModel.Resources; | ||
using Windows.Storage; | ||
using Windows.Storage.Pickers; | ||
|
||
namespace Uno.Extensions.Storage.Pickers | ||
{ | ||
internal class FileOpenPickerExtension : IFileOpenPickerExtension | ||
{ | ||
private readonly FileOpenPicker _picker; | ||
|
||
public FileOpenPickerExtension(FileOpenPicker owner) | ||
{ | ||
_picker = owner ?? throw new ArgumentNullException(nameof(owner)); | ||
} | ||
|
||
public async Task<StorageFile?> PickSingleFileAsync(CancellationToken token) | ||
{ | ||
var files = await OpenPickerAsync(false); | ||
return files.FirstOrDefault(); | ||
} | ||
|
||
public async Task<IReadOnlyList<StorageFile>> PickMultipleFilesAsync(CancellationToken token) | ||
{ | ||
var files = await OpenPickerAsync(true); | ||
return new FilePickerSelectedFilesArray(files); | ||
} | ||
|
||
private async Task<StorageFile[]> OpenPickerAsync(bool multiple) | ||
{ | ||
string commitText = "Open"; | ||
if (!string.IsNullOrWhiteSpace(_picker.CommitButtonText)) | ||
{ | ||
commitText = _picker.CommitButtonText; | ||
} | ||
|
||
FileChooserDialog dialog = new FileChooserDialog( | ||
"Open", | ||
GtkHost.Window, | ||
FileChooserAction.Open, | ||
"Cancel", ResponseType.Cancel, | ||
commitText, ResponseType.Accept); | ||
|
||
dialog.SelectMultiple = multiple; | ||
|
||
if (!_picker.FileTypeFilter.Contains("*")) | ||
{ | ||
FileFilter filter = new FileFilter(); | ||
|
||
foreach (string pattern in _picker.FileTypeFilter) | ||
{ | ||
// Pattern is already validated to start with a period, so prepend star | ||
filter.AddPattern("*" + pattern); | ||
} | ||
|
||
if (_picker.FileTypeFilter.Count > 0) | ||
{ | ||
dialog.AddFilter(filter); | ||
} | ||
} | ||
|
||
dialog.SetCurrentFolder(PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation)); | ||
|
||
var files = new List<StorageFile>(); | ||
if (dialog.Run() == (int)ResponseType.Accept) | ||
{ | ||
foreach (var fileName in dialog.Filenames) | ||
{ | ||
files.Add(await StorageFile.GetFileFromPathAsync(fileName)); | ||
} | ||
} | ||
|
||
dialog.Destroy(); | ||
return files.ToArray(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Uno.UI.Runtime.Skia.Gtk/Extensions/Storage/Pickers/PickerHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#nullable enable | ||
|
||
using Windows.Storage.Pickers; | ||
using static System.Environment; | ||
|
||
namespace Uno.Extensions.Storage.Pickers | ||
{ | ||
internal static class PickerHelpers | ||
{ | ||
public static string GetInitialDirectory(PickerLocationId location) | ||
{ | ||
switch (location) | ||
{ | ||
case PickerLocationId.DocumentsLibrary: | ||
return GetFolderPath(SpecialFolder.MyDocuments); | ||
case PickerLocationId.ComputerFolder: | ||
return @"/"; | ||
case PickerLocationId.Desktop: | ||
return GetFolderPath(SpecialFolder.Desktop); | ||
case PickerLocationId.MusicLibrary: | ||
return GetFolderPath(SpecialFolder.MyMusic); | ||
case PickerLocationId.PicturesLibrary: | ||
return GetFolderPath(SpecialFolder.MyPictures); | ||
case PickerLocationId.VideosLibrary: | ||
return GetFolderPath(SpecialFolder.MyVideos); | ||
default: | ||
return string.Empty; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters