From 6258c77f868d2274d4c56a3c0ac99441a0a4b3f8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 16 Mar 2023 15:58:26 -0400 Subject: [PATCH] Added an align method to label --- elements/basic/label.go | 15 +++++++++++++++ examples/align/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 examples/align/main.go diff --git a/elements/basic/label.go b/elements/basic/label.go index bd99b56..566be37 100644 --- a/elements/basic/label.go +++ b/elements/basic/label.go @@ -11,6 +11,7 @@ type Label struct { *core.Core core core.CoreControl + align textdraw.Align wrap bool text string drawer textdraw.Drawer @@ -121,6 +122,20 @@ func (element *Label) SetWrap (wrap bool) { } } +// 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() + + if element.core.HasImage () { + element.draw() + element.core.DamageAll() + } +} + // SetTheme sets the element's theme. func (element *Label) SetTheme (new theme.Theme) { if new == element.theme.Theme { return } diff --git a/examples/align/main.go b/examples/align/main.go new file mode 100644 index 0000000..7757e49 --- /dev/null +++ b/examples/align/main.go @@ -0,0 +1,41 @@ +package main + +import "git.tebibyte.media/sashakoshka/tomo" +import "git.tebibyte.media/sashakoshka/tomo/textdraw" +import "git.tebibyte.media/sashakoshka/tomo/elements/basic" +import _ "git.tebibyte.media/sashakoshka/tomo/backends/all" +import "git.tebibyte.media/sashakoshka/tomo/elements/containers" + +func main () { + tomo.Run(run) +} + +func run () { + window, _ := tomo.NewWindow(360, 360) + window.SetTitle("Text alignment") + + container := containers.NewDocumentContainer() + scrollContainer := containers.NewScrollContainer(false, true) + scrollContainer.Adopt(container) + window.Adopt(scrollContainer) + + left := basicElements.NewLabel(text, true) + center := basicElements.NewLabel(text, true) + right := basicElements.NewLabel(text, true) + justify := basicElements.NewLabel(text, true) + + left.SetAlign(textdraw.AlignLeft) + center.SetAlign(textdraw.AlignCenter) + right.SetAlign(textdraw.AlignRight) + justify.SetAlign(textdraw.AlignJustify) + + container.Adopt(left) + container.Adopt(center) + container.Adopt(right) + container.Adopt(justify) + + window.OnClose(tomo.Stop) + window.Show() +} + +const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Fermentum et sollicitudin ac orci phasellus egestas tellus rutrum. Aliquam vestibulum morbi blandit cursus risus at ultrices mi."