Fix defaults (windows).

This commit is contained in:
Nuno Cruces 2021-03-04 03:25:45 +00:00
parent ce2fa0e05d
commit b9d5eb011b
5 changed files with 41 additions and 30 deletions

View file

@ -46,7 +46,7 @@ func selectColor(options []Option) (color.Color, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
if opts.ctx != nil || opts.title != "" { if opts.ctx != nil || opts.title != nil {
unhook, err := hookDialogTitle(opts.ctx, opts.title) unhook, err := hookDialogTitle(opts.ctx, opts.title)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -28,8 +28,8 @@ func selectFile(options []Option) (string, error) {
args.StructSize = uint32(unsafe.Sizeof(args)) args.StructSize = uint32(unsafe.Sizeof(args))
args.Flags = 0x81008 // OFN_NOCHANGEDIR|OFN_FILEMUSTEXIST|OFN_EXPLORER args.Flags = 0x81008 // OFN_NOCHANGEDIR|OFN_FILEMUSTEXIST|OFN_EXPLORER
if opts.title != "" { if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(*opts.title)
} }
if opts.showHidden { if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
@ -76,8 +76,8 @@ func selectFileMutiple(options []Option) ([]string, error) {
args.StructSize = uint32(unsafe.Sizeof(args)) args.StructSize = uint32(unsafe.Sizeof(args))
args.Flags = 0x81208 // OFN_NOCHANGEDIR|OFN_ALLOWMULTISELECT|OFN_FILEMUSTEXIST|OFN_EXPLORER args.Flags = 0x81208 // OFN_NOCHANGEDIR|OFN_ALLOWMULTISELECT|OFN_FILEMUSTEXIST|OFN_EXPLORER
if opts.title != "" { if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(*opts.title)
} }
if opts.showHidden { if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
@ -149,8 +149,8 @@ func selectFileSave(options []Option) (string, error) {
args.StructSize = uint32(unsafe.Sizeof(args)) args.StructSize = uint32(unsafe.Sizeof(args))
args.Flags = 0x88808 // OFN_NOCHANGEDIR|OFN_PATHMUSTEXIST|OFN_NOREADONLYRETURN|OFN_EXPLORER args.Flags = 0x88808 // OFN_NOCHANGEDIR|OFN_PATHMUSTEXIST|OFN_NOREADONLYRETURN|OFN_EXPLORER
if opts.title != "" { if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(*opts.title)
} }
if opts.confirmOverwrite { if opts.confirmOverwrite {
args.Flags |= 0x2 // OFN_OVERWRITEPROMPT args.Flags |= 0x2 // OFN_OVERWRITEPROMPT
@ -229,8 +229,8 @@ func pickFolders(opts options, multi bool) (str string, lst []string, err error)
return "", nil, syscall.Errno(hr) return "", nil, syscall.Errno(hr)
} }
if opts.title != "" { if opts.title != nil {
ptr := syscall.StringToUTF16Ptr(opts.title) ptr := syscall.StringToUTF16Ptr(*opts.title)
dialog.Call(dialog.vtbl.SetTitle, uintptr(unsafe.Pointer(ptr))) dialog.Call(dialog.vtbl.SetTitle, uintptr(unsafe.Pointer(ptr)))
} }
@ -319,8 +319,8 @@ func browseForFolder(opts options) (string, []string, error) {
var args _BROWSEINFO var args _BROWSEINFO
args.Flags = 0x1 // BIF_RETURNONLYFSDIRS args.Flags = 0x1 // BIF_RETURNONLYFSDIRS
if opts.title != "" { if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(*opts.title)
} }
if opts.filename != "" { if opts.filename != "" {
ptr := syscall.StringToUTF16Ptr(opts.filename) ptr := syscall.StringToUTF16Ptr(opts.filename)

View file

@ -17,9 +17,9 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
var flags uintptr var flags uintptr
switch { switch {
case kind == questionKind && opts.extraButton != "": case kind == questionKind && opts.extraButton != nil:
flags |= 0x3 // MB_YESNOCANCEL flags |= 0x3 // MB_YESNOCANCEL
case kind == questionKind || opts.extraButton != "": case kind == questionKind || opts.extraButton != nil:
flags |= 0x1 // MB_OKCANCEL flags |= 0x1 // MB_OKCANCEL
} }
@ -35,14 +35,14 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
} }
if kind == questionKind && opts.defaultCancel { if kind == questionKind && opts.defaultCancel {
if opts.extraButton == "" { if opts.extraButton == nil {
flags |= 0x100 // MB_DEFBUTTON2 flags |= 0x100 // MB_DEFBUTTON2
} else { } else {
flags |= 0x200 // MB_DEFBUTTON3 flags |= 0x200 // MB_DEFBUTTON3
} }
} }
if opts.ctx != nil || opts.okLabel != "" || opts.cancelLabel != "" || opts.extraButton != "" { if opts.ctx != nil || opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -53,10 +53,15 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
defer unhook() defer unhook()
} }
var title *uint16
if opts.title != nil {
title = syscall.StringToUTF16Ptr(*opts.title)
}
activate() activate()
s, _, err := messageBox.Call(0, s, _, err := messageBox.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title))), flags) uintptr(unsafe.Pointer(title)), flags)
if opts.ctx != nil && opts.ctx.Err() != nil { if opts.ctx != nil && opts.ctx.Err() != nil {
return false, opts.ctx.Err() return false, opts.ctx.Err()
@ -81,14 +86,14 @@ func hookMessageLabels(kind messageKind, opts options) (unhook context.CancelFun
getClassName.Call(wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name))) getClassName.Call(wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
if syscall.UTF16ToString(name[:]) == "Button" { if syscall.UTF16ToString(name[:]) == "Button" {
ctl, _, _ := getDlgCtrlID.Call(wnd) ctl, _, _ := getDlgCtrlID.Call(wnd)
var text string var text *string
switch ctl { switch ctl {
case 1, 6: // IDOK, IDYES case 1, 6: // IDOK, IDYES
text = opts.okLabel text = opts.okLabel
case 2: // IDCANCEL case 2: // IDCANCEL
if kind == questionKind { if kind == questionKind {
text = opts.cancelLabel text = opts.cancelLabel
} else if opts.extraButton != "" { } else if opts.extraButton != nil {
text = opts.extraButton text = opts.extraButton
} else { } else {
text = opts.okLabel text = opts.okLabel
@ -96,8 +101,8 @@ func hookMessageLabels(kind messageKind, opts options) (unhook context.CancelFun
case 7: // IDNO case 7: // IDNO
text = opts.extraButton text = opts.extraButton
} }
if text != "" { if text != nil {
ptr := syscall.StringToUTF16Ptr(text) ptr := syscall.StringToUTF16Ptr(*text)
setWindowText.Call(wnd, uintptr(unsafe.Pointer(ptr))) setWindowText.Call(wnd, uintptr(unsafe.Pointer(ptr)))
} }
} }

View file

@ -29,8 +29,10 @@ func notify(text string, options []Option) error {
info := syscall.StringToUTF16(text) info := syscall.StringToUTF16(text)
copy(args.Info[:len(args.Info)-1], info) copy(args.Info[:len(args.Info)-1], info)
title := syscall.StringToUTF16(opts.title) if opts.title != nil {
copy(args.InfoTitle[:len(args.InfoTitle)-1], title) title := syscall.StringToUTF16(*opts.title)
copy(args.InfoTitle[:len(args.InfoTitle)-1], title)
}
switch opts.icon { switch opts.icon {
case InfoIcon: case InfoIcon:
@ -71,8 +73,8 @@ func wtsMessage(text string, opts options) error {
} }
title := opts.title title := opts.title
if title == "" { if title == nil {
title = "Notification" title = stringPtr("Notification")
} }
timeout := zenutil.Timeout timeout := zenutil.Timeout
@ -81,7 +83,7 @@ func wtsMessage(text string, opts options) error {
} }
ptext := syscall.StringToUTF16(text) ptext := syscall.StringToUTF16(text)
ptitle := syscall.StringToUTF16(title) ptitle := syscall.StringToUTF16(*title)
var res uint32 var res uint32
s, _, err := wtsSendMessage.Call( s, _, err := wtsSendMessage.Call(

View file

@ -138,11 +138,15 @@ func hookDialog(ctx context.Context, initDialog func(wnd uintptr)) (unhook conte
}, nil }, nil
} }
func hookDialogTitle(ctx context.Context, title string) (unhook context.CancelFunc, err error) { func hookDialogTitle(ctx context.Context, title *string) (unhook context.CancelFunc, err error) {
return hookDialog(ctx, func(wnd uintptr) { var init func(wnd uintptr)
ptr := syscall.StringToUTF16Ptr(title) if title != nil {
setWindowText.Call(wnd, uintptr(unsafe.Pointer(ptr))) init = func(wnd uintptr) {
}) ptr := syscall.StringToUTF16Ptr(*title)
setWindowText.Call(wnd, uintptr(unsafe.Pointer(ptr)))
}
}
return hookDialog(ctx, init)
} }
type _COMObject struct{} type _COMObject struct{}