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/label.go

222 lines
5.8 KiB
Go
Raw Normal View History

2023-03-30 21:19:04 -06:00
package elements
2023-01-08 23:03:19 -07:00
2023-04-20 22:52:34 -06:00
import "image"
2023-03-13 15:10:27 -06:00
import "golang.org/x/image/math/fixed"
2023-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo"
2023-04-20 22:52:34 -06:00
import "git.tebibyte.media/sashakoshka/tomo/data"
import "git.tebibyte.media/sashakoshka/tomo/input"
2023-04-14 20:03:22 -06:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-02-15 16:45:58 -07:00
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
2023-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
import "git.tebibyte.media/sashakoshka/tomo/default/config"
2023-01-08 23:03:19 -07:00
// Label is a simple text box.
2023-01-08 23:03:19 -07:00
type Label struct {
2023-04-14 20:03:22 -06:00
entity tomo.FlexibleEntity
2023-03-16 13:58:26 -06:00
align textdraw.Align
wrap bool
2023-01-08 23:03:19 -07:00
text string
2023-02-15 16:45:58 -07:00
drawer textdraw.Drawer
2023-03-13 15:10:27 -06:00
forcedColumns int
forcedRows int
2023-04-14 20:03:22 -06:00
minHeight int
2023-02-08 12:36:14 -07:00
config config.Wrapped
theme theme.Wrapped
2023-01-08 23:03:19 -07:00
}
// NewLabel creates a new label.
func NewLabel (text string) (element *Label) {
2023-02-08 12:36:14 -07:00
element = &Label { }
2023-03-30 23:06:29 -06:00
element.theme.Case = tomo.C("tomo", "label")
element.entity = tomo.NewEntity(element).(tomo.FlexibleEntity)
2023-03-30 23:06:29 -06:00
element.drawer.SetFace (element.theme.FontFace (
tomo.FontStyleRegular,
tomo.FontSizeNormal))
2023-01-08 23:03:19 -07:00
element.SetText(text)
return
}
// NewLabelWrapped creates a new label with text wrapping on.
func NewLabelWrapped (text string) (element *Label) {
element = NewLabel(text)
element.SetWrap(true)
return
}
// Entity returns this element's entity.
func (element *Label) Entity () tomo.Entity {
return element.entity
2023-01-08 23:03:19 -07:00
}
2023-04-20 22:52:34 -06:00
// Draw causes the element to draw to the specified destination canvas.
func (element *Label) Draw (destination canvas.Canvas) {
bounds := element.entity.Bounds()
if element.wrap {
element.drawer.SetMaxWidth(bounds.Dx())
element.drawer.SetMaxHeight(bounds.Dy())
}
element.entity.DrawBackground(destination)
textBounds := element.drawer.LayoutBounds()
foreground := element.theme.Color (
tomo.ColorForeground,
tomo.State { })
element.drawer.Draw(destination, foreground, bounds.Min.Sub(textBounds.Min))
}
// Copy copies the label's textto the clipboard.
func (element *Label) Copy () {
window := element.entity.Window()
if window != nil {
window.Copy(data.Bytes(data.MimePlain, []byte(element.text)))
}
}
2023-03-13 15:10:27 -06:00
// EmCollapse forces a minimum width and height upon the label. The width is
// measured in emspaces, and the height is measured in lines. If a zero value is
// given for a dimension, its minimum will be determined by the label's content.
// If the label's content is greater than these dimensions, it will be truncated
// to fit.
func (element *Label) EmCollapse (columns int, rows int) {
element.forcedColumns = columns
element.forcedRows = rows
element.updateMinimumSize()
}
// 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 {
2023-04-14 20:03:22 -06:00
return element.minHeight
}
}
// SetText sets the label's text.
2023-01-08 23:03:19 -07:00
func (element *Label) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText([]rune(text))
element.updateMinimumSize()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
2023-01-08 23:03:19 -07: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()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
}
2023-03-16 13:58:26 -06:00
// 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)
element.updateMinimumSize()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
2023-03-16 13:58:26 -06:00
}
2023-02-07 22:22:40 -07:00
// SetTheme sets the element's theme.
2023-03-30 23:06:29 -06:00
func (element *Label) SetTheme (new tomo.Theme) {
2023-02-08 12:36:14 -07:00
if new == element.theme.Theme { return }
element.theme.Theme = new
2023-02-07 22:22:40 -07:00
element.drawer.SetFace (element.theme.FontFace (
2023-03-30 23:06:29 -06:00
tomo.FontStyleRegular,
tomo.FontSizeNormal))
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
2023-02-07 22:22:40 -07:00
}
// SetConfig sets the element's configuration.
2023-03-30 23:06:29 -06:00
func (element *Label) SetConfig (new tomo.Config) {
2023-02-08 12:36:14 -07:00
if new == element.config.Config { return }
element.config.Config = new
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
}
2023-04-20 22:52:34 -06:00
func (element *Label) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
if button == input.ButtonRight {
element.contextMenu(position)
2023-02-07 22:22:40 -07:00
}
2023-04-20 22:52:34 -06:00
}
2023-04-14 20:03:22 -06:00
2023-04-20 22:52:34 -06:00
func (element *Label) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) { }
func (element *Label) contextMenu (position image.Point) {
window := element.entity.Window()
menu, err := window.NewMenu(image.Rectangle { position, position })
if err != nil { return }
closeAnd := func (callback func ()) func () {
return func () { callback(); menu.Close() }
}
copyButton := NewButton("Copy")
copyButton.ShowText(false)
copyButton.SetIcon(tomo.IconCopy)
copyButton.OnClick(closeAnd(element.Copy))
menu.Adopt (NewHBox (
SpaceNone,
copyButton,
))
copyButton.Focus()
menu.Show()
2023-02-07 22:22:40 -07:00
}
func (element *Label) updateMinimumSize () {
2023-03-13 15:10:27 -06:00
var width, height int
if element.wrap {
em := element.drawer.Em().Round()
2023-02-26 20:20:17 -07:00
if em < 1 {
2023-03-30 23:06:29 -06:00
em = element.theme.Padding(tomo.PatternBackground)[0]
2023-02-26 20:20:17 -07:00
}
2023-03-13 15:10:27 -06:00
width, height = em, element.drawer.LineHeight().Round()
element.entity.NotifyFlexibleHeightChange()
} else {
bounds := element.drawer.LayoutBounds()
2023-03-13 15:10:27 -06:00
width, height = bounds.Dx(), bounds.Dy()
}
2023-03-13 15:10:27 -06:00
if element.forcedColumns > 0 {
2023-03-16 23:00:11 -06:00
width =
2023-03-13 15:10:27 -06:00
element.drawer.Em().
2023-03-16 23:00:11 -06:00
Mul(fixed.I(element.forcedColumns)).Floor()
2023-03-13 15:10:27 -06:00
}
if element.forcedRows > 0 {
2023-03-16 23:00:11 -06:00
height =
2023-03-13 15:10:27 -06:00
element.drawer.LineHeight().
2023-03-16 23:00:11 -06:00
Mul(fixed.I(element.forcedRows)).Floor()
2023-03-13 15:10:27 -06:00
}
2023-04-14 20:03:22 -06:00
element.minHeight = height
element.entity.SetMinimumSize(width, height)
2023-01-08 23:03:19 -07:00
}