From 6c7ec2a44a8063470b1305864ce988447248ddc2 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 27 May 2021 02:59:27 +0200 Subject: [PATCH] Make widgets resolution independent Fixes #1 and #19. --- widget_button.go | 21 ++++++++++++++++----- widget_clock.go | 26 ++++++++++++++++++++----- widget_date.go | 26 ++++++++++++++++++++----- widget_recent_window.go | 7 ++++--- widget_top.go | 42 ++++++++++++++++++++++++++++++++--------- 5 files changed, 95 insertions(+), 27 deletions(-) diff --git a/widget_button.go b/widget_button.go index 0823b50..72b72f5 100644 --- a/widget_button.go +++ b/widget_button.go @@ -5,7 +5,6 @@ import ( "log" "sync" - "github.com/golang/freetype" "github.com/muesli/streamdeck" ) @@ -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) diff --git a/widget_clock.go b/widget_clock.go index 23ecf1d..4ff366a 100644 --- a/widget_clock.go +++ b/widget_clock.go @@ -5,7 +5,6 @@ import ( "log" "time" - "github.com/golang/freetype" "github.com/muesli/streamdeck" ) @@ -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 { diff --git a/widget_date.go b/widget_date.go index 12513ea..dd5f854 100644 --- a/widget_date.go +++ b/widget_date.go @@ -6,7 +6,6 @@ import ( "strconv" "time" - "github.com/golang/freetype" "github.com/muesli/streamdeck" ) @@ -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 { diff --git a/widget_recent_window.go b/widget_recent_window.go index 3c474e6..de8e4f6 100644 --- a/widget_recent_window.go +++ b/widget_recent_window.go @@ -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) diff --git a/widget_top.go b/widget_top.go index e8f4970..ab0e5bf 100644 --- a/widget_top.go +++ b/widget_top.go @@ -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" @@ -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: @@ -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 {