go-toast

Send toast notifications in Windows
Log | Files | Refs | README | LICENSE

commit 67a0f31080910ecdd58ce91c21992b832d85c61e
parent 1067c138245e5c5bda6ed12aa7015ac18f5a036f
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Wed, 15 Mar 2023 14:08:37 +0800

tmpl: package templating details

- add powershell template
- wrap templating in package to keep things clean

We expose exactly two symbols: each processed template
for convenient use.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>

Diffstat:
Dtemplate.go | 17-----------------
Atmpl/powershell.go.tmpl | 13+++++++++++++
Atmpl/tmpl.go | 28++++++++++++++++++++++++++++
Rtemplate.go.tmpl -> tmpl/xml.go.tmpl | 0
Mtoast.go | 3++-
5 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/template.go b/template.go @@ -1,17 +0,0 @@ -package toast - -import ( - _ "embed" - "text/template" -) - -//go:embed template.go.tmpl -var tmpl string - -// For more information about the schema: -// https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/schema-root -var toastTemplate = func() *template.Template { - t := template.New("toast") - template.Must(t.Parse(tmpl)) - return t -}() diff --git a/tmpl/powershell.go.tmpl b/tmpl/powershell.go.tmpl @@ -0,0 +1,12 @@ +[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null +[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null +[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null + +$APP_ID = '{{if .AppID}}{{.AppID}}{{else}}Windows App{{end}}' + +$template = @"{{template "toast-xml"}}"@ + +$xml = New-Object Windows.Data.Xml.Dom.XmlDocument +$xml.LoadXml($template) +$toast = New-Object Windows.UI.Notifications.ToastNotification $xml +[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast) +\ No newline at end of file diff --git a/tmpl/tmpl.go b/tmpl/tmpl.go @@ -0,0 +1,28 @@ +// Package tmpl wraps several templates that are used to generate notifications. +// +// The primary template describes the XML structure that the Windows Runtime expects +// to consume. The powershell template describes a script that we can use to execute +// the notification if the Windows Runtime is unavailable. +// +// For more information about the xml schema: +// https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/schema-root +package tmpl + +import ( + _ "embed" + "text/template" +) + +//go:embed xml.go.tmpl +var xml string + +//go:embed powershell.go.tmpl +var powershell string + +// XMLTemplate describes the XML content that the Windows Runtime uses to build +// toast notifications. +var XMLTemplate = template.Must(template.New("toast-xml").Parse(xml)) + +// ScriptTemplate describes the Powershell script that will invoke a toast notification +// given some XML. +var ScriptTemplate = template.Must(template.New("script").Parse(powershell)) diff --git a/template.go.tmpl b/tmpl/xml.go.tmpl diff --git a/toast.go b/toast.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "toast/internal/bind" + "toast/tmpl" ) // Notification @@ -134,7 +135,7 @@ func (n *Notification) applyDefaults() { func (n *Notification) buildXML() (string, error) { var out bytes.Buffer - err := toastTemplate.Execute(&out, n) + err := tmpl.XMLTemplate.Execute(&out, n) if err != nil { return "", err }