This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
/
progress_bar.go
82 lines (67 loc) · 1.78 KB
/
progress_bar.go
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
80
81
82
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mixins
import (
"github.com/google/gxui"
"github.com/google/gxui/math"
"github.com/google/gxui/mixins/base"
"github.com/google/gxui/mixins/parts"
)
type ProgressBarOuter interface {
base.ControlOuter
PaintProgress(gxui.Canvas, math.Rect, float32)
}
type ProgressBar struct {
base.Control
parts.BackgroundBorderPainter
outer ProgressBarOuter
desiredSize math.Size
progress, target int
}
func (b *ProgressBar) Init(outer ProgressBarOuter, theme gxui.Theme) {
b.outer = outer
b.Control.Init(outer, theme)
b.BackgroundBorderPainter.Init(outer)
b.desiredSize = math.MaxSize
b.target = 100
// Interface compliance test
_ = gxui.ProgressBar(b)
}
func (b *ProgressBar) Paint(c gxui.Canvas) {
frac := math.Saturate(float32(b.progress) / float32(b.target))
r := b.outer.Size().Rect()
b.PaintBackground(c, r)
b.outer.PaintProgress(c, r, frac)
b.PaintBorder(c, r)
}
func (b *ProgressBar) PaintProgress(c gxui.Canvas, r math.Rect, frac float32) {
r.Max.X = math.Lerp(r.Min.X, r.Max.X, frac)
c.DrawRect(r, gxui.CreateBrush(gxui.Gray50))
}
func (b *ProgressBar) DesiredSize(min, max math.Size) math.Size {
return b.desiredSize.Clamp(min, max)
}
// gxui.ProgressBar compliance
func (b *ProgressBar) SetDesiredSize(size math.Size) {
b.desiredSize = size
b.Relayout()
}
func (b *ProgressBar) SetProgress(progress int) {
if b.progress != progress {
b.progress = progress
b.Redraw()
}
}
func (b *ProgressBar) Progress() int {
return b.progress
}
func (b *ProgressBar) SetTarget(target int) {
if b.target != target {
b.target = target
b.Redraw()
}
}
func (b *ProgressBar) Target() int {
return b.target
}