main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "embed"
  4. _ "embed"
  5. "time"
  6. _ "git.familybaby.top/flight/tools/config"
  7. _ "git.familybaby.top/flight/tools/log"
  8. "git.familybaby.top/flight/tools/services"
  9. "github.com/sirupsen/logrus"
  10. "github.com/wailsapp/wails/v3/pkg/application"
  11. )
  12. var (
  13. log = logrus.WithField("module", "main")
  14. )
  15. // Wails uses Go's `embed` package to embed the frontend files into the binary.
  16. // Any files in the frontend/dist folder will be embedded into the binary and
  17. // made available to the frontend.
  18. // See https://pkg.go.dev/embed for more information.
  19. //go:embed all:frontend/dist
  20. var assets embed.FS
  21. func init() {
  22. // Register a custom event whose associated data type is string.
  23. // This is not required, but the binding generator will pick up registered events
  24. // and provide a strongly typed JS/TS API for them.
  25. application.RegisterEvent[string]("time")
  26. }
  27. // main function serves as the application's entry point. It initializes the application, creates a window,
  28. // and starts a goroutine that emits a time-based event every second. It subsequently runs the application and
  29. // logs any error that might occur.
  30. func main() {
  31. // Create a new Wails application by providing the necessary options.
  32. // Variables 'Name' and 'Description' are for application metadata.
  33. // 'Assets' configures the asset server with the 'FS' variable pointing to the frontend files.
  34. // 'Bind' is a list of Go struct instances. The frontend has access to the methods of these instances.
  35. // 'Mac' options tailor the application when running an macOS.
  36. app := application.New(application.Options{
  37. Name: "tools",
  38. Description: "A demo of using raw HTML & CSS",
  39. Services: []application.Service{
  40. // application.NewService(&GreetService{}),
  41. application.NewService(&services.Service{}),
  42. },
  43. Assets: application.AssetOptions{
  44. Handler: application.AssetFileServerFS(assets),
  45. },
  46. Mac: application.MacOptions{
  47. ApplicationShouldTerminateAfterLastWindowClosed: true,
  48. },
  49. })
  50. // Create a new window with the necessary options.
  51. // 'Title' is the title of the window.
  52. // 'Mac' options tailor the window when running on macOS.
  53. // 'BackgroundColour' is the background colour of the window.
  54. // 'URL' is the URL that will be loaded into the webview.
  55. app.Window.NewWithOptions(application.WebviewWindowOptions{
  56. Title: "Window 1",
  57. Mac: application.MacWindow{
  58. InvisibleTitleBarHeight: 50,
  59. Backdrop: application.MacBackdropTranslucent,
  60. TitleBar: application.MacTitleBarHiddenInset,
  61. },
  62. BackgroundColour: application.NewRGB(27, 38, 54),
  63. URL: "/",
  64. })
  65. // Create a goroutine that emits an event containing the current time every second.
  66. // The frontend can listen to this event and update the UI accordingly.
  67. go func() {
  68. for {
  69. now := time.Now().Format(time.RFC1123)
  70. app.Event.Emit("time", now)
  71. time.Sleep(time.Second)
  72. }
  73. }()
  74. // Run the application. This blocks until the application has been exited.
  75. err := app.Run()
  76. // If an error occurred while running the application, log it and exit.
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. }