2020-01-26 11:04:49 -05:00
|
|
|
package zenity
|
|
|
|
|
2020-01-27 08:44:47 -05:00
|
|
|
import (
|
2020-01-27 13:11:38 -05:00
|
|
|
"runtime"
|
2020-01-27 08:44:47 -05:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
shellNotifyIcon = shell32.NewProc("Shell_NotifyIconW")
|
|
|
|
)
|
|
|
|
|
2020-01-26 11:04:49 -05:00
|
|
|
func notify(text string, options []Option) error {
|
2020-01-27 08:44:47 -05:00
|
|
|
opts := applyOptions(options)
|
|
|
|
|
2020-01-30 20:31:54 -05:00
|
|
|
if opts.ctx != nil && opts.ctx.Err() != nil {
|
|
|
|
return opts.ctx.Err()
|
|
|
|
}
|
|
|
|
|
2020-01-27 08:44:47 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-27 13:11:38 -05:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2020-01-29 09:15:21 -05:00
|
|
|
s, _, err := shellNotifyIcon.Call(0 /* NIM_ADD */, uintptr(unsafe.Pointer(&args)))
|
|
|
|
if s == 0 {
|
2020-01-27 13:11:38 -05:00
|
|
|
if errno, ok := err.(syscall.Errno); ok && errno == 0 {
|
|
|
|
_, err = Info(text, Title(opts.title), Icon(opts.icon))
|
|
|
|
}
|
2020-01-27 08:44:47 -05:00
|
|
|
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
|
2020-01-26 11:04:49 -05:00
|
|
|
}
|