Files
voidraft/main.go

126 lines
4.1 KiB
Go

package main
import (
"embed"
_ "embed"
"log"
"log/slog"
"time"
"voidraft/internal/services"
"voidraft/internal/systray"
"github.com/wailsapp/wails/v3/pkg/application"
)
// Wails uses Go's `embed` package to embed the frontend files into the binary.
// Any files in the frontend/dist folder will be embedded into the binary and
// made available to the frontend.
// See https://pkg.go.dev/embed for more information.
//go:embed all:frontend/dist
var assets embed.FS
// main function serves as the application's entry point. It initializes the application, creates a window,
// and starts a goroutine that emits a time-based event every second. It subsequently runs the application and
// logs any error that might occur.
func main() {
serviceManager := services.NewServiceManager()
var encryptionKey = [32]byte{
0x1e, 0x1f, 0x1c, 0x1d, 0x1a, 0x1b, 0x18, 0x19,
0x16, 0x17, 0x14, 0x15, 0x12, 0x13, 0x10, 0x11,
0x0e, 0x0f, 0x0c, 0x0d, 0x0a, 0x0b, 0x08, 0x09,
0x06, 0x07, 0x04, 0x05, 0x02, 0x03, 0x00, 0x01,
}
// Create a new Wails application by providing the necessary options.
// Variables 'Name' and 'Description' are for application metadata.
// 'Assets' configures the asset server with the 'FS' variable pointing to the frontend files.
// 'Bind' is a list of Go struct instances. The frontend has access to the methods of these instances.
// 'Mac' options tailor the application when running an macOS.
app := application.New(application.Options{
Name: "voidraft",
Description: "voidraft",
Services: serviceManager.GetServices(),
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
LogLevel: slog.LevelDebug,
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
SingleInstance: &application.SingleInstanceOptions{
UniqueID: "com.voidraft",
EncryptionKey: encryptionKey,
AdditionalData: map[string]string{
"launchtime": time.Now().Local().String(),
},
},
})
// Create a new window with the necessary options.
// 'Title' is the title of the window.
// 'Mac' options tailor the window when running on macOS.
// 'BackgroundColour' is the background colour of the window.
// 'URL' is the URL that will be loaded into the webview.
mainWindow := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "voidraft",
Width: 700,
Height: 800,
Hidden: false,
Frameless: true,
DevToolsEnabled: false,
DefaultContextMenuDisabled: false,
Mac: application.MacWindow{
InvisibleTitleBarHeight: 50,
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInset,
},
Windows: application.WindowsWindow{
Theme: application.SystemDefault,
},
BackgroundColour: application.NewRGB(27, 38, 54),
URL: "/",
})
mainWindow.Center()
// 获取托盘服务并设置应用引用
trayService := serviceManager.GetTrayService()
trayService.SetAppReferences(app, mainWindow)
// 获取窗口服务并设置应用引用
windowService := serviceManager.GetWindowService()
windowService.SetAppReferences(app, mainWindow)
// 设置系统托盘
systray.SetupSystemTray(app, mainWindow, assets, trayService)
// 初始化热键服务
hotkeyService := serviceManager.GetHotkeyService()
err := hotkeyService.Initialize(app)
if err != nil {
panic(err)
}
// 设置对话框服务的窗口绑定
dialogService := serviceManager.GetDialogService()
dialogService.SetWindow(mainWindow)
// Create a goroutine that emits an event containing the current time every second.
// The frontend can listen to this event and update the UI accordingly.
go func() {
for {
now := time.Now().Format(time.RFC1123)
app.Event.Emit("time", now)
time.Sleep(time.Second)
}
}()
// Run the application. This blocks until the application has been exited.
err = app.Run()
// If an error occurred while running the application, log it and exit.
if err != nil {
log.Fatal(err)
}
}