Skip to content

Commit

Permalink
Make the flatpak file opening code more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Mar 20, 2024
1 parent d551593 commit d47ccbc
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions dialog/file_xdg_flatpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,47 @@ import (
"github.com/rymdport/portal/filechooser"
)

func openFolder(parentWindowHandle string, callback func(fyne.ListableURI, error), options *filechooser.OpenFileOptions) {
uris, err := filechooser.OpenFile(parentWindowHandle, "Open Folder", options)
if err != nil {
callback(nil, err)
}

if len(uris) == 0 {
callback(nil, nil)
return
}

uri, err := storage.ParseURI(uris[0])
if err != nil {
callback(nil, err)
return
}

callback(storage.ListerForURI(uri))
}

func openFile(parentWindowHandle string, callback func(fyne.URIReadCloser, error), options *filechooser.OpenFileOptions) {
uris, err := filechooser.OpenFile(parentWindowHandle, "Open File", options)
if err != nil {
callback(nil, err)
return
}

if len(uris) == 0 {
callback(nil, nil)
return
}

uri, err := storage.ParseURI(uris[0])
if err != nil {
callback(nil, err)
return
}

callback(storage.Reader(uri))
}

func fileOpenOSOverride(d *FileDialog) bool {
go func() {
folderCallback, folder := d.callback.(func(fyne.ListableURI, error))
Expand All @@ -22,45 +63,12 @@ func fileOpenOSOverride(d *FileDialog) bool {
parentWindowHandle := d.parent.(interface{ GetWindowHandle() string }).GetWindowHandle()

if folder {
uris, err := filechooser.OpenFile(parentWindowHandle, "Open Folder", options)
if err != nil {
folderCallback(nil, err)
}

if len(uris) == 0 {
folderCallback(nil, nil)
return
}

uri, err := storage.ParseURI(uris[0])
if err != nil {
folderCallback(nil, err)
return
}

folderCallback(storage.ListerForURI(uri))
openFolder(parentWindowHandle, folderCallback, options)
return
}

uris, err := filechooser.OpenFile(parentWindowHandle, "Open File", options)
fileCallback := d.callback.(func(fyne.URIReadCloser, error))
if err != nil {
fileCallback(nil, err)
return
}

if len(uris) == 0 {
fileCallback(nil, nil)
return
}

uri, err := storage.ParseURI(uris[0])
if err != nil {
fileCallback(nil, err)
return
}

fileCallback(storage.Reader(uri))
openFile(parentWindowHandle, fileCallback, options)
}()
return true
}
Expand Down

0 comments on commit d47ccbc

Please sign in to comment.