package main import ( "embed" _ "embed" "log" "time" "voidraft/internal/services" "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() // 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), }, Mac: application.MacOptions{ ApplicationShouldTerminateAfterLastWindowClosed: true, }, }) // 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. log.Println("Creating main window...") mainWindow := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Title: "voidraft", Width: 700, Height: 800, Hidden: false, Mac: application.MacWindow{ InvisibleTitleBarHeight: 50, Backdrop: application.MacBackdropTranslucent, TitleBar: application.MacTitleBarHiddenInset, }, BackgroundColour: application.NewRGB(27, 38, 54), URL: "/", }) systray := app.NewSystemTray() iconBytes, _ := assets.ReadFile("appicon.png") systray.SetIcon(iconBytes) systray.SetLabel("VoidRaft") menu := app.NewMenu() menu.Add("显示主窗口").OnClick(func(data *application.Context) { mainWindow.Show() }) menu.AddSeparator() menu.Add("退出").OnClick(func(data *application.Context) { app.Quit() }) systray.SetMenu(menu) systray.AttachWindow(mainWindow) systray.WindowDebounce(200 * time.Millisecond) // 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.EmitEvent("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) } }