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-05-02 20:19:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-01-17 18:16:03 -07:00
|
|
|
|
2023-05-02 23:07:44 -06:00
|
|
|
var progressBarCase = tomo.C("tomo", "progressBar")
|
|
|
|
|
2023-01-17 18:16:03 -07:00
|
|
|
// ProgressBar displays a visual indication of how far along a task is.
|
|
|
|
type ProgressBar struct {
|
2023-04-14 23:45:11 -06:00
|
|
|
entity tomo.Entity
|
2023-01-17 18:16:03 -07:00
|
|
|
progress float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgressBar creates a new progress bar displaying the given progress
|
|
|
|
// level.
|
|
|
|
func NewProgressBar (progress float64) (element *ProgressBar) {
|
2023-04-18 11:14:10 -06:00
|
|
|
if progress < 0 { progress = 0 }
|
|
|
|
if progress > 1 { progress = 1 }
|
2023-02-08 12:36:14 -07:00
|
|
|
element = &ProgressBar { progress: progress }
|
2023-05-02 23:07:44 -06:00
|
|
|
element.entity = tomo.GetBackend().NewEntity(element)
|
2023-04-03 14:12:53 -06:00
|
|
|
element.updateMinimumSize()
|
2023-01-17 18:16:03 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:45:11 -06:00
|
|
|
// Entity returns this element's entity.
|
|
|
|
func (element *ProgressBar) Entity () tomo.Entity {
|
|
|
|
return element.entity
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw causes the element to draw to the specified destination canvas.
|
2023-05-02 20:19:29 -06:00
|
|
|
func (element *ProgressBar) Draw (destination artist.Canvas) {
|
2023-04-14 23:45:11 -06:00
|
|
|
bounds := element.entity.Bounds()
|
|
|
|
|
2023-05-02 23:07:44 -06:00
|
|
|
pattern := element.entity.Theme().Pattern(tomo.PatternSunken, tomo.State { }, progressBarCase)
|
|
|
|
padding := element.entity.Theme().Padding(tomo.PatternSunken, progressBarCase)
|
2023-04-14 23:45:11 -06:00
|
|
|
pattern.Draw(destination, 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-05-02 23:07:44 -06:00
|
|
|
mercury := element.entity.Theme().Pattern(tomo.PatternMercury, tomo.State { }, progressBarCase)
|
2023-04-14 23:45:11 -06:00
|
|
|
mercury.Draw(destination, meterBounds)
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
// SetProgress sets the progress level of the bar.
|
|
|
|
func (element *ProgressBar) SetProgress (progress float64) {
|
2023-04-18 11:14:10 -06:00
|
|
|
if progress < 0 { progress = 0 }
|
|
|
|
if progress > 1 { progress = 1 }
|
2023-02-07 22:22:40 -07:00
|
|
|
if progress == element.progress { return }
|
|
|
|
element.progress = progress
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 09:27:59 -07:00
|
|
|
}
|
|
|
|
|
2023-05-02 23:07:44 -06:00
|
|
|
func (element *ProgressBar) HandleThemeChange () {
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:12:53 -06:00
|
|
|
func (element *ProgressBar) updateMinimumSize() {
|
2023-05-02 23:07:44 -06:00
|
|
|
padding := element.entity.Theme().Padding(tomo.PatternSunken, progressBarCase)
|
|
|
|
innerPadding := element.entity.Theme().Padding(tomo.PatternMercury, progressBarCase)
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.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
|
|
|
}
|