From a34b3365b4d3f9fcdb61704f62ea5080c060599f Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 30 May 2021 00:48:33 +0200 Subject: [PATCH] Let Update return an optional error and handle it during widget updates --- deck.go | 4 +++- widget.go | 2 +- widget_button.go | 12 ++++++------ widget_recent_window.go | 9 +++------ widget_time.go | 11 ++++------- widget_top.go | 9 +++------ 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/deck.go b/deck.go index ccc7db6..9f7086f 100644 --- a/deck.go +++ b/deck.go @@ -170,6 +170,8 @@ func (d *Deck) triggerAction(index uint8, hold bool) { // updateWidgets updates/repaints all the widgets. func (d *Deck) updateWidgets() { for _, w := range d.Widgets { - w.Update(&dev) + if err := w.Update(&dev); err != nil { + log.Fatalf("error: %v", err) + } } } diff --git a/widget.go b/widget.go index d129a9c..f28d5a8 100644 --- a/widget.go +++ b/widget.go @@ -16,7 +16,7 @@ import ( type Widget interface { Key() uint8 - Update(dev *streamdeck.Device) + Update(dev *streamdeck.Device) error Action() *ActionConfig ActionHold() *ActionConfig TriggerAction() diff --git a/widget_button.go b/widget_button.go index 870df21..a992109 100644 --- a/widget_button.go +++ b/widget_button.go @@ -2,7 +2,6 @@ package main import ( "image" - "log" "sync" "github.com/muesli/streamdeck" @@ -17,7 +16,9 @@ type ButtonWidget struct { init sync.Once } -func (w *ButtonWidget) Update(dev *streamdeck.Device) { +func (w *ButtonWidget) Update(dev *streamdeck.Device) error { + var err error + w.init.Do(func() { size := int(dev.Pixels) margin := size / 18 @@ -42,9 +43,8 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) { _ = drawImage(img, w.icon, height, image.Pt(-1, -1)) } - err := dev.SetImage(w.key, img) - if err != nil { - log.Fatal(err) - } + err = dev.SetImage(w.key, img) }) + + return err } diff --git a/widget_recent_window.go b/widget_recent_window.go index 062477c..0ca4634 100644 --- a/widget_recent_window.go +++ b/widget_recent_window.go @@ -16,13 +16,13 @@ type RecentWindowWidget struct { lastClass string } -func (w *RecentWindowWidget) Update(dev *streamdeck.Device) { +func (w *RecentWindowWidget) Update(dev *streamdeck.Device) error { img := image.NewRGBA(image.Rect(0, 0, int(dev.Pixels), int(dev.Pixels))) size := int(dev.Pixels) if int(w.window) < len(recentWindows) { if w.lastClass == recentWindows[w.window].Class { - return + return nil } w.lastClass = recentWindows[w.window].Class @@ -30,10 +30,7 @@ func (w *RecentWindowWidget) Update(dev *streamdeck.Device) { draw.Draw(img, image.Rect(4, 4, size-4, size-4), icon, image.Point{0, 0}, draw.Src) } - err := dev.SetImage(w.key, img) - if err != nil { - log.Fatal(err) - } + return dev.SetImage(w.key, img) } func (w *RecentWindowWidget) TriggerAction() { diff --git a/widget_time.go b/widget_time.go index 10f46de..11bfe27 100644 --- a/widget_time.go +++ b/widget_time.go @@ -1,8 +1,8 @@ package main import ( + "fmt" "image" - "log" "strings" "time" @@ -41,7 +41,7 @@ func formatTime(t time.Time, format string) string { return t.Format(format) } -func (w *TimeWidget) Update(dev *streamdeck.Device) { +func (w *TimeWidget) Update(dev *streamdeck.Device) error { size := int(dev.Pixels) margin := size / 18 height := size - (margin * 2) @@ -51,7 +51,7 @@ func (w *TimeWidget) Update(dev *streamdeck.Device) { fonts := strings.Split(w.font, ";") if len(formats) == 0 { - return + return fmt.Errorf("no time format supplied") } for len(fonts) < len(formats) { fonts = append(fonts, "regular") @@ -70,8 +70,5 @@ func (w *TimeWidget) Update(dev *streamdeck.Device) { image.Pt(-1, -1)) } - err := dev.SetImage(w.key, img) - if err != nil { - log.Fatal(err) - } + return dev.SetImage(w.key, img) } diff --git a/widget_top.go b/widget_top.go index 7823c6d..954ea94 100644 --- a/widget_top.go +++ b/widget_top.go @@ -21,7 +21,7 @@ type TopWidget struct { lastValue float64 } -func (w *TopWidget) Update(dev *streamdeck.Device) { +func (w *TopWidget) Update(dev *streamdeck.Device) error { var value float64 var label string @@ -48,7 +48,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) { } if w.lastValue == value { - return + return nil } w.lastValue = value @@ -98,8 +98,5 @@ func (w *TopWidget) Update(dev *streamdeck.Device) { -1, image.Pt(-1, -1)) - err = dev.SetImage(w.key, img) - if err != nil { - log.Fatal(err) - } + return dev.SetImage(w.key, img) }