Checkpoint
This commit is contained in:
223 files changed
+14770
-18352
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -0,0 +1,72 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
)
|
||||
|
||||
type base struct {
|
||||
name string
|
||||
widgets []app.Widget
|
||||
visible bool
|
||||
onButtonPress func(app.Context)
|
||||
onButtonLongPress func(app.Context)
|
||||
}
|
||||
|
||||
func (l *base) log() *logrus.Entry {
|
||||
return logrus.WithField("layout", l.name)
|
||||
}
|
||||
|
||||
func (l *base) String() string {
|
||||
return l.name
|
||||
}
|
||||
|
||||
func (l *base) Widgets() []app.Widget {
|
||||
return l.widgets
|
||||
}
|
||||
|
||||
func (l *base) IsVisible() bool {
|
||||
return l.visible
|
||||
}
|
||||
|
||||
func (l *base) Hide(ctx app.Context) {
|
||||
log := l.log()
|
||||
log.Debug("hide")
|
||||
for _, widget := range l.widgets {
|
||||
log.WithField("widget", widget.String()).Debug("hide widget")
|
||||
_ = widget.Hide(ctx)
|
||||
}
|
||||
l.visible = false
|
||||
}
|
||||
|
||||
func (l *base) Show(ctx app.Context) {
|
||||
log := l.log()
|
||||
log.Debug("show")
|
||||
ctx.Display().Clear()
|
||||
for _, widget := range l.widgets {
|
||||
log.WithField("widget", widget.String()).Debug("show widget")
|
||||
_ = widget.Show(ctx)
|
||||
}
|
||||
l.visible = true
|
||||
}
|
||||
|
||||
func (i *base) OnButtonPress(action func(app.Context)) {
|
||||
i.onButtonPress = action
|
||||
}
|
||||
|
||||
func (i *base) OnButtonLongPress(action func(app.Context)) {
|
||||
i.onButtonLongPress = action
|
||||
}
|
||||
|
||||
func (l *base) HandleButtonPress(ctx app.Context) {
|
||||
if l.onButtonPress != nil {
|
||||
l.onButtonPress(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *base) HandleButtonLongPress(ctx app.Context) {
|
||||
if l.onButtonLongPress != nil {
|
||||
l.onButtonLongPress(ctx)
|
||||
}
|
||||
}
|
||||
+228
-25
@@ -3,85 +3,288 @@ package layout
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"periph.io/x/conn/v3/physic"
|
||||
|
||||
"github.com/BeatGlow/audio"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/font"
|
||||
"github.com/BeatGlow/beatglow/app/widget"
|
||||
"github.com/BeatGlow/beatglow/player"
|
||||
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
const (
|
||||
formatTextPlaceholder = "None"
|
||||
// "FLAC@192k "
|
||||
// "FLAC@44.1k"
|
||||
formatTextPlaceholder = "No track "
|
||||
durationTextPlaceholder = "--:-- / --:--"
|
||||
trackTextPlaceholder = "No track playing..."
|
||||
trackTextPlaceholder = "No track playing"
|
||||
iconTextPlaceholder = string(font.IconPhones)
|
||||
)
|
||||
|
||||
type playing struct {
|
||||
base
|
||||
phonesWidget *widget.Text
|
||||
formatRate bool
|
||||
formatText string
|
||||
formatWidget *widget.Text
|
||||
durationText string
|
||||
durationWidget *widget.Text
|
||||
track *player.Track
|
||||
trackText string
|
||||
trackWidget *widget.Text
|
||||
iconText string
|
||||
iconWidget *widget.Text
|
||||
artWidget *widget.Image
|
||||
spectrumWidget *widget.Spectrum
|
||||
}
|
||||
|
||||
const playingWaterfallHeight = 32
|
||||
|
||||
func Playing(ctx app.Context) app.Layout {
|
||||
bounds := ctx.Display().Bounds()
|
||||
styles := app.StylesFor(ctx)
|
||||
layout := &playing{
|
||||
base: base{name: "playing"},
|
||||
phonesWidget: widget.NewText(image.Pt(10, 12)),
|
||||
formatText: formatTextPlaceholder,
|
||||
formatWidget: widget.NewText(image.Pt(2+len(formatTextPlaceholder)*8, 12)),
|
||||
formatWidget: widget.NewText(image.Pt(2+len(formatTextPlaceholder)*9, 12)),
|
||||
durationText: durationTextPlaceholder,
|
||||
durationWidget: widget.NewText(image.Pt(2+len(durationTextPlaceholder)*8, 12)),
|
||||
durationWidget: widget.NewText(image.Pt(2+len(durationTextPlaceholder)*9, 12)),
|
||||
trackText: trackTextPlaceholder,
|
||||
trackWidget: widget.NewText(image.Pt(bounds.Dx()-bounds.Dy(), bounds.Dy()-12-16)),
|
||||
trackWidget: widget.NewText(image.Pt(bounds.Dx()-bounds.Dy(), bounds.Dy()-12-playingWaterfallHeight)),
|
||||
iconText: iconTextPlaceholder,
|
||||
iconWidget: widget.NewText(image.Pt(10, 12)),
|
||||
artWidget: widget.NewImage(image.Pt(bounds.Dy(), bounds.Dy())),
|
||||
spectrumWidget: widget.NewSpectrum(image.Pt(bounds.Dx()-bounds.Dy(), playingWaterfallHeight)),
|
||||
}
|
||||
|
||||
var (
|
||||
formatStyle = styles.Text.WithFont(font.Spleen6x12)
|
||||
iconStyle = styles.Text.WithFont(font.SM4TIK).WithForegroundColor(color.Gray{0x3f})
|
||||
durationStyle = formatStyle
|
||||
titleStyle = styles.Text.WithFont(font.Montserrat.WithPointSize(20))
|
||||
offset int
|
||||
)
|
||||
|
||||
layout.phonesWidget.
|
||||
SetStyle(iconStyle).
|
||||
Update(string(font.IconNote)).
|
||||
Resize(measureString(iconStyle.Font, iconTextPlaceholder)).
|
||||
Move(image.Pt(offset, -3))
|
||||
offset += 10
|
||||
|
||||
layout.formatWidget.
|
||||
SetStyle(formatStyle).
|
||||
Bind(&layout.formatText).
|
||||
Move(image.Pt(0, 0))
|
||||
Resize(measureString(formatStyle.Font, formatTextPlaceholder)).
|
||||
Move(image.Pt(offset, 0))
|
||||
offset += layout.formatWidget.Bounds().Dx() + 10
|
||||
|
||||
layout.iconWidget.
|
||||
SetStyle(iconStyle).
|
||||
Bind(&layout.iconText).
|
||||
Resize(measureString(iconStyle.Font, iconTextPlaceholder)).
|
||||
Move(image.Pt(offset, -3))
|
||||
offset += 10
|
||||
|
||||
layout.durationWidget.
|
||||
SetStyle(durationStyle).
|
||||
Bind(&layout.durationText).
|
||||
Move(layout.formatWidget.Bounds().Min.Add(image.Pt(8, 0)))
|
||||
Resize(measureString(formatStyle.Font, durationTextPlaceholder)).
|
||||
Move(image.Pt(offset, 0))
|
||||
|
||||
layout.trackWidget.
|
||||
SetStyle(titleStyle).
|
||||
SetScroll(1, 8, time.Second/20).
|
||||
Bind(&layout.trackText).
|
||||
Move(image.Pt(0, 12))
|
||||
|
||||
layout.artWidget.
|
||||
Move(image.Pt(bounds.Dx()-bounds.Dy(), 0))
|
||||
|
||||
layout.spectrumWidget.
|
||||
SetWaterfall(false).
|
||||
SetChannels(2).
|
||||
SetStyles(*styles).
|
||||
Move(image.Pt(0, bounds.Dy()-playingWaterfallHeight))
|
||||
|
||||
layout.widgets = []app.Widget{
|
||||
layout.phonesWidget,
|
||||
layout.formatWidget,
|
||||
layout.iconWidget,
|
||||
layout.durationWidget,
|
||||
layout.trackWidget,
|
||||
layout.artWidget,
|
||||
layout.spectrumWidget,
|
||||
}
|
||||
|
||||
return layout
|
||||
}
|
||||
|
||||
/*
|
||||
func (l *playing) log() *logrus.Entry {
|
||||
return logrus.WithField("layout", "playing")
|
||||
}
|
||||
|
||||
|
||||
func (playing) String() string {
|
||||
return "playing"
|
||||
}
|
||||
|
||||
func (layout *playing) Widgets() []app.Widget {
|
||||
return []app.Widget{
|
||||
layout.formatWidget,
|
||||
layout.durationWidget,
|
||||
layout.trackWidget,
|
||||
layout.artWidget,
|
||||
func (l *playing) Hide(ctx app.Context) {
|
||||
log := l.log()
|
||||
log.Debug("hide")
|
||||
for _, widget := range l.Widgets() {
|
||||
log.WithField("widget", widget.String()).Debug("hide widget")
|
||||
_ = widget.Hide(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (layout *playing) TrackChanged(track *player.Track) {
|
||||
if track == nil {
|
||||
layout.formatText = formatTextPlaceholder
|
||||
layout.durationText = durationTextPlaceholder
|
||||
layout.trackText = trackTextPlaceholder
|
||||
} else {
|
||||
layout.trackText = track.Artist + " - " + track.Title
|
||||
layout.formatText = strings.ToUpper(track.Format)
|
||||
layout.durationText = fmt.Sprintf("00:00 / %s", formatDuration(track.Duration))
|
||||
func (l *playing) Show(ctx app.Context) {
|
||||
log := l.log()
|
||||
log.Debug("show")
|
||||
for _, widget := range l.Widgets() {
|
||||
log.WithField("widget", widget.String()).Debug("show widget")
|
||||
_ = widget.Show(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *playing) Widgets() []app.Widget {
|
||||
return []app.Widget{
|
||||
l.phonesWidget,
|
||||
l.formatWidget,
|
||||
l.iconWidget,
|
||||
l.durationWidget,
|
||||
l.trackWidget,
|
||||
l.artWidget,
|
||||
l.spectrumWidget,
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func (l *playing) TrackChanged(ctx app.Context, track *player.Track) {
|
||||
if track == nil {
|
||||
l.formatText = formatTextPlaceholder
|
||||
l.durationText = durationTextPlaceholder
|
||||
l.trackText = trackTextPlaceholder
|
||||
} else {
|
||||
l.trackText = formatTrack(track)
|
||||
l.formatRate = false
|
||||
l.formatText = strings.ToUpper(track.Format)
|
||||
}
|
||||
|
||||
if !track.Eq(l.track) {
|
||||
l.track = track
|
||||
l.artWidget.Update(beatglowLogo, ctx.Display().ColorModel())
|
||||
go l.updateArt(ctx, track)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *playing) updateArt(ctx app.Context, track *player.Track) {
|
||||
log := logrus.WithField("track", track)
|
||||
if fetcher, ok := ctx.Player().(player.AlbumArt); ok {
|
||||
if image, ok := fetcher.AlbumArt(track); ok {
|
||||
log.WithField("size", image.Bounds().Size()).Debug("updating album art")
|
||||
l.artWidget.Update(image, ctx.Display().ColorModel())
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Debug("updating album art failed: unavailable")
|
||||
}
|
||||
|
||||
func (l *playing) StatusChanged(ctx app.Context, status *player.Status) {
|
||||
if status == nil {
|
||||
l.formatText = formatTextPlaceholder
|
||||
l.durationText = durationTextPlaceholder
|
||||
l.trackText = trackTextPlaceholder
|
||||
l.iconText = string(font.IconBug)
|
||||
return
|
||||
}
|
||||
|
||||
if status.Paused {
|
||||
l.iconText = string(font.IconPause)
|
||||
} else if status.Playing {
|
||||
l.iconText = string(font.IconPlay)
|
||||
} else {
|
||||
l.iconText = string(font.IconStop)
|
||||
}
|
||||
|
||||
if status.Duration > 0 {
|
||||
l.durationText = fmt.Sprintf("%s / %s", formatDuration(status.Elapsed), formatDuration(status.Duration))
|
||||
}
|
||||
|
||||
if status.Track != nil {
|
||||
if l.formatRate {
|
||||
l.formatText = fmt.Sprintf("%04d@%s", status.Format.BitRate, formatRate(status.Format.SampleRate))
|
||||
} else {
|
||||
l.formatText = fmt.Sprintf("%-4s@%s", strings.ToUpper(status.Track.Format), formatRate(status.Format.SampleRate))
|
||||
}
|
||||
l.formatRate = !l.formatRate
|
||||
}
|
||||
}
|
||||
|
||||
func (l *playing) ProcessBuffer(buffer audio.Buffer[float64]) {
|
||||
if l.spectrumWidget.IsVisible() {
|
||||
l.spectrumWidget.ProcessBuffer(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
func measureString(font font.Font, text string) image.Point {
|
||||
width, height := font.MeasureString(text)
|
||||
return image.Pt(width, height)
|
||||
}
|
||||
|
||||
func formatDuration(d time.Duration) string {
|
||||
h := int(d / time.Hour)
|
||||
d -= time.Duration(h) * time.Hour
|
||||
m := int(d / time.Minute)
|
||||
return fmt.Sprintf("%02d:%02d", h, m)
|
||||
d -= time.Duration(m) * time.Minute
|
||||
s := int(d / time.Second)
|
||||
return fmt.Sprintf("%02d:%02d", m, s)
|
||||
}
|
||||
|
||||
func formatTrack(track *player.Track) string {
|
||||
var artist, title string
|
||||
if artist = track.Artist; artist == "" {
|
||||
artist = "Unknown Artist"
|
||||
}
|
||||
if title = track.Title; title == "" {
|
||||
title = "Unknown Title"
|
||||
}
|
||||
return artist + " - " + title
|
||||
}
|
||||
|
||||
func formatRate(rate physic.Frequency) string {
|
||||
if rate > physic.MegaHertz {
|
||||
m := rate / physic.MegaHertz
|
||||
k := (rate - m*physic.MegaHertz) / 100
|
||||
if m > 100 {
|
||||
return fmt.Sprintf("%dm", int(m))
|
||||
}
|
||||
return fmt.Sprintf("%d.%1dm", int(m), int(k))
|
||||
} else if rate > physic.KiloHertz {
|
||||
v := float64(rate) / float64(physic.KiloHertz)
|
||||
if v >= 100 {
|
||||
return fmt.Sprintf("%dk", int(v))
|
||||
}
|
||||
return fmt.Sprintf("%03.1fk", v)
|
||||
}
|
||||
return fmt.Sprintf("%dh", rate/physic.Hertz)
|
||||
}
|
||||
|
||||
var (
|
||||
_ app.StatusChangeHandler = (*playing)(nil)
|
||||
_ app.TrackChangeHandler = (*playing)(nil)
|
||||
_ app.BufferProcessor = (*playing)(nil)
|
||||
|
||||
//go:embed asset/beatglow.png
|
||||
beatglowPNG []byte
|
||||
|
||||
beatglowLogo = app.MustDecodeImage(beatglowPNG)
|
||||
)
|
||||
@@ -0,0 +1,230 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/widget"
|
||||
"github.com/BeatGlow/beatglow/config"
|
||||
)
|
||||
|
||||
type settings struct {
|
||||
base
|
||||
settings *config.Settings
|
||||
menu []settingsMenu
|
||||
menuWidget *widget.Menu
|
||||
index int
|
||||
}
|
||||
|
||||
type settingsMenu struct {
|
||||
Item interface{} // pointer to the item we're showing a menu for
|
||||
Value reflect.Value // value of the item we're showing a menu for
|
||||
Fields []reflect.Value // fields contained in this item
|
||||
Items []widget.MenuItem // items shown in the menu
|
||||
}
|
||||
|
||||
// Settings is the settings menu.
|
||||
func Settings(ctx app.Context) app.Layout {
|
||||
bounds := ctx.Display().Bounds()
|
||||
styles := app.StylesFor(ctx)
|
||||
layout := &settings{
|
||||
base: base{name: "settings"},
|
||||
menuWidget: widget.NewMenu(bounds.Size()),
|
||||
settings: ctx.Settings(),
|
||||
}
|
||||
|
||||
layout.menuWidget.SetStyles(*styles)
|
||||
layout.widgets = []app.Widget{
|
||||
layout.menuWidget,
|
||||
}
|
||||
layout.show(layout.settings)
|
||||
|
||||
return layout
|
||||
}
|
||||
|
||||
func (l *settings) show(data interface{}) {
|
||||
var (
|
||||
log = l.log()
|
||||
v = reflect.ValueOf(data)
|
||||
pointer bool
|
||||
pointerV = v
|
||||
)
|
||||
for {
|
||||
if pointerV.Kind() == reflect.Interface {
|
||||
pointerV = pointerV.Elem()
|
||||
}
|
||||
|
||||
if pointerV.Kind() == reflect.Ptr {
|
||||
pointer = true
|
||||
v = reflect.Indirect(pointerV)
|
||||
}
|
||||
|
||||
if pointer {
|
||||
pointerV = v
|
||||
}
|
||||
pointer = false
|
||||
|
||||
// If we still have a pointer or interface we have to indirect another level.
|
||||
switch pointerV.Kind() {
|
||||
case reflect.Ptr, reflect.Interface:
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
switch v.Kind() {
|
||||
case reflect.Struct:
|
||||
log.Printf("setup for struct: %T", data)
|
||||
l.showStruct(v, data)
|
||||
|
||||
default:
|
||||
log.Printf("setup for what? %T", data)
|
||||
}
|
||||
|
||||
// Update widget.
|
||||
l.menuWidget.SetItems(l.menu[0].Items)
|
||||
|
||||
// Reset selected item.
|
||||
l.index = 0
|
||||
}
|
||||
|
||||
func (l *settings) showStruct(v reflect.Value, data interface{}) {
|
||||
var (
|
||||
log = l.log()
|
||||
vt = v.Type()
|
||||
fields = make([]reflect.Value, 0, vt.NumField())
|
||||
menuItems = make([]widget.MenuItem, 0, vt.NumField())
|
||||
)
|
||||
|
||||
foreachStructField(v, func(fv reflect.Value, sf reflect.StructField) bool {
|
||||
icon, tags := parseSettingsTag(sf.Tag)
|
||||
log.Printf("field %#+v: %#+v -> %s %v", sf, fv, icon, tags)
|
||||
log.Printf("can addr? %t", fv.CanAddr())
|
||||
|
||||
if _, ignore := tags["ignore"]; ignore {
|
||||
return false
|
||||
}
|
||||
|
||||
fields = append(fields, fv)
|
||||
menuItems = append(menuItems, widget.MenuItem{
|
||||
Icon: icon,
|
||||
Text: sf.Name,
|
||||
Type: fv.Type(),
|
||||
Value: fv,
|
||||
})
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
// Push current item forward on the stack
|
||||
l.menu = append([]settingsMenu{{
|
||||
Item: data,
|
||||
Value: v,
|
||||
Fields: fields,
|
||||
Items: menuItems,
|
||||
}}, l.menu...)
|
||||
}
|
||||
|
||||
func (l *settings) handleSelect(index int) {
|
||||
var (
|
||||
active = l.menu[0].Fields[index]
|
||||
value = active
|
||||
kind = value.Type().Kind()
|
||||
)
|
||||
|
||||
if kind == reflect.Ptr {
|
||||
value = reflect.Indirect(value)
|
||||
kind = value.Type().Kind()
|
||||
}
|
||||
|
||||
log := l.log()
|
||||
log.WithField("index", index).Debugf("jump to menu item %v (%v/%v)", value, value.Type(), kind)
|
||||
|
||||
switch kind {
|
||||
case reflect.Struct:
|
||||
l.show(active.Interface())
|
||||
|
||||
case reflect.Bool:
|
||||
//newValue := reflect.New(active.Type())
|
||||
//newValue.Elem().Set(reflect.ValueOf(!active.Bool()))
|
||||
//active.Elem().Set(newValue)
|
||||
active.SetBool(!active.Bool())
|
||||
l.menuWidget.Redraw()
|
||||
|
||||
default:
|
||||
log.WithField("type", kind).Debug("no handler for data type")
|
||||
}
|
||||
}
|
||||
|
||||
func parseSettingsTag(tag reflect.StructTag) (icon string, settings map[string]string) {
|
||||
icon = tag.Get("icon")
|
||||
if s, ok := tag.Lookup("settings"); ok {
|
||||
settings = make(map[string]string)
|
||||
for _, kv := range strings.Split(s, " ") {
|
||||
if kv = strings.TrimSpace(kv); kv == "" {
|
||||
continue
|
||||
}
|
||||
if i := strings.IndexByte(kv, ':'); i > -1 {
|
||||
k, v := kv[:i], kv[i+1:]
|
||||
settings[k] = v
|
||||
} else {
|
||||
settings[kv] = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (l *settings) HandleRotary(ctx app.Context, delta int) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
l.index = l.menuWidget.Change(delta)
|
||||
l.log().WithField("item", l.index).Debug("menu item changed")
|
||||
}
|
||||
}
|
||||
|
||||
func (l *settings) HandleButtonPress(ctx app.Context) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
log := l.log().WithField("press", l.index)
|
||||
if l.index == 0 {
|
||||
if depth := len(l.menu); depth > 1 {
|
||||
log.Debug("go back in menu")
|
||||
l.index = 0
|
||||
l.menu = l.menu[1:]
|
||||
l.menuWidget.SetItems(l.menu[0].Items)
|
||||
} else {
|
||||
// Go back pressed.
|
||||
log.Debug("go back to previous layout")
|
||||
ctx.Show(nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("handle selected item")
|
||||
l.handleSelect(l.index - 1)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
l.menu = nil
|
||||
l.show(l.settings)
|
||||
ctx.Show(nil) // Close settings
|
||||
}
|
||||
}
|
||||
|
||||
func foreachStructField(v reflect.Value, f func(fv reflect.Value, sf reflect.StructField) (stop bool)) {
|
||||
t := v.Type()
|
||||
for i, l := 0, t.NumField(); i < l; i++ {
|
||||
sf := t.Field(i)
|
||||
fv := v.Field(i).Addr()
|
||||
if f(fv, sf) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
_ app.RotaryInputHandler = (*settings)(nil)
|
||||
_ app.ButtonInputHandler = (*settings)(nil)
|
||||
)
|
||||
Reference in new issue
Block a user