2014-02-24 23:13:47 -06:00
|
|
|
// 25 february 2014
|
2014-03-12 20:55:45 -05:00
|
|
|
|
2014-02-24 23:13:47 -06:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2014-02-24 23:48:23 -06:00
|
|
|
"sync"
|
2014-02-24 23:13:47 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// A ProgressBar is a horizontal rectangle that fills up from left to right to indicate the progress of a long-running task.
|
2014-03-12 16:12:41 -05:00
|
|
|
// This progress is represented by an integer within the range [0,100], representing a percentage.
|
|
|
|
// Alternatively, a progressbar can show an animation indicating that progress is being made but how much is indeterminate.
|
|
|
|
// Newly-created ProgressBars default to showing 0% progress.
|
2014-02-24 23:13:47 -06:00
|
|
|
type ProgressBar struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
created bool
|
|
|
|
sysData *sysData
|
|
|
|
initProg int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgressBar creates a new ProgressBar.
|
|
|
|
func NewProgressBar() *ProgressBar {
|
|
|
|
return &ProgressBar{
|
|
|
|
sysData: mksysdata(c_progressbar),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 16:12:41 -05:00
|
|
|
// SetProgress sets the currently indicated progress amount on the ProgressBar.
|
|
|
|
// If percent is in the range [0,100], the progressBar shows that much percent complete.
|
|
|
|
// If percent is -1, the ProgressBar is made indeterminate.
|
|
|
|
// Otherwise, SetProgress panics.
|
2014-03-12 16:31:13 -05:00
|
|
|
// TODO what happens if you repeatedly call SetProgress(-1)?
|
2014-02-24 23:13:47 -06:00
|
|
|
func (p *ProgressBar) SetProgress(percent int) {
|
2014-03-18 10:50:56 -05:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
2014-03-12 16:12:41 -05:00
|
|
|
if percent < -1 || percent > 100 {
|
|
|
|
panic("percent value out of range")
|
2014-02-24 23:31:48 -06:00
|
|
|
}
|
2014-02-24 23:13:47 -06:00
|
|
|
if p.created {
|
|
|
|
p.sysData.setProgress(percent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.initProg = percent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProgressBar) make(window *sysData) error {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
2014-04-01 15:43:56 -05:00
|
|
|
err := p.sysData.make(window)
|
2014-02-24 23:13:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.sysData.setProgress(p.initProg)
|
|
|
|
p.created = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-17 20:09:03 -05:00
|
|
|
func (p *ProgressBar) setRect(x int, y int, width int, height int, rr *[]resizerequest) {
|
|
|
|
*rr = append(*rr, resizerequest{
|
2014-03-17 19:42:36 -05:00
|
|
|
sysData: p.sysData,
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
2014-03-17 20:09:03 -05:00
|
|
|
})
|
2014-02-24 23:13:47 -06:00
|
|
|
}
|
|
|
|
|
2014-03-09 18:44:41 -05:00
|
|
|
func (p *ProgressBar) preferredSize() (width int, height int) {
|
|
|
|
return p.sysData.preferredSize()
|
2014-02-24 23:13:47 -06:00
|
|
|
}
|