summaryrefslogtreecommitdiff
path: root/limox.go
blob: 9b7c23561aee9d49b586c35347a4f11744b05db2 (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
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("Disconnecting ...")
		l.GuiEvents <- Disconnect
		l.State = Disconnected
	}
}