-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
tray_darwin.go
136 lines (112 loc) · 2.79 KB
/
tray_darwin.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
//go:build darwin
// +build darwin
package main
import (
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"syscall"
"time"
"github.com/fy0/systray"
"github.com/gen2brain/beeep"
"github.com/labstack/echo/v4"
"sealdice-core/dice"
"sealdice-core/icon"
log "sealdice-core/utils/kratos"
)
var theDm *dice.DiceManager
func trayInit(dm *dice.DiceManager) {
theDm = dm
runtime.LockOSThread()
systray.Run(onReady, onExit)
}
func hideWindow() {
}
func showWindow() {
}
func TestRunning() bool {
return false
}
func tempDirWarn() {
log.Info("当前工作路径为临时目录,因此拒绝继续执行。")
}
func showMsgBox(title string, message string) {
log.Info(title, message)
}
func executeWin(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: os.Getppid(),
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
return cmd
}
var _trayPortStr = "3211"
var systrayQuited bool = false
func onReady() {
systray.SetIcon(icon.Data)
systray.SetTitle("海豹核心")
systray.SetTooltip("海豹TRPG骰点核心")
mOpen := systray.AddMenuItem("打开界面", "开启WebUI")
mOpen.SetIcon(icon.Data)
mOpenExeDir := systray.AddMenuItem("打开海豹目录", "访达访问程序所在目录")
mQuit := systray.AddMenuItem("退出", "退出程序")
go func() {
_ = beeep.Notify("SealDice", "我藏在托盘区域了,点我的小图标可以快速打开UI", "icon/icon.ico")
}()
for {
select {
case <-mOpen.ClickedCh:
_ = exec.Command(`open`, `http://localhost:`+_trayPortStr).Start()
case <-mOpenExeDir.ClickedCh:
_ = exec.Command(`open`, filepath.Dir(os.Args[0])).Start()
case <-mQuit.ClickedCh:
systrayQuited = true
cleanupCreate(theDm)()
systray.Quit()
time.Sleep(3 * time.Second)
os.Exit(0)
}
}
}
func onExit() {
// clean up hear
}
func httpServe(e *echo.Echo, dm *dice.DiceManager, hideUI bool) {
portStr := "3211"
go func() {
for {
time.Sleep(5 * time.Second)
if systrayQuited {
break
}
runtime.LockOSThread()
systray.SetTooltip("海豹TRPG骰点核心 #" + portStr)
runtime.UnlockOSThread()
}
}()
rePort := regexp.MustCompile(`:(\d+)$`)
m := rePort.FindStringSubmatch(dm.ServeAddress)
if len(m) > 0 {
portStr = m[1]
}
ln, err := net.Listen("tcp", ":"+portStr)
if err != nil {
log.Errorf("端口已被占用,即将自动退出: %s", dm.ServeAddress)
runtime.Goexit()
}
_ = ln.Close()
// exec.Command(`cmd`, `/c`, `start`, fmt.Sprintf(`http://localhost:%s`, portStr)).Start()
log.Infof("如果浏览器没有自动打开,请手动访问:\nhttp://localhost:%s", portStr)
err = e.Start(dm.ServeAddress)
if err != nil {
log.Errorf("端口已被占用,即将自动退出: %s", dm.ServeAddress)
return
}
}