-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
325 lines (290 loc) · 9.21 KB
/
main.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
// Package main include main screen.
package main
import (
"fmt"
"log"
"net/url"
"os"
Database "chatGopher/Database"
Screen "chatGopher/Screens"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/cmd/fyne_settings/settings"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
const preferenceCurrentUI = "currentUI"
var topWindow fyne.Window
func main() {
os.Setenv("FYNE_FONT", "./Font/LXGWWENKAIGBSCREENR.TTF") //加载字体
a := app.NewWithID("github.albertchamberlain.chatGopher")
a.SetIcon(theme.FyneLogo())
makeTray(a)
logLifecycle(a)
w := a.NewWindow("chatGopher")
topWindow = w
w.SetMainMenu(makeMenu(a, w))
w.SetMaster()
content := container.NewMax()
title := widget.NewLabel("Component name")
intro := widget.NewLabel("An introduction would probably go\nhere, as well as a")
intro.Wrapping = fyne.TextWrapWord
setUI := func(t Screen.AiModel) {
if fyne.CurrentDevice().IsMobile() {
child := a.NewWindow(t.Title)
topWindow = child
child.SetContent(t.View(topWindow))
child.Show()
child.SetOnClosed(func() {
topWindow = w
})
return
}
title.SetText(t.Title)
intro.SetText(t.Intro)
content.Objects = []fyne.CanvasObject{t.View(w)}
content.Refresh()
}
UIContainer := container.NewBorder(
container.NewVBox(title, widget.NewSeparator(), intro), nil, nil, nil, content)
if fyne.CurrentDevice().IsMobile() {
w.SetContent(makeNav(setUI, false))
} else {
split := container.NewHSplit(makeNav(setUI, true), UIContainer)
split.Offset = 0.2
w.SetContent(split)
}
w.Resize(fyne.NewSize(900, 900))
w.ShowAndRun()
os.Unsetenv("FYNE_FONT")
}
//打印窗口生命周期
func logLifecycle(a fyne.App) {
a.Lifecycle().SetOnStarted(func() {
log.Println("Lifecycle: Started")
})
a.Lifecycle().SetOnStopped(func() {
log.Println("Lifecycle: Stopped")
})
a.Lifecycle().SetOnEnteredForeground(func() {
log.Println("Lifecycle: Entered Foreground")
})
a.Lifecycle().SetOnExitedForeground(func() {
log.Println("Lifecycle: Exited Foreground")
})
}
//菜单栏
func makeMenu(a fyne.App, w fyne.Window) *fyne.MainMenu {
newItem := fyne.NewMenuItem("Add OpenAI Key", nil) //添加open ai key
checkedNotificationItem := fyne.NewMenuItem("Open Notifications", nil)
checkedNotificationItem.Checked = true
checkedAutoCopyItem := fyne.NewMenuItem("Auto Copy", nil)
checkedAutoCopyItem.Checked = true
addGPT3KeyWindow := func() {
w := a.NewWindow("Add GPT-3 Key") //另开一个窗口
inputkeyEntry := widget.NewPasswordEntry()
inputkeyEntry.SetPlaceHolder("Enter GPT-3 Key")
inputkeyEntryItem := container.NewVBox(inputkeyEntry, widget.NewButton("Save", func() {
fmt.Println(inputkeyEntry.Text)
Database.SetKeyAndValue("gpt3", []byte(inputkeyEntry.Text)) //插入到DB中
}))
w.SetContent(inputkeyEntryItem)
w.Resize(fyne.NewSize(400, 100))
w.Show()
}
addGPT3KeyItem := fyne.NewMenuItem("GPT-3", addGPT3KeyWindow)
addGPT3KeyItem.Icon = theme.AccountIcon()
addGPT4KeyWindow := func() {
w := a.NewWindow("Add GPT-4 Key") //另开一个窗口
inputkeyEntry := widget.NewPasswordEntry()
inputkeyEntry.SetPlaceHolder("Enter GPT-4 Key")
inputkeyEntryItem := container.NewVBox(inputkeyEntry, widget.NewButton("Save", func() {
fmt.Println(inputkeyEntry.Text)
Database.SetKeyAndValue("gpt4", []byte(inputkeyEntry.Text)) //插入到DB中
}))
w.SetContent(inputkeyEntryItem)
w.Resize(fyne.NewSize(400, 100))
w.Show()
}
addGPT4KeyItem := fyne.NewMenuItem("GPT-4", addGPT4KeyWindow)
addGPT4KeyItem.Icon = theme.AccountIcon()
ListAllKeyWindow := func() {
w := a.NewWindow("List All Key") //另开一个窗口
kvs := Database.ListAllApi()
fmt.Println(kvs)
var counter float32
counter = 1
outputkeyEntryItem := container.NewVBox()
for k, v := range kvs {
outputkeyEntry := widget.NewPasswordEntry()
kvitems := k + ":" + v
outputkeyEntry.SetText(kvitems)
fmt.Println(outputkeyEntry.Text)
outputkeyEntryItem.Add(outputkeyEntry)
w.Resize(fyne.NewSize(400, 100*counter))
counter++
}
w.SetContent(outputkeyEntryItem)
w.Show()
}
listAllKeyItem := fyne.NewMenuItem("List All Key", ListAllKeyWindow)
listAllKeyItem.Icon = theme.ComputerIcon()
newItem.ChildMenu = fyne.NewMenu("",
addGPT3KeyItem,
addGPT4KeyItem,
listAllKeyItem,
)
openSettings := func() {
w := a.NewWindow("Appearance Settings") //另开一个窗口
w.SetContent(settings.NewSettings().LoadAppearanceScreen(w))
w.Resize(fyne.NewSize(480, 480))
w.Show()
}
settingsItem := fyne.NewMenuItem("Appearance", openSettings)
settingsShortcut := &desktop.CustomShortcut{KeyName: fyne.KeyComma, Modifier: fyne.KeyModifierShortcutDefault}
settingsItem.Shortcut = settingsShortcut
w.Canvas().AddShortcut(settingsShortcut, func(shortcut fyne.Shortcut) {
openSettings()
})
cutShortcut := &fyne.ShortcutCut{Clipboard: w.Clipboard()}
cutItem := fyne.NewMenuItem("Cut", func() {
shortcutFocused(cutShortcut, w)
})
cutItem.Shortcut = cutShortcut
copyShortcut := &fyne.ShortcutCopy{Clipboard: w.Clipboard()}
copyItem := fyne.NewMenuItem("Copy", func() {
shortcutFocused(copyShortcut, w)
})
copyItem.Shortcut = copyShortcut
pasteShortcut := &fyne.ShortcutPaste{Clipboard: w.Clipboard()}
pasteItem := fyne.NewMenuItem("Paste", func() {
shortcutFocused(pasteShortcut, w)
})
pasteItem.Shortcut = pasteShortcut
performFind := func() { fmt.Println("Menu Find") }
findItem := fyne.NewMenuItem("Find", performFind)
findItem.Shortcut = &desktop.CustomShortcut{KeyName: fyne.KeyF, Modifier: fyne.KeyModifierShortcutDefault | fyne.KeyModifierAlt | fyne.KeyModifierShift | fyne.KeyModifierControl | fyne.KeyModifierSuper}
w.Canvas().AddShortcut(findItem.Shortcut, func(shortcut fyne.Shortcut) {
performFind()
})
helpMenu := fyne.NewMenu("Help",
fyne.NewMenuItem("Documentation", func() {
u, _ := url.Parse("https://github.com/Albertchamberlain/chatGopher")
a.OpenURL(u)
}),
fyne.NewMenuItem("Support", func() {
u, _ := url.Parse("https://github.com/Albertchamberlain/chatGopher")
a.OpenURL(u)
}),
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("Sponsor", func() {
u, _ := url.Parse("https://github.com/Albertchamberlain/chatGopher")
a.OpenURL(u)
}))
// a quit item will be appended to our first (File) menu
file := fyne.NewMenu("setting", newItem, checkedAutoCopyItem, checkedNotificationItem)
device := fyne.CurrentDevice()
if !device.IsMobile() && !device.IsBrowser() { //PC
file.Items = append(file.Items, fyne.NewMenuItemSeparator(), settingsItem)
}
mainMean := fyne.NewMainMenu(
file,
fyne.NewMenu("Edit", cutItem, copyItem, pasteItem, fyne.NewMenuItemSeparator(), findItem),
helpMenu,
)
checkedNotificationItem.Action = func() {
checkedNotificationItem.Checked = !checkedNotificationItem.Checked
mainMean.Refresh()
fmt.Println(checkedAutoCopyItem.Checked)
}
checkedAutoCopyItem.Action = func() {
checkedAutoCopyItem.Checked = !checkedAutoCopyItem.Checked
mainMean.Refresh()
fmt.Println(checkedAutoCopyItem.Checked)
}
return mainMean
}
func makeTray(a fyne.App) {
if desk, ok := a.(desktop.App); ok {
h := fyne.NewMenuItem("Hello", func() {})
h.Icon = theme.HomeIcon()
menu := fyne.NewMenu("Hello World", h)
h.Action = func() {
log.Println("System tray menu tapped")
h.Label = "Welcome"
menu.Refresh()
}
desk.SetSystemTrayMenu(menu)
}
}
func unsupportedUI(t Screen.AiModel) bool {
//fmt.Println("Unsupport UI")
return !t.SupportWeb && fyne.CurrentDevice().IsBrowser()
}
//侧面导航栏
func makeNav(setModelUI func(modelUi Screen.AiModel), loadPrevious bool) fyne.CanvasObject {
a := fyne.CurrentApp()
tree := &widget.Tree{
ChildUIDs: func(uid string) []string {
return Screen.AiModelsIndex[uid]
},
IsBranch: func(uid string) bool {
children, ok := Screen.AiModelsIndex[uid]
return ok && len(children) > 0
},
CreateNode: func(branch bool) fyne.CanvasObject {
return widget.NewLabel("Collection Widgets")
},
UpdateNode: func(uid string, branch bool, obj fyne.CanvasObject) {
t, ok := Screen.AiModels[uid]
if !ok {
fyne.LogError("Missing ui panel: "+uid, nil)
return
}
obj.(*widget.Label).SetText(t.Title)
if unsupportedUI(t) {
obj.(*widget.Label).TextStyle = fyne.TextStyle{Italic: true}
} else {
obj.(*widget.Label).TextStyle = fyne.TextStyle{}
}
},
OnSelected: func(uid string) {
if t, ok := Screen.AiModels[uid]; ok {
if unsupportedUI(t) {
return
}
a.Preferences().SetString(preferenceCurrentUI, uid)
setModelUI(t)
}
},
}
if loadPrevious {
currentPref := a.Preferences().StringWithFallback(preferenceCurrentUI, "Welcome")
tree.Select(currentPref)
}
themes := container.NewGridWithColumns(2,
widget.NewButton("Light", func() {
a.Settings().SetTheme(theme.LightTheme())
}),
widget.NewButton("Dark", func() {
a.Settings().SetTheme(theme.DarkTheme())
}),
)
return container.NewBorder(nil, themes, nil, nil, tree)
}
//剪切板相关
func shortcutFocused(s fyne.Shortcut, w fyne.Window) {
switch sh := s.(type) {
case *fyne.ShortcutCopy:
sh.Clipboard = w.Clipboard()
case *fyne.ShortcutCut:
sh.Clipboard = w.Clipboard()
case *fyne.ShortcutPaste:
sh.Clipboard = w.Clipboard()
}
if focused, ok := w.Canvas().Focused().(fyne.Shortcutable); ok {
focused.TypedShortcut(s)
}
}