bind.go (2691B)
1 // Package wintoast provides a pure-Go implementation of toast notifications on Windows. 2 package wintoast 3 4 import "errors" 5 6 // AppData describes the application to the Windows Runtime. 7 // See toast.Notification for more thorough documentation off these fields. 8 type AppData struct { 9 AppID string 10 GUID string 11 ActivationExe string // optional 12 IconPath string // optional 13 IconBackgroundColor string // optional 14 } 15 16 // UserData contains Key:Value pairs generated within the notification, based 17 // on the XML content of the notification. Specifically, all inputs within 18 // the XML will generate a corresponding UserData struct. 19 type UserData struct { 20 Key string 21 Value string 22 } 23 24 // Callback is a function that gets invoked when the notification is activated. 25 type Callback func(appUserModelId string, invokedArgs string, userData []UserData) 26 27 // SetAppData teaches the Windows Runtime about our application and establishes the activation GUID 28 // so Windows will know how to invoke us back. 29 func SetAppData(data AppData) (err error) { 30 return setAppData(data) 31 } 32 33 // SetActivationCallback establishes the callback `cb` to be invoked when 34 // the toast notification is activated. This callback instance should handle 35 // being activated from any available toast notification. 36 func SetActivationCallback(cb Callback) { 37 callback = cb 38 } 39 40 // Push a notification described by the XML to the Windows Runtime. 41 // 42 // App data should be set first via a call to SetAppData before calling 43 // this function. 44 // 45 // If the powershell fallback is engaged, activation callbacks will not 46 // work as expected and the COM error will still be returned. 47 func Push(appID, xml string, op ...option) error { 48 var opts options 49 for _, opt := range op { 50 opt(&opts) 51 } 52 if opts.PowershellPreferred { 53 return pushPowershell(xml) 54 } 55 if appID == "" { 56 appID = appData.AppID 57 } 58 if err := pushCOM(appID, xml); err != nil { 59 if opts.PowershellFallback { 60 return errors.Join(err, pushPowershell(xml)) 61 } 62 return err 63 } 64 return nil 65 } 66 67 type options struct { 68 PowershellFallback bool 69 PowershellPreferred bool 70 } 71 72 type option func(*options) 73 74 // PreferPowershell indicates to use the powershell method by default. 75 // COM will not be used. 76 func PreferPowershell(opt *options) { 77 opt.PowershellPreferred = true 78 } 79 80 // PowershellFallback specifies to use the powershell method as a fallback 81 // if the COM api fails. 82 func PowershellFallback(opt *options) { 83 opt.PowershellFallback = true 84 } 85 86 // callback is the global callback reference that is invoked by Activate. 87 // 88 // NOTE(jfm): synchronize access to this? 89 var callback Callback = func(model, args string, data []UserData) {}