Skip to content

Commit

Permalink
Enforce non-default username
Browse files Browse the repository at this point in the history
The user is enforced to set a non-default username (default: lanty).
This is done by the following methods:
1) The application starts with the SettingsBrowser, when the default
   username is still set at application start.
2) The user can not set and save the default username in the
   SettingsBrowser view.
  • Loading branch information
seternate committed Aug 22, 2024
1 parent 9e03d49 commit 61f2f9b
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 26 deletions.
1 change: 0 additions & 1 deletion .github/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ changelog:
categories:
- title: Breaking Changes 🛠
labels:
- feature
- breaking-change
- title: New Features 🎉
labels:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/rs/zerolog v1.31.0
github.com/seternate/go-lanty v0.1.0-beta.0.20240818103544-c247788a1ae4
github.com/seternate/go-lanty v0.2.0
golang.design/x/clipboard v0.7.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seternate/go-lanty v0.1.0-beta.0.20240818103544-c247788a1ae4 h1:vZbd/gyKrHFOBXz4htFCNR/AxD2c8avrPFazuYtMOdk=
github.com/seternate/go-lanty v0.1.0-beta.0.20240818103544-c247788a1ae4/go.mod h1:QL66bV5xXhUysHVUI8VaX773dsvXJp0bDzrXWWq3IOs=
github.com/seternate/go-lanty v0.2.0 h1:Oj/ubPDZOvJerYcsCZXkiHQpXyzAZkcvpS+keiYrTTU=
github.com/seternate/go-lanty v0.2.0/go.mod h1:QL66bV5xXhUysHVUI8VaX773dsvXJp0bDzrXWWq3IOs=
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
3 changes: 2 additions & 1 deletion pkg/setting/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
const (
APPLICATION_NAME = "Lanty"
SETTINGS_PATH = "settings.yaml"
VERSION = "v0.1.0-beta"
VERSION = "v0.2.0"
DEFAULT_USERNAME = "lanty"
)

type Settings struct {
Expand Down
76 changes: 76 additions & 0 deletions pkg/widget/defaultusernamebrowser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package widget

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
fynetheme "fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/seternate/go-lanty-client/pkg/controller"
)

type DefaultUsernameBrowser struct {
widget.BaseWidget

controller *controller.Controller

settingsbrowser *SettingsBrowser
}

func NewDefaultUsernameBrowser(controller *controller.Controller, window fyne.Window) (browser *DefaultUsernameBrowser) {
browser = &DefaultUsernameBrowser{
controller: controller,
settingsbrowser: NewSettingsBrowser(controller, window),
}
browser.ExtendBaseWidget(browser)

return
}

func (widget *DefaultUsernameBrowser) SetOnSubmit(onSubmit func()) {
widget.settingsbrowser.SetOnSubmit(onSubmit)
}

func (widget *DefaultUsernameBrowser) CreateRenderer() fyne.WidgetRenderer {
return newDefaulusernamebrowserRenderer(widget)
}

type defaultusernamebrowserRenderer struct {
widget *DefaultUsernameBrowser
background *canvas.Rectangle
}

func newDefaulusernamebrowserRenderer(widget *DefaultUsernameBrowser) *defaultusernamebrowserRenderer {
renderer := &defaultusernamebrowserRenderer{
widget: widget,
background: canvas.NewRectangle(fynetheme.BackgroundColor()),
}

return renderer
}

func (renderer *defaultusernamebrowserRenderer) Objects() []fyne.CanvasObject {
objects := []fyne.CanvasObject{
renderer.background,
renderer.widget.settingsbrowser,
}

return objects
}

func (renderer *defaultusernamebrowserRenderer) Layout(size fyne.Size) {
renderer.background.Move(fyne.NewPos(0, 0))
renderer.background.Resize(size)
renderer.widget.settingsbrowser.Move(fyne.NewPos(0, 0))
renderer.widget.settingsbrowser.Resize(size)
}

func (renderer *defaultusernamebrowserRenderer) MinSize() fyne.Size {
return renderer.widget.settingsbrowser.MinSize()
}

func (renderer *defaultusernamebrowserRenderer) Refresh() {
renderer.background.Refresh()
renderer.widget.settingsbrowser.Refresh()
}

func (renderer *defaultusernamebrowserRenderer) Destroy() {}
5 changes: 5 additions & 0 deletions pkg/widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (widget *Form) HideSubmit() {
widget.Refresh()
}

func (widget *Form) ShowSubmit() {
widget.submit.Show()
widget.Refresh()
}

func (widget *Form) SetCancelText(text string) {
widget.cancel.SetText(text)
widget.Refresh()
Expand Down
78 changes: 57 additions & 21 deletions pkg/widget/lanty.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (
type Lanty struct {
widget.BaseWidget

controller *controller.Controller
sidebar *Sidebar
gamebrowser *ScrollWithState
startserver *ScrollWithState
joinserver *JoinServer
downloadbrowser *ScrollWithState
userbrowser *ScrollWithState
settingsbrowser *ScrollWithState
statusbar *StatusBar
chatbrowser *fyne.Container
controller *controller.Controller
sidebar *Sidebar
gamebrowser *ScrollWithState
startserver *ScrollWithState
joinserver *JoinServer
downloadbrowser *ScrollWithState
userbrowser *ScrollWithState
settingsbrowser *ScrollWithState
statusbar *StatusBar
chatbrowser *fyne.Container
defaultusernamebrowser *ScrollWithState

resetSettingsBrowser func()

Expand All @@ -41,18 +42,20 @@ func NewLanty(controller *controller.Controller, window fyne.Window) *Lanty {
userbrowser := NewUserBrowser(controller)
settingsbrowser := NewSettingsBrowser(controller, window)
chatbrowser := NewChatBrowser(controller, window)
defaultusernamebrowser := NewDefaultUsernameBrowser(controller, window)

lanty := &Lanty{
controller: controller,
sidebar: NewSidebar(setting.APPLICATION_NAME),
gamebrowser: NewVScrollWithState(gamebrowser),
startserver: NewVScrollWithState(startserver),
joinserver: NewJoinServer(controller),
downloadbrowser: NewVScrollWithState(downloadbrowser),
userbrowser: NewVScrollWithState(userbrowser),
settingsbrowser: NewVScrollWithState(settingsbrowser),
statusbar: NewStatusBar(controller),
chatbrowser: container.NewStack(chatbrowser),
controller: controller,
sidebar: NewSidebar(setting.APPLICATION_NAME),
gamebrowser: NewVScrollWithState(gamebrowser),
startserver: NewVScrollWithState(startserver),
joinserver: NewJoinServer(controller),
downloadbrowser: NewVScrollWithState(downloadbrowser),
userbrowser: NewVScrollWithState(userbrowser),
settingsbrowser: NewVScrollWithState(settingsbrowser),
statusbar: NewStatusBar(controller),
chatbrowser: container.NewStack(chatbrowser),
defaultusernamebrowser: NewVScrollWithState(defaultusernamebrowser),
resetSettingsBrowser: func() {
settingsbrowser.ResetData()
},
Expand Down Expand Up @@ -110,7 +113,17 @@ func NewLanty(controller *controller.Controller, window fyne.Window) *Lanty {
controller.Status.Info("IP copied", 3*time.Second)
})

lanty.showGameBrowser()
defaultusernamebrowser.SetOnSubmit(func() {
defaultusernamebrowser.Hide()
lanty.showGameBrowser()
})
if controller.Settings.Settings().Username == setting.DEFAULT_USERNAME {
defaultusernamebrowser.Show()
lanty.hideAll()
} else {
defaultusernamebrowser.Hide()
lanty.showGameBrowser()
}

controller.Status.Subscribe(lanty.statusupdate)
controller.WaitGroup().Add(1)
Expand All @@ -119,7 +132,20 @@ func NewLanty(controller *controller.Controller, window fyne.Window) *Lanty {
return lanty
}

func (widget *Lanty) hideAll() {
widget.sidebar.Hide()
widget.gamebrowser.Hide()
widget.downloadbrowser.Hide()
widget.chatbrowser.Hide()
widget.userbrowser.Hide()
widget.settingsbrowser.Hide()
widget.startserver.Hide()
widget.joinserver.Hide()
widget.Refresh()
}

func (widget *Lanty) showGameBrowser() {
widget.sidebar.Show()
widget.gamebrowser.Show()
widget.downloadbrowser.Hide()
widget.chatbrowser.Hide()
Expand All @@ -131,6 +157,7 @@ func (widget *Lanty) showGameBrowser() {
}

func (widget *Lanty) showDownloadBrowser() {
widget.sidebar.Show()
widget.gamebrowser.Hide()
widget.downloadbrowser.Show()
widget.chatbrowser.Hide()
Expand All @@ -142,6 +169,7 @@ func (widget *Lanty) showDownloadBrowser() {
}

func (widget *Lanty) showChatBrowser() {
widget.sidebar.Show()
widget.gamebrowser.Hide()
widget.downloadbrowser.Hide()
widget.chatbrowser.Show()
Expand All @@ -153,6 +181,7 @@ func (widget *Lanty) showChatBrowser() {
}

func (widget *Lanty) showUserBrowser() {
widget.sidebar.Show()
widget.gamebrowser.Hide()
widget.downloadbrowser.Hide()
widget.chatbrowser.Hide()
Expand All @@ -164,6 +193,7 @@ func (widget *Lanty) showUserBrowser() {
}

func (widget *Lanty) showSettingsBrowser() {
widget.sidebar.Show()
widget.gamebrowser.Hide()
widget.downloadbrowser.Hide()
widget.chatbrowser.Hide()
Expand All @@ -176,6 +206,7 @@ func (widget *Lanty) showSettingsBrowser() {
}

func (widget *Lanty) showStartServer() {
widget.sidebar.Show()
widget.gamebrowser.Hide()
widget.downloadbrowser.Hide()
widget.chatbrowser.Hide()
Expand All @@ -187,6 +218,7 @@ func (widget *Lanty) showStartServer() {
}

func (widget *Lanty) showJoinServer() {
widget.sidebar.Show()
widget.gamebrowser.Hide()
widget.downloadbrowser.Hide()
widget.chatbrowser.Hide()
Expand Down Expand Up @@ -241,10 +273,13 @@ func (renderer *lantyRenderer) Objects() []fyne.CanvasObject {
renderer.widget.sidebar,
renderer.main,
renderer.widget.statusbar,
renderer.widget.defaultusernamebrowser,
}
}

func (renderer *lantyRenderer) Layout(size fyne.Size) {
renderer.widget.defaultusernamebrowser.Move(fyne.NewPos(0, 0))
renderer.widget.defaultusernamebrowser.Resize(size)
renderer.widget.sidebar.Move(fyne.NewPos(0, 0))
renderer.widget.sidebar.Resize(fyne.NewSize(fyne.Max(size.Width/7, renderer.widget.sidebar.MinSize().Width), size.Height))
renderer.main.Resize(fyne.NewSize(size.Width-fyne.Max(size.Width/7, renderer.widget.sidebar.MinSize().Width), size.Height))
Expand All @@ -268,6 +303,7 @@ func (renderer *lantyRenderer) Refresh() {
renderer.main.Refresh()
renderer.widget.sidebar.Refresh()
renderer.widget.statusbar.Refresh()
renderer.widget.defaultusernamebrowser.Refresh()
}

func (renderer *lantyRenderer) Destroy() {}
30 changes: 30 additions & 0 deletions pkg/widget/settingbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fyne.io/fyne/v2/widget"
"github.com/rs/zerolog/log"
"github.com/seternate/go-lanty-client/pkg/controller"
"github.com/seternate/go-lanty-client/pkg/setting"
)

type SettingsBrowser struct {
Expand All @@ -27,6 +28,8 @@ type SettingsBrowser struct {
username *Entry
downloaddirectory *Entry

OnSubmit func()

settingschanged chan struct{}
}

Expand Down Expand Up @@ -69,6 +72,9 @@ func NewSettingsBrowser(controller *controller.Controller, window fyne.Window) (

settingsbrowser.username.SetText(controller.Settings.Settings().Username)
settingsbrowser.username.Validator = func(username string) error {
if username == setting.DEFAULT_USERNAME {
return errors.New("default username not allowed")
}
match, err := regexp.MatchString("^(?:[a-zA-Z]|[0-9]|-)+$", username)
if !match || err != nil {
return errors.New("only alphanumeric characters and \"-\" allowed")
Expand Down Expand Up @@ -101,6 +107,20 @@ func NewSettingsBrowser(controller *controller.Controller, window fyne.Window) (
settingsbrowser.form.AppendItem(NewFormItem("Download Directory", downloaddirectory))

settingsbrowser.form.HideSubmit()
settingsbrowser.form.OnSubmit = func() {
if settingsbrowser.OnSubmit != nil {
serverurl := settingsbrowser.serverurl.Text
gamedir := settingsbrowser.gamedirectory.Text
username := settingsbrowser.username.Text
downloaddir := settingsbrowser.downloaddirectory.Text
controller.Settings.SetServerURL(serverurl)
controller.Settings.SetGameDirectory(gamedir)
controller.Settings.SetUsername(username)
controller.Settings.SetDownloadDirectory(downloaddir)
settingsbrowser.OnSubmit()
}
}

settingsbrowser.form.SetCancelText("Reset")
settingsbrowser.form.OnCancel = func() {
settingsbrowser.ResetData()
Expand All @@ -113,6 +133,16 @@ func NewSettingsBrowser(controller *controller.Controller, window fyne.Window) (
return settingsbrowser
}

func (widget *SettingsBrowser) SetOnSubmit(onSubmit func()) {
if onSubmit != nil {
widget.form.ShowSubmit()
} else {
widget.form.HideSubmit()
}
widget.OnSubmit = onSubmit
widget.Refresh()
}

func (widget *SettingsBrowser) run() {
widget.controller.WaitGroup().Add(1)
go widget.settingsUpdater()
Expand Down

0 comments on commit 61f2f9b

Please sign in to comment.