go-notify-darwin

Rich notifications on macOS for Go
Log | Files | Refs | README | LICENSE

main.go (2567B)


      1 package main
      2 
      3 import (
      4 	"log/slog"
      5 	"os"
      6 
      7 	notify "git.sr.ht/~jackmordaunt/go-notify-darwin"
      8 
      9 	"gioui.org/app"
     10 	"gioui.org/layout"
     11 	"gioui.org/op"
     12 	"gioui.org/widget"
     13 	"gioui.org/widget/material"
     14 )
     15 
     16 func main() {
     17 	slog.SetLogLoggerLevel(slog.LevelDebug)
     18 
     19 	notify.Init(notify.Category{
     20 		ID:      "best category eva",
     21 		Summary: "cool category",
     22 		Preview: "wowo cool",
     23 		Actions: []notify.Action{
     24 			{
     25 				ID:    "plain-action",
     26 				Title: "Button",
     27 				Icon:  "icon.png",
     28 			},
     29 		},
     30 		TextInputActions: []notify.TextInputAction{
     31 			{
     32 				ID:          "plain-action-text-input",
     33 				Title:       "Say Something",
     34 				ButtonTitle: "Send Message",
     35 				Icon:        "icon.jpg",
     36 				Placeholder: "type here",
     37 			},
     38 		},
     39 	})
     40 
     41 	notify.SetCallback(func(args notify.CallbackArgs) {
     42 		slog.Info("callback", "args", args)
     43 	})
     44 
     45 	go func() {
     46 		os.Exit(run())
     47 	}()
     48 
     49 	slog.Info("waiting")
     50 	app.Main()
     51 }
     52 
     53 func run() int {
     54 	w := new(app.Window)
     55 	w.Option(app.Title("gio app"))
     56 
     57 	var (
     58 		ops op.Ops
     59 	)
     60 
     61 	for {
     62 		switch e := w.Event().(type) {
     63 		case app.FrameEvent:
     64 			gtx := app.NewContext(&ops, e)
     65 			frame(gtx)
     66 			e.Frame(gtx.Ops)
     67 		case app.DestroyEvent:
     68 			return 0
     69 		}
     70 	}
     71 }
     72 
     73 var (
     74 	notifyButton      widget.Clickable
     75 	notifyAdhocButton widget.Clickable
     76 	cancelButton      widget.Clickable
     77 	th                = material.NewTheme()
     78 )
     79 
     80 type (
     81 	C = layout.Context
     82 	D = layout.Dimensions
     83 )
     84 
     85 func frame(gtx layout.Context) layout.Dimensions {
     86 	if cancelButton.Clicked(gtx) {
     87 		notify.Cancel()
     88 	}
     89 
     90 	if notifyButton.Clicked(gtx) {
     91 		notify.Notify(notify.Notification{
     92 			Title:       "title",
     93 			Subtitle:    "subtitle",
     94 			Body:        "body",
     95 			UserData:    map[string]string{"foo": "bar"},
     96 			Attachments: []string{"icon.jpg"},
     97 			Category:    "best category eva",
     98 		})
     99 	}
    100 
    101 	if notifyAdhocButton.Clicked(gtx) {
    102 		notify.Notify(notify.Notification{
    103 			Title:    "title",
    104 			Subtitle: "subtitle",
    105 			Body:     "body",
    106 			Actions: []notify.Action{
    107 				{
    108 					ID:    "buy",
    109 					Title: "Buy",
    110 				},
    111 				{
    112 					ID:    "sell",
    113 					Title: "Sell",
    114 				},
    115 			},
    116 		})
    117 	}
    118 
    119 	return layout.Center.Layout(gtx, func(gtx C) D {
    120 		return layout.Flex{}.Layout(gtx,
    121 			layout.Rigid(func(gtx C) D {
    122 				return material.Button(th, &notifyButton, "notify").Layout(gtx)
    123 			}),
    124 			layout.Rigid(layout.Spacer{Width: 8}.Layout),
    125 			layout.Rigid(func(gtx C) D {
    126 				return material.Button(th, &notifyAdhocButton, "adhoc").Layout(gtx)
    127 			}),
    128 			layout.Rigid(layout.Spacer{Width: 8}.Layout),
    129 			layout.Rigid(func(gtx C) D {
    130 				return material.Button(th, &cancelButton, "cancel").Layout(gtx)
    131 			}),
    132 		)
    133 	})
    134 }