Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
@@ -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)
|
||||
Reference in new issue
Block a user