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
Sasha Koshka 92aeb48a1f Overhauled the theme system
Also added a toggle switch :)
2023-01-29 01:49:01 -05:00

54 lines
1.5 KiB
Go

package basic
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 }
element.Core, element.core = core.NewCore(element)
element.core.SetMinimumSize(theme.Padding() * 2, theme.Padding() * 2)
return
}
// Resize resizes the progress bar.
func (element *ProgressBar) Resize (width, height int) {
element.core.AllocateCanvas(width, height)
element.draw()
return
}
// 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.core.Bounds()
pattern, inset := theme.SunkenPattern(theme.PatternState { })
artist.FillRectangle(element.core, 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)
accent, _ := theme.AccentPattern(theme.PatternState { })
artist.FillRectangle(element.core, accent, meterBounds)
}