summaryrefslogtreecommitdiff
path: root/limox.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-04-16 22:02:47 +0200
committerxengineering <me@xengineering.eu>2023-04-18 21:01:14 +0200
commit4d4d3f00ddee089de5b29bfe322022173c239aff (patch)
tree8c9c4862eb435c7c3d9683b6d49e821dd1caa195 /limox.go
parentbcbeb1b8e07049098459b7512d59dbdea7752289 (diff)
downloadlimox-4d4d3f00ddee089de5b29bfe322022173c239aff.tar
limox-4d4d3f00ddee089de5b29bfe322022173c239aff.tar.zst
limox-4d4d3f00ddee089de5b29bfe322022173c239aff.zip
Split Go code into multiple files
Diffstat (limited to 'limox.go')
-rw-r--r--limox.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/limox.go b/limox.go
new file mode 100644
index 0000000..3ca21b3
--- /dev/null
+++ b/limox.go
@@ -0,0 +1,99 @@
+package main
+
+import (
+ "log"
+ "reflect"
+
+ "gioui.org/app"
+ "gioui.org/font/gofont"
+ "gioui.org/io/system"
+ "gioui.org/op"
+ "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
+ XmppEvents chan any
+ GuiEvents chan GuiEvent
+ State LimoxState
+ Window *app.Window
+ Operations op.Ops
+ Theme *material.Theme
+}
+
+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()),
+ XmppEvents: make(chan any),
+ GuiEvents: make(chan GuiEvent),
+ State: Disconnected,
+ }
+}
+
+func (l *Limox) run() error {
+ for {
+ select {
+ case 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)
+ }
+ case ev := <-l.XmppEvents:
+ switch ev.(type) {
+ case error:
+ log.Print(ev)
+ l.State = Disconnected
+ case XmppEvent:
+ switch ev {
+ case XmppDisconnect:
+ l.State = Disconnected
+ case XmppConnect:
+ l.State = Connected
+ default:
+ log.Fatalf("Unknown XmppEvent '%d'\n", ev)
+ }
+ default:
+ log.Fatalf("Unknown event type '%s'.\n", reflect.TypeOf(ev))
+ }
+ l.Window.Invalidate()
+ }
+ }
+}
+
+func (l *Limox) buttonCallback() {
+ switch l.State {
+ case Disconnected:
+ log.Println("Starting connection establishment")
+ go l.xmpp(l.JidEditor.Text(), l.PwdEditor.Text())
+ l.State = Connecting
+ case Connecting:
+ log.Println("Aborted connection establishment")
+ l.State = Disconnected
+ case Connected:
+ log.Println("Disconnected")
+ l.GuiEvents <- Disconnect
+ l.State = Disconnected
+ }
+}