175 lines
4.0 KiB
Go
175 lines
4.0 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.
|
|
if w.styles.Text.Background != nil {
|
|
log.Trace("fill canvas with background")
|
|
app.ImageCopy(w.buffer, w.styles.Text.Background)
|
|
} else {
|
|
log.WithField("color", w.styles.Text.BackgroundColor).
|
|
Trace("fill canvas with background color")
|
|
app.ImageFill(w.buffer, w.styles.Text.BackgroundColor)
|
|
}
|
|
|
|
// Fill text.
|
|
inner := w.styles.Text.Inner(w.buffer)
|
|
if w.styles.Text.Foreground != nil {
|
|
log.WithField("text", w.text).
|
|
WithField("inner", inner).
|
|
Debug("text canvas with foreground mask")
|
|
w.styles.Text.Font.Draw(inner, w.scroll, w.styles.Text.Foreground, w.text)
|
|
} else {
|
|
log.WithField("text", w.text).
|
|
WithField("inner", inner).
|
|
WithField("color", w.styles.Text.ForegroundColor).
|
|
Debug("text canvas with foreground color")
|
|
w.styles.Text.Font.Draw(inner, w.scroll, image.NewUniform(w.styles.Text.ForegroundColor), 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)
|