Skip to content

Commit

Permalink
Make widgets resolution independent
Browse files Browse the repository at this point in the history
Fixes #1 and #19.
  • Loading branch information
muesli committed May 27, 2021
1 parent 9acdc9d commit 6c7ec2a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 27 deletions.
21 changes: 16 additions & 5 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"sync"

"github.com/golang/freetype"
"github.com/muesli/streamdeck"
)

Expand All @@ -19,12 +18,24 @@ type ButtonWidget struct {

func (w *ButtonWidget) Update(dev *streamdeck.Device) {
w.init.Do(func() {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))
const margin = 4

img := image.NewRGBA(image.Rect(0, 0, int(dev.Pixels), int(dev.Pixels)))
if w.label != "" {
_ = drawImage(img, w.icon, 48, 12, 4)
drawString(img, ttfFont, w.label, 8, freetype.Pt(-1, img.Bounds().Dx()-6))
size := int((float64(dev.Pixels-margin*2) / 3.0) * 2.0)
_ = drawImage(img, w.icon, size, image.Pt(-1, margin))

pt := (float64(dev.Pixels) / 3.0) * 42.0 / float64(dev.DPI)
bounds := img.Bounds()
bounds.Min.Y += size + margin/2
drawString(img,
bounds,
ttfFont,
w.label,
pt,
image.Pt(-1, -1))
} else {
_ = drawImage(img, w.icon, 64, 4, 4)
_ = drawImage(img, w.icon, 64, image.Pt(-1, -1))
}

err := dev.SetImage(w.key, img)
Expand Down
26 changes: 21 additions & 5 deletions widget_clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"time"

"github.com/golang/freetype"
"github.com/muesli/streamdeck"
)

Expand All @@ -14,15 +13,32 @@ type ClockWidget struct {
}

func (w *ClockWidget) Update(dev *streamdeck.Device) {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))
const margin = 4
size := int(dev.Pixels)
img := image.NewRGBA(image.Rect(0, 0, size, size))
height := size - (margin * 2)
pt := (float64(height) / 3.0) * 72.0 / float64(dev.DPI)

t := time.Now()
hour := t.Format("15")
min := t.Format("04")
sec := t.Format("05")
drawString(img, ttfBoldFont, hour, 13, freetype.Pt(-1, 20))
drawString(img, ttfFont, min, 13, freetype.Pt(-1, 43))
drawString(img, ttfThinFont, sec, 13, freetype.Pt(-1, 66))

drawString(img, image.Rectangle{image.Pt(0, margin), image.Pt(size, margin+(height/3))},
ttfBoldFont,
hour,
pt,
image.Pt(-1, -1))
drawString(img, image.Rectangle{image.Pt(0, (height/3)+margin), image.Pt(size, margin+(height/3)*2)},
ttfFont,
min,
pt,
image.Pt(-1, -1))
drawString(img, image.Rectangle{image.Pt(0, (height/3)*2+margin), image.Pt(size, margin+height)},
ttfThinFont,
sec,
pt,
image.Pt(-1, -1))

err := dev.SetImage(w.key, img)
if err != nil {
Expand Down
26 changes: 21 additions & 5 deletions widget_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"
"time"

"github.com/golang/freetype"
"github.com/muesli/streamdeck"
)

Expand All @@ -15,15 +14,32 @@ type DateWidget struct {
}

func (w *DateWidget) Update(dev *streamdeck.Device) {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))
const margin = 4
size := int(dev.Pixels)
img := image.NewRGBA(image.Rect(0, 0, size, size))
height := size - (margin * 2)
pt := (float64(height) / 3.0) * 72.0 / float64(dev.DPI)

t := time.Now()
day := t.Day()
month := t.Month().String()
year := t.Year()
drawString(img, ttfBoldFont, strconv.Itoa(day), 13, freetype.Pt(-1, 20))
drawString(img, ttfFont, month, 13, freetype.Pt(-1, 43))
drawString(img, ttfThinFont, strconv.Itoa(year), 13, freetype.Pt(-1, 66))

drawString(img, image.Rectangle{image.Pt(0, margin), image.Pt(size, margin+(height/3))},
ttfBoldFont,
strconv.Itoa(day),
pt,
image.Pt(-1, -1))
drawString(img, image.Rectangle{image.Pt(0, (height/3)+margin), image.Pt(size, margin+(height/3)*2)},
ttfFont,
month,
pt,
image.Pt(-1, -1))
drawString(img, image.Rectangle{image.Pt(0, (height/3)*2+margin), image.Pt(size, margin+height)},
ttfThinFont,
strconv.Itoa(year),
pt,
image.Pt(-1, -1))

err := dev.SetImage(w.key, img)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions widget_recent_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ type RecentWindowWidget struct {
}

func (w *RecentWindowWidget) Update(dev *streamdeck.Device) {
img := image.NewRGBA(image.Rect(0, 0, 72, 72))
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
}
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)
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)
Expand Down
42 changes: 33 additions & 9 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"strconv"

"github.com/golang/freetype"
colorful "github.com/lucasb-eyer/go-colorful"
"github.com/muesli/streamdeck"
"github.com/shirou/gopsutil/cpu"
Expand Down Expand Up @@ -36,11 +35,11 @@ func (w *TopWidget) Update(dev *streamdeck.Device) {
label = "CPU"

case "memory":
mem, err := mem.VirtualMemory()
memory, err := mem.VirtualMemory()
if err != nil {
log.Fatal(err)
}
value = mem.UsedPercent
value = memory.UsedPercent
label = "MEM"

default:
Expand All @@ -57,22 +56,47 @@ func (w *TopWidget) Update(dev *streamdeck.Device) {
panic("Invalid color: " + w.fillColor)
}

img := image.NewRGBA(image.Rect(0, 0, 72, 72))
size := int(dev.Pixels)
pt := (float64(size) / 3.0) * 66.0 / float64(dev.DPI)
ptSmall := (float64(size) / 3.0) * 40.0 / float64(dev.DPI)

img := image.NewRGBA(image.Rect(0, 0, size, size))

draw.Draw(img,
image.Rect(12, 6, 60, 54),
image.Rect(12, 6, size-12, size-18),
&image.Uniform{color.RGBA{255, 255, 255, 255}},
image.Point{}, draw.Src)
draw.Draw(img,
image.Rect(13, 7, 59, 53),
image.Rect(13, 7, size-14, size-20),
&image.Uniform{color.RGBA{0, 0, 0, 255}},
image.Point{}, draw.Src)
draw.Draw(img,
image.Rect(14, 7+int(46*(1-value/100)), 58, 53),
image.Rect(14, 7+int(float64(size-26)*(1-value/100)), size-15, size-21),
&image.Uniform{fill},
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))
// draw percentage
bounds := img.Bounds()
bounds.Min.Y = 6
bounds.Max.Y -= 18

drawString(img,
bounds,
ttfFont,
strconv.FormatInt(int64(value), 10),
pt,
image.Pt(-1, -1))

// draw description
bounds = img.Bounds()
bounds.Min.Y = size - 18

drawString(img,
bounds,
ttfFont,
"% "+label,
ptSmall,
image.Pt(-1, -1))

err = dev.SetImage(w.key, img)
if err != nil {
Expand Down

0 comments on commit 6c7ec2a

Please sign in to comment.