172 lines
4.8 KiB
Go
172 lines
4.8 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"
|
|
}
|
|
|
|
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.State == "running" && vm.PID != nil && *vm.PID == 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(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
|
|
}
|