package main import ( "log" "gioui.org/app" "gioui.org/font/gofont" "gioui.org/io/system" "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: app.NewWindow( app.Title("LimoX"), app.Size(unit.Dp(400), unit.Dp(600)), ), Operations: op.Ops{}, Theme: material.NewTheme(gofont.Collection()), sessionOut: make(chan any), State: Disconnected, } l.JidEditor.SetText(getLastJid()) l.PwdEditor.SetText(getLastPwd()) return l } 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 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 } }