From 7b7d915e3aecc5b44a5e2baf2c53a8851c26b6da Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 27 Jan 2020 13:44:47 +0000 Subject: [PATCH] Notifications (windows). --- color_windows.go | 11 ++++----- notify_windows.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/color_windows.go b/color_windows.go index 43d454b..df5aeec 100644 --- a/color_windows.go +++ b/color_windows.go @@ -38,9 +38,9 @@ func selectColor(options []Option) (color.Color, error) { args.RgbResult = uint32(n.R) | (uint32(n.G) << 8) | (uint32(n.B) << 16) } if opts.showPalette { - args.Flags |= 4 // CC_PREVENTFULLOPEN + args.Flags |= 0x4 // CC_PREVENTFULLOPEN } else { - args.Flags |= 2 // CC_FULLOPEN + args.Flags |= 0x2 // CC_FULLOPEN } runtime.LockOSThread() @@ -55,16 +55,15 @@ func selectColor(options []Option) (color.Color, error) { } n, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args))) + if n == 0 { + return nil, commDlgError() + } // save custom colors back colorsMutex.Lock() savedColors = customColors colorsMutex.Unlock() - if n == 0 { - return nil, commDlgError() - } - r := uint8(args.RgbResult >> 0) g := uint8(args.RgbResult >> 8) b := uint8(args.RgbResult >> 16) diff --git a/notify_windows.go b/notify_windows.go index d89ae3f..4d84382 100644 --- a/notify_windows.go +++ b/notify_windows.go @@ -1,5 +1,61 @@ package zenity +import ( + "syscall" + "unsafe" +) + +var ( + shellNotifyIcon = shell32.NewProc("Shell_NotifyIconW") +) + func notify(text string, options []Option) error { - panic("not implemented") + opts := applyOptions(options) + + var args _NOTIFYICONDATA + args.StructSize = uint32(unsafe.Sizeof(args)) + args.ID = 0x378eb49c // random + args.Flags = 0x00000010 // NIF_INFO + args.State = 0x00000001 // NIS_HIDDEN + + info := syscall.StringToUTF16(text) + copy(args.Info[:len(args.Info)-1], info) + + title := syscall.StringToUTF16(opts.title) + copy(args.InfoTitle[:len(args.InfoTitle)-1], title) + + switch opts.icon { + case InfoIcon: + args.InfoFlags |= 0x1 // NIIF_INFO + case WarningIcon: + args.InfoFlags |= 0x2 // NIIF_WARNING + case ErrorIcon: + args.InfoFlags |= 0x3 // NIIF_ERROR + } + + n, _, err := shellNotifyIcon.Call(0 /* NIM_ADD */, uintptr(unsafe.Pointer(&args))) + if n == 0 { + return err + } + + shellNotifyIcon.Call(2 /* NIM_DELETE */, uintptr(unsafe.Pointer(&args))) + return nil +} + +type _NOTIFYICONDATA struct { + StructSize uint32 + Owner uintptr + ID uint32 + Flags uint32 + CallbackMessage uint32 + Icon uintptr + Tip [128]uint16 + State uint32 + StateMask uint32 + Info [256]uint16 + Version uint32 + InfoTitle [64]uint16 + InfoFlags uint32 + // GuidItem [16]byte + // BalloonIcon uintptr }