Skip to content

Commit

Permalink
Fix config listview context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
koho committed Dec 13, 2020
1 parent 99634fb commit eac358a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
18 changes: 15 additions & 3 deletions ui/confpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type ConfPage struct {
confContainer *walk.Composite
detailContainer *walk.Composite
fillerContainer *walk.Composite
confDB *walk.DataBinder
}

func NewConfPage() *ConfPage {
v := new(ConfPage)
v.ConfView = NewConfView(&v.confContainer)
v.ConfView = NewConfView(&v.confContainer, &v.confDB)
v.ConfView.ConfigChanged = v.onConfigChanged
v.DetailView = NewDetailView()
return v
Expand All @@ -34,6 +35,14 @@ func (t *ConfPage) View() TabPage {
StretchFactor: 1,
AssignTo: &t.confContainer,
Layout: VBox{MarginsZero: true, SpacingZero: true},
DataBinder: DataBinder{AssignTo: &t.confDB, DataSource: &struct {
ConfSize func() int
SelectedIndex func() int
}{func() int {
return len(config.Configurations)
}, func() int {
return t.ConfListView.view.CurrentIndex()
}}, Name: "conf"},
Children: []Widget{
t.ConfListView.View(),
t.ToolbarView.View(),
Expand Down Expand Up @@ -75,7 +84,7 @@ func (t *ConfPage) View() TabPage {

func (t *ConfPage) Initialize() {
t.ConfView.Initialize()
t.ConfView.ConfListView.view.CurrentIndexChanged().Attach(t.UpdateDetailView)
t.ConfView.ConfListView.view.CurrentIndexChanged().Attach(t.UpdateView)
t.DetailView.Initialize()
t.onConfigChanged(len(config.Configurations))
t.ConfView.ConfListView.view.SetCurrentIndex(0)
Expand Down Expand Up @@ -105,12 +114,15 @@ func (t *ConfPage) startQueryStatus() {
}()
}

func (t *ConfPage) UpdateDetailView() {
func (t *ConfPage) UpdateView() {
conf := t.ConfView.ConfListView.CurrentConf()
t.DetailView.SetConf(conf)
if conf != nil {
lastEditName = conf.Name
}
if *(t.db) != nil {
(*t.db).Reset()
}
}

func (t *ConfPage) SwapFiller(filler bool) {
Expand Down
25 changes: 13 additions & 12 deletions ui/confview.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ type ConfView struct {
*ConfListView
*ToolbarView
ConfigChanged func(int)
db **walk.DataBinder
}

func NewConfView(parent **walk.Composite) *ConfView {
func NewConfView(parent **walk.Composite, db **walk.DataBinder) *ConfView {
v := new(ConfView)
v.db = db
v.ConfListView = NewConfListView()
v.ToolbarView = NewToolbarView(parent)
return v
Expand All @@ -44,8 +46,8 @@ func (t *ConfView) reloadConf() {
if idx, found := utils.Find(config.GetConfigNames(), lastEditName); found {
t.ConfListView.view.SetCurrentIndex(idx)
}
if t.toolbarDB != nil {
t.toolbarDB.Reset()
if *(t.db) != nil {
(*t.db).Reset()
}
}

Expand Down Expand Up @@ -193,10 +195,12 @@ func (t *ConfView) Initialize() {
t.ConfListView.editAction.Triggered().Attach(func() {
t.onEditConf(t.ConfListView.CurrentConf())
})
t.ConfListView.editAction.SetDefault(true)
t.ConfListView.newAction.Triggered().Attach(func() {
t.onEditConf(nil)
})
t.ConfListView.importAction.Triggered().Attach(t.onImport)
t.ConfListView.exportAction.Triggered().Attach(t.onExport)
t.ConfListView.deleteAction.Triggered().Attach(t.onDelete)
}

Expand All @@ -206,6 +210,7 @@ type ConfListView struct {
editAction *walk.Action
newAction *walk.Action
importAction *walk.Action
exportAction *walk.Action
deleteAction *walk.Action
}

Expand All @@ -225,10 +230,13 @@ func (t *ConfListView) View() Widget {
Columns: []TableViewColumn{{DataMember: "Name"}},
Model: t.model,
ContextMenuItems: []MenuItem{
Action{AssignTo: &t.editAction, Text: "编辑配置"},
Action{AssignTo: &t.editAction, Text: "编辑配置", Enabled: Bind("conf.SelectedIndex >= 0")},
Separator{},
Action{AssignTo: &t.newAction, Text: "创建新配置"},
Action{AssignTo: &t.importAction, Text: "从文件导入配置"},
Action{AssignTo: &t.deleteAction, Text: "删除配置"},
Action{AssignTo: &t.exportAction, Text: "导出所有配置 (ZIP 压缩包)", Enabled: Bind("conf.ConfSize > 0")},
Separator{},
Action{AssignTo: &t.deleteAction, Text: "删除配置", Enabled: Bind("conf.SelectedIndex >= 0")},
},
StyleCell: func(style *walk.CellStyle) {
row := style.Row()
Expand Down Expand Up @@ -285,8 +293,6 @@ type ToolbarView struct {
addAction *walk.Action
deleteAction *walk.Action
exportAction *walk.Action

toolbarDB *walk.DataBinder
}

func NewToolbarView(parent **walk.Composite) *ToolbarView {
Expand All @@ -297,11 +303,6 @@ func NewToolbarView(parent **walk.Composite) *ToolbarView {

func (t *ToolbarView) View() Widget {
return Composite{
DataBinder: DataBinder{AssignTo: &t.toolbarDB, DataSource: &struct {
ConfSize func() int
}{func() int {
return len(config.Configurations)
}}, Name: "conf"},
Layout: HBox{MarginsZero: true, SpacingZero: true},
Children: []Widget{
ToolBar{
Expand Down

0 comments on commit eac358a

Please sign in to comment.