- Build single-source-of-truth 7-domain locale catalog (`locales/{zh-CN,en-US}/*.json`, 1,100+ keys) and `cmd/i18ngen` generator emitting Go catalogs and eager/lazy-split TypeScript domain modules (`core`/`vm` eager; `wizard`/`settings`/`plugins` lazy)
- Add zero-dependency ICU MessageFormat subset (`{var}`, `{n, number}`, CLDR `plural`, `select`), 8-hop cycle-free regional fallback (`zh-HK/zh-MO -> zh-Hant -> zh-TW -> zh-CN`), `auto` OS display-language preference, and `en-XA` pseudo-localization across Go and TypeScript
- Introduce `CodedError` supportability with `[E_CODE]` badges, collapsible `[detail]` diagnostics, and automatic SMC OSK / SMBIOS serial / MAC / token redaction
- Add Host × Guest macOS dual-terminology annotation (`FormatGuestTerm`) across 6 official Apple guest UI locales and localize Win32 system tray, file pickers, and DPI-adaptive `installguide` overlays with CJK font fallback probing
- Upgrade L1–L4 plugin localization to dual-track v2: Track A full language packs (`files`, WOFF2 `fonts`, cross-plugin `pluginOverrides`), Track B plugin-scoped `l10n` bundles (`HasUIMessages` isolation), live `plugin/localeChanged` notifications, `< 2ms` static CLI pack loading, and `reims-go plugin locale init|check` translator toolchain with `zh-TW` and `ja-JP` reference packs
- Harden security, WCAG 2.1 AA token contrast, `< 768px` responsive drawer & form layouts, and RTL logical properties; add backend security/locale regression tests and frontend `node --test` static/runtime guard suite in CI
# Conflicts:
# frontend/src/i18n.ts
# internal/cli/cli.go
# internal/i18n/i18n.go
323 lines
8.5 KiB
Go
323 lines
8.5 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 {
|
||
Name string // identifier 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
|
||
LocaleZh bool // legacy fallback when Locale is empty
|
||
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
|
||
)
|
||
|
||
// 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(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
|
||
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
|
||
shared, openInApp string
|
||
running, stopped, starting, stopping, broken, unknown string
|
||
}
|
||
|
||
func menuTexts(snap Snapshot) texts {
|
||
loc := snap.Locale
|
||
if loc == "" {
|
||
if snap.LocaleZh {
|
||
loc = i18n.ZhCN
|
||
} else {
|
||
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"),
|
||
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"),
|
||
starting: i18n.T(loc, "tray.state.starting"),
|
||
stopping: i18n.T(loc, "tray.state.stopping"),
|
||
broken: i18n.T(loc, "tray.state.broken"),
|
||
unknown: i18n.T(loc, "tray.state.unknown"),
|
||
}
|
||
}
|
||
|
||
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 > ActOpenInApp {
|
||
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(ActScreenshot, t.shot), cmd(ActSharedFolder, t.shared), sep,
|
||
cmd(ActRestart, t.restart), cmd(ActStop, t.stop), cmd(ActForceStop, t.forceStop), sep,
|
||
cmd(ActOpenInApp, t.openInApp),
|
||
}
|
||
case "starting", "stopping":
|
||
dot, state = dotBusy, t.starting
|
||
if vm.State == "stopping" {
|
||
state = t.stopping
|
||
}
|
||
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 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
|
||
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)
|
||
}
|
||
const ss = 4 // 4×4 supersampling for smooth edges
|
||
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
|
||
}
|