Ver Fonte

Make main read as a list of steps

Move the app options and panic handler, the window and its sizing, the
close guard and the file-drop handler into newApp, newWindow,
guardClose and handleFileDrops, each with the comment it had (R5).
main() goes from 96 lines to 12; the steps run in the same order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude há 1 hora atrás
pai
commit
2a65de0117
1 ficheiros alterados com 28 adições e 14 exclusões
  1. 28 14
      main.go

+ 28 - 14
main.go

@@ -43,6 +43,19 @@ func init() {
 func main() {
 	d := &dialogs{}
 	svc, host := app.NewService(d)
+	a := newApp(svc, host)
+	d.a = a
+	w := newWindow(a)
+	guardClose(a, w, host)
+	handleFileDrops(a, w, host)
+	if err := a.Run(); err != nil {
+		log.Fatal(err)
+	}
+}
+
+// newApp creates the application. Only svc is bound; host serves the
+// sample previews.
+func newApp(svc *app.Service, host *app.Host) *application.App {
 	var a *application.App
 	a = application.New(application.Options{
 		Name:        "MPCkit",
@@ -65,11 +78,12 @@ func main() {
 			ApplicationShouldTerminateAfterLastWindowClosed: true,
 		},
 	})
+	return a
+}
 
-	d.a = a
-
-	// Start hidden and size the window once the screens are known, so it
-	// fits the display instead of being clamped by it.
+// newWindow opens the main window. It starts hidden and is sized once the
+// screens are known, so it fits the display instead of being clamped by it.
+func newWindow(a *application.App) *application.WebviewWindow {
 	w := a.Window.NewWithOptions(application.WebviewWindowOptions{
 		Title:            "MPCkit",
 		Width:            maxWidth,
@@ -81,7 +95,6 @@ func main() {
 		BackgroundColour: application.NewRGB(0x15, 0x17, 0x1A),
 		URL:              "/",
 	})
-
 	a.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
 		if s := a.Screen.GetPrimary(); s != nil {
 			width, height := fitSize(s.WorkArea.Width, s.WorkArea.Height)
@@ -90,10 +103,13 @@ func main() {
 		w.Center()
 		w.Show()
 	})
+	return w
+}
 
-	// Closing with unsaved changes: stop, and let the frontend ask. If the
-	// user chooses to discard them, the frontend calls CloseWindow, which
-	// closes for real.
+// guardClose handles closing with unsaved changes: stop, and let the
+// frontend ask. If the user chooses to discard them, the frontend calls
+// CloseWindow, which closes for real.
+func guardClose(a *application.App, w *application.WebviewWindow, host *app.Host) {
 	var closeConfirmed atomic.Bool
 	host.SetCloser(func() {
 		closeConfirmed.Store(true)
@@ -106,9 +122,11 @@ func main() {
 		e.Cancel()
 		a.Event.Emit(eventClose)
 	})
+}
 
-	// A WAV dropped on a pad (an element with data-file-drop-target and
-	// data-slot) is assigned to that pad.
+// handleFileDrops assigns a WAV dropped on a pad (an element with
+// data-file-drop-target and data-slot) to that pad.
+func handleFileDrops(a *application.App, w *application.WebviewWindow, host *app.Host) {
 	w.OnWindowEvent(events.Common.WindowFilesDropped, func(e *application.WindowEvent) {
 		ctx := e.Context()
 		target := ctx.DropTargetDetails()
@@ -131,10 +149,6 @@ func main() {
 		}
 		a.Event.Emit(eventError, "Only WAV files can be assigned to a pad.")
 	})
-
-	if err := a.Run(); err != nil {
-		log.Fatal(err)
-	}
 }
 
 // fitSize returns a 16:9 window size that takes screenShare of the work