feat: reveal device cards progressively with camera zoom
This commit is contained in:
1 parent
e62f987563
commit
a2a119d3ee
18 files changed
+458
-265
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import type { FurniturePowerCard } from "./furniture-power-card.ts";
|
||||
import type { HouseCard, HydronicCard } from "./cards.ts";
|
||||
import { cardDetail, cardVisibleAtZoom } from "./card-visibility.ts";
|
||||
|
||||
const power=(w: number|null): FurniturePowerCard=>({kind:"power",id:"wallbox",name:"Wallbox",powerEntity:"sensor.power",power:w,at:{floorId:"f",x:0,y:1,z:0}});
|
||||
const house: HouseCard={kind:"house",at:{floorId:"f",x:0,y:5,z:0},summary:{grid:0,solar:0,battery:null,soc:null,tariff:null,consumption:0},autarky:null};
|
||||
const hydraulic: HydronicCard={kind:"hydronic",at:{floorId:"f",x:0,y:2,z:0},readOnly:false,item:{id:"h",type:"heating_manifold",x:0,z:0,w:1.5,d:.35,h:1.1,rotation:0,variant:null}};
|
||||
|
||||
test("distant views retain the summary while small device cards wait for more zoom",()=>{
|
||||
assert.equal(cardVisibleAtZoom(house,0,false),true);
|
||||
for(const card of [power(0),power(7400),hydraulic]) assert.equal(cardVisibleAtZoom(card,15,false),false);
|
||||
assert.equal(cardVisibleAtZoom(hydraulic,50,false),true);
|
||||
assert.equal(cardVisibleAtZoom(power(7400),60,false),true);
|
||||
assert.equal(cardVisibleAtZoom(power(0),60,false),false);
|
||||
assert.equal(cardVisibleAtZoom(power(0),90,false),true);
|
||||
});
|
||||
|
||||
test("live consumption and export appear before idle devices, without discarding the zero value",()=>{
|
||||
assert.ok(cardDetail(power(7400)).priority>cardDetail(power(0)).priority);
|
||||
assert.equal(cardVisibleAtZoom(power(-1500),60,false),true);
|
||||
assert.equal(cardVisibleAtZoom(power(0),90,false),true);
|
||||
assert.equal(power(0).power,0);
|
||||
assert.equal(cardVisibleAtZoom(power(null),40,false),false);
|
||||
});
|
||||
|
||||
test("hysteresis keeps a visible card stable near its boundary and restores it after zooming back in",()=>{
|
||||
const c=power(0),threshold=cardDetail(c).density;
|
||||
assert.equal(cardVisibleAtZoom(c,threshold,false),false);
|
||||
assert.equal(cardVisibleAtZoom(c,threshold,true),true);
|
||||
assert.equal(cardVisibleAtZoom(c,threshold*.85,true),false);
|
||||
assert.equal(cardVisibleAtZoom(c,threshold*1.15,false),true);
|
||||
for(const density of [NaN,Infinity,-1,0]) assert.equal(cardVisibleAtZoom(c,density,true),false);
|
||||
});
|
||||
|
||||
test("charging telemetry follows detail zoom even without a power link",()=>{
|
||||
const c={...power(null),powerEntity:null,readings:[{id:"soc",entity:"sensor.soc",label:"SoC",icon:"🔋",text:"0 %"}]};
|
||||
assert.equal(cardVisibleAtZoom(c,30,false),false);
|
||||
assert.equal(cardVisibleAtZoom(c,70,false),true);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { LiveCard } from "./cards.ts";
|
||||
|
||||
/** Screen pixels per plan metre at the anchor, independent of building size and camera angle. */
|
||||
export function cardDetail(card: LiveCard): { density: number; priority: number } {
|
||||
switch (card.kind) {
|
||||
case "house": return { density:0,priority:100 };
|
||||
case "roomclimate": return { density:24,priority:80 };
|
||||
case "hydronic": return { density:42,priority:45 };
|
||||
case "power": {
|
||||
const active=card.power!==null&&Math.abs(card.power)>1;
|
||||
return active ? {density:48,priority:60} : card.readings?.length ? {density:56,priority:30} : {density:card.power===null?64:72,priority:20};
|
||||
}
|
||||
case "car": return {density:card.car.charging?36:60,priority:card.car.charging?60:20};
|
||||
case "printer": return {density:card.printer.printing?36:60,priority:card.printer.printing?60:20};
|
||||
case "heatpump": return {density:card.pump.mode!=="idle"?36:60,priority:card.pump.mode!=="idle"?60:20};
|
||||
case "media": return {density:48,priority:40};
|
||||
}
|
||||
}
|
||||
|
||||
/** A small dead band prevents cards flickering at a zoom boundary. Summary cards keep their existing visibility. */
|
||||
export function cardVisibleAtZoom(card: LiveCard, density: number, previouslyVisible: boolean): boolean {
|
||||
const limit=cardDetail(card).density;
|
||||
if (!limit) return true;
|
||||
return Number.isFinite(density) && density >= limit * (previouslyVisible ? .88 : 1.12);
|
||||
}
|
||||
@@ -246,7 +246,7 @@ function mediaCard(hass: HomeAssistant | undefined, c: MediaCard, i: number, act
|
||||
* before it (lower on screen first) moves up above it. Its thread to the anchor stretches.
|
||||
*/
|
||||
export function unstackLhCards(root: ParentNode): void {
|
||||
const cards = [...root.querySelectorAll<HTMLElement>(".live-card")].filter((el) => el.style.visibility !== "hidden");
|
||||
const cards = [...root.querySelectorAll<HTMLElement>(".live-card")].filter((el) => el.style.visibility !== "hidden" && el.offsetWidth > 0);
|
||||
const placed: { x0: number; x1: number; y0: number; y1: number }[] = [];
|
||||
// a card near the edge slides back into the view (on a phone it was cut off); its thread still points home
|
||||
const width = cards[0]?.parentElement?.clientWidth ?? 0;
|
||||
@@ -258,7 +258,7 @@ export function unstackLhCards(root: ParentNode): void {
|
||||
el.style.setProperty("--live-dx", `${Math.round(anchor - x)}px`);
|
||||
return { el, x, y: Number(el.dataset.y), w, h: el.offsetHeight };
|
||||
})
|
||||
.sort((a, b) => b.y - a.y);
|
||||
.sort((a, b) => Number(b.el.dataset.priority || 0) - Number(a.el.dataset.priority || 0) || b.y - a.y);
|
||||
for (const it of items) {
|
||||
let bottom = it.y;
|
||||
// Multi-reading equipment cards use nearby free columns before stacking out of view.
|
||||
@@ -278,8 +278,14 @@ export function unstackLhCards(root: ParentNode): void {
|
||||
if (!hit) break;
|
||||
bottom = hit.y0 - 8;
|
||||
}
|
||||
placed.push({ x0: it.x - it.w / 2, x1: it.x + it.w / 2, y0: bottom - it.h, y1: bottom });
|
||||
const lift = it.y - bottom;
|
||||
// Device details wait for a closer view instead of forming long towers or leaving the stage.
|
||||
const detail = !it.el.classList.contains("live-card-house") && !it.el.classList.contains("live-card-roomclimate") && it.el.dataset.focused !== "1";
|
||||
const multi = it.el.classList.contains("live-card-hydronic") || it.el.classList.contains("live-card-charging");
|
||||
if (detail && height > 0 && (bottom-it.h < 8 || bottom > height-8 || (!multi && Math.abs(lift) > Math.max(100,Math.min(180,height*.25))))) {
|
||||
it.el.style.visibility="hidden"; continue;
|
||||
}
|
||||
placed.push({ x0: it.x - it.w / 2, x1: it.x + it.w / 2, y0: bottom - it.h, y1: bottom });
|
||||
it.el.style.setProperty("--live-dx", `${Math.round(Number(it.el.dataset.x) - it.x)}px`);
|
||||
it.el.style.transform = `translate(${Math.round(it.x)}px, ${Math.round(bottom)}px) translate(-50%, -100%)`;
|
||||
it.el.style.setProperty("--live-thread", `${Math.round(18 + lift)}px`);
|
||||
|
||||
Reference in new issue
Block a user