2023-03-30 21:19:04 -06:00
|
|
|
package elements
|
2023-01-17 18:16:03 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-01-17 18:16:03 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
2023-01-17 18:16:03 -07:00
|
|
|
|
|
|
|
// 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
|
2023-01-17 18:16:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "progressBar")
|
2023-03-14 23:41:23 -06:00
|
|
|
element.Core, element.core = core.NewCore(element, element.draw)
|
2023-01-17 18:16:03 -07:00
|
|
|
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.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *ProgressBar) SetTheme (new tomo.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.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *ProgressBar) SetConfig (new tomo.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-03-30 23:06:29 -06:00
|
|
|
padding := element.theme.Padding(tomo.PatternSunken)
|
|
|
|
innerPadding := element.theme.Padding(tomo.PatternMercury)
|
2023-02-07 22:22:40 -07:00
|
|
|
element.core.SetMinimumSize (
|
2023-02-28 17:00:34 -07:00
|
|
|
padding.Horizontal() + innerPadding.Horizontal(),
|
|
|
|
padding.Vertical() + innerPadding.Vertical())
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ProgressBar) redo () {
|
2023-01-17 18:16:03 -07:00
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 18:16:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *ProgressBar) draw () {
|
2023-01-31 14:18:10 -07:00
|
|
|
bounds := element.Bounds()
|
2023-01-17 18:16:03 -07:00
|
|
|
|
2023-03-30 23:06:29 -06:00
|
|
|
pattern := element.theme.Pattern(tomo.PatternSunken, tomo.State { })
|
|
|
|
padding := element.theme.Padding(tomo.PatternSunken)
|
2023-02-26 20:20:17 -07:00
|
|
|
pattern.Draw(element.core, bounds)
|
|
|
|
bounds = padding.Apply(bounds)
|
2023-01-17 18:16:03 -07:00
|
|
|
meterBounds := image.Rect (
|
2023-01-28 23:49:01 -07:00
|
|
|
bounds.Min.X, bounds.Min.Y,
|
|
|
|
bounds.Min.X + int(float64(bounds.Dx()) * element.progress),
|
|
|
|
bounds.Max.Y)
|
2023-03-30 23:06:29 -06:00
|
|
|
mercury := element.theme.Pattern(tomo.PatternMercury, tomo.State { })
|
2023-03-11 23:15:36 -07:00
|
|
|
mercury.Draw(element.core, meterBounds)
|
2023-01-17 18:16:03 -07:00
|
|
|
}
|