Files
t3code/apps/web/src/browser/browserDefaults.ts
T
Julius MarmingeandClaude Opus 5 86975b87a7 feat(web): proper number fields, orientation, and floating-preview toggle
Three follow-ups to the browser defaults:

The dimension inputs were bare `Input type=number` at w-16, which clipped
4-digit values ("1280" rendered as "128("). They are now `NumberField`, the
repo's real numeric control, widened to fit and with grouping switched off so
a height reads "1024" rather than "1,024".

They commit on blur via `onValueCommitted` instead of per keystroke. Typing
"2560" passes through "256", which is itself a legal dimension, so a
change-handler would persist that intermediate size and rewrite settings on
every key.

Adds an orientation control next to the dimensions, reusing the device
toolbar's rotate affordance — its icon moves to a shared module rather than
being duplicated. Rotating a preset keeps the preset: `resolvePreviewViewport`
already stores rotated presets as the id plus swapped dimensions, so a rotated
iPad Mini stays an iPad Mini instead of decaying into a custom size.

Adds `browserAutoShowFloatingPreview` (on by default) for whether an
agent-opened preview pops the floating player into view. An explicit
`open`/`show` on `preview_open` still wins: that is the agent deliberately
surfacing or hiding its work, which shouldn't be overridden by a default.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-15 16:26:16 +02:00

99 lines
3.8 KiB
TypeScript

/**
* Browser defaults - resolves the configured starting state for preview tabs.
*
* Settings → Integrations → Browser lets the user pick the viewport, zoom, and
* appearance a preview tab should open at. Those preferences apply to every
* entry point that opens a tab without stating its own: the user opening a
* browser panel, and agents calling `preview_open` with no size.
*
* The values live in client settings because the Chromium guest they configure
* is desktop-local, so this module reads them through the same external store
* the settings UI writes to, and exposes a non-hook accessor for the imperative
* open paths that run outside React.
*
* @module browserDefaults
*/
import type {
DesktopPreviewTabDefaults,
PreviewAppearancePreference,
PreviewViewportSetting,
} from "@t3tools/contracts";
import { getClientSettings, useClientSettings } from "~/hooks/useSettings";
import { resolveResponsiveBrowserViewportSize } from "./browserViewportLayout";
export interface BrowserDefaults {
readonly viewport: PreviewViewportSetting;
readonly zoomFactor: number;
readonly appearance: PreviewAppearancePreference;
readonly autoShowFloatingPreview: boolean;
}
const toBrowserDefaults = (settings: {
readonly browserDefaultViewport: PreviewViewportSetting;
readonly browserDefaultZoomFactor: number;
readonly browserDefaultAppearance: PreviewAppearancePreference;
readonly browserAutoShowFloatingPreview: boolean;
}): BrowserDefaults => ({
viewport: settings.browserDefaultViewport,
zoomFactor: settings.browserDefaultZoomFactor,
appearance: settings.browserDefaultAppearance,
autoShowFloatingPreview: settings.browserAutoShowFloatingPreview,
});
/** Non-hook accessor for imperative open paths (menu actions, automation hosts). */
export function getBrowserDefaults(): BrowserDefaults {
return toBrowserDefaults(getClientSettings());
}
export function useBrowserDefaults(): BrowserDefaults {
return useClientSettings(toBrowserDefaults);
}
/**
* The zoom/appearance half of the defaults, in the shape `createTab` takes.
* Passing these at creation rather than after registration keeps the guest from
* painting a frame at 100%/system first.
*/
export function browserDefaultTabState(
defaults: BrowserDefaults = getBrowserDefaults(),
): DesktopPreviewTabDefaults {
return { zoomFactor: defaults.zoomFactor, colorScheme: defaults.appearance };
}
/**
* The viewport a *newly opened* tab should start at. Sent with `preview.open`
* so the session is born at the configured size instead of being resized a
* frame later, which the user would see as a visible reflow.
*/
export function browserDefaultOpenViewport(
defaults: BrowserDefaults = getBrowserDefaults(),
): PreviewViewportSetting {
return defaults.viewport;
}
/**
* The viewport to switch to when the user turns the device toolbar on for a tab
* currently in fill mode.
*
* A configured non-fill default is what the user said they want to look at, so
* it wins. When the default is fill there is no stated preference to honour, so
* fall back to fitting the panel — and only when the panel hasn't been measured
* yet does a fixed size apply.
*/
export const FALLBACK_RESPONSIVE_VIEWPORT_SIZE = { width: 1024, height: 768 } as const;
export function browserResponsiveViewportForToggle(input: {
readonly defaults?: BrowserDefaults;
readonly panelRect: { readonly width: number; readonly height: number } | null;
readonly zoomFactor: number | undefined;
}): PreviewViewportSetting {
const defaults = input.defaults ?? getBrowserDefaults();
if (defaults.viewport._tag !== "fill") return defaults.viewport;
const size = input.panelRect
? resolveResponsiveBrowserViewportSize(input.panelRect, input.zoomFactor)
: FALLBACK_RESPONSIVE_VIEWPORT_SIZE;
return { _tag: "freeform", ...size };
}