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

Support custom sort order in config list #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions pkg/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type App struct {
Lang string `json:"lang,omitempty"`
Password string `json:"password,omitempty"`
Defaults DefaultValue `json:"defaults"`
Sort []string `json:"sort,omitempty"`
}

type DefaultValue struct {
Expand Down
17 changes: 17 additions & 0 deletions ui/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"os"
"path/filepath"
"slices"
"strings"
"sync"

"github.com/lxn/walk"
"github.com/samber/lo"

"github.com/koho/frpmgr/pkg/config"
"github.com/koho/frpmgr/pkg/consts"
Expand Down Expand Up @@ -74,6 +76,7 @@ func (conf *Conf) Delete() (bool, error) {
func (conf *Conf) Save() error {
conf.Data.Complete(false)
conf.Path = PathOfConf(conf.Name + ".conf")
defer saveAppConfig()
return conf.Data.Save(conf.Path)
}

Expand Down Expand Up @@ -111,6 +114,16 @@ func loadAllConfs() error {
confList = append(confList, c)
}
}
slices.SortStableFunc(confList, func(a, b *Conf) int {
i := slices.Index(appConf.Sort, strings.TrimSuffix(filepath.Base(a.Path), filepath.Ext(a.Path)))
j := slices.Index(appConf.Sort, strings.TrimSuffix(filepath.Base(b.Path), filepath.Ext(b.Path)))
if i < 0 && j >= 0 {
return 1
} else if j < 0 && i >= 0 {
return -1
}
return i - j
})
return nil
}

Expand All @@ -133,6 +146,7 @@ func addConf(conf *Conf) {
func deleteConf(conf *Conf) bool {
confMutex.Lock()
defer confMutex.Unlock()
defer saveAppConfig()
for i := range confList {
if confList[i] == conf {
confList = append(confList[:i], confList[i+1:]...)
Expand Down Expand Up @@ -194,5 +208,8 @@ func newDefaultClientConfig() *config.ClientConfig {
}

func saveAppConfig() error {
appConf.Sort = lo.Map(confList, func(item *Conf, index int) string {
return strings.TrimSuffix(filepath.Base(item.Path), filepath.Ext(item.Path))
})
return appConf.Save(config.DefaultAppFile)
}
51 changes: 51 additions & 0 deletions ui/confview.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ func NewConfView() *ConfView {
}

func (cv *ConfView) View() Widget {
moveUpCond := Bind("confView.CurrentIndex > 0")
moveDownCond := Bind("confView.CurrentIndex >= 0 && confView.CurrentIndex < confView.ItemCount - 1")
return Composite{
AssignTo: &cv.Composite,
Layout: VBox{MarginsZero: true, SpacingZero: true},
Children: []Widget{
TableView{
Name: "confView",
AssignTo: &cv.listView,
LastColumnStretched: true,
HeaderHidden: true,
Expand All @@ -63,6 +66,40 @@ func (cv *ConfView) View() Widget {
Enabled: Bind("conf.Selected"),
OnTriggered: cv.editCurrent,
},
Menu{
Text: i18n.Sprintf("Move"),
Enabled: Bind("confView.CurrentIndex >= 0 && confView.ItemCount > 1"),
Items: []MenuItem{
Action{
Text: i18n.Sprintf("Up"),
Enabled: moveUpCond,
OnTriggered: func() {
cv.onMove(-1)
},
},
Action{
Text: i18n.Sprintf("Down"),
Enabled: moveDownCond,
OnTriggered: func() {
cv.onMove(1)
},
},
Action{
Text: i18n.Sprintf("To Top"),
Enabled: moveUpCond,
OnTriggered: func() {
cv.onMove(-cv.listView.CurrentIndex())
},
},
Action{
Text: i18n.Sprintf("To Bottom"),
Enabled: moveDownCond,
OnTriggered: func() {
cv.onMove(len(cv.model.items) - cv.listView.CurrentIndex() - 1)
},
},
},
},
Action{Text: i18n.Sprintf("Open File"),
Enabled: Bind("conf.Selected"),
OnTriggered: func() { cv.onOpen(false) },
Expand Down Expand Up @@ -557,6 +594,20 @@ func (cv *ConfView) onNATDiscovery() {
}
}

func (cv *ConfView) onMove(delta int) {
curIdx := cv.listView.CurrentIndex()
if curIdx < 0 || curIdx >= len(cv.model.items) {
return
}
targetIdx := curIdx + delta
if targetIdx < 0 || targetIdx >= len(cv.model.items) {
return
}
cv.model.Move(curIdx, targetIdx)
saveAppConfig()
cv.listView.SetCurrentIndex(targetIdx)
}

// reset config listview with selected name
func (cv *ConfView) reset(selectName string) {
// Make sure `sel` is a valid index
Expand Down
7 changes: 6 additions & 1 deletion ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type ConfListModel struct {
walk.TableModelBase
walk.ReflectTableModelBase

items []*Conf
}
Expand All @@ -35,6 +35,11 @@ func (m *ConfListModel) Items() interface{} {
return m.items
}

func (m *ConfListModel) Move(i, j int) {
util.MoveSlice(m.items, i, j)
m.PublishRowsChanged(min(i, j), max(i, j))
}

type ListModel struct {
walk.ListModelBase

Expand Down