Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X11 - Apply current_filter extension from dbus save picker #15217

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion samples/ControlCatalog/Pages/DialogsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ List<FileDialogFilter> GetFilters()
FileTypeChoices = fileTypes,
SuggestedStartLocation = lastSelectedDirectory,
SuggestedFileName = "FileName",
DefaultExtension = fileTypes?.Any() == true ? "txt" : null,
ShowOverwritePrompt = true
});

Expand Down
36 changes: 35 additions & 1 deletion src/Avalonia.FreeDesktop/DBusSystemDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,46 @@ public override async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(File
objectPath = await _fileChooser.SaveFileAsync(parentWindow, options.Title ?? string.Empty, chooserOptions);
var request = new OrgFreedesktopPortalRequest(_connection, "org.freedesktop.portal.Desktop", objectPath);
var tsc = new TaskCompletionSource<string[]?>();
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<string> filters = new List<string>();
List<string> mimeTypes = new List<string>();
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;
Expand All @@ -113,7 +147,7 @@ public override async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(File
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));
}

Expand Down
Loading