/** * The styles of the form layer's elements, the same in every framework: the * box over each widget, the text field's editor, the list box and the focus * ring. Each is a style record (camelCase names, lengths with their units), * the shape a style object takes in React, Vue and Angular; Svelte writes one * as CSS text with `cssText`. Each is a type alias, so it fits Vue's * `CSSProperties` (see the note in `form-field`). The field's own look inside them comes from * `form-field` ({@link textFieldStyleOf}, {@link listBoxStyleOf}); its colors * from the form settings ({@link formColorsOf}); its font from the form plugin. */ import { listBoxStyleOf, scaleOf, textFieldFontPxOf, textFieldStyleOf } from './form-field'; import type { CssFont, FieldLook, FormColors, ListBoxStyle, TextFieldStyle } from './form-field'; import { legibleScaleOf, legibleShrinkOf } from './legible-text'; import type { PixelRect } from './page-pixels'; /** * A control that fills the widget's box it sits in: every native control the * layer draws. Its corners are square, as the widget's are: the layer draws in * the page, where a rule of the page's own for its inputs and buttons (a * radius on the focused one, say) would round the control and its focus ring. */ export const FORM_CONTROL_FILL = Object.freeze({ position: 'absolute', inset: '0', width: '100%', height: '100%', boxSizing: 'border-box', margin: '0', borderRadius: '0', } as const); /** {@link FORM_CONTROL_FILL}'s shape. */ export type FormControlFill = typeof FORM_CONTROL_FILL; /** The box a widget's control sits in, placed over the widget on the page. */ export type WidgetBoxStyle = { position: 'absolute'; left: string; top: string; width: string; height: string; /** * A turned widget's quarter turn, about the box's middle: the box is the * widget's frame, and turned it covers where the widget stands. Left out * for a widget that isn't turned. */ transform?: string; /** Always the widget's event surface: the control inside gates the edits. */ pointerEvents: 'auto'; /** The edge of a field without a border of its own, so people see where to fill in. */ boxShadow?: string; }; /** * The style of a widget's box at `frame`: the widget's frame (`box`, what its * contents are laid out in) in the page layer's pixels. A turned widget's box * turns with it, about its middle (`rotation`, a quarter turn clockwise), so * whatever control sits in it reads the way the field does, and the turned * box covers the rect the widget stands in. `edge: false` leaves the edge * out, for a control that draws its own (a list box) or never has one (a * button). */ export function widgetBoxStyleOf( widget: { look: Pick; rotation?: number | null }, frame: PixelRect, colors: FormColors, { edge = true }: { edge?: boolean } = {}, ): WidgetBoxStyle { return { position: 'absolute', left: `${frame.left}px`, top: `${frame.top}px`, width: `${frame.width}px`, height: `${frame.height}px`, ...(widget.rotation ? { transform: `rotate(${widget.rotation}deg)` } : {}), pointerEvents: 'auto', ...(edge && widget.look.border === null ? { boxShadow: `inset 0 0 0 1px ${colors.border}` } : {}), }; } /** * Where a text field's editor shows the text typed in it, so it sits where the engine will draw * it (the form plugin's `getTextEditorLayout`), in page points. */ export interface TextEditorLayout { readonly fontFamily: string; readonly fontWeight: number; readonly fontStyle: 'normal' | 'italic'; readonly fontSize: number; readonly lineHeight: number; readonly padding: { readonly top: number; readonly right: number; readonly bottom: number; readonly left: number; }; /** A comb's room between characters; it starts at the left padding, whatever the alignment. */ readonly letterSpacing: number; readonly comb: boolean; /** The text reads right to left, as the engine draws it (its first strong character is). */ readonly rtl: boolean; } /** A text field's editor: see-through at rest, the field's own look while focused. */ export type TextFieldEditorStyle = TextFieldStyle & { position: 'absolute'; left: '0'; top: '0'; /** The widget's box times the editor's legible scale, in percent with its unit. */ width: string; height: string; boxSizing: 'border-box'; margin: '0'; /** Draws the editor, laid out at a legible size, at the size of the widget's box. */ transform: string; transformOrigin: '0 0'; border: 'none'; /** Square corners, whatever the page gives its inputs: the ring follows them. */ borderRadius: '0'; outline: string; outlineOffset: string; padding: string; /** In pixels, with its unit: one line of the engine's layout. */ lineHeight?: string; /** `none` with the engine's layout: the browser never fakes a bold or an italic the face doesn't have. */ fontSynthesis?: 'none'; /** * With the engine's layout, the direction the line reads in, the engine's: * mixed scripts are ordered as the engine orders them, and the end of a * right-to-left line is where typing goes on. Alignment stays the field's. */ direction?: 'ltr' | 'rtl'; resize: 'none'; cursor: 'text'; /** 1 while focused, 0 at rest, where the field's picture shows through. */ opacity: number; /** `none` for a disabled field: the box below runs the widget's action. */ pointerEvents?: 'none'; }; /** * The style of a text field's editor, the `` or `