blob: 82bb24e1cd4a72b9db0827ca22457b99eb4368f2 (
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
|
package main
import (
"log"
"gioui.org/app"
"gioui.org/io/event"
"gioui.org/op"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"xengineering.eu/limox/xmpp"
)
type LimoxState uint8
const (
Disconnected LimoxState = iota
Connecting
Connected
)
type Limox struct {
JidEditor widget.Editor
PwdEditor widget.Editor
MainButton widget.Clickable
sessionIn chan<- any
sessionOut chan any
State LimoxState
Window *app.Window
Operations op.Ops
Theme *material.Theme
}
func NewLimox() Limox {
l := Limox{
Window: new(app.Window),
Operations: op.Ops{},
Theme: material.NewTheme(),
sessionOut: make(chan any),
State: Disconnected,
}
l.Window.Option(app.Title("LimoX"))
l.Window.Option(app.Size(unit.Dp(400), unit.Dp(600)))
l.JidEditor.SetText(getLastJid())
l.PwdEditor.SetText(getLastPwd())
return l
}
func (l *Limox) run() error {
c := make(chan event.Event)
go func() {
for {
c <- l.Window.Event()
}
}()
for {
select {
case e := <-c:
switch e := e.(type) {
case app.DestroyEvent:
return e.Err
case app.FrameEvent:
l.draw(e)
}
case data := <-l.sessionOut:
switch data.(type) {
case xmpp.SessionDisconnect:
l.State = Disconnected
case xmpp.SessionConnect:
l.State = Connected
default:
log.Printf("Unknown XMPP data '%d'\n", data)
}
l.Window.Invalidate()
}
}
}
func (l *Limox) buttonCallback() {
c := l.sessionIn
switch l.State {
case Disconnected:
jid := l.JidEditor.Text()
pwd := l.PwdEditor.Text()
setLastJid(jid)
setLastPwd(pwd)
l.sessionIn = xmpp.StartSession(l.sessionOut, jid, pwd)
l.State = Connecting
case Connecting:
go func() { c <- xmpp.SessionShouldDisconnect{} }()
l.State = Disconnected
case Connected:
go func() { c <- xmpp.SessionShouldDisconnect{} }()
l.State = Disconnected
}
}
|