Files
Reims-Go/frontend/src/lib/displayResolution.ts
T

326 lines
11 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 {
canKeepDisplayPreset,
FullscreenDisplayPreset,
fullscreenDisplayPresets,
recommendedResolution,
resolveAutoDisplayPreset,
windowDisplayPresets,
} from './format'
import { CreateOpts, DisplayInfo, HostReport, SetOpts } from '../types'
export const AUTO_RESOLUTION_OPTION = '__auto'
export const CUSTOM_RESOLUTION_OPTION = '__custom'
const CANONICAL_RESOLUTION_RE = /^\d{3,5}x\d{3,5}$/
export function canonicalResolution(raw: string | undefined): string {
const trimmed = (raw ?? '').trim()
const m = /^(\d+)\s*[x×X]\s*(\d+)$/.exec(trimmed)
return m ? `${m[1]}x${m[2]}` : trimmed
}
export const normalizeResolution = (raw: string | undefined) => canonicalResolution(raw).toLowerCase()
export function isValidResolution(raw: string | undefined): boolean {
return CANONICAL_RESOLUTION_RE.test(canonicalResolution(raw))
}
interface ResolutionOptionItem {
value: string
kind: 'auto' | 'preset' | 'custom'
preset?: FullscreenDisplayPreset
}
export function buildResolutionOptions(params: {
graphics?: string
autoPreset: FullscreenDisplayPreset
activePresets: FullscreenDisplayPreset[]
includeCustom?: boolean
}): ResolutionOptionItem[] {
const items: ResolutionOptionItem[] = []
if (supportsAutoResolution(params.graphics)) {
items.push({ value: AUTO_RESOLUTION_OPTION, kind: 'auto', preset: params.autoPreset })
}
for (const preset of params.activePresets) {
items.push({ value: preset.resolution, kind: 'preset', preset })
}
if (params.includeCustom) {
items.push({ value: CUSTOM_RESOLUTION_OPTION, kind: 'custom' })
}
return items
}
export function supportsAutoResolution(graphics: string | undefined): boolean {
return graphics !== 'vmware-svga'
}
export function deriveSettingsResolutionState(params: {
form: Pick<SetOpts, 'graphics' | 'resolution' | 'auto_resolution' | 'borderless'>
displayProfile: HostReport
fixedModeSupported: boolean
forceCustomRes: boolean
}) {
const { form, displayProfile, fixedModeSupported, forceCustomRes } = params
const vgpu = supportsAutoResolution(form.graphics)
const autoOn = vgpu && !!form.auto_resolution
const presets = windowDisplayPresets(displayProfile, fixedModeSupported)
const fsPresets = fullscreenDisplayPresets(displayProfile)
const activePresets: FullscreenDisplayPreset[] = form.borderless ? fsPresets : presets
const autoPreset = resolveAutoDisplayPreset(displayProfile, !!form.borderless, fixedModeSupported)
const matchedPreset = activePresets.find((p) => normalizeResolution(p.resolution) === normalizeResolution(form.resolution))
const presetHit = !!matchedPreset
const customRes = vgpu && !autoOn && (forceCustomRes || !presetHit)
const resValue = autoOn
? AUTO_RESOLUTION_OPTION
: (customRes ? CUSTOM_RESOLUTION_OPTION : matchedPreset?.resolution)
const resBad = vgpu && !autoOn && customRes && !isValidResolution(form.resolution)
return { presets, fsPresets, activePresets, autoPreset, matchedPreset, presetHit, customRes, resValue, resBad }
}
export function selectSettingsResolution(params: {
value: string
form: Pick<SetOpts, 'resolution'>
base: Pick<SetOpts, 'resolution'>
autoPreset: FullscreenDisplayPreset
}): { patch: Partial<SetOpts>; forceCustomRes: boolean } {
const { value, form, base, autoPreset } = params
if (value === AUTO_RESOLUTION_OPTION) {
return {
patch: { auto_resolution: true, resolution: base.resolution },
forceCustomRes: false,
}
}
if (value === CUSTOM_RESOLUTION_OPTION) {
return {
patch: { auto_resolution: false, resolution: form.resolution || autoPreset.resolution },
forceCustomRes: true,
}
}
return {
patch: { auto_resolution: false, resolution: value },
forceCustomRes: false,
}
}
export function shouldExitCustomResOnBlur(params: {
autoResolution?: boolean
forceCustomRes: boolean
presetHit: boolean
}): boolean {
return !params.autoResolution && params.forceCustomRes && params.presetHit
}
export function resolveCustomResBlur(params: {
resolution?: string
autoResolution?: boolean
forceCustomRes: boolean
activePresets: FullscreenDisplayPreset[]
}): { exitCustom: boolean; nextResolution?: string } {
const { resolution, autoResolution, forceCustomRes, activePresets } = params
if (autoResolution) return { exitCustom: false }
const matched = activePresets.find((p) => normalizeResolution(p.resolution) === normalizeResolution(resolution))
if (matched) {
return {
exitCustom: forceCustomRes || true,
nextResolution: matched.resolution,
}
}
if (isValidResolution(resolution)) {
const canonical = canonicalResolution(resolution)
if (canonical && canonical !== resolution) {
return { exitCustom: false, nextResolution: canonical }
}
}
return { exitCustom: false }
}
export function switchSettingsGraphics(graphics: string): { patch: Partial<SetOpts>; forceCustomRes?: boolean } {
if (!supportsAutoResolution(graphics)) {
return { patch: { graphics }, forceCustomRes: false }
}
return { patch: { graphics } }
}
export function expandTouchedSettingsFields(fields: Iterable<string>): string[] {
const out = new Set<string>()
for (const field of fields) {
out.add(field)
if (field === 'resolution' || field === 'auto_resolution') {
out.add('resolution')
out.add('auto_resolution')
}
}
return Array.from(out)
}
export function syncUntouchedSettingsForm<T extends object>(
form: T,
base: T,
touchedFields: ReadonlySet<string>,
): T {
let changed = false
const next = { ...form } as Record<string, unknown>
const baseRec = base as Record<string, unknown>
for (const key of Object.keys(baseRec)) {
if (touchedFields.has(key)) continue
if (next[key] !== baseRec[key]) {
next[key] = baseRec[key]
changed = true
}
}
return changed ? (next as T) : form
}
export function reconcileSettingsFormOnBaseChange<T extends object>(params: {
previousVMKey: string
nextVMKey: string
form: T
base: T
touchedFields: ReadonlySet<string>
}): { form: T; resetDraft: boolean } {
const { previousVMKey, nextVMKey, form, base, touchedFields } = params
if (previousVMKey !== nextVMKey) {
return { form: base, resetDraft: true }
}
return { form: syncUntouchedSettingsForm(form, base, touchedFields), resetDraft: false }
}
export function toggleSettingsBorderless(params: {
borderless: boolean
form: Pick<SetOpts, 'resolution' | 'auto_resolution'>
displayProfile: HostReport
targetDisplay?: DisplayInfo
presets: FullscreenDisplayPreset[]
fsPresets: FullscreenDisplayPreset[]
customRes: boolean
}): Partial<SetOpts> {
const { borderless, form, displayProfile, targetDisplay, presets, fsPresets, customRes } = params
if (form.auto_resolution) {
return { borderless }
}
const targetPresets = borderless ? fsPresets : presets
if (!customRes && !targetPresets.some((p) => canKeepDisplayPreset(p, form.resolution, borderless))) {
const nextRes = borderless
? (targetDisplay && targetDisplay.width > 0 && targetDisplay.height > 0
? `${targetDisplay.width}x${targetDisplay.height}`
: targetPresets[0]?.resolution ?? form.resolution)
: recommendedResolution(displayProfile)
return { borderless, resolution: nextRes }
}
return { borderless }
}
export function countChangedSettingsFields(changedFields: readonly string[]): number {
const hasBothRes = changedFields.includes('resolution') && changedFields.includes('auto_resolution')
return changedFields.length - (hasBothRes ? 1 : 0)
}
export function buildSettingsSaveDiff(
name: string,
changedFields: readonly string[],
form: SetOpts,
base?: Pick<SetOpts, 'auto_resolution'>,
): SetOpts {
const diff: SetOpts = { name }
const vgpu = supportsAutoResolution(form.graphics)
const effectiveAuto = vgpu && !!form.auto_resolution
for (const field of changedFields) {
if (field === 'resolution') {
if (!vgpu || effectiveAuto) continue
diff.resolution = canonicalResolution(form.resolution)
continue
}
if (field === 'auto_resolution') {
diff.auto_resolution = effectiveAuto
continue
}
;(diff as unknown as Record<string, unknown>)[field] = (form as unknown as Record<string, unknown>)[field]
}
if (!vgpu && changedFields.includes('graphics') && base?.auto_resolution) {
diff.auto_resolution = false
}
if (vgpu && !effectiveAuto && diff.auto_resolution === false && !diff.resolution) {
const resolved = canonicalResolution(form.resolution)
if (resolved) diff.resolution = resolved
}
return diff
}
export function initialWizardManualResolution(
draft?: { opts: Pick<CreateOpts, 'graphics' | 'auto_resolution'>; manualResolution?: boolean } | null,
): boolean {
if (!draft) return false
if (typeof draft.manualResolution === 'boolean') return draft.manualResolution
return supportsAutoResolution(draft.opts.graphics) && draft.opts.auto_resolution === false
}
export function initialWizardResolution(
host: HostReport,
fixedModeSupported: boolean,
graphics: string = 'reims-vgpu',
): Pick<CreateOpts, 'auto_resolution' | 'resolution'> {
return {
auto_resolution: supportsAutoResolution(graphics),
resolution: resolveAutoDisplayPreset(host, false, fixedModeSupported).resolution,
}
}
export function resolveEffectiveWizardOpts(
opts: CreateOpts,
autoPreset: FullscreenDisplayPreset,
): CreateOpts {
if (!supportsAutoResolution(opts.graphics)) {
return opts.auto_resolution ? { ...opts, auto_resolution: false } : opts
}
if (opts.auto_resolution) {
return { ...opts, resolution: autoPreset.resolution }
}
return opts
}
export function switchWizardGraphics(params: {
graphics: CreateOpts['graphics']
manualResolutionPicked: boolean
autoPreset: FullscreenDisplayPreset
}): Partial<CreateOpts> {
const { graphics, manualResolutionPicked, autoPreset } = params
if (!supportsAutoResolution(graphics)) {
return { graphics, auto_resolution: false }
}
if (!manualResolutionPicked) {
return { graphics, auto_resolution: true, resolution: autoPreset.resolution }
}
return { graphics }
}
export function selectWizardResolution(
value: string,
autoPreset: FullscreenDisplayPreset,
): Pick<CreateOpts, 'auto_resolution' | 'resolution'> {
if (value === AUTO_RESOLUTION_OPTION) {
return { auto_resolution: true, resolution: autoPreset.resolution }
}
return { auto_resolution: false, resolution: value }
}
export function toggleWizardBorderless(params: {
borderless: boolean
opts: Pick<CreateOpts, 'graphics' | 'auto_resolution' | 'resolution'>
host: HostReport
fixedModeSupported: boolean
windowPresets: FullscreenDisplayPreset[]
fullscreenPresets: FullscreenDisplayPreset[]
}): Partial<CreateOpts> {
const { borderless, opts, host, fixedModeSupported, windowPresets, fullscreenPresets } = params
if (supportsAutoResolution(opts.graphics) && opts.auto_resolution) {
return {
borderless,
auto_resolution: true,
resolution: resolveAutoDisplayPreset(host, borderless, fixedModeSupported).resolution,
}
}
const list = borderless ? fullscreenPresets : windowPresets
const keep = list.some((p) => canKeepDisplayPreset(p, opts.resolution, borderless))
const fallback = list.find((p) => p.recommended)?.resolution ?? list[0]?.resolution ?? opts.resolution
return keep ? { borderless } : { borderless, resolution: fallback }
}