From 77e024ee9763cd572dfd6e2e8de724ecec3bfcb0 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Thu, 4 Apr 2024 01:07:34 +0000 Subject: [PATCH] X11 - Apply current_filter extension from dbus save picker (#15217) * apply current_filter extension from dbus save picker * simplify reading filters --- .../ControlCatalog/Pages/DialogsPage.xaml.cs | 1 - src/Avalonia.FreeDesktop/DBusSystemDialog.cs | 36 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs index 39b00590f3..7cfb036577 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs @@ -247,7 +247,6 @@ namespace ControlCatalog.Pages FileTypeChoices = fileTypes, SuggestedStartLocation = lastSelectedDirectory, SuggestedFileName = "FileName", - DefaultExtension = fileTypes?.Any() == true ? "txt" : null, ShowOverwritePrompt = true }); diff --git a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs index 7e43aaf943..7ee963fc17 100644 --- a/src/Avalonia.FreeDesktop/DBusSystemDialog.cs +++ b/src/Avalonia.FreeDesktop/DBusSystemDialog.cs @@ -98,12 +98,46 @@ namespace Avalonia.FreeDesktop objectPath = await _fileChooser.SaveFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions); var request = new OrgFreedesktopPortalRequest(_connection, "org.freedesktop.portal.Desktop", objectPath); var tsc = new TaskCompletionSource(); + FilePickerFileType? selectedType = null; using var disposable = await request.WatchResponseAsync((e, x) => { if (e is not null) tsc.TrySetException(e); else + { + if(x.results.TryGetValue("current_filter", out var value)) + { + var currentFilter = value.Value as DBusStructItem; + if(currentFilter != null) + { + var name = (currentFilter[0] as DBusStringItem)?.Value.ToString() ?? ""; + selectedType = new FilePickerFileType(name); + if(currentFilter[1] is DBusArrayItem types) + { + List filters = new List(); + List mimeTypes = new List(); + foreach(var t in types) + { + if(t is DBusStructItem filter) + { + if((filter[0] as DBusUInt32Item)?.Value == 1) + { + mimeTypes.Add((filter[1] as DBusStringItem)?.Value.ToString() ?? ""); + } + else + { + filters.Add((filter[1] as DBusStringItem)?.Value.ToString() ?? ""); + } + } + } + + selectedType.Patterns = filters; + selectedType.MimeTypes = mimeTypes; + } + } + } tsc.TrySetResult((x.results["uris"].Value as DBusArrayItem)?.Select(static y => (y as DBusStringItem)!.Value).ToArray()); + } }); var uris = await tsc.Task; @@ -113,7 +147,7 @@ namespace Avalonia.FreeDesktop return null; // WSL2 freedesktop automatically adds extension from selected file type, but we can't pass "default ext". So apply it manually. - path = StorageProviderHelpers.NameWithExtension(path, options.DefaultExtension, null); + path = StorageProviderHelpers.NameWithExtension(path, options.DefaultExtension, selectedType); return new BclStorageFile(new FileInfo(path)); }