1
0

main.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Command mpckit is the desktop kit editor for the MPC Sample.
  2. package main
  3. import (
  4. "embed"
  5. "log"
  6. "github.com/wailsapp/wails/v3/pkg/application"
  7. "github.com/wailsapp/wails/v3/pkg/events"
  8. "git.monoexp.com/bajdev/mpckit/internal/app"
  9. )
  10. // The built Svelte app (frontend/dist), embedded in the binary.
  11. //
  12. //go:embed all:frontend/dist
  13. var assets embed.FS
  14. // Window sizes are in device-independent pixels, 16:9 like most displays.
  15. const (
  16. maxWidth, maxHeight = 1280, 720 // largest default size
  17. minWidth, minHeight = 960, 540 // smallest the layout is designed for
  18. screenShare = 0.9 // default size: this share of the screen's work area
  19. )
  20. func main() {
  21. a := application.New(application.Options{
  22. Name: "mpckit",
  23. Description: "Desktop kit editor for the MPC Sample",
  24. Services: []application.Service{
  25. application.NewService(app.NewService()),
  26. },
  27. Assets: application.AssetOptions{
  28. Handler: application.AssetFileServerFS(assets),
  29. },
  30. Mac: application.MacOptions{
  31. ApplicationShouldTerminateAfterLastWindowClosed: true,
  32. },
  33. })
  34. // Start hidden and size the window once the screens are known, so it
  35. // fits the display instead of being clamped by it.
  36. w := a.Window.NewWithOptions(application.WebviewWindowOptions{
  37. Title: "mpckit",
  38. Width: maxWidth,
  39. Height: maxHeight,
  40. MinWidth: minWidth,
  41. MinHeight: minHeight,
  42. Hidden: true,
  43. BackgroundColour: application.NewRGB(0x15, 0x17, 0x1A),
  44. URL: "/",
  45. })
  46. a.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
  47. if s := a.Screen.GetPrimary(); s != nil {
  48. width, height := fitSize(s.WorkArea.Width, s.WorkArea.Height)
  49. w.SetSize(width, height)
  50. }
  51. w.Center()
  52. w.Show()
  53. })
  54. if err := a.Run(); err != nil {
  55. log.Fatal(err)
  56. }
  57. }
  58. // fitSize returns a 16:9 window size that takes screenShare of the work
  59. // area (areaW x areaH), capped at maxWidth x maxHeight and no smaller than
  60. // minWidth x minHeight.
  61. func fitSize(areaW, areaH int) (int, int) {
  62. width := min(maxWidth, int(float64(areaW)*screenShare))
  63. height := width * 9 / 16
  64. if limit := int(float64(areaH) * screenShare); height > limit {
  65. height = limit
  66. width = height * 16 / 9
  67. }
  68. return max(width, minWidth), max(height, minHeight)
  69. }