Repository: AvaloniaUI/Avalonia
File: src/Avalonia.FreeDesktop/DBusSystemDialog.cs (C#)

Code before:
                if (e is not null)
                {
                    tsc.TrySetException(e);
                }
                else
                {
                    if (x.Results.TryGetValue("current_filter", out var currentFilter))
                    {
                        var name = currentFilter.GetItem(0).GetString();
                        var patterns = new List<string>();
                        var mimeTypes = new List<string>();
                        var types = currentFilter.GetItem(1).GetArray<VariantValue>();
                        foreach (var t in types)
                        {
                            if (t.GetItem(0).GetUInt32() == 1)
                                mimeTypes.Add(t.GetItem(1).GetString());
                            else
                                patterns.Add(t.GetItem(1).GetString());
                        }
                        
                        // Reuse the file type objects from options
                        // so the consuming code can match exactly the
                        // file type selected instead of spawning one.
                        selectedType = options.FileTypeChoices?.FirstOrDefault(type => type.Name == name ||
                            (type.MimeTypes?.All(y => mimeTypes.Contains(y)) ?? false) ||
                            (type.Patterns?.All(y => patterns.Contains(y)) ?? false));
                    }

                    tsc.TrySetResult(x.Results["uris"].GetArray<string>());
                }
            }).ConfigureAwait(false);

            var uris = await tsc.Task.ConfigureAwait(false);
            var path = uris?.FirstOrDefault() is { } filePath ? new Uri(filePath).LocalPath : null;

            if (path is null)
                return (null, selectedType);

            // 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, selectedType);
            return (new BclStorageFile(new FileInfo(path)), selectedType);
        }

        public override async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options)
        {
            if (_version < 3)
                return [];

            var parentWindow = $"x11:{_handle.Handle:X}";
            var chooserOptions = new Dictionary<string, VariantValue>
            {

Review comment (suggestion):
We probably should change `nameCondition || mimeCondition || patternCondition` into `nameCondition && (mimeCondition || patternCondition)`

Code after:
                if (e is not null)
                {
                    tsc.TrySetException(e);
                }
                else
                {
                    if (x.Results.TryGetValue("current_filter", out var currentFilter))
                    {
                        var name = currentFilter.GetItem(0).GetString();
                        var patterns = new List<string>();
                        var mimeTypes = new List<string>();
                        var types = currentFilter.GetItem(1).GetArray<VariantValue>();
                        foreach (var t in types)
                        {
                            if (t.GetItem(0).GetUInt32() == 1)
                                mimeTypes.Add(t.GetItem(1).GetString());
                            else
                                patterns.Add(t.GetItem(1).GetString());
                        }
                        
                        // Reuse the file type objects from options
                        // so the consuming code can match exactly the
                        // file type selected instead of spawning one.
                        selectedType = options.FileTypeChoices?.FirstOrDefault(type => type.Name == name && (
                            (type.MimeTypes?.All(y => mimeTypes.Contains(y)) ?? false) ||
                            (type.Patterns?.All(y => patterns.Contains(y)) ?? false))) 
                            ?? new FilePickerFileType(name) { MimeTypes = mimeTypes, Patterns = patterns };
                    }

                    tsc.TrySetResult(x.Results["uris"].GetArray<string>());
                }
            }).ConfigureAwait(false);

            var uris = await tsc.Task.ConfigureAwait(false);
            var path = uris?.FirstOrDefault() is { } filePath ? new Uri(filePath).LocalPath : null;

            if (path is null)
                return (null, selectedType);

            // 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, selectedType);
            return (new BclStorageFile(new FileInfo(path)), selectedType);
        }

        public override async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options)
        {
            if (_version < 3)
                return [];

            var parentWindow = $"x11:{_handle.Handle:X}";
            var chooserOptions = new Dictionary<string, VariantValue>