Skip to content

Commit

Permalink
Support drawing images from a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jun 8, 2021
1 parent 5a21195 commit 5e9cd17
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 59 deletions.
25 changes: 10 additions & 15 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/atotto/clipboard"
"github.com/godbus/dbus"
"github.com/mitchellh/go-homedir"
"github.com/muesli/streamdeck"
)

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

// LoadDeck loads a deck configuration.
func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {
exp, err := homedir.Expand(deck)
path, err := expandPath(base, deck)
if err != nil {
return nil, err
}
if !filepath.IsAbs(exp) {
exp = filepath.Join(base, exp)
}
abs, err := filepath.Abs(exp)
if err != nil {
return nil, err
}
log.Println("Loading deck:", abs)
log.Println("Loading deck:", path)

dc, err := LoadConfig(abs)
dc, err := LoadConfig(path)
if err != nil {
return nil, err
}

d := Deck{
File: abs,
File: path,
}
if dc.Background != "" {
bgpath := findImage(filepath.Dir(abs), dc.Background)
bgpath, err := expandPath(filepath.Dir(path), dc.Background)
if err != nil {
return nil, err
}
if err := d.loadBackground(dev, bgpath); err != nil {
return nil, err
}
Expand All @@ -65,12 +60,12 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) {

var w Widget
if k, found := keyMap[i]; found {
w, err = NewWidget(k, bg)
w, err = NewWidget(filepath.Dir(path), k, bg)
if err != nil {
return nil, err
}
} else {
w = NewBaseWidget(i, nil, nil, bg)
w = NewBaseWidget(filepath.Dir(path), i, nil, nil, bg)
}

d.Widgets = append(d.Widgets, w)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240
github.com/jezek/xgbutil v0.0.0-20210302171758-530099784e66
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/streamdeck v0.2.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/shirou/gopsutil v2.18.12+incompatible
Expand Down
23 changes: 0 additions & 23 deletions images.go

This file was deleted.

18 changes: 10 additions & 8 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Widget interface {

// BaseWidget provides common functionality required by all widgets.
type BaseWidget struct {
base string
key uint8
action *ActionConfig
actionHold *ActionConfig
Expand Down Expand Up @@ -72,8 +73,9 @@ func (w *BaseWidget) Update(dev *streamdeck.Device) error {
}

// NewBaseWidget returns a new BaseWidget.
func NewBaseWidget(index uint8, action, actionHold *ActionConfig, bg image.Image) *BaseWidget {
func NewBaseWidget(base string, index uint8, action, actionHold *ActionConfig, bg image.Image) *BaseWidget {
return &BaseWidget{
base: base,
key: index,
action: action,
actionHold: actionHold,
Expand All @@ -82,8 +84,8 @@ func NewBaseWidget(index uint8, action, actionHold *ActionConfig, bg image.Image
}

// NewWidget initializes a widget.
func NewWidget(kc KeyConfig, bg image.Image) (Widget, error) {
bw := NewBaseWidget(kc.Index, kc.Action, kc.ActionHold, bg)
func NewWidget(base string, kc KeyConfig, bg image.Image) (Widget, error) {
bw := NewBaseWidget(base, kc.Index, kc.Action, kc.ActionHold, bg)

switch kc.Widget.ID {
case "button":
Expand Down Expand Up @@ -140,18 +142,18 @@ func (w *BaseWidget) setInterval(interval uint, defaultInterval uint) {
w.interval = interval
}

func drawImage(img *image.RGBA, path string, size int, pt image.Point) error {
func loadImage(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return err
return nil, err
}
defer f.Close() //nolint:errcheck

icon, _, err := image.Decode(f)
if err != nil {
return err
}
return icon, err
}

func drawImage(img *image.RGBA, icon image.Image, size int, pt image.Point) error {
if pt.X < 0 {
xcenter := float64(img.Bounds().Dx()/2.0) - (float64(size) / 2.0)
pt = image.Pt(int(xcenter), pt.Y)
Expand Down
36 changes: 24 additions & 12 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package main

import (
"image"
"path/filepath"

"github.com/muesli/streamdeck"
)

// ButtonWidget is a simple widget displaying an icon and/or label.
type ButtonWidget struct {
BaseWidget
icon string

icon image.Image
label string
fontsize float64
}
Expand All @@ -20,17 +20,29 @@ func NewButtonWidget(bw BaseWidget, opts WidgetConfig) (*ButtonWidget, error) {
bw.setInterval(opts.Interval, 0)

var icon, label string
ConfigValue(opts.Config["icon"], &icon)
ConfigValue(opts.Config["label"], &label)
_ = ConfigValue(opts.Config["icon"], &icon)
_ = ConfigValue(opts.Config["label"], &label)
var fontsize float64
ConfigValue(opts.Config["fontsize"], &fontsize)
_ = ConfigValue(opts.Config["fontsize"], &fontsize)

return &ButtonWidget{
w := &ButtonWidget{
BaseWidget: bw,
icon: icon,
label: label,
fontsize: fontsize,
}, nil
}

if icon != "" {
path, err := expandPath(w.base, icon)
if err != nil {
return nil, err
}
w.icon, err = loadImage(path)
if err != nil {
return nil, err
}
}

return w, nil
}

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

if w.icon != "" {
if w.icon != nil {
err := drawImage(img,
findImage(filepath.Dir(deck.File), w.icon),
w.icon,
iconsize,
image.Pt(-1, margin))

Expand All @@ -65,9 +77,9 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {
dev.DPI,
w.fontsize,
image.Pt(-1, -1))
} else if w.icon != "" {
} else if w.icon != nil {
err := drawImage(img,
findImage(filepath.Dir(deck.File), w.icon),
w.icon,
height,
image.Pt(-1, -1))

Expand Down

0 comments on commit 5e9cd17

Please sign in to comment.