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

65 lines
1.7 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"
import "git.tebibyte.media/sashakoshka/tomo/artist"
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
}
// NewProgressBar creates a new progress bar displaying the given progress
// level.
func NewProgressBar (progress float64) (element *ProgressBar) {
element = &ProgressBar { progress: progress }
2023-02-07 09:27:59 -07:00
element.Core, element.core = core.NewCore (
element.draw,
element.redo,
element.redo,
theme.C("basic", "progressBar"))
return
}
2023-02-07 09:27:59 -07:00
func (element *ProgressBar) redo () {
element.core.SetMinimumSize (
element.core.Config().Padding() * 2,
element.core.Config().Padding() * 2)
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
}
// SetProgress sets the progress level of the bar.
func (element *ProgressBar) SetProgress (progress float64) {
if progress == element.progress { return }
element.progress = progress
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
}
func (element *ProgressBar) draw () {
bounds := element.Bounds()
2023-02-07 09:27:59 -07:00
pattern := element.core.Pattern (
theme.PatternSunken,
theme.PatternState { })
inset := element.core.Inset(theme.PatternSunken)
artist.FillRectangle(element, pattern, bounds)
bounds = inset.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-07 09:27:59 -07:00
accent := element.core.Pattern (
theme.PatternSunken,
theme.PatternState { })
artist.FillRectangle(element, accent, meterBounds)
}