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

83 lines
2.2 KiB
Go
Raw Normal View History

2023-03-31 03:19:04 +00:00
package elements
2023-01-17 22:01:35 +00:00
2023-03-31 05:06:29 +00:00
import "git.tebibyte.media/sashakoshka/tomo"
2023-04-15 05:45:11 +00:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-03-31 05:06:29 +00:00
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
import "git.tebibyte.media/sashakoshka/tomo/default/config"
2023-01-17 22:01:35 +00:00
// Spacer can be used to put space between two elements..
type Spacer struct {
2023-04-15 05:45:11 +00:00
entity tomo.Entity
2023-01-17 22:01:35 +00:00
line bool
2023-02-08 05:22:40 +00:00
2023-02-08 19:36:14 +00:00
config config.Wrapped
theme theme.Wrapped
2023-01-17 22:01:35 +00:00
}
// NewSpacer creates a new spacer. If line is set to true, the spacer will be
// filled with a line color, and if compressed to its minimum width or height,
// will appear as a line.
func NewSpacer (line bool) (element *Spacer) {
2023-02-08 19:36:14 +00:00
element = &Spacer { line: line }
2023-04-15 05:45:11 +00:00
element.entity = tomo.NewEntity(element)
2023-03-31 05:06:29 +00:00
element.theme.Case = tomo.C("tomo", "spacer")
element.updateMinimumSize()
2023-01-17 22:01:35 +00:00
return
}
2023-04-15 05:45:11 +00:00
// Entity returns this element's entity.
func (element *Spacer) Entity () tomo.Entity {
return element.entity
}
// Draw causes the element to draw to the specified destination canvas.
func (element *Spacer) Draw (destination canvas.Canvas) {
bounds := element.entity.Bounds()
if element.line {
pattern := element.theme.Pattern (
tomo.PatternLine,
tomo.State { })
pattern.Draw(destination, bounds)
} else {
pattern := element.theme.Pattern (
tomo.PatternBackground,
tomo.State { })
pattern.Draw(destination, bounds)
}
}
2023-01-17 22:01:35 +00:00
/// SetLine sets whether or not the spacer will appear as a colored line.
func (element *Spacer) SetLine (line bool) {
if element.line == line { return }
element.line = line
element.updateMinimumSize()
2023-04-15 05:45:11 +00:00
element.entity.Invalidate()
2023-01-17 22:01:35 +00:00
}
2023-02-08 05:22:40 +00:00
// SetTheme sets the element's theme.
2023-03-31 05:06:29 +00:00
func (element *Spacer) SetTheme (new tomo.Theme) {
2023-02-08 19:36:14 +00:00
if new == element.theme.Theme { return }
element.theme.Theme = new
2023-04-15 05:45:11 +00:00
element.entity.Invalidate()
2023-02-08 05:22:40 +00:00
}
// SetConfig sets the element's configuration.
2023-03-31 05:06:29 +00:00
func (element *Spacer) SetConfig (new tomo.Config) {
2023-02-08 19:36:14 +00:00
if new == element.config.Config { return }
element.config.Config = new
2023-04-15 05:45:11 +00:00
element.entity.Invalidate()
2023-02-08 05:22:40 +00:00
}
func (element *Spacer) updateMinimumSize () {
if element.line {
2023-03-31 05:06:29 +00:00
padding := element.theme.Padding(tomo.PatternLine)
2023-04-15 05:45:11 +00:00
element.entity.SetMinimumSize (
padding.Horizontal(),
padding.Vertical())
} else {
2023-04-15 05:45:11 +00:00
element.entity.SetMinimumSize(1, 1)
2023-01-17 22:01:35 +00:00
}
}