add systray
This commit is contained in:
154
main.go
154
main.go
@@ -24,9 +24,25 @@ type FilesDroppedEvent struct {
|
||||
Target string `json:"target"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
conf := config.Load()
|
||||
type App struct {
|
||||
app *application.App
|
||||
mainWindows *application.WebviewWindow
|
||||
conf *config.Config
|
||||
discoveryService *discovery.Service
|
||||
transferService *transfer.Service
|
||||
notifier *notifications.NotificationService
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 设置日志
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
}))
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
|
||||
func NewApp() *App {
|
||||
conf := config.Load()
|
||||
app := application.New(application.Options{
|
||||
Name: "mesh-drop",
|
||||
Assets: application.AssetOptions{
|
||||
@@ -38,6 +54,26 @@ func main() {
|
||||
Icon: icon,
|
||||
})
|
||||
|
||||
win := app.Window.NewWithOptions(application.WebviewWindowOptions{
|
||||
Title: "mesh drop",
|
||||
Width: conf.GetWindowState().Width,
|
||||
Height: conf.GetWindowState().Height,
|
||||
X: conf.GetWindowState().X,
|
||||
Y: conf.GetWindowState().Y,
|
||||
EnableFileDrop: true,
|
||||
Linux: application.LinuxWindow{
|
||||
WebviewGpuPolicy: application.WebviewGpuPolicyAlways,
|
||||
},
|
||||
})
|
||||
|
||||
return &App{
|
||||
app: app,
|
||||
mainWindows: win,
|
||||
conf: conf,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) registerServices() {
|
||||
// 初始化通知服务
|
||||
notifier := notifications.New()
|
||||
authorized, err := notifier.RequestNotificationAuthorization()
|
||||
@@ -51,54 +87,49 @@ func main() {
|
||||
port := 9989
|
||||
|
||||
// 初始化发现服务
|
||||
discoveryService := discovery.NewService(conf, app, port)
|
||||
discoveryService := discovery.NewService(a.conf, a.app, port)
|
||||
discoveryService.Start()
|
||||
|
||||
// 初始化传输服务
|
||||
transferService := transfer.NewService(conf, app, notifier, port, discoveryService)
|
||||
transferService := transfer.NewService(a.conf, a.app, notifier, port, discoveryService)
|
||||
transferService.Start()
|
||||
// 加载传输历史
|
||||
if conf.GetSaveHistory() {
|
||||
if a.conf.GetSaveHistory() {
|
||||
transferService.LoadHistory()
|
||||
}
|
||||
|
||||
slog.Info("Backend Service Started", "discovery_port", discovery.DiscoveryPort, "transfer_port", port)
|
||||
a.discoveryService = discoveryService
|
||||
a.transferService = transferService
|
||||
a.notifier = notifier
|
||||
|
||||
app.RegisterService(application.NewService(discoveryService))
|
||||
app.RegisterService(application.NewService(transferService))
|
||||
app.RegisterService(application.NewService(conf))
|
||||
app.RegisterService(application.NewService(notifier))
|
||||
a.app.RegisterService(application.NewService(discoveryService))
|
||||
a.app.RegisterService(application.NewService(transferService))
|
||||
a.app.RegisterService(application.NewService(a.conf))
|
||||
a.app.RegisterService(application.NewService(notifier))
|
||||
}
|
||||
|
||||
window := app.Window.NewWithOptions(application.WebviewWindowOptions{
|
||||
Title: "mesh drop",
|
||||
Width: conf.WindowState.Width,
|
||||
Height: conf.WindowState.Height,
|
||||
X: conf.WindowState.X,
|
||||
Y: conf.WindowState.Y,
|
||||
EnableFileDrop: true,
|
||||
Linux: application.LinuxWindow{
|
||||
WebviewGpuPolicy: application.WebviewGpuPolicyAlways,
|
||||
},
|
||||
})
|
||||
|
||||
// 设置系统托盘
|
||||
setupSystray(app, window)
|
||||
func (a *App) registerCustomEvents() {
|
||||
application.RegisterEvent[FilesDroppedEvent]("files-dropped")
|
||||
application.RegisterEvent[[]discovery.Peer]("peers:update")
|
||||
application.RegisterEvent[application.Void]("transfer:refreshList")
|
||||
}
|
||||
|
||||
func (a *App) setupWindowEvents() {
|
||||
// 窗口文件拖拽事件
|
||||
window.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) {
|
||||
a.mainWindows.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) {
|
||||
files := event.Context().DroppedFiles()
|
||||
details := event.Context().DropTargetDetails()
|
||||
app.Event.Emit("files-dropped", FilesDroppedEvent{
|
||||
a.app.Event.Emit("files-dropped", FilesDroppedEvent{
|
||||
Files: files,
|
||||
Target: details.ElementID,
|
||||
})
|
||||
})
|
||||
|
||||
// 应用关闭事件
|
||||
app.OnShutdown(func() {
|
||||
x, y := window.Position()
|
||||
width, height := window.Size()
|
||||
conf.SetWindowState(config.WindowState{
|
||||
a.app.OnShutdown(func() {
|
||||
x, y := a.mainWindows.Position()
|
||||
width, height := a.mainWindows.Size()
|
||||
a.conf.SetWindowState(config.WindowState{
|
||||
X: x,
|
||||
Y: y,
|
||||
Width: width,
|
||||
@@ -106,31 +137,66 @@ func main() {
|
||||
})
|
||||
|
||||
// 保存传输历史
|
||||
if conf.GetSaveHistory() {
|
||||
transferService.SaveHistory()
|
||||
if a.conf.GetSaveHistory() {
|
||||
// 将 pending 状态的任务改为 canceled
|
||||
t := a.transferService.GetTransferList()
|
||||
for _, task := range t {
|
||||
if task.Status == transfer.TransferStatusPending {
|
||||
task.Status = transfer.TransferStatusCanceled
|
||||
}
|
||||
}
|
||||
a.transferService.SaveHistory()
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
err := conf.Save()
|
||||
err := a.conf.Save()
|
||||
if err != nil {
|
||||
slog.Error("Failed to save config", "error", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 注册事件
|
||||
application.RegisterEvent[FilesDroppedEvent]("files-dropped")
|
||||
application.RegisterEvent[[]discovery.Peer]("peers:update")
|
||||
application.RegisterEvent[application.Void]("transfer:refreshList")
|
||||
func (a *App) setupSystray() {
|
||||
systray := a.app.SystemTray.New()
|
||||
systray.SetIcon(icon)
|
||||
systray.SetLabel("Mesh Drop")
|
||||
|
||||
// 设置日志
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
}))
|
||||
slog.SetDefault(logger)
|
||||
menu := a.app.NewMenu()
|
||||
menu.Add("Quit").OnClick(func(ctx *application.Context) {
|
||||
a.app.Quit()
|
||||
})
|
||||
|
||||
// 运行应用
|
||||
err = app.Run()
|
||||
systray.OnClick(func() {
|
||||
if a.mainWindows.IsVisible() {
|
||||
a.mainWindows.Hide()
|
||||
} else {
|
||||
a.mainWindows.Show()
|
||||
a.mainWindows.Focus()
|
||||
}
|
||||
})
|
||||
|
||||
systray.SetMenu(menu)
|
||||
|
||||
a.mainWindows.OnWindowEvent(events.Common.WindowClosing, func(event *application.WindowEvent) {
|
||||
if a.conf.GetCloseToSystray() {
|
||||
event.Cancel()
|
||||
a.mainWindows.Hide()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) Run() {
|
||||
a.registerServices()
|
||||
a.setupSystray()
|
||||
a.registerCustomEvents()
|
||||
a.setupWindowEvents()
|
||||
err := a.app.Run()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := NewApp()
|
||||
app.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user