Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:41 +01:00
1 parent 85a9ec684c
commit c5d6137fab
160 files changed
+76293 -387

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
package app
import (
"image"
"github.com/BeatGlow/beatglow/player"
"github.com/BeatGlow/display"
)
type Layout interface {
String() string
// Widgets used by this screen.
Widgets() []Widget
}
type Context interface {
// Display is the display driver.
Display() display.Display
// Player is the music player.
Player() player.Player
// Track is the current track playing.
Track() *player.Track
}
type Widget interface {
String() string
// Bounds is the bounding box.
Bounds() image.Rectangle
// Move the drawable to a new position on the screen.
Move(image.Point) Widget
// Resize changes the widget's size.
Resize(image.Point) Widget
// Hide the drawable.
Hide(Context) Widget
// Show the drawable.
Show(Context) Widget
// IsVisible checks if the drawable is visible.
IsVisible() bool
Refresh(Context) Widget
}
type RotaryInputHandler interface {
HandleRotary(delta int)
}
type ButtonInputHandler interface {
HandleButtonPress()
HandleButtonLongPress()
}
type StatusChangeHandler interface {
StatusChanged(*player.Status)
}
type TrackChangeHandler interface {
TrackChanged(*player.Track)
}
+76
View File
@@ -0,0 +1,76 @@
package app
import (
"image"
"image/color"
"image/draw"
"github.com/BeatGlow/display/pixel"
)
// BufferFor creates a new image buffer for the provided display matching its color model.
func BufferFor(ctx Context, size image.Point) draw.Image {
switch ctx.Display().ColorModel() {
case pixel.MonoModel:
return pixel.NewMonoImage(size.X, size.Y)
case pixel.Gray2Model:
return pixel.NewGray2Image(size.X, size.Y)
case pixel.Gray4Model:
return pixel.NewGray4Image(size.X, size.Y)
case pixel.CRGB15Model:
return pixel.NewCRGB15Image(size.X, size.Y)
case pixel.CRGB16Model:
return pixel.NewCRGB16Image(size.X, size.Y)
default:
return nil
}
}
type clearer interface {
Clear()
}
// ImageCopy copies an image over dst.
func ImageCopy(dst draw.Image, src image.Image) {
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
}
// ImageCopyBounds copies an image over dst bounded by r.
func ImageCopyBounds(dst draw.Image, src image.Image, r image.Rectangle) {
draw.Draw(dst, r, src, image.Point{}, draw.Src)
}
// Clear an image.
func ImageClear(dst draw.Image) {
if c, ok := dst.(clearer); ok {
c.Clear()
return
}
ImageFill(dst, color.Black)
}
type filler interface {
Fill(color.Color)
}
// Fill an imagre with a single color.
func ImageFill(dst draw.Image, c color.Color) {
if f, ok := dst.(filler); ok {
f.Fill(c)
return
}
var src image.Image
switch c {
case color.Black:
src = image.Black
case color.White:
src = image.White
case color.Transparent:
src = image.Transparent
default:
src = image.NewUniform(c)
}
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
}
+87
View File
@@ -0,0 +1,87 @@
package layout
import (
"fmt"
"image"
"strings"
"time"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/app/widget"
"github.com/BeatGlow/beatglow/player"
)
const (
formatTextPlaceholder = "None"
durationTextPlaceholder = "--:-- / --:--"
trackTextPlaceholder = "No track playing..."
)
type playing struct {
formatText string
formatWidget *widget.Text
durationText string
durationWidget *widget.Text
trackText string
trackWidget *widget.Text
artWidget *widget.Image
}
func Playing(ctx app.Context) app.Layout {
bounds := ctx.Display().Bounds()
layout := &playing{
formatText: formatTextPlaceholder,
formatWidget: widget.NewText(image.Pt(2+len(formatTextPlaceholder)*8, 12)),
durationText: durationTextPlaceholder,
durationWidget: widget.NewText(image.Pt(2+len(durationTextPlaceholder)*8, 12)),
trackText: trackTextPlaceholder,
trackWidget: widget.NewText(image.Pt(bounds.Dx()-bounds.Dy(), bounds.Dy()-12-16)),
artWidget: widget.NewImage(image.Pt(bounds.Dy(), bounds.Dy())),
}
layout.formatWidget.
Bind(&layout.formatText).
Move(image.Pt(0, 0))
layout.durationWidget.
Bind(&layout.durationText).
Move(layout.formatWidget.Bounds().Min.Add(image.Pt(8, 0)))
layout.trackWidget.
Bind(&layout.trackText).
Move(image.Pt(0, 12))
layout.artWidget.
Move(image.Pt(bounds.Dx()-bounds.Dy(), 0))
return layout
}
func (playing) String() string {
return "playing"
}
func (layout *playing) Widgets() []app.Widget {
return []app.Widget{
layout.formatWidget,
layout.durationWidget,
layout.trackWidget,
layout.artWidget,
}
}
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 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)
}
+264
View File
@@ -0,0 +1,264 @@
package app
import (
"image"
"image/color"
"image/draw"
"github.com/BeatGlow/beatglow/ui/font"
"github.com/BeatGlow/display/pixel"
)
var (
// DefaultTheme is the default theme per color model.
DefaultTheme = &Theme{
Mono: &defaultMonoThemeStyles,
Gray: &defaultGrayThemeStyles,
Color: &defaultColorThemeStyles,
}
// UserTheme can be set to override styles.
UserTheme *Theme
)
var (
defaultMonoObjectStyle = ObjectStyle{
BackgroundColor: pixel.Off,
ForegroundColor: pixel.On,
BorderColor: pixel.On,
}
defaultMonoThemeStyles = Styles{
Window: defaultMonoObjectStyle,
Progress: defaultMonoObjectStyle.WithBorder(1),
Image: defaultMonoObjectStyle,
Graph: defaultMonoObjectStyle,
Text: TextStyle{
ObjectStyle: defaultMonoObjectStyle.WithMargin(1),
Font: font.SmoochSans,
},
}
defaultGrayObjectStyle = ObjectStyle{
Background: image.Black,
ForegroundColor: color.White,
BorderColor: color.Gray{Y: 0x7F},
}
defaultGrayThemeStyles = Styles{
Window: defaultGrayObjectStyle,
Progress: defaultGrayObjectStyle.WithBorder(1),
Image: defaultGrayObjectStyle,
Graph: defaultGrayObjectStyle,
Text: TextStyle{
ObjectStyle: defaultGrayObjectStyle.WithMargin(1),
Font: font.SmoochSans,
},
}
defaultColorObjectStyle = ObjectStyle{
BackgroundColor: color.RGBA{R: 0xB0, G: 0x26, B: 0xFF, A: 0xFF},
ForegroundColor: color.RGBA{R: 0x01, G: 0xFB, B: 0xC1, A: 0xFF},
BorderColor: color.RGBA{R: 0x1D, G: 0xEA, B: 0xA1, A: 0xFF},
}
defaultColorThemeStyles = Styles{
Window: defaultColorObjectStyle,
Progress: defaultColorObjectStyle.WithBorder(1).Inverse(),
Image: defaultColorObjectStyle,
Graph: defaultColorObjectStyle,
Text: TextStyle{
ObjectStyle: defaultColorObjectStyle.WithMargin(1),
Font: font.SmoochSans,
},
}
)
type Theme struct {
Mono *Styles
Gray *Styles
Color *Styles
}
type Styles struct {
Window ObjectStyle
Progress ObjectStyle
Text TextStyle
Image ObjectStyle
Graph ObjectStyle
}
func StylesFor(ctx Context) *Styles {
theme := UserTheme
if theme == nil {
// Use the default theme.
theme = DefaultTheme
} else {
// Make sure all color models have a theme defined.
if theme.Mono == nil {
theme.Mono = DefaultTheme.Mono
}
if theme.Gray == nil {
theme.Gray = DefaultTheme.Gray
}
if theme.Color == nil {
theme.Color = DefaultTheme.Color
}
}
model := ctx.Display().ColorModel()
switch model {
case pixel.MonoModel:
return theme.Mono
case pixel.Gray2Model, pixel.Gray4Model, color.GrayModel, color.Gray16Model:
return theme.Gray
default:
return theme.Color
}
}
type ObjectStyle struct {
// Background image or color.
Background image.Image
BackgroundColor color.Color
// Foreground image (mask) or color.
Foreground image.Image
ForegroundColor color.Color
// Margin around the object.
Margin Edge
// Padding inside the object.
Padding Edge
// Border around the object.
Border Edge
BorderColor color.Color
// Stroke size for lines etc.
Stroke int
}
func (s ObjectStyle) Inverse() ObjectStyle {
i := new(ObjectStyle)
*i = s
i.Background, i.Foreground = i.Foreground, i.Background
i.BackgroundColor, i.ForegroundColor = i.ForegroundColor, i.BackgroundColor
return *i
}
func (s ObjectStyle) WithBorder(size ...int) ObjectStyle {
i := new(ObjectStyle)
*i = s
switch len(size) {
case 0:
return s
case 1:
i.Border = Edge{size[0], size[0], size[0], size[0]}
case 2:
i.Border = Edge{size[0], size[0], size[1], size[1]}
case 3:
i.Border = Edge{size[0], size[1], size[2], size[2]}
default:
i.Border = Edge{size[0], size[1], size[2], size[3]}
}
return *i
}
func (s ObjectStyle) WithMargin(size ...int) ObjectStyle {
i := new(ObjectStyle)
*i = s
switch len(size) {
case 0:
return s
case 1:
i.Margin = Edge{size[0], size[0], size[0], size[0]}
case 2:
i.Margin = Edge{size[0], size[0], size[1], size[1]}
case 3:
i.Margin = Edge{size[0], size[1], size[2], size[2]}
default:
i.Margin = Edge{size[0], size[1], size[2], size[3]}
}
return *i
}
func (s ObjectStyle) WithPadding(size ...int) ObjectStyle {
i := new(ObjectStyle)
*i = s
switch len(size) {
case 0:
return s
case 1:
i.Padding = Edge{size[0], size[0], size[0], size[0]}
case 2:
i.Padding = Edge{size[0], size[0], size[1], size[1]}
case 3:
i.Padding = Edge{size[0], size[1], size[2], size[2]}
default:
i.Padding = Edge{size[0], size[1], size[2], size[3]}
}
return *i
}
// boundedImage translates (x, y) coorindates to coordinats relative to View.Min; if
// writes are outside of View, they are ignored.
type boundedImage struct {
draw.Image
View image.Rectangle
}
func (i boundedImage) At(x, y int) color.Color {
if !(image.Point{X: x, Y: y}).In(i.View.Sub(i.View.Min)) {
return color.Transparent
}
return i.Image.At(x+i.View.Min.X, y+i.View.Min.Y)
}
func (i boundedImage) Set(x, y int, c color.Color) {
if !(image.Point{X: x, Y: y}).In(i.View.Sub(i.View.Min)) {
return
}
i.Image.Set(x+i.View.Min.X, y+i.View.Min.Y, c)
}
func (i boundedImage) Bounds() image.Rectangle {
return i.View
}
// Outer returns an image that draws into i's outer region (inside the border).
func (s ObjectStyle) Outer(i draw.Image) draw.Image {
bounds := i.Bounds()
bounds.Min.X += s.Margin.Left + s.Border.Left
bounds.Min.Y += s.Margin.Top + s.Border.Top
bounds.Max.X -= s.Margin.Right + s.Border.Right
bounds.Max.Y -= s.Margin.Bottom + s.Border.Bottom
return boundedImage{i, bounds}
}
// Inner returns an image that draws into i's inner region.
func (s ObjectStyle) Inner(i draw.Image) draw.Image {
bounded := s.Outer(i).(boundedImage)
bounded.View.Min.X += s.Padding.Left
bounded.View.Min.Y += s.Padding.Top
bounded.View.Max.X -= s.Padding.Right
bounded.View.Max.Y -= s.Padding.Bottom
return bounded
}
type TextStyle struct {
ObjectStyle
Font *font.Font
}
type Edge struct {
Left int
Right int
Top int
Bottom int
}
func (edge Edge) Size() image.Point {
return image.Point{
X: edge.Left + edge.Right,
Y: edge.Top + edge.Bottom,
}
}
+126
View File
@@ -0,0 +1,126 @@
package widget
import (
"image"
"sync"
"sync/atomic"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/draw"
"github.com/BeatGlow/beatglow/internal/safe"
)
type Base struct {
bounds safe.Rectangle
widget atomic.Pointer[app.Widget]
mu sync.RWMutex
buffer draw.Image
hidden bool
styles app.Styles
changed atomic.Bool
}
func (b *Base) Extend(w app.Widget) {
if widget := b.widget.Load(); widget != nil {
return
}
b.widget.Store(&w)
b.changed.Store(true)
}
func (b *Base) super() app.Widget {
if widget := b.widget.Load(); widget != nil {
return *widget
}
return nil
}
func (b *Base) Bounds() image.Rectangle {
return b.bounds.Load()
}
func (b *Base) Move(p image.Point) app.Widget {
bounds := b.bounds.Load()
b.bounds.Store(image.Rectangle{
Min: p,
Max: bounds.Size(),
})
return b.super()
}
func (b *Base) Resize(size image.Point) app.Widget {
bounds := b.bounds.Load()
bounds.Max = bounds.Min.Add(size)
b.bounds.Store(bounds)
return b.super()
}
func (b *Base) Hide(ctx app.Context) app.Widget {
if !b.IsVisible() {
return b.super()
}
b.mu.Lock()
b.hidden = true
b.mu.Unlock()
b.changed.Store(true)
if widget := b.super(); widget != nil {
widget.Refresh(ctx)
}
return b.super()
}
func (b *Base) Show(ctx app.Context) app.Widget {
if b.IsVisible() {
return b.super()
}
b.mu.Lock()
b.hidden = false
b.mu.Unlock()
b.changed.Store(true)
if widget := b.super(); widget != nil {
widget.Refresh(ctx)
}
return b.super()
}
func (b *Base) IsVisible() bool {
b.mu.RLock()
visible := !b.hidden
b.mu.RUnlock()
return visible
}
func (b *Base) Styles() app.Styles {
b.mu.RLock()
styles := b.styles
b.mu.RUnlock()
return styles
}
func (b *Base) SetStyles(styles app.Styles) app.Widget {
b.mu.Lock()
b.styles = styles
b.mu.Unlock()
b.changed.Store(true)
return b.super()
}
func (b *Base) Render(ctx app.Context) app.Widget {
if !b.changed.CompareAndSwap(true, false) {
return b.super()
}
if b.buffer == nil {
b.buffer = app.BufferFor(ctx, b.Bounds().Size())
}
return b.super()
}
+49
View File
@@ -0,0 +1,49 @@
package widget
import (
"image"
"github.com/BeatGlow/beatglow/app"
)
type Image struct {
Base
image image.Image
}
func NewImage(size image.Point) *Image {
w := new(Image)
w.Extend(w)
w.Resize(size)
return w
}
func (w *Image) String() string {
return "image:" + w.image.Bounds().String()
}
func (w *Image) Update(image image.Image) app.Widget {
w.mu.Lock()
w.image = image
w.mu.Unlock()
w.changed.Store(true)
return w
}
func (w *Image) Refresh(ctx app.Context) app.Widget {
if !w.IsVisible() {
return w
}
if !w.changed.CompareAndSwap(true, false) {
return w
}
w.mu.RLock()
app.ImageCopyBounds(ctx.Display(), w.image, w.Bounds())
w.mu.RUnlock()
return w
}
var _ app.Widget = (*Image)(nil)
+70
View File
@@ -0,0 +1,70 @@
package widget
import (
"image"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/display/draw"
)
type Object struct {
active bool
width int
height int
pos image.Point
style app.ObjectStyle
canvas draw.Image
changed bool
}
func MakeObject(ctx app.Context, position image.Point, width, height int, style app.ObjectStyle) Object {
return Object{
width: width,
height: height,
pos: position,
style: style,
canvas: app.BufferFor(ctx, width, height),
changed: true,
}
}
func (w *Object) Bounds() image.Rectangle {
return image.Rect(0, 0, w.width, w.height)
}
func (w *Object) Position() image.Point {
return w.pos
}
func (w *Object) IsActive() bool {
return w.active
}
func (w *Object) Render() draw.Image {
if w.changed {
// Clear entire buffer.
app.ImageClear(w.canvas)
var (
bounds = w.canvas.Bounds()
outer = w.style.Outer(w.canvas)
)
// Background draws into outer region.
if w.style.Background != nil {
app.ImageCopy(outer, w.style.Background)
} else {
app.ImageFill(outer, w.style.BackgroundColor)
}
// Draw border
draw.Box(w.canvas, image.Rect(0, 0, bounds.Max.X, w.style.Border.Top), w.style.BorderColor)
draw.Box(w.canvas, image.Rect(0, bounds.Max.Y-w.style.Border.Bottom, bounds.Max.X, bounds.Max.Y), w.style.BorderColor)
draw.Box(w.canvas, image.Rect(0, 0, w.style.Border.Left, bounds.Max.Y), w.style.BorderColor)
draw.Box(w.canvas, image.Rect(bounds.Max.X-w.style.Border.Right, 0, bounds.Max.X, bounds.Max.Y), w.style.BorderColor)
// After redrawing mark the object as unchanged.
w.changed = false
}
return w.canvas
}
+109
View File
@@ -0,0 +1,109 @@
package widget
import (
"fmt"
"image"
"github.com/BeatGlow/beatglow/app"
)
type Text struct {
Base
text string
bind *string
}
func NewText(size image.Point) *Text {
w := new(Text)
w.Extend(w)
w.Resize(size)
return w
}
func (w *Text) String() string {
if w.bind != nil {
return fmt.Sprintf("text:%p", w.bind)
}
return fmt.Sprintf("text:%q", w.text)
}
func (w *Text) SetStyle(style app.TextStyle) app.Widget {
styles := w.Styles()
styles.Text = style
_ = w.SetStyles(styles)
return w
}
func (w *Text) Bind(text *string) app.Widget {
w.mu.Lock()
w.text = "--- intentionally set to garbage ---"
w.bind = text
w.mu.Unlock()
w.changed.Store(true)
return w
}
func (w *Text) Update(text string) app.Widget {
w.mu.Lock()
w.text = text
w.bind = nil
w.mu.Unlock()
width, height := w.styles.Text.Font.MeasureText(w.text)
bounds := w.bounds.Load()
bounds.Max = bounds.Min.Add(image.Pt(width, height))
w.bounds.Store(bounds)
w.changed.Store(true)
return w
}
func (w *Text) Refresh(ctx app.Context) app.Widget {
if !w.IsVisible() {
return w
}
if !w.changed.CompareAndSwap(true, false) {
// Only bail if our bound value hasn't changed.
if w.bind == nil || (w.text == *w.bind) {
return w
}
}
var (
resize bool
bounds image.Rectangle
)
if w.bind != nil {
w.text = *w.bind
}
width, height := w.styles.Text.Font.MeasureText(w.text)
bounds = w.bounds.Load()
size := bounds.Size()
if resize = (size.X != width || size.Y != height); resize {
bounds.Max = bounds.Min.Add(image.Pt(width, height))
}
if w.buffer == nil || !resize {
w.buffer = app.BufferFor(ctx, bounds.Size())
}
if w.styles.Text.Background != nil {
app.ImageCopy(w.buffer, w.styles.Text.Background)
} else {
app.ImageFill(w.buffer, w.styles.Text.BackgroundColor)
}
inner := w.styles.Text.Inner(w.buffer)
if w.styles.Text.Foreground != nil {
w.styles.Text.Font.Draw(inner, image.Point{}, w.styles.Text.Foreground, w.text)
} else {
w.styles.Text.Font.Draw(inner, image.Point{}, image.NewUniform(w.styles.Text.ForegroundColor), w.text)
}
app.ImageCopyBounds(ctx.Display(), w.buffer, w.Bounds())
return w
}
var _ app.Widget = (*Text)(nil)