* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
407 lines
11 KiB
Go
407 lines
11 KiB
Go
package tray
|
||
|
||
import (
|
||
"bytes"
|
||
"image"
|
||
"image/draw"
|
||
"image/png"
|
||
|
||
"github.com/Nwflower/reims-go/internal/i18n"
|
||
)
|
||
|
||
// VMItem is a lightweight summary shown inside the system tray menu.
|
||
type VMItem struct {
|
||
DriverID string // owning guest driver, passed back to callbacks with Name
|
||
Name string // VM name within DriverID, passed back to callbacks
|
||
Label string // display name (the VM's note); empty shows Name
|
||
State string // running | stopped | starting | stopping | broken
|
||
}
|
||
|
||
func (v VMItem) title() string {
|
||
if v.Label != "" {
|
||
return v.Label
|
||
}
|
||
return v.Name
|
||
}
|
||
|
||
// Snapshot holds the live state read each time the user opens the tray menu.
|
||
type Snapshot struct {
|
||
Locale i18n.Locale
|
||
CanSuspend bool
|
||
VMs []VMItem
|
||
}
|
||
|
||
// Action is a per-VM command picked from the tray menu.
|
||
type Action int
|
||
|
||
const (
|
||
ActStart Action = iota + 1
|
||
ActStop
|
||
ActForceStop
|
||
ActRestart
|
||
ActShowWindow
|
||
ActScreenshot
|
||
ActSharedFolder
|
||
ActOpenInApp
|
||
ActToggleFullscreen
|
||
ActSuspend
|
||
ActResume
|
||
ActDiscardSuspend
|
||
)
|
||
|
||
// Callbacks wires tray actions back into the Wails application. Each runs
|
||
// on its own goroutine, never on the tray's window thread.
|
||
type Callbacks struct {
|
||
OnShow func()
|
||
OnNewVM func()
|
||
OnVMAction func(driverID, name string, act Action)
|
||
OnQuit func()
|
||
GetSnapshot func() Snapshot
|
||
}
|
||
|
||
// Menu command IDs. A VM's commands are cmdVMBase + index*vmSlot + Action.
|
||
const (
|
||
cmdShow = 1001
|
||
cmdQuit = 1002
|
||
cmdNewVM = 1003
|
||
cmdVMBase = 2000
|
||
vmSlot = 20
|
||
maxTrayVMs = 16
|
||
)
|
||
|
||
// dotKind is the coloured status mark drawn beside a VM, as in the sidebar.
|
||
type dotKind int
|
||
|
||
const (
|
||
dotNone dotKind = iota
|
||
dotRunning
|
||
dotStopped
|
||
dotSuspended
|
||
dotBusy
|
||
dotBroken
|
||
)
|
||
|
||
// menuEntry is one row of the tray menu, built by buildMenu and drawn by the
|
||
// platform code, so the structure is testable without Win32.
|
||
type menuEntry struct {
|
||
ID int
|
||
Label string // "\t" right-aligns what follows, like a shortcut
|
||
Default bool
|
||
Disabled bool
|
||
Separator bool
|
||
Dot dotKind
|
||
Sub []menuEntry
|
||
}
|
||
|
||
type texts struct {
|
||
show, newVM, quit string
|
||
start, stop, forceStop, restart, showWindow, shot string
|
||
suspend, resume, discardSuspend string
|
||
shared, openInApp string
|
||
running, stopped, suspended string
|
||
starting, stopping, suspending, resuming string
|
||
broken, unknown string
|
||
|
||
toggleFullscreen string
|
||
|
||
resumeUnavailable string
|
||
}
|
||
|
||
func menuTexts(snap Snapshot) texts {
|
||
loc := snap.Locale
|
||
if loc == "" {
|
||
loc = i18n.EnUS
|
||
}
|
||
return texts{
|
||
show: i18n.T(loc, "tray.menu.show"),
|
||
newVM: i18n.T(loc, "tray.menu.new_vm"),
|
||
quit: i18n.T(loc, "tray.menu.quit"),
|
||
start: i18n.T(loc, "tray.menu.start"),
|
||
stop: i18n.T(loc, "tray.menu.stop"),
|
||
forceStop: i18n.T(loc, "tray.menu.force_stop"),
|
||
restart: i18n.T(loc, "tray.menu.restart"),
|
||
suspend: i18n.T(loc, "tray.menu.suspend"),
|
||
resume: i18n.T(loc, "tray.menu.resume"),
|
||
discardSuspend: i18n.T(loc, "tray.menu.discard_suspend"),
|
||
showWindow: i18n.T(loc, "tray.menu.show_window"),
|
||
shot: i18n.T(loc, "tray.menu.screenshot"),
|
||
shared: i18n.T(loc, "tray.menu.shared_folder"),
|
||
openInApp: i18n.T(loc, "tray.menu.open_in_app"),
|
||
running: i18n.T(loc, "tray.state.running"),
|
||
stopped: i18n.T(loc, "tray.state.stopped"),
|
||
suspended: i18n.T(loc, "tray.state.suspended"),
|
||
starting: i18n.T(loc, "tray.state.starting"),
|
||
stopping: i18n.T(loc, "tray.state.stopping"),
|
||
suspending: i18n.T(loc, "tray.state.suspending"),
|
||
resuming: i18n.T(loc, "tray.state.resuming"),
|
||
broken: i18n.T(loc, "tray.state.broken"),
|
||
unknown: i18n.T(loc, "tray.state.unknown"),
|
||
|
||
toggleFullscreen: i18n.T(loc, "tray.menu.toggle_fullscreen"),
|
||
|
||
resumeUnavailable: i18n.T(loc, "tray.menu.resume_unavailable"),
|
||
}
|
||
}
|
||
|
||
func vmCmd(i int, act Action) int { return cmdVMBase + i*vmSlot + int(act) }
|
||
|
||
// decodeVMCmd splits a VM command ID into the VM's index and the action.
|
||
func decodeVMCmd(id int) (int, Action, bool) {
|
||
if id < cmdVMBase || id >= cmdVMBase+maxTrayVMs*vmSlot {
|
||
return 0, 0, false
|
||
}
|
||
off := id - cmdVMBase
|
||
act := Action(off % vmSlot)
|
||
if act < ActStart || act > ActDiscardSuspend {
|
||
return 0, 0, false
|
||
}
|
||
return off / vmSlot, act, true
|
||
}
|
||
|
||
// buildMenu lays out the tray menu for a snapshot: Show (default), New VM,
|
||
// one submenu per VM whose default item is the state's main action (Start,
|
||
// or Show Window while running), then Quit.
|
||
func buildMenu(snap Snapshot) []menuEntry {
|
||
t := menuTexts(snap)
|
||
menu := []menuEntry{
|
||
{ID: cmdShow, Label: t.show, Default: true},
|
||
{ID: cmdNewVM, Label: t.newVM},
|
||
}
|
||
vms := snap.VMs
|
||
if len(vms) > maxTrayVMs {
|
||
vms = vms[:maxTrayVMs]
|
||
}
|
||
if len(vms) > 0 {
|
||
menu = append(menu, menuEntry{Separator: true})
|
||
}
|
||
sep := menuEntry{Separator: true}
|
||
for i, vm := range vms {
|
||
cmd := func(act Action, label string) menuEntry { return menuEntry{ID: vmCmd(i, act), Label: label} }
|
||
var sub []menuEntry
|
||
var dot dotKind
|
||
var state string
|
||
switch vm.State {
|
||
case "running":
|
||
dot, state = dotRunning, t.running
|
||
show := cmd(ActShowWindow, t.showWindow)
|
||
show.Default = true
|
||
sub = []menuEntry{
|
||
show, cmd(ActToggleFullscreen, t.toggleFullscreen),
|
||
cmd(ActScreenshot, t.shot), cmd(ActSharedFolder, t.shared), sep,
|
||
}
|
||
if snap.CanSuspend {
|
||
sub = append(sub, cmd(ActSuspend, t.suspend))
|
||
}
|
||
sub = append(sub,
|
||
cmd(ActRestart, t.restart), cmd(ActStop, t.stop), cmd(ActForceStop, t.forceStop), sep,
|
||
cmd(ActOpenInApp, t.openInApp),
|
||
)
|
||
case "suspended":
|
||
dot, state = dotSuspended, t.suspended
|
||
resume := cmd(ActResume, t.resume)
|
||
resume.Default = snap.CanSuspend
|
||
if !snap.CanSuspend {
|
||
resume.Label = t.resumeUnavailable
|
||
resume.Disabled = true
|
||
}
|
||
sub = []menuEntry{
|
||
resume, cmd(ActDiscardSuspend, t.discardSuspend), sep,
|
||
cmd(ActSharedFolder, t.shared), sep,
|
||
cmd(ActOpenInApp, t.openInApp),
|
||
}
|
||
case "starting", "stopping", "suspending", "resuming":
|
||
dot = dotBusy
|
||
switch vm.State {
|
||
case "stopping":
|
||
state = t.stopping
|
||
case "suspending":
|
||
state = t.suspending
|
||
case "resuming":
|
||
state = t.resuming
|
||
default:
|
||
state = t.starting
|
||
}
|
||
sub = []menuEntry{cmd(ActForceStop, t.forceStop), sep, cmd(ActOpenInApp, t.openInApp)}
|
||
case "broken":
|
||
dot, state = dotBroken, t.broken
|
||
sub = []menuEntry{cmd(ActOpenInApp, t.openInApp)}
|
||
case "stopped":
|
||
dot, state = dotStopped, t.stopped
|
||
start := cmd(ActStart, t.start)
|
||
start.Default = true
|
||
sub = []menuEntry{start, cmd(ActSharedFolder, t.shared), sep, cmd(ActOpenInApp, t.openInApp)}
|
||
default:
|
||
dot, state = dotStopped, t.unknown
|
||
sub = []menuEntry{cmd(ActOpenInApp, t.openInApp)}
|
||
}
|
||
menu = append(menu, menuEntry{Label: vm.title() + "\t" + state, Dot: dot, Sub: sub})
|
||
}
|
||
return append(menu, menuEntry{Separator: true}, menuEntry{ID: cmdQuit, Label: t.quit})
|
||
}
|
||
|
||
// dotPixels draws a status dot as size×size premultiplied BGRA pixels (the
|
||
// layout a 32-bit menu bitmap wants): a filled, anti-aliased circle, or a
|
||
// ring for "stopped" so the state differs by shape as well as colour.
|
||
func dotPixels(size int, kind dotKind) []byte {
|
||
px := make([]byte, size*size*4)
|
||
var r, g, b uint8
|
||
switch kind {
|
||
case dotRunning:
|
||
r, g, b = 0x30, 0xD1, 0x58
|
||
case dotSuspended:
|
||
r, g, b = 0xFF, 0x9F, 0x0A
|
||
case dotBusy:
|
||
r, g, b = 0xFF, 0x9F, 0x0A
|
||
case dotBroken:
|
||
r, g, b = 0xFF, 0x45, 0x3A
|
||
case dotStopped:
|
||
r, g, b = 0x8E, 0x8E, 0x93
|
||
default:
|
||
return px
|
||
}
|
||
c := float64(size) / 2
|
||
const ss = 4 // 4×4 supersampling for smooth edges
|
||
if kind == dotSuspended {
|
||
halfH := float64(size) * 0.28
|
||
barW := maxf(1.8, float64(size)*0.15)
|
||
gap := maxf(1.2, float64(size)*0.12)
|
||
for y := 0; y < size; y++ {
|
||
for x := 0; x < size; x++ {
|
||
hits := 0
|
||
for sy := 0; sy < ss; sy++ {
|
||
for sx := 0; sx < ss; sx++ {
|
||
dx := float64(x) + (float64(sx)+0.5)/ss - c
|
||
dy := float64(y) + (float64(sy)+0.5)/ss - c
|
||
adx := dx
|
||
if adx < 0 {
|
||
adx = -adx
|
||
}
|
||
ady := dy
|
||
if ady < 0 {
|
||
ady = -ady
|
||
}
|
||
if ady <= halfH && adx >= gap/2 && adx <= gap/2+barW {
|
||
hits++
|
||
}
|
||
}
|
||
}
|
||
if hits == 0 {
|
||
continue
|
||
}
|
||
a := hits * 255 / (ss * ss)
|
||
off := (y*size + x) * 4
|
||
px[off+0] = uint8(int(b) * a / 255)
|
||
px[off+1] = uint8(int(g) * a / 255)
|
||
px[off+2] = uint8(int(r) * a / 255)
|
||
px[off+3] = uint8(a)
|
||
}
|
||
}
|
||
return px
|
||
}
|
||
outer := float64(size) * 0.28 // dot diameter ≈ 56% of the check-mark cell
|
||
inner := 0.0
|
||
if kind == dotStopped {
|
||
inner = outer - maxf(1.4, float64(size)*0.09)
|
||
}
|
||
for y := 0; y < size; y++ {
|
||
for x := 0; x < size; x++ {
|
||
hits := 0
|
||
for sy := 0; sy < ss; sy++ {
|
||
for sx := 0; sx < ss; sx++ {
|
||
dx := float64(x) + (float64(sx)+0.5)/ss - c
|
||
dy := float64(y) + (float64(sy)+0.5)/ss - c
|
||
d2 := dx*dx + dy*dy
|
||
if d2 <= outer*outer && d2 >= inner*inner {
|
||
hits++
|
||
}
|
||
}
|
||
}
|
||
if hits == 0 {
|
||
continue
|
||
}
|
||
a := hits * 255 / (ss * ss)
|
||
off := (y*size + x) * 4
|
||
px[off+0] = uint8(int(b) * a / 255)
|
||
px[off+1] = uint8(int(g) * a / 255)
|
||
px[off+2] = uint8(int(r) * a / 255)
|
||
px[off+3] = uint8(a)
|
||
}
|
||
}
|
||
return px
|
||
}
|
||
|
||
func maxf(a, b float64) float64 {
|
||
if a > b {
|
||
return a
|
||
}
|
||
return b
|
||
}
|
||
|
||
// resizePNGIcon scales an embedded PNG (e.g. assets/logo.png) to size×size PNG
|
||
// bytes suitable for Windows CreateIconFromResourceEx.
|
||
func resizePNGIcon(srcPNG []byte, size int) ([]byte, error) {
|
||
if len(srcPNG) == 0 || size <= 0 {
|
||
return nil, nil
|
||
}
|
||
img, err := png.Decode(bytes.NewReader(srcPNG))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
sb := img.Bounds()
|
||
sw, sh := sb.Dx(), sb.Dy()
|
||
if sw == size && sh == size {
|
||
return srcPNG, nil
|
||
}
|
||
src := image.NewNRGBA(image.Rect(0, 0, sw, sh))
|
||
draw.Draw(src, src.Bounds(), img, sb.Min, draw.Src)
|
||
|
||
dst := image.NewNRGBA(image.Rect(0, 0, size, size))
|
||
// Box-filter downscale for crisp tray icon edges without external dependencies.
|
||
for y := 0; y < size; y++ {
|
||
y0 := y * sh / size
|
||
y1 := (y + 1) * sh / size
|
||
if y1 <= y0 {
|
||
y1 = y0 + 1
|
||
}
|
||
if y1 > sh {
|
||
y1 = sh
|
||
}
|
||
for x := 0; x < size; x++ {
|
||
x0 := x * sw / size
|
||
x1 := (x + 1) * sw / size
|
||
if x1 <= x0 {
|
||
x1 = x0 + 1
|
||
}
|
||
if x1 > sw {
|
||
x1 = sw
|
||
}
|
||
var rSum, gSum, bSum, aSum, count uint64
|
||
for sy := y0; sy < y1; sy++ {
|
||
row := src.Pix[sy*src.Stride:]
|
||
for sx := x0; sx < x1; sx++ {
|
||
off := sx * 4
|
||
a := uint64(row[off+3])
|
||
rSum += uint64(row[off+0]) * a
|
||
gSum += uint64(row[off+1]) * a
|
||
bSum += uint64(row[off+2]) * a
|
||
aSum += a
|
||
count++
|
||
}
|
||
}
|
||
doff := y*dst.Stride + x*4
|
||
if aSum > 0 && count > 0 {
|
||
dst.Pix[doff+0] = uint8(rSum / aSum)
|
||
dst.Pix[doff+1] = uint8(gSum / aSum)
|
||
dst.Pix[doff+2] = uint8(bSum / aSum)
|
||
dst.Pix[doff+3] = uint8(aSum / count)
|
||
}
|
||
}
|
||
}
|
||
|
||
var buf bytes.Buffer
|
||
if err := png.Encode(&buf, dst); err != nil {
|
||
return nil, err
|
||
}
|
||
return buf.Bytes(), nil
|
||
}
|