-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprogress.go
226 lines (189 loc) · 4.69 KB
/
progress.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
225
226
package x11ui
import (
"fmt"
"image"
"image/color"
"log"
"github.com/llgcode/draw2d/draw2dkit"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/xgraphics"
"github.com/llgcode/draw2d/draw2dimg"
"github.com/lucasb-eyer/go-colorful"
)
type ProgressBar struct {
p *Window //ParentWindow
me *Window
// Custom properties
dispScaler float64
val float64
bgColor color.Color
barColor color.Color
txtColor color.Color
fmtString string
margin float64
border float64
ShowGrid bool
NGrids float64
}
func NewProgressBar(title string, p *Window, dims ...int) *ProgressBar {
if p == nil {
log.Fatal("Cannot Create Widget without Application")
}
pbar := new(ProgressBar)
pbar.me = newWindow(p.Window.X, p, title, dims...)
pbar.SetDisplayScale(100.0)
pbar.ResetFmtString()
pbar.loadTheme()
// pbar.SetValue(0.5)
return pbar
}
func (p *ProgressBar) X() *xgbutil.XUtil {
return p.me.X()
}
func (p *ProgressBar) loadTheme() {
p.bgColor = systemBG
p.barColor = colorful.LinearRgb(.4, .6, .1)
p.txtColor = color.RGBA{20, 200, 30, 200}
}
func (p *ProgressBar) SetBackGroundColor(bc color.Color) {
p.bgColor = bc
}
func (p *ProgressBar) SetBarColor(bc color.Color) {
p.barColor = bc
}
func (p *ProgressBar) SetTextColor(tc color.Color) {
p.txtColor = tc
}
func (p *ProgressBar) SetFmtString(s string) {
p.fmtString = s
}
func (p *ProgressBar) FmtString() string {
return p.fmtString
}
func (p *ProgressBar) ResetFmtString() {
p.fmtString = "%3.0f%%"
}
// return the value in the scale 0 to 1
func (p ProgressBar) Value() float64 {
return p.val
}
func (p *ProgressBar) SetDisplayScale(s float64) {
p.dispScaler = s
}
// Sets the value in the fraction 0 to 1
func (p *ProgressBar) SetValue(v float64) {
p.val = v
p.reDrawBar()
}
func (p *ProgressBar) Parent() *Window {
return p.p
}
func (p *ProgressBar) Widget() *Window {
return p.me
}
func (p *ProgressBar) reDrawBar() {
/// ignoring widgetState
p.drawBackground(StateNormal)
}
func (p *ProgressBar) SetBorderWidth(bw float64) {
p.border = bw
}
func (p *ProgressBar) Margin() float64 {
return p.margin
}
func (p *ProgressBar) SetMargin(m float64) {
p.margin = m
}
func (p *ProgressBar) drawBackground(s WidgetState) {
r := p.me.Rect
r.MoveTo(0, 0)
r.ImageRect()
dest := image.NewRGBA(r.ImageRect())
gc := draw2dimg.NewGraphicContext(dest)
// bg := colorful.LinearRgb(.025, .025, .025)
switch s {
case StateNormal, StateReleased:
gc.SetFillColor(color.RGBA{0x20, 0x20, 0x20, 20})
gc.SetStrokeColor(systemFG)
case StateHovered:
gc.SetFillColor(color.RGBA{0x35, 0x20, 0x20, 20})
gc.SetStrokeColor(systemFG)
case StatePressed:
gc.SetFillColor(color.RGBA{0x20, 0x30, 0x20, 20})
gc.SetStrokeColor(systemFG)
case StateSpecial:
gc.SetFillColor(color.RGBA{0x20, 0x80, 0x20, 0x80})
gc.SetStrokeColor(systemFG)
}
// // gc.SetLineJoin(draw2d.RoundJoin)
// // gc.Rotate(math.Pi / 4.0)
WW := float64(r.Width)
HH := float64(r.Height)
// Draw Background
gc.SetLineWidth(p.border)
gc.SetFillColor(p.bgColor)
draw2dkit.Rectangle(gc, 0, 0, WW, HH)
gc.FillStroke()
if p.val > 0 {
// Draw the BAR
NSegments := 1.0
ww := float64(r.Width) * p.val
margin := p.margin
ww = ww - margin
hh := HH - margin
gc.SetLineWidth(0)
sw := (ww - (NSegments-1)*margin) / NSegments
gc.SetFillColor(p.barColor)
// for i := 0.0; i < NSegments; i++ {
draw2dkit.Rectangle(gc, margin, margin, sw, hh)
gc.FillStroke()
// gc.Stroke()
/// Draw Grid Lines
if p.ShowGrid && p.NGrids > 1 {
NGrids := p.NGrids
GridWidth := (WW - 2*margin) / NGrids
lc, ok := p.barColor.(color.RGBA)
if ok {
lc.A = 10
gc.SetStrokeColor(lc)
}
xpos := margin
gc.SetLineWidth(1)
for i := 0.0; i < NGrids; i++ {
if xpos < ww {
gc.MoveTo(xpos, margin)
gc.LineTo(xpos, hh)
xpos += GridWidth
gc.Close()
gc.Stroke()
}
}
}
// }
}
// gc.SetFillColor(color.RGBA{130, 120, 30, 110})
// gc.SetStrokeColor(color.RGBA{30, 120, 130, 110})
// draw2dkit.Circle(gc, 250, 10, 30)
// gc.FillStroke()
// c := color.RGBA{200, 200, 200, 255}
// gc.SetStrokeColor(image.White)
// c := color.RGBA{200, 200, 200, 255}
gc.SetFillColor(p.txtColor)
gc.SetLineWidth(0)
cx, cy := r.Center()
str := fmt.Sprintf(p.fmtString, (p.val * p.dispScaler))
x0, y0, w0, h0 := gc.GetStringBounds(str)
// log.Println("Required dimension for string ", x0, y0, w0, h0, cx, cy)
tx, ty := float64(cx)-w0/2.0-x0, float64(cy)-h0/2.0-y0/2.0
// gc.StrokeStringAt(str, tx, ty)
gc.FillStringAt(str, tx, ty)
// gc.FillStroke()
gc.Close()
g := xgraphics.NewConvert(p.me.X(), dest)
// w.drawLabel(g, w.title)
g.XSurfaceSet(p.me.Id)
g.XDraw()
g.XPaintRects(p.me.Id, r.ImageRect())
// return g
// return g
}