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 XmppConn chan xmpp.Event 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()), XmppConn: make(chan xmpp.Event), 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.XmppConn: switch ev { case xmpp.DisconnectEvent: l.State = Disconnected case xmpp.ConnectEvent: l.State = Connected default: log.Printf("Unknown xmpp.Event '%d'\n", ev) } l.Window.Invalidate() } } } func (l *Limox) buttonCallback() { switch l.State { case Disconnected: log.Println("Starting connection establishment ...") go xmpp.Run(l.XmppConn, 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.XmppConn <- xmpp.ShouldDisconnectEvent l.State = Disconnected } }