-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdots_pb.go
224 lines (187 loc) · 5.71 KB
/
dots_pb.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package xwidget
import (
"math"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type dotsProgressRenderer struct {
BaseRenderer
dots []*canvas.Circle
label *canvas.Text
progress *DotsProgressBar
}
// MinSize calculates the minimum size of a progress bar.
// This is simply the "100%" label size plus padding.
func (r *dotsProgressRenderer) MinSize() fyne.Size {
if r.progress.minSize.Width == 0 && r.progress.minSize.Height == 0 {
var tsize fyne.Size
if text := r.progress.TextFormatter; text != nil {
tsize = fyne.MeasureText(text(), r.label.TextSize, r.label.TextStyle)
} else {
tsize = fyne.MeasureText("100%", r.label.TextSize, r.label.TextStyle)
}
return fyne.NewSize(tsize.Width+theme.InnerPadding()*2, tsize.Height+theme.InnerPadding()*2)
}
return r.progress.minSize
}
func (r *dotsProgressRenderer) updateBar() {
if r.progress.Value < r.progress.Min {
r.progress.Value = r.progress.Min
}
if r.progress.Value > r.progress.Max {
r.progress.Value = r.progress.Max
}
delta := float32(r.progress.Max - r.progress.Min)
ratio := float32(r.progress.Value-r.progress.Min) / delta
if text := r.progress.TextFormatter; text != nil {
r.label.Text = text()
} else {
r.label.Text = strconv.Itoa(int(ratio*100)) + "%"
}
dotNum := int(math.Floor(float64(float32(len(r.dots)) * ratio)))
for i := 0; i < len(r.dots); i++ {
if i < dotNum {
r.dots[i].FillColor = theme.PrimaryColor()
} else {
r.dots[i].FillColor = primaryFadedColor(4)
}
}
}
// Layout the components of the check xwidget
func (r *dotsProgressRenderer) Layout(size fyne.Size) {
r.dotsResize(size)
r.label.TextSize = fyne.Min(size.Width, size.Height) * 0.15
r.label.Resize(size)
r.updateBar()
}
func (r *dotsProgressRenderer) dotsResize(size fyne.Size) {
centerX := float64(size.Width / 2)
centerY := float64(size.Height / 2)
length := fyne.Min(size.Width, size.Height) / 2
dotSize := length * 0.1
radius := float64(length - dotSize)
dot := 0
for angle := 435; angle >= 90; angle -= 15 {
x := float32(centerX + radius*math.Cos(float64(angle)*2*math.Pi/360))
y := float32(centerY - radius*math.Sin(float64(angle)*2*math.Pi/360))
r.dots[dot].Position1 = fyne.Position{X: x - dotSize, Y: y - dotSize}
r.dots[dot].Position2 = fyne.Position{X: x + dotSize, Y: y + dotSize}
dot++
}
}
// Refresh
func (r *dotsProgressRenderer) Refresh() {
size := r.progress.Size()
r.label.Color = theme.PrimaryColor()
r.label.TextSize = fyne.Min(size.Width, size.Height) * 0.15
r.label.Refresh()
r.updateBar()
for _, dot := range r.dots {
dot.Refresh()
}
}
// DotsProgressBar the widget creates a circle of dots indicating progress
type DotsProgressBar struct {
widget.BaseWidget
minSize fyne.Size
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 DotsProgressBar.
// The current value will be displayed and any changes in the data will cause the widget to update.
//
// Since: 2.0
func (p *DotsProgressBar) 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 *DotsProgressBar) SetValue(v float64) {
p.Value = v
p.Refresh()
}
// MinSize returns the size that this widget should not shrink below
func (p *DotsProgressBar) MinSize() fyne.Size {
p.ExtendBaseWidget(p)
return p.BaseWidget.MinSize()
}
func (p *DotsProgressBar) SetMinSize(size fyne.Size) {
p.minSize = size
p.Refresh()
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (p *DotsProgressBar) CreateRenderer() fyne.WidgetRenderer {
p.ExtendBaseWidget(p)
if p.Min == 0 && p.Max == 0 {
p.Max = 1.0
}
size := p.Size()
label := canvas.NewText("0%", theme.PrimaryColor())
label.TextSize = fyne.Min(size.Width, size.Height) * 0.15
label.Alignment = fyne.TextAlignCenter
var dots []*canvas.Circle
var objects []fyne.CanvasObject
for angle := 435; angle >= 90; angle -= 15 {
dot := canvas.NewCircle(primaryFadedColor(4))
dot.StrokeWidth = 1
dot.StrokeColor = primaryFadedColor(2)
dots = append(dots, dot)
objects = append(objects, dot)
}
objects = append(objects, label)
renderer := dotsProgressRenderer{
BaseRenderer: NewBaseRenderer(objects),
dots: dots,
label: label,
progress: p,
}
return &renderer
}
// 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 *DotsProgressBar) Unbind() {
p.binder.Unbind()
}
// NewDotsProgressBar creates a new progress bar xwidget.
// 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 NewDotsProgressBar() *DotsProgressBar {
p := &DotsProgressBar{Min: 0, Max: 1, minSize: fyne.Size{0, 0}}
p.ExtendBaseWidget(p)
return p
}
// NewDotsProgressBarWithData returns a progress bar connected with the specified data source.
//
// Since: 2.0
func NewDotsProgressBarWithData(data binding.Float) *DotsProgressBar {
p := NewDotsProgressBar()
p.Bind(data)
return p
}
func (p *DotsProgressBar) 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)
}