-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
125 lines (107 loc) · 2.98 KB
/
app.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
package main
import (
"changeme/config"
"context"
"fmt"
"github.com/imroc/req/v3"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) Menu() *menu.Menu {
return menu.NewMenuFromItems(
menu.SubMenu("设置", menu.NewMenuFromItems(
menu.Text("关于", nil, func(_ *menu.CallbackData) {
a.diag(config.Description, false)
}),
menu.Text("检查更新", nil, func(_ *menu.CallbackData) {
resp, err := req.C().SetUserAgent("Chrome Win7: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1").R().Get("https://github.com/hq-zhonger/gopoc/version")
if err != nil {
a.diag("检查更新出错\n"+err.Error(), true)
return
}
fmt.Println(resp.String())
needUpdate := config.Version < resp.String()
msg := config.VersionNewMsg
btns := []string{config.BtnConfirmText}
if needUpdate {
msg = fmt.Sprintf(config.VersionOldMsg, config.Version)
btns = []string{"确定", "取消"}
}
selection, err := a.diag(msg, false, btns...)
if err != nil {
return
}
if needUpdate && selection == config.BtnConfirmText {
url := fmt.Sprintf("https://github.com/yhy0/ChYing/releases/tag/%s", config.Version)
runtime.BrowserOpenURL(a.ctx, url)
}
}),
menu.Text(
"主页",
keys.Combo("H", keys.CmdOrCtrlKey, keys.ShiftKey),
func(_ *menu.CallbackData) {
runtime.BrowserOpenURL(a.ctx, "https://github.com/yhy0/ChYing/")
},
),
menu.Text(
"背景",
keys.Combo("H", keys.CmdOrCtrlKey, keys.ShiftKey),
func(_ *menu.CallbackData) {
},
),
menu.Separator(),
menu.Text("退出", keys.CmdOrCtrl("Q"), func(_ *menu.CallbackData) {
runtime.Quit(a.ctx)
}),
)),
menu.EditMenu(),
menu.SubMenu("Help", menu.NewMenuFromItems(
menu.Text(
"打开配置文件夹",
keys.Combo("C", keys.CmdOrCtrlKey, keys.ShiftKey),
func(_ *menu.CallbackData) {
},
),
)),
)
}
// diag ...
func (a *App) diag(message string, error bool, buttons ...string) (string, error) {
if len(buttons) == 0 {
buttons = []string{
config.BtnConfirmText,
}
}
var t runtime.DialogType
if error {
t = runtime.ErrorDialog
} else {
t = runtime.InfoDialog
}
return runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: t,
Title: config.Title,
Message: message,
CancelButton: config.BtnCancelText,
DefaultButton: config.BtnConfirmText,
Buttons: buttons,
})
}