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
8 changes: 8 additions & 0 deletions .changes/themes/fix-bedroom-click-areas-and-card-4b9e2d71.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bump: patch
---

### Fixed

- A click on the console's right edge also started the tape, and a click where the lava lamp meets the wall light flipped both. The console, the tape's spine and the lava lamp now take clicks over slightly narrower areas, so every click flips one thing.
- While another plugin's theme covered the room, the settings card still said `theme: on` and its theme button cleared that theme instead of bringing the room back. The card now says `theme: covered` and its button puts the room back; `/bedroom on` does the same, and `/bedroom status` says which plugin's theme covers the room.
7 changes: 4 additions & 3 deletions plugins/themes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Changing the time or the weather by command shows within a few seconds and eases
### The settings card

`/bedroom` alone shows a card in the conversation, so you need not know the words. It is six rows
whatever is open, so it fits without scrolling: the theme on or off, reset, calibrate, titles and
whatever is open, so it fits without scrolling: the theme on, off or covered (another plugin's
theme stands over the room; a press puts the room back), reset, calibrate, titles and
Close; a button for each switch showing whether it is on, then `pass` (someone passes the door),
`door` (the hall light's switch, checked while the light is on; the next press turns it off) and
`bolt` (a lightning strike), each dim while its command would only say why not, the hallway off or
Expand Down Expand Up @@ -176,7 +177,7 @@ clicks are answered at once. A click in those seven seconds still counts and is
| Command | Does |
|---|---|
| `/bedroom` | Says what is on and off; in the desktop app, also shows the settings card |
| `/bedroom status` | Says what is on and off |
| `/bedroom status` | Says what is on and off, and whose theme covers the room, if another plugin's does |
| `/bedroom <tv\|tape\|console\|pc\|light\|lamp\|lava> [on\|off]` | Switches one thing; with neither word it flips. A switch already in that state stays as it is |
| `/bedroom time <auto\|dusk\|evening\|night\|predawn\|dawn\|morning\|noon\|golden\|HH:MM>` | Follows the clock, or holds a phase (`HH:MM` holds the phase of that hour); alone, says which |
| `/bedroom date <auto\|MM-DD\|spring\|summer\|autumn\|winter>` | Follows the real date, or holds a day of the year for sunrise and sunset (the seasons hold 04-20, 07-15, 10-15, 01-15); alone, says which |
Expand All @@ -199,7 +200,7 @@ clicks are answered at once. A click in those seven seconds still counts and is
| `/bedroom stats` | Render counts and timings |
| `/bedroom calibrate` | Fits clicks to your window's shape |
| `/bedroom reset` | Every switch, dim, glass and blur back to its default, and time, weather, lightning, signal, hallway and sound back to the plugin's settings; titles, calibration, volume and the bank's choices stay |
| `/bedroom on`, `/bedroom off` | Sets or clears the theme |
| `/bedroom on`, `/bedroom off` | Sets or clears the theme; `on` also puts the room back over another plugin's theme |

For diagnosis, `/bedroom shader off` (or `instant off`) shows the hand-drawn room alone for the rest
of the session, with no fades, clock or lamp light, sending it again on every change; `/bedroom
Expand Down
6 changes: 3 additions & 3 deletions plugins/themes/hooks/bedroom/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { BedroomCard } from '../../types'
import { TITLE_CAPS, describe, extraMsOf, runCommand, type TitleKey } from './core/commands.js'
import { ctxOf } from './core/context.js'
import { SWITCHES, type Switch } from './core/piece.js'
import { isThemeOn } from './core/render.js'
import { coveringPlugin, isThemeOn } from './core/render.js'
import { volumeOf } from './core/saved.js'
import { WEATHERS } from './core/settings.js'
import { hallLightOn } from './pieces/hallway.js'
Expand Down Expand Up @@ -185,7 +185,7 @@ type CardElements = Pick<ElementTable<'desktop'>, 'Box' | 'Text' | 'Button' | 'I
export function settingsCard($: EngineInterface, { Box, Text, Button, Input }: CardElements, now: BedroomCard) {
const ctx = ctxOf($)
const saved = ctx.saved
const themeLabel = isThemeOn() ? 'theme: on' : 'theme: off'
const themeLabel = coveringPlugin() !== undefined ? 'theme: covered' : isThemeOn() ? 'theme: on' : 'theme: off'
const titlesLabel = now.titles === true ? 'titles: hide' : 'titles: show'
const soundLabel = `sound: ${ctx.setting('sound') ? 'on' : 'off'}`
const soundsLabel = now.sounds === true ? 'sounds: hide' : 'sounds: show'
Expand Down Expand Up @@ -218,7 +218,7 @@ export function settingsCard($: EngineInterface, { Box, Text, Button, Input }: C
<Box key="head" flexDirection="row" flexWrap="wrap" justifyContent="space-between" alignItems="center" columnGap="sm" rowGap="xs">
<Text bold>Bedroom settings</Text>
<Box flexDirection="row" flexWrap="wrap" columnGap="xs" rowGap="xs">
<Button key="theme" label={themeLabel} onPress={() => press($, isThemeOn() ? 'off' : 'on')}>
<Button key="theme" label={themeLabel} onPress={() => press($, isThemeOn() && coveringPlugin() === undefined ? 'off' : 'on')}>
{themeLabel}
</Button>
<Button
Expand Down
4 changes: 2 additions & 2 deletions plugins/themes/hooks/bedroom/core/clicks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ test('a click flips the saved switch, mapped with the stored window shape; with
expect(room.redraws).toEqual([1, 1, 1])
})

test('where two click rectangles overlap a click flips both switches, as the shader does', async () => {
test('a click beside a neighbor flips only the thing under it', async () => {
const aspect = 1.6
const room = standIn({ state: { tv: true, tape: false, console: false, pc: true, light: false, lamp: false, lava: false, posters: 'plain' }, aspect })
await load(room.$)
await handleClick(room.$, inWindow(1340, 560, aspect))
await handleClick(room.$, inWindow(1399, 500, aspect))
await Promise.resolve()
expect((room.store.get(STORE_KEY) as Saved).state).toMatchObject({ console: true, tape: true, lava: true, light: true, tv: true, pc: true, lamp: false })
expect((room.store.get(STORE_KEY) as Saved).state).toMatchObject({ console: false, tape: true, lava: false, light: true, tv: true, pc: true, lamp: false })
})

test('a click at each window shape lands where toViewBox puts it', async () => {
Expand Down
14 changes: 11 additions & 3 deletions plugins/themes/hooks/bedroom/core/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { calibrateNext } from './clicks.js'
import { readNow } from './clock.js'
import { builtPieces, ctxOf } from './context.js'
import { NOT_BUILT, SWITCHES, type Ctx, type Switch } from './piece.js'
import { clearLook, holdShader, isShaderRunning, noteAsked, setLook } from './render.js'
import { clearLook, coveringPlugin, holdShader, isShaderRunning, noteAsked, setLook } from './render.js'
import { defaults } from './saved.js'
import { OVERRIDES, accepts, type Override } from './settings.js'
import { switched } from './switched.js'
Expand Down Expand Up @@ -69,14 +69,22 @@ export async function runCommand($: EngineInterface, args: string): Promise<stri
const rest = words.slice(2).join(' ')
note(`/bedroom ${word}`)

if (word === '' || word === 'status') return describe(ctx)
if (word === '' || word === 'status') {
const covering = coveringPlugin()
return covering === undefined ? describe(ctx) : `the room is covered by the ${covering} theme; /bedroom on puts it back. ${describe(ctx)}`
}
if (word === 'off') {
const cleared = await clearLook($)
return cleared.isDrawn ? 'the theme is cleared' : 'cleared'
}
if (word === 'on') {
const wasCovered = coveringPlugin() !== undefined
const set = await setLook($, ctx)
return set.isDrawn ? 'the room is on' : `not drawn: ${set.reason}`
if (!set.isDrawn) return `not drawn: ${set.reason}`
if (!wasCovered) return 'the room is on'
holdShader($, 'the room set again')
await ask($, 'the room set again')
return 'the room is back'
}
if ((OVERRIDES as readonly string[]).includes(word) && value === 'default') {
await ctx.save({ [word]: undefined })
Expand Down
13 changes: 13 additions & 0 deletions plugins/themes/hooks/bedroom/core/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,35 @@ export function lookOf(ctx: Ctx): ThemeSetArgs {
}

let isThemeSet = false
let coveredBy: string | undefined
/** Whether the theme is set: the card's theme button shows it. A set stands for the session whether or not a surface draws it. */
export function isThemeOn(): boolean {
return isThemeSet
}

/** The plugin whose theme replaced the room's while the room was on, until the room is set or cleared again. */
export function coveringPlugin(): string | undefined {
return isThemeSet ? coveredBy : undefined
}

/** Another plugin set its theme over the room's: the session has one theme, so the room no longer shows. */
export function markCovered(plugin: string): void {
coveredBy = plugin
}

/** Sets the bedroom's look and notes that the theme is on. */
export async function setLook($: EngineInterface, ctx: Ctx): Promise<Awaited<ReturnType<EngineInterface['theme']['set']>>> {
const set = await $.theme.set(lookOf(ctx))
isThemeSet = true
coveredBy = undefined
return set
}

/** Clears the theme and notes that it is off. */
export async function clearLook($: EngineInterface): Promise<Awaited<ReturnType<EngineInterface['theme']['clear']>>> {
const cleared = await $.theme.clear()
isThemeSet = false
coveredBy = undefined
return cleared
}

Expand Down
42 changes: 42 additions & 0 deletions plugins/themes/hooks/bedroom/core/restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,48 @@ test('another mod sets its own theme: it stands until cleared, and the room come
await room.unmount()
})

test('another mod sets its theme over the room: status says covered, and /bedroom on sets the room again without clearing', { timeoutMs: 20000, options: QUIET, plugins: [FIREWORKS] }, async ($, on) => {
const room = await startRoom($, on)
expect(await room.run('status')).not.toContain('covered')
await fireworks($, 'show')
expect(await room.run('status')).toContain('the room is covered by the fireworks theme; /bedroom on puts it back.')
const before = room.themes.length
expect(await room.run('on')).toBe('the room is back')
expect(room.themes.slice(before)).toEqual(['set bedroom'])
expect(await room.run('status')).not.toContain('covered')
await room.unmount()
})

test('while another mod covers the room the card says so, and its theme button sets the room again instead of clearing', { timeoutMs: 20000, options: QUIET, plugins: [FIREWORKS] }, async ($, on) => {
const room = await startRoom($, on)
await room.run('')
const card = await $.ui.mount({ plugin: 'themes', surface: 'desktop', component: 'Placed', requestId: 'bedroom-card-1', props: { id: 'bedroom-card-1', plugin: 'themes' } } as never)
expect((await card.find({ key: 'theme' }))?.props.label).toBe('theme: on')
await fireworks($, 'show')
expect((await card.find({ key: 'theme' }))?.props.label).toBe('theme: covered')
const before = room.themes.length
await card.press({ key: 'theme' })
expect(room.themes.slice(before)).toEqual(['set bedroom'])
expect((await card.find({ key: 'theme' }))?.props.label).toBe('theme: on')
await card.unmount()
await room.unmount()
})

test('a covering mod that then clears its theme brings the room back, and the open card says on again with a button that turns it off', { timeoutMs: 20000, options: QUIET, plugins: [FIREWORKS] }, async ($, on) => {
const room = await startRoom($, on)
await room.run('')
const card = await $.ui.mount({ plugin: 'themes', surface: 'desktop', component: 'Placed', requestId: 'bedroom-card-1', props: { id: 'bedroom-card-1', plugin: 'themes' } } as never)
await fireworks($, 'show')
expect((await card.find({ key: 'theme' }))?.props.label).toBe('theme: covered')
await fireworks($, 'clear')
expect((await card.find({ key: 'theme' }))?.props.label).toBe('theme: on')
const before = room.themes.length
await card.press({ key: 'theme' })
expect(room.themes.slice(before)).toEqual(['clear'])
await card.unmount()
await room.unmount()
})

test('/bedroom off clears the theme without setting it again, and a later clear by another mod leaves it off until /bedroom on', { timeoutMs: 20000, options: QUIET, plugins: [FIREWORKS] }, async ($, on) => {
const room = await startRoom($, on)
const before = room.themes.length
Expand Down
4 changes: 2 additions & 2 deletions plugins/themes/hooks/bedroom/lights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,8 @@ acc = vec4(vec4(acc.rgb * (1 - ct_b) + vec3(0.82, 0.86, 0.96) * ct_b, acc.a * (1
}


const HIT = table([[1125,425,166,122],[1358,470,42,136],[195,500,150,145],[498,530,58,70],[1125,562,166,46],[1298,490,46,118],[1398,468,34,50]])
const HIT2 = table([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1337,533,21,69],[0,0,0,0],[0,0,0,0]])
const HIT = table([[1125,425,166,122],[1358,470,39,136],[195,500,150,145],[498,530,58,70],[1125,562,166,46],[1298,490,38,118],[1398,468,34,50]])
const HIT2 = table([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1337,533,20,69],[0,0,0,0],[0,0,0,0]])
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
const PACE = table([[4.5,3],[2.5,2.5],[5,3.3],[6,3.5],[6,6],[6,6],[3.3,7]])
const SUN = table([[0,0,0,0],[0,0.5,0,0],[0,1,0,0],[0,0.6,0,0.4],[0.35,0,0,1],[0.85,0,0,0.25],[1,0,0,0],[0.65,0,1,0]])
const WEATHER = table([[0,0,0],[0.75,0,0],[0.9,0.6,0],[1,1,0],[1,0.9,1]])
Expand Down
27 changes: 23 additions & 4 deletions plugins/themes/hooks/bedroom/register.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, test } from 'claude-code/testing'
import { TITLE_CAPS, aspectFrom, hitAt, switchesAt, toViewBox, withTitle } from './register.tsx'
import { SECOND_STEP_MS, commandSwitch, nextValues, startShader } from './core/values.js'
import { bedroomSvg } from './scene.ts'
import { DEFAULT_STATE } from './scene/lib.ts'
import { DEFAULT_STATE, HITS } from './scene/lib.ts'

test('a click maps to the scene by the window shape', () => {
expect(toViewBox(0.5, 0.5, 1.6)).toEqual({ x: 800, y: 500 })
Expand Down Expand Up @@ -33,9 +33,28 @@ test('a title set with /bedroom title replaces the default', () => {
expect(svg.includes('FAR MERIDIAN')).toBe(false)
})

test('a click where two things overlap flips both, as the shader does', () => {
expect(switchesAt(1340, 560)).toEqual(['console', 'tape'])
expect(switchesAt(1399, 500)).toEqual(['light', 'lava'])
test('no two click rectangles meet, edges included, so one click flips one switch', () => {
const names = Object.keys(HITS)
const meeting: string[] = []
for (const [i, a] of names.entries()) {
for (const b of names.slice(i + 1)) {
const [ax, ay, aw, ah] = HITS[a]!
const [bx, by, bw, bh] = HITS[b]!
if (ax <= bx + bw && bx <= ax + aw && ay <= by + bh && by <= ay + ah) meeting.push(`${a} and ${b}`)
}
}
expect(meeting, 'rectangles that share a point').toEqual([])
})

test('a click on either side of each border between neighbors flips the one thing on that side', () => {
expect(switchesAt(1336, 560)).toEqual(['console'])
expect(switchesAt(1337, 560)).toEqual(['tape'])
expect(switchesAt(1340, 560)).toEqual(['tape'])
expect(switchesAt(1357, 560)).toEqual(['tape'])
expect(switchesAt(1358, 560)).toEqual(['lava'])
expect(switchesAt(1397, 500)).toEqual(['lava'])
expect(switchesAt(1398, 500)).toEqual(['light'])
expect(switchesAt(1399, 500)).toEqual(['light'])
expect(switchesAt(1200, 480)).toEqual(['tv'])
expect(switchesAt(10, 10)).toEqual([])
})
Expand Down
12 changes: 11 additions & 1 deletion plugins/themes/hooks/bedroom/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { handleClick } from './core/clicks.js'
import { minuteOfHalfDay, readNow, startMinuteChain } from './core/clock.js'
import { describe, runCommand } from './core/commands.js'
import { ctxOf, usePieces } from './core/context.js'
import { SCENE, colorProblems, draw, holdShader, isThemeOn, markStarted, setLook } from './core/render.js'
import { SCENE, colorProblems, draw, holdShader, isThemeOn, markCovered, markStarted, setLook } from './core/render.js'
import { load } from './core/saved.js'
import { useOptions } from './core/settings.js'
import { failed, flush, note } from './core/telemetry.js'
Expand Down Expand Up @@ -73,6 +73,16 @@ export const register: Register = (on, options) => {
return cleared
}).catch(($, e, next) => next(e))

on('theme.set', async ($, e, next) => {
const set = await next(e)
if (next.origin.plugin === $.plugin.name || !isThemeOn()) return set
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
note(`${next.origin.plugin} set its theme over the room: the room is covered until /bedroom on`)
markCovered(next.origin.plugin)
void refreshCard($).catch(() => undefined)
flush($)
return set
}).catch(($, e, next) => next(e))

on('ui.render', { component: 'Backdrop' }, ($, e, next) => {
if (e.props.plugin !== $.plugin.name || e.props.scene !== SCENE || e.surface !== 'desktop') return next(e)
const { Shader, Svg } = $.ui.resolve(e)
Expand Down
6 changes: 3 additions & 3 deletions plugins/themes/hooks/bedroom/scene/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ export const DEFAULT_STATE: State = { tv: true, tape: false, console: false, pc:
export const HITS: Record<string, readonly [number, number, number, number]> = {
tv: [1125, 425, 166, 122],
tape: [1125, 562, 166, 46],
console: [1298, 490, 46, 118],
console: [1298, 490, 38, 118],
pc: [195, 500, 150, 145],
light: [1398, 468, 34, 50],
lamp: [498, 530, 58, 70],
spine: [1337, 533, 21, 69],
lava: [1358, 470, 42, 136],
spine: [1337, 533, 20, 69],
lava: [1358, 470, 39, 136],
cat: [1135, 360, 155, 62],
}
58 changes: 58 additions & 0 deletions plugins/themes/tests/shader-hits.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// test-scope: plugins/themes/hooks/bedroom/scene/lib.ts plugins/themes/hooks/bedroom/lights.ts
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { test } from 'node:test'

const BEDROOM = new URL('../hooks/bedroom/', import.meta.url)
const read = path => readFileSync(new URL(path, BEDROOM), 'utf8')

/**
* The shader's rows, one per switch column of its buffer: 0 tv, 1 lava, 2 pc, 3 lamp, 4 tape, 5 console, 6 light.
* lights.ts picks each column's level in that order (v.tv, then v.lava past 0.5, v.pc past 1.5, v.lamp past 2.5,
* v.light past 5.5), and its screen field units (signals.gen.ts SCREEN: tape 4, console 16) put the tape at 4 and the
* console at 5. HIT2 is each column's second rectangle, all zeros but the tape's, which is its spine. The cat has no row.
*/
const SHADER_ROWS = {
tv: ['HIT', 0],
lava: ['HIT', 1],
pc: ['HIT', 2],
lamp: ['HIT', 3],
tape: ['HIT', 4],
console: ['HIT', 5],
light: ['HIT', 6],
spine: ['HIT2', 4],
}

function hitsOf(source) {
const block = /export const HITS[^=]*= \{\n([\s\S]*?)\n\}/.exec(source)
assert.ok(block, 'scene/lib.ts declares HITS')
return Object.fromEntries(
block[1].split('\n').map(line => {
const row = /^ {2}(\w+): (\[[\d, ]+\]),$/.exec(line)
assert.ok(row, `a HITS line reads name: [x, y, width, height]: ${line}`)
return [row[1], JSON.parse(row[2])]
}),
)
}

function tableOf(source, name) {
const line = source.split('\n').find(text => text.startsWith(`const ${name} = table(`))
assert.ok(line, `lights.ts declares ${name}`)
return JSON.parse(line.slice(`const ${name} = table(`.length, -1))
}

test('the shader takes clicks over the same rectangles as HITS', () => {
const hits = hitsOf(read('scene/lib.ts'))
const lights = read('lights.ts')
const tables = { HIT: tableOf(lights, 'HIT'), HIT2: tableOf(lights, 'HIT2') }

assert.deepEqual(Object.keys(hits).filter(name => name !== 'cat').sort(), Object.keys(SHADER_ROWS).sort(), 'every switch rectangle has a shader row')
for (const [name, [table, row]] of Object.entries(SHADER_ROWS)) {
assert.deepEqual(tables[table][row], hits[name], `${name} is ${table} row ${row}`)
}
assert.equal(tables.HIT.length, 7, 'HIT has one row per switch column')
assert.ok(
tables.HIT2.every((box, row) => row === 4 || box.every(n => n === 0)),
'HIT2 holds no rectangle but the spine',
)
})
12 changes: 12 additions & 0 deletions plugins/themes/tests/shader-hits.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# test-scope: plugins/themes/hooks/bedroom/scene/lib.ts plugins/themes/hooks/bedroom/lights.ts
set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if ! command -v node >/dev/null 2>&1; then
echo "SKIP: node not installed" >&2
exit 0
fi

exec node --test "$SCRIPT_DIR/shader-hits.test.mjs"
Loading