Appearance, VM busy state, the in-app update, file drops and the theme event stream are now hooks in lib/. VMAction moved to types.ts, so the VM page and its parts no longer import each other, and error toasts have one builder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
205 lines
8.2 KiB
TypeScript
205 lines
8.2 KiB
TypeScript
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
|
import type { app as WailsApp } from '../../../wailsjs/go/models'
|
|
import { I18nKey, parseAppError, t } from '../../i18n'
|
|
import { humanDisplayFallbackName, humanDisplayMeta, humanDisplayTitle, sortDisplaysForPicker } from '../../lib/format'
|
|
import { DisplayInfo, HostReport, Locale } from '../../types'
|
|
import { PopupSelect, SetRow } from '../../ui/settings'
|
|
import { errorToast, type ToastInput } from '../../ui/toast'
|
|
import { api } from '../../wails'
|
|
|
|
const PRIMARY_DISPLAY_OPT = '__primary'
|
|
|
|
interface Options {
|
|
locale: Locale
|
|
driverID: string
|
|
vm: string
|
|
running: boolean
|
|
host: HostReport
|
|
shown: boolean
|
|
isBlocked: () => boolean
|
|
onToast: (toast: ToastInput) => void
|
|
}
|
|
|
|
/** The host display the VM window opens on. A change applies at once. */
|
|
export function useTargetDisplay({ locale, driverID, vm, running, host, shown, isBlocked, onToast }: Options) {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const activeVMNameRef = useRef(vm)
|
|
activeVMNameRef.current = vm
|
|
const [displayStatus, setDisplayStatus] = useState<WailsApp.VMDisplayStatus | null>(null)
|
|
const [displayBusy, setDisplayBusy] = useState(false)
|
|
const [displayErr, setDisplayErr] = useState('')
|
|
const displayBusyRef = useRef(false)
|
|
const displayReqSeqRef = useRef(0)
|
|
|
|
const connectedDisplays = useMemo<DisplayInfo[]>(
|
|
() => sortDisplaysForPicker(displayStatus?.displays ?? host.displays ?? []),
|
|
[displayStatus, host.displays],
|
|
)
|
|
const activeTargetDisplay = displayStatus
|
|
? connectedDisplays.find((d) => d.name === displayStatus.effective_display)
|
|
: host.primary_display
|
|
const displayProfile = useMemo(() => displayStatus ? {
|
|
...host,
|
|
primary_display: activeTargetDisplay,
|
|
display_presets: displayStatus.display_presets,
|
|
fullscreen_hidpi: displayStatus.fullscreen_hidpi,
|
|
hidpi_advice: displayStatus.hidpi_advice,
|
|
recommended_resolution: undefined,
|
|
} : host, [host, displayStatus, activeTargetDisplay])
|
|
|
|
const hostDisplaysSig = (host.displays ?? [])
|
|
.map((d) => `${d.name ?? ''}:${d.friendly_name ?? ''}:${d.width}x${d.height}@${d.scale_pct}:${d.refresh_hz ?? 0}:${d.work_width ?? 0}x${d.work_height ?? 0}:${d.primary ? 1 : 0}`)
|
|
.join('|')
|
|
|
|
useEffect(() => {
|
|
displayReqSeqRef.current += 1
|
|
displayBusyRef.current = false
|
|
setDisplayStatus(null)
|
|
setDisplayBusy(false)
|
|
setDisplayErr('')
|
|
}, [driverID, vm])
|
|
|
|
useEffect(() => {
|
|
let active = true
|
|
const reqSeq = displayReqSeqRef.current
|
|
if (displayBusyRef.current) return () => { active = false }
|
|
api.GetVMDisplay(driverID, vm)
|
|
.then((st) => {
|
|
if (active && displayReqSeqRef.current === reqSeq) {
|
|
setDisplayStatus(st)
|
|
setDisplayErr('')
|
|
}
|
|
})
|
|
.catch((e) => {
|
|
if (active && displayReqSeqRef.current === reqSeq) {
|
|
setDisplayErr(parseAppError(e).summary)
|
|
}
|
|
})
|
|
return () => { active = false }
|
|
}, [driverID, vm, running, hostDisplaysSig, shown])
|
|
|
|
const numberedDisplay = (n: number) => tr('targetDisplayNumbered', { n })
|
|
|
|
const displayOptions = useMemo(() => {
|
|
const opts: Array<{ value: string; label: string; detail?: string }> = [
|
|
{
|
|
value: PRIMARY_DISPLAY_OPT,
|
|
label: tr('targetDisplayPrimaryDefault'),
|
|
},
|
|
...connectedDisplays
|
|
.filter((d): d is DisplayInfo & { name: string } => Boolean(d.name?.trim()))
|
|
.map((d, idx) => ({
|
|
value: d.name,
|
|
label: humanDisplayTitle(d, idx + 1, numberedDisplay),
|
|
detail: humanDisplayMeta(d, d.primary ? tr('hostDisplayPrimary') : undefined),
|
|
})),
|
|
]
|
|
const savedTarget = displayStatus?.target_display?.trim()
|
|
if (savedTarget && displayStatus?.fallback_to_primary) {
|
|
if (!opts.some((o) => o.value.toLowerCase() === savedTarget.toLowerCase())) {
|
|
opts.push({
|
|
value: savedTarget,
|
|
label: humanDisplayFallbackName(savedTarget, numberedDisplay),
|
|
detail: tr('targetDisplayDisconnectedOption'),
|
|
})
|
|
}
|
|
}
|
|
return opts
|
|
}, [connectedDisplays, displayStatus?.fallback_to_primary, displayStatus?.target_display, locale])
|
|
const selectedDisplayValue = useMemo(() => {
|
|
const saved = displayStatus?.target_display?.trim()
|
|
if (!saved) return PRIMARY_DISPLAY_OPT
|
|
return displayOptions.find((o) => o.value.toLowerCase() === saved.toLowerCase())?.value ?? saved
|
|
}, [displayOptions, displayStatus?.target_display])
|
|
const activeTargetDisplayTitle = useMemo(() => {
|
|
if (!activeTargetDisplay) return ''
|
|
const idx = connectedDisplays.findIndex((d) => d === activeTargetDisplay || (d.name && activeTargetDisplay.name && d.name.toLowerCase() === activeTargetDisplay.name.toLowerCase()))
|
|
return humanDisplayTitle(activeTargetDisplay, idx >= 0 ? idx + 1 : 1, numberedDisplay)
|
|
}, [activeTargetDisplay, connectedDisplays, locale])
|
|
const handleTargetDisplayChange = async (nextValue: string) => {
|
|
if (isBlocked() || displayBusyRef.current || displayStatus === null) return
|
|
const nextTarget = nextValue === PRIMARY_DISPLAY_OPT ? '' : nextValue
|
|
const currentTarget = displayStatus.target_display ?? ''
|
|
if (nextTarget.toLowerCase() === currentTarget.toLowerCase()) return
|
|
|
|
const opSeq = ++displayReqSeqRef.current
|
|
displayBusyRef.current = true
|
|
setDisplayBusy(true)
|
|
setDisplayErr('')
|
|
try {
|
|
const next = await api.SetVMDisplay(driverID, vm, nextTarget)
|
|
if (activeVMNameRef.current !== vm || displayReqSeqRef.current !== opSeq) return
|
|
setDisplayStatus(next)
|
|
const chosenLabel = displayOptions.find((o) => o.value === nextValue)?.label
|
|
?? humanDisplayFallbackName(next.target_display, numberedDisplay)
|
|
if (next.move_error) {
|
|
onToast(errorToast(locale, tr('targetDisplayToastSaved', { display: chosenLabel }), next.move_error))
|
|
} else {
|
|
onToast({
|
|
kind: 'ok',
|
|
title: next.applied_live
|
|
? tr('targetDisplayToastMoved', { display: chosenLabel })
|
|
: tr('targetDisplayToastSaved', { display: chosenLabel }),
|
|
})
|
|
}
|
|
} catch (e) {
|
|
if (activeVMNameRef.current !== vm || displayReqSeqRef.current !== opSeq) return
|
|
onToast(errorToast(locale, tr('targetDisplaySaveFailed'), e))
|
|
} finally {
|
|
if (displayReqSeqRef.current === opSeq) {
|
|
displayBusyRef.current = false
|
|
setDisplayBusy(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
displayStatus, displayBusy, displayErr, connectedDisplays, activeTargetDisplay,
|
|
activeTargetDisplayTitle, displayProfile, displayOptions, selectedDisplayValue, handleTargetDisplayChange,
|
|
/** Whether a change is being saved right now, ahead of the next render. */
|
|
isBusy: () => displayBusyRef.current,
|
|
}
|
|
}
|
|
|
|
interface RowProps {
|
|
locale: Locale
|
|
running: boolean
|
|
disabled: boolean
|
|
state: ReturnType<typeof useTargetDisplay>
|
|
}
|
|
|
|
export const TargetDisplayRow: React.FC<RowProps> = ({ locale, running, disabled, state }) => {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const { displayStatus, displayBusy, displayErr, connectedDisplays, displayOptions, selectedDisplayValue, handleTargetDisplayChange } = state
|
|
const numberedDisplay = (n: number) => tr('targetDisplayNumbered', { n })
|
|
const targetDisplayDesc = displayStatus?.fallback_to_primary && displayStatus.target_display
|
|
? (
|
|
<span className="warn-text">
|
|
{tr('targetDisplayFallbackHint', {
|
|
name: humanDisplayFallbackName(displayStatus.target_display, numberedDisplay),
|
|
})}
|
|
</span>
|
|
)
|
|
: connectedDisplays.length === 0
|
|
? tr('targetDisplayNoneHint')
|
|
: connectedDisplays.length === 1
|
|
? tr('targetDisplaySingleHint')
|
|
: tr(running ? 'targetDisplayRunningHint' : 'targetDisplayStoppedHint')
|
|
return (
|
|
<SetRow
|
|
title={tr('targetDisplayTitle')}
|
|
desc={targetDisplayDesc}
|
|
error={displayErr || undefined}
|
|
disabled={disabled || displayBusy || displayStatus === null || (connectedDisplays.length === 0 && !displayStatus.target_display)}
|
|
control={
|
|
<PopupSelect
|
|
label={tr('targetDisplayTitle')}
|
|
value={selectedDisplayValue}
|
|
onChange={(v) => void handleTargetDisplayChange(v)}
|
|
options={displayOptions}
|
|
/>
|
|
}
|
|
/>
|
|
)
|
|
}
|