vendor/github.com/gen2brain/beeep/notify_windows.go

127 lines · 2662 bytes

  1//go:build windows && !linux && !freebsd && !netbsd && !openbsd && !darwin && !js
  2// +build windows,!linux,!freebsd,!netbsd,!openbsd,!darwin,!js
  3
  4package beeep
  5
  6import (
  7	"bufio"
  8	"bytes"
  9	"errors"
 10	"os/exec"
 11	"strings"
 12	"syscall"
 13	"time"
 14
 15	toast "github.com/go-toast/toast"
 16	"github.com/tadvi/systray"
 17	"golang.org/x/sys/windows/registry"
 18)
 19
 20var isWindows10 bool
 21var applicationID string
 22
 23func init() {
 24	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
 25	if err != nil {
 26		return
 27	}
 28	defer k.Close()
 29
 30	maj, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
 31	if err != nil {
 32		return
 33	}
 34
 35	isWindows10 = maj == 10
 36
 37	if isWindows10 {
 38		applicationID = appID()
 39	}
 40}
 41
 42// Notify sends desktop notification.
 43func Notify(title, message, appIcon string) error {
 44	if isWindows10 {
 45		return toastNotify(title, message, appIcon)
 46	}
 47
 48	err := baloonNotify(title, message, appIcon, false)
 49	if err != nil {
 50		e := msgNotify(title, message)
 51		if e != nil {
 52			return errors.New("beeep: " + err.Error() + "; " + e.Error())
 53		}
 54	}
 55
 56	return nil
 57
 58}
 59
 60func msgNotify(title, message string) error {
 61	msg, err := exec.LookPath("msg")
 62	if err != nil {
 63		return err
 64	}
 65	cmd := exec.Command(msg, "*", "/TIME:3", title+"\n\n"+message)
 66	cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
 67	return cmd.Run()
 68}
 69
 70func baloonNotify(title, message, appIcon string, bigIcon bool) error {
 71	tray, err := systray.New()
 72	if err != nil {
 73		return err
 74	}
 75
 76	err = tray.ShowCustom(pathAbs(appIcon), title)
 77	if err != nil {
 78		return err
 79	}
 80
 81	go func() {
 82		go func() {
 83			_ = tray.Run()
 84		}()
 85		time.Sleep(3 * time.Second)
 86		_ = tray.Stop()
 87	}()
 88
 89	return tray.ShowMessage(title, message, bigIcon)
 90}
 91
 92func toastNotify(title, message, appIcon string) error {
 93	notification := toastNotification(title, message, pathAbs(appIcon))
 94	return notification.Push()
 95}
 96
 97func toastNotification(title, message, appIcon string) toast.Notification {
 98	return toast.Notification{
 99		AppID:   applicationID,
100		Title:   title,
101		Message: message,
102		Icon:    appIcon,
103	}
104}
105
106func appID() string {
107	defID := "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe"
108	cmd := exec.Command("powershell", "-NoProfile", "Get-StartApps")
109	cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
110	out, err := cmd.Output()
111	if err != nil {
112		return defID
113	}
114
115	scanner := bufio.NewScanner(bytes.NewReader(out))
116	for scanner.Scan() {
117		line := strings.TrimSpace(scanner.Text())
118		if strings.Contains(line, "powershell.exe") {
119			sp := strings.Split(line, " ")
120			if len(sp) > 0 {
121				return sp[len(sp)-1]
122			}
123		}
124	}
125
126	return defID
127}