summaryrefslogtreecommitdiff
path: root/go/main.go
blob: f7e7ee9cf71e1c8adf0c26d6f8859c2d76ed9226 (plain)
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
package main

import (
	"image/color"
	"log"
	"os"

	"gioui.org/app"
	"gioui.org/font/gofont"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
)

type LimoxState uint8

const (
	Disconnected LimoxState = iota
	Connecting
	Connected
)

type Limox struct {
	JidEditor       widget.Editor
	PwdEditor       widget.Editor
	MainButton      widget.Clickable
	State           LimoxState
	Window          *app.Window
	Operations      op.Ops
	Theme           *material.Theme
}

func main() {
	limox := NewLimox()

	go func() {
		err := limox.run()
		if err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()

	app.Main()
}

func NewLimox() Limox {
	return Limox{
		Window: app.NewWindow(
			app.Title("LimoX"),
			app.Size(unit.Dp(400), unit.Dp(600)),
		),
		Operations:      op.Ops{},
		Theme:           material.NewTheme(gofont.Collection()),
		State:           Disconnected,
	}
}

func (l *Limox) run() error {
	for {
		e := <-l.Window.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			if l.MainButton.Clicked() {
				l.buttonCallback()
			}
			l.draw(e)
		}
	}
}

func (l *Limox) buttonCallback() {
	switch l.State {
	case Disconnected:
		log.Println("Starting connection establishment")
		l.State = Connecting
	case Connecting:
		log.Println("Aborted connection establishment")
		l.State = Disconnected
	case Connected:
		log.Println("Disconnected")
		l.State = Disconnected
	}
}

func (l *Limox) draw(e system.FrameEvent) {
	gtx := layout.NewContext(&l.Operations, e)

	flex := layout.Flex{
		Axis:    layout.Vertical,
		Spacing: layout.SpaceBetween,
	}

	flex.Layout(gtx,
		layout.Rigid(
			func(gtx layout.Context) layout.Dimensions {
				h2 := material.H2(l.Theme, "LimoX")
				h2.Color = color.NRGBA{R: 20, G: 20, B: 20, A: 255}
				h2.Alignment = text.Middle
				return h2.Layout(gtx)
			},
		),
		layout.Rigid(
			func(gtx layout.Context) layout.Dimensions {
				jid := material.Editor(l.Theme, &l.JidEditor, "user@example.com")
				jid.Editor.Alignment = text.Middle
				return jid.Layout(gtx)
			},
		),
		layout.Rigid(
			func(gtx layout.Context) layout.Dimensions {
				pwd := material.Editor(l.Theme, &l.PwdEditor, "mySafePassword")
				pwd.Editor.Alignment = text.Middle
				pwd.Editor.Mask = '*'
				return pwd.Layout(gtx)
			},
		),
		layout.Rigid(
			func(gtx layout.Context) layout.Dimensions {
				btn := material.Button(l.Theme, &l.MainButton,
					l.buttonLabel())
				return btn.Layout(gtx)
			},
		),
	)

	e.Frame(gtx.Ops)
}

func (l *Limox) buttonLabel() string {
	var label string
	switch l.State {
		case Disconnected:
			label = "Connect"
		case Connecting:
			label = "Abort"
		case Connected:
			label = "Disconnect"
		default:
			log.Fatalf("Unknown Limox state '%d'\n", l.State)
	}
	return label
}