-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (63 loc) · 1.43 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
package main
import (
"strconv"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
"github.com/Knetic/govaluate"
)
func main() {
a := app.New()
loadTheme(a)
w := a.NewWindow("calculator")
g := newGUI()
w.SetContent(g.makeUI())
g.setupActions()
w.ShowAndRun()
}
var clearNext = false
// here you can add some button / callbacks code using widget IDs
func (g *gui) setupActions() {
g.out.SetText("")
g.bc.OnTapped = func() {
g.out.SetText("")
}
append := func(s string) {
text := g.out.Text
if clearNext {
text = ""
clearNext = false
}
g.out.SetText(text + s)
}
digits := []*widget.Button{g.b0, g.b1, g.b2, g.b3, g.b4, g.b5, g.b6, g.b7, g.b8, g.b9}
for _, b := range digits {
digit := b
digit.OnTapped = func() { append(digit.Text) }
}
g.ba.OnTapped = func() { append("+") }
g.bs.OnTapped = func() { append("-") }
g.bm.OnTapped = func() { append("*") }
g.bd.OnTapped = func() { append("/") }
g.bbo.OnTapped = func() { append("(") }
g.bbc.OnTapped = func() { append(")") }
g.bp.OnTapped = func() { append(".") }
}
func (g *gui) evaluate() {
clearNext = true
expression, err := govaluate.NewEvaluableExpression(g.out.Text)
if err != nil {
g.out.SetText("error")
return
}
result, err := expression.Evaluate(nil)
if err != nil {
g.out.SetText("error")
return
}
value, ok := result.(float64)
if !ok {
g.out.SetText("error")
return
}
g.out.SetText(strconv.FormatFloat(value, 'f', -1, 64))
}