Round dashboards: one face at a time, a face per widget type, paged by swipe, dots and keys; slider reading; face row in the editor
This commit is contained in:
@@ -4,6 +4,7 @@ import type { DashboardDef_Output, Placement, WidgetDef } from "@/client"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import "./dashboard.css"
|
||||
import { RoundView } from "./RoundFace"
|
||||
import {
|
||||
LockedProvider,
|
||||
PaletteProvider,
|
||||
@@ -13,7 +14,6 @@ import { useUi } from "./ui"
|
||||
import { scrolls, showTitle } from "./ui/core/config"
|
||||
import { LookProvider, useCanvasRoot, useLook } from "./ui/core/look"
|
||||
import { gridStagger, LOOK } from "./ui/core/motion"
|
||||
import { RING_INSET } from "./ui/core/ring"
|
||||
import { WidgetBody, widgetIssue } from "./widgets"
|
||||
|
||||
export type Dashboard = DashboardDef_Output
|
||||
@@ -22,7 +22,7 @@ export type Dashboard = DashboardDef_Output
|
||||
export const DEFAULT_COLUMNS = 12
|
||||
|
||||
/** What a grid size setting may be set to; a panel is matched to one of these. */
|
||||
export const COLUMN_CHOICES = [4, 6, 8, 12, 16, 24]
|
||||
export const COLUMN_CHOICES = [6, 8, 12, 16, 24]
|
||||
|
||||
/** The panel a dashboard is drawn for, if the document does not say. */
|
||||
export const DEFAULT_CANVAS = { width: 1920, height: 1080 }
|
||||
@@ -79,21 +79,10 @@ export const widgetsOf = (dashboard: DashboardDef_Output) =>
|
||||
export const columnsOf = (dashboard: Dashboard) =>
|
||||
dashboard.columns || DEFAULT_COLUMNS
|
||||
|
||||
/** Drawn in the circle its canvas inscribes, for a round screen. */
|
||||
/** Drawn on the circle its canvas inscribes, one widget at a time, for a
|
||||
* round screen. */
|
||||
export const isRound = (dashboard: Dashboard) => dashboard.shape === "round"
|
||||
|
||||
/** Widget types a round dashboard may draw on its ring. Mirrors the server's
|
||||
* `RING_WIDGETS` (`flow/dashboards.py`). */
|
||||
export const RING_WIDGETS = new Set(["slider", "dropdown", "color", "gauge"])
|
||||
|
||||
/** The widget a round dashboard draws along its edge, if it has one. */
|
||||
export const ringOf = (dashboard: Dashboard): WidgetDef | null =>
|
||||
(isRound(dashboard) &&
|
||||
widgetsOf(dashboard).find(
|
||||
(widget) => widget.id === dashboard.ring && RING_WIDGETS.has(widget.type),
|
||||
)) ||
|
||||
null
|
||||
|
||||
/** A round canvas is square whatever its stored height says: a circle has one
|
||||
* size. The server squares it on save; this covers a draft not saved yet. */
|
||||
export const canvasOf = (dashboard: Dashboard) => {
|
||||
@@ -134,22 +123,18 @@ const CELL_ASPECT = DEFAULT_COLUMN_WIDTH / DEFAULT_ROW_HEIGHT
|
||||
* room it takes comes out of the canvas the same way the margin does. Which
|
||||
* cell each widget sits in never changes — only how big a cell is.
|
||||
*
|
||||
* A round dashboard has no rail — a panel shows page dots in the ring's gap
|
||||
* instead — and is inset evenly, by the ring's band as well when it has one.
|
||||
* The cells are the same maths in that square; what a circle cuts off of it
|
||||
* is the editor's to warn about (`offFace`).
|
||||
* A round dashboard has no grid and no rail: it is one face at a time, drawn
|
||||
* on the whole canvas (`RoundView`).
|
||||
*/
|
||||
export const areaOf = (dashboard: Dashboard, rail = false) => {
|
||||
const { width, height } = canvasOf(dashboard)
|
||||
const round = isRound(dashboard)
|
||||
const inset = GRID_INSET + (ringOf(dashboard) ? RING_INSET : 0)
|
||||
const left = round ? inset : GRID_INSET + (rail ? RAIL_INSET : 0)
|
||||
const top = round ? inset : GRID_INSET
|
||||
if (isRound(dashboard)) return { left: 0, top: 0, width, height }
|
||||
const left = GRID_INSET + (rail ? RAIL_INSET : 0)
|
||||
return {
|
||||
left,
|
||||
top,
|
||||
width: Math.max(1, width - left - (round ? inset : GRID_INSET)),
|
||||
height: Math.max(1, height - 2 * top),
|
||||
top: GRID_INSET,
|
||||
width: Math.max(1, width - left - GRID_INSET),
|
||||
height: Math.max(1, height - 2 * GRID_INSET),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,18 +175,14 @@ export function CanvasSurface({
|
||||
dashboard,
|
||||
dots,
|
||||
rail,
|
||||
face,
|
||||
touch,
|
||||
children,
|
||||
}: {
|
||||
dashboard: Dashboard
|
||||
/** Draw the placement grid. A wall panel is not being arranged, so: no. */
|
||||
dots?: boolean
|
||||
/** The dashboard-switching rail, drawn on the panel rather than beside it.
|
||||
* On a round dashboard, the page dots in the ring's gap. */
|
||||
/** The dashboard-switching rail, drawn on the panel rather than beside it. */
|
||||
rail?: React.ReactNode
|
||||
/** What a round dashboard draws over the whole face: its ring. */
|
||||
face?: React.ReactNode
|
||||
/** Whether the panel this canvas hangs on is touched rather than pointed at. */
|
||||
touch?: boolean
|
||||
children: (scale: number) => React.ReactNode
|
||||
@@ -248,7 +229,6 @@ export function CanvasSurface({
|
||||
data-touch={root["data-touch"]}
|
||||
data-palette={root["data-palette"]}
|
||||
data-shape={round ? "round" : undefined}
|
||||
data-arranging={dots ? "" : undefined}
|
||||
className={cn(
|
||||
"absolute overflow-hidden bg-background text-foreground",
|
||||
root.className,
|
||||
@@ -259,10 +239,8 @@ export function CanvasSurface({
|
||||
...root.style,
|
||||
width,
|
||||
height,
|
||||
// Round, nothing outside the circle is drawn — except while it
|
||||
// is being arranged, where a tile reaching past the edge has to
|
||||
// stay in reach to be moved back (`FaceGuide` shades it).
|
||||
clipPath: round && !dots ? "circle(50%)" : undefined,
|
||||
// Round, nothing outside the circle is drawn.
|
||||
clipPath: round ? "circle(50%)" : undefined,
|
||||
left: (box.width - width * scale) / 2,
|
||||
top: (box.height - height * scale) / 2,
|
||||
transform: `scale(${scale})`,
|
||||
@@ -289,15 +267,11 @@ export function CanvasSurface({
|
||||
>
|
||||
{/* Over the ground rather than under it: the dots are what is
|
||||
being arranged against, and a wallpaper must not hide them. */}
|
||||
{dots ? (
|
||||
{dots && !round ? (
|
||||
<div className="dot-canvas pointer-events-none absolute inset-0" />
|
||||
) : null}
|
||||
{content}
|
||||
</div>
|
||||
{round ? face : null}
|
||||
{round && dots ? (
|
||||
<FaceGuide size={width} ring={Boolean(ringOf(dashboard))} />
|
||||
) : null}
|
||||
</div>
|
||||
</LookProvider>
|
||||
) : null}
|
||||
@@ -305,83 +279,6 @@ export function CanvasSurface({
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The circle, while a round dashboard is being arranged.
|
||||
*
|
||||
* The canvas is not clipped then, so what lies outside the face is shaded
|
||||
* rather than hidden, and the edge the grid's tiles must keep inside — the
|
||||
* ring's inner edge, when there is one — is drawn dashed.
|
||||
*/
|
||||
function FaceGuide({ size, ring }: { size: number; ring: boolean }) {
|
||||
const half = size / 2
|
||||
const safe = half - (ring ? RING_INSET : 0)
|
||||
return (
|
||||
<svg
|
||||
role="presentation"
|
||||
width={size}
|
||||
height={size}
|
||||
className="pointer-events-none absolute inset-0 z-[5]"
|
||||
data-testid="face-guide"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
className="fill-background opacity-70"
|
||||
d={`M0 0H${size}V${size}H0Z M${half} 0a${half} ${half} 0 1 0 0.01 0Z`}
|
||||
/>
|
||||
<circle
|
||||
cx={half}
|
||||
cy={half}
|
||||
r={half - 0.5}
|
||||
fill="none"
|
||||
className="stroke-border"
|
||||
/>
|
||||
{ring ? (
|
||||
<circle
|
||||
cx={half}
|
||||
cy={half}
|
||||
r={safe}
|
||||
fill="none"
|
||||
strokeDasharray="4 4"
|
||||
className="stroke-muted-foreground opacity-50"
|
||||
/>
|
||||
) : null}
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Which tiles reach past a round dashboard's face: any corner outside the
|
||||
* circle the grid has to keep inside, which is the ring's inner edge when
|
||||
* there is a ring. A device clips them, so the editor says so.
|
||||
*/
|
||||
export function offFace(
|
||||
dashboard: Dashboard,
|
||||
items: readonly { i: string; x: number; y: number; w: number; h: number }[],
|
||||
): string[] {
|
||||
if (!isRound(dashboard)) return []
|
||||
const { width } = canvasOf(dashboard)
|
||||
const area = areaOf(dashboard)
|
||||
const column = (area.width + GRID_GAP) / columnsOf(dashboard)
|
||||
const row = rowPitchOf(dashboard)
|
||||
const reach = width / 2 - (ringOf(dashboard) ? RING_INSET : 0)
|
||||
const out = (x: number, y: number) =>
|
||||
Math.hypot(x - width / 2, y - width / 2) > reach
|
||||
return items
|
||||
.filter((item) => {
|
||||
const left = area.left + item.x * column
|
||||
const top = area.top + item.y * row
|
||||
const right = left + item.w * column - GRID_GAP
|
||||
const bottom = top + item.h * row - GRID_GAP
|
||||
return (
|
||||
out(left, top) ||
|
||||
out(right, top) ||
|
||||
out(left, bottom) ||
|
||||
out(right, bottom)
|
||||
)
|
||||
})
|
||||
.map((item) => item.i)
|
||||
}
|
||||
|
||||
/**
|
||||
* What the widgets sit on.
|
||||
*
|
||||
@@ -449,6 +346,7 @@ export function DashboardView({
|
||||
stacked,
|
||||
rail,
|
||||
editing,
|
||||
onDashboard,
|
||||
}: {
|
||||
dashboard: Dashboard
|
||||
renderWidget?: (widget: WidgetDef) => React.ReactNode
|
||||
@@ -459,12 +357,19 @@ export function DashboardView({
|
||||
/** Being arranged rather than used, so the lock is not mounted — the
|
||||
* stacked editor draws through here, where the canvas draws its own grid. */
|
||||
editing?: boolean
|
||||
/** A round one's way to the panel's other dashboards: next (1) or
|
||||
* previous (-1). */
|
||||
onDashboard?: (by: number) => void
|
||||
}) {
|
||||
const all = widgetsOf(dashboard)
|
||||
const round = isRound(dashboard)
|
||||
if (all.length === 0) {
|
||||
return (
|
||||
<p
|
||||
className="text-sm text-muted-foreground"
|
||||
className={cn(
|
||||
"text-sm text-muted-foreground",
|
||||
round && "grid h-full place-items-center px-16 text-center",
|
||||
)}
|
||||
data-testid="dashboard-empty"
|
||||
>
|
||||
Nothing on this dashboard yet. Edit it to add a widget.
|
||||
@@ -472,30 +377,38 @@ export function DashboardView({
|
||||
)
|
||||
}
|
||||
|
||||
// The ring is drawn along the face's edge (`RoundFace`), not in a cell.
|
||||
const ring = ringOf(dashboard)
|
||||
const placed = ring ? all.filter((widget) => widget !== ring) : all
|
||||
// Stacked, the arrangement becomes a reading order, so it follows the rows
|
||||
// the panel shows rather than the order widgets happened to be added in.
|
||||
const widgets = stacked
|
||||
? [...placed].sort((a, b) => {
|
||||
? [...all].sort((a, b) => {
|
||||
const left = placement(a)
|
||||
const right = placement(b)
|
||||
return (left.y ?? 0) - (right.y ?? 0) || (left.x ?? 0) - (right.x ?? 0)
|
||||
})
|
||||
: placed
|
||||
: all
|
||||
const columns = columnsOf(dashboard)
|
||||
|
||||
const grid = (
|
||||
<PaletteProvider dashboard={dashboard}>
|
||||
<WidgetGrid
|
||||
dashboard={dashboard}
|
||||
widgets={widgets}
|
||||
columns={columns}
|
||||
stacked={stacked}
|
||||
rail={rail}
|
||||
renderWidget={renderWidget}
|
||||
/>
|
||||
{round ? (
|
||||
// Its own face memory per dashboard, so a panel switching to another
|
||||
// starts that one where it was left.
|
||||
<RoundView
|
||||
key={dashboard.name}
|
||||
dashboard={dashboard}
|
||||
size={canvasOf(dashboard).width}
|
||||
onDashboard={onDashboard}
|
||||
/>
|
||||
) : (
|
||||
<WidgetGrid
|
||||
dashboard={dashboard}
|
||||
widgets={widgets}
|
||||
columns={columns}
|
||||
stacked={stacked}
|
||||
rail={rail}
|
||||
renderWidget={renderWidget}
|
||||
/>
|
||||
)}
|
||||
</PaletteProvider>
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user