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
@@ -0,0 +1,115 @@
|
||||
//go:build windows
|
||||
|
||||
package window
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
var (
|
||||
comdlg32 = windows.NewLazySystemDLL("comdlg32.dll")
|
||||
pGetOpenFile = comdlg32.NewProc("GetOpenFileNameW")
|
||||
pGetSaveFile = comdlg32.NewProc("GetSaveFileNameW")
|
||||
pCommDlgExtErr = comdlg32.NewProc("CommDlgExtendedError")
|
||||
errNoFileDialog = fmt.Errorf("window: file dialog failed")
|
||||
)
|
||||
|
||||
// openFileName is OPENFILENAMEW (commdlg.h).
|
||||
type openFileName struct {
|
||||
lStructSize uint32
|
||||
hwndOwner uintptr
|
||||
hInstance uintptr
|
||||
lpstrFilter *uint16
|
||||
lpstrCustomFilter *uint16
|
||||
nMaxCustFilter uint32
|
||||
nFilterIndex uint32
|
||||
lpstrFile *uint16
|
||||
nMaxFile uint32
|
||||
lpstrFileTitle *uint16
|
||||
nMaxFileTitle uint32
|
||||
lpstrInitialDir *uint16
|
||||
lpstrTitle *uint16
|
||||
flags uint32
|
||||
nFileOffset uint16
|
||||
nFileExtension uint16
|
||||
lpstrDefExt *uint16
|
||||
lCustData uintptr
|
||||
lpfnHook uintptr
|
||||
lpTemplateName *uint16
|
||||
pvReserved uintptr
|
||||
dwReserved uint32
|
||||
flagsEx uint32
|
||||
}
|
||||
|
||||
const (
|
||||
ofnOverwritePrompt = 0x00000002
|
||||
ofnNoChangeDir = 0x00000008
|
||||
ofnPathMustExist = 0x00000800
|
||||
ofnFileMustExist = 0x00001000
|
||||
ofnExplorer = 0x00080000
|
||||
)
|
||||
|
||||
// fileFilter is the dialog's filter list: the allowed extensions, then
|
||||
// all files; pairs of display text and pattern, each NUL-terminated,
|
||||
// the list ending with an extra NUL.
|
||||
func fileFilter(exts []string) []uint16 {
|
||||
var b strings.Builder
|
||||
if len(exts) > 0 {
|
||||
pats := make([]string, len(exts))
|
||||
for i, e := range exts {
|
||||
pats[i] = "*." + strings.TrimPrefix(e, ".")
|
||||
}
|
||||
p := strings.Join(pats, ";")
|
||||
b.WriteString(p + " (" + p + ")\x00" + p + "\x00")
|
||||
}
|
||||
b.WriteString("All files (*.*)\x00*.*\x00\x00")
|
||||
return windows.StringToUTF16(b.String())
|
||||
}
|
||||
|
||||
// FileDialog shows the common open or save dialog, modal to the window
|
||||
// (it runs its own message loop on the UI thread); done runs right after.
|
||||
func (b *winBackend) FileDialog(d FileDialog, done func(string, error)) {
|
||||
buf := make([]uint16, 32768)
|
||||
if d.Save && d.Name != "" {
|
||||
copy(buf[:len(buf)-1], windows.StringToUTF16(d.Name))
|
||||
}
|
||||
filter := fileFilter(d.Extensions)
|
||||
ofn := openFileName{
|
||||
hwndOwner: b.hwnd,
|
||||
lpstrFilter: &filter[0],
|
||||
nFilterIndex: 1,
|
||||
lpstrFile: &buf[0],
|
||||
nMaxFile: uint32(len(buf)),
|
||||
flags: ofnExplorer | ofnNoChangeDir | ofnPathMustExist,
|
||||
}
|
||||
ofn.lStructSize = uint32(unsafe.Sizeof(ofn))
|
||||
if d.Title != "" {
|
||||
ofn.lpstrTitle, _ = windows.UTF16PtrFromString(d.Title)
|
||||
}
|
||||
if d.Directory != "" {
|
||||
ofn.lpstrInitialDir, _ = windows.UTF16PtrFromString(d.Directory)
|
||||
}
|
||||
proc := pGetOpenFile
|
||||
if d.Save {
|
||||
proc = pGetSaveFile
|
||||
ofn.flags |= ofnOverwritePrompt
|
||||
if len(d.Extensions) > 0 {
|
||||
ofn.lpstrDefExt, _ = windows.UTF16PtrFromString(strings.TrimPrefix(d.Extensions[0], "."))
|
||||
}
|
||||
} else {
|
||||
ofn.flags |= ofnFileMustExist
|
||||
}
|
||||
if r, _, _ := proc.Call(uintptr(unsafe.Pointer(&ofn))); r == 0 {
|
||||
if code, _, _ := pCommDlgExtErr.Call(); code != 0 {
|
||||
done("", fmt.Errorf("%w: CommDlgExtendedError 0x%x", errNoFileDialog, code))
|
||||
return
|
||||
}
|
||||
done("", nil) // cancelled
|
||||
return
|
||||
}
|
||||
done(windows.UTF16ToString(buf), nil)
|
||||
}
|
||||
Reference in new issue
Block a user