Files
Reims-Go/internal/app/install_guide.go
T
Nwflower 8969c53644 Merge pull request #3 from yxc0915/feat/vm-window-install-guide
Integrate the VM-window macOS installation and recovery guide from #3
as an optional built-in plugin (builtin.install-guide):
- Register builtin.install-guide providing ui.install-guide with
  zero-residue lifecycle disposal and live toggle support
- Keep VMPage's in-page recovery guide as automatic fallback when
  the plugin is disabled
- Preserve VM-window guide UX, DPI layout, and restart resume from #3
2026-11-29 10:39:02 -07:00

170 lines
4.6 KiB
Go

package app
import (
"context"
"fmt"
"github.com/Nwflower/reims-go/internal/driver/reims"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/installguide"
"github.com/Nwflower/reims-go/internal/plugin/builtin"
"github.com/Nwflower/reims-go/internal/vmstart"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// getInstallGuideManager returns the active builtin.install-guide manager when
// the plugin is enabled, or nil when disabled or unavailable.
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(builtin.ServiceInstallGuide)
if !ok {
return nil
}
gm, _ := raw.(*installguide.Manager)
return gm
}
// installGuideEnabled reports whether the VM-window installation guide builtin
// plugin is currently active.
func (a *App) installGuideEnabled() bool {
return a.getInstallGuideManager() != nil
}
func (a *App) guidePreferences() (i18n.Locale, installguide.Theme) {
a.mu.Lock()
defer a.mu.Unlock()
return a.cfg.Locale, a.themeMode
}
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 = 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, vmstart.ReadRecoveryMarker(d.Directory) == "on"
}
func (a *App) maybeStartInstallGuide(ctx context.Context, gm *installguide.Manager, name string, pid int, d reims.VMDetail, loc i18n.Locale) bool {
if gm == nil || pid <= 0 || gm.HasActive(name, pid) {
return false
}
kind, recovery := installGuideKind(d)
g := installguide.Guide{
Name: name,
PID: uint32(pid),
Locale: loc,
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.State == "running" && vm.PID != nil && *vm.PID == pid {
return ctx.Err()
}
}
if loc, _ := a.guidePreferences(); loc == i18n.ZhCN {
return fmt.Errorf("虚拟机 %s 的运行状态已改变", name)
}
return fmt.Errorf("the running instance of %s has changed", name)
}
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.emitLog(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.restartVM(name)
if stopped && err != nil {
// The guest window has closed, so report start failures via the tray.
title := "Could not restart VM: " + name
if loc, _ := a.guidePreferences(); loc == i18n.ZhCN {
title = "未能重启虚拟机:" + name
}
a.emitLog(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
}