forked from YanxinTang/clipboard-online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
77 lines (66 loc) · 1.55 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
package main
import (
"path"
"path/filepath"
"sync"
"github.com/gin-gonic/gin"
"github.com/lxn/walk"
)
type Application struct {
config *Config
*walk.MainWindow
ni *walk.NotifyIcon
wg sync.WaitGroup
}
func (app *Application) RunHTTPServer() {
app.wg.Add(1)
go func() {
engin := gin.New()
setupRoute(engin)
if err := engin.Run(":" + app.config.Port); err != nil {
app.ni.ShowError("HTTP Server 启动失败", "您的应用可能不能正常运行")
app.Synchronize(func() {
walk.App().Exit(1)
})
log.WithError(err).Error("failed to start http server")
return
}
}()
}
func (app *Application) StopHTTPServer() {
app.wg.Done()
}
func (app *Application) BeforeExit() {
app.StopHTTPServer()
app.ni.Dispose()
}
func (app *Application) AddActions(actions ...*walk.Action) error {
for _, action := range actions {
if err := app.ni.ContextMenu().Actions().Add(action); err != nil {
return err
}
}
return nil
}
func (app *Application) GetTempFilePath(filename string) string {
if !filepath.IsAbs(app.config.TempDir) {
// temp files path in exec path but not pwd
tempAbsPath := path.Join(execPath, app.config.TempDir)
return filepath.Join(tempAbsPath, filename)
}
return filepath.Join(app.config.TempDir, filename)
}
func NewApplication(config *Config) (*Application, error) {
app := new(Application)
var err error
app.config = config
app.MainWindow, err = walk.NewMainWindow()
if err != nil {
return nil, err
}
app.ni, err = walk.NewNotifyIcon(app.MainWindow)
if err != nil {
return nil, err
}
return app, nil
}