Checkpoint
This commit is contained in:
223 files changed
+14770
-18352
No files matched your search
+102
-37
@@ -3,14 +3,23 @@ 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
|
||||
text string
|
||||
bind *string
|
||||
scroll image.Point
|
||||
scrollDir int
|
||||
scrollGap int
|
||||
scrollLast time.Time
|
||||
scrollRate time.Duration
|
||||
}
|
||||
|
||||
func NewText(size image.Point) *Text {
|
||||
@@ -27,14 +36,27 @@ func (w *Text) String() string {
|
||||
return fmt.Sprintf("text:%q", w.text)
|
||||
}
|
||||
|
||||
func (w *Text) SetStyle(style app.TextStyle) app.Widget {
|
||||
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) Bind(text *string) app.Widget {
|
||||
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
|
||||
@@ -43,13 +65,13 @@ func (w *Text) Bind(text *string) app.Widget {
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Text) Update(text string) app.Widget {
|
||||
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.MeasureText(w.text)
|
||||
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)
|
||||
@@ -58,52 +80,95 @@ func (w *Text) Update(text string) app.Widget {
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Text) Refresh(ctx app.Context) app.Widget {
|
||||
if !w.IsVisible() {
|
||||
return w
|
||||
}
|
||||
func (w *Text) Refresh(ctx app.Context) bool {
|
||||
log := logrus.WithField("widget", w.String())
|
||||
|
||||
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
|
||||
}
|
||||
if !w.IsVisible() {
|
||||
log.Trace("not refreshing; not visisble")
|
||||
return false
|
||||
}
|
||||
|
||||
var (
|
||||
resize bool
|
||||
bounds image.Rectangle
|
||||
now = time.Now()
|
||||
changed = w.changed.CompareAndSwap(true, false)
|
||||
bounds = w.bounds.Load()
|
||||
)
|
||||
if w.bind != nil {
|
||||
|
||||
// Update text with our bound value, if changed.
|
||||
if w.bind != nil && w.text != *w.bind {
|
||||
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))
|
||||
w.scroll.X = 0
|
||||
changed = true
|
||||
}
|
||||
|
||||
if w.buffer == nil || !resize {
|
||||
w.buffer = app.BufferFor(ctx, bounds.Size())
|
||||
// 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
|
||||
}
|
||||
|
||||
if w.styles.Text.Background != nil {
|
||||
app.ImageCopy(w.buffer, w.styles.Text.Background)
|
||||
} else {
|
||||
app.ImageFill(w.buffer, w.styles.Text.BackgroundColor)
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
// 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
|
||||
}
|
||||
|
||||
app.ImageCopyBounds(ctx.Display(), w.buffer, w.Bounds())
|
||||
//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 w
|
||||
return true
|
||||
}
|
||||
|
||||
var _ app.Widget = (*Text)(nil)
|
||||
Reference in new issue
Block a user