summaryrefslogtreecommitdiff
path: root/progressbar.go
blob: c2cd289897e4f7115e210aa99636b7fefddadde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 25 february 2014

package ui

import (
	"sync"
)

// A ProgressBar is a horizontal rectangle that fills up from left to right to indicate the progress of a long-running task.
// 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.
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),
	}
}

// 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.
// Calling SetProgress(-1) repeatedly will neither leave indeterminate mode nor stop any animation involved in indeterminate mode indefinitely; any other side-effect of doing so is implementation-defined.
func (p *ProgressBar) SetProgress(percent int) {
	p.lock.Lock()
	defer p.lock.Unlock()

	if percent < -1 || percent > 100 {
		panic("percent value out of range")
	}
	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()

	err := p.sysData.make(window)
	if err != nil {
		return err
	}
	p.sysData.setProgress(p.initProg)
	p.created = true
	return nil
}

func (p *ProgressBar) allocate(x int, y int, width int, height int, d *sysSizeData) []*allocation {
	return []*allocation{&allocation{
		x:       x,
		y:       y,
		width:   width,
		height:  height,
		this:		p,
	}}
}

func (p *ProgressBar) preferredSize(d *sysSizeData) (width int, height int) {
	return p.sysData.preferredSize(d)
}

func (p *ProgressBar) commitResize(a *allocation, d *sysSizeData) {
	p.sysData.preferredSize(a, d)
}

func (p *ProgressBar) getAuxResizeInfo(d *sysSizeData) {
	p.sysData.getAuxResizeInfo(d)
}