Skip to content

Commit

Permalink
Add time widget with variable format that replaces the clock and date…
Browse files Browse the repository at this point in the history
… widgets
  • Loading branch information
zMoooooritz authored and muesli committed May 29, 2021
1 parent aca4e2c commit d4d3f5f
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 126 deletions.
10 changes: 8 additions & 2 deletions decks/main.deck
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
[[keys]]
index = 3
[keys.widget]
id = "date"
id = "time"
[keys.widget.config]
format = "dd;mmm;yyyy"
font = "bold;regular;thin"

[[keys]]
index = 4
[keys.widget]
id = "clock"
id = "time"
[keys.widget.config]
format = "HH;MM;SS"
font = "bold;regular;thin"

[[keys]]
index = 5
Expand Down
10 changes: 4 additions & 6 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionC
label: config["label"],
}

case "clock":
return &ClockWidget{
case "time":
return &TimeWidget{
BaseWidget: bw,
mode: config["mode"],
format: config["format"],
font: config["font"],
}

case "date":
return &DateWidget{bw}

case "recentWindow":
i, err := strconv.ParseUint(config["window"], 10, 64)
if err != nil {
Expand Down
70 changes: 0 additions & 70 deletions widget_clock.go

This file was deleted.

48 changes: 0 additions & 48 deletions widget_date.go

This file was deleted.

105 changes: 105 additions & 0 deletions widget_time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"image"
"log"
"time"
"strings"

"github.com/muesli/streamdeck"
"github.com/golang/freetype/truetype"
)

type TimeWidget struct {
BaseWidget
format string
font string
}

func mapToFont(font string) *truetype.Font {
switch font {
case "thin":
return ttfThinFont
case "regular":
return ttfFont
case "bold":
return ttfBoldFont
default:
return ttfFont
}
}

func mapToTimeString(format string) string {
t := time.Now()
switch format {
case "yyyy":
return t.Format("2006")
case "yy":
return t.Format("06")
case "mmmm":
return t.Format("January")
case "mmm":
return t.Format("Jan")
case "mm":
return t.Format("01")
case "dddd":
return t.Format("Monday")
case "ddd":
return t.Format("Mon")
case "dd":
return t.Format("02")
case "HHT":
return t.Format("03")
case "HH", "hour":
return t.Format("15")
case "MM", "min":
return t.Format("04")
case "ss", "SS", "sec":
return t.Format("05")
case "tt":
return t.Format("PM")
case "Z", "ZZZ":
return t.Format("MST")
case "o":
return t.Format("Z07:00")
default:
return t.Format(format)
}
}

func (w *TimeWidget) Update(dev *streamdeck.Device) {
const margin = 4
size := int(dev.Pixels)
img := image.NewRGBA(image.Rect(0, 0, size, size))
height := size - (margin * 2)

formats := strings.Split(w.format, ";")
fonts := strings.Split(w.font, ";")

if len(formats) == 0 {
return
}
for len(fonts) < len(formats) {
fonts = append(fonts, "regular")
}

pt := (float64(height) / float64(len(formats))) * 72.0 / float64(dev.DPI)

for i:=0; i<len(formats); i++ {
str := mapToTimeString(formats[i])
font := mapToFont(fonts[i])
lower := margin + (height / len(formats)) * i
upper := margin + (height / len(formats)) * (i + 1)

drawString(img, image.Rect(0, lower, size, upper),
font,
str,
pt,
image.Pt(-1, -1))
}

err := dev.SetImage(w.key, img)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit d4d3f5f

Please sign in to comment.