diff --git a/kit/Bureau.tsx b/kit/Bureau.tsx index 32d0aac..e56e191 100644 --- a/kit/Bureau.tsx +++ b/kit/Bureau.tsx @@ -615,7 +615,15 @@ export type ButtonProps = { */ layout?: Pick< Layout, - "width" | "margin" | "padding" | "gap" | "grow" | "shrink" | "minWidth" + | "width" + | "height" + | "margin" + | "padding" + | "gap" + | "grow" + | "shrink" + | "minWidth" + | "minHeight" >; /** Takes the room left in its row: a lone key across a strip. */ grow?: true; diff --git a/kit/HudKey.tsx b/kit/HudKey.tsx index 6b63f41..176410b 100644 --- a/kit/HudKey.tsx +++ b/kit/HudKey.tsx @@ -24,7 +24,7 @@ import { Box, Image, Layer, type PixelSource, Text } from "bureau/primitives"; const HUD_CORNER_INSET = 6; /** The room one corner key takes from the window's side: what a strip - * across the bottom, the toolbox's shelf, leaves it. */ + * across the bottom, the toolbox's touch drawer, leaves it. */ export const HUD_CORNER_WIDTH = HUD_CORNER_INSET + CONTROL_METRICS.large.height; /** A corner of the map: its keys centred 28px up, where the toolbox's tiles diff --git a/kit/Shelf.test.ts b/kit/Shelf.test.ts deleted file mode 100644 index 3593339..0000000 --- a/kit/Shelf.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { assertEquals } from "@std/assert"; -import { cardLeft, shelfOrder } from "./Shelf.tsx"; - -Deno.test("the shelf keeps a drawer's usable tools in their order", () => { - assertEquals( - shelfOrder<{ key: string; withheld?: boolean }>([ - { key: "a" }, - { key: "b" }, - { key: "c" }, - ]).map((i) => i.key), - ["a", "b", "c"], - ); -}); - -Deno.test("the shelf sets the withheld tools after the usable ones", () => { - assertEquals( - shelfOrder([ - { key: "tower", withheld: true }, - { key: "house" }, - { key: "block", withheld: true }, - { key: "cottage" }, - ]).map((i) => i.key), - ["house", "cottage", "tower", "block"], - ); -}); - -Deno.test("a card stands centred over its key", () => { - assertEquals( - cardLeft({ - keyCentre: 300, - cardWidth: 200, - shelfLeft: 100, - safeLeft: 0, - safeRight: 800, - }), - 200, - ); -}); - -Deno.test("a card over the first key stays a gutter inside the window", () => { - assertEquals( - cardLeft({ - keyCentre: 50, - cardWidth: 200, - shelfLeft: 20, - safeLeft: 0, - safeRight: 800, - }), - -4, - ); -}); - -Deno.test("a card over the last key stays a gutter inside the window", () => { - assertEquals( - cardLeft({ - keyCentre: 600, - cardWidth: 200, - shelfLeft: 100, - safeLeft: 0, - safeRight: 800, - }), - 484, - ); -}); - -Deno.test("a card over the first key keeps clear of a phone's notch", () => { - assertEquals( - cardLeft({ - keyCentre: 50, - cardWidth: 200, - shelfLeft: 120, - safeLeft: 59, - safeRight: 800, - }), - -45, - ); -}); diff --git a/kit/Shelf.tsx b/kit/Shelf.tsx deleted file mode 100644 index 3e5b6f2..0000000 --- a/kit/Shelf.tsx +++ /dev/null @@ -1,255 +0,0 @@ -/** - * Shelf — the dock's drawer for a finger. A tooltip is read by hovering, - * and a finger never hovers, so a drawer opened by touch takes the dock's - * place as one row of its keys, and the chosen key's card stands above it - * carrying what the tooltip would have said, with the key that acts on it. - * - * The row scrolls under a swipe; the arrow keys either side step it a page. - * A tap chooses; only the card's action key builds or opens. Keys the city - * may not use yet stand after the rest, and can still be chosen to read why. - */ - -import React, { useLayoutEffect, useRef, useState } from "react"; -import { host, type HostNode } from "bureau/host"; -import { Box, Scroll, Text } from "bureau/primitives"; -import { useTranslation } from "./i18n/i18n.ts"; -import { Button, CloseButton } from "./Bureau.tsx"; - -export interface ShelfItem { - key: string; - icon: React.ReactNode; - /** Not usable yet: shelved after the rest. */ - withheld?: boolean; - /** Something new behind it, as the dock's badge says. */ - badge?: React.ReactNode; -} - -export interface ShelfAction { - label: string; - onPress: () => void; -} - -/** The strip's and the card's ids: what a tutor's bubble keeps clear of. */ -export const SHELF_ID = "bureau-shelf"; -export const SHELF_CARD_ID = "bureau-shelf-card"; - -/** The key edge, the gap between keys and the strip's inset: the drawer's. */ -const KEY_SIZE = 44; -const KEY_GAP = 5; -const STRIP_INSET = 4; -/** Air between the card and the strip, and the card and the safe area's side. */ -const CARD_GAP = 6; -const SAFE_GUTTER = 16; -/** 350px of text and the 12px insets either side, as the floating hint. */ -const CARD_MAX_WIDTH = 374; - -/** The usable keys first, then the withheld, each in the drawer's order. */ -export function shelfOrder( - items: readonly T[], -): T[] { - const usable = items.filter((item) => !item.withheld); - const withheld = items.filter((item) => item.withheld); - return [...usable, ...withheld]; -} - -/** - * The card's left edge, in the shelf's pixels: centred over its key, but - * kept a gutter inside the safe area, whose sides are in the shelf's frame. - */ -export function cardLeft( - place: { - keyCentre: number; - cardWidth: number; - shelfLeft: number; - safeLeft: number; - safeRight: number; - }, -): number { - const centred = place.keyCentre - place.cardWidth / 2; - const leftmost = place.safeLeft + SAFE_GUTTER - place.shelfLeft; - const rightmost = place.safeRight - SAFE_GUTTER - place.cardWidth - - place.shelfLeft; - return Math.max(leftmost, Math.min(centred, rightmost)); -} - -interface CardPlace { - left: number; - bottom: number; -} - -interface StepsOpen { - back: boolean; - forward: boolean; -} - -export function Shelf( - { title, items, chosen, onChoose, card, action, onClose }: { - /** The drawer's name, for the node tree. */ - title: string; - items: readonly ShelfItem[]; - /** The chosen key's `key`; the first key when it names none. */ - chosen: string | null; - onChoose: (key: string) => void; - /** What the chosen key's tooltip says. */ - card: React.ReactNode; - /** The chosen key's act; none while it is withheld. */ - action?: ShelfAction; - onClose: () => void; - }, -) { - const { t } = useTranslation("common"); - const shelved = shelfOrder(items); - const chosenKey = shelved.some((item) => item.key === chosen) - ? chosen - : shelved[0]?.key ?? null; - - const shelf = useRef(null); - const scroller = useRef(null); - const plate = useRef(null); - const keys = useRef(new Map()); - // Where the row stands, so the card follows its key as the row scrolls. - const [scrolledTo, setScrolledTo] = useState(0); - const [place, setPlace] = useState(null); - const [steps, setSteps] = useState({ - back: false, - forward: false, - }); - - // Measured after every commit: the card's text, the chosen key and the - // row's scroll all move it. Each state changes only when its value does. - useLayoutEffect(() => { - const shelfNode = shelf.current; - const scrollNode = scroller.current; - const plateNode = plate.current; - const keyNode = chosenKey === null ? null : keys.current.get(chosenKey); - if (!shelfNode || !scrollNode) return; - - const extent = host.contentExtent(scrollNode); - const back = scrolledTo > 0; - const forward = scrolledTo + extent.width < extent.contentWidth - 1; - if (back !== steps.back || forward !== steps.forward) { - setSteps({ back, forward }); - } - - if (!plateNode || !keyNode) return; - const shelfRect = host.measure(shelfNode); - const keyRect = host.measure(keyNode); - const plateRect = host.measure(plateNode); - const safe = host.viewport(); - const left = cardLeft({ - keyCentre: keyRect.left + keyRect.width / 2 - shelfRect.left, - cardWidth: plateRect.width, - shelfLeft: shelfRect.left, - safeLeft: safe.left, - safeRight: safe.left + safe.width, - }); - const bottom = shelfRect.height + CARD_GAP; - if (place?.left !== left || place?.bottom !== bottom) { - setPlace({ left, bottom }); - } - }); - - // Arrows only for a row that overflows: a drawer that fits has nowhere to go. - const scrolls = steps.back || steps.forward; - - function step(direction: -1 | 1) { - const scrollNode = scroller.current; - if (!scrollNode) return; - const { width } = host.contentExtent(scrollNode); - // A page less one key, so the key at the edge stays in sight. - const page = Math.max(KEY_SIZE, width - KEY_SIZE - KEY_GAP); - host.scrollBy(scrollNode, direction * page, 0); - } - - return ( - { - shelf.current = node; - }} - id={SHELF_ID} - label={title} - shrink={1} - minWidth={0} - surface="drawer" - padding={STRIP_INSET} - gap={KEY_GAP} - align="center" - > - {scrolls && ( - - ))} - - - {scrolls && ( - - - )} - - )} - - ); -} diff --git a/kit/Tabs.tsx b/kit/Tabs.tsx index 5b2c633..fbb666e 100644 --- a/kit/Tabs.tsx +++ b/kit/Tabs.tsx @@ -15,7 +15,7 @@ import { getFocusedNode, host, type HostNode } from "bureau/host"; * reading a different one. * * THE TAB'S FORM is a trapezoid slanted 10px on both sides, Mac OS 8's tab - * panel: an ink shape with the face laid a pixel inside it, open at the + * panel: a shape in the frame's line with the face laid a pixel inside it, open at the * bottom, so the open tab and the page read as one piece of card. The host * draws the card whole, a slanted box, at the screen's own pixels. Each tab is * pulled left by both its slants, so neighbours interlock the way filed card @@ -116,10 +116,20 @@ function TabFocusMark({ height }: { height: number }) { ); } +/** + * The fill of the page a strip is filed in, which its tabs are cut from: a + * window's face, or the paper of an instrument on the map. + */ +type TabGround = "face" | "paper"; + +/** What a filed tab lifts to under the pointer, a step off its ground. */ +const LIFTED: Record = { face: "face-light", paper: "face" }; + function TabKey( { stripId, tab, + ground, selected, tabStop, interlocks, @@ -131,6 +141,7 @@ function TabKey( }: { stripId: string; tab: Tab; + ground: TabGround; selected: boolean; tabStop: boolean; interlocks: boolean; @@ -147,7 +158,7 @@ function TabKey( const drop = selected ? 0 : FILED_CARD_DROP; // Card filed behind the page lifts under the pointer; the open tab never // changes at all. - const face: Ink = hovered && !selected ? "face-light" : "face"; + const face: Ink = hovered && !selected ? LIFTED[ground] : ground; const { badge, hint } = tab; return ( ( - { id, tabs, activeId, onSelect }: TabsProps, + { id, tabs, activeId, onSelect, ground = "face" }: + & TabsProps + & { + /** The fill of the page the strip is filed in; a window's face unsaid. */ + ground?: TabGround; + }, ) { const inputMode = useHintDevice(); const showGamepadHints = inputMode === "gamepad"; @@ -283,6 +299,7 @@ export function Tabs( key={tab.id} stripId={id} tab={tab} + ground={ground} selected={tab.id === activeId} tabStop={index === rovingIndex} interlocks={index > 0} diff --git a/kit/TouchDrawer.test.tsx b/kit/TouchDrawer.test.tsx new file mode 100644 index 0000000..66e80c9 --- /dev/null +++ b/kit/TouchDrawer.test.tsx @@ -0,0 +1,124 @@ +import { assertEquals } from "@std/assert"; +import { TouchDrawer } from "./TouchDrawer.tsx"; +import { createNativeRoot, nativeNodes } from "./primitives/reconciler.ts"; + +const SECTIONS = [ + { id: "shops", label: "Shops" }, + { id: "farms", label: "Farms" }, +]; + +type Drawn = Partial[0]>; + +/** The nodes of a drawer on its second tool of three, but for `changes`. */ +function drawn(changes: Drawn = {}) { + const root = createNativeRoot(() => {}); + root.renderSync( + {}} + onTake={() => {}} + onClose={() => {}} + {...changes} + />, + ); + const nodes = [...nativeNodes()]; + root.renderSync(null); + return nodes; +} + +function node(nodes: ReturnType, id: string) { + return nodes.find((candidate) => candidate.props.id === id); +} + +Deno.test("the forward key steps on a tool", () => { + const steps: number[] = []; + const nodes = drawn({ onStep: (direction) => steps.push(direction) }); + (node(nodes, "touch-drawer-forward")!.props.onPress as () => void)(); + assertEquals(steps, [1]); +}); + +Deno.test("the back key steps back a tool", () => { + const steps: number[] = []; + const nodes = drawn({ onStep: (direction) => steps.push(direction) }); + (node(nodes, "touch-drawer-back")!.props.onPress as () => void)(); + assertEquals(steps, [-1]); +}); + +Deno.test("a drawer of one tool has dead step keys", () => { + const nodes = drawn({ place: { index: 0, count: 1 } }); + assertEquals( + { + back: node(nodes, "touch-drawer-back")!.props.disabled, + forward: node(nodes, "touch-drawer-forward")!.props.disabled, + }, + { back: true, forward: true }, + ); +}); + +Deno.test("the page says which tool of how many it shows", () => { + const counter = drawn().find((candidate) => candidate.text === "2 / 3"); + assertEquals(counter?.text, "2 / 3"); +}); + +Deno.test("a drawer of one tool counts nothing", () => { + const nodes = drawn({ place: { index: 0, count: 1 } }); + const counter = nodes.find((candidate) => candidate.text === "1 / 1"); + assertEquals(counter, undefined); +}); + +Deno.test("the picture is the key that takes the tool", () => { + let taken = 0; + const nodes = drawn({ onTake: () => taken++ }); + (node(nodes, "touch-drawer-take")!.props.onPress as () => void)(); + assertEquals(taken, 1); +}); + +Deno.test("the picture of a tool the city may take is a live key", () => { + assertEquals(node(drawn(), "touch-drawer-take")!.props.disabled, false); +}); + +Deno.test("a withheld tool's picture is a dead key", () => { + const nodes = drawn({ withheld: true }); + assertEquals(node(nodes, "touch-drawer-take")!.props.disabled, true); +}); + +Deno.test("the close key closes the drawer", () => { + let closed = 0; + const nodes = drawn({ onClose: () => closed++ }); + (node(nodes, "touch-drawer-close")!.props.onPress as () => void)(); + assertEquals(closed, 1); +}); + +Deno.test("a drawer with sections wears a tab for each", () => { + const nodes = drawn({ tabs: SECTIONS, activeTab: "farms" }); + assertEquals( + { + shops: node(nodes, "touch-drawer-tabs-shops")?.props.selected, + farms: node(nodes, "touch-drawer-tabs-farms")?.props.selected, + }, + { shops: false, farms: true }, + ); +}); + +Deno.test("a tab's press asks for its section", () => { + const asked: string[] = []; + const nodes = drawn({ + tabs: SECTIONS, + activeTab: "farms", + onSelectTab: (id) => asked.push(id), + }); + (node(nodes, "touch-drawer-tabs-shops")!.props.onPress as () => void)(); + assertEquals(asked, ["shops"]); +}); + +Deno.test("a drawer with no sections wears no tabs", () => { + assertEquals(node(drawn(), "touch-drawer-tabs"), undefined); +}); + +Deno.test("a drawer with one section wears no tabs", () => { + const nodes = drawn({ tabs: [SECTIONS[0]!], activeTab: "shops" }); + assertEquals(node(nodes, "touch-drawer-tabs"), undefined); +}); diff --git a/kit/TouchDrawer.tsx b/kit/TouchDrawer.tsx new file mode 100644 index 0000000..2b3b635 --- /dev/null +++ b/kit/TouchDrawer.tsx @@ -0,0 +1,213 @@ +/** + * TouchDrawer — the dock's drawer for a finger. A tooltip is read by + * hovering, and a finger never hovers, so a drawer opened by touch takes the + * dock's place and shows one of its tools at a time, with everything the + * tooltip would have said printed in the open: the tool's name, its copy + * across the page and its figures under it. The tool's picture is the key + * that takes it, the biggest key on the page. + * + * The arrow keys either side step through the tools. A drawer that files its + * tools under sections wears them as folder tabs, and the arrows step through + * the open tab's tools alone. + */ + +import type React from "react"; +import { Box, Text } from "bureau/primitives"; +import { useTranslation } from "./i18n/i18n.ts"; +import { Button, CloseButton } from "./Bureau.tsx"; +import { type Tab, Tabs } from "./Tabs.tsx"; + +/** The drawer's id: what a tutor's bubble keeps clear of. */ +export const TOUCH_DRAWER_ID = "touch-drawer"; + +/** As wide as a phone held sideways leaves between its corner keys. */ +export const TOUCH_DRAWER_MAX_WIDTH = 720; + +/** The page's inset, and the air between its columns. */ +const PAGE_INSET = 8; +const COLUMN_GAP = 10; +/** The picture's key: 96px of art, three screen pixels to each of its own, + * and the key's face round it. */ +const PICTURE_KEY_SIZE = 96 + 2 * 8; +/** The figures' column: room for a card row's longest label beside its + * figure, and no wider, so the two still read as one line. */ +const FIGURES_WIDTH = 340; +/** A drawer key's edge, and the compact close key's hit area. */ +const KEY_SIZE = 44; +const CLOSE_SIZE = 36; +/** Tall enough that the close key clears the arrow centred under it. */ +const PAGE_MIN_HEIGHT = 2 * (CLOSE_SIZE + 6) + KEY_SIZE; +/** How far an arrow stands off the page's foot: centred in the shortest + * page, and no higher in a taller one, so it holds still under a thumb + * that steps through pages of different heights. */ +const ARROW_LIFT = (PAGE_MIN_HEIGHT - KEY_SIZE) / 2; + +/** The tool on show's place among the tools its arrows step through. */ +export interface TouchDrawerPlace { + /** From 0. */ + index: number; + count: number; +} + +export function TouchDrawer( + { + title, + tabs = [], + activeTab, + onSelectTab, + name, + picture, + place, + onStep, + figures, + onTake, + withheld = false, + onClose, + children, + }: { + /** The drawer's name, for the node tree. */ + title: string; + /** The drawer's sections; fewer than two draw no strip. */ + tabs?: readonly Tab[]; + activeTab?: TabId; + onSelectTab?: (id: TabId) => void; + /** The tool on show: its name and its picture, with any badge it wears. */ + name: React.ReactNode; + picture: React.ReactNode; + /** The picture pressed: the tool is taken. */ + onTake: () => void; + /** The city may not take the tool yet: its picture stands dead. */ + withheld?: boolean; + place: TouchDrawerPlace; + /** Back or forward one tool, round the ends. */ + onStep: (direction: -1 | 1) => void; + /** The tool's rows of figures, set under its copy. */ + figures?: React.ReactNode; + onClose: () => void; + /** The tool's copy, a line each: what it is, and what the player + * should know of it. */ + children?: React.ReactNode; + }, +) { + const { t } = useTranslation("common"); + const filed = tabs.length > 1 && activeTab !== undefined; + const steps = place.count > 1; + + return ( + + {filed && ( + + onSelectTab?.(id)} + /> + + )} + + + + + + + {name} + + {steps && ( + + {`${place.index + 1} / ${place.count}`} + + )} + + {children && ( + + {children} + + )} + {figures && ( + + {figures} + + )} + + +