This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/basic/label.go

168 lines
4.1 KiB
Go
Raw Normal View History

2023-02-02 06:48:16 +00:00
package basicElements
2023-01-09 06:03:19 +00:00
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-08 05:22:40 +00:00
import "git.tebibyte.media/sashakoshka/tomo/config"
2023-01-09 06:03:19 +00:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-01-10 02:25:11 +00:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
2023-01-09 06:03:19 +00:00
// Label is a simple text box.
2023-01-09 06:03:19 +00:00
type Label struct {
2023-01-10 02:25:11 +00:00
*core.Core
core core.CoreControl
wrap bool
2023-01-09 06:03:19 +00:00
text string
drawer artist.TextDrawer
2023-02-08 05:22:40 +00:00
config config.Config
theme theme.Theme
c theme.Case
onFlexibleHeightChange func ()
2023-01-09 06:03:19 +00:00
}
// NewLabel creates a new label. If wrap is set to true, the text inside will be
// wrapped.
func NewLabel (text string, wrap bool) (element *Label) {
2023-02-08 05:22:40 +00:00
element = &Label { c: theme.C("basic", "label") }
element.Core, element.core = core.NewCore(element.handleResize)
element.SetWrap(wrap)
2023-01-09 06:03:19 +00:00
element.SetText(text)
return
}
2023-02-07 16:27:59 +00:00
func (element *Label) redo () {
2023-02-08 05:22:40 +00:00
face := element.theme.FontFace (
2023-02-07 16:27:59 +00:00
theme.FontStyleRegular,
2023-02-08 05:22:40 +00:00
theme.FontSizeNormal,
element.c)
2023-02-07 16:27:59 +00:00
element.drawer.SetFace(face)
element.updateMinimumSize()
bounds := element.Bounds()
if element.wrap {
element.drawer.SetMaxWidth(bounds.Dx())
element.drawer.SetMaxHeight(bounds.Dy())
}
element.draw()
element.core.DamageAll()
}
2023-01-31 19:54:43 +00:00
func (element *Label) handleResize () {
bounds := element.Bounds()
if element.wrap {
2023-01-31 19:54:43 +00:00
element.drawer.SetMaxWidth(bounds.Dx())
element.drawer.SetMaxHeight(bounds.Dy())
2023-01-09 06:03:19 +00:00
}
element.draw()
2023-01-09 06:03:19 +00:00
return
}
// FlexibleHeightFor returns the reccomended height for this element based on
// the given width in order to allow the text to wrap properly.
func (element *Label) FlexibleHeightFor (width int) (height int) {
if element.wrap {
return element.drawer.ReccomendedHeightFor(width)
} else {
_, height = element.MinimumSize()
return
}
}
// 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.
2023-01-09 06:03:19 +00:00
func (element *Label) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText([]rune(text))
element.updateMinimumSize()
2023-01-09 06:03:19 +00:00
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
2023-01-09 06:03:19 +00:00
}
}
// SetWrap sets wether or not the label's text wraps. If the text is set to
// wrap, the element will have a minimum size of a single character and
// automatically wrap its text. If the text is set to not wrap, the element will
// have a minimum size that fits its text.
func (element *Label) SetWrap (wrap bool) {
if wrap == element.wrap { return }
if !wrap {
element.drawer.SetMaxWidth(0)
element.drawer.SetMaxHeight(0)
}
element.wrap = wrap
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
2023-02-08 05:22:40 +00:00
// SetTheme sets the element's theme.
func (element *Label) SetTheme (new theme.Theme) {
element.theme = new
element.drawer.SetFace (element.theme.FontFace (
theme.FontStyleRegular,
theme.FontSizeNormal,
element.c))
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
// SetConfig sets the element's configuration.
func (element *Label) SetConfig (new config.Config) {
element.config = new
element.updateMinimumSize()
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
func (element *Label) updateMinimumSize () {
if element.wrap {
em := element.drawer.Em().Round()
2023-02-08 05:22:40 +00:00
if em < 1 { em = element.config.Padding() }
element.core.SetMinimumSize (
em, element.drawer.LineHeight().Round())
if element.onFlexibleHeightChange != nil {
element.onFlexibleHeightChange()
}
} else {
bounds := element.drawer.LayoutBounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
}
}
2023-01-09 06:03:19 +00:00
func (element *Label) draw () {
bounds := element.Bounds()
2023-01-09 06:03:19 +00:00
2023-02-08 05:22:40 +00:00
pattern := element.theme.Pattern (
2023-02-07 16:27:59 +00:00
theme.PatternBackground,
2023-02-08 05:22:40 +00:00
element.c,
2023-02-07 16:27:59 +00:00
theme.PatternState { })
artist.FillRectangle(element, pattern, bounds)
2023-01-09 06:03:19 +00:00
textBounds := element.drawer.LayoutBounds()
2023-02-08 05:22:40 +00:00
foreground := element.theme.Pattern (
2023-02-07 16:27:59 +00:00
theme.PatternForeground,
2023-02-08 05:22:40 +00:00
element.c,
2023-02-07 16:27:59 +00:00
theme.PatternState { })
2023-02-08 05:22:40 +00:00
element.drawer.Draw(element, foreground, bounds.Min.Sub(textBounds.Min))
2023-01-09 06:03:19 +00:00
}