Skip to content

Commit

Permalink
Support showing window title below recent window icons
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jun 8, 2021
1 parent ec3a10c commit ec4edf8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions decks/main.deck
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,36 @@
id = "recentWindow"
[keys.widget.config]
window = 1
showTitle = true

[[keys]]
index = 11
[keys.widget]
id = "recentWindow"
[keys.widget.config]
window = 2
showTitle = true

[[keys]]
index = 12
[keys.widget]
id = "recentWindow"
[keys.widget.config]
window = 3
showTitle = true

[[keys]]
index = 13
[keys.widget]
id = "recentWindow"
[keys.widget.config]
window = 4
showTitle = true

[[keys]]
index = 14
[keys.widget]
id = "recentWindow"
[keys.widget.config]
window = 5
showTitle = true
34 changes: 24 additions & 10 deletions widget_recent_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package main

import (
"image"
"image/draw"
"log"

"github.com/muesli/streamdeck"
"github.com/nfnt/resize"
)

// RecentWindowWidget is a widget displaying a recently activated window.
type RecentWindowWidget struct {
BaseWidget
window uint8

lastClass string
window uint8
showTitle bool

lastID uint32
}

// NewRecentWindowWidget returns a new RecentWindowWidget.
Expand All @@ -23,17 +23,20 @@ func NewRecentWindowWidget(bw BaseWidget, opts WidgetConfig) (*RecentWindowWidge
if err := ConfigValue(opts.Config["window"], &window); err != nil {
return nil, err
}
var showTitle bool
_ = ConfigValue(opts.Config["showTitle"], &showTitle)

return &RecentWindowWidget{
BaseWidget: bw,
window: uint8(window),
showTitle: showTitle,
}, nil
}

// RequiresUpdate returns true when the widget wants to be repainted.
func (w *RecentWindowWidget) RequiresUpdate() bool {
if int(w.window) < len(recentWindows) {
return w.lastClass != recentWindows[w.window].Class
return w.lastID != recentWindows[w.window].ID
}

return w.BaseWidget.RequiresUpdate()
Expand All @@ -43,15 +46,26 @@ func (w *RecentWindowWidget) RequiresUpdate() bool {
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 {
if w.lastID == recentWindows[w.window].ID {
return nil
}
w.lastClass = recentWindows[w.window].Class
w.lastID = recentWindows[w.window].ID

var name string
if w.showTitle {
name = recentWindows[w.window].Name
if len(name) > 10 {
name = name[:10]
}
}

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)
bw := ButtonWidget{
BaseWidget: w.BaseWidget,
icon: recentWindows[w.window].Icon,
label: name,
}
return bw.Update(dev)
}

return w.render(dev, img)
Expand Down

0 comments on commit ec4edf8

Please sign in to comment.