/
treeview.go
62 lines (59 loc) · 1.71 KB
/
treeview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package gui
import (
"encoding/json"
"io/ioutil"
"os"
. "github.com/lxn/walk/declarative"
"github.com/tinycedar/lily/common"
"github.com/tinycedar/lily/conf"
"github.com/tinycedar/lily/core"
"github.com/tinycedar/lily/model"
)
func newTreeView() TreeView {
treeModel := conf.Config.HostConfigModel
return TreeView{
AssignTo: &(context.treeView),
Model: treeModel,
Font: Font{
Family: "MS Shell Dlg 2",
PointSize: 11,
Bold: false,
},
// click
OnCurrentItemChanged: func() {
context.deleteButton.SetEnabled(true)
current := context.treeView.CurrentItem().(*model.HostConfigItem)
if bytes, err := ioutil.ReadFile("conf/hosts/" + current.Text() + ".hosts"); err == nil {
context.hostConfigText.SetText(string(bytes))
} else {
context.hostConfigText.SetText("")
}
},
StretchFactor: 1,
// double click
OnItemActivated: func() {
current := context.treeView.CurrentItem().(*model.HostConfigItem)
for i := 0; i < treeModel.RootCount(); i++ {
item := treeModel.RootAt(i).(*model.HostConfigItem)
if item == current {
conf.Config.CurrentHostIndex = i
item.Icon = common.IconMap[common.ICON_OPEN]
} else {
item.Icon = common.IconMap[common.ICON_NEW]
}
treeModel.PublishItemChanged(item)
}
configJSON, err := json.Marshal(conf.Config)
if err != nil {
common.Error("Error marshal json: %v", err)
} else {
ioutil.WriteFile("conf/config.json", configJSON, os.ModeExclusive)
}
if core.FireHostsSwitch() {
context.notifyIcon.ShowCustom("切换"+current.Text()+"成功!", "Best hosts manager ever")
} else {
context.notifyIcon.ShowError("切换"+current.Text()+"失败!", "Best hosts manager ever")
}
},
}
}