Skip to content

Commit

Permalink
ui refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
genshen committed Oct 20, 2024
1 parent 9dfe270 commit 682b617
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 97 deletions.
92 changes: 14 additions & 78 deletions client-ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
resource "github.com/genshen/wssocks-plugin-ustb/client-ui/resources"
"github.com/genshen/wssocks-plugin-ustb/extra"
"github.com/genshen/wssocks-plugin-ustb/plugins/vpn"
"github.com/genshen/wssocks-plugin-ustb/plugins/vpn/passwd"
pluginversion "github.com/genshen/wssocks-plugin-ustb/wssocks-ustb/version"
"github.com/genshen/wssocks/client"
"github.com/genshen/wssocks/version"
Expand Down Expand Up @@ -233,102 +232,39 @@ func main() {
// loadUiValue for loading value from the input box.
func loadVpnUI(wssApp *fyne.App) (*fyne.Container, func() vpn.UstbVpn, func()) {
uiVpnEnable := newCheckbox("enable ustb vpn", true, nil)
uiVpnForceLogout := newCheckbox("", true, nil)
uiVpnHostEncrypt := newCheckbox("", true, nil)
uiVpnHostInput := &widget.Entry{PlaceHolder: "vpn hostname", Text: "n.ustb.edu.cn"}
uiVpnUsername := &widget.Entry{PlaceHolder: "vpn username", Text: ""}
uiVpnPassword := &widget.Entry{PlaceHolder: "vpn password", Text: "", Password: true}

// select auth method
uiVpnAuthMethod := widget.NewRadioGroup([]string{"Password", "QR Code"}, func(value string) {
// todo:
})
uiVpnAuthMethod.Horizontal = true
// vpn auth config buttons
uiBtnAuthPasswd := widget.NewButton("Config password auth", func() {
passwdAuthWindow := (*wssApp).NewWindow("password vpn auth")
passwdAuthWindow.SetContent(&widget.Form{Items: []*widget.FormItem{
{Text: "force logout", Widget: uiVpnForceLogout},
{Text: "host encrypt", Widget: uiVpnHostEncrypt},
{Text: "vpn host", Widget: uiVpnHostInput},
{Text: "username", Widget: uiVpnUsername},
{Text: "password", Widget: uiVpnPassword},
}})
passwdAuthWindow.Resize(fyne.NewSize(320, 0))
passwdAuthWindow.Show()
return
})
uiBtnAuthQrCode := widget.NewButton("Config QR Code auth", func() {
qrAuthWindow := (*wssApp).NewWindow("QR Code vpn auth")
qrAuthWindow.SetContent(container.NewVBox(
widget.NewLabel("QR code scanned"),
widget.NewButton("Load/Reload QR Code", func() {}),
))
qrAuthWindow.Show()
return
vpnSettings := VpnSettingsUI{}
vpnSettingBtn := widget.NewButtonWithIcon("VPN Settings", theme.SettingsIcon(), func() {
vpnSettings.OpenVpnSettings(wssApp, (*wssApp).Preferences())
})

loadVPNPreference((*wssApp).Preferences(), uiVpnAuthMethod, uiVpnEnable, uiVpnForceLogout,
uiVpnHostEncrypt, uiVpnHostInput, uiVpnUsername, uiVpnPassword)
loadVPNMainPreference((*wssApp).Preferences(), uiVpnEnable)

uiVpnEnable.OnChanged = func(checked bool) {
if checked {
uiVpnAuthMethod.Enable()
uiVpnForceLogout.Enable()
uiVpnHostEncrypt.Enable()
uiVpnHostInput.Enable()
uiVpnUsername.Enable()
uiVpnPassword.Enable()
uiBtnAuthPasswd.Enable()
uiBtnAuthQrCode.Enable()
vpnSettingBtn.Enable()
} else {
uiVpnAuthMethod.Disable()
uiVpnForceLogout.Disable()
uiVpnHostEncrypt.Disable()
uiVpnHostInput.Disable()
uiVpnUsername.Disable()
uiVpnPassword.Disable()
uiBtnAuthPasswd.Disable()
uiBtnAuthQrCode.Disable()
vpnSettingBtn.Disable()
}
}

// convert from selected string to int value
getAuthMethodInt := func() int {
if uiVpnAuthMethod.Selected == TextVpnAuthMethodPasswd {
return vpn.VpnAuthMethodPasswd
} else {
return vpn.VpnAuthMethodQRCode
}
}
// the vpn UI
// the vpn UI and vpn settings UI
vpnUi := container.NewVBox(
&widget.Form{Items: []*widget.FormItem{
{Text: "vpn enable", Widget: uiVpnEnable},
{Text: "auth method", Widget: uiVpnAuthMethod},
}},
container.NewGridWithColumns(2,
uiBtnAuthPasswd,
uiBtnAuthQrCode,
))
vpnSettingBtn,
)

loadUiValues := func() vpn.UstbVpn {
return vpn.UstbVpn{
Enable: uiVpnEnable.Checked,
ForceLogout: uiVpnForceLogout.Checked,
HostEncrypt: uiVpnHostEncrypt.Checked,
TargetVpn: uiVpnHostInput.Text,
AuthMethod: getAuthMethodInt(),
PasswdAuth: passwd.UstbVpnPasswdAuth{
Username: uiVpnUsername.Text,
Password: uiVpnPassword.Text,
},
vals := vpn.UstbVpn{
Enable: uiVpnEnable.Checked,
QrCodeAuth: newQrCodeAuth(wssApp),
}
vpnSettings.LoadSettingsValues(&vals)
return vals
}
onVpnClose := func() {
saveVPNPreference((*wssApp).Preferences(), uiVpnAuthMethod, uiVpnEnable, uiVpnForceLogout,
uiVpnHostEncrypt, uiVpnHostInput, uiVpnUsername, uiVpnPassword)
saveVPNMainPreference((*wssApp).Preferences(), uiVpnEnable)
}
return vpnUi, loadUiValues, onVpnClose
}
Expand Down
41 changes: 22 additions & 19 deletions client-ui/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ func saveBasicPreference(pref fyne.Preferences, uiLocalAddr, uiRemoteAddr,
pref.SetBool(PrefSkipTSLVerify, uiSkipTSLVerify.Checked)
}

func saveVPNPreference(pref fyne.Preferences,
uiVpnAuthMethod *widget.RadioGroup,
uiVpnEnable, uiVpnForceLogout, uiVpnHostEncrypt *widget.Check,
uiVpnHostInput, uiVpnUsername, uiVpnPassword *widget.Entry) {
func saveVPNMainPreference(pref fyne.Preferences,
uiVpnEnable *widget.Check) {
pref.SetBool(PrefVpnEnable, uiVpnEnable.Checked)
}

func saveVPNPreference(pref fyne.Preferences, uiVpnAuthMethod *widget.RadioGroup, uiVpnForceLogout, uiVpnHostEncrypt *widget.Check,
uiVpnHostInput, uiVpnUsername, uiVpnPassword *widget.Entry) {
if !pref.Bool(PrefHasPreference) {
return
}

pref.SetBool(PrefVpnForceLogout, uiVpnForceLogout.Checked)
pref.SetBool(PrefVpnHostEncrypt, uiVpnHostEncrypt.Checked)
pref.SetString(PrefVpnHostInput, uiVpnHostInput.Text)
pref.SetString(PrefVpnUsername, uiVpnUsername.Text)
//pref.SetString(PrefVpnPassword,uiVpnPassword.Text)
pref.SetString(PrefVpnPassword, uiVpnPassword.Text)
if uiVpnAuthMethod.Selected == TextVpnAuthMethodPasswd {
pref.SetInt(PrefVpnAuthMethod, vpn.VpnAuthMethodPasswd)
} else if uiVpnAuthMethod.Selected == TextVpnAuthMethodQrCode {
Expand Down Expand Up @@ -86,9 +92,7 @@ func loadBasicPreference(pref fyne.Preferences, uiLocalAddr, uiRemoteAddr,
}
}

func loadVPNPreference(pref fyne.Preferences,
uiVpnAuthMethod *widget.RadioGroup, uiVpnEnable, uiVpnForceLogout, uiVpnHostEncrypt *widget.Check,
uiVpnHostInput, uiVpnUsername, uiVpnPassword *widget.Entry) {
func loadVPNMainPreference(pref fyne.Preferences, uiVpnEnable *widget.Check) {
if !pref.Bool(PrefHasPreference) {
return
}
Expand All @@ -97,6 +101,13 @@ func loadVPNPreference(pref fyne.Preferences,
uiVpnEnable.SetChecked(enable) // toggle default value
} // else, default value(true) or preference is true, dont touch it.

}

func loadVpnPreference(pref fyne.Preferences, uiVpnAuthMethod *widget.RadioGroup, uiVpnForceLogout, uiVpnHostEncrypt *widget.Check,
uiVpnHostInput, uiVpnUsername, uiVpnPassword *widget.Entry) {
if !pref.Bool(PrefHasPreference) {
return
}
// vpn force logout
if enable := pref.Bool(PrefVpnForceLogout); !enable {
uiVpnForceLogout.SetChecked(enable)
Expand All @@ -123,17 +134,9 @@ func loadVPNPreference(pref fyne.Preferences,
if username := pref.String(PrefVpnUsername); strings.TrimSpace(username) != "" {
uiVpnUsername.SetText(strings.TrimSpace(username))
}
//if password := pref.String(PrefVpnPassword); password != "" {
// uiVpnPassword.SetText(password)
//}
if password := pref.String(PrefVpnPassword); password != "" {
uiVpnPassword.SetText(password)
}

// if vpn is disabled
if !uiVpnEnable.Checked {
uiVpnAuthMethod.Disable()
uiVpnForceLogout.Disable()
uiVpnHostEncrypt.Disable()
uiVpnHostInput.Disable()
uiVpnUsername.Disable()
uiVpnPassword.Disable()
}
}
83 changes: 83 additions & 0 deletions client-ui/vpn_settings_ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/genshen/wssocks-plugin-ustb/plugins/vpn"
"github.com/genshen/wssocks-plugin-ustb/plugins/vpn/passwd"
)

type VpnSettingsUI struct {
uiVpnForceLogout *widget.Check
uiVpnHostEncrypt *widget.Check
uiVpnHostInput *widget.Entry
uiVpnUsername *widget.Entry
uiVpnPassword *widget.Entry
uiVpnAuthMethod *widget.RadioGroup
}

func (v *VpnSettingsUI) OpenVpnSettings(wssApp *fyne.App, pref fyne.Preferences) {
v.uiVpnForceLogout = newCheckbox("", true, nil)
v.uiVpnHostEncrypt = newCheckbox("", true, nil)
v.uiVpnHostInput = &widget.Entry{PlaceHolder: "vpn hostname", Text: "n.ustb.edu.cn"}
v.uiVpnUsername = &widget.Entry{PlaceHolder: "vpn username", Text: ""}
v.uiVpnPassword = &widget.Entry{PlaceHolder: "vpn password", Text: "", Password: true}

// select auth method
v.uiVpnAuthMethod = widget.NewRadioGroup([]string{TextVpnAuthMethodPasswd, TextVpnAuthMethodQrCode}, func(value string) {
// todo:
})
v.uiVpnAuthMethod.Horizontal = true

// load Preference
loadVpnPreference(pref, v.uiVpnAuthMethod, v.uiVpnForceLogout, v.uiVpnHostEncrypt, v.uiVpnHostInput, v.uiVpnUsername, v.uiVpnPassword)

content := container.NewVBox(
&widget.Form{Items: []*widget.FormItem{
{Text: "force logout", Widget: v.uiVpnForceLogout},
{Text: "host encrypt", Widget: v.uiVpnHostEncrypt},
{Text: "vpn host", Widget: v.uiVpnHostInput},
{Text: "auth method", Widget: v.uiVpnAuthMethod},
}},
&widget.Separator{},
container.NewAppTabs(
container.NewTabItem("Password Auth",
&widget.Form{Items: []*widget.FormItem{
{Text: "username", Widget: v.uiVpnUsername},
{Text: "password", Widget: v.uiVpnPassword},
}}),
container.NewTabItem("QR Code Auth", widget.NewLabel("World!")),
),
container.NewVBox(),
)

authWindow := (*wssApp).NewWindow("VPN Auth Settings")
authWindow.SetContent(content)
authWindow.Resize(fyne.NewSize(400, 0))
authWindow.SetOnClosed(func() {
// saveVpnMainPreference store the values from vpn UI to filesystem
saveVPNPreference(pref, v.uiVpnAuthMethod, v.uiVpnForceLogout, v.uiVpnHostEncrypt, v.uiVpnHostInput, v.uiVpnUsername, v.uiVpnPassword)
})
authWindow.Show()
}

func (v *VpnSettingsUI) LoadSettingsValues(values *vpn.UstbVpn) {
values.ForceLogout = v.uiVpnForceLogout.Checked
values.HostEncrypt = v.uiVpnHostEncrypt.Checked
values.TargetVpn = v.uiVpnHostInput.Text
values.AuthMethod = getAuthMethodInt(v.uiVpnAuthMethod)
values.PasswdAuth = passwd.UstbVpnPasswdAuth{
Username: v.uiVpnUsername.Text,
Password: v.uiVpnPassword.Text,
}
}

// convert from selected string to int value
func getAuthMethodInt(uiVpnAuthMethod *widget.RadioGroup) int {
if uiVpnAuthMethod.Selected == TextVpnAuthMethodPasswd {
return vpn.VpnAuthMethodPasswd
} else if uiVpnAuthMethod.Selected == TextVpnAuthMethodQrCode {
return vpn.VpnAuthMethodQRCode
}
}

0 comments on commit 682b617

Please sign in to comment.