diff options
| author | xengineering <me@xengineering.eu> | 2023-04-15 22:06:48 +0200 | 
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2023-04-15 22:06:48 +0200 | 
| commit | cc04cf414760732b8c812aab5d3dfa51b8c0816a (patch) | |
| tree | ee3a97d6dbcc5139643f50fd6b417214883ab64c /go | |
| parent | 4830acbae283d5f88b8e65aa187c054203fe4f20 (diff) | |
| download | limox-cc04cf414760732b8c812aab5d3dfa51b8c0816a.tar limox-cc04cf414760732b8c812aab5d3dfa51b8c0816a.tar.zst limox-cc04cf414760732b8c812aab5d3dfa51b8c0816a.zip | |
Implement basic connection statemachine
This prepares a real TCP / XMPP connection.
Diffstat (limited to 'go')
| -rw-r--r-- | go/main.go | 76 | 
1 files changed, 51 insertions, 25 deletions
| @@ -16,14 +16,22 @@ import (  	"gioui.org/widget/material"  ) +type LimoxState uint8 + +const ( +	Disconnected LimoxState = iota +	Connecting +	Connected +) +  type Limox struct { -	JidEditor        widget.Editor -	PwdEditor        widget.Editor -	ConnectClickable widget.Clickable -	Connecting       bool -	Window           *app.Window -	Operations       op.Ops -	Theme            *material.Theme +	JidEditor       widget.Editor +	PwdEditor       widget.Editor +	MainButton      widget.Clickable +	State           LimoxState +	Window          *app.Window +	Operations      op.Ops +	Theme           *material.Theme  }  func main() { @@ -46,9 +54,9 @@ func NewLimox() Limox {  			app.Title("LimoX"),  			app.Size(unit.Dp(400), unit.Dp(600)),  		), -		Operations: op.Ops{}, -		Theme:      material.NewTheme(gofont.Collection()), -		Connecting: false, +		Operations:      op.Ops{}, +		Theme:           material.NewTheme(gofont.Collection()), +		State:           Disconnected,  	}  } @@ -59,20 +67,28 @@ func (l *Limox) run() error {  		case system.DestroyEvent:  			return e.Err  		case system.FrameEvent: -			if l.ConnectClickable.Clicked() { -				l.Connecting = !l.Connecting -				if l.Connecting { -					log.Printf("User wants to connect with '%s' and '%s'.\n", -						l.JidEditor.Text(), -						l.PwdEditor.Text(), -					) -				} +			if l.MainButton.Clicked() { +				l.buttonCallback()  			}  			l.draw(e)  		}  	}  } +func (l *Limox) buttonCallback() { +	switch l.State { +	case Disconnected: +		log.Println("Starting connection establishment") +		l.State = Connecting +	case Connecting: +		log.Println("Aborted connection establishment") +		l.State = Disconnected +	case Connected: +		log.Println("Disconnected") +		l.State = Disconnected +	} +} +  func (l *Limox) draw(e system.FrameEvent) {  	gtx := layout.NewContext(&l.Operations, e) @@ -107,13 +123,8 @@ func (l *Limox) draw(e system.FrameEvent) {  		),  		layout.Rigid(  			func(gtx layout.Context) layout.Dimensions { -				var label string -				if l.Connecting { -					label = "abort" -				} else { -					label = "connect" -				} -				btn := material.Button(l.Theme, &l.ConnectClickable, label) +				btn := material.Button(l.Theme, &l.MainButton, +					l.buttonLabel())  				return btn.Layout(gtx)  			},  		), @@ -121,3 +132,18 @@ func (l *Limox) draw(e system.FrameEvent) {  	e.Frame(gtx.Ops)  } + +func (l *Limox) buttonLabel() string { +	var label string +	switch l.State { +		case Disconnected: +			label = "Connect" +		case Connecting: +			label = "Abort" +		case Connected: +			label = "Disconnect" +		default: +			log.Fatalf("Unknown Limox state '%d'\n", l.State) +	} +	return label +} | 
