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

Support filtering of file dialog #893

Closed
andydotxyz opened this issue Apr 22, 2020 · 10 comments
Closed

Support filtering of file dialog #893

andydotxyz opened this issue Apr 22, 2020 · 10 comments
Assignees

Comments

@andydotxyz
Copy link
Member

Probably filter by mime type - this will need to be translated to other mobile specific approaches for iOS, Android.
Provide standard filterings, but maybe the ability to pass a function for more specific filtering...

@suifengqjn
Copy link

Why does it not support selecting folders or multiple files

@andydotxyz
Copy link
Member Author

Because that is a lot more complicated to do across all platforms.
We will try to add support for that in future releases. Opening a new ticket would be helpful so we can track the requirement.

@stuartmscott
Copy link
Member

Opened #941

@andydotxyz
Copy link
Member Author

andydotxyz commented May 6, 2020

In a slack conversation I posted this as a conversation piece, thought I would post it here too.

package main

type FileFilter interface {
	Matches(fyne.File) bool
}

type ExtensionFileFilter struct
	Extensions []string
}

func (e *ExtensionFileFilter) Matches(url string) bool {
	ext := filepath.Extension(uri)
	for _, childExt := range e.extensions {
		if childExt == ext {
			return true
		}
	}
	
	return false
}

type MimeFileFilter struct
	Mime string
}

func (m *MimeFileFilter) Matches(url string) bool {
	mime := mimeForURI(uri)
	if mime == m.mime {
		return true
	}
	
	if m.mime[:len(m.mime)-1] == "*" {
		return strings.SubString(mime, m.mime)
	}
	
	return false
}

type LocalFileFilter struct {
}

func (l *LocalFileFilter) Matches(url string) bool {
	return len(uri) > 7  && uri()[:7] == "file://"
}

@stuartmscott
Copy link
Member

I like it - it gives a lot of flexibility. The only limitation is fyne.File - you can only make a filefilter based on Name/URI, you can't for example make a filter based on the file size. However that is not a typical use case, and we can always extend fyne.File in future. I think your proposal covers most use cases well.

@andydotxyz
Copy link
Member Author

A bummer just realised it will have to be URI (string) only because we moved away from fyne.File to fyne.FileReadCloser - we cannot pass them around as they are literally pointers into files.
Slightly less extensible then, but still checks the current boxes.

@steveoc64
Copy link
Member

Could I ask why we are needing to create a fyne.File / fyne.FileReadCloser as opposed to being able to use os.File / io.Filexxxxx ?

@andydotxyz
Copy link
Member Author

Look at all of the functionality on os.File - a lot of that is not possible in the cross platform (esp mobile) environments. our FileReadCloser can be used in place of any os.ReadCloser so it's not as buig a deal as it seems.

andydotxyz added a commit to andydotxyz/fyne that referenced this issue May 28, 2020
Update vendored mobile driver.
Export the file filter types so we can work with them.

Also tidy up extension handling a little.
And remove excess demo buttons from Windows tab.

Fixes fyne-io#893
@andydotxyz
Copy link
Member Author

Good hussle, thanks @okratitan :)

This is now on develop for testing

@Niek
Copy link

Niek commented Jul 10, 2020

I noticed ExtensionFileFilter is case-sensitive. This is a but annoying, because some software saves files as .e.g .JPG I have to duplicate each extension filter. What about making it case-insensitive? I'm talking about this line:

if extension == ext {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants