Skip to content

Commit

Permalink
Improve time widget rendering
Browse files Browse the repository at this point in the history
- Support multiple placeholders when formatting time string
- Automatically pick matching font size
  • Loading branch information
muesli committed May 29, 2021
1 parent bf18f43 commit 3e0e8cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 64 deletions.
6 changes: 3 additions & 3 deletions decks/main.deck
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
[keys.widget]
id = "time"
[keys.widget.config]
format = "dd;mmm;yyyy"
font = "bold;regular;thin"
format = "%l;%d;%M"
font = "regular;bold;regular"

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

[[keys]]
Expand Down
6 changes: 3 additions & 3 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionC
case "clock":
return &TimeWidget{
BaseWidget: bw,
format: "HH;MM;SS",
format: "%H;%i;%s",
font: "bold;regular;thin",
}

case "date":
return &TimeWidget{
BaseWidget: bw,
format: "dd;mmm;yyyy",
font: "bold;regular;thin",
format: "%l;%d;%M",
font: "regular;bold;regular",
}

case "time":
Expand Down
88 changes: 30 additions & 58 deletions widget_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,49 @@ package main
import (
"image"
"log"
"time"
"strings"
"time"

"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 formatTime(t time.Time, format string) string {
tm := map[string]string{
"%Y": "2006",
"%y": "06",
"%F": "January",
"%M": "Jan",
"%m": "01",
"%l": "Monday",
"%D": "Mon",
"%d": "02",
"%h": "03",
"%H": "15",
"%i": "04",
"%s": "05",
"%a": "PM",
"%t": "MST",
}
}

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)
for k, v := range tm {
format = strings.ReplaceAll(format, k, v)
}

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))
margin := size / 18
height := size - (margin * 2)
img := image.NewRGBA(image.Rect(0, 0, size, size))

formats := strings.Split(w.format, ";")
fonts := strings.Split(w.font, ";")
Expand All @@ -83,18 +57,16 @@ func (w *TimeWidget) Update(dev *streamdeck.Device) {
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)
for i := 0; i < len(formats); i++ {
str := formatTime(time.Now(), formats[i])
font := fontByName(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,
-1,
image.Pt(-1, -1))
}

Expand Down

0 comments on commit 3e0e8cc

Please sign in to comment.