Skip to content

Commit

Permalink
Merge pull request #5394 from Jacalz/flatpak-dialog-races
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz authored Jan 11, 2025
2 parents cfa66a1 + d9dbf25 commit b7b8a61
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 99 deletions.
143 changes: 76 additions & 67 deletions dialog/file_xdg_flatpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,103 +12,112 @@ import (
"github.com/rymdport/portal/filechooser"
)

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

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

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

callback(storage.ListerForURI(uri))
return storage.ListerForURI(uri)
}

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

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

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

callback(storage.Reader(uri))
return storage.Reader(uri)
}

func saveFile(parentWindowHandle string, options *filechooser.SaveFileOptions) (fyne.URIWriteCloser, error) {
uris, err := filechooser.SaveFile(parentWindowHandle, "Save File", options)
if err != nil {
return nil, err
}

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

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

return storage.Writer(uri)
}

func fileOpenOSOverride(d *FileDialog) bool {
go func() {
folderCallback, folder := d.callback.(func(fyne.ListableURI, error))
options := &filechooser.OpenFileOptions{
Directory: folder,
AcceptLabel: d.confirmText,
}
if d.startingLocation != nil {
options.CurrentFolder = d.startingLocation.Path()
}

parentWindowHandle := windowHandleForPortal(d.parent)

if folder {
openFolder(parentWindowHandle, folderCallback, options)
return
}

fileCallback := d.callback.(func(fyne.URIReadCloser, error))
openFile(parentWindowHandle, fileCallback, options)
}()
folderCallback, folder := d.callback.(func(fyne.ListableURI, error))
fileCallback, _ := d.callback.(func(fyne.URIReadCloser, error))
options := &filechooser.OpenFileOptions{
Directory: folder,
AcceptLabel: d.confirmText,
}

if d.startingLocation != nil {
options.CurrentFolder = d.startingLocation.Path()
}

windowHandle := windowHandleForPortal(d.parent)

if folder {
go func() {
folder, err := openFolder(windowHandle, options)
fyne.Do(func() {
folderCallback(folder, err)
})
}()
} else {
go func() {
file, err := openFile(windowHandle, options)
fyne.Do(func() {
fileCallback(file, err)
})
}()
}

return true
}

func fileSaveOSOverride(d *FileDialog) bool {
options := &filechooser.SaveFileOptions{
AcceptLabel: d.confirmText,
CurrentName: d.initialFileName,
}
if d.startingLocation != nil {
options.CurrentFolder = d.startingLocation.Path()
}

callback := d.callback.(func(fyne.URIWriteCloser, error))
windowHandle := windowHandleForPortal(d.parent)

go func() {
options := &filechooser.SaveFileOptions{
AcceptLabel: d.confirmText,
CurrentName: d.initialFileName,
}
if d.startingLocation != nil {
options.CurrentFolder = d.startingLocation.Path()
}

parentWindowHandle := windowHandleForPortal(d.parent)

callback := d.callback.(func(fyne.URIWriteCloser, error))
uris, err := filechooser.SaveFile(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.Writer(uri))
file, err := saveFile(windowHandle, options)
fyne.Do(func() {
callback(file, err)
})
}()

return true
}

Expand All @@ -117,14 +126,14 @@ func x11WindowHandleToString(handle uintptr) string {
}

func windowHandleForPortal(window fyne.Window) string {
parentWindowHandle := ""
windowHandle := ""
if !build.IsWayland {
window.(driver.NativeWindow).RunNative(func(context any) {
handle := context.(driver.X11WindowContext).WindowHandle
parentWindowHandle = x11WindowHandleToString(handle)
windowHandle = x11WindowHandleToString(handle)
})
}

// TODO: We need to get the Wayland handle from the xdg_foreign protocol and convert to string on the form "wayland:{id}".
return parentWindowHandle
return windowHandle
}
15 changes: 6 additions & 9 deletions internal/driver/glfw/window_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (
var _ driver.NativeWindow = (*window)(nil)

func (w *window) RunNative(f func(any)) {
runOnMain(func() {
var nsWindow uintptr
if v := w.view(); v != nil {
nsWindow = uintptr(v.GetCocoaWindow())
}
f(driver.MacWindowContext{
NSWindow: nsWindow,
})
})
context := driver.MacWindowContext{}
if v := w.view(); v != nil {
context.NSWindow = uintptr(v.GetCocoaWindow())
}

f(context)
}
15 changes: 6 additions & 9 deletions internal/driver/glfw/window_wayland.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import (
var _ driver.NativeWindow = (*window)(nil)

func (w *window) RunNative(f func(any)) {
runOnMain(func() {
var waylandSurface uintptr
if v := w.view(); v != nil {
waylandSurface = uintptr(unsafe.Pointer(v.GetWaylandWindow()))
}
f(driver.WaylandWindowContext{
WaylandSurface: waylandSurface,
})
})
context := driver.WaylandWindowContext{}
if v := w.view(); v != nil {
context.WaylandSurface = uintptr(unsafe.Pointer(v.GetWaylandWindow()))
}

f(context)
}
11 changes: 4 additions & 7 deletions internal/driver/glfw/window_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ func (w *window) computeCanvasSize(width, height int) fyne.Size {
var _ driver.NativeWindow = (*window)(nil)

func (w *window) RunNative(f func(any)) {
var hwnd uintptr
context := driver.WindowsWindowContext{}
if v := w.view(); v != nil {
hwnd = uintptr(unsafe.Pointer(v.GetWin32Window()))
context.HWND = uintptr(unsafe.Pointer(v.GetWin32Window()))
}
runOnMain(func() {
f(driver.WindowsWindowContext{
HWND: hwnd,
})
})

f(context)
}
11 changes: 4 additions & 7 deletions internal/driver/glfw/window_x11.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import "fyne.io/fyne/v2/driver"
var _ driver.NativeWindow = (*window)(nil)

func (w *window) RunNative(f func(any)) {
var handle uintptr
context := driver.X11WindowContext{}
if v := w.view(); v != nil {
handle = uintptr(v.GetX11Window())
context.WindowHandle = uintptr(v.GetX11Window())
}
runOnMain(func() {
f(driver.X11WindowContext{
WindowHandle: handle,
})
})

f(context)
}

0 comments on commit b7b8a61

Please sign in to comment.