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

+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)