1
0

main.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // ============================================================================
  2. // main.go — 应用入口
  3. //
  4. // 创建 Wails 应用、主窗口与系统托盘,注册服务与窗口事件;
  5. // 启动每秒时间广播与待办提醒调度协程,随后进入事件循环。
  6. // ============================================================================
  7. package main
  8. import (
  9. "bytes" // 托盘菜单图标:PNG 编码缓冲区
  10. "embed" // Go 内嵌资源包:将前端静态文件打包进可执行文件
  11. "fmt" // 格式化 I/O:用于控制台打印调试信息
  12. "image" // 托盘菜单图标:RGBA 画布
  13. "image/color"
  14. "image/png" // 托盘菜单图标:将 RGBA 编码为 PNG 字节
  15. "log" // 日志:记录并输出错误
  16. "os" // 操作系统:服务模式退出码
  17. "strconv" // 数字转换:解析自定义 Tab 序号
  18. "strings" // 字符串处理:Tab id 折叠标记解析
  19. "sync" // 窗口尺寸保存防抖
  20. "time" // 时间处理:用于每秒定时发送事件
  21. "github.com/wailsapp/wails/v3/pkg/application" // Wails3 核心:应用/窗口/菜单/对话框/事件/托盘
  22. "github.com/wailsapp/wails/v3/pkg/events" // Wails3 事件常量:窗口关闭、文件拖放等
  23. "github.com/wailsapp/wails/v3/pkg/services/dock"
  24. )
  25. // ============================================================================
  26. // 前端资源嵌入
  27. // ============================================================================
  28. // Wails 使用 Go 的 `embed` 包将前端文件嵌入到二进制文件中。
  29. // frontend/dist 目录下的所有文件都会被嵌入到可执行文件里,
  30. // 并由 Wails 内置的资源服务器(Asset Server)提供给 WebView 前端访问。
  31. //go:embed all:frontend/dist
  32. var assets embed.FS // assets 持有 frontend/dist 下全部前端文件的只读文件系统
  33. //go:embed frontend/public/wails.png
  34. var trayIcon []byte // 嵌入系统托盘图标(PNG 格式)
  35. // 全局运行时对象:服务、API、托盘菜单都要用到应用实例与主窗口
  36. var (
  37. app *application.App // 应用实例
  38. mainWindow *application.WebviewWindow // 主窗口
  39. // 窗口隐藏前的位置,恢复显示时还原
  40. savedX, savedY int
  41. hasSavedPos bool
  42. )
  43. // showMainWindow 统一显示入口:先还原位置,再显示并聚焦。
  44. // 托盘、热键、API 接口都通过它唤出窗口。
  45. func showMainWindow() {
  46. if mainWindow == nil {
  47. return
  48. }
  49. // 热键/托盘呼出时,即使窗口已收拢为圆点,也先还原到收拢前的尺寸。
  50. compactMu.Lock()
  51. wasCompact := compactActive
  52. compactMu.Unlock()
  53. if wasCompact {
  54. (&SettingsService{}).ToggleWindowCompact()
  55. }
  56. if hasSavedPos {
  57. mainWindow.SetPosition(savedX, savedY) // 还原隐藏前的窗口坐标
  58. }
  59. mainWindow.Show() // 显示窗口
  60. mainWindow.Focus() // 聚焦到前台
  61. if wasCompact && app != nil {
  62. app.Event.Emit("windowCompact", false) // 通知前端移除圆点收拢样式
  63. }
  64. }
  65. var (
  66. windowSizeSaveMu sync.Mutex
  67. windowSizeSaveTimer *time.Timer
  68. )
  69. // rememberWindowSize 延迟保存窗口尺寸,避免拖动边框时频繁写配置文件。
  70. func rememberWindowSize(width, height int) {
  71. if width <= 0 || height <= 0 {
  72. return
  73. }
  74. windowSizeSaveMu.Lock()
  75. defer windowSizeSaveMu.Unlock()
  76. if windowSizeSaveTimer != nil {
  77. windowSizeSaveTimer.Stop()
  78. }
  79. windowSizeSaveTimer = time.AfterFunc(500*time.Millisecond, func() {
  80. windowSizeSaveMu.Lock()
  81. defer windowSizeSaveMu.Unlock()
  82. settings := loadSettings()
  83. if settings.WindowWidth == width && settings.WindowHeight == height {
  84. return
  85. }
  86. settings.WindowWidth, settings.WindowHeight = width, height
  87. if err := saveSettings(settings); err != nil {
  88. log.Printf("保存窗口尺寸失败:%v", err)
  89. }
  90. })
  91. }
  92. // init 在 main 函数之前自动执行,用于应用启动前的注册工作。
  93. func init() {
  94. application.RegisterEvent[string]("time") // 注册 string 类型的自定义事件,绑定生成器据此为前端生成强类型事件 API
  95. application.RegisterEvent[string]("apilog") // Gin 日志行实时推送(「主机」页订阅)
  96. application.RegisterEvent[string]("todoReminder") // 待办提醒:到点时向前端广播提醒内容
  97. application.RegisterEvent[string]("ntfyChat") // ntfy 聊天室:订阅主题后收到消息广播给前端
  98. application.RegisterEvent[int]("handleFlash") // 拖动手柄圆点闪烁:API 触发,前端播放颜色+大小动画
  99. application.RegisterEvent[bool]("windowCompact") // 后端呼出窗口时同步圆点收拢/还原状态
  100. application.RegisterEvent[string]("switchTab") // 托盘菜单点击 Tab:前端切换到对应页面
  101. // 升级状态由 Wails 内置升级框架自行推给前端(wails:updater:* 系列事件),无需在此注册
  102. }
  103. // trayTabItem 托盘菜单里的一个 Tab 入口(显示名 + 前端 tabId)
  104. type trayTabItem struct {
  105. name string // 菜单显示文字(与 Tab 按钮同名)
  106. id string // 前端 tabId(内置 id 或 custom-N)
  107. }
  108. // trayTabItems 按已保存配置的 Tab 顺序生成托盘菜单项(「设置」除外,托盘里单独固定一项)。
  109. // 前端同名逻辑:内置 Tab 用固定中文名,自定义 Tab 用配置里的名字。
  110. func trayTabItems() []trayTabItem {
  111. settings := loadSettings()
  112. // 内置 Tab 名称(与前端 TAB_NAMES 保持一致)
  113. builtin := map[string]string{
  114. "links": "网址", "news": "新闻", "github": "Github", "stock": "股市",
  115. "todo": "待办", "record": "记录", "tools": "工具", "locals": "本地",
  116. "control": "控制", "host": "服务", "update": "更新", "settings": "设置",
  117. }
  118. nameFor := func(id string) (string, bool) {
  119. if n, ok := builtin[id]; ok {
  120. return n, true
  121. }
  122. // 自定义 Tab:id 形如 custom-3
  123. if i, err := strconv.Atoi(strings.TrimPrefix(id, "custom-")); err == nil {
  124. if i >= 0 && i < len(settings.CustomTabs) && settings.CustomTabs[i].Name != "" {
  125. return settings.CustomTabs[i].Name, true
  126. }
  127. }
  128. return "", false
  129. }
  130. items := make([]trayTabItem, 0, len(settings.TabOrder))
  131. seen := make(map[string]bool, len(settings.TabOrder))
  132. add := func(id, name string) {
  133. if seen[id] {
  134. return
  135. }
  136. seen[id] = true
  137. items = append(items, trayTabItem{name: name, id: id})
  138. }
  139. for _, raw := range settings.TabOrder {
  140. id := strings.TrimSpace(raw)
  141. wrapped := strings.HasPrefix(id, "[") && strings.HasSuffix(id, "]")
  142. if wrapped {
  143. id = strings.TrimSpace(id[1 : len(id)-1]) // 去掉「更多」折叠标记
  144. }
  145. if id == "settings" {
  146. continue // 「设置」在托盘里固定在分隔线下方单独一项
  147. }
  148. if name, ok := nameFor(id); ok {
  149. add(id, name)
  150. }
  151. }
  152. return items
  153. }
  154. // ============================================================================
  155. // 托盘菜单图标:用 image/png 程序化生成 16×16 图标,无需随仓库附带素材文件。
  156. // SetBitmap 只接收 PNG 字节,因此这里现画现编码。
  157. // ============================================================================
  158. // drawMenuIcon 在 16×16 画布上执行 draw,再把画布编码为 PNG 字节。
  159. func drawMenuIcon(draw func(img *image.RGBA)) []byte {
  160. img := image.NewRGBA(image.Rect(0, 0, 16, 16))
  161. draw(img)
  162. var buf bytes.Buffer
  163. _ = png.Encode(&buf, img) // 固定 16×16 RGBA 编码不会失败
  164. return buf.Bytes()
  165. }
  166. // trayIconLight 浅色托盘菜单图标配色
  167. var trayIconLight = color.RGBA{226, 232, 240, 255}
  168. // iconWindow 窗口图标:表示「显示主窗口」
  169. func iconWindow() []byte {
  170. return drawMenuIcon(func(img *image.RGBA) {
  171. c := trayIconLight
  172. for y := 2; y <= 13; y++ {
  173. for x := 2; x <= 13; x++ {
  174. if y == 2 || y == 13 || x == 2 || x == 13 || y == 5 {
  175. img.Set(x, y, c)
  176. }
  177. }
  178. }
  179. d := color.RGBA{120, 170, 255, 255}
  180. for y := 7; y <= 12; y++ {
  181. for x := 4; x <= 11; x++ {
  182. if x <= y+2 {
  183. img.Set(x, y, d)
  184. }
  185. }
  186. }
  187. })
  188. }
  189. // iconGear 齿轮图标:表示「设置」
  190. func iconGear() []byte {
  191. return drawMenuIcon(func(img *image.RGBA) {
  192. c := trayIconLight
  193. for x := 0; x <= 15; x++ {
  194. for y := 0; y <= 15; y++ {
  195. dx, dy := x-7, y-7
  196. r := dx*dx + dy*dy
  197. if r <= 25 && r >= 9 {
  198. img.Set(x, y, c)
  199. }
  200. }
  201. }
  202. })
  203. }
  204. // iconExit 叉号图标:表示「退出」
  205. func iconExit() []byte {
  206. return drawMenuIcon(func(img *image.RGBA) {
  207. c := color.RGBA{255, 138, 138, 255}
  208. put := func(x, y int) {
  209. if x >= 0 && x <= 15 && y >= 0 && y <= 15 { // 防止对角线端点越界
  210. img.Set(x, y, c)
  211. }
  212. }
  213. for x := 3; x <= 12; x++ {
  214. put(x, x+3) // 主对角线
  215. put(x, 21-x) // 副对角线
  216. }
  217. })
  218. }
  219. // main 是应用程序的入口,整体流程如下:
  220. // 1. 创建 Wails 应用实例(注册服务、配置资源服务器)
  221. // 2. 创建主窗口
  222. // 3. 应用已保存的配置(热键、置顶、开机启动、API 服务、滚动条窗口)
  223. // 4. 创建系统托盘(关闭时隐藏到托盘,双击恢复显示)
  224. // 5. 注册窗口事件(文件拖放、关闭→隐藏托盘、显示时还原位置)
  225. // 6. 启动后台协程,运行应用(阻塞直到退出)
  226. func main() {
  227. // 升级的下载、校验、原子替换与重启全部交给 Wails v3 内置升级框架(app.Updater),
  228. // 它通过环境变量派生 helper 子进程完成二进制交换,无需在本进程里处理文件锁。
  229. // 这里只处理两件自研的事:
  230. // 1. --rollback 角色:回滚子进程,在主程序退出后用备份覆盖并重启旧版;
  231. // 2. 启动时判定是否要自动回滚(新版本反复异常启动则退回上一版)。
  232. // 两条路径都已派生子进程完成替换,本进程直接退出,不再初始化应用。
  233. if rollbackModeRequested() {
  234. if err := runRollbackRole(); err != nil {
  235. log.Printf("回滚执行失败:%v", err)
  236. }
  237. return
  238. }
  239. if maybeAutoRollback() {
  240. os.Exit(0)
  241. }
  242. // 服务模式:由 Windows 服务管理器带 --service 启动,无界面、仅提供电源类 API
  243. // (关机/重启/休眠/取消/状态),无人登录时也可调用;登录后仍由界面程序提供完整功能。
  244. if serviceModeRequested() {
  245. if err := runPlatformService(); err != nil {
  246. fmt.Fprintln(os.Stderr, "服务模式启动失败:", err)
  247. os.Exit(1)
  248. }
  249. return
  250. }
  251. // 创建 dock service 实例,自定义 badge 选项
  252. dockService := dock.NewWithOptions(dock.BadgeOptions{
  253. TextColour: color.RGBA{255, 255, 255, 255}, // 白色文字
  254. BackgroundColour: color.RGBA{0, 0, 255, 255}, // 蓝色背景
  255. FontName: "consolab.ttf", // 粗体 Consolas 字体
  256. FontSize: 20, // 单字符字号
  257. SmallFontSize: 14, // 多字符字号
  258. })
  259. // ------------------------------------------------------------------------
  260. // 第 1 步:创建 Wails 应用实例
  261. // ------------------------------------------------------------------------
  262. app = application.New(application.Options{
  263. Name: "TengTu Toolbox",
  264. Description: "TengTu Toolbox for OS",
  265. Services: []application.Service{
  266. application.NewService(&GreetService{}), // 注册 GreetService,前端可通过 bindings 直接调用其 Greet 方法
  267. application.NewService(&SettingsService{}), // 注册 SettingsService,供设置页读写配置、拉取行情
  268. application.NewService(&LocalSendService{}), // 注册 LocalSendService,供 LocalSend 页控制发现/接收
  269. application.NewService(NewCrocService()), // 注册 CrocService,供 Croc 传送工具发送/接收文件
  270. application.NewService(NewYTDLPService()), // 注册 YTDLPService,供视频下载工具调用 yt-dlp
  271. application.NewService(NewMemosService()), // 注册 MemosService,供 Memos 速记工具调用 REST API
  272. application.NewService(NewBrightnessService()), // 注册 BrightnessService,供显示器亮度工具调光
  273. application.NewService(&LocalService{}), // 注册 LocalService,供「本地」页启动配置的程序/脚本
  274. application.NewService(NewUDPService()), // 注册 UDPService,供工具页 UDP 键值服务器启停/发送
  275. application.NewService(NewProxyService()), // 注册 ProxyService,供工具页「网络代理」设置/启用/禁用系统代理
  276. application.NewService(dockService), // 注册 dockService,前端可通过 bindings 直接调用其 SetBadge 方法
  277. },
  278. Assets: application.AssetOptions{
  279. Handler: application.AssetFileServerFS(assets), // 使用嵌入的前端文件作为资源服务器处理器
  280. },
  281. Mac: application.MacOptions{
  282. ApplicationShouldTerminateAfterLastWindowClosed: false, // 最后一个窗口关闭后不退出应用(托盘常驻)
  283. },
  284. })
  285. // ------------------------------------------------------------------------
  286. // 第 2 步:读取配置并创建主窗口(使用上次保存的窗口尺寸)
  287. // ------------------------------------------------------------------------
  288. ensureSettingsFile() // 首次运行:自动生成默认配置文件(已存在则不覆盖)
  289. settings := loadSettings() // 读取用户配置,首次运行返回默认值(开机启动默认开启)
  290. win := app.Window.NewWithOptions(application.WebviewWindowOptions{
  291. Title: "腾图工具箱", // 窗口标题
  292. Width: settings.WindowWidth,
  293. Height: settings.WindowHeight,
  294. Mac: application.MacWindow{
  295. InvisibleTitleBarHeight: 50, // 顶部 50px 为不可见但可拖拽的标题栏区域
  296. Backdrop: application.MacBackdropTranslucent, // 半透明背景材质
  297. TitleBar: application.MacTitleBarHiddenInset, // 隐藏标题栏但保留红绿灯按钮
  298. },
  299. Windows: application.WindowsWindow{
  300. DisableFramelessWindowDecorations: false, // 禁用无边框窗口装饰(标题栏、边框等)
  301. },
  302. BackgroundColour: application.NewRGB(6, 7, 15), // 窗口背景色(深色)
  303. URL: "/", // WebView 加载的页面路径("/" 即前端首页 index.html)
  304. DevToolsEnabled: false, // 关闭开发者工具(生产环境不开放)
  305. DefaultContextMenuDisabled: true, // 禁用默认上下文菜单,防止右键点击打开菜单
  306. UseApplicationMenu: true, // 启用应用菜单
  307. EnableFileDrop: true, // 启用文件拖拽,需在 HTML 中添加 data-file-drop-target 标签
  308. ContentProtectionEnabled: true, // 启用内容保护,防止截取窗口内容
  309. Permissions: map[application.PermissionType]application.Permission{
  310. application.PermissionMicrophone: application.PermissionAllow,
  311. application.PermissionNotifications: application.PermissionAllow,
  312. application.PermissionCamera: application.PermissionAllow,
  313. application.PermissionGeolocation: application.PermissionAllow,
  314. application.PermissionClipboardRead: application.PermissionAllow,
  315. },
  316. Frameless: true, // 无边框窗口
  317. BackgroundType: application.BackgroundTypeTransparent, // 透明背景
  318. })
  319. mainWindow = win // 暴露给热键、托盘、API 使用
  320. // ------------------------------------------------------------------------
  321. // 第 3 步:应用已保存的配置(热键、置顶、开机启动)
  322. // ------------------------------------------------------------------------
  323. applySettings(settings) // 注册热键、设置置顶、同步开机启动
  324. startUIRegistration() // 向服务模式登记界面 API 地址(服务转发未实现接口时使用)
  325. if settings.Brightness.RestoreOnStart {
  326. restoreBrightness(settings.Brightness.Levels) // 启动时恢复上次各显示器亮度
  327. }
  328. // 初始化内置升级框架并检查一次新版本(私有仓库,地址来自设置页)。
  329. // 结果经 wails:updater:* 事件推给「更新」Tab,这里不再单独广播。
  330. if err := initAppUpdater(); err != nil {
  331. log.Printf("内置升级框架初始化失败:%v", err)
  332. } else {
  333. go func() {
  334. time.Sleep(2 * time.Second) // 等窗口与前端就绪,避免事件早于订阅
  335. if err := checkUpdateNow(); err != nil {
  336. log.Printf("启动检查新版本失败:%v", err)
  337. }
  338. }()
  339. }
  340. // 新版本存活判定:窗口创建后持续运行一小段时间即视为正常,清除升级待确认标记。
  341. // 只有进程在这之前就退出(崩溃/白屏循环)才会累计 failCount 并触发自动回滚。
  342. go func() {
  343. time.Sleep(8 * time.Second)
  344. MarkUpdateOK()
  345. }()
  346. // ------------------------------------------------------------------------
  347. // 第 4 步:创建系统托盘
  348. // ------------------------------------------------------------------------
  349. // 关闭窗口时隐藏到托盘;双击托盘图标恢复窗口显示
  350. // 托盘菜单提供「显示 / 退出」两个入口
  351. tray := app.SystemTray.New()
  352. tray.SetIcon(trayIcon) // 设置托盘图标(嵌入的 wails.png)
  353. tray.SetTooltip("腾图工具箱") // 设置鼠标悬停提示
  354. tray.AttachWindow(win) // 绑定窗口:点击托盘图标自动切换窗口显隐
  355. // 构建托盘右键菜单
  356. trayMenu := application.NewMenu()
  357. // 菜单项图标:运行时程序化生成 16×16 PNG(SetBitmap 支持,官方系统托盘文档标注 Windows/macOS/Linux 均支持)
  358. trayMenu.Add("显示主窗口").SetBitmap(iconWindow()).OnClick(func(_ *application.Context) {
  359. showMainWindow() // 显示窗口到隐藏前的位置并聚焦
  360. })
  361. trayMenu.AddSeparator() // 「显示主窗口」与 Tab 列表之间的分隔线
  362. // 为每个 Tab 生成同名菜单项:点击后显示窗口并切到对应页面
  363. for _, tab := range trayTabItems() {
  364. id := tab.id // 闭包捕获当前 Tab id
  365. trayMenu.Add(tab.name).OnClick(func(_ *application.Context) {
  366. showMainWindow()
  367. if app != nil {
  368. app.Event.Emit("switchTab", id) // 前端收到后切换到该 Tab
  369. }
  370. })
  371. }
  372. trayMenu.AddSeparator() // Tab 列表与「设置 / 退出」之间的分隔线
  373. trayMenu.Add("设置").SetBitmap(iconGear()).OnClick(func(_ *application.Context) {
  374. showMainWindow()
  375. if app != nil {
  376. app.Event.Emit("switchTab", "settings")
  377. }
  378. })
  379. trayMenu.Add("退出").SetBitmap(iconExit()).OnClick(func(_ *application.Context) {
  380. app.Quit() // 彻底退出应用
  381. })
  382. tray.SetMenu(trayMenu) // 设置托盘右键菜单
  383. // ------------------------------------------------------------------------
  384. // 第 5 步:注册窗口事件处理
  385. // ------------------------------------------------------------------------
  386. // 监听「文件拖放完成」事件,打印拖放目标详情与所有被拖入的文件
  387. win.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) {
  388. files := event.Context().DroppedFiles() // 获取拖入的文件路径列表
  389. details := event.Context().DropTargetDetails() // 获取拖放目标位置等详情
  390. fmt.Println("Drop target details:", details)
  391. // 遍历并打印每个被拖入的文件
  392. for _, file := range files {
  393. fmt.Println("Dropped:", file)
  394. }
  395. })
  396. // 记住窗口尺寸供下次启动使用;收拢为圆点时的 32×32 不保存,
  397. // 圆点还原产生的尺寸变化则正常记录。
  398. win.OnWindowEvent(events.Common.WindowDidResize, func(_ *application.WindowEvent) {
  399. compactMu.Lock()
  400. isCompact := compactActive
  401. compactMu.Unlock()
  402. if isCompact {
  403. return
  404. }
  405. width, height := win.Size()
  406. rememberWindowSize(width, height)
  407. })
  408. // 注册「窗口关闭前」勾子:记录位置后取消关闭,改为隐藏到托盘
  409. win.RegisterHook(events.Common.WindowClosing, func(event *application.WindowEvent) {
  410. savedX, savedY = win.Position() // 记录隐藏前的窗口坐标
  411. hasSavedPos = true // 标记位置已记录
  412. event.Cancel() // 拦截关闭事件,阻止窗口被系统销毁
  413. win.Hide() // 隐藏窗口到托盘
  414. })
  415. // 窗口重新显示时(含托盘图标点击恢复),还原到隐藏前的位置
  416. win.OnWindowEvent(events.Common.WindowShow, func(_ *application.WindowEvent) {
  417. if hasSavedPos {
  418. win.SetPosition(savedX, savedY) // 还原窗口位置
  419. }
  420. })
  421. // 监听「窗口正在关闭」事件,执行清理逻辑
  422. win.OnWindowEvent(events.Common.WindowClosing, func(event *application.WindowEvent) {
  423. fmt.Printf("Window %s is closing\n", win.Name()) // 清理代码写在这里,此处仅打印关闭日志
  424. })
  425. // API 服务与滚动条窗口已在 applySettings(第 3 步)中按配置启动
  426. // 时间事件:footer 只需显示到分,故仅在分钟变化时广播,避免每秒唤醒前端
  427. lastMinute := ""
  428. go func() {
  429. for {
  430. now := time.Now().Format(time.DateTime) // 格式化为 DateTime 时间字符串
  431. minute := now[:len(now)-3] // 去掉 ":SS" 秒部分,取到分
  432. if minute != lastMinute { // 仅跨分钟时发送一次
  433. app.Event.Emit("time", now) // 向所有前端窗口广播 "time" 事件
  434. lastMinute = minute
  435. }
  436. time.Sleep(time.Second) // 每秒检查一次分钟边界
  437. }
  438. }()
  439. // 待办提醒调度:每 15 秒扫描一次待办文件,到点的提醒通过事件广播给前端弹窗。
  440. // 放在后台协程,即使窗口隐藏/最小化也能触发。
  441. go startTodoReminderLoop()
  442. // 运行应用(阻塞,直到退出)
  443. if err := app.Run(); err != nil {
  444. log.Fatal(err) // 运行过程中发生错误则记录日志并退出进程
  445. }
  446. }