Skip to content

Commit

Permalink
Make Widget.interval a native time.Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jun 10, 2021
1 parent 67b69cd commit c3e8de1
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 8 deletions.
6 changes: 3 additions & 3 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BaseWidget struct {
actionHold *ActionConfig
background image.Image
lastUpdate time.Time
interval uint
interval time.Duration
}

// Key returns the key a widget is mapped to.
Expand All @@ -65,7 +65,7 @@ func (w *BaseWidget) TriggerAction(_ bool) {
func (w *BaseWidget) RequiresUpdate() bool {
if !w.lastUpdate.IsZero() && // initial paint done
(w.interval == 0 || // never to be repainted
time.Since(w.lastUpdate) < time.Duration(w.interval)*time.Millisecond) {
time.Since(w.lastUpdate) < w.interval) {
return false
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func (w *BaseWidget) render(dev *streamdeck.Device, fg image.Image) error {
}

// change the interval a widget gets rendered in.
func (w *BaseWidget) setInterval(interval uint, defaultInterval uint) {
func (w *BaseWidget) setInterval(interval time.Duration, defaultInterval time.Duration) {
if interval == 0 {
interval = defaultInterval
}
Expand Down
3 changes: 2 additions & 1 deletion widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"image"
"image/color"
"time"

"github.com/muesli/streamdeck"
)
Expand All @@ -20,7 +21,7 @@ type ButtonWidget struct {

// NewButtonWidget returns a new ButtonWidget.
func NewButtonWidget(bw *BaseWidget, opts WidgetConfig) (*ButtonWidget, error) {
bw.setInterval(opts.Interval, 0)
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, 0)

var icon, label string
_ = ConfigValue(opts.Config["icon"], &icon)
Expand Down
3 changes: 2 additions & 1 deletion widget_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/color"
"os/exec"
"strings"
"time"

"github.com/muesli/streamdeck"
)
Expand All @@ -21,7 +22,7 @@ type CommandWidget struct {

// NewCommandWidget returns a new CommandWidget.
func NewCommandWidget(bw *BaseWidget, opts WidgetConfig) *CommandWidget {
bw.setInterval(opts.Interval, 1000)
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second)

var command, font string
_ = ConfigValue(opts.Config["command"], &command)
Expand Down
2 changes: 1 addition & 1 deletion widget_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type TimeWidget struct {

// NewTimeWidget returns a new TimeWidget.
func NewTimeWidget(bw *BaseWidget, opts WidgetConfig) *TimeWidget {
bw.setInterval(opts.Interval, 500)
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second/2)

var format, font string
_ = ConfigValue(opts.Config["format"], &format)
Expand Down
3 changes: 2 additions & 1 deletion widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/color"
"image/draw"
"strconv"
"time"

"github.com/muesli/streamdeck"
"github.com/shirou/gopsutil/cpu"
Expand All @@ -25,7 +26,7 @@ type TopWidget struct {

// NewTopWidget returns a new TopWidget.
func NewTopWidget(bw *BaseWidget, opts WidgetConfig) *TopWidget {
bw.setInterval(opts.Interval, 500)
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second/2)

var mode string
_ = ConfigValue(opts.Config["mode"], &mode)
Expand Down
2 changes: 1 addition & 1 deletion widget_weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewWeatherWidget(bw *BaseWidget, opts WidgetConfig) (*WeatherWidget, error)
}
// this needs to be called after NewButtonWidget, otherwise its value gets
// overwritten by it.
bw.setInterval(opts.Interval, 60000)
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Minute)

return &WeatherWidget{
ButtonWidget: widget,
Expand Down

0 comments on commit c3e8de1

Please sign in to comment.