-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplash.go
87 lines (80 loc) · 2.03 KB
/
splash.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
package splash
import (
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"image"
"image/color"
)
type Splash struct {
img image.Image
progressHeight unit.Dp
progressInset layout.Inset
progressCol color.NRGBA
progress float64
}
func NewSplash(img image.Image, progressInset layout.Inset, progressCol color.NRGBA) *Splash {
return &Splash{
img,
progressInset.Bottom - progressInset.Top,
progressInset,
progressCol,
0,
}
}
func (s *Splash) SetProgress(progress float64) {
s.progress = progress
if s.progress > 1 {
s.progress = 1
}
if s.progress < 0 {
s.progress = 0
}
}
func (s *Splash) Layout(gtx layout.Context) layout.Dimensions {
// Lay out the image and overlay the progress bar on top of it, aligning
// to the south so that the progress bar is at the bottom.
return layout.Stack{
Alignment: layout.S,
}.Layout(gtx,
layout.Stacked(s.drawLogo),
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
// Lay out the progress bar within the inset.
return s.progressInset.Layout(gtx, s.drawProgress)
}),
)
}
func (s *Splash) drawLogo(gtx layout.Context) layout.Dimensions {
return widget.Image{
Src: paint.NewImageOp(s.img),
Fit: widget.Contain,
}.Layout(gtx)
}
func (s *Splash) drawProgress(gtx layout.Context) layout.Dimensions {
// Our gtx.Constraints.Min.X provide the width
// that a full progress bar must occupy.
spread := gtx.Constraints.Min.X
rectangle := image.Rectangle{
Max: image.Pt(
// Since spread is already a unit of screen pixels,
// no need to pass through gtx.Dp to convert to them.
int(float64(spread)*s.progress),
// Convert our progress bar height to screen pixels.
gtx.Dp(s.progressHeight),
),
}
paint.FillShape(
gtx.Ops,
s.progressCol,
clip.Rect(rectangle).Op())
// Return the logical dimensions occupied by the progress bar,
// which is the width of a full bar.
return layout.Dimensions{
Size: image.Point{
X: gtx.Constraints.Min.X,
Y: gtx.Dp(s.progressHeight),
},
}
}