-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (90 loc) · 2.57 KB
/
main.go
File metadata and controls
109 lines (90 loc) · 2.57 KB
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
package main
import (
"embed"
_ "embed"
"os"
"runtime"
"strings"
"netcatcher/config"
"netcatcher/llog"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/services/notifications"
)
//go:embed frontend/dist
var assets embed.FS
//go:embed build/appicon.png
var appIcon []byte
func main() {
configPath := config.DefaultConfigPath()
wailsApp := application.New(application.Options{
Name: "NetCatcher",
Description: "Network route manager",
Icon: appIcon,
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
Mac: application.MacOptions{
ActivationPolicy: application.ActivationPolicyAccessory,
},
})
var notifSvc *notifications.NotificationService
if shouldRegisterNotifications() {
notifSvc = notifications.New()
wailsApp.RegisterService(application.NewService(notifSvc))
} else {
llog.Infof("notify", "system notifications unavailable (run from .app bundle on macOS)")
}
app := NewApp(configPath, wailsApp, notifSvc)
// Register App as a service so its exported methods are available to the frontend.
wailsApp.RegisterService(application.NewService(app))
// Create main window
mainWindow := wailsApp.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "NetCatcher",
Width: 900,
Height: 600,
MinWidth: 700,
MinHeight: 450,
Frameless: false,
URL: "/",
BackgroundColour: application.NewRGBA(13, 17, 23, 255),
})
// Hide window instead of closing (intercept close via hook).
mainWindow.RegisterHook(events.Common.WindowClosing, func(event *application.WindowEvent) {
event.Cancel()
mainWindow.Hide()
})
// System tray
trayMenu := wailsApp.Menu.New()
trayMenu.Add("Show Window").OnClick(func(ctx *application.Context) {
mainWindow.Show()
mainWindow.Focus()
})
trayMenu.AddSeparator()
trayMenu.Add("Quit").OnClick(func(ctx *application.Context) {
wailsApp.Quit()
})
systray := wailsApp.SystemTray.New()
systray.SetIcon(appIcon)
systray.SetMenu(trayMenu)
// Register shutdown hook so monitoring stops cleanly.
wailsApp.OnShutdown(func() {
app.OnShutdown()
})
// Start monitoring on launch.
app.OnStartup(nil)
if err := wailsApp.Run(); err != nil {
llog.Errorf("app", "wails run failed: %v", err)
os.Exit(1)
}
}
func shouldRegisterNotifications() bool {
if runtime.GOOS == "darwin" {
exe, err := os.Executable()
if err != nil {
return false
}
return strings.Contains(exe, ".app/Contents/MacOS/")
}
return true
}