Added untested label collapse

This commit is contained in:
Sasha Koshka 2023-03-13 17:10:27 -04:00
parent 7ef95cc751
commit 5149c27cf3
2 changed files with 46 additions and 4 deletions

View File

@ -1,5 +1,6 @@
package basicElements
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
@ -13,6 +14,9 @@ type Label struct {
wrap bool
text string
drawer textdraw.Drawer
forcedColumns int
forcedRows int
config config.Wrapped
theme theme.Wrapped
@ -56,6 +60,17 @@ func (element *Label) handleResize () {
return
}
// 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) {
@ -134,20 +149,35 @@ func (element *Label) SetConfig (new config.Config) {
}
func (element *Label) updateMinimumSize () {
var width, height int
if element.wrap {
em := element.drawer.Em().Round()
if em < 1 {
em = element.theme.Padding(theme.PatternBackground)[0]
}
element.core.SetMinimumSize (
em, element.drawer.LineHeight().Round())
width, height = em, element.drawer.LineHeight().Round()
if element.onFlexibleHeightChange != nil {
element.onFlexibleHeightChange()
}
} else {
bounds := element.drawer.LayoutBounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
width, height = bounds.Dx(), bounds.Dy()
}
if element.forcedColumns > 0 {
width = int (
element.drawer.Em().
Mul(fixed.I(element.forcedColumns)))
}
if element.forcedRows > 0 {
height = int (
element.drawer.LineHeight().
Mul(fixed.I(element.forcedRows)))
}
element.core.SetMinimumSize(width, height)
}
func (element *Label) draw () {
@ -160,7 +190,7 @@ func (element *Label) draw () {
textBounds := element.drawer.LayoutBounds()
foreground := element.theme.Color (
foreground := element.theme.Color (
theme.ColorForeground,
theme.State { })
element.drawer.Draw(element.core, foreground, bounds.Min.Sub(textBounds.Min))

View File

@ -4,6 +4,7 @@ import "os"
import "image"
import _ "image/png"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
@ -44,6 +45,17 @@ func run () {
"lay out a settings menu with descriptive label text between " +
"control groups like in iOS, or list comment or chat histories.", true))
document.Adopt(basicElements.NewImage(logo))
document.Adopt (basicElements.NewLabel (
"Oh, you're a switch? Then name all of these switches:", true))
for i := 0; i < 3; i ++ {
switchContainer := basicElements.NewContainer (basicLayouts.Horizontal {
Gap: true,
})
for i := 0; i < 10; i ++ {
switchContainer.Adopt(basicElements.NewSwitch("", false), true)
}
document.Adopt(switchContainer)
}
scrollContainer.Adopt(document)
window.Adopt(scrollContainer)