Skip to content

Commit 5e9cd17

Browse files
committed
Support drawing images from a buffer
1 parent 5a21195 commit 5e9cd17

File tree

5 files changed

+45
-59
lines changed

5 files changed

+45
-59
lines changed

deck.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/atotto/clipboard"
1616
"github.com/godbus/dbus"
17-
"github.com/mitchellh/go-homedir"
1817
"github.com/muesli/streamdeck"
1918
)
2019

@@ -27,29 +26,25 @@ type Deck struct {
2726

2827
// LoadDeck loads a deck configuration.
2928
func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
30-
exp, err := homedir.Expand(deck)
29+
path, err := expandPath(base, deck)
3130
if err != nil {
3231
return nil, err
3332
}
34-
if !filepath.IsAbs(exp) {
35-
exp = filepath.Join(base, exp)
36-
}
37-
abs, err := filepath.Abs(exp)
38-
if err != nil {
39-
return nil, err
40-
}
41-
log.Println("Loading deck:", abs)
33+
log.Println("Loading deck:", path)
4234

43-
dc, err := LoadConfig(abs)
35+
dc, err := LoadConfig(path)
4436
if err != nil {
4537
return nil, err
4638
}
4739

4840
d := Deck{
49-
File: abs,
41+
File: path,
5042
}
5143
if dc.Background != "" {
52-
bgpath := findImage(filepath.Dir(abs), dc.Background)
44+
bgpath, err := expandPath(filepath.Dir(path), dc.Background)
45+
if err != nil {
46+
return nil, err
47+
}
5348
if err := d.loadBackground(dev, bgpath); err != nil {
5449
return nil, err
5550
}
@@ -65,12 +60,12 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
6560

6661
var w Widget
6762
if k, found := keyMap[i]; found {
68-
w, err = NewWidget(k, bg)
63+
w, err = NewWidget(filepath.Dir(path), k, bg)
6964
if err != nil {
7065
return nil, err
7166
}
7267
} else {
73-
w = NewBaseWidget(i, nil, nil, bg)
68+
w = NewBaseWidget(filepath.Dir(path), i, nil, nil, bg)
7469
}
7570

7671
d.Widgets = append(d.Widgets, w)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240
1717
github.com/jezek/xgbutil v0.0.0-20210302171758-530099784e66
1818
github.com/lucasb-eyer/go-colorful v1.2.0
19-
github.com/mitchellh/go-homedir v1.1.0 // indirect
19+
github.com/mitchellh/go-homedir v1.1.0
2020
github.com/muesli/streamdeck v0.2.0
2121
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
2222
github.com/shirou/gopsutil v2.18.12+incompatible

images.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

widget.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Widget interface {
2727

2828
// BaseWidget provides common functionality required by all widgets.
2929
type BaseWidget struct {
30+
base string
3031
key uint8
3132
action *ActionConfig
3233
actionHold *ActionConfig
@@ -72,8 +73,9 @@ func (w *BaseWidget) Update(dev *streamdeck.Device) error {
7273
}
7374

7475
// NewBaseWidget returns a new BaseWidget.
75-
func NewBaseWidget(index uint8, action, actionHold *ActionConfig, bg image.Image) *BaseWidget {
76+
func NewBaseWidget(base string, index uint8, action, actionHold *ActionConfig, bg image.Image) *BaseWidget {
7677
return &BaseWidget{
78+
base: base,
7779
key: index,
7880
action: action,
7981
actionHold: actionHold,
@@ -82,8 +84,8 @@ func NewBaseWidget(index uint8, action, actionHold *ActionConfig, bg image.Image
8284
}
8385

8486
// NewWidget initializes a widget.
85-
func NewWidget(kc KeyConfig, bg image.Image) (Widget, error) {
86-
bw := NewBaseWidget(kc.Index, kc.Action, kc.ActionHold, bg)
87+
func NewWidget(base string, kc KeyConfig, bg image.Image) (Widget, error) {
88+
bw := NewBaseWidget(base, kc.Index, kc.Action, kc.ActionHold, bg)
8789

8890
switch kc.Widget.ID {
8991
case "button":
@@ -140,18 +142,18 @@ func (w *BaseWidget) setInterval(interval uint, defaultInterval uint) {
140142
w.interval = interval
141143
}
142144

143-
func drawImage(img *image.RGBA, path string, size int, pt image.Point) error {
145+
func loadImage(path string) (image.Image, error) {
144146
f, err := os.Open(path)
145147
if err != nil {
146-
return err
148+
return nil, err
147149
}
148150
defer f.Close() //nolint:errcheck
149151

150152
icon, _, err := image.Decode(f)
151-
if err != nil {
152-
return err
153-
}
153+
return icon, err
154+
}
154155

156+
func drawImage(img *image.RGBA, icon image.Image, size int, pt image.Point) error {
155157
if pt.X < 0 {
156158
xcenter := float64(img.Bounds().Dx()/2.0) - (float64(size) / 2.0)
157159
pt = image.Pt(int(xcenter), pt.Y)

widget_button.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package main
22

33
import (
44
"image"
5-
"path/filepath"
65

76
"github.com/muesli/streamdeck"
87
)
98

109
// ButtonWidget is a simple widget displaying an icon and/or label.
1110
type ButtonWidget struct {
1211
BaseWidget
13-
icon string
12+
13+
icon image.Image
1414
label string
1515
fontsize float64
1616
}
@@ -20,17 +20,29 @@ func NewButtonWidget(bw BaseWidget, opts WidgetConfig) (*ButtonWidget, error) {
2020
bw.setInterval(opts.Interval, 0)
2121

2222
var icon, label string
23-
ConfigValue(opts.Config["icon"], &icon)
24-
ConfigValue(opts.Config["label"], &label)
23+
_ = ConfigValue(opts.Config["icon"], &icon)
24+
_ = ConfigValue(opts.Config["label"], &label)
2525
var fontsize float64
26-
ConfigValue(opts.Config["fontsize"], &fontsize)
26+
_ = ConfigValue(opts.Config["fontsize"], &fontsize)
2727

28-
return &ButtonWidget{
28+
w := &ButtonWidget{
2929
BaseWidget: bw,
30-
icon: icon,
3130
label: label,
3231
fontsize: fontsize,
33-
}, nil
32+
}
33+
34+
if icon != "" {
35+
path, err := expandPath(w.base, icon)
36+
if err != nil {
37+
return nil, err
38+
}
39+
w.icon, err = loadImage(path)
40+
if err != nil {
41+
return nil, err
42+
}
43+
}
44+
45+
return w, nil
3446
}
3547

3648
// Update renders the widget.
@@ -44,9 +56,9 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {
4456
iconsize := int((float64(height) / 3.0) * 2.0)
4557
bounds := img.Bounds()
4658

47-
if w.icon != "" {
59+
if w.icon != nil {
4860
err := drawImage(img,
49-
findImage(filepath.Dir(deck.File), w.icon),
61+
w.icon,
5062
iconsize,
5163
image.Pt(-1, margin))
5264

@@ -65,9 +77,9 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {
6577
dev.DPI,
6678
w.fontsize,
6779
image.Pt(-1, -1))
68-
} else if w.icon != "" {
80+
} else if w.icon != nil {
6981
err := drawImage(img,
70-
findImage(filepath.Dir(deck.File), w.icon),
82+
w.icon,
7183
height,
7284
image.Pt(-1, -1))
7385

0 commit comments

Comments
 (0)