This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
/
main_window.go
341 lines (319 loc) · 10.1 KB
/
main_window.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/dialog"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
"github.com/perqin/go-shadowsocks2"
"github.com/perqin/shadowsocks-fyne/material"
"github.com/perqin/shadowsocks-fyne/resources"
customWidget "github.com/perqin/shadowsocks-fyne/widget"
"log"
"strconv"
)
const toolbarHeight = 56
var mainWindow fyne.Window
var drawer *subscriptionDrawer
var titleLabel *widget.Label
var updateSubscriptionButton *material.ToolbarActionButton
var editSubscriptionButton *material.ToolbarActionButton
var removeSubscriptionButton *material.ToolbarActionButton
var runButton *material.ToolbarActionButton
var stopButton *material.ToolbarActionButton
var addProfileButton *material.ToolbarActionButton
var editProfileButton *material.ToolbarActionButton
var settingsButton *material.ToolbarActionButton
var profileList *widget.Box
func showMainWindow() {
if mainWindow != nil {
mainWindow.RequestFocus()
return
}
mainWindow = application.NewWindow(appName)
mainWindow.SetOnClosed(func() {
mainWindow = nil
})
mainWindow.CenterOnScreen()
drawer = newSubscriptionDrawer(config.Subscriptions)
titleLabel = widget.NewLabel("Title")
updateSubscriptionButton = material.NewToolbarActionButton(resources.RefreshPng, onRefreshAction)
editSubscriptionButton = material.NewToolbarActionButton(resources.EditsubscriptionPng, onEditSubscriptionAction)
removeSubscriptionButton = material.NewToolbarActionButton(resources.DeletePng, onRemoveSubscriptionAction)
runButton = material.NewToolbarActionButton(resources.PlayPng, onRunAction)
stopButton = material.NewToolbarActionButton(resources.StopPng, onStopAction)
addProfileButton = material.NewToolbarActionButton(resources.AddprofilePng, onAddProfileAction)
editProfileButton = material.NewToolbarActionButton(resources.EditprofilePng, onEditProfileAction)
settingsButton = material.NewToolbarActionButton(resources.SettingsPng, onSettingsAction)
profileList = widget.NewVBox()
selectSubscription(config.CurrentProfileSubscription)
mainWindow.SetContent(fyne.NewContainerWithLayout(layout.NewMaxLayout(),
canvas.NewRectangle(application.Settings().Theme().(*material.Theme).WindowBackground),
fyne.NewContainerWithLayout(&mainWindowLayout{},
drawer,
material.NewToolbar(titleLabel, updateSubscriptionButton, editSubscriptionButton, removeSubscriptionButton,
runButton, stopButton, addProfileButton, editProfileButton, settingsButton),
widget.NewScrollContainer(profileList))))
mainWindow.Show()
}
func refreshAll(resetSelection bool) {
subscriptions := config.Subscriptions
titles := make([]string, len(subscriptions)-1)
for i, s := range subscriptions[1:] {
titles[i] = s.Name
}
drawer.SubscriptionTitles = titles
drawer.Refresh()
if drawer.SelectedIndex < 0 || drawer.SelectedIndex >= len(config.Subscriptions) {
resetSelection = true
}
if resetSelection {
selectSubscription(0)
} else {
selectSubscription(drawer.SelectedIndex)
}
}
func selectSubscription(index int) {
drawer.SelectedIndex = index
drawer.Refresh()
titleLabel.SetText(config.Subscriptions[index].Name)
profileList.Children = buildServerList(index, config.Subscriptions[index])
profileList.Refresh()
}
// UI widgets that should be updated on data changed
var subscriptionsTabs *widget.TabContainer
var currentProfileWidget *ProfileWidget
func buildServerList(index int, subscription Subscription) []fyne.CanvasObject {
list := make([]fyne.CanvasObject, 0)
for indexP, profile := range subscription.Profiles {
indexP := indexP
profileWidget := NewProfileWidget()
profileWidget.OnTapped = func() {
if currentProfileWidget != nil {
currentProfileWidget.Selected = false
currentProfileWidget.Refresh()
}
currentProfileWidget = profileWidget
selectCurrentProfile(indexP, index)
}
profileWidget.Profile = profile
if index == config.CurrentProfileSubscription && indexP == config.CurrentProfile {
profileWidget.Selected = true
currentProfileWidget = profileWidget
}
profileWidget.Refresh()
list = append(list, profileWidget)
}
return list
}
func showEditSubscriptionDialog(index int) {
if index == 0 {
log.Println("The Subscription for custom profiles cannot be edited.")
return
}
edit := index != -1
if edit && (index < 0 || index >= len(config.Subscriptions)) {
log.Println(fmt.Sprintf("Invalid index %d", index))
return
}
var subscription Subscription
if edit {
subscription = config.Subscriptions[index]
}
// Show dialog for new subscription
nameEntry := widget.NewEntry()
urlEntry := widget.NewEntry()
if edit {
nameEntry.SetText(subscription.Name)
urlEntry.SetText(subscription.Url)
}
form := widget.NewForm(
widget.NewFormItem("Name", nameEntry),
widget.NewFormItem("Url", urlEntry))
dialog.ShowCustomConfirm("Add subscription", "Add", "Cancel", form, func(confirmed bool) {
if confirmed {
if edit {
// Update subscription
subscription.Name = nameEntry.Text
subscription.Url = urlEntry.Text
SaveSubscription(index, subscription)
refreshAll(false)
} else {
// Add new one
AddSubscription(Subscription{
Name: nameEntry.Text,
Url: urlEntry.Text,
})
refreshAll(true)
}
}
}, mainWindow)
}
func onRefreshAction() {
go func() {
err := UpdateSubscription(drawer.SelectedIndex)
if err != nil {
customWidget.Toast(err.Error())
return
}
refreshAll(false)
}()
}
func onEditSubscriptionAction() {
showEditSubscriptionDialog(config.CurrentProfileSubscription)
}
func onRemoveSubscriptionAction() {
subscriptionIndex := drawer.SelectedIndex
err := removeSubscription(subscriptionIndex)
if err != nil {
log.Println(err)
return
}
refreshAll(false)
}
func onRunAction() {
si := config.CurrentProfileSubscription
if si < 0 || si >= len(config.Subscriptions) {
log.Printf("Invalid index %d\n", si)
return
}
pi := config.CurrentProfile
if pi < 0 || pi >= len(config.Subscriptions[si].Profiles) {
log.Printf("Invalid index %d %d\n", si, pi)
return
}
profile := config.Subscriptions[si].Profiles[pi]
client := fmt.Sprintf("ss://%s:%s@%s:%d", profile.Method, profile.Password, profile.Server, profile.ServerPort)
log.Printf("onRunAction client:%s\n", client)
if err := runShadowsocks(shadowsocks2.Flags{
Client: client,
Socks: fmt.Sprintf("%s:%d", config.LocalAddress, config.LocalPort),
}); err != nil {
customWidget.Toast(fmt.Sprintf("Fail to stop: %v\n", err))
}
}
func onStopAction() {
if err := stopShadowsocks(); err != nil {
customWidget.Toast(fmt.Sprintf("Fail to stop: %v\n", err))
}
}
func showEditProfileDialog(edit bool) {
var si, pi int
var profile Profile
if edit {
si = config.CurrentProfileSubscription
pi = config.CurrentProfile
if si != subscriptionsTabs.CurrentTabIndex() {
log.Println("Please switch to the tab containing the profile you want to edit.")
return
}
if si < 0 || si >= len(config.Subscriptions) {
log.Printf("Invalid subscription index %d\n", si)
return
}
if pi < 0 || pi >= len(config.Subscriptions[si].Profiles) {
log.Printf("Invalid profile index %d\n", pi)
return
}
profile = config.Subscriptions[si].Profiles[pi]
} else {
si = subscriptionsTabs.CurrentTabIndex()
}
// Can show dialog now
nameEntry := widget.NewEntry()
serverEntry := widget.NewEntry()
portEntry := widget.NewEntry()
methodEntry := widget.NewEntry()
passwordEntry := widget.NewEntry()
if edit {
nameEntry.Text = profile.Name
serverEntry.Text = profile.Server
portEntry.Text = fmt.Sprint(profile.ServerPort)
methodEntry.Text = profile.Method
passwordEntry.Text = profile.Password
}
form := widget.NewForm(
widget.NewFormItem("Name", nameEntry),
widget.NewFormItem("Server", serverEntry),
widget.NewFormItem("Port", portEntry),
widget.NewFormItem("Method", methodEntry),
widget.NewFormItem("Password", passwordEntry))
dialog.ShowCustomConfirm("Edit Profile", "Save", "Cancel", form, func(confirmed bool) {
if confirmed {
profile.Name = nameEntry.Text
profile.Server = serverEntry.Text
// TODO: Validate
profile.ServerPort, _ = strconv.Atoi(portEntry.Text)
profile.Method = methodEntry.Text
profile.Password = passwordEntry.Text
var err error
if edit {
if si != 0 {
log.Println("Only custom profile can be edited.")
return
}
err = saveProfile(si, pi, profile)
} else {
err = addProfile(si, profile)
}
if err != nil {
log.Println(err)
} else {
refreshAll(false)
}
}
}, mainWindow)
}
func onAddProfileAction() {
showEditProfileDialog(false)
}
func onEditProfileAction() {
showEditProfileDialog(true)
}
func onSettingsAction() {
localAddressEntry := widget.NewEntry()
localAddressEntry.SetText(config.LocalAddress)
localPortEntry := widget.NewEntry()
localPortEntry.SetText(strconv.Itoa(config.LocalPort))
form := widget.NewForm(
widget.NewFormItem("Local address", localAddressEntry),
widget.NewFormItem("Local port", localPortEntry))
dialog.ShowCustomConfirm("Settings", "Save", "Cancel", form, func(confirmed bool) {
if confirmed {
// Update settings
localAddress := localAddressEntry.Text
localPort, err := strconv.Atoi(localPortEntry.Text)
if err != nil {
log.Println(err)
return
}
config.LocalAddress = localAddress
config.LocalPort = localPort
if err = SaveConfig(); err != nil {
log.Println(err)
}
}
}, mainWindow)
}
type mainWindowLayout struct {
}
func (l *mainWindowLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
drawerWidth := objects[0].MinSize().Width
// Drawer
objects[0].Move(fyne.NewPos(0, 0))
objects[0].Resize(fyne.NewSize(drawerWidth, size.Height))
// Toolbar background
objects[1].Move(fyne.NewPos(drawerWidth, 0))
objects[1].Resize(fyne.NewSize(size.Width-drawerWidth, toolbarHeight))
// Content
objects[2].Move(fyne.NewPos(drawerWidth, toolbarHeight))
objects[2].Resize(fyne.NewSize(size.Width-drawerWidth, size.Height-toolbarHeight))
}
func (l *mainWindowLayout) MinSize(objects []fyne.CanvasObject) (size fyne.Size) {
drawerMinSize := objects[0].MinSize()
toolbarMinWidth := objects[1].MinSize().Width
size.Width = drawerMinSize.Width + fyne.Max(toolbarMinWidth, 640)
size.Height = fyne.Max(drawerMinSize.Height, toolbarHeight)
return
}