forked from fyne-io/fyne
/
progressbar.go
180 lines (151 loc) · 4.84 KB
/
progressbar.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package widget
import (
"image/color"
"strconv"
"github.com/nemesis567/fyne"
"github.com/nemesis567/fyne/canvas"
"github.com/nemesis567/fyne/data/binding"
"github.com/nemesis567/fyne/internal/cache"
col "github.com/nemesis567/fyne/internal/color"
"github.com/nemesis567/fyne/internal/widget"
"github.com/nemesis567/fyne/theme"
)
type progressRenderer struct {
widget.BaseRenderer
background, bar *canvas.Rectangle
label *canvas.Text
progress *ProgressBar
}
// MinSize calculates the minimum size of a progress bar.
// This is simply the "100%" label size plus padding.
func (p *progressRenderer) MinSize() fyne.Size {
var tsize fyne.Size
if text := p.progress.TextFormatter; text != nil {
tsize = fyne.MeasureText(text(), p.label.TextSize, p.label.TextStyle)
} else {
tsize = fyne.MeasureText("100%", p.label.TextSize, p.label.TextStyle)
}
return fyne.NewSize(tsize.Width+theme.InnerPadding()*2, tsize.Height+theme.InnerPadding()*2)
}
func (p *progressRenderer) updateBar() {
if p.progress.Value < p.progress.Min {
p.progress.Value = p.progress.Min
}
if p.progress.Value > p.progress.Max {
p.progress.Value = p.progress.Max
}
delta := float32(p.progress.Max - p.progress.Min)
ratio := float32(p.progress.Value-p.progress.Min) / delta
if text := p.progress.TextFormatter; text != nil {
p.label.Text = text()
} else {
p.label.Text = strconv.Itoa(int(ratio*100)) + "%"
}
size := p.progress.Size()
p.bar.Resize(fyne.NewSize(size.Width*ratio, size.Height))
}
// Layout the components of the check widget
func (p *progressRenderer) Layout(size fyne.Size) {
p.background.Resize(size)
p.label.Resize(size)
p.updateBar()
}
// applyTheme updates the progress bar to match the current theme
func (p *progressRenderer) applyTheme() {
p.background.FillColor = progressBackgroundColor()
p.bar.FillColor = theme.PrimaryColor()
p.label.Color = theme.BackgroundColor()
p.label.TextSize = theme.TextSize()
}
func (p *progressRenderer) Refresh() {
p.applyTheme()
p.updateBar()
p.background.Refresh()
p.bar.Refresh()
canvas.Refresh(p.progress.super())
}
// ProgressBar widget creates a horizontal panel that indicates progress
type ProgressBar struct {
BaseWidget
Min, Max, Value float64
// TextFormatter can be used to have a custom format of progress text.
// If set, it overrides the percentage readout and runs each time the value updates.
//
// Since: 1.4
TextFormatter func() string `json:"-"`
binder basicBinder
}
// Bind connects the specified data source to this ProgressBar.
// The current value will be displayed and any changes in the data will cause the widget to update.
//
// Since: 2.0
func (p *ProgressBar) Bind(data binding.Float) {
p.binder.SetCallback(p.updateFromData)
p.binder.Bind(data)
}
// SetValue changes the current value of this progress bar (from p.Min to p.Max).
// The widget will be refreshed to indicate the change.
func (p *ProgressBar) SetValue(v float64) {
p.Value = v
p.Refresh()
}
// MinSize returns the size that this widget should not shrink below
func (p *ProgressBar) MinSize() fyne.Size {
p.ExtendBaseWidget(p)
return p.BaseWidget.MinSize()
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (p *ProgressBar) CreateRenderer() fyne.WidgetRenderer {
p.ExtendBaseWidget(p)
if p.Min == 0 && p.Max == 0 {
p.Max = 1.0
}
background := canvas.NewRectangle(progressBackgroundColor())
bar := canvas.NewRectangle(theme.PrimaryColor())
label := canvas.NewText("0%", theme.ForegroundColor())
label.Alignment = fyne.TextAlignCenter
return &progressRenderer{widget.NewBaseRenderer([]fyne.CanvasObject{background, bar, label}), background, bar, label, p}
}
// Unbind disconnects any configured data source from this ProgressBar.
// The current value will remain at the last value of the data source.
//
// Since: 2.0
func (p *ProgressBar) Unbind() {
p.binder.Unbind()
}
// NewProgressBar creates a new progress bar widget.
// The default Min is 0 and Max is 1, Values set should be between those numbers.
// The display will convert this to a percentage.
func NewProgressBar() *ProgressBar {
p := &ProgressBar{Min: 0, Max: 1}
cache.Renderer(p).Layout(p.MinSize())
return p
}
// NewProgressBarWithData returns a progress bar connected with the specified data source.
//
// Since: 2.0
func NewProgressBarWithData(data binding.Float) *ProgressBar {
p := NewProgressBar()
p.Bind(data)
return p
}
func progressBackgroundColor() color.Color {
r, g, b, a := col.ToNRGBA(theme.PrimaryColor())
faded := uint8(a) / 2
return &color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: faded}
}
func (p *ProgressBar) updateFromData(data binding.DataItem) {
if data == nil {
return
}
floatSource, ok := data.(binding.Float)
if !ok {
return
}
val, err := floatSource.Get()
if err != nil {
fyne.LogError("Error getting current data value", err)
return
}
p.SetValue(val)
}