gui/window: native open and save file dialogs
ShowFileDialog (UI thread) and ChooseFile (any goroutine, waits) show a native dialog for the window -- NSOpenPanel/NSSavePanel as a sheet on macOS, GetOpenFileNameW/GetSaveFileNameW on Windows -- with a title, a start folder, a suggested name and allowed extensions. It only returns the chosen path; one dialog at a time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e1495430c5
commit
5d141c7cef
7 files changed
+284
-2
No files matched your search
@@ -4,7 +4,7 @@ package window
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c -fobjc-arc
|
||||
#cgo LDFLAGS: -framework Cocoa -framework WebKit
|
||||
#cgo LDFLAGS: -framework Cocoa -framework WebKit -framework UniformTypeIdentifiers
|
||||
#include <stdlib.h>
|
||||
#include "darwin.h"
|
||||
*/
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"net/http"
|
||||
"runtime"
|
||||
"runtime/cgo"
|
||||
"strings"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
@@ -35,6 +36,7 @@ type darwinBackend struct {
|
||||
mu sync.RWMutex // guards h against Destroy while a goroutine calls into ObjC
|
||||
|
||||
snapDone func(error)
|
||||
fileDone func(path string, err error)
|
||||
}
|
||||
|
||||
// with runs f with the live host pointer, or not at all after Destroy.
|
||||
@@ -298,3 +300,49 @@ func (t *darwinTask) Close(err error) {
|
||||
}
|
||||
t.b.with(func(h unsafe.Pointer) { C.yt_task_finish(h, C.long(t.id), ce) })
|
||||
}
|
||||
|
||||
// FileDialog shows a native open or save panel as a sheet on the window;
|
||||
// done runs on the UI thread.
|
||||
func (b *darwinBackend) FileDialog(d FileDialog, done func(string, error)) {
|
||||
b.mu.Lock()
|
||||
if b.fileDone != nil {
|
||||
b.mu.Unlock()
|
||||
done("", ErrDialogOpen)
|
||||
return
|
||||
}
|
||||
b.fileDone = done
|
||||
b.mu.Unlock()
|
||||
save := 0
|
||||
if d.Save {
|
||||
save = 1
|
||||
}
|
||||
title, dir, name, types := cstr(d.Title), cstr(d.Directory), cstr(d.Name), cstr(strings.Join(d.Extensions, ","))
|
||||
defer func() {
|
||||
for _, c := range []*C.char{title, dir, name, types} {
|
||||
C.free(unsafe.Pointer(c))
|
||||
}
|
||||
}()
|
||||
if !b.with(func(h unsafe.Pointer) { C.yt_file_dialog(h, C.int(save), title, dir, name, types) }) {
|
||||
b.mu.Lock()
|
||||
b.fileDone = nil
|
||||
b.mu.Unlock()
|
||||
done("", errUnavailable("file dialog: the window is gone"))
|
||||
}
|
||||
}
|
||||
|
||||
//export ytGoFileDone
|
||||
func ytGoFileDone(h C.uintptr_t, path *C.char) {
|
||||
b := cgo.Handle(h).Value().(*darwinBackend)
|
||||
b.mu.Lock()
|
||||
done := b.fileDone
|
||||
b.fileDone = nil
|
||||
b.mu.Unlock()
|
||||
if done == nil {
|
||||
return
|
||||
}
|
||||
if path == nil {
|
||||
done("", nil)
|
||||
return
|
||||
}
|
||||
done(C.GoString(path), nil)
|
||||
}
|
||||
Reference in new issue
Block a user