Skip to content

Your home as a live 3D model in the Mac menu bar and on iPhone — lights and ACs via Smart Life, Home Assistant, Philips Hue and Apple Home.

Web UI: iPhone layout, island mode and views that share one state

0xProbability10168ca1fe00
diff --git a/macos/Sources/HouseControl/EventLog.swift b/core/Sources/HouseCore/EventLog.swift
similarity index 100%
rename from macos/Sources/HouseControl/EventLog.swift
rename to core/Sources/HouseCore/EventLog.swift
diff --git a/macos/Sources/HouseControl/InfraredRemote.swift b/core/Sources/HouseCore/InfraredRemote.swift
similarity index 100%
rename from macos/Sources/HouseControl/InfraredRemote.swift
rename to core/Sources/HouseCore/InfraredRemote.swift
diff --git a/macos/Sources/HouseControl/PlanStore.swift b/core/Sources/HouseCore/PlanStore.swift
similarity index 100%
rename from macos/Sources/HouseControl/PlanStore.swift
rename to core/Sources/HouseCore/PlanStore.swift
diff --git a/macos/Sources/HouseControl/SmartLifeService.swift b/core/Sources/HouseCore/SmartLifeService.swift
similarity index 100%
rename from macos/Sources/HouseControl/SmartLifeService.swift
rename to core/Sources/HouseCore/SmartLifeService.swift
diff --git a/macos/Sources/HouseControl/TuyaService.swift b/core/Sources/HouseCore/TuyaService.swift
similarity index 100%
rename from macos/Sources/HouseControl/TuyaService.swift
rename to core/Sources/HouseCore/TuyaService.swift
diff --git a/web/index.html b/web/index.html
index 42df411..ca63955 100644
--- a/web/index.html
+++ b/web/index.html
@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no" />
<meta name="color-scheme" content="dark" />
<title>House Control</title>
<link rel="stylesheet" href="./src/style.css" />
diff --git a/web/src/bridge.js b/web/src/bridge.js
index 251366e..49bea07 100644
--- a/web/src/bridge.js
+++ b/web/src/bridge.js
@@ -1,14 +1,25 @@
-// Thin bridge to the native macOS shell. In a plain browser every call is a no-op,
-// so the same bundle runs under `vite dev` and inside the menu bar app.
+// Thin bridge to the native shell (the macOS menu bar app or the iPhone app). In a plain
+// browser every call is a no-op, so the same bundle runs under `vite dev` too.
const handler = window.webkit?.messageHandlers?.house;
export const isNative = Boolean(handler);
+// 'ios' inside the iPhone app (or with ?platform=ios in a browser, to work on that layout).
+export const platform = window.__HOUSE_BOOT__?.platform
+ || new URLSearchParams(location.search).get('platform') || (handler ? 'macos' : 'web');
+export const isPhone = platform === 'ios';
+
export function post(type, payload = {}) {
handler?.postMessage({ type, ...payload });
}
+// A tap you can feel, on the iPhone only: 'tick' for a step of a slider, 'light' /
+// 'medium' for a switch or a scene, 'error' when a device refuses a change.
+export function haptic(kind = 'light') {
+ if (isPhone) post('haptic', { kind });
+}
+
// Native -> JS entry points are attached here by main.js.
export const incoming = (window.house = window.house || {});
@@ -17,7 +28,7 @@ let nextId = 1;
const pending = new Map();
export function call(type, payload = {}) {
- if (!handler) return Promise.reject(new Error('Only available inside the macOS app'));
+ if (!handler) return Promise.reject(new Error('Only available inside the app'));
return new Promise((resolve, reject) => {
const id = nextId++;
pending.set(id, { resolve, reject });
diff --git a/web/src/island/island.css b/web/src/island/island.css
new file mode 100644
index 0000000..edc3035
--- /dev/null
+++ b/web/src/island/island.css
@@ -0,0 +1,230 @@
+/* The island lives in a transparent window pinned to the top of the screen; only the
+ black shape is drawn. Its top edge is the screen's edge, so it reads as part of the notch. */
+
+html.mode-island, html.mode-island body { background: transparent; }
+html.mode-island #app { display: block; padding: 0; height: 100%; }
+
+.isle {
+ --notch-w: 190px;
+ --notch-h: 34px;
+ --ear: 50px;
+ --mood: #17274f;
+ --w: calc(var(--notch-w) + var(--ear) * 2);
+ --h: var(--notch-h);
+ --r: 13px;
+ position: absolute;
+ inset: 0 0 auto 0;
+ display: flex;
+ justify-content: center;
+ pointer-events: none;
+ font-size: 12px;
+}
+.isle.quiet { --ear: 0px; }
+.isle[data-state='peek'] { --ear: 132px; --r: 16px; }
+.isle[data-state='expanded'] { --w: 664px; --h: calc(var(--notch-h) + 292px); --r: 34px; }
+
+.isle-shape {
+ position: relative;
+ width: var(--w);
+ height: var(--h);
+ background: #000;
+ border-radius: 0 0 var(--r) var(--r);
+ pointer-events: auto;
+ /* Opening overshoots a little, like something with weight; closing just closes. */
+ transition:
+ width 560ms cubic-bezier(0.3, 1.36, 0.5, 1),
+ height 560ms cubic-bezier(0.3, 1.36, 0.5, 1),
+ border-radius 420ms cubic-bezier(0.22, 1, 0.36, 1),
+ box-shadow 500ms ease;
+ will-change: width, height;
+}
+.isle[data-state='collapsed'] .isle-shape { transition-duration: 380ms, 380ms, 320ms, 300ms; transition-timing-function: cubic-bezier(0.32, 0.72, 0.2, 1); }
+.isle[data-state='expanded'] .isle-shape {
+ box-shadow:
+ 0 0 0 1px rgba(255, 255, 255, 0.06),
+ 0 26px 60px -12px rgba(0, 0, 0, 0.75),
+ 0 40px 110px -30px color-mix(in srgb, var(--mood) 85%, transparent);
+}
+/* The little concave shoulders where the tab meets the top of the screen. */
+.isle-shape::before, .isle-shape::after {
+ content: '';
+ position: absolute;
+ top: 0;
+ width: 9px;
+ height: 9px;
+ background: radial-gradient(circle at 0 100%, transparent 8.5px, #000 9px);
+}
+.isle-shape::before { right: 100%; transform: scaleX(-1); }
+.isle-shape::after { left: 100%; }
+
+/* ---------- at rest and while peeking ---------- */
+
+.ear {
+ position: absolute;
+ top: 0;
+ height: var(--notch-h);
+ width: var(--ear);
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ padding: 0 10px 0 12px;
+ white-space: nowrap;
+ overflow: hidden;
+ color: #f3f5fa;
+ font-weight: 600;
+ font-variant-numeric: tabular-nums;
+ transition: opacity 220ms ease, width 420ms cubic-bezier(0.3, 1.36, 0.5, 1);
+}
+.ear.left { left: 0; }
+.ear.right { right: 0; justify-content: flex-end; padding: 0 12px 0 10px; }
+.isle[data-state='expanded'] .ear { opacity: 0; transition-duration: 120ms; }
+.ear-icon { color: var(--c); filter: drop-shadow(0 0 5px color-mix(in srgb, var(--c) 70%, transparent)); flex: none; }
+.ear-text { overflow: hidden; text-overflow: ellipsis; }
+.ear-text.strong { color: color-mix(in srgb, var(--mood) 35%, #fff); }
+.ear > * { animation: ear-in 360ms cubic-bezier(0.22, 1, 0.36, 1) both; }
+@keyframes ear-in { from { opacity: 0; transform: scale(0.6); filter: blur(4px); } }
+
+/* ---------- open ---------- */
+
+.isle-body {
+ position: absolute;
+ inset: 0;
+ width: 664px;
+ left: 50%;
+ margin-left: -332px;
+ display: flex;
+ flex-direction: column;
+ opacity: 0;
+ transform: scale(0.94);
+ transform-origin: 50% 0;
+ filter: blur(10px);
+ pointer-events: none;
+ transition: opacity 140ms ease, transform 200ms ease, filter 160ms ease;
+}
+.isle[data-state='expanded'] .isle-body {
+ opacity: 1;
+ transform: none;
+ filter: none;
+ pointer-events: auto;
+ transition: opacity 300ms ease 130ms, transform 520ms cubic-bezier(0.22, 1, 0.36, 1) 90ms, filter 320ms ease 110ms;
+}
+
+.isle-head {
+ flex: none;
+ height: var(--notch-h);
+ display: grid;
+ grid-template-columns: 1fr var(--notch-w) 1fr;
+ align-items: center;
+ padding: 0 14px 0 16px;
+ color: var(--muted);
+}
+.isle-title { display: flex; align-items: center; gap: 8px; min-width: 0; font-size: 12.5px; }
+.isle-title b { color: var(--text); font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
+.isle-badge { color: color-mix(in srgb, var(--mood) 40%, #fff); display: grid; }
+.isle-right { display: flex; align-items: center; justify-content: flex-end; gap: 10px; min-width: 0; }
+.isle-status { display: flex; align-items: center; gap: 5px; white-space: nowrap; font-size: 12px; }
+.isle-status b { color: var(--text); font-weight: 600; }
+.isle-status .dot { width: 6px; height: 6px; border-radius: 50%; background: #4ade80; box-shadow: 0 0 8px #4ade80; margin-right: 2px; }
+.isle-power {
+ width: 24px; height: 24px; border-radius: 50%;
+ display: grid; place-items: center;
+ background: rgba(255, 255, 255, 0.1);
+ color: var(--muted);
+ transition: background 200ms, color 200ms, transform 260ms var(--spring), box-shadow 300ms;
+}
+.isle-power:active { transform: scale(0.88); }
+.isle-power.on { background: linear-gradient(180deg, #5aa2ff, #2f7bf2); color: #fff; box-shadow: 0 4px 14px -3px rgba(61, 139, 255, 0.8); }
+
+.isle-main { flex: 1; min-height: 0; display: grid; grid-template-columns: 286px 1fr; gap: 6px; padding: 2px 14px 14px 8px; }
+
+.isle-stage { position: relative; min-height: 0; }
+.isle-stage canvas {
+ width: 100%; height: 100%; display: block; outline: none;
+ /* The model floats in the black of the island: no edge of the canvas may show. */
+ -webkit-mask-image: radial-gradient(closest-side, #000 66%, transparent 100%);
+ mask-image: radial-gradient(closest-side, #000 66%, transparent 100%);
+}
+
+.isle-side { min-width: 0; min-height: 0; display: flex; flex-direction: column; gap: 9px; }
+
+.isle-scenes { flex: none; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; }
+.isle-scene {
+ height: 50px; border-radius: 15px;
+ display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
+ font-size: 10.5px; font-weight: 600; color: var(--muted);
+ background: rgba(255, 255, 255, 0.07);
+ border: 1px solid rgba(255, 255, 255, 0.06);
+ transition: transform 300ms var(--spring), background 220ms, color 220ms, border-color 220ms, box-shadow 300ms;
+}
+.isle-scene span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 4px; }
+@media (hover: hover) { .isle-scene:hover { background: rgba(255, 255, 255, 0.12); color: var(--text); } }
+.isle-scene:active { transform: scale(0.93); }
+.isle-scene.active {
+ color: #fff;
+ background: linear-gradient(180deg, rgba(88, 150, 255, 0.4), rgba(61, 139, 255, 0.16));
+ border-color: rgba(120, 175, 255, 0.55);
+ box-shadow: 0 8px 20px -10px rgba(61, 139, 255, 0.9);
+}
+.isle-scene.active .icon { color: #a9cbff; }
+
+.isle-list {
+ flex: 1; min-height: 0;
+ display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 44px; gap: 6px;
+ overflow-y: auto; scrollbar-width: none;
+ margin-bottom: -14px; padding-bottom: 14px;
+ -webkit-mask-image: linear-gradient(180deg, #000 calc(100% - 26px), transparent 100%);
+ mask-image: linear-gradient(180deg, #000 calc(100% - 26px), transparent 100%);
+}
+.isle-list::-webkit-scrollbar { display: none; }
+
+.chip {
+ --c: var(--warm);
+ position: relative;
+ min-width: 0;
+ border-radius: 14px;
+ padding: 0 9px;
+ display: flex; align-items: center; gap: 8px;
+ overflow: hidden;
+ background: rgba(255, 255, 255, 0.07);
+ border: 1px solid rgba(255, 255, 255, 0.06);
+ transition: transform 300ms var(--spring), background 240ms, border-color 240ms, box-shadow 320ms;
+}
+.chip.ac { grid-column: 1 / -1; gap: 6px; }
+@media (hover: hover) { .chip.light:hover { background: rgba(255, 255, 255, 0.12); } }
+.chip.pressed { transform: scale(0.96); }
+.chip.dimmable { cursor: ew-resize; }
+.chip.on {
+ background: linear-gradient(135deg, color-mix(in srgb, var(--c) 30%, transparent), color-mix(in srgb, var(--c) 9%, transparent));
+ border-color: color-mix(in srgb, var(--c) 44%, transparent);
+ box-shadow: 0 10px 22px -16px var(--c);
+}
+.chip-bubble {
+ flex: none; width: 28px; height: 28px; border-radius: 10px;
+ display: grid; place-items: center;
+ color: var(--muted); background: rgba(255, 255, 255, 0.08);
+ transition: background 240ms, color 240ms, box-shadow 320ms;
+}
+.chip.on .chip-bubble { color: #241503; background: var(--c); box-shadow: 0 0 16px -3px var(--c); }
+.chip.ac.on .chip-bubble { color: #06162c; }
+.chip-text { flex: 1; min-width: 0; }
+.chip-name { font-size: 12px; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
+.chip-sub { margin-top: 1px; font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
+.chip.on .chip-sub { color: color-mix(in srgb, var(--c) 55%, #fff); }
+.chip-level {
+ position: absolute; left: 0; bottom: 0; height: 2.5px; width: 100%;
+ transform-origin: left; transform: scaleX(var(--level, 0));
+ background: var(--c); opacity: 0;
+ transition: transform 240ms var(--ease), opacity 240ms;
+}
+.chip.on.dimmable .chip-level { opacity: 0.9; }
+.chip-step, .chip-power {
+ flex: none; width: 24px; height: 24px; border-radius: 8px;
+ display: grid; place-items: center; color: var(--muted);
+ transition: background 180ms, color 180ms, transform 240ms var(--spring), box-shadow 300ms;
+}
+@media (hover: hover) { .chip-step:hover { background: rgba(255, 255, 255, 0.14); color: var(--text); } }
+.chip-step:active, .chip-power:active { transform: scale(0.86); }
+.chip-value { min-width: 30px; text-align: center; font-size: 14px; font-weight: 650; color: var(--text); font-variant-numeric: tabular-nums; }
+.chip.ac:not(.on) .chip-value { color: var(--faint); }
+.chip-power { border-radius: 50%; margin-left: 2px; background: rgba(255, 255, 255, 0.1); }
+.chip.ac.on .chip-power { background: var(--c); color: #06162c; box-shadow: 0 0 14px -4px var(--c); }
diff --git a/web/src/island/island.js b/web/src/island/island.js
new file mode 100644
index 0000000..fb7dec3
--- /dev/null
+++ b/web/src/island/island.js
@@ -0,0 +1,317 @@
+// The island: a black tab that hangs from the MacBook's notch. At rest it shows how many
+// lights are on; under the pointer it springs open into a small live model of the home
+// with scenes and devices; when something changes elsewhere it peeks out to say so.
+import './island.css';
+import * as THREE from 'three';
+import { loadConfig } from '../config.js';
+import { createStore } from '../store.js';
+import { createWorld } from '../three/world.js';
+import { h, clamp } from '../ui/dom.js';
+import { icon } from '../ui/icons.js';
+import { lightColor } from '../three/lights.js';
+import { MODE_COLORS } from '../three/ac.js';
+import { post, incoming, isNative } from '../bridge.js';
+
+const MODE_LABEL = { cool: 'Cool', heat: 'Heat', dry: 'Dry', fan: 'Fan', auto: 'Auto' };
+const NAVY = new THREE.Color('#17274f');
+const tmp = new THREE.Color();
+const css = (c) => `#${c.getHexString()}`;
+const PEEK_MS = 2400;
+
+export async function bootIsland(root) {
+ const boot = window.__HOUSE_BOOT__?.island || {};
+ const query = new URLSearchParams(location.search);
+ // The notch this island grows out of, in points. A screen without one gets a drawn tab.
+ const notch = {
+ w: boot.notchWidth ?? (Number(query.get('notch')) || 190),
+ h: boot.notchHeight ?? 34,
+ real: boot.hasNotch ?? false,
+ };
+
+ let config;
+ try {
+ config = await loadConfig();
+ } catch (error) {
+ console.error(error);
+ post('ready', { error: String(error?.message || error) });
+ return;
+ }
+ const store = createStore(config);
+ const { state } = store;
+ const roomName = new Map(config.rooms.map((r) => [r.id, r.name]));
+
+ /* ---------- skeleton ---------- */
+
+ const earLeft = h('div.ear.left');
+ const earRight = h('div.ear.right');
+ const canvas = h('canvas', { tabindex: -1 });
+ const title = h('div.isle-title', h('span.isle-badge', icon('home', 14)), h('b', config.name));
+ const status = h('div.isle-status');
+ const power = h('button.isle-power', { 'aria-label': 'Everything on / off', onclick: toggleEverything }, icon('power', 15, 2.4));
+ const scenes = h('div.isle-scenes');
+ const list = h('div.isle-list');
+ const body = h('div.isle-body',
+ h('div.isle-head', title, h('i.isle-gap'), h('div.isle-right', status, power)),
+ h('div.isle-main', h('div.isle-stage', canvas), h('div.isle-side', scenes, list)));
+ const shape = h('div.isle-shape', earLeft, earRight, body);
+ const isle = h('div.isle', shape);
+ isle.dataset.state = 'collapsed';
+ isle.classList.toggle('real', notch.real);
+ isle.style.setProperty('--notch-w', `${notch.w}px`);
+ isle.style.setProperty('--notch-h', `${notch.h}px`);
+ root.replaceChildren(isle);
+
+ /* ---------- scenes ---------- */
+
+ const sceneButtons = new Map();
+ for (const s of config.scenes.slice(0, 5)) {
+ const b = h('button.isle-scene', { onclick: () => store.applyScene(s.id) }, icon(s.icon, 17), h('span', s.name));
+ sceneButtons.set(s.id, b);
+ scenes.append(b);
+ }
+ if (!config.scenes.length) scenes.hidden = true;
+
+ /* ---------- devices ---------- */
+
+ const rows = [];
+
+ for (const cfg of config.acs) {
+ const sub = h('div.chip-sub');
+ const value = h('span.chip-value');
+ const bubble = h('span.chip-bubble');
+ const step = (d) => (e) => { e.stopPropagation(); store.setAC(cfg.id, { on: true, target: state.acs[cfg.id].target + d }); };
+ const el = h('div.chip.ac',
+ bubble,
+ h('div.chip-text', h('div.chip-name', cfg.name), sub),
+ h('button.chip-step', { 'aria-label': 'Cooler', onclick: step(-1) }, icon('minus', 13, 2.4)),
+ value,
+ h('button.chip-step', { 'aria-label': 'Warmer', onclick: step(1) }, icon('plus', 13, 2.4)),
+ h('button.chip-power', { 'aria-label': `${cfg.name} power`, onclick: () => store.setAC(cfg.id, { on: !state.acs[cfg.id].on }) }, icon('power', 13, 2.4)));
+ let shownMode = null;
+ list.append(el);
+ rows.push(() => {
+ const s = state.acs[cfg.id];
+ el.classList.toggle('on', s.on);
+ el.style.setProperty('--c', MODE_COLORS[s.mode]);
+ if (shownMode !== s.mode) { bubble.replaceChildren(icon(s.mode, 15)); shownMode = s.mode; }
+ value.textContent = `${s.target}°`;
+ sub.textContent = `${s.on ? MODE_LABEL[s.mode] : 'Off'} · ${s.roomTemp.toFixed(0)}° inside`;
+ });
+ }
+
+ for (const cfg of config.lights) {
+ const dimmable = cfg.capabilities.includes('brightness');
+ const sub = h('div.chip-sub');
+ const el = h(`div.chip.light${dimmable ? '.dimmable' : ''}`, { role: 'button', 'aria-label': cfg.name },
+ h('span.chip-bubble', icon(cfg.capabilities.length ? cfg.type : 'plug', 15)),
+ h('div.chip-text', h('div.chip-name', cfg.name), sub),
+ h('i.chip-level'));
+ // Click toggles, dragging sideways dims: the same gesture as the panel's tiles.
+ let gesture = null;
+ el.addEventListener('pointerdown', (ev) => {
+ if (ev.button !== 0) return;
+ el.setPointerCapture(ev.pointerId);
+ const s = state.lights[cfg.id];
+ gesture = { x: ev.clientX, start: s.on ? s.brightness : 0, moved: false };
+ el.classList.add('pressed');
+ });
+ el.addEventListener('pointermove', (ev) => {
+ if (!gesture) return;
+ const dx = ev.clientX - gesture.x;
+ if (!gesture.moved && dimmable && Math.abs(dx) > 5) gesture.moved = true;
+ if (gesture.moved) {
+ const b = clamp(gesture.start + dx / el.clientWidth, 0, 1);
+ store.setLight(cfg.id, b < 0.04 ? { on: false } : { on: true, brightness: b });
+ }
+ });
+ const finish = (ev) => {
+ if (!gesture) return;
+ const tapped = !gesture.moved && ev.type === 'pointerup';
+ gesture = null;
+ el.classList.remove('pressed');
+ if (tapped) { world?.lights.get(cfg.id)?.bump(); store.toggleLight(cfg.id); }
+ };
+ el.addEventListener('pointerup', finish);
+ el.addEventListener('pointercancel', finish);
+ list.append(el);
+ rows.push(() => {
+ const s = state.lights[cfg.id];
+ el.classList.toggle('on', s.on);
+ el.style.setProperty('--c', css(lightColor(s, tmp)));
+ el.style.setProperty('--level', s.on ? String(s.brightness) : '0');
+ sub.textContent = `${lightLabel(cfg, s)} · ${roomName.get(cfg.room)}`;
+ });
+ }
+
+ function lightLabel(cfg, s) {
+ if (!s.on) return 'Off';
+ return cfg.capabilities.includes('brightness') ? `${Math.round(s.brightness * 100)}%` : 'On';
+ }
+
+ function toggleEverything() {
+ const anyOn = Object.values(state.lights).some((l) => l.on) || Object.values(state.acs).some((a) => a.on);
+ if (anyOn) {
+ store.setAllLights(false);
+ for (const id of Object.keys(state.acs)) store.setAC(id, { on: false });
+ } else {
+ store.setAllLights(true);
+ }
+ }
+
+ /* ---------- the model ---------- */
+
+ let world = null;
+ let focused = null;
+ try {
+ world = createWorld(canvas, config, store, {
+ background: '#000000',
+ onHover: () => {},
+ onPick(target) {
+ if (!target) return;
+ if (target.kind === 'light') store.toggleLight(target.id);
+ else if (target.kind === 'ac') store.setAC(target.id, { on: !state.acs[target.id].on });
+ else if (target.kind === 'room') {
+ focused = focused === target.id ? null : target.id;
+ world.focus(focused ? { kind: 'room', id: focused } : null);
+ }
+ },
+ });
+ world.setInsets(0, 0);
+ new ResizeObserver(() => world.resize()).observe(canvas);
+ } catch (error) {
+ console.error(error); // no WebGL: the island still works as a remote
+ }
+
+ /* ---------- what it shows ---------- */
+
+ function mood() {
+ const sum = new THREE.Color(0, 0, 0);
+ let weight = 0;
+ for (const cfg of config.lights) {
+ const s = state.lights[cfg.id];
+ if (!s.on) continue;
+ const w = cfg.capabilities.includes('brightness') ? s.brightness : 1;
+ lightColor(s, tmp);
+ sum.r += tmp.r * w; sum.g += tmp.g * w; sum.b += tmp.b * w;
+ weight += w;
+ }
+ if (!weight) return NAVY.clone();
+ sum.multiplyScalar(0.6 / weight);
+ return NAVY.clone().lerp(sum, clamp(weight / 3, 0.35, 0.9));
+ }
+
+ const ear = (iconName, text, color) => [h('span.ear-icon', { vars: { '--c': color } }, icon(iconName, 14, 2.2)), text != null ? h('span.ear-text', text) : null];
+
+ let peek = null; // { left: [...nodes], right: text } while something is being announced
+
+ function update() {
+ const lights = Object.values(state.lights);
+ const onCount = lights.filter((l) => l.on).length;
+ const acOn = config.acs.map((a) => state.acs[a.id]).find((a) => a.on);
+ const m = css(mood());
+ isle.style.setProperty('--mood', m);
+ isle.classList.toggle('quiet', onCount === 0 && !acOn);
+
+ if (isle.dataset.state === 'peek' && peek) {
+ earLeft.replaceChildren(...peek.left);
+ earRight.replaceChildren(h('span.ear-text.strong', peek.right));
+ } else {
+ earLeft.replaceChildren(...(onCount ? ear('home', String(onCount), '#ffb46b') : acOn ? ear('home', null, '#8fa3c7') : []));
+ earRight.replaceChildren(...(acOn ? ear(acOn.mode, `${acOn.target}°`, MODE_COLORS[acOn.mode]) : []));
+ }
+
+ status.replaceChildren(
+ h('i.dot', { style: onCount || acOn ? {} : { background: 'rgba(255,255,255,.25)', boxShadow: 'none' } }),
+ h('b', onCount ? `${onCount} of ${lights.length}` : 'No'), ' lights on', acOn ? ` · ${acOn.target}°` : '');
+ power.classList.toggle('on', onCount > 0 || Boolean(acOn));
+ for (const [id, b] of sceneButtons) b.classList.toggle('active', state.scene === id);
+ for (const row of rows) row();
+ }
+
+ /* ---------- states ---------- */
+
+ let peekTimer = 0;
+
+ function setState(next) {
+ if (isle.dataset.state === next) return;
+ const was = isle.dataset.state;
+ isle.dataset.state = next;
+ body.inert = next !== 'expanded';
+ if (next === 'expanded') {
+ clearTimeout(peekTimer);
+ peek = null;
+ world?.setRunning(true);
+ if (was !== 'expanded') world?.intro();
+ } else if (was === 'expanded') {
+ // Keep drawing while the island closes over the model, then rest.
+ setTimeout(() => { if (isle.dataset.state !== 'expanded') world?.setRunning(false); }, 500);
+ if (focused) { focused = null; world?.focus(null); }
+ }
+ update();
+ }
+
+ // Says what just changed somewhere else, unless the island is open (then you see it anyway).
+ function announce(before) {
+ if (isle.dataset.state === 'expanded') return;
+ const news = describe(before);
+ if (!news) return;
+ peek = news;
+ setState('peek');
+ update();
+ clearTimeout(peekTimer);
+ peekTimer = setTimeout(() => { peek = null; if (isle.dataset.state === 'peek') setState('collapsed'); }, PEEK_MS);
+ }
+
+ function describe(before) {
+ if (state.scene && state.scene !== before.scene) {
+ const s = config.scenes.find((x) => x.id === state.scene);
+ if (s) return { left: ear(s.icon, s.name, '#a9cbff'), right: 'Scene' };
+ }
+ const lights = config.lights.filter((l) => {
+ const [a, b] = [before.lights[l.id], state.lights[l.id]];
+ return a && (a.on !== b.on || Math.abs(a.brightness - b.brightness) > 0.02 || a.color !== b.color);
+ });
+ const acs = config.acs.filter((c) => {
+ const [a, b] = [before.acs[c.id], state.acs[c.id]];
+ return a && (a.on !== b.on || a.target !== b.target || a.mode !== b.mode);
+ });
+ if (acs.length === 1 && !lights.length) {
+ const [cfg, s] = [acs[0], state.acs[acs[0].id]];
+ return { left: ear(s.mode, cfg.name, s.on ? MODE_COLORS[s.mode] : '#8fa3c7'), right: s.on ? `${s.target}°` : 'Off' };
+ }
+ if (lights.length === 1 && !acs.length) {
+ const [cfg, s] = [lights[0], state.lights[lights[0].id]];
+ return { left: ear(cfg.capabilities.length ? cfg.type : 'plug', cfg.name, s.on ? css(lightColor(s, tmp)) : '#8fa3c7'), right: lightLabel(cfg, s) };
+ }
+ if (lights.length + acs.length > 1) {
+ const on = Object.values(state.lights).filter((l) => l.on).length;
+ return { left: ear('home', config.name, on ? '#ffb46b' : '#8fa3c7'), right: on ? `${on} on` : 'All off' };
+ }
+ return null;
+ }
+
+ const copy = () => JSON.parse(JSON.stringify({ lights: state.lights, acs: state.acs, scene: state.scene }));
+ let last = copy();
+ store.on((change) => {
+ if (change.type === 'tick') { for (const row of rows) row(); return; }
+ const before = last;
+ last = copy();
+ update();
+ if (change.remote) announce(before);
+ });
+
+ incoming.islandState = (next) => setState(next === 'expanded' ? 'expanded' : 'collapsed');
+ incoming.adopt = (other) => store.adopt(other);
+ incoming.applyRemote = (remote) => store.applyRemote(remote);
+
+ body.inert = true;
+ update();
+ if (window.__HOUSE_BOOT__?.island?.expanded || query.get('expanded')) setState('expanded');
+ // Without the native shell (vite dev) the pointer drives it, so the page can be worked on.
+ if (!isNative) {
+ shape.addEventListener('pointerenter', () => setState('expanded'));
+ shape.addEventListener('pointerleave', () => setState('collapsed'));
+ }
+ post('ready', { summary: store.summary() });
+ window.__house = { store, world, config, island: { setState, announce } };
+}
diff --git a/web/src/main.js b/web/src/main.js
index 08fe4a9..97ad15f 100644
--- a/web/src/main.js
+++ b/web/src/main.js
@@ -4,7 +4,7 @@ import { createWorld } from './three/world.js';
import { createPanel } from './ui/panel.js';
import { h, openMenu } from './ui/dom.js';
import { icon } from './ui/icons.js';
-import { isNative, post, incoming } from './bridge.js';
+import { isNative, isPhone, post, incoming, haptic } from './bridge.js';
const DEMO_PLANS = [
{ id: 'home', name: 'Demo · Apartment' },
@@ -14,6 +14,15 @@ const STAGE_INSETS = [62, 50]; // header above, panel overlap below
const app = document.getElementById('app');
if (isNative) document.documentElement.classList.add('native');
+if (isPhone) document.documentElement.classList.add('ios');
+
+// Height of the status bar / Dynamic Island on a phone; 0 everywhere else.
+function safeAreaTop() {
+ const probe = document.body.appendChild(h('div', { style: { position: 'fixed', paddingTop: 'env(safe-area-inset-top, 0px)' } }));
+ const top = parseFloat(getComputedStyle(probe).paddingTop) || 0;
+ probe.remove();
+ return top;
+}
function fatal(title, error) {
console.error(error);
@@ -45,7 +54,11 @@ async function boot() {
let world;
const panel = createPanel(app, config, store, {
onFocus: (target) => world?.focus(target),
- onMenu: (anchor, reason) => (reason === 'escape' ? post('close') : planMenu(anchor)),
+ onMenu: (anchor, reason) => {
+ if (reason === 'escape') post('close');
+ else if (isPhone) post('openSettings'); // plans, sync and sign-in are native screens there
+ else planMenu(anchor);
+ },
});
try {
@@ -53,7 +66,7 @@ async function boot() {
onHover: panel.showTooltip,
onPick(target) {
if (!target) return;
- if (target.kind === 'light') store.toggleLight(target.id);
+ if (target.kind === 'light') { haptic('light'); store.toggleLight(target.id); }
else if (target.kind === 'ac') panel.openSheet(target);
else if (target.kind === 'room') panel.selectRoom(target.id);
},
@@ -64,18 +77,33 @@ async function boot() {
}
panel.attach(world);
- world.setInsets(...STAGE_INSETS);
- new ResizeObserver(() => world.resize()).observe(panel.canvas);
+ // On a phone the header sits lower (status bar) but is closer to the model.
+ const applyInsets = () => (isPhone ? world.setInsets(50 + safeAreaTop(), 36) : world.setInsets(...STAGE_INSETS));
+ applyInsets();
+ new ResizeObserver(() => (isPhone ? applyInsets() : world.resize())).observe(panel.canvas);
const show = () => { world.setRunning(true); world.intro(); };
const hide = () => { world.setRunning(false); };
incoming.shown = show;
incoming.hidden = () => { hide(); panel.closeSheet(); if (panel.room !== 'all') panel.selectRoom('all'); };
incoming.setNotch = (x) => panel.setNotch(x);
+ if (isPhone) {
+ // Swiping in from the left edge goes back from a device sheet, as it does everywhere on iOS.
+ let edge = null;
+ window.addEventListener('pointerdown', (ev) => { edge = ev.clientX < 28 ? { x: ev.clientX, y: ev.clientY } : null; }, true);
+ window.addEventListener('pointerup', (ev) => {
+ const swiped = edge && ev.clientX - edge.x > 56 && Math.abs(ev.clientY - edge.y) < 80;
+ edge = null;
+ if (swiped && panel.closeSheet()) haptic('light');
+ }, true);
+ window.addEventListener('pointercancel', () => { edge = null; }, true);
+ }
incoming.applyRemote = (update) => store.applyRemote(update);
+ incoming.adopt = (state) => store.adopt(state);
store.onDeviceError = (id, message) => {
const name = store.lightCfg.get(id)?.name || store.acCfg.get(id)?.name || id;
panel.toast(`${name}: ${message}`);
+ haptic('error');
};
// The loop pauses whenever the page is hidden; any sign of life resumes it.
const resume = () => { if (!document.hidden) world.setRunning(true); };
@@ -125,6 +153,9 @@ const mode = window.__HOUSE_BOOT__?.mode || new URLSearchParams(location.search)
if (mode === 'editor') {
document.documentElement.classList.add('mode-editor');
import('./editor/editor.js').then((m) => m.bootEditor(app)).catch((e) => fatal('Editor failed to start', e));
+} else if (mode === 'island') {
+ document.documentElement.classList.add('mode-island');
+ import('./island/island.js').then((m) => m.bootIsland(app)).catch((e) => console.error(e));
} else {
boot();
}
diff --git a/web/src/store.js b/web/src/store.js
index e734696..831a373 100644
--- a/web/src/store.js
+++ b/web/src/store.js
@@ -57,6 +57,9 @@ export function createStore(config) {
if (change.type !== 'tick') {
persist();
post('summary', summary());
+ // The native shell passes this on to its other views of the same home (the panel
+ // and the island each run their own store), which adopt it without re-sending.
+ if (!change.adopted) post('state', { lights: state.lights, acs: state.acs, scene: state.scene });
}
}
@@ -212,6 +215,17 @@ export function createStore(config) {
emit({ type: 'bulk', scene: sceneId });
},
+ // The state of another view of this home (see emit). Nothing goes to the devices:
+ // the view where the change was made has already sent it.
+ adopt(other) {
+ let any = false;
+ for (const [id, s] of Object.entries(other.lights || {})) any = writeLight(id, s, { remote: true }) || any;
+ for (const [id, s] of Object.entries(other.acs || {})) any = writeAC(id, s, { remote: true }) || any;
+ const scene = other.scene ?? null;
+ if (scene !== state.scene) { state.scene = scene; any = true; }
+ if (any) emit({ type: 'bulk', remote: true, adopted: true });
+ },
+
// State reported by real devices (native provider). Never echoed back as a command.
applyRemote(update) {
let any = false;
diff --git a/web/src/style.css b/web/src/style.css
index e9ccfed..ee63486 100644
--- a/web/src/style.css
+++ b/web/src/style.css
@@ -200,7 +200,7 @@ html.native .card { width: auto; height: auto; box-shadow:
color: var(--muted);
transition: transform 260ms var(--spring), background 200ms, color 200ms, box-shadow 300ms;
}
-.round:hover { background: rgba(255, 255, 255, 0.16); color: var(--text); }
+@media (hover: hover) { .round:hover { background: rgba(255, 255, 255, 0.16); color: var(--text); } }
.round:active { transform: scale(0.9); }
.power {
@@ -213,7 +213,7 @@ html.native .card { width: auto; height: auto; box-shadow:
color: #fff;
box-shadow: 0 8px 22px -4px rgba(61, 139, 255, 0.75), 0 0 0 1px rgba(255, 255, 255, 0.12) inset;
}
-.power.on:hover { background: linear-gradient(180deg, #6aacff, #3a85f8); }
+@media (hover: hover) { .power.on:hover { background: linear-gradient(180deg, #6aacff, #3a85f8); } }
.top .round { transition: transform 260ms var(--spring), background 200ms, color 200ms, box-shadow 300ms, opacity 220ms; }
.card.detail .top .round { opacity: 0; transform: scale(0.8); pointer-events: none; }
@@ -259,7 +259,7 @@ html.native .card { width: auto; height: auto; box-shadow:
-webkit-backdrop-filter: blur(18px);
transition: transform 300ms var(--spring), background 220ms, color 220ms, border-color 220ms, box-shadow 300ms;
}
-.scene:hover { background: var(--glass-hi); color: var(--text); }
+@media (hover: hover) { .scene:hover { background: var(--glass-hi); color: var(--text); } }
.scene:active { transform: scale(0.94); }
.scene.active {
color: #fff;
@@ -294,7 +294,7 @@ html.native .card { width: auto; height: auto; box-shadow:
border: 1px solid transparent;
transition: background 220ms, color 220ms, border-color 220ms, transform 260ms var(--spring);
}
-.room:hover { color: var(--text); background: var(--glass); }
+@media (hover: hover) { .room:hover { color: var(--text); background: var(--glass); } }
.room:active { transform: scale(0.95); }
.room.active { color: var(--text); background: var(--glass-hi); border-color: var(--line); }
.room .count {
@@ -324,7 +324,7 @@ html.native .card { width: auto; height: auto; box-shadow:
background: var(--glass); border: 1px solid var(--line);
transition: background 200ms, color 200ms, transform 260ms var(--spring);
}
-.pill:hover { background: var(--glass-hi); color: var(--text); }
+@media (hover: hover) { .pill:hover { background: var(--glass-hi); color: var(--text); } }
.pill:active { transform: scale(0.93); }
.devices {
@@ -374,7 +374,7 @@ html.native .card { width: auto; height: auto; box-shadow:
@keyframes tile-in {
from { opacity: 0; transform: translateY(10px) scale(0.97); }
}
-.tile:hover { background: var(--glass-hi); }
+@media (hover: hover) { .tile:hover { background: var(--glass-hi); } }
.tile.pressed { transform: scale(0.96); }
.tile.on {
background: linear-gradient(135deg, color-mix(in srgb, var(--c) 26%, transparent), color-mix(in srgb, var(--c) 8%, transparent));
@@ -410,8 +410,9 @@ html.native .card { width: auto; height: auto; box-shadow:
opacity: 0;
transition: opacity 180ms, background 180ms, color 180ms;
}
-.tile:hover .tile-more, .tile-more:focus-visible { opacity: 1; }
-.tile-more:hover { background: rgba(255, 255, 255, 0.12); color: var(--text); }
+.tile-more:focus-visible { opacity: 1; }
+@media (hover: hover) { .tile:hover .tile-more { opacity: 1; } }
+@media (hover: hover) { .tile-more:hover { background: rgba(255, 255, 255, 0.12); color: var(--text); } }
.tile-level {
position: absolute;
@@ -435,7 +436,7 @@ html.native .card { width: auto; height: auto; box-shadow:
display: grid; place-items: center; color: var(--muted);
transition: background 180ms, color 180ms, transform 240ms var(--spring);
}
-.stepper button:hover { background: rgba(255, 255, 255, 0.12); color: var(--text); }
+@media (hover: hover) { .stepper button:hover { background: rgba(255, 255, 255, 0.12); color: var(--text); } }
.stepper button:active { transform: scale(0.86); }
.stepper .value { min-width: 38px; text-align: center; font-size: 16px; font-weight: 650; font-variant-numeric: tabular-nums; }
.tile.ac:not(.on) .stepper .value { color: var(--faint); }
@@ -447,7 +448,7 @@ html.native .card { width: auto; height: auto; box-shadow:
color: var(--muted);
transition: background 220ms, color 220ms, transform 260ms var(--spring), box-shadow 300ms;
}
-.mini-power:hover { background: rgba(255, 255, 255, 0.16); color: var(--text); }
+@media (hover: hover) { .mini-power:hover { background: rgba(255, 255, 255, 0.16); color: var(--text); } }
.mini-power:active { transform: scale(0.88); }
.tile.ac.on .mini-power { background: var(--c); color: #06162c; box-shadow: 0 0 18px -4px var(--c); }
@@ -504,7 +505,7 @@ html.native .card { width: auto; height: auto; box-shadow:
background: var(--glass); border: 1px solid var(--line);
transition: background 220ms, border-color 220ms;
}
-.toggle-chip:hover { background: var(--glass-hi); }
+@media (hover: hover) { .toggle-chip:hover { background: var(--glass-hi); } }
.toggle-chip .icon { color: var(--muted); transition: color 220ms; }
.toggle-chip.on .icon { color: #6ee7a0; }
.switch { width: 38px; height: 23px; border-radius: 12px; background: rgba(255, 255, 255, 0.16); position: relative; transition: background 240ms; }
@@ -576,7 +577,7 @@ html.native .card { width: auto; height: auto; box-shadow:
border: 1px solid var(--line);
transition: background 220ms, border-color 220ms, transform 300ms var(--spring), box-shadow 300ms;
}
-.option:hover { background: var(--glass-hi); }
+@media (hover: hover) { .option:hover { background: var(--glass-hi); } }
.option:active { transform: scale(0.95); }
.option.open, .option.active {
background: linear-gradient(180deg, rgba(88, 150, 255, 0.26), rgba(61, 139, 255, 0.1));
@@ -613,7 +614,7 @@ html.native .card { width: auto; height: auto; box-shadow:
font-size: 13px; font-weight: 500; color: var(--muted); text-align: left;
transition: background 140ms, color 140ms;
}
-.menu button:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }
+@media (hover: hover) { .menu button:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); } }
.menu button.selected { background: rgba(70, 130, 255, 0.28); color: #fff; font-weight: 650; }
.menu button.selected .icon { color: #8fbcff; }
.menu hr { border: 0; height: 1px; margin: 5px 8px; background: var(--line); }
@@ -654,11 +655,93 @@ html.native .card { width: auto; height: auto; box-shadow:
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.1) inset;
transition: transform 260ms var(--spring), border-color 200ms, box-shadow 260ms;
}
-.swatch:hover { transform: translateY(-2px); }
+@media (hover: hover) { .swatch:hover { transform: translateY(-2px); } }
.swatch.selected { border-color: #fff; box-shadow: 0 6px 18px -4px var(--sw); }
.sheet-note { margin-top: auto; padding: 12px 14px; border-radius: 16px; background: var(--glass); border: 1px solid var(--line); font-size: 12px; line-height: 1.45; color: var(--muted); display: flex; gap: 10px; align-items: center; }
+/* ---------- iPhone ---------- */
+/* The same panel fills the whole screen: no card, no notch, safe areas respected,
+ and controls sized for a thumb instead of a pointer. */
+
+html.ios, html.ios body { background: var(--bg); }
+html.ios * { -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; }
+html.ios #app { padding: 0; place-items: stretch; }
+html.ios .card {
+ --stage-h: min(470px, 52%);
+ --safe-top: env(safe-area-inset-top, 0px);
+ --safe-bottom: env(safe-area-inset-bottom, 0px);
+ width: auto;
+ height: auto;
+ border-radius: 0;
+ box-shadow: none;
+}
+html.ios .card-clip { border-radius: 0; box-shadow: none; }
+html.ios .notch { display: none; }
+
+html.ios .top { padding: calc(var(--safe-top) + 6px) 18px 0 20px; }
+html.ios .stage::after { height: calc(var(--safe-top) + 96px); }
+html.ios .home-badge { width: 38px; height: 38px; border-radius: 12px; }
+html.ios .home-name { font-size: 18px; }
+html.ios .home-sub { font-size: 13px; }
+html.ios .round { width: 42px; height: 42px; }
+html.ios .power { width: 50px; height: 50px; }
+
+html.ios .panel { padding: 0 16px; }
+html.ios .sheet { padding: 0 20px calc(var(--safe-bottom) + 14px); }
+html.ios .scene { height: 68px; border-radius: 20px; font-size: 12.5px; }
+html.ios .room { height: 38px; padding: 0 15px; font-size: 14px; }
+html.ios .list-head { font-size: 13px; margin: 12px 4px 10px; }
+html.ios .pill { height: 32px; padding: 0 14px; font-size: 13px; }
+html.ios .devices {
+ margin: 0 -16px;
+ padding: 0 16px calc(var(--safe-bottom) + 18px);
+ overscroll-behavior: contain;
+ -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 10px, #000 calc(100% - 30px), transparent 100%);
+ mask-image: linear-gradient(180deg, transparent 0, #000 10px, #000 calc(100% - 30px), transparent 100%);
+}
+html.ios .grid { gap: 10px; }
+html.ios .group-label { font-size: 11.5px; margin-top: 10px; }
+html.ios .tile { height: 76px; border-radius: 22px; padding: 0 13px; }
+html.ios .bubble { width: 42px; height: 42px; border-radius: 14px; }
+html.ios .tile-name { font-size: 14.5px; }
+html.ios .tile-sub { font-size: 12.5px; }
+html.ios .grid-hint { grid-column: 1 / -1; margin: 10px 4px 0; text-align: center; font-size: 12.5px; color: var(--faint); }
+html.ios .tile.dimmable { touch-action: pan-y; }
+html.ios .stepper button { width: 36px; height: 38px; border-radius: 12px; }
+html.ios .stepper .value { font-size: 18px; min-width: 40px; }
+html.ios .mini-power { width: 40px; height: 40px; }
+
+html.ios .sheet-name { font-size: 18px; }
+html.ios .sheet-sub { font-size: 13px; }
+html.ios .big { font-size: 84px; }
+html.ios .big small { font-size: 22px; }
+html.ios .slider-row { margin-top: 22px; }
+html.ios .slider-row .round { width: 46px; height: 46px; }
+html.ios .slider-track, html.ios .slider-thumb { height: 52px; border-radius: 17px; }
+html.ios .slider-thumb { width: 52px; }
+html.ios .slider-fill { width: calc(var(--p) * (100% - 52px) + 52px); border-radius: 17px; }
+html.ios .slider-thumb { left: calc(var(--p) * (100% - 52px)); }
+html.ios .slider-thumb::after { inset: 17px 24px; }
+html.ios .slider.compact .slider-track, html.ios .slider.compact .slider-thumb { height: 44px; }
+html.ios .slider.compact .slider-thumb::after { inset: 13px 24px; }
+html.ios .slider-scale, html.ios .field-label { font-size: 13px; }
+html.ios .field-label { margin-top: 18px; }
+html.ios .option { height: 108px; border-radius: 22px; padding: 14px 12px; }
+html.ios .option-label { font-size: 13px; }
+html.ios .option-value { font-size: 14.5px; }
+html.ios .toggle-chip { height: 48px; font-size: 14px; }
+html.ios .pad { max-height: 150px; border-radius: 18px; }
+html.ios .swatch { height: 36px; border-radius: 12px; }
+html.ios .swatches { margin-top: 12px; }
+html.ios .sheet-note { font-size: 13px; }
+html.ios .menu { min-width: 190px; border-radius: 20px; }
+html.ios .menu button { height: 46px; font-size: 15px; border-radius: 14px; }
+html.ios .menu button:active { background: rgba(255, 255, 255, 0.1); color: var(--text); }
+html.ios .panel-toast { bottom: calc(var(--safe-bottom) + 14px); font-size: 13.5px; }
+html.ios .tooltip { display: none; }
+html.ios .fatal { margin: calc(env(safe-area-inset-top, 0px) + 20px) 16px 0; width: auto; }
+
/* ---------- boot / error ---------- */
.fatal {
diff --git a/web/src/three/world.js b/web/src/three/world.js
index 6c5a14d..b254bb6 100644
--- a/web/src/three/world.js
+++ b/web/src/three/world.js
@@ -16,8 +16,10 @@ const clamp = THREE.MathUtils.clamp;
const ORBIT_AZ = 40; // degrees of freedom either side of the plan's default view
const EL_MIN = 26;
const EL_MAX = 70;
+const ZOOM_MIN = 0.55; // closest
+const ZOOM_MAX = 1.2;
-export function createWorld(canvas, config, store, { onHover, onPick }) {
+export function createWorld(canvas, config, store, { onHover, onPick, background = STAGE_BG }) {
const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, alpha: false, powerPreference: 'high-performance' });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.toneMapping = THREE.ACESFilmicToneMapping;
@@ -27,7 +29,7 @@ export function createWorld(canvas, config, store, { onHover, onPick }) {
renderer.shadowMap.autoUpdate = false; // the model is static: shadow maps are rendered once
const scene = new THREE.Scene();
- scene.background = new THREE.Color(STAGE_BG);
+ scene.background = new THREE.Color(background);
const camera = new THREE.PerspectiveCamera(24, 1, 0.5, 120);
@@ -227,11 +229,36 @@ export function createWorld(canvas, config, store, { onHover, onPick }) {
wake();
}
+ // Two fingers pinch to zoom, the way the scroll wheel does with a mouse.
+ const fingers = new Map();
+ let pinch = null;
+ const spread = () => {
+ const [a, b] = [...fingers.values()];
+ return Math.max(1, Math.hypot(a.x - b.x, a.y - b.y));
+ };
+
canvas.addEventListener('pointerdown', (ev) => {
canvas.setPointerCapture(ev.pointerId);
+ fingers.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
+ if (fingers.size === 2) {
+ pinch = { spread: spread(), zoom: rig.zoom };
+ drag = null;
+ setHover(null);
+ return;
+ }
+ if (fingers.size > 2) return;
drag = { x: ev.clientX, y: ev.clientY, az: rig.goal.az, el: rig.goal.el, moved: false };
});
canvas.addEventListener('pointermove', (ev) => {
+ if (fingers.has(ev.pointerId)) fingers.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
+ if (pinch) {
+ if (fingers.size >= 2) {
+ rig.zoom = clamp(pinch.zoom * pinch.spread / spread(), ZOOM_MIN, ZOOM_MAX);
+ frameGoal();
+ wake();
+ }
+ return;
+ }
if (drag) {
const dx = ev.clientX - drag.x;
const dy = ev.clientY - drag.y;
@@ -247,6 +274,8 @@ export function createWorld(canvas, config, store, { onHover, onPick }) {
setHover(pick(ev));
});
const release = (ev) => {
+ fingers.delete(ev.pointerId);
+ if (pinch) { if (!fingers.size) pinch = null; return; } // lifting after a pinch picks nothing
if (!drag) return;
const wasDrag = drag.moved;
drag = null;
@@ -259,11 +288,15 @@ export function createWorld(canvas, config, store, { onHover, onPick }) {
wake();
};
canvas.addEventListener('pointerup', release);
- canvas.addEventListener('pointercancel', () => { drag = null; });
+ canvas.addEventListener('pointercancel', (ev) => {
+ fingers.delete(ev.pointerId);
+ if (!fingers.size) pinch = null;
+ drag = null;
+ });
canvas.addEventListener('pointerleave', () => { if (!drag) setHover(null); });
canvas.addEventListener('wheel', (ev) => {
ev.preventDefault();
- rig.zoom = clamp(rig.zoom * Math.exp(ev.deltaY * 0.0015), 0.55, 1.2);
+ rig.zoom = clamp(rig.zoom * Math.exp(ev.deltaY * 0.0015), ZOOM_MIN, ZOOM_MAX);
frameGoal();
wake();
}, { passive: false });
diff --git a/web/src/ui/dom.js b/web/src/ui/dom.js
index aea67d0..3b6055a 100644
--- a/web/src/ui/dom.js
+++ b/web/src/ui/dom.js
@@ -1,3 +1,5 @@
+import { haptic } from '../bridge.js';
+
// Minimal hyperscript helper: h('div.class', { onclick, style, ... }, ...children)
export function h(spec, props, ...children) {
const [tag, ...classes] = spec.split('.');
@@ -24,7 +26,8 @@ export function h(spec, props, ...children) {
export const clamp = (v, a, b) => Math.min(b, Math.max(a, v));
// Horizontal slider with a gradient fill that reveals under a chunky thumb.
-export function createSlider({ min, max, step = 1, value, gradient, compact = false, labels, onInput }) {
+// `detent` is how far the value travels between two haptic ticks on a phone.
+export function createSlider({ min, max, step = 1, value, gradient, compact = false, labels, onInput, detent = step }) {
const fill = h('div.slider-fill');
const thumb = h('div.slider-thumb');
const track = h('div.slider-track', { vars: { '--gradient': gradient } }, fill, thumb);
@@ -32,6 +35,14 @@ export function createSlider({ min, max, step = 1, value, gradient, compact = fa
labels ? h('div.slider-scale', h('span', labels[0]), h('span', labels[1])) : null);
let current = value;
+ let notch = Math.round(value / detent);
+ const input = (next) => {
+ current = next;
+ paint();
+ const at = Math.round(next / detent);
+ if (at !== notch) { notch = at; haptic('tick'); }
+ onInput(current);
+ };
const paint = () => {
el.style.setProperty('--p', String(clamp((current - min) / (max - min), 0, 1)));
};
@@ -49,12 +60,12 @@ export function createSlider({ min, max, step = 1, value, gradient, compact = fa
track.setPointerCapture(ev.pointerId);
el.classList.add('dragging');
const next = fromEvent(ev);
- if (next !== current) { current = next; paint(); onInput(current); }
+ if (next !== current) input(next);
});
track.addEventListener('pointermove', (ev) => {
if (!el.classList.contains('dragging')) return;
const next = fromEvent(ev);
- if (next !== current) { current = next; paint(); onInput(current); }
+ if (next !== current) input(next);
});
const end = () => el.classList.remove('dragging');
track.addEventListener('pointerup', end);
@@ -63,7 +74,7 @@ export function createSlider({ min, max, step = 1, value, gradient, compact = fa
paint();
return {
el,
- set(v) { if (v !== current && !el.classList.contains('dragging')) { current = v; paint(); } },
+ set(v) { if (v !== current && !el.classList.contains('dragging')) { current = v; notch = Math.round(v / detent); paint(); } },
};
}
diff --git a/web/src/ui/panel.js b/web/src/ui/panel.js
index 4ab65da..a9ee2af 100644
--- a/web/src/ui/panel.js
+++ b/web/src/ui/panel.js
@@ -6,6 +6,7 @@ import { icon } from './icons.js';
import { lightColor } from '../three/lights.js';
import { MODE_COLORS } from '../three/ac.js';
import { AC_SWINGS } from '../store.js';
+import { haptic, isPhone } from '../bridge.js';
const MODE_LABEL = { cool: 'Cool', heat: 'Heat', dry: 'Dry', fan: 'Fan', auto: 'Auto' };
const FAN_LABEL = { auto: 'Auto', low: 'Low', medium: 'Medium', high: 'High', turbo: 'Turbo' };
@@ -73,7 +74,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
const sceneButtons = new Map();
for (const s of config.scenes) {
- const b = h('button.scene', { onclick: () => store.applyScene(s.id) }, icon(s.icon, 19), s.name);
+ const b = h('button.scene', { onclick: () => { haptic('medium'); store.applyScene(s.id); } }, icon(s.icon, 19), s.name);
sceneButtons.set(s.id, b);
scenes.append(b);
}
@@ -84,7 +85,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
const roomButtons = new Map();
for (const r of [{ id: 'all', name: 'All' }, ...config.rooms]) {
const count = h('span.count.zero', '0');
- const b = h('button.room', { onclick: () => selectRoom(r.id) }, r.name, count);
+ const b = h('button.room', { onclick: () => { haptic('tick'); selectRoom(r.id); } }, r.name, count);
roomButtons.set(r.id, { b, count });
rooms.append(b);
}
@@ -103,11 +104,13 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
}
function bulk(on) {
+ haptic('medium');
if (ui.room === 'all') { store.setAllLights(on); return; }
for (const l of config.lights) if (l.room === ui.room) store.setLight(l.id, { on });
}
function toggleEverything() {
+ haptic('heavy');
const anyOn = Object.values(state.lights).some((l) => l.on) || Object.values(state.acs).some((a) => a.on);
if (anyOn) {
store.setAllLights(false);
@@ -125,7 +128,8 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
const dimmable = cfg.capabilities.includes('brightness');
const hasSheet = cfg.capabilities.length > 0;
const sub = h('div.tile-sub');
- const more = hasSheet
+ // A phone has no room for the button: touching and holding the tile opens the sheet there.
+ const more = hasSheet && !isPhone
? h('button.tile-more', { 'aria-label': `${cfg.name} settings`, onclick: (e) => { e.stopPropagation(); openSheet({ kind: 'light', id: cfg.id }); } }, icon('sliders', 15))
: null;
const el = h(`div.tile${dimmable ? '.dimmable' : ''}`, { role: 'button', tabindex: 0, 'aria-label': cfg.name },
@@ -140,21 +144,38 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
if (ev.target.closest('.tile-more') || ev.button !== 0) return;
el.setPointerCapture(ev.pointerId);
const s = state.lights[cfg.id];
- gesture = { x: ev.clientX, start: s.on ? s.brightness : 0, moved: false };
+ gesture = { x: ev.clientX, y: ev.clientY, start: s.on ? s.brightness : 0, moved: false, step: null };
el.classList.add('pressed');
+ if (isPhone && hasSheet) {
+ gesture.hold = setTimeout(() => {
+ if (!gesture || gesture.moved) return;
+ gesture = null;
+ el.classList.remove('pressed');
+ haptic('medium');
+ openSheet({ kind: 'light', id: cfg.id });
+ }, 420);
+ }
});
el.addEventListener('pointermove', (ev) => {
if (!gesture) return;
const dx = ev.clientX - gesture.x;
- if (!gesture.moved && dimmable && Math.abs(dx) > 6) { gesture.moved = true; el.classList.add('dragging'); }
+ if (Math.hypot(dx, ev.clientY - gesture.y) > 8) clearTimeout(gesture.hold);
+ // A mostly vertical move is the list being scrolled, not a dim.
+ if (!gesture.moved && dimmable && Math.abs(dx) > 6 && Math.abs(dx) > Math.abs(ev.clientY - gesture.y)) {
+ gesture.moved = true;
+ el.classList.add('dragging');
+ }
if (gesture.moved) {
const b = clamp(gesture.start + dx / el.clientWidth, 0, 1);
+ const step = Math.round(b * 20);
+ if (step !== gesture.step) { if (gesture.step !== null) haptic('tick'); gesture.step = step; }
store.setLight(cfg.id, b < 0.04 ? { on: false } : { on: true, brightness: b });
}
});
const finish = (ev) => {
if (!gesture) return;
const tapped = !gesture.moved && ev.type === 'pointerup';
+ clearTimeout(gesture.hold);
gesture = null;
el.classList.remove('pressed', 'dragging');
if (tapped) toggleLight(cfg.id);
@@ -172,7 +193,8 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
el.classList.toggle('on', s.on);
el.style.setProperty('--c', css(lightColor(s, tmp)));
el.style.setProperty('--level', s.on ? String(s.brightness) : '0');
- sub.textContent = ui.room === 'all' ? `${lightStatus(cfg, s)} · ${roomName.get(cfg.room)}` : lightStatus(cfg, s);
+ // On a phone the group heading above the tile already names the room.
+ sub.textContent = ui.room === 'all' && !isPhone ? `${lightStatus(cfg, s)} · ${roomName.get(cfg.room)}` : lightStatus(cfg, s);
},
};
}
@@ -183,6 +205,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
const bubble = h('div.bubble');
const step = (d) => (e) => {
e.stopPropagation();
+ haptic('tick');
const s = state.acs[cfg.id];
store.setAC(cfg.id, { on: true, target: s.target + d });
};
@@ -193,7 +216,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
h('button', { 'aria-label': 'Cooler', onclick: step(-1) }, icon('minus', 15, 2.2)),
value,
h('button', { 'aria-label': 'Warmer', onclick: step(1) }, icon('plus', 15, 2.2))),
- h('button.mini-power', { 'aria-label': `${cfg.name} power`, onclick: (e) => { e.stopPropagation(); store.setAC(cfg.id, { on: !state.acs[cfg.id].on }); } }, icon('power', 16, 2.2)));
+ h('button.mini-power', { 'aria-label': `${cfg.name} power`, onclick: (e) => { e.stopPropagation(); haptic('medium'); store.setAC(cfg.id, { on: !state.acs[cfg.id].on }); } }, icon('power', 16, 2.2)));
el.addEventListener('keydown', (ev) => { if (ev.key === 'Enter') openSheet({ kind: 'ac', id: cfg.id }); });
let shownMode = null;
return {
@@ -204,7 +227,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
el.style.setProperty('--c', MODE_COLORS[s.mode]);
if (shownMode !== s.mode) { bubble.replaceChildren(icon(s.mode, 19)); shownMode = s.mode; }
value.textContent = `${s.target}°`;
- sub.textContent = `${s.on ? MODE_LABEL[s.mode] : 'Off'} · Room ${s.roomTemp.toFixed(1)}°`;
+ sub.textContent = `${s.on ? MODE_LABEL[s.mode] : 'Off'} · Room ${s.roomTemp.toFixed(isPhone ? 0 : 1)}°`;
},
};
}
@@ -213,6 +236,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
for (const cfg of config.lights) tiles.set(cfg.id, lightTile(cfg));
function toggleLight(id) {
+ haptic('light');
world?.lights.get(id)?.bump();
store.toggleLight(id);
}
@@ -230,6 +254,9 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
devices.filter((t) => t.kind === 'light').forEach(add);
}
if (!nodes.length) nodes.push(h('div.group-label', 'No devices in this room yet'));
+ else if (isPhone && config.lights.some((l) => l.capabilities.length && inScope(l))) {
+ nodes.push(h('div.grid-hint', 'Touch and hold a light for brightness and colour'));
+ }
grid.replaceChildren(...nodes);
grid.parentElement.scrollTop = 0;
}
@@ -298,9 +325,9 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
const s = () => state.acs[id];
const set = (patch) => store.setAC(id, patch);
const sub = h('div.sheet-sub');
- const powerBtn = h('button.round.power', { 'aria-label': 'Power', onclick: () => set({ on: !s().on }) }, icon('power', 21, 2.2));
+ const powerBtn = h('button.round.power', { 'aria-label': 'Power', onclick: () => { haptic('medium'); set({ on: !s().on }); } }, icon('power', 21, 2.2));
const big = bigNumber('°C');
- const eco = h('button.toggle-chip', { onclick: () => set({ eco: !s().eco }) }, icon('leaf', 17), 'Eco Mode', h('span.switch'));
+ const eco = h('button.toggle-chip', { onclick: () => { haptic('light'); set({ eco: !s().eco }); } }, icon('leaf', 17), 'Eco Mode', h('span.switch'));
const slider = createSlider({
min: cfg.min, max: cfg.max, value: s().target, gradient: TEMP_GRADIENT,
labels: [`${cfg.min}°`, `${cfg.max}°`],
@@ -315,7 +342,7 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
el.classList.add('open');
openMenu(clip, el, values().map((v) => ({
label: labels[v], selected: s()[key] === v, icon: s()[key] === v ? icon('check', 16, 2.4) : null,
- action: () => set(key === 'mode' ? { on: true, mode: v } : { [key]: v }),
+ action: () => { haptic('light'); set(key === 'mode' ? { on: true, mode: v } : { [key]: v }); },
})), { onClose: () => el.classList.remove('open') });
} }, holder, h('span.corner', icon('down', 15, 2.2)), h('div.option-label', label), value);
let shown = null;
@@ -335,10 +362,10 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
};
const mode = option('mode', 'Mode', MODE_LABEL, () => features().modes, null);
- const fan = option('fan', 'Fan speed', FAN_LABEL, () => features().fans, 'fan');
+ const fan = option('fan', isPhone ? 'Fan' : 'Fan speed', FAN_LABEL, () => features().fans, 'fan');
const swing = option('swing', 'Airflow', SWING_LABEL, () => AC_SWINGS, 'wind');
const sleepValue = h('div.option-value');
- const sleep = h('button.option', { onclick: () => set({ sleep: !s().sleep }) },
+ const sleep = h('button.option', { onclick: () => { haptic('light'); set({ sleep: !s().sleep }); } },
icon('moon', 24, 1.6), h('span.corner.dot'), h('div.option-label', 'Sleep'), sleepValue);
const options = h('div.options', mode.el, fan.el, sleep, swing.el);
@@ -347,9 +374,9 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
sheetHead(cfg.name, sub, powerBtn),
h('div.readout', big.el, eco),
h('div.slider-row',
- h('button.round', { 'aria-label': 'Cooler', onclick: () => set({ on: true, target: s().target - 1 }) }, icon('minus', 18, 2.2)),
+ h('button.round', { 'aria-label': 'Cooler', onclick: () => { haptic('tick'); set({ on: true, target: s().target - 1 }); } }, icon('minus', 18, 2.2)),
slider.el,
- h('button.round', { 'aria-label': 'Warmer', onclick: () => set({ on: true, target: s().target + 1 }) }, icon('plus', 18, 2.2))),
+ h('button.round', { 'aria-label': 'Warmer', onclick: () => { haptic('tick'); set({ on: true, target: s().target + 1 }); } }, icon('plus', 18, 2.2))),
options,
],
update() {
@@ -386,19 +413,19 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
if (caps.includes('brightness')) {
sliders.brightness = createSlider({
- min: 5, max: 100, value: Math.round(s().brightness * 100), gradient: DIM_GRADIENT,
+ min: 5, max: 100, value: Math.round(s().brightness * 100), gradient: DIM_GRADIENT, detent: 5,
onInput: (v) => set({ on: true, brightness: v / 100 }),
});
nodes.push(h('div.slider-row',
- h('button.round', { 'aria-label': 'Dimmer', onclick: () => set({ on: true, brightness: s().brightness - 0.1 }) }, icon('dim', 18)),
+ h('button.round', { 'aria-label': 'Dimmer', onclick: () => { haptic('tick'); set({ on: true, brightness: s().brightness - 0.1 }); } }, icon('dim', 18)),
sliders.brightness.el,
- h('button.round', { 'aria-label': 'Brighter', onclick: () => set({ on: true, brightness: s().brightness + 0.1 }) }, icon('bright', 18))));
+ h('button.round', { 'aria-label': 'Brighter', onclick: () => { haptic('tick'); set({ on: true, brightness: s().brightness + 0.1 }); } }, icon('bright', 18))));
}
let kelvinLabel = null;
if (caps.includes('temp')) {
kelvinLabel = h('span');
sliders.temp = createSlider({
- min: 2200, max: 6500, step: 100, value: s().temp, gradient: KELVIN_GRADIENT, compact: true,
+ min: 2200, max: 6500, step: 100, value: s().temp, gradient: KELVIN_GRADIENT, compact: true, detent: 500,
onInput: (v) => set({ on: true, temp: v }),
});
nodes.push(h('div.field-label', h('span', 'Colour temperature'), kelvinLabel), h('div.slider-row', sliders.temp.el));
@@ -409,11 +436,11 @@ export function createPanel(root, config, store, { onFocus, onMenu }) {
if (caps.includes('color')) {
colorLabel = h('span');
pad = createColorPad({ value: s().color, onInput: (c) => set({ on: true, color: c }) });
- swatches = SWATCHES.map((c) => h('button.swatch', { 'aria-label': `Colour ${c}`, vars: { '--sw': c }, onclick: () => set({ on: true, color: c }) }));
+ swatches = SWATCHES.map((c) => h('button.swatch', { 'aria-label': `Colour ${c}`, vars: { '--sw': c }, onclick: () => { haptic('light'); set({ on: true, color: c }); } }));
nodes.push(h('div.field-label', h('span', 'Colour'), colorLabel), pad.el, h('div.swatches', swatches));
} else {
nodes.push(h('div.sheet-note', icon(cfg.type, 20),
- `${roomName.get(cfg.room)} · drag across a light tile to dim it, or click the lamp in the 3D view to toggle.`));
+ `${roomName.get(cfg.room)} · drag across a light tile to dim it, or ${isPhone ? 'tap' : 'click'} the lamp in the 3D view to toggle.`));
}
return {