mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* windows: default the dictation shortcut to the left alt key Alt sits under the thumb where Option does on a Mac, so holding it to dictate stops straining the pinky for Ctrl+Space (#2278). The left Alt only: Windows sends AltGr as a synthesized Left Ctrl plus the Right Alt, so an either-side Alt would press on every AltGr character, start a capture, and cancel it when the character joined. A saved shortcut stays as saved; there is no migration. The site's hero key and its "Hold …" line follow the selected install tab (Option, Alt, Alt+Space), and its keycap names that key. * docs: say the alt chord discards before the hold threshold * hotkeys: only Linux still builds a Control modifier helper * tests: pass the windows suite with its new default and CRLF checkout The playground list test compared the first story to the second through the dictation shortcut, which is now the left Alt on Windows, the same binding the Left Option shortcut story sets; it steps to the third story (Toggle cancel) instead. The bundled skill and the Swift instructions tests normalize CRLF, which a Windows checkout writes into include_str!. * windows: a modifier outside the shortcut joins as a chord A modifier-only hold (Left Alt by default) treated Shift, Ctrl, Win, or the other Alt going down as its release: Hold finished and pasted, Toggle locked a recording, and Left Alt+Shift injected the 0xE8 mask key before Alt's release, breaking Windows' input-language switch. It is a chord like any key now: the press cancels (Labs › Hold through other keys keeps a hold a second or more old), Toggle locks nothing, and Alt's release is the app's, so Ctrl+Alt+V still fires Paste Last. A modifier the chord names is part of it, whichever order the keys go down in. The modifier-event policy moved to input/modifier_chord.rs, and the new tests to input/tests/chords.rs, so input.rs stays under its cap. * site: the show's keycap names the selected OS's shortcut The animation's shattering keycap was always the Mac's option key. It takes the same lowercase legend the hero keycap shows now: option with the glyph, alt, or alt + space. * tests: import the right shift key the chords hold * docs: a joined hold leaves alt release to the app * docs: a swallowed key re-arms alt's release mask A modifier joining a Left Alt hold leaves Alt's release to the app, but only when Hex swallowed nothing. Paste Last's V is Hex's key, so the mask comes back and the app never reads a lone Alt tap. Say so in windows.md and the feature map, and name the test that holds it. * site: keep the keycap legend on the dish's bottom row With the Mac's glyph omitted on Windows and Linux, the label was the dish's only child and space-between put it at the top, while the canvas shatter draws the legend at the bottom. An auto top margin keeps both in the same place; the Mac's glyph and label are unchanged. * docs: qualify alt's release after a joined hold Alt's release is the app's after a joining modifier cancels the hold, but not after Hex swallows a key of the chord: Paste Last's V re-arms the mask. Say so in the hook and test comments, and stop claiming every Hold-mode dictation finishes when a tap or chord discards. * tests: one test at a time drives the windows hook's global state Paste Last's Ctrl+Alt+V does not leave Alt's release unmasked: its V is Hex's key, so that chord masks (#1254). Only a hold a joining modifier cancelled leaves the release to the app. The docs say so now. Under plain `cargo test` the hook tests share one process, so a static test lock serializes everything that installs, drives, or clears CALLBACK_STATE. --------- Co-authored-by: opencode <opencode@anoma.ly>
268 lines
9.2 KiB
TypeScript
268 lines
9.2 KiB
TypeScript
import { useCallback, useEffect, useRef, useState, type RefObject } from "react"
|
|
import { setGlow } from "./glow"
|
|
|
|
// The hero show lives in ./shout (plain JS, WebGPU with a Canvas 2D fallback). The prerendered
|
|
// page shows a still of the idle head; the engine loads when the browser is idle or the visitor
|
|
// reaches for the key, takes over the same box, and only fetches audio once they press it.
|
|
|
|
type Engine = {
|
|
renderer: string
|
|
pressKey?(pressed: boolean, rect?: DOMRect | null): void
|
|
explodeKey?(rect?: DOMRect | null, key?: string): void
|
|
start(options: { context: AudioContext | null; muted: boolean; keyRect?: DOMRect | null; key?: string }): Promise<void>
|
|
setMuted(muted: boolean, context?: AudioContext | null): void
|
|
destroy(): void
|
|
}
|
|
export type Phase = "idle" | "live" | "starting" | "started"
|
|
|
|
const MUTED_KEY = "hex-shout-muted"
|
|
const readMuted = () => {
|
|
try {
|
|
return localStorage.getItem(MUTED_KEY) === "1"
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
const newContext = () => {
|
|
try {
|
|
return new AudioContext({ latencyHint: "interactive" })
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function useShout(root: RefObject<HTMLElement | null>, canvas: RefObject<HTMLCanvasElement | null>, head: RefObject<HTMLElement | null>) {
|
|
const [phase, setPhase] = useState<Phase>("idle")
|
|
const [muted, setMutedState] = useState(false)
|
|
const engine = useRef<Promise<Engine | null> | null>(null)
|
|
const live = useRef<Engine | null>(null)
|
|
const reduced = useRef(false)
|
|
|
|
const load = useCallback(() => {
|
|
if (!engine.current) {
|
|
const params = new URLSearchParams(location.search)
|
|
engine.current = import("./shout/main.js")
|
|
.then(({ mountShout }) =>
|
|
mountShout({
|
|
root: root.current,
|
|
canvas: canvas.current,
|
|
head: head.current,
|
|
reduced: reduced.current,
|
|
forceCanvas2d: params.has("canvas2d"),
|
|
}),
|
|
)
|
|
.then((e: Engine) => {
|
|
live.current = e
|
|
// The canvas has drawn the head; the still can go.
|
|
root.current?.setAttribute("data-live", "")
|
|
setPhase((p) => (p === "idle" ? "live" : p))
|
|
return e
|
|
})
|
|
.catch(() => null)
|
|
}
|
|
return engine.current
|
|
}, [root, canvas, head])
|
|
|
|
useEffect(() => {
|
|
setMutedState(readMuted())
|
|
reduced.current = matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
// Reduced motion and phones keep the still until the visitor asks for the show: building
|
|
// the renderer is a long task on a phone CPU.
|
|
if (reduced.current || matchMedia("(pointer: coarse), (max-width: 40rem)").matches) return
|
|
const idle = "requestIdleCallback" in window ? requestIdleCallback(() => void load(), { timeout: 2500 }) : setTimeout(() => void load(), 1200)
|
|
return () => {
|
|
if ("cancelIdleCallback" in window) cancelIdleCallback(idle as number)
|
|
else clearTimeout(idle as ReturnType<typeof setTimeout>)
|
|
}
|
|
}, [load])
|
|
|
|
useEffect(() => () => live.current?.destroy(), [])
|
|
|
|
const context = useRef<AudioContext | null>(null)
|
|
// Building an AudioContext blocks the main thread for tens of milliseconds, so it is made when the
|
|
// visitor reaches for the key; the click still resumes it, inside the gesture.
|
|
const intent = useCallback(() => {
|
|
if (!readMuted()) context.current ??= newContext()
|
|
void load()
|
|
}, [load])
|
|
|
|
const pressKey = useCallback((down: boolean, el?: HTMLElement | null) => {
|
|
live.current?.pressKey?.(down, el?.getBoundingClientRect() ?? null)
|
|
}, [])
|
|
|
|
// Must run inside the click: that gesture is what lets the browser play audio.
|
|
const start = useCallback(
|
|
(el?: HTMLElement | null, key = "option") => {
|
|
if (phase === "starting" || phase === "started") return
|
|
const quiet = readMuted()
|
|
if (!quiet) context.current ??= newContext()
|
|
const resumed = context.current?.resume()
|
|
const keyRect = el?.getBoundingClientRect() ?? null
|
|
setPhase("starting")
|
|
if (live.current && !reduced.current) {
|
|
root.current?.setAttribute("data-key-exploded", "")
|
|
live.current.explodeKey?.(keyRect, key)
|
|
}
|
|
void load().then(async (e) => {
|
|
if (!e) return setPhase("idle")
|
|
if (!reduced.current) root.current?.setAttribute("data-key-exploded", "")
|
|
// A context made before the gesture is still suspended; the engine only sounds once it runs.
|
|
if (resumed && context.current?.state !== "running") await Promise.race([resumed.catch(() => {}), new Promise((r) => setTimeout(r, 250))])
|
|
await e.start({ context: context.current, muted: quiet, keyRect, key })
|
|
setPhase("started")
|
|
})
|
|
},
|
|
[phase, load, root],
|
|
)
|
|
|
|
const toggleMuted = useCallback(() => {
|
|
const next = !muted
|
|
setMutedState(next)
|
|
try {
|
|
localStorage.setItem(MUTED_KEY, next ? "1" : "0")
|
|
} catch {}
|
|
if (!next) {
|
|
context.current ??= newContext()
|
|
void context.current?.resume()
|
|
}
|
|
live.current?.setMuted(next, context.current)
|
|
}, [muted])
|
|
|
|
return { phase, muted, start, intent, pressKey, toggleMuted }
|
|
}
|
|
|
|
export function OptionKey({
|
|
os,
|
|
phase,
|
|
onStart,
|
|
onIntent,
|
|
onPress,
|
|
}: {
|
|
// The hero's key names the selected OS's shortcut: Option on the Mac, Alt
|
|
// on Windows, and Alt+Space on Linux, which binds no modifier alone.
|
|
os: "mac" | "windows" | "linux"
|
|
phase: Phase
|
|
onStart(el?: HTMLElement | null): void
|
|
onIntent(): void
|
|
onPress?(down: boolean, el?: HTMLElement | null): void
|
|
}) {
|
|
const ref = useRef<HTMLButtonElement>(null)
|
|
const [pressed, setPressed] = useState(false)
|
|
const gone = phase === "started"
|
|
// The glow pulses (./glow.ts) from here until the show's engine draws the head and takes it over. A timer waits
|
|
// out each 1/30 s and the value is set in the next frame callback, so it lands on that vsync's frame.
|
|
const pulsing = phase === "idle"
|
|
useEffect(() => {
|
|
const el = ref.current
|
|
if (!el || !pulsing || matchMedia("(prefers-reduced-motion: reduce)").matches) return
|
|
let timer = 0
|
|
let raf = 0
|
|
let onScreen = true
|
|
const frame = (now: number) => {
|
|
raf = 0
|
|
setGlow(el, now)
|
|
// A frame and a half early: asked for from idle, a frame's slot is already under way (see shout/main.js).
|
|
if (!document.hidden && onScreen) timer = window.setTimeout(next, 1000 / 30 - 10)
|
|
}
|
|
const next = () => {
|
|
timer = 0
|
|
raf = requestAnimationFrame(frame)
|
|
}
|
|
const resume = () => {
|
|
if (!timer && !raf && !document.hidden && onScreen) next()
|
|
}
|
|
const io = new IntersectionObserver(([e]) => {
|
|
onScreen = e.isIntersecting
|
|
resume()
|
|
})
|
|
io.observe(el)
|
|
document.addEventListener("visibilitychange", resume)
|
|
next()
|
|
return () => {
|
|
clearTimeout(timer)
|
|
cancelAnimationFrame(raf)
|
|
io.disconnect()
|
|
document.removeEventListener("visibilitychange", resume)
|
|
}
|
|
}, [pulsing])
|
|
|
|
const setDown = (down: boolean) => {
|
|
setPressed(down)
|
|
onPress?.(down, ref.current)
|
|
}
|
|
|
|
return (
|
|
<button
|
|
ref={ref}
|
|
type="button"
|
|
className="keycap"
|
|
data-pressed={pressed || phase === "starting" || undefined}
|
|
data-gone={gone || undefined}
|
|
aria-label="Play the demo with sound"
|
|
tabIndex={gone ? -1 : undefined}
|
|
aria-hidden={gone || undefined}
|
|
onPointerEnter={onIntent}
|
|
onFocus={onIntent}
|
|
onPointerDown={() => {
|
|
onIntent()
|
|
setDown(true)
|
|
}}
|
|
onPointerUp={() => setDown(false)}
|
|
onPointerLeave={() => {
|
|
if (pressed) setDown(false)
|
|
}}
|
|
onPointerCancel={() => setDown(false)}
|
|
onBlur={() => {
|
|
if (pressed) setDown(false)
|
|
}}
|
|
onKeyDown={(e) => {
|
|
if ((e.key === " " || e.key === "Enter") && !e.repeat) {
|
|
onIntent()
|
|
setDown(true)
|
|
}
|
|
}}
|
|
onKeyUp={(e) => {
|
|
if (e.key === " " || e.key === "Enter") setDown(false)
|
|
}}
|
|
onClick={() => onStart(ref.current)}
|
|
>
|
|
<span className="keycap__glow" aria-hidden="true" />
|
|
<span className="keycap__well" aria-hidden="true" />
|
|
<span className="keycap__cap" aria-hidden="true">
|
|
<span className="keycap__dish">
|
|
{os === "mac" && (
|
|
<svg viewBox="0 0 24 18" className="keycap__glyph">
|
|
<path d="M2.5 3.5h5.2l6.2 11h7.6M14.2 3.5h7.3" />
|
|
</svg>
|
|
)}
|
|
<span className="keycap__label">
|
|
{os === "linux" ? (
|
|
<>
|
|
alt<span className="keycap__chord">+ space</span>
|
|
</>
|
|
) : os === "windows" ? (
|
|
"alt"
|
|
) : (
|
|
"option"
|
|
)}
|
|
</span>
|
|
</span>
|
|
</span>
|
|
<span className="keycap__hint" aria-hidden="true">
|
|
<span className="keycap__hint-fine">Click to play</span>
|
|
<span className="keycap__hint-touch">Tap to play</span>
|
|
</span>
|
|
</button>
|
|
)
|
|
}
|
|
|
|
export function SoundToggle({ muted, onToggle }: { muted: boolean; onToggle(): void }) {
|
|
return (
|
|
<button type="button" className="sound" aria-pressed={!muted} aria-label={muted ? "Unmute" : "Mute"} onClick={onToggle}>
|
|
<svg viewBox="0 0 20 20" aria-hidden="true">
|
|
<path d="M3 8v4h3l4 3V5L6 8z" />
|
|
{muted ? <path d="m13.5 8 4 4m0-4-4 4" fill="none" /> : <path d="M13 7.5a3.5 3.5 0 0 1 0 5M15.2 5.5a6.5 6.5 0 0 1 0 9" fill="none" />}
|
|
</svg>
|
|
</button>
|
|
)
|
|
}
|