flexible-elements-were-a-mistake #11
| @ -1,5 +1,6 @@ | |||||||
| package basicElements | package basicElements | ||||||
| 
 | 
 | ||||||
|  | import "golang.org/x/image/math/fixed" | ||||||
| import "git.tebibyte.media/sashakoshka/tomo/theme" | import "git.tebibyte.media/sashakoshka/tomo/theme" | ||||||
| import "git.tebibyte.media/sashakoshka/tomo/config" | import "git.tebibyte.media/sashakoshka/tomo/config" | ||||||
| import "git.tebibyte.media/sashakoshka/tomo/textdraw" | import "git.tebibyte.media/sashakoshka/tomo/textdraw" | ||||||
| @ -14,6 +15,9 @@ type Label struct { | |||||||
| 	text   string | 	text   string | ||||||
| 	drawer textdraw.Drawer | 	drawer textdraw.Drawer | ||||||
| 
 | 
 | ||||||
|  | 	forcedColumns int | ||||||
|  | 	forcedRows    int | ||||||
|  | 	 | ||||||
| 	config config.Wrapped | 	config config.Wrapped | ||||||
| 	theme  theme.Wrapped | 	theme  theme.Wrapped | ||||||
| 	 | 	 | ||||||
| @ -56,6 +60,17 @@ func (element *Label) handleResize () { | |||||||
| 	return | 	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 | // FlexibleHeightFor returns the reccomended height for this element based on | ||||||
| // the given width in order to allow the text to wrap properly. | // the given width in order to allow the text to wrap properly. | ||||||
| func (element *Label) FlexibleHeightFor (width int) (height int) { | func (element *Label) FlexibleHeightFor (width int) (height int) { | ||||||
| @ -134,20 +149,35 @@ func (element *Label) SetConfig (new config.Config) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (element *Label) updateMinimumSize () { | func (element *Label) updateMinimumSize () { | ||||||
|  | 	var width, height int | ||||||
|  | 	 | ||||||
| 	if element.wrap { | 	if element.wrap { | ||||||
| 		em := element.drawer.Em().Round() | 		em := element.drawer.Em().Round() | ||||||
| 		if em < 1 { | 		if em < 1 { | ||||||
| 			em = element.theme.Padding(theme.PatternBackground)[0] | 			em = element.theme.Padding(theme.PatternBackground)[0] | ||||||
| 		} | 		} | ||||||
| 		element.core.SetMinimumSize ( | 		width, height = em, element.drawer.LineHeight().Round() | ||||||
| 			em, element.drawer.LineHeight().Round()) |  | ||||||
| 		if element.onFlexibleHeightChange != nil { | 		if element.onFlexibleHeightChange != nil { | ||||||
| 			element.onFlexibleHeightChange() | 			element.onFlexibleHeightChange() | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		bounds := element.drawer.LayoutBounds() | 		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 () { | func (element *Label) draw () { | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ import "os" | |||||||
| import "image" | import "image" | ||||||
| import _ "image/png" | import _ "image/png" | ||||||
| import "git.tebibyte.media/sashakoshka/tomo" | 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/elements/basic" | ||||||
| import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" | import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" | ||||||
| 
 | 
 | ||||||
| @ -44,6 +45,17 @@ func run () { | |||||||
| 		"lay out a settings menu with descriptive label text between " + | 		"lay out a settings menu with descriptive label text between " + | ||||||
| 		"control groups like in iOS, or list comment or chat histories.", true)) | 		"control groups like in iOS, or list comment or chat histories.", true)) | ||||||
| 	document.Adopt(basicElements.NewImage(logo)) | 	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) | 	scrollContainer.Adopt(document) | ||||||
| 	window.Adopt(scrollContainer) | 	window.Adopt(scrollContainer) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user