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/basic/progressbar.go

83 lines
2.3 KiB
Go
Raw Normal View History

2023-02-01 23:48:16 -07:00
package basicElements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-07 22:22:40 -07:00
import "git.tebibyte.media/sashakoshka/tomo/config"
2023-02-26 20:20:17 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// ProgressBar displays a visual indication of how far along a task is.
type ProgressBar struct {
*core.Core
core core.CoreControl
progress float64
2023-02-07 22:22:40 -07:00
2023-02-08 12:36:14 -07:00
config config.Wrapped
theme theme.Wrapped
}
// NewProgressBar creates a new progress bar displaying the given progress
// level.
func NewProgressBar (progress float64) (element *ProgressBar) {
2023-02-08 12:36:14 -07:00
element = &ProgressBar { progress: progress }
element.theme.Case = theme.C("basic", "progressBar")
2023-02-07 22:22:40 -07:00
element.Core, element.core = core.NewCore(element.draw)
return
}
2023-02-07 22:22:40 -07:00
// SetProgress sets the progress level of the bar.
func (element *ProgressBar) SetProgress (progress float64) {
if progress == element.progress { return }
element.progress = progress
2023-02-07 09:27:59 -07:00
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
}
2023-02-07 22:22:40 -07:00
// SetTheme sets the element's theme.
func (element *ProgressBar) SetTheme (new theme.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.updateMinimumSize()
element.redo()
}
// SetConfig sets the element's configuration.
func (element *ProgressBar) SetConfig (new config.Config) {
2023-02-08 12:36:14 -07:00
if new == nil || new == element.config.Config { return }
element.config.Config = new
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
element.redo()
}
func (element (ProgressBar)) updateMinimumSize() {
2023-02-26 20:20:17 -07:00
padding := element.theme.Padding(theme.PatternSunken)
2023-02-07 22:22:40 -07:00
element.core.SetMinimumSize (
2023-02-26 20:20:17 -07:00
padding[3] + padding[1],
padding[0] + padding[2])
2023-02-07 22:22:40 -07:00
}
func (element *ProgressBar) redo () {
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
}
func (element *ProgressBar) draw () {
bounds := element.Bounds()
2023-02-26 20:20:17 -07:00
pattern := element.theme.Pattern(theme.PatternSunken, theme.State { })
padding := element.theme.Padding(theme.PatternSunken)
pattern.Draw(element.core, bounds)
bounds = padding.Apply(bounds)
meterBounds := image.Rect (
bounds.Min.X, bounds.Min.Y,
bounds.Min.X + int(float64(bounds.Dx()) * element.progress),
bounds.Max.Y)
2023-02-26 20:20:17 -07:00
// TODO: maybe dont use the accent color here...
accent := element.theme.Color(theme.ColorAccent, theme.State { })
shapes.FillColorRectangle(element.core, accent, meterBounds)
}