Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | 114x 114x 114x 114x 72x 13x 72x 13x 2x 2x 13x 1x 1x 1x 1x 13x 13x 13x 13x 13x 114x 14x 14x 114x 114x 114x 114x 684x 114x 114x 114x 114x 228x 456x 2x 4x 456x 5x 228x 4x 1x 1x 228x 456x 1x 798x | import { useEffect, useId, useRef } from 'react';
import { lengthFromUnits, lengthInUnits, lengthUnit } from '@ies-tools/core';
import { TONE_MAPPING_OPTIONS, UNITS_OPTIONS, VIEW_MODE_OPTIONS, type ViewerSettings } from '../settings.js';
/** `open` shows the panel, `closed` shows only its toggle button, and `hidden` shows neither. */
export type SettingsPanelMode = 'open' | 'closed' | 'hidden';
/** Props for {@link ViewerSettingsPanel}. */
export interface ViewerSettingsPanelProps {
/** Current settings to display. */
settings: ViewerSettings;
mode?: SettingsPanelMode;
/** Called with each changed setting. */
onChange: (patch: Partial<ViewerSettings>) => void;
/** Called when the user opens or closes the panel. */
onModeChange: (mode: SettingsPanelMode) => void;
/** Called when an entered value is rejected. */
onError?: (error: unknown) => void;
}
/**
* Shared React controls for the custom element, React viewer, and embedded applications.
*
* @param props - See {@link ViewerSettingsPanelProps}.
*/
export function ViewerSettingsPanel({
settings,
mode = 'closed',
onChange,
onModeChange,
onError,
}: ViewerSettingsPanelProps) {
const id = useId();
const button = useRef<HTMLButtonElement>(null);
const panel = useRef<HTMLElement>(null);
useEffect(() => {
if (mode !== 'open') return;
const document = button.current?.ownerDocument;
Iif (!document) return;
const outside = (event: Event) => {
const path = event.composedPath();
Eif (!path.includes(panel.current!) && !path.includes(button.current!)) onModeChange('closed');
};
const escape = (event: KeyboardEvent) => {
Iif (event.key !== 'Escape') return;
event.preventDefault();
onModeChange('closed');
button.current?.focus();
};
document.addEventListener('pointerdown', outside);
document.addEventListener('keydown', escape);
return () => {
document.removeEventListener('pointerdown', outside);
document.removeEventListener('keydown', escape);
};
}, [mode, onModeChange]);
const change = (patch: Partial<ViewerSettings>) => {
try {
onChange(patch);
} catch (error) {
if (onError) onError(error);
else throw error;
}
};
const close = () => {
onModeChange('closed');
button.current?.focus();
};
const control =
'rounded-md border border-border bg-input px-2 py-1 text-foreground outline-none focus-visible:ring-2 focus-visible:ring-ring';
const unit = lengthUnit(settings.units);
// Distances are stored in metres; imperial inputs show feet rounded to hundredths.
const shownLength = (meters: number) =>
settings.units === 'imperial' ? Number(lengthInUnits(meters, 'imperial').toFixed(2)) : meters;
// False colours bypass exposure and tone mapping, so those controls stay visible but inert while measuring.
const measuring = settings.viewMode === 'measurement';
const unused = measuring ? 'Not used in measurement view' : undefined;
const toggles = [
['viewMode', 'View mode', VIEW_MODE_OPTIONS],
['units', 'Units', UNITS_OPTIONS],
] as const;
return (
<div
className="settings-ui pointer-events-none absolute inset-0 z-20 font-sans text-[length:var(--vscode-font-size,13px)] text-foreground"
hidden={mode === 'hidden'}
>
<div className="viewer-toggles pointer-events-auto absolute right-14 bottom-3 flex gap-2">
{toggles.map(([key, label, options]) => (
<fieldset
key={key}
aria-label={label}
className="m-0 flex h-9 min-w-0 p-0 overflow-hidden rounded-md border border-border bg-input shadow-sm"
>
{options.map((option) => (
<button
key={option.value}
type="button"
aria-pressed={settings[key] === option.value}
className="cursor-pointer px-2.5 text-foreground outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset aria-pressed:bg-primary aria-pressed:text-primary-foreground"
onClick={() => change({ [key]: option.value })}
>
{option.label}
</button>
))}
</fieldset>
))}
</div>
<button
ref={button}
type="button"
aria-label="Viewer options"
title="Viewer options"
aria-controls={id}
aria-expanded={mode === 'open'}
className={`settings-toggle pointer-events-auto absolute right-3 bottom-3 grid size-9 cursor-pointer place-items-center text-xl shadow-sm ${control}`}
onClick={() => onModeChange(mode === 'open' ? 'closed' : 'open')}
>
<svg
viewBox="0 0 24 24"
width="20"
height="20"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
aria-hidden="true"
>
<path
d="m9 3-.8 2.2-2 .9L4 5.5 2.5 8l1.6 1.7-.2 2.3L2 13.5 3.5 16l2.3-.4 1.8 1.4.4 2.4h3l.8-2.2 2-.9 2.2.6 1.5-2.5-1.6-1.7.2-2.3L18 8.5 16.5 6l-2.3.4-1.8-1.4L12 3Z"
transform="translate(2 1)"
/>
<circle cx="12" cy="12" r="3" />
</svg>
</button>
<section
ref={panel}
id={id}
className="settings-panel pointer-events-auto absolute right-3 bottom-14 max-h-[calc(100%-72px)] w-[min(300px,calc(100%-24px))] overflow-auto rounded-lg border border-border bg-popover p-4 text-popover-foreground shadow-xl"
aria-label="Viewer options"
hidden={mode !== 'open'}
>
<div className="mb-3 flex items-center justify-between gap-3">
<strong className="font-semibold">Viewer options</strong>
<button
type="button"
aria-label="Close viewer options"
className={`grid size-7 cursor-pointer place-items-center text-lg ${control}`}
onClick={close}
>
×
</button>
</div>
{(
[
['showWall', 'Back wall'],
['helper', 'Helper'],
['photometricSolid', 'Photometric solid'],
['rulers', 'Rulers'],
] as const
).map(([key, label]) => (
<label key={key} className="my-3 flex items-center justify-between gap-3">
{label}
<input
name={key}
type="checkbox"
checked={settings[key]}
className="size-4 accent-primary focus-visible:ring-2 focus-visible:ring-ring"
onChange={(event) => change({ [key]: event.currentTarget.checked })}
/>
</label>
))}
{(
[
['wallDistance', 'Wall distance'],
['floorDistance', 'Floor distance'],
] as const
).map(([key, label]) => (
<label key={key} className="my-3 flex items-center justify-between gap-3">
{`${label} (${unit})`}
<input
name={key}
type="number"
min={shownLength(0.1)}
max={shownLength(100)}
step="0.1"
value={shownLength(settings[key])}
className={`w-24 ${control}`}
onChange={(event) =>
change({ [key]: lengthFromUnits(event.currentTarget.valueAsNumber, settings.units) })
}
/>
</label>
))}
{/* While metering, the EV field shows the metered value; unchecking keeps it as the manual start. */}
<div className="my-3 flex items-center justify-between gap-3">
<label className="flex items-center gap-2">
<input
name="autoExposure"
type="checkbox"
checked={settings.autoExposure}
disabled={measuring}
title={unused}
className="size-4 accent-primary disabled:opacity-60 focus-visible:ring-2 focus-visible:ring-ring"
onChange={(event) => change({ autoExposure: event.currentTarget.checked })}
/>
Auto exposure
</label>
<label className="flex items-center gap-2">
EV
<input
name="exposure"
type="number"
min={-6}
max={20}
step="0.1"
disabled={settings.autoExposure || measuring}
title={unused ?? 'Camera exposure value at ISO 100 (EV100)'}
value={Number(settings.exposure.toFixed(1))}
className={`w-20 disabled:opacity-60 ${control}`}
onChange={(event) => change({ exposure: event.currentTarget.valueAsNumber })}
/>
</label>
</div>
{toggles.map(([key, label, options]) => (
<label key={key} className="my-3 flex items-center justify-between gap-3">
{label}
<select
name={key}
value={settings[key]}
className={`w-36 ${control}`}
onChange={(event) => change({ [key]: event.currentTarget.value })}
>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</label>
))}
<label className="my-3 flex items-center justify-between gap-3">
Tone mapping
<select
name="toneMapping"
value={settings.toneMapping}
disabled={measuring}
title={unused}
className={`w-36 disabled:opacity-60 ${control}`}
onChange={(event) => change({ toneMapping: event.currentTarget.value as ViewerSettings['toneMapping'] })}
>
{TONE_MAPPING_OPTIONS.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</label>
</section>
</div>
);
}
|