147 lines
3.1 KiB
Go
147 lines
3.1 KiB
Go
package installguide
|
|
|
|
import (
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
var getDoubleClickTime = user32.NewProc("GetDoubleClickTime")
|
|
|
|
type session struct {
|
|
current, unfolded, returnTo, beforeConfirm page
|
|
imagePath, failure string
|
|
confirmAfter time.Time
|
|
}
|
|
|
|
func (s session) detail() string {
|
|
if s.current == confirmImage {
|
|
return filepath.Base(s.imagePath)
|
|
}
|
|
return s.failure
|
|
}
|
|
|
|
func (s session) target(g Guide, id uintptr) page {
|
|
return s.targetAt(g, id, time.Now())
|
|
}
|
|
|
|
func (s session) targetAt(g Guide, id uintptr, now time.Time) page {
|
|
p := s.current
|
|
if p == compact {
|
|
if id == idFold {
|
|
return s.unfolded
|
|
}
|
|
return p
|
|
}
|
|
if p == failed {
|
|
if id == idLeft || id == 2 {
|
|
return s.returnTo
|
|
}
|
|
return p
|
|
}
|
|
c := g.content(p, "")
|
|
if (p == confirm || p == confirmRepair) && s.beforeConfirm != "" {
|
|
c.prev = s.beforeConfirm
|
|
}
|
|
var next page
|
|
switch id {
|
|
case idFold:
|
|
if c.fold != "" {
|
|
return compact
|
|
}
|
|
case 2: // IDCANCEL (Escape)
|
|
if c.fold != "" {
|
|
return compact
|
|
}
|
|
fallthrough
|
|
case idLeft:
|
|
next = c.prev
|
|
case idRight:
|
|
if !c.isConfirm || !now.Before(s.confirmAfter) {
|
|
next = c.next
|
|
}
|
|
case idExtra:
|
|
next = c.alt
|
|
case idJump:
|
|
if c.jump != "" {
|
|
return confirm
|
|
}
|
|
}
|
|
if next != "" {
|
|
return next
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (s *session) enter(next page) {
|
|
s.enterAt(next, time.Now())
|
|
}
|
|
|
|
func (s *session) enterAt(next page, now time.Time) {
|
|
if next == compact {
|
|
s.unfolded = s.current
|
|
}
|
|
if (next == confirm || next == confirmRepair) && s.current != failed && s.current != compact {
|
|
s.beforeConfirm = s.current
|
|
}
|
|
if next.confirmsRestart() {
|
|
// A double-click on the previous card can land on the new primary button.
|
|
ms, _, _ := getDoubleClickTime.Call()
|
|
s.confirmAfter = now.Add(time.Duration(ms) * time.Millisecond)
|
|
}
|
|
s.current = next
|
|
}
|
|
|
|
type outcome struct {
|
|
// On failure, next is the page to return to. Empty on success closes the panel.
|
|
next page
|
|
err error
|
|
}
|
|
|
|
// Called off the window thread. Save the handoff before shutting down QEMU:
|
|
// the previous window may disappear before Restart returns.
|
|
func (g Guide) perform(action, from page, path string) outcome {
|
|
var handoff page
|
|
switch action {
|
|
case typingCommand:
|
|
if err := g.TypeText(OfflineCommand); err != nil {
|
|
return outcome{from, err}
|
|
}
|
|
return outcome{next: verifyCommand}
|
|
case typingTools:
|
|
if err := g.TypeText(ToolsCommand); err != nil {
|
|
return outcome{from, err}
|
|
}
|
|
return outcome{next: toolsVerify}
|
|
case mounting:
|
|
if err := g.AttachImage(path); err != nil {
|
|
return outcome{mediaChoice, err}
|
|
}
|
|
case mountTools:
|
|
if err := g.AttachTools(); err != nil {
|
|
return outcome{desktop, err}
|
|
}
|
|
handoff = toolsTerminal
|
|
case working:
|
|
if from != restartPending {
|
|
if err := g.FinishRecovery(); err != nil {
|
|
return outcome{from, err}
|
|
}
|
|
handoff = setup
|
|
if from == confirmRepair {
|
|
handoff = restored
|
|
}
|
|
}
|
|
default:
|
|
panic("unexpected guide operation")
|
|
}
|
|
if handoff != "" {
|
|
if err := g.save(handoff, g.PID); err != nil {
|
|
return outcome{from, err}
|
|
}
|
|
}
|
|
if err := g.Restart(); err != nil {
|
|
return outcome{restartPending, err}
|
|
}
|
|
return outcome{}
|
|
}
|