-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
248 lines (227 loc) · 6.21 KB
/
main.go
File metadata and controls
248 lines (227 loc) · 6.21 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @Time : 2025/4/30 10:05
* @File : main.go
* @Software: go-hosts
* @Author : Mr.Fang
* @Description: 程序入口
*/
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/systemmin/go-hosts/internal"
"github.com/systemmin/go-hosts/internal/storage"
"github.com/systemmin/go-hosts/internal/wind"
"github.com/systemmin/go-hosts/models"
"github.com/systemmin/go-hosts/pkg/data"
"log"
"os"
"path/filepath"
"strings"
)
const title = "Go Hosts"
var listData = storage.ListConfig()
func main() {
// 创建数据目录
data.CreateDataDir()
// 初始化日志输出
f, err := os.OpenFile(filepath.Join(data.GetHome(), "go-hosts.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err == nil {
log.SetOutput(f)
defer f.Close()
}
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lshortfile)
//utils.CheckSingleInstance()
// 创建应用
a := app.NewWithID("hosts")
a.Settings().SetTheme(&wind.ForcedVariant{Theme: theme.DefaultTheme(), Variant: theme.VariantDark})
// 创建窗口
w := a.NewWindow(title)
// 主界面
w.SetMaster()
// 添加托盘菜单
wind.AddMenu(w)
// 当前选中节点
currentId := ""
// 控制台日志
logs := binding.NewString()
// 提前定义变量,闭包可引用
var tree *widget.Tree
tree = &widget.Tree{
ChildUIDs: func(uid string) []string {
return IndexData(uid, listData)
},
IsBranch: func(uid string) bool {
indexData := IndexData(uid, listData)
return len(indexData) > 0
},
CreateNode: createNode,
UpdateNode: func(uid string, branch bool, obj fyne.CanvasObject) {
if branch {
handleMasterUpdate(uid, currentId, &listData, a, obj, tree)
} else {
handleBranchUpdate(uid, logs, &listData, obj, tree)
}
},
OnSelected: func(uid string) {
currentId = uid
content := genCurrentDomainContent(uid)
err := logs.Set(content)
if err != nil {
return
}
tree.Refresh()
},
}
// 右侧内容容器
rightContent := wind.FunctionButton(&listData, logs, ¤tId, w)
right := widget.NewMultiLineEntry()
right.Wrapping = fyne.TextWrapWord
right.Bind(logs)
right.SetText("控制台")
// 主题
themes := container.NewGridWithColumns(2,
widget.NewButton("深色", func() {
a.Settings().SetTheme(&wind.ForcedVariant{Theme: theme.DefaultTheme(), Variant: theme.VariantDark})
}),
widget.NewButton("浅色", func() {
a.Settings().SetTheme(&wind.ForcedVariant{Theme: theme.DefaultTheme(), Variant: theme.VariantLight})
}),
)
layoutLeft := container.NewBorder(nil, themes, nil, nil, tree)
layoutRight := container.NewBorder(rightContent, nil, nil, nil, container.NewVScroll(right))
// 布局
content := container.NewHSplit(layoutLeft, layoutRight)
content.Offset = 0.3
w.Resize(fyne.NewSize(800, 600))
w.SetContent(content)
w.CenterOnScreen()
// 拦截关闭按钮,隐藏窗口
w.SetCloseIntercept(func() {
w.Hide()
})
w.ShowAndRun()
}
func IndexData(id string, listData []models.Domain) []string {
var list []string
for _, datum := range listData {
if len(id) == 0 {
list = append(list, datum.Id)
} else if id == datum.Id {
for _, ip := range datum.Mappings {
list = append(list, ip.Id)
}
break
}
}
return list
}
func findIP(id string, listData []models.Domain) (pid, iid int, ipData models.Mapping) {
for i, datum := range listData {
ips := datum.Mappings
for j, ip := range ips {
if ip.Id == id {
return i, j, ip
}
}
}
return -1, -1, models.Mapping{}
}
// 创建节点
func createNode(branch bool) fyne.CanvasObject {
if branch { // 分支一级
delButton := &widget.Button{Icon: theme.CancelIcon()}
delButton.Hide()
editButton := &widget.Button{Icon: theme.DocumentCreateIcon()}
editButton.Hide()
return container.NewHBox(widget.NewLabel("域名"), layout.NewSpacer(), delButton, editButton)
}
// 二级
label := widget.NewLabel("")
check := widget.NewCheck("", nil)
return container.New(layout.NewHBoxLayout(), label, layout.NewSpacer(), check)
}
// 更新一级节点
func handleMasterUpdate(uid, currentId string, listData *[]models.Domain, a fyne.App, obj fyne.CanvasObject, tree *widget.Tree) {
box := obj.(*fyne.Container)
label := box.Objects[0].(*widget.Label)
index := -1 // 当前选中下标
for i, datum := range *listData {
if datum.Id == uid {
label.SetText("🌐 " + datum.Name)
index = i
break
}
}
delButton := box.Objects[2].(*widget.Button)
editButton := box.Objects[3].(*widget.Button)
if uid == currentId {
delButton.Show()
editButton.Show()
} else {
delButton.Hide()
editButton.Hide()
}
delButton.OnTapped = func() {
*listData = storage.DelConfig(uid)
tree.Refresh()
}
editButton.OnTapped = func() {
wind.NewChildWindow(listData, index)
}
}
func handleBranchUpdate(uid string, logs binding.String, listData *[]models.Domain, obj fyne.CanvasObject, tree *widget.Tree) {
box := obj.(*fyne.Container)
check := box.Objects[2].(*widget.Check)
label := box.Objects[0].(*widget.Label)
for _, datum := range *listData {
for _, ip := range datum.Mappings {
if ip.Id == uid {
label.SetText("📶 " + ip.Value)
break
}
}
}
pid, iid, ipData := findIP(uid, *listData)
check.OnChanged = nil
check.SetChecked(ipData.Check)
check.OnChanged = func(b bool) {
// 更新所有状态
for i, _ := range (*listData)[pid].Mappings {
if (*listData)[pid].Type == "Domain" {
if i != iid {
(*listData)[pid].Mappings[i].Check = false
}
}
}
(*listData)[pid].Mappings[iid].Check = b
file := internal.UpdateHostsFile(*listData)
err := logs.Set(file)
if err != nil {
return
}
storage.WriteConfig(*listData)
tree.Refresh()
}
}
func genCurrentDomainContent(uid string) string {
var current []string
for _, datum := range listData {
if uid == datum.Id {
current = append(current, fmt.Sprintf("域名:%s", datum.Name))
current = append(current, fmt.Sprintf("备注:%s", datum.Note))
current = append(current, fmt.Sprintf("类型:%s", datum.Type))
for _, ip := range datum.Mappings {
current = append(current, fmt.Sprintf("\t%s\t备注:%s", ip.Value, ip.Region))
}
break
}
}
return strings.Join(current, "\n")
}