Migrated over some elements

This commit is contained in:
2023-04-14 22:03:22 -04:00
parent 4c6f1f80e7
commit 68128c94d8
16 changed files with 526 additions and 410 deletions

View File

@@ -2,16 +2,15 @@ package elements
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
import "git.tebibyte.media/sashakoshka/tomo/default/config"
// Label is a simple text box.
type Label struct {
*core.Core
core core.CoreControl
entity tomo.FlexibleEntity
align textdraw.Align
wrap bool
text string
@@ -19,11 +18,10 @@ type Label struct {
forcedColumns int
forcedRows int
minHeight int
config config.Wrapped
theme theme.Wrapped
onFlexibleHeightChange func ()
}
// NewLabel creates a new label. If wrap is set to true, the text inside will be
@@ -31,7 +29,6 @@ type Label struct {
func NewLabel (text string, wrap bool) (element *Label) {
element = &Label { }
element.theme.Case = tomo.C("tomo", "label")
element.Core, element.core = core.NewCore(element, element.handleResize)
element.drawer.SetFace (element.theme.FontFace (
tomo.FontStyleRegular,
tomo.FontSizeNormal))
@@ -40,29 +37,11 @@ func NewLabel (text string, wrap bool) (element *Label) {
return
}
func (element *Label) redo () {
face := element.theme.FontFace (
tomo.FontStyleRegular,
tomo.FontSizeNormal)
element.drawer.SetFace(face)
// Bind binds this element to an entity.
func (element *Label) Bind (entity tomo.Entity) {
if entity == nil { element.entity = nil; return }
element.entity = entity.(tomo.FlexibleEntity)
element.updateMinimumSize()
bounds := element.Bounds()
if element.wrap {
element.drawer.SetMaxWidth(bounds.Dx())
element.drawer.SetMaxHeight(bounds.Dy())
}
element.draw()
element.core.DamageAll()
}
func (element *Label) handleResize () {
bounds := element.Bounds()
if element.wrap {
element.drawer.SetMaxWidth(bounds.Dx())
element.drawer.SetMaxHeight(bounds.Dy())
}
element.draw()
return
}
// EmCollapse forces a minimum width and height upon the label. The width is
@@ -73,6 +52,7 @@ func (element *Label) handleResize () {
func (element *Label) EmCollapse (columns int, rows int) {
element.forcedColumns = columns
element.forcedRows = rows
if element.entity == nil { return }
element.updateMinimumSize()
}
@@ -82,29 +62,19 @@ func (element *Label) FlexibleHeightFor (width int) (height int) {
if element.wrap {
return element.drawer.ReccomendedHeightFor(width)
} else {
_, height = element.MinimumSize()
return
return element.minHeight
}
}
// OnFlexibleHeightChange sets a function to be called when the parameters
// affecting this element's flexible height are changed.
func (element *Label) OnFlexibleHeightChange (callback func ()) {
element.onFlexibleHeightChange = callback
}
// SetText sets the label's text.
func (element *Label) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText([]rune(text))
if element.entity == nil { return }
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
element.entity.Invalidate()
}
// SetWrap sets wether or not the label's text wraps. If the text is set to
@@ -118,26 +88,19 @@ func (element *Label) SetWrap (wrap bool) {
element.drawer.SetMaxHeight(0)
}
element.wrap = wrap
if element.entity == nil { return }
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
element.entity.Invalidate()
}
// SetAlign sets the alignment method of the label.
func (element *Label) SetAlign (align textdraw.Align) {
if align == element.align { return }
element.align = align
element.drawer.SetAlign(align)
if element.entity == nil { return }
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
element.entity.Invalidate()
}
// SetTheme sets the element's theme.
@@ -147,24 +110,38 @@ func (element *Label) SetTheme (new tomo.Theme) {
element.drawer.SetFace (element.theme.FontFace (
tomo.FontStyleRegular,
tomo.FontSizeNormal))
if element.entity == nil { return }
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
element.entity.Invalidate()
}
// SetConfig sets the element's configuration.
func (element *Label) SetConfig (new tomo.Config) {
if new == element.config.Config { return }
element.config.Config = new
if element.entity == nil { return }
element.updateMinimumSize()
element.entity.Invalidate()
}
// Draw causes the element to draw to the specified destination canvas.
func (element *Label) Draw (destination canvas.Canvas) {
if element.entity == nil { return }
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
bounds := element.entity. Bounds()
if element.wrap {
element.drawer.SetMaxWidth(bounds.Dx())
element.drawer.SetMaxHeight(bounds.Dy())
}
element.entity.DrawBackground(destination, bounds)
textBounds := element.drawer.LayoutBounds()
foreground := element.theme.Color (
tomo.ColorForeground,
tomo.State { })
element.drawer.Draw(destination, foreground, bounds.Min.Sub(textBounds.Min))
}
func (element *Label) updateMinimumSize () {
@@ -176,9 +153,8 @@ func (element *Label) updateMinimumSize () {
em = element.theme.Padding(tomo.PatternBackground)[0]
}
width, height = em, element.drawer.LineHeight().Round()
if element.onFlexibleHeightChange != nil {
element.onFlexibleHeightChange()
}
// FIXME we shoudl not have to pass in the element here
element.entity.NotifyFlexibleHeightChange(element)
} else {
bounds := element.drawer.LayoutBounds()
width, height = bounds.Dx(), bounds.Dy()
@@ -196,18 +172,6 @@ func (element *Label) updateMinimumSize () {
Mul(fixed.I(element.forcedRows)).Floor()
}
element.core.SetMinimumSize(width, height)
}
func (element *Label) draw () {
element.core.DrawBackground (
element.theme.Pattern(tomo.PatternBackground, tomo.State { }))
bounds := element.Bounds()
textBounds := element.drawer.LayoutBounds()
foreground := element.theme.Color (
tomo.ColorForeground,
tomo.State { })
element.drawer.Draw(element.core, foreground, bounds.Min.Sub(textBounds.Min))
element.minHeight = height
element.entity.SetMinimumSize(width, height)
}