Files
Reims-Go/frontend/src/lib/format.ts
T
NwflowerandClaude Opus 5.5 181ad64618 fix(display): mark HiDPI presets by reims-vgpu's own rule
Presets were flagged HiDPI by screen width alone, so a 2880x1800 preset
read "HiDPI" on a 100% screen where macOS runs it at 1x, and a VM left at
the 1920x1080 default ran 1x on a 200% screen.

- GuestStartsHiDPI follows upstream reims-vgpu 3731afd
  (RgfxDisplayIdentity.hpp, reims-vgpu-window features.md): HiDPI from a
  2048x1536 window above 160 ppi (150% scaling or more), or from
  3840x2160 at any scale.
- Presets are the 16:9 or 16:10 sizes that fit the screen's work area
  below a title bar, largest four; the largest HiDPI one is recommended.
  The display probe now reports the work area.
- When no preset is HiDPI, the resolution row says how to get there:
  borderless full screen, 150% Windows scaling, or not on this screen.

On this host (3072x1920 at 200%) the presets are 2560x1600 (HiDPI,
recommended), 1920x1200, 1680x1050 and 1280x800.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 23:48:52 -07:00

211 lines
7.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { I18nKey, t } from '../i18n'
import { DisplayPreset, GPUInfo, HostReport, Locale, VMDetail } from '../types'
// Human-readable values for the main view (docs/ui-redesign.md §4.3).
// Raw values stay available under "Technical details".
const MEMORY_CHOICES = [4, 6, 8, 12, 16, 24, 32, 48, 64]
/**
* Largest offered memory size that fits in 60% of physical RAM and in the
* commit memory free now, minus 1 GB for QEMU and 2 GB for the host. Mirrors
* host.RecommendedMemoryGB (internal/host/memory.go).
*/
export function recommendedMemoryGB(host: HostReport): number {
let limit = host.total_ram_gb * 0.6
if (host.commit_avail_gb > 0) limit = Math.min(limit, host.commit_avail_gb - 3)
return MEMORY_CHOICES.reduce((best, m) => (m <= limit ? m : best), MEMORY_CHOICES[0])
}
/** Memory sizes offered as quick picks, in GB. */
export const MEMORY_MARKS = MEMORY_CHOICES
/**
* Largest memory size in GB that still fits the same limits
* recommendedMemoryGB applies; above it the settings slider turns amber.
*/
export function memorySafeLimitGB(host: HostReport): number {
let limit = host.total_ram_gb * 0.6
if (host.commit_avail_gb > 0) limit = Math.min(limit, host.commit_avail_gb - 3)
return Math.max(Math.floor(limit), recommendedMemoryGB(host))
}
/** vCPU count suggested for a new VM; same rule as the create wizard. */
export function recommendedCPUs(host: HostReport): number {
const rec = host.logical_cpus >= 12 ? 8 : 4
return host.logical_cpus > 0 ? Math.min(rec, host.logical_cpus) : rec
}
/** "16G" / "16384M" / "16GiB" → 16. Null when the shape is unknown. */
export function parseMemoryGB(raw: string | undefined): number | null {
const m = /^\s*(\d+(?:\.\d+)?)\s*([KMGT])?(?:i?B)?\s*$/i.exec(raw ?? '')
if (!m) return null
const n = Number(m[1])
const unit = (m[2] ?? 'M').toUpperCase()
return unit === 'T' ? n * 1024 : unit === 'G' ? n : unit === 'M' ? n / 1024 : n / 1024 / 1024
}
/** "16G" / "16384M" / "16GiB" → "16 GB". Unknown shapes pass through. */
export function formatMemory(raw: string): string {
const gb = parseMemoryGB(raw)
if (gb === null) return raw
const rounded = gb >= 10 ? Math.round(gb) : Math.round(gb * 10) / 10
return `${rounded} GB`
}
export function controllerLabel(raw: string): string {
switch ((raw ?? '').toLowerCase()) {
case 'ahci': case 'sata': return 'SATA'
case 'nvme': return 'NVMe'
case 'virtio': return 'VirtIO'
default: return raw ? raw.toUpperCase() : '—'
}
}
function findGPU(host: HostReport, icd: string): GPUInfo | undefined {
const norm = (s: string) => s.replace(/\//g, '\\').toLowerCase()
return (host.gpus ?? []).find((g) => norm(g.vulkan_icd) === norm(icd))
}
/** Drop vendor boilerplate: "NVIDIA GeForce RTX 4070 Ti" → "RTX 4070 Ti". */
export function shortGPUName(name: string): string {
return name
.replace(/^NVIDIA\s+(GeForce\s+)?/i, '')
.replace(/^AMD\s+(Radeon\s+)?(?=RX|Pro)/i, 'Radeon ')
.replace(/^Intel\(R\)\s+/i, 'Intel ')
.trim()
}
/** "1920x1080" → "1080p"; non-16:9 sizes keep "W×H". */
export function resolutionLabel(raw: string): string {
const m = /^(\d+)\s*[x×]\s*(\d+)$/i.exec(raw ?? '')
if (!m) return raw
const w = Number(m[1]), h = Number(m[2])
return Math.abs(w / h - 16 / 9) < 0.01 ? `${h}p` : `${w}×${h}`
}
/** A `gpu` value ("auto" or a Vulkan ICD path) as the card's short name, e.g. "RTX 4070 Ti". */
export function gpuLabel(locale: Locale, icd: string | undefined, host: HostReport): string {
if (!icd || icd === 'auto') {
return host.recommended_gpu ? shortGPUName(host.recommended_gpu.name) : t(locale, 'gpuAuto')
}
const found = findGPU(host, icd)
return found ? shortGPUName(found.name) : t(locale, 'gpuAuto')
}
export function graphicsLabel(locale: Locale, d: VMDetail, host: HostReport): string {
if (d.graphics && d.graphics !== 'reims-vgpu') return t(locale, 'softwareGraphics')
const gpu = gpuLabel(locale, d.gpu, host)
const res = resolutionLabel(d.resolution)
const screens = d.displays > 1 ? `${d.displays} × ${res}` : res
return res ? `${gpu} · ${screens}` : gpu
}
export function sshCommand(port: number): string {
return `ssh -p ${port} 127.0.0.1`
}
export function formatBytes(bytes: number): string {
if (!bytes || bytes <= 0) return '0 B'
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let v = bytes
let i = 0
while (v >= 1024 && i < units.length - 1) {
v /= 1024
i++
}
const digits = v >= 100 || i === 0 ? 0 : v >= 10 ? 1 : 2
return `${v.toFixed(digits)} ${units[i]}`
}
export function formatUptime(sec: number, locale: Locale): string {
if (!sec || sec <= 0) return '—'
const h = Math.floor(sec / 3600)
const m = Math.floor((sec % 3600) / 60)
const s = sec % 60
if (locale === 'zh-CN') {
if (h > 0) return `${h}小时 ${m}分`
if (m > 0) return `${m}分 ${s}秒`
return `${s}秒`
}
if (h > 0) return `${h}h ${m}m`
if (m > 0) return `${m}m ${s}s`
return `${s}s`
}
// Used only when the host report has no presets: with nothing known about the
// screen, no size can be called HiDPI (internal/host/display.go).
const DEFAULT_DISPLAY_PRESETS: DisplayPreset[] = [
{ resolution: '1920x1080', label: '1920 × 1080', hidpi: false, recommended: true },
{ resolution: '2560x1440', label: '2560 × 1440', hidpi: false },
{ resolution: '2880x1800', label: '2880 × 1800', hidpi: false },
]
// The resolution row's description: the main display, the recommended size
// and, when no size gives macOS a HiDPI desktop, how to get one.
export function hostDisplayDesc(host: HostReport, tr: (k: I18nKey, v?: Record<string, string | number>) => string): string | undefined {
const d = host.primary_display
if (!d) return undefined
const base = tr('hostDisplayHint', {
w: d.width,
h: d.height,
scale: d.scale_pct || 100,
rec: host.recommended_resolution || '1920x1080',
})
const tip: Record<string, I18nKey> = {
fullscreen: 'hidpiTipFullscreen',
scale: 'hidpiTipScale',
unavailable: 'hidpiTipUnavailable',
}
const key = host.hidpi_advice ? tip[host.hidpi_advice] : undefined
return key ? `${base} · ${tr(key)}` : base
}
export function recommendedDisplayPresets(host: HostReport): DisplayPreset[] {
if (host.display_presets && host.display_presets.length > 0) {
return host.display_presets
}
return DEFAULT_DISPLAY_PRESETS
}
export function recommendedResolution(host: HostReport): string {
if (host.recommended_resolution) return host.recommended_resolution
const rec = recommendedDisplayPresets(host).find((p) => p.recommended)
return rec?.resolution ?? '1920x1080'
}
export interface GuestLangPreset {
value: string
zh: string
en: string
}
export const GUEST_LANG_PRESETS: GuestLangPreset[] = [
{ value: 'zh-Hans:0', zh: '简体中文(美式键盘,推荐)', en: 'Simplified Chinese (US Keyboard)' },
{ value: 'zh-Hans:252', zh: '简体中文(拼音键盘)', en: 'Simplified Chinese (Pinyin)' },
{ value: 'zh-Hant:0', zh: '繁體中文(美式键盘)', en: 'Traditional Chinese (US Keyboard)' },
{ value: 'en-US:0', zh: 'English (US)', en: 'English (US)' },
{ value: 'ja-JP:0', zh: '日本語(美式键盘)', en: 'Japanese (US Keyboard)' },
{ value: 'ko-KR:0', zh: '한국어(美式键盘)', en: 'Korean (US Keyboard)' },
]
export function defaultGuestLang(locale: Locale): string {
return locale === 'en-US' ? 'en-US:0' : 'zh-Hans:0'
}
export function guestLangLabel(locale: Locale, code: string | undefined): string {
if (!code) return defaultGuestLang(locale)
const hit = GUEST_LANG_PRESETS.find((p) => p.value === code)
if (hit) return locale === 'zh-CN' ? hit.zh : hit.en
return code
}
/**
* Split "简体中文(美式键盘,推荐)" / "Simplified Chinese (US Keyboard)" into a
* pop-up label and its grey detail, so the menu reads like macOS.
*/
export function splitLangLabel(value: string, text: string): { value: string; label: string; detail?: string } {
const m = /^(.*?)\s*[((](.*)[))]\s*$/.exec(text)
return m ? { value, label: m[1], detail: m[2] } : { value, label: text }
}