AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.22k stars 2.18k forks source link

Add FilterIndex to StroageFile when getting files from FilePicker #15861

Open perfect100 opened 3 months ago

perfect100 commented 3 months ago

Is your feature request related to a problem? Please describe.

Assume that we want to import file from a file picker. Firstly we need to set the filters.

        var filters = new FilePickerFileType[]
        {
            new("txt")
            {
                MimeTypes = [...],
                Patterns = ["*.txt"],
            },
            new("excel")
            {
                MimeTypes = [...],
                Patterns = ["*.xls", "xlsx"],
            },
            new("all")
            {
                MimeTypes = [...],
                Patterns = ["*.*"],
            },
        };
        var storageFiles = await StorageProvider.OpenFilePickerAsync(
            new FilePickerOpenOptions()
            {
                FileTypeFilter = filters,
            });

        // var filterIndex = storageFiles.FilterIndex
        // todo: handle with filterIndex

If user select all, I will do nothing...

Currently, the only way to get the filter index is matching the file extension name.

Describe the solution you'd like

Get the filter index from IStorageFile directly.

Describe alternatives you've considered

No response

Additional context

No response

thevortexcloud commented 3 months ago

What is the use case for this? The whole point of the file picker is to pick a file. You as a consumer of the file picker don't generally care what the file is as long as it matches your filters. If you want to do nothing when someone picks a specific option then you should just not offer the option?

maxkatz6 commented 3 months ago

@thevortexcloud typical use case would be to provide user a choice of different file types to save (.png, .jpg...), where app will get this index and actually save in a correct format after the picker. In case if app developer doesn't want to rely on extension.

But that's for Save file dialog. Not Open.

And I am not sure if we can implement this feature properly on most of platforms. Needs to be double checked.

Other than that, another (possibly better?) alternative would be to add MimeType property to the IStorageFile. Which is useful for both open/save scenarios. Also, should implementable pretty much everywhere, even on browser.