Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
188 lines (155 loc) · 5.09 KB
/
Copy pathmain.go
File metadata and controls
188 lines (155 loc) · 5.09 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
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
package main
import (
"AI-ViewNote/backend/models"
"AI-ViewNote/backend/repository"
"AI-ViewNote/backend/service"
"embed"
"log"
"os"
"path/filepath"
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
wnotifications "github.com/wailsapp/wails/v3/pkg/services/notifications"
)
type WailsEventEmitter struct{}
func (w *WailsEventEmitter) Emit(eventName string, data any) {
app := application.Get()
if app != nil {
app.Event.Emit(eventName, data)
}
}
//go:embed all:frontend/dist
var assets embed.FS
//go:embed build/appicon.png
var trayIconPNG []byte
//go:embed build/windows/icon.ico
var trayIconICO []byte
func init() {
application.RegisterEvent[string]("time")
}
func main() {
configDir, _ := os.UserConfigDir()
dBPath := filepath.Join(configDir, "AI-ViewNote", "data.db")
db, err := repository.InitDB(dBPath)
if err != nil {
log.Fatal("Failed to initialize database: ", err)
}
defer db.Close()
wailsEmitter := &WailsEventEmitter{}
notificationService := wnotifications.New()
taskRepo := repository.NewTaskRepository(db)
confRepo := repository.NewConfigRepository(db)
chatRepo := repository.NewChatRepository(db)
taskService := service.NewTaskService(taskRepo, confRepo, wailsEmitter, notificationService)
confService := service.NewConfigService(confRepo)
chatService := service.NewChatService(confService, taskRepo, chatRepo, wailsEmitter)
// 重置卡住的任务(应用意外关闭后)
if resp := taskService.ResetStuckTasks(); !resp.Success {
log.Printf("Failed to reset stuck tasks: %s\n", resp.Message)
}
// 确保默认配置项存在
if err := confService.EnsureConfigDefaultValue(models.RunInBackground, "true"); err != nil {
log.Printf("Failed to ensure config '%s': %s\n", models.RunInBackground, err.Error())
}
if err := confService.EnsureConfigDefaultValue(models.DesktopNotifications, "true"); err != nil {
log.Printf("Failed to ensure config '%s': %s\n", models.DesktopNotifications, err.Error())
}
if err := confService.EnsureConfigDefaultValue(models.LogFolding, "true"); err != nil {
log.Printf("Failed to ensure config '%s': %s\n", models.LogFolding, err.Error())
}
if err := confService.EnsureConfigDefaultValue(models.EnableScreenshot, "true"); err != nil {
log.Printf("Failed to ensure config '%s': %s\n", models.EnableScreenshot, err.Error())
}
app := application.New(application.Options{
Name: "AI-ViewNote",
Description: "AI powered video note taking application",
Services: []application.Service{
application.NewService(taskService),
application.NewService(confService),
application.NewService(chatService),
application.NewService(notificationService),
},
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: false,
},
Windows: application.WindowsOptions{
DisableQuitOnLastWindowClosed: true,
},
Linux: application.LinuxOptions{
DisableQuitOnLastWindowClosed: true,
},
})
mainWindow := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "AI-ViewNote - AI Powered Video Note Taking",
Mac: application.MacWindow{
InvisibleTitleBarHeight: 50,
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInset,
},
BackgroundColour: application.NewRGB(27, 38, 54),
URL: "/",
Width: 1200,
Height: 800,
MinWidth: 800,
MinHeight: 600,
})
// 托盘模式下拦截关闭按钮,改为隐藏窗口。
mainWindow.RegisterHook(events.Common.WindowClosing, func(event *application.WindowEvent) {
runInBackground, err := confService.GetBoolConfig(models.RunInBackground, false)
if err != nil {
log.Printf("Failed to load config '%s': %s. Using default value false.\n", models.RunInBackground, err.Error())
runInBackground = false
}
if runInBackground {
event.Cancel()
mainWindow.Hide()
return
}
app.Quit()
})
setupSystemTray(app, mainWindow)
// 运行应用程序。此操作会阻塞,直到应用退出。
err = app.Run()
// 如果运行应用时发生错误,则记录日志并退出。
if err != nil {
log.Fatal(err)
}
}
func setupSystemTray(app *application.App, mainWindow application.Window) {
trayMenu := application.NewMenu()
showMainWindow := func() {
mainWindow.Show()
mainWindow.UnMinimise()
mainWindow.Focus()
}
trayMenu.Add("显示主窗口").OnClick(func(_ *application.Context) {
showMainWindow()
})
trayMenu.Add("隐藏主窗口").OnClick(func(_ *application.Context) {
mainWindow.Hide()
})
trayMenu.AddSeparator()
trayMenu.Add("退出").OnClick(func(_ *application.Context) {
app.Quit()
})
tray := app.SystemTray.New().
SetMenu(trayMenu).
AttachWindow(mainWindow).
WindowOffset(8)
tray.SetTooltip("AI-ViewNote")
if runtime.GOOS == "windows" && len(trayIconICO) > 0 {
tray.SetIcon(trayIconICO)
} else if len(trayIconPNG) > 0 {
tray.SetIcon(trayIconPNG)
}
tray.OnRightClick(func() {
tray.OpenMenu()
})
tray.OnClick(func() {
showMainWindow()
})
}