Files
Reims-Go/internal/app/install_guide.go
T
NwflowerandClaude Opus 5.5 165f26351d refactor(app): split the VM poll loop into steps
The poll loop was one 290-line function that did everything that follows the
VM list. Its steps are now separate and recording a listing has a test. The
install guide part moved next to the rest of the guide code. Running process
IDs and capabilities are read through one method each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:26:54 -07:00

225 lines
6.5 KiB
Go

package app
import (
"context"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/macos/guest"
"github.com/Nwflower/reims-go/internal/macos/installguide"
"github.com/Nwflower/reims-go/internal/macos/reims"
"github.com/Nwflower/reims-go/internal/macos/startprep"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// getInstallGuideManager returns the active VM-window install guide manager
// provided by builtin.guest.macos, or nil when disabled.
func (a *App) getInstallGuideManager() *installguide.Manager {
a.mu.Lock()
mgr := a.pluginMgr
a.mu.Unlock()
if mgr == nil {
return nil
}
raw, ok := mgr.Registry().LookupService(guest.ServiceInstallGuide)
if !ok {
return nil
}
gm, _ := raw.(*installguide.Manager)
return gm
}
// installGuideEnabled reports whether the VM-window installation guide service is active.
func (a *App) installGuideEnabled() bool {
return a.getInstallGuideManager() != nil
}
func (a *App) guidePreferences() (i18n.Locale, installguide.Theme) {
a.mu.Lock()
rawLoc := a.cfg.Locale
th := a.themeMode
a.mu.Unlock()
return i18n.EffectiveLocale(rawLoc), installguide.Theme(th)
}
func (a *App) bindGuideEvents(ctx context.Context) {
wailsRuntime.EventsOn(ctx, "reims:theme", func(data ...any) {
if len(data) != 1 {
return
}
raw, _ := data[0].(string)
theme := installguide.Theme(raw)
if theme == installguide.ThemeLight || theme == installguide.ThemeDark {
a.mu.Lock()
a.themeMode = string(theme)
a.mu.Unlock()
}
})
}
func (a *App) unfoldInstallGuide(name string, pid int) {
if gm := a.getInstallGuideManager(); gm != nil {
gm.Unfold(name, pid)
}
}
// A queued recovery picker ("want") applies at the next boot. The guide
// describes the running boot, including one already running when the app opens.
func installGuideKind(d reims.VMDetail) (installguide.Kind, bool) {
if d.HasInstaller {
if reims.IsOfflineInstallerPath(d.InstallerPath) {
return installguide.Offline, true
}
return installguide.Online, true
}
return installguide.BuiltIn, startprep.ReadRecoveryMarker(d.Directory) == "on"
}
// guideTracker follows the install guides of running VMs for the poll loop.
type guideTracker struct {
a *App
mgr *installguide.Manager // nil while the guide service is disabled
checked map[int]bool // PIDs already offered a guide by mgr
}
func (a *App) newGuideTracker() *guideTracker {
return &guideTracker{a: a, mgr: a.getInstallGuideManager(), checked: make(map[int]bool)}
}
// cancelStopped closes the guides of VMs that stopped and reports whether any
// did. A restarted guide service checks every running VM again.
func (t *guideTracker) cancelStopped(vms []driver.VMListItem) bool {
prunePinnedPIDs(t.checked, vms)
if mgr := t.a.getInstallGuideManager(); mgr != t.mgr {
clear(t.checked)
t.mgr = mgr
}
return t.mgr != nil && t.mgr.CancelStopped(runningPIDs(vms))
}
// startMissing opens the guide of running VMs that need one and reports
// whether any opened. details holds VM details already looked up, by VM key.
func (t *guideTracker) startMissing(ctx context.Context, vms []driver.VMListItem, details map[string]driver.VMInfo, loc i18n.Locale) bool {
if t.mgr == nil {
return false
}
started := false
for _, vm := range vms {
pid := vm.RunningPID()
// The install guide belongs to the macOS installer flow.
if pid == 0 || t.checked[pid] || !driver.SupportsCapability(vm.Capabilities, driver.CapOpenCore) {
continue
}
det, ok := details[vmKeyForDriver(vm.DriverID, vm.Name)]
if !ok {
var err error
if det, err = t.a.resolveVMInfo(vm.DriverID, vm.Name); err != nil {
continue
}
if det.Directory == "" {
det.Directory = vm.Dir
}
}
t.checked[pid] = true
if t.a.maybeStartInstallGuide(ctx, t.mgr, vm.Name, pid, det, loc) {
started = true
}
}
return started
}
func (a *App) maybeStartInstallGuide(ctx context.Context, gm *installguide.Manager, name string, pid int, vm driver.VMInfo, loc i18n.Locale) bool {
if gm == nil || pid <= 0 || gm.HasActive(name, pid) {
return false
}
d, err := macOSDetailFromVMInfo(vm)
if err != nil {
return false
}
kind, recovery := installGuideKind(d)
g := installguide.Guide{
Name: name,
PID: uint32(pid),
Locale: loc,
GuestLang: d.GuestLang,
Kind: kind,
ToolsAttached: reims.HasGuestToolsArgs(d.ExtraArgs),
}
if !g.Resume(d.Directory, d.InstallerPath, recovery) {
return false
}
return gm.Start(ctx, a.buildInstallGuide(ctx, g, d))
}
func (a *App) buildInstallGuide(ctx context.Context, g installguide.Guide, d reims.VMDetail) installguide.Guide {
name, pid := g.Name, int(g.PID)
g.CurrentPreferences = a.guidePreferences
if d.Graphics == "vmware-svga" {
g.ReleaseKeys = "Ctrl+Alt+G"
} else if d.Capture {
g.ReleaseKeys = "Ctrl+Alt+Win"
}
// The panel may have been open across a shutdown. Re-list instead of
// using the poll cache before acting on a VM addressed by name.
checkBoot := func() error {
vms, err := a.listVMsInternal(ctx)
if err != nil {
return err
}
for _, vm := range vms {
if vm.Name == name && vm.RunningPID() == pid {
return ctx.Err()
}
}
loc, _ := a.guidePreferences()
return i18n.NewCodedError(loc, "E_GUIDE_INSTANCE_CHANGED", "guide.err.instance_changed", i18n.Vars{"name": name}, nil)
}
g.TypeText = func(text string) error {
if err := checkBoot(); err != nil {
return err
}
_, err := a.TypeText("", name, text)
return err
}
g.FinishRecovery = func() error {
if err := checkBoot(); err != nil {
return err
}
return a.FinishInstall(name)
}
g.AttachImage = func(path string) error {
if err := checkBoot(); err != nil {
return err
}
_, err := a.AttachInstaller(name, path)
return err
}
g.LogError = func(err error) { a.emitVMLog(name, "Installation guide: "+err.Error()) }
g.AttachTools = func() error {
if err := checkBoot(); err != nil {
return err
}
return a.AttachGuestTools(name)
}
g.Restart = func() error {
if err := checkBoot(); err != nil {
return err
}
stopped, err := a.restartMacOSVM(a.baseCtx(), name)
if stopped && err != nil {
// The guest window has closed, so report start failures via the tray.
loc, _ := a.guidePreferences()
title := i18n.M(loc, "guide.err.restart_failed", i18n.Vars{"name": name})
a.emitVMLog(name, title+": "+err.Error())
a.mu.Lock()
tm := a.trayMgr
a.mu.Unlock()
if tm != nil {
tm.Notify(title, err.Error(), true)
}
}
return err
}
return g
}