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()
defer runtime.UnlockOSThread()
if opts.ctx != nil || opts.title != "" {
if opts.ctx != nil || opts.title != nil {
unhook, err := hookDialogTitle(opts.ctx, opts.title)
if err != nil {
return nil, err

View file

@ -28,8 +28,8 @@ func selectFile(options []Option) (string, error) {
args.StructSize = uint32(unsafe.Sizeof(args))
args.Flags = 0x81008 // OFN_NOCHANGEDIR|OFN_FILEMUSTEXIST|OFN_EXPLORER
if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title)
if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(*opts.title)
}
if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
@ -76,8 +76,8 @@ func selectFileMutiple(options []Option) ([]string, error) {
args.StructSize = uint32(unsafe.Sizeof(args))
args.Flags = 0x81208 // OFN_NOCHANGEDIR|OFN_ALLOWMULTISELECT|OFN_FILEMUSTEXIST|OFN_EXPLORER
if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title)
if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(*opts.title)
}
if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
@ -149,8 +149,8 @@ func selectFileSave(options []Option) (string, error) {
args.StructSize = uint32(unsafe.Sizeof(args))
args.Flags = 0x88808 // OFN_NOCHANGEDIR|OFN_PATHMUSTEXIST|OFN_NOREADONLYRETURN|OFN_EXPLORER
if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title)
if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(*opts.title)
}
if opts.confirmOverwrite {
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)
}
if opts.title != "" {
ptr := syscall.StringToUTF16Ptr(opts.title)
if opts.title != nil {
ptr := syscall.StringToUTF16Ptr(*opts.title)
dialog.Call(dialog.vtbl.SetTitle, uintptr(unsafe.Pointer(ptr)))
}
@ -319,8 +319,8 @@ func browseForFolder(opts options) (string, []string, error) {
var args _BROWSEINFO
args.Flags = 0x1 // BIF_RETURNONLYFSDIRS
if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title)
if opts.title != nil {
args.Title = syscall.StringToUTF16Ptr(*opts.title)
}
if 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
switch {
case kind == questionKind && opts.extraButton != "":
case kind == questionKind && opts.extraButton != nil:
flags |= 0x3 // MB_YESNOCANCEL
case kind == questionKind || opts.extraButton != "":
case kind == questionKind || opts.extraButton != nil:
flags |= 0x1 // MB_OKCANCEL
}
@ -35,14 +35,14 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
}
if kind == questionKind && opts.defaultCancel {
if opts.extraButton == "" {
if opts.extraButton == nil {
flags |= 0x100 // MB_DEFBUTTON2
} else {
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()
defer runtime.UnlockOSThread()
@ -53,10 +53,15 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
defer unhook()
}
var title *uint16
if opts.title != nil {
title = syscall.StringToUTF16Ptr(*opts.title)
}
activate()
s, _, err := messageBox.Call(0,
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 {
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)))
if syscall.UTF16ToString(name[:]) == "Button" {
ctl, _, _ := getDlgCtrlID.Call(wnd)
var text string
var text *string
switch ctl {
case 1, 6: // IDOK, IDYES
text = opts.okLabel
case 2: // IDCANCEL
if kind == questionKind {
text = opts.cancelLabel
} else if opts.extraButton != "" {
} else if opts.extraButton != nil {
text = opts.extraButton
} else {
text = opts.okLabel
@ -96,8 +101,8 @@ func hookMessageLabels(kind messageKind, opts options) (unhook context.CancelFun
case 7: // IDNO
text = opts.extraButton
}
if text != "" {
ptr := syscall.StringToUTF16Ptr(text)
if text != nil {
ptr := syscall.StringToUTF16Ptr(*text)
setWindowText.Call(wnd, uintptr(unsafe.Pointer(ptr)))
}
}

View file

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

View file

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