Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More intuitive settings save #36

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/controller/settingscontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"slices"
"sync"
"time"

"github.com/seternate/go-lanty-client/pkg/setting"
"github.com/seternate/go-lanty/pkg/util"
Expand Down Expand Up @@ -52,6 +53,14 @@ func (controller *SettingsController) Settings() setting.Settings {
return *controller.settings
}

func (controller *SettingsController) Save() (err error) {
err = controller.Settings().Save()
if err != nil {
controller.parent.Status.Error("Error saving settings: "+err.Error(), 3*time.Second)
}
return
}

func (controller *SettingsController) Subscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
Expand Down
29 changes: 29 additions & 0 deletions pkg/widget/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package widget

import "fyne.io/fyne/v2/widget"

type Entry struct {
widget.Entry

OnFocusChanged func(bool)
}

func NewEntry() *Entry {
entry := &Entry{}
entry.ExtendBaseWidget(entry)
return entry
}

func (widget *Entry) FocusGained() {
widget.Entry.FocusGained()
if widget.OnFocusChanged != nil {
widget.OnFocusChanged(true)
}
}

func (widget *Entry) FocusLost() {
widget.Entry.FocusLost()
if widget.OnFocusChanged != nil {
widget.OnFocusChanged(false)
}
}
21 changes: 16 additions & 5 deletions pkg/widget/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (widget *Form) SetSubmitText(text string) {
widget.Refresh()
}

func (widget *Form) HideSubmit() {
widget.submit.Hide()
widget.Refresh()
}

func (widget *Form) SetCancelText(text string) {
widget.cancel.SetText(text)
widget.Refresh()
Expand Down Expand Up @@ -91,11 +96,17 @@ func (renderer *formRenderer) Layout(size fyne.Size) {
item.Resize(fyne.NewSize(size.Width-2*theme.InnerPadding(), item.MinSize().Height))
lastposition = item.Position().Y + item.Size().Height
}
buttonSize := fyne.NewSize(fyne.Max(renderer.widget.cancel.MinSize().Width, renderer.widget.submit.MinSize().Width), renderer.widget.submit.MinSize().Height)
renderer.widget.submit.Resize(buttonSize)
renderer.widget.submit.Move(fyne.NewPos(size.Width-theme.InnerPadding()-buttonSize.Width, size.Height-theme.InnerPadding()-buttonSize.Height))
renderer.widget.cancel.Resize(buttonSize)
renderer.widget.cancel.Move(fyne.NewPos(renderer.widget.submit.Position().X-theme.InnerPadding()-buttonSize.Width, renderer.widget.submit.Position().Y))
if renderer.widget.submit.Hidden {
buttonSize := fyne.NewSize(renderer.widget.cancel.MinSize().Width, renderer.widget.cancel.MinSize().Height)
renderer.widget.cancel.Resize(buttonSize)
renderer.widget.cancel.Move(fyne.NewPos(size.Width-theme.InnerPadding()-buttonSize.Width, size.Height-theme.InnerPadding()-buttonSize.Height))
} else {
buttonSize := fyne.NewSize(fyne.Max(renderer.widget.cancel.MinSize().Width, renderer.widget.submit.MinSize().Width), renderer.widget.submit.MinSize().Height)
renderer.widget.submit.Resize(buttonSize)
renderer.widget.submit.Move(fyne.NewPos(size.Width-theme.InnerPadding()-buttonSize.Width, size.Height-theme.InnerPadding()-buttonSize.Height))
renderer.widget.cancel.Resize(buttonSize)
renderer.widget.cancel.Move(fyne.NewPos(renderer.widget.submit.Position().X-theme.InnerPadding()-buttonSize.Width, renderer.widget.submit.Position().Y))
}
}

func (renderer *formRenderer) MinSize() fyne.Size {
Expand Down
80 changes: 60 additions & 20 deletions pkg/widget/settingbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package widget

import (
"errors"
"os"
"regexp"
"time"

Expand All @@ -22,9 +23,9 @@ type SettingsBrowser struct {
window fyne.Window
form *Form

serverurl *widget.Entry
gamedirectory *widget.Entry
username *widget.Entry
serverurl *Entry
gamedirectory *Entry
username *Entry

settingschanged chan struct{}
}
Expand All @@ -34,44 +35,79 @@ func NewSettingsBrowser(controller *controller.Controller, window fyne.Window) (
controller: controller,
window: window,
form: NewForm(),
serverurl: widget.NewEntry(),
gamedirectory: widget.NewEntry(),
username: widget.NewEntry(),
serverurl: NewEntry(),
gamedirectory: NewEntry(),
username: NewEntry(),
settingschanged: make(chan struct{}, 50),
}
settingsbrowser.ExtendBaseWidget(settingsbrowser)

settingsbrowser.serverurl.SetText(controller.Settings.Settings().ServerURL)
settingsbrowser.serverurl.OnFocusChanged = func(b bool) {
if !b && settingsbrowser.serverurl.Validate() == nil {
controller.Settings.SetServerURL(settingsbrowser.serverurl.Text)
controller.Settings.Save()
}
}
settingsbrowser.serverurl.OnSubmitted = func(s string) {
if settingsbrowser.serverurl.Validate() == nil {
controller.Settings.SetServerURL(settingsbrowser.serverurl.Text)
controller.Settings.Save()
}
}
settingsbrowser.form.AppendItem(NewFormItem("Server URL", settingsbrowser.serverurl))

settingsbrowser.gamedirectory.SetText(controller.Settings.Settings().GameDirectory)
settingsbrowser.gamedirectory.Validator = func(path string) error {
info, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return errors.New("folder does not exist")
}
if !info.IsDir() {
return errors.New("path is not a folder")
}
return nil
}
settingsbrowser.gamedirectory.OnFocusChanged = func(b bool) {
if !b && settingsbrowser.gamedirectory.Validate() == nil {
controller.Settings.SetGameDirectory(settingsbrowser.gamedirectory.Text)
controller.Settings.Save()
}
}
settingsbrowser.gamedirectory.OnSubmitted = func(s string) {
if settingsbrowser.gamedirectory.Validate() == nil {
controller.Settings.SetGameDirectory(settingsbrowser.gamedirectory.Text)
controller.Settings.Save()
}
}
gamedirectoryexplorer := widget.NewButtonWithIcon("", theme.FolderOpenIcon(), settingsbrowser.gamedirectoryExplorerCallback)
gamedirectory := container.NewBorder(nil, nil, nil, gamedirectoryexplorer, settingsbrowser.gamedirectory)
settingsbrowser.form.AppendItem(NewFormItem("Game Directory", gamedirectory))

settingsbrowser.username.SetText(controller.Settings.Settings().Username)
settingsbrowser.username.Validator = func(s string) error {
match, err := regexp.MatchString("^(?:[a-zA-Z]|[0-9]|-)+$", s)
settingsbrowser.username.Validator = func(username string) error {
match, err := regexp.MatchString("^(?:[a-zA-Z]|[0-9]|-)+$", username)
if !match || err != nil {
return errors.New("only alphanumeric characters and \"-\" allowed")
}
return nil
}
settingsbrowser.username.OnFocusChanged = func(b bool) {
if !b && settingsbrowser.username.Validate() == nil {
controller.Settings.SetUsername(settingsbrowser.username.Text)
controller.Settings.Save()
}
}
settingsbrowser.username.OnSubmitted = func(s string) {
if settingsbrowser.username.Validate() == nil {
controller.Settings.SetUsername(settingsbrowser.username.Text)
controller.Settings.Save()
}
}
settingsbrowser.form.AppendItem(NewFormItem("Username", settingsbrowser.username))

settingsbrowser.form.SetSubmitText("Save")
settingsbrowser.form.HideSubmit()
settingsbrowser.form.SetCancelText("Reset")
settingsbrowser.form.OnSubmit = func() {
controller.Settings.SetServerURL(settingsbrowser.serverurl.Text)
controller.Settings.SetGameDirectory(settingsbrowser.gamedirectory.Text)
controller.Settings.SetUsername(settingsbrowser.username.Text)
err := controller.Settings.Settings().Save()
if err != nil {
controller.Status.Error("Error saving settings: "+err.Error(), 3*time.Second)
} else {
controller.Status.Info("Settings successfully saved", 3*time.Second)
}
}
settingsbrowser.form.OnCancel = func() {
settingsbrowser.ResetData()
controller.Status.Info("Settings resetted", 3*time.Second)
Expand Down Expand Up @@ -110,6 +146,10 @@ func (widget *SettingsBrowser) gamedirectoryExplorerCallback() {
return
}
widget.gamedirectory.SetText(uri.Path())
if widget.gamedirectory.Validate() == nil {
widget.controller.Settings.SetGameDirectory(widget.gamedirectory.Text)
widget.controller.Settings.Save()
}
}, widget.window)

dialogStartURI, err := storage.ListerForURI(storage.NewFileURI(widget.controller.Settings.Settings().GameDirectory))
Expand Down
Loading