Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/integrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ set_target_properties(game PROPERTIES BUILD_RPATH "${BUREAU_HERMES_FRAMEWORKS}")
`Options::readAsset` also answers for Bureau's own `assets/`, its fonts and icons,
so ship that folder among your game's assets.

A game that reads its pictures into memory ahead of time says which it holds with
`Options::holdsAsset`. A picture it does not hold yet is then read on a thread of
the UI's own and drawn once it arrives, so a slow disk does not hold the interface.
Until then the view lays the picture out at no size: give a picture whose place
matters its `width` and `height`.
A game that reads its pictures and typefaces into memory ahead of time says which it
holds with `Options::holdsAsset`. A file it does not hold yet is then read on a thread
of the UI's own and drawn once it arrives, so a slow disk does not hold the interface.
Until then the view lays the picture out at no size and sets no text in the face: give
a picture whose place matters its `width` and `height`.

The bundler knows Bureau's import names by itself. For your editor and type checker,
set `bureau/kit/`, `bureau/primitives`, `bureau/host`, `bureau/jsx-runtime`,
Expand Down
10 changes: 5 additions & 5 deletions include/bureau/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ class Ui {
/** A file the UI asks for by its path under the game's assets (`/fonts/…`,
* pictures); nothing when there is none. On the UI's thread for the
* view's faces and pictures, and on a thread of the UI's own for what
* TypeScript reads (`assetText`, `assetBytes`) and pictures `holdsAsset`
* does not hold, so it must be safe to call from both. */
* TypeScript reads (`assetText`, `assetBytes`) and the faces and pictures
* `holdsAsset` does not hold, so it must be safe to call from both. */
std::function<std::optional<std::string>(const std::string& path)> readAsset;
/** Whether `readAsset` answers `path` from memory, without waiting on a
* disk. Given, a picture it does not hold is read on the UI's own reading
* thread, and the view lays out and paints without it until it arrives.
* Empty: the view reads each picture on the UI's thread, where it first
* disk. Given, a picture or a face it does not hold is read on the UI's own
* reading thread, and the view lays out and paints without it until it
* arrives. Empty: the view reads each on the UI's thread, where it first
* lays it out. Called on the UI's thread. */
std::function<bool(const std::string& path)> holdsAsset;
/** The window, in points, and its device pixels per point. */
Expand Down
8 changes: 6 additions & 2 deletions kit/Bureau.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ export type ButtonProps = {
/** A stop on the pad's walk through its window, in `navigationGrid`. */
navigable?: boolean;
navigationGrid?: string;
/** How far past its face a finger still presses it, in points: a small
* key with room around it. Unsaid, the size's own. */
hitSlop?: number;
/** False: the pad and the host focus it, Tab passes it by. */
tabStop?: boolean;
/**
Expand Down Expand Up @@ -1055,6 +1058,7 @@ function Key(
onPointerEnter,
onHoverChange,
onFocusChange,
hitSlop,
children,
...rest
}: ButtonProps & {
Expand Down Expand Up @@ -1196,13 +1200,13 @@ function Key(
disabled={disabled}
pressed={pressed}
held={held || inProgress}
hitSlop={isClose
hitSlop={hitSlop ?? (isClose
? CLOSE_KEY_INSET
: size === "compact"
? 4
: size === "small"
? 2
: undefined}
: undefined)}
focusable
feedback={sound}
opacity={withheld ? 0.6 : undefined}
Expand Down
11 changes: 11 additions & 0 deletions kit/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ Deno.test("a small destructive mark has a 33px face and an expanded hit area", (
);
});

Deno.test("a key given a hit slop reaches that far past its face", () => {
assertEquals(
drawn(
<Button variant="toolbar" mark="left" hitSlop={8} />,
(nodes) =>
nodes.find((node) => node.props.role === "button")?.props.hitSlop,
),
8,
);
});

Deno.test("replacing a small destructive mark with a pad glyph keeps its square", () => {
assertEquals(
drawn(
Expand Down
85 changes: 85 additions & 0 deletions kit/Instrument.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { assertEquals } from "@std/assert";
import type { ReactNode } from "react";
import { installPlatform, testPlatform } from "platform";
import { type PlotCommand, type ScrollOffset, Text } from "bureau/primitives";
import {
createNativeRoot,
type NativeNode,
nativeNodes,
} from "./primitives/reconciler.ts";
import {
Instrument,
InstrumentBody,
InstrumentFooter,
InstrumentHeader,
InstrumentScoreHeader,
} from "./Instrument.tsx";
import { ListRow } from "./List.tsx";
import { useMapPanelFold } from "./map_panel_fold.ts";
import { useInstrumentStack } from "./instrument_stack.ts";

/** Renders one element, reads what it drew, then clears it away. */
Expand Down Expand Up @@ -194,6 +197,88 @@ Deno.test("a scrambled header glitches its facts", () => {
);
});

const layerAnchor = (nodes: NativeNode[]) =>
nodes.find((node) => node.type === "bureau-layer")?.props.anchor;

Deno.test("an instrument with no place stands in the map views' corner", () => {
assertEquals(
drawn(
<Instrument>
<InstrumentHeader heading="Police" />
</Instrument>,
layerAnchor,
),
{ kind: "screen", top: 60, right: 10, bottom: 76 },
);
});

Deno.test("an instrument given a place stands there", () => {
assertEquals(
drawn(
<Instrument anchor={{ left: 24, top: 80, bottom: 96 }}>
<InstrumentHeader heading="Start new city" />
</Instrument>,
layerAnchor,
),
{ kind: "screen", left: 24, top: 80, bottom: 96 },
);
});

/** A map's legend, as the city view's: it says whether it stands folded. */
function Legend() {
const { folded } = useMapPanelFold("legend");
return <Text>{folded ? "legend folded" : "legend open"}</Text>;
}

/** Draws `element` on a platform that folds the map panels, as a tablet. */
function drawnOnATablet<T>(
element: ReactNode,
read: (nodes: NativeNode[]) => T,
): T {
const restore = installPlatform({
...testPlatform,
capabilities: { ...testPlatform.capabilities, foldsMapPanels: true },
});
try {
return drawn(element, read);
} finally {
restore();
}
}

const shows = (text: string) => (nodes: NativeNode[]) =>
nodes.some((node) => node.children.some((child) => child.text === text));

Deno.test("on a tablet, a map view's instrument folds the legend", () => {
assertEquals(
drawnOnATablet(
<>
<Legend />
<Instrument>
<InstrumentHeader heading="Police" />
</Instrument>
</>,
shows("legend folded"),
),
true,
);
});

Deno.test("on a tablet, an instrument given a place counts as no map panel", () => {
assertEquals(
drawnOnATablet(
<>
<Legend />
<Instrument anchor={{ left: 24, top: 80, bottom: 96 }}>
<InstrumentHeader heading="Start new city" />
</Instrument>
</>,
shows("legend open"),
),
true,
);
});

/** An instrument whose root shows one tab's body, as the transport panel's. */
function TabbedInstrument({ tab }: { tab: string }) {
const stack = useInstrumentStack();
Expand Down
28 changes: 16 additions & 12 deletions kit/Instrument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
* parts these are (instrument_pad.ts).
*
* The geometry is the panels' own and is not negotiable — one 280px width and
* anchor for every map view, 33px title bars (44px for a score),
* anchor for every map view (a panel that is no map view says where it
* stands), 33px title bars (44px for a score),
* 33px readout or 44px action footers,
* a body that grows until the bottom controls reserve their space,
* a 12px inset everywhere,
Expand Down Expand Up @@ -71,6 +72,8 @@ export const INSTRUMENT_RIGHT_INSET = WIDGET_WIDTH + PANEL_RIGHT;
/** The room the bottom controls keep: an xlarge HUD key (56), its margin
* (8) and a group's gap (12). */
const PANEL_BOTTOM = 56 + 8 + 12;
/** The top-right corner every map view's instrument shares. */
const MAP_CORNER = { top: PANEL_TOP, right: PANEL_RIGHT, bottom: PANEL_BOTTOM };

/** A list body's height, about twelve rows: a long list scrolls in it
* instead of running the panel down the screen. */
Expand All @@ -84,6 +87,10 @@ type InstrumentProps = {
/** In the flow of a page rather than pinned over the city's corner: the
* design catalog's specimens. */
inline?: boolean;
/** Where it stands, in UI points from the screen's left edge and its top
* and bottom; absent, the map views' corner. A panel that names its own
* place is no map view, so it takes no part in the map panels' folding. */
anchor?: { left: number; top: number; bottom: number };
/** The light tokens forced back over the instrument's dark ones: the road
* not taken, shown beside the shipped one. */
chrome?: "light";
Expand All @@ -101,14 +108,16 @@ const InstrumentFold = createContext<
>(null);

/**
* The shell: a dark Bureau window pinned to the top-right of the viewport,
* its body shrinking first so the header, tabs and footer never clip. Its
* loose text is set in the data font at 12px.
* The shell: a dark Bureau window pinned to the top-right of the viewport, or
* where its caller says, its body shrinking first so the header, tabs and
* footer never clip. Its loose text is set in the data font at 12px.
*/
export function Instrument(
{ width = WIDGET_WIDTH, id, inline, chrome, children }: InstrumentProps,
{ width = WIDGET_WIDTH, id, inline, anchor, chrome, children }:
InstrumentProps,
) {
const { folded, unfold } = useMapPanelFold("instrument", !inline);
const isMapPanel = !inline && !anchor;
const { folded, unfold } = useMapPanelFold("instrument", isMapPanel);
const [headerHeight, setHeaderHeight] = useState<number>(
HEADER_HEIGHT.compact,
);
Expand Down Expand Up @@ -141,12 +150,7 @@ export function Instrument(
<Layer
stacking="instrument"
pinned
anchor={{
kind: "screen",
top: PANEL_TOP,
right: PANEL_RIGHT,
bottom: PANEL_BOTTOM,
}}
anchor={{ kind: "screen", ...(anchor ?? MAP_CORNER) }}
passThrough
>
{shell}
Expand Down
4 changes: 3 additions & 1 deletion kit/MessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ function widestBox(wide: boolean, details: boolean): number {
}

/** The severity mark: a glyph on a 32px tile, bevelled in highlight and ink. */
function SeverityTile({ severity }: { severity: MessageSeverity }) {
export function SeverityTile(
{ severity }: { severity: MessageSeverity },
) {
const tile = SEVERITY_TILE[severity];
return (
<Box
Expand Down
33 changes: 33 additions & 0 deletions kit/TextField.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { assertEquals } from "@std/assert";
import type { ReactNode } from "react";
import { TextField } from "./TextField.tsx";
import {
createNativeRoot,
type NativeNode,
nativeNodes,
} from "./primitives/reconciler.ts";

/** The type style the field's input is set in. */
function inputStyle(element: ReactNode): unknown {
const root = createNativeRoot(() => {});
root.renderSync(element);
const field = [...nativeNodes()].find((node: NativeNode) =>
node.type === "bureau-field"
);
root.renderSync(null);
return field?.props.typeStyle;
}

Deno.test("a name being typed is capitalized", () => {
assertEquals(
inputStyle(<TextField value="newtown" placeholder="Up to 16" capitalize />),
"ui-16-capitalize-solid",
);
});

Deno.test("an empty name's placeholder keeps its own case", () => {
assertEquals(
inputStyle(<TextField value="" placeholder="Up to 16" capitalize />),
"ui-16-solid",
);
});
7 changes: 6 additions & 1 deletion kit/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ export const TextField = React.forwardRef<HostNode, TextFieldProps>(
kind={type}
bare
disabled={disabled}
typeStyle={valueType(figures, Boolean(capitalize))}
// An empty field shows its placeholder in this style, and a
// placeholder keeps its own case.
typeStyle={valueType(
figures,
Boolean(capitalize) && String(value) !== "",
)}
ink={disabled ? "ink-disabled" : invalid ? "ink-bad" : "ink"}
align={EDGES[edge]}
grow={1}
Expand Down
56 changes: 56 additions & 0 deletions kit/i18n/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,62 @@ Deno.test("a language change re-renders mounted native text", async () => {
root.renderSync(null);
});

const winner = {
en: {
common: {
won: "{player, select, she {She won} he {He won} other {They won}}",
},
},
};

Deno.test("a default argument reaches a select whose call passes none", () => {
const i18n = createInstance({ resources: winner });
i18n.setDefaultArgument("player", "she");
assertEquals(i18n.t("won"), "She won");
});

Deno.test("a call's own argument wins over the default", () => {
const i18n = createInstance({ resources: winner });
i18n.setDefaultArgument("player", "she");
assertEquals(i18n.t("won", { player: "he" }), "He won");
});

Deno.test("with no default argument a select reads other", () => {
const i18n = createInstance({ resources: winner });
assertEquals(i18n.t("won"), "They won");
});

Deno.test("a default argument set to undefined is taken away", () => {
const i18n = createInstance({ resources: winner });
i18n.setDefaultArgument("player", "she");
i18n.setDefaultArgument("player", undefined);
assertEquals(i18n.t("won"), "They won");
assertEquals(i18n.defaultArguments, {});
});

Deno.test("a default argument re-renders mounted native text", async () => {
const i18n = createInstance({ resources: winner });
function Caption() {
const { t } = useTranslation("common", { i18n });
return createElement(Text, null, t("won"));
}
const commits: MutationStream[] = [];
const root = createNativeRoot((stream) => commits.push(stream));
root.renderSync(createElement(Caption));
i18n.setDefaultArgument("player", "he");
await new Promise((resolve) => setTimeout(resolve, 0));
assertEquals(commits.at(-1)?.strings.includes("He won"), true);
root.renderSync(null);
});

Deno.test("a default argument leaves languageChanged listeners alone", () => {
const i18n = createInstance({ resources: winner });
const languages: string[] = [];
i18n.on("languageChanged", (language) => languages.push(language));
i18n.setDefaultArgument("player", "she");
assertEquals(languages, []);
});

Deno.test("context candidates prefer their English before the general message", () => {
const i18n = createInstance({
lng: "es",
Expand Down
Loading
Loading