Skip to content

Commit

Permalink
Let Update return an optional error and handle it during widget updates
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed May 29, 2021
1 parent 7a48fb5 commit a34b336
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 27 deletions.
4 changes: 3 additions & 1 deletion deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type Widget interface {
Key() uint8
Update(dev *streamdeck.Device)
Update(dev *streamdeck.Device) error
Action() *ActionConfig
ActionHold() *ActionConfig
TriggerAction()
Expand Down
12 changes: 6 additions & 6 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"image"
"log"
"sync"

"github.com/muesli/streamdeck"
Expand All @@ -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
Expand All @@ -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
}
9 changes: 3 additions & 6 deletions widget_recent_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ 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

icon := resize.Resize(uint(size-8), uint(size-8), recentWindows[w.window].Icon, resize.Bilinear)
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() {
Expand Down
11 changes: 4 additions & 7 deletions widget_time.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"image"
"log"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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)
}
9 changes: 3 additions & 6 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -48,7 +48,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) {
}

if w.lastValue == value {
return
return nil
}
w.lastValue = value

Expand Down Expand Up @@ -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)
}

0 comments on commit a34b336

Please sign in to comment.