feat: 记住上次选中的配置,下次启动自动选中
- 启用 AppConfig.DefaultProfileID 字段(此前声明但未使用) - 启动时 loadProfiles() 优先选中上次记录的配置,找不到则回退到第一个 - 切换配置时立即持久化到 config.yml,防止崩溃丢失 - 使用 Profile ID 而非名称,避免重命名后失效
This commit is contained in:
@@ -11,7 +11,7 @@ GO = go
|
||||
CGO_ENABLED = 1
|
||||
WINDRES ?= x86_64-w64-mingw32-windres
|
||||
MINGW_CC ?= x86_64-w64-mingw32-gcc
|
||||
SEMVER ?= 0.4.2
|
||||
SEMVER ?= 0.4.3
|
||||
GIT_HASH = $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
|
||||
VERSION = $(SEMVER)-$(GIT_HASH)
|
||||
LDFLAGS = -s -w -X lmvpn/internal/version.Version=$(VERSION)
|
||||
|
||||
+42
-7
@@ -48,11 +48,12 @@ type App struct {
|
||||
disconnectBtn *widget.Button
|
||||
|
||||
// State
|
||||
mu sync.Mutex
|
||||
ipcClient *ipc.Client
|
||||
profiles []model.ServerProfile
|
||||
currentProfile *model.ServerProfile
|
||||
langSetting string
|
||||
mu sync.Mutex
|
||||
ipcClient *ipc.Client
|
||||
profiles []model.ServerProfile
|
||||
currentProfile *model.ServerProfile
|
||||
defaultProfileID int64
|
||||
langSetting string
|
||||
}
|
||||
|
||||
// Run initialises and starts the GUI application.
|
||||
@@ -84,6 +85,7 @@ func Run() {
|
||||
db: store,
|
||||
kc: keychain.New(),
|
||||
langSetting: cfg.Language,
|
||||
defaultProfileID: cfg.DefaultProfileID,
|
||||
listSelectedIndex: -1,
|
||||
}
|
||||
|
||||
@@ -144,8 +146,21 @@ func (a *App) loadProfiles() {
|
||||
names := a.profileNames()
|
||||
a.profileSelect.Options = names
|
||||
if len(names) > 0 {
|
||||
a.profileSelect.SetSelectedIndex(0)
|
||||
a.selectProfileByName(names[0])
|
||||
selected := false
|
||||
if a.defaultProfileID > 0 {
|
||||
for i := range a.profiles {
|
||||
if a.profiles[i].ID == a.defaultProfileID {
|
||||
a.profileSelect.SetSelectedIndex(i)
|
||||
a.selectProfileByName(a.profiles[i].Name)
|
||||
selected = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !selected {
|
||||
a.profileSelect.SetSelectedIndex(0)
|
||||
a.selectProfileByName(names[0])
|
||||
}
|
||||
} else {
|
||||
a.currentProfile = nil
|
||||
a.profileSelect.SetSelected("")
|
||||
@@ -182,6 +197,26 @@ func (a *App) selectProfileByName(name string) {
|
||||
}
|
||||
}
|
||||
|
||||
// saveDefaultProfile persists the currently selected profile ID to the
|
||||
// config file so it can be restored on the next launch.
|
||||
func (a *App) saveDefaultProfile() {
|
||||
if a.currentProfile == nil {
|
||||
return
|
||||
}
|
||||
a.defaultProfileID = a.currentProfile.ID
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
cfg = config.Default()
|
||||
}
|
||||
if cfg.DefaultProfileID == a.currentProfile.ID {
|
||||
return
|
||||
}
|
||||
cfg.DefaultProfileID = a.currentProfile.ID
|
||||
if err := config.Save(cfg); err != nil {
|
||||
log.L().Error("save default profile", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// onAddProfile shows a dialog to create a new profile.
|
||||
func (a *App) onAddProfile() {
|
||||
a.showProfileDialog(nil)
|
||||
|
||||
@@ -54,6 +54,7 @@ func (a *App) buildMainWindow() fyne.CanvasObject {
|
||||
// Profile selector.
|
||||
a.profileSelect = widget.NewSelect(a.profileNames(), func(sel string) {
|
||||
a.selectProfileByName(sel)
|
||||
a.saveDefaultProfile()
|
||||
})
|
||||
|
||||
// Status display.
|
||||
|
||||
Reference in New Issue
Block a user