Skip to content

Commit

Permalink
Only update widgets when content has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Feb 23, 2020
1 parent bbaadba commit c3d97f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
27 changes: 16 additions & 11 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"image"
"log"
"sync"

"github.com/golang/freetype"
"github.com/muesli/streamdeck"
Expand All @@ -12,19 +13,23 @@ type ButtonWidget struct {
BaseWidget
icon string
label string

init sync.Once
}

func (w *ButtonWidget) Update(dev *streamdeck.Device) {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))
if w.label != "" {
drawImage(img, w.icon, 48, 12, 4)
drawString(img, ttfFont, w.label, 8, freetype.Pt(-1, img.Bounds().Dx()-6))
} else {
drawImage(img, w.icon, 64, 4, 4)
}
w.init.Do(func() {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))
if w.label != "" {
_ = drawImage(img, w.icon, 48, 12, 4)
drawString(img, ttfFont, w.label, 8, freetype.Pt(-1, img.Bounds().Dx()-6))
} else {
_ = drawImage(img, w.icon, 64, 4, 4)
}

err := dev.SetImage(w.key, img)
if err != nil {
log.Fatal(err)
}
err := dev.SetImage(w.key, img)
if err != nil {
log.Fatal(err)
}
})
}
7 changes: 7 additions & 0 deletions widget_recent_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import (
type RecentWindowWidget struct {
BaseWidget
window uint8

lastClass string
}

func (w *RecentWindowWidget) Update(dev *streamdeck.Device) {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))

if int(w.window) < len(recentWindows) {
if w.lastClass == recentWindows[w.window].Class {
return
}
w.lastClass = recentWindows[w.window].Class

icon := resize.Resize(64, 64, recentWindows[w.window].Icon, resize.Bilinear)
draw.Draw(img, image.Rect(4, 4, 68, 68), icon, image.Point{0, 0}, draw.Src)
}
Expand Down
28 changes: 16 additions & 12 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ type TopWidget struct {
BaseWidget
mode string
fillColor string

lastValue float64
}

func (w *TopWidget) Update(dev *streamdeck.Device) {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))

var value float64
var label string
var fill colorful.Color

fill, err := colorful.Hex(w.fillColor)
if err != nil {
panic("Invalid color: " + w.fillColor)
}

switch w.mode {
case "cpu":
cpuUsage, err := cpu.Percent(0, false)
Expand All @@ -54,18 +47,29 @@ func (w *TopWidget) Update(dev *streamdeck.Device) {
panic("Unknown widget mode: " + w.mode)
}

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

fill, err := colorful.Hex(w.fillColor)
if err != nil {
panic("Invalid color: " + w.fillColor)
}

img := image.NewRGBA(image.Rect(0, 0, 72, 72))
draw.Draw(img,
image.Rect(12, 6, 60, 54),
&image.Uniform{color.RGBA{255, 255, 255, 255}},
image.ZP, draw.Src)
image.Point{}, draw.Src)
draw.Draw(img,
image.Rect(13, 7, 59, 53),
&image.Uniform{color.RGBA{0, 0, 0, 255}},
image.ZP, draw.Src)
image.Point{}, draw.Src)
draw.Draw(img,
image.Rect(14, 7+int(46*(1-value/100)), 58, 53),
&image.Uniform{fill},
image.ZP, draw.Src)
image.Point{}, draw.Src)

drawString(img, ttfFont, strconv.FormatInt(int64(value), 10), 12, freetype.Pt(-1, -1))
drawString(img, ttfFont, "% "+label, 7, freetype.Pt(-1, img.Bounds().Dx()-4))
Expand Down

0 comments on commit c3d97f8

Please sign in to comment.