-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.go
158 lines (136 loc) · 2.92 KB
/
stack.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
// SPDX-License-Identifier: Unlicense OR MIT
/*
these methods were implemented by Elias Naur <mail@eliasnaur.com>
for the scatter.im project https://git.sr.ht/~eliasnaur/scatter
*/
package main
import (
"image"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"image/color"
)
type pageStack struct {
pages []Page
stopChan chan<- struct{}
}
type Page interface {
Start(stop <-chan struct{})
Event(gtx layout.Context) interface{}
Layout(gtx layout.Context) layout.Dimensions
}
type Background struct {
Color color.NRGBA
Radius unit.Dp
Inset layout.Inset
}
type clipCircle struct {
}
func (cc *clipCircle) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
macro := op.Record(gtx.Ops)
dims := w(gtx)
call := macro.Stop()
max := dims.Size.X
if dy := dims.Size.Y; dy > max {
max = dy
}
rr := max >> 1
t := clip.RRect{
Rect: image.Rectangle{Max: image.Point{X: max, Y: max}},
NE: rr, NW: rr, SE: rr, SW: rr,
}.Push(gtx.Ops)
call.Add(gtx.Ops)
t.Pop()
return dims
}
func (b *Background) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
macro := op.Record(gtx.Ops)
dims := b.Inset.Layout(gtx, w)
call := macro.Stop()
size := dims.Size
if r := gtx.Dp(b.Radius); r > 0 {
if r > size.X/2 {
r = size.X >> 1
}
if r > size.Y/2 {
r = size.Y >> 1
}
t := clip.RRect{
Rect: image.Rectangle{Max: image.Point{
X: size.X, Y: size.Y,
}}, NW: r, NE: r, SW: r, SE: r,
}.Push(gtx.Ops)
defer t.Pop()
}
paint.FillShape(gtx.Ops, b.Color, clip.Rect(image.Rectangle{Max: size}).Op())
call.Add(gtx.Ops)
return dims
}
type RedrawEvent struct{}
type BackEvent struct{}
type fill struct {
color color.NRGBA
}
type icon struct {
src []byte
size unit.Dp
// Cached values.
op paint.ImageOp
imgSize int
}
func rgb(c uint32) color.NRGBA {
return argb((0xff << 24) | c)
}
func argb(c uint32) color.NRGBA {
return color.NRGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
}
func (f fill) Layout(gtx layout.Context) layout.Dimensions {
cs := gtx.Constraints
d := cs.Min
paint.FillShape(gtx.Ops, f.color, clip.Rect(image.Rectangle{Max: d}).Op())
return layout.Dimensions{Size: d, Baseline: d.Y}
}
func (s *pageStack) Len() int {
return len(s.pages)
}
func (s *pageStack) Current() Page {
return s.pages[len(s.pages)-1]
}
func (s *pageStack) Pop() {
if len(s.pages) > 0 {
if s.stopChan != nil {
s.stop()
}
i := len(s.pages) - 1
s.pages[i] = nil
s.pages = s.pages[:i]
}
if len(s.pages) > 0 {
s.start() // start new top of stack
}
}
func (s *pageStack) start() {
stop := make(chan struct{})
s.stopChan = stop
s.Current().Start(stop)
}
func (s *pageStack) Push(p Page) {
if s.stopChan != nil {
s.stop()
}
s.pages = append(s.pages, p)
s.start()
}
func (s *pageStack) stop() {
close(s.stopChan)
s.stopChan = nil
}
func (s *pageStack) Clear(p Page) {
for len(s.pages) > 0 {
s.Pop()
}
s.Push(p)
}