-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
245 lines (218 loc) · 6.13 KB
/
main.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"fmt"
"image"
"image/color"
"log"
"math"
"os"
"strconv"
"strings"
"time"
"gioui.org/app"
"gioui.org/f32"
"gioui.org/font/gofont"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
// Define the progress variables, a channel and a variable
var progressIncrementer chan bool
var progress float32
func main() {
// Setup a separate channel to provide ticks to increment progress
progressIncrementer = make(chan bool)
go func() {
for {
time.Sleep(time.Second / 25)
progressIncrementer <- true
}
}()
go func() {
// create new window
w := app.NewWindow(
app.Title("Egg timer"),
app.Size(unit.Dp(400), unit.Dp(600)),
app.MaxSize(unit.Dp(600), unit.Dp(800)),
app.MinSize(unit.Dp(300), unit.Dp(400)),
)
if err := draw(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
type C = layout.Context
type D = layout.Dimensions
func draw(w *app.Window) error {
var boilDurationInput widget.Editor
var boilDuration float32
// ops are the operations from the UI
var ops op.Ops
// startButton is a clickable widget
var startButton widget.Clickable
// is the egg boiling?
var boiling bool
// th defines the material design style
th := material.NewTheme(gofont.Collection())
for {
select {
// listen for events in the window.
case e := <-w.Events():
// detect what type of event
switch e := e.(type) {
// this is sent when the application should re-render.
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
// Let's try out the flexbox layout concept
if startButton.Clicked() {
boiling = !boiling
//reset the boil
if progress >= 1 {
progress = 0
}
//here we read from the input box
inputString := boilDurationInput.Text()
inputString = strings.TrimSpace(inputString)
inputFloat, _ := strconv.ParseFloat(inputString, 32)
boilDuration = float32(inputFloat)
boilDuration = boilDuration / (1 - progress)
}
layout.Flex{
// Vertical alignment, from top to bottom
Axis: layout.Vertical,
// Empty space is left at the start, i.e. at the top
Spacing: layout.SpaceStart,
}.Layout(gtx,
layout.Rigid(
func(gtx C) D {
// Draw a custom path, shaped like an egg
var eggPath clip.Path
op.Offset(image.Pt(gtx.Dp(200), gtx.Dp(150))).Add(gtx.Ops)
eggPath.Begin(gtx.Ops)
// Rotate from 0 to 360 degrees
for deg := 0.0; deg <= 360; deg++ {
// Egg math (really) at this brilliant site. Thanks!
// https://observablehq.com/@toja/egg-curve
// Convert degrees to radians
rad := deg / 360 * 2 * math.Pi
// Trig gives the distance in X and Y direction
cosT := math.Cos(rad)
sinT := math.Sin(rad)
// Constants to define the eggshape
a := 110.0
b := 150.0
d := 20.0
// The x/y coordinates
x := a * cosT
y := -(math.Sqrt(b*b-d*d*cosT*cosT) + d*sinT) * sinT
// Finally the point on the outline
p := f32.Pt(float32(x), float32(y))
// Draw the line to this point
eggPath.LineTo(p)
}
// Close the path
eggPath.Close()
// Get hold of the actual clip
eggArea := clip.Outline{Path: eggPath.End()}.Op()
// Fill the shape
// color := color.NRGBA{R: 255, G: 239, B: 174, A: 255}
color := color.NRGBA{R: 255, G: uint8(239 * (1 - progress)), B: uint8(174 * (1 - progress)), A: 255}
paint.FillShape(gtx.Ops, color, eggArea)
d := image.Point{Y: 375}
return layout.Dimensions{Size: d}
},
),
layout.Rigid(
func(gtx C) D {
ed := material.Editor(th, &boilDurationInput, "sec")
boilDurationInput.SingleLine = true
boilDurationInput.Alignment = text.Middle
if boiling && progress < 1 {
boilRemain := (1 - progress) * boilDuration
//formatting to make things look nice, using the
// multiply by 10 devide by 10 trick
inputStr := fmt.Sprintf("%.1f", math.Round(math.Abs(float64(boilRemain)*10))/10)
//update the text inside of the input box
boilDurationInput.SetText(inputStr)
}
//layouts for the text box
margins := layout.Inset{
Top: unit.Dp(0),
Right: unit.Dp(170),
Bottom: unit.Dp(40),
Left: unit.Dp(170),
}
border := widget.Border{
Color: color.NRGBA{R: 204, G: 204, B: 204, A: 255},
CornerRadius: unit.Dp(3),
Width: unit.Dp(2),
}
return margins.Layout(gtx,
func(gtx C) D {
return border.Layout(gtx, ed.Layout)
})
},
),
//Progress Bar happens here
layout.Rigid(
func(gtx C) D {
bar := material.ProgressBar(th, progress)
return bar.Layout(gtx)
},
),
//this is the bounding box for the start button
layout.Rigid(
func(gtx C) D {
// We start by defining a set of margins
margins := layout.Inset{
Top: unit.Dp(25),
Bottom: unit.Dp(25),
Right: unit.Dp(35),
Left: unit.Dp(35),
}
// Then we lay out within those margins ...
return margins.Layout(gtx,
// ...the same function we earlier used to create a button
func(gtx C) D {
var text string
if !boiling {
text = "Start"
}
if boiling && progress < 1 {
text = "Stop"
}
if boiling && progress >= 1 {
text = "Egg Boiled"
}
btn := material.Button(th, &startButton, text)
return btn.Layout(gtx)
},
)
},
),
)
e.Frame(gtx.Ops)
// this is sent when the application is closed.
case system.DestroyEvent:
return e.Err
}
// listen for events in the incrementor channel
case <-progressIncrementer:
if boiling && progress < 1 {
progress += float32(1.0 / 25.0 / math.Abs(float64(boilDuration)))
if progress >= 1 {
progress = 1
}
w.Invalidate()
}
}
}
}