Files
beatglow/app/widget/text.go
T
2026-10-29 17:29:54 +01:00

157 lines
3.3 KiB
Go

package widget
import (
"fmt"
"image"
"image/draw"
"time"
"github.com/sirupsen/logrus"
"github.com/BeatGlow/beatglow/app"
)
type Text struct {
Base
text string
bind *string
scroll image.Point
scrollDir int
scrollGap int
scrollLast time.Time
scrollRate time.Duration
}
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) *Text {
styles := w.Styles()
styles.Text = style
_ = w.SetStyles(styles)
logrus.Debugf("using font %#+v", style.Font)
return w
}
func (w *Text) SetScroll(dir, gap int, rate time.Duration) *Text {
w.mu.Lock()
w.scroll = image.Point{}
w.scrollDir = dir
w.scrollGap = gap
w.scrollLast = time.Now()
w.scrollRate = rate
w.mu.Unlock()
w.changed.Store(true)
return w
}
func (w *Text) Bind(text *string) *Text {
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) *Text {
w.mu.Lock()
w.text = text
w.bind = nil
w.mu.Unlock()
width, height := w.styles.Text.Font.MeasureString(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) bool {
log := logrus.WithField("widget", w.String())
if !w.IsVisible() {
log.Trace("not refreshing; not visisble")
return false
}
var (
now = time.Now()
changed = w.changed.CompareAndSwap(true, false)
bounds = w.bounds.Load()
)
// Update text with our bound value, if changed.
if w.bind != nil && w.text != *w.bind {
w.text = *w.bind
w.scroll.X = 0
changed = true
}
// Mark the widget as changed if the buffer doesn't fit our text.
var (
resize image.Point
textWidth, textHeight = w.styles.Text.Font.MeasureString(w.text)
)
textWidth += w.scrollGap
if w.buffer == nil {
resize = image.Pt(textWidth, textHeight)
changed = true
} else if size := w.buffer.Bounds().Size(); size.X < textWidth || size.Y < textHeight {
resize = image.Pt(textWidth, textHeight)
changed = true
}
// Re-render if changed
if changed {
// Resize buffer if required.
if w.buffer == nil || resize.X+resize.Y > 0 {
log.WithField("size", resize).Debug("allocating new buffer")
w.buffer = app.BufferFor(ctx, resize)
}
// Fill background.
app.ImageCopy(w.buffer, w.styles.Text.Background())
// Fill text.
inner := w.styles.Text.Inner(w.buffer)
w.styles.Text.Font.Draw(inner, w.scroll, w.styles.Text.Foreground(), w.text)
}
// Scroll text if required.
scrollRequired := textWidth > bounds.Dx()
if scrollRequired && w.scrollDir != 0 && now.Sub(w.scrollLast) >= w.scrollRate {
w.scroll.X += w.scrollDir
if w.scroll.X > (textWidth + w.scrollGap) {
w.scroll.X = 0
} else if w.scroll.X < -(textWidth + w.scrollGap) {
w.scroll.X = 0
}
w.scrollLast = now
}
//log.WithField("bounds", w.Bounds()).Debug("draw")
output := image.Image(w.buffer)
if scrollRequired {
output = app.HorizontalWrappingImage{Image: w.buffer}
}
draw.Draw(ctx.Display(), bounds.Add(w.position.Load()), output, w.scroll, draw.Src)
return true
}
var _ app.Widget = (*Text)(nil)