diff --git a/README.md b/README.md index bed2f11..053df7c 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,11 @@ automation systems like openHAB). Runs as a systemd service. ![PiMonitor dashboard](docs/dashboard.png) +The Console theme, light and dark: + +![PiMonitor dashboard, Console theme, light](docs/dashboard-console-light.png) +![PiMonitor dashboard, Console theme, dark](docs/dashboard-console-dark.png) + ## Features - **CPU usage** - overall and per-core percentage, with a trend sparkline @@ -54,8 +59,8 @@ automation systems like openHAB). Runs as a systemd service. metric sampling - **Light/dark mode toggle** - follows the OS setting by default, with a manual override remembered in the browser -- **Choosable themes** - picked in the Customize dialog once more than one - theme is available (only Clear today), remembered in the browser +- **Choosable themes** - Clear and Console (a monospace, outlined look), picked + in the Customize dialog, remembered in the browser - **Configurable dashboard layout** - show/hide and reorder metric cards from a settings modal, remembered in the browser - A versioned REST API (`/api/v1/...`) for third-party consumers, with diff --git a/docs/areas/dashboard.md b/docs/areas/dashboard.md index b4528e2..59d74b7 100644 --- a/docs/areas/dashboard.md +++ b/docs/areas/dashboard.md @@ -158,7 +158,7 @@ client of the [HTTP API](http-api.md) like any third-party integration and has n - CPU usage, temperature, memory, swap and each filesystem are colored `crit` when the value is greater than or equal to the matching `*_crit` threshold from `/api/v1/config`, otherwise `warn` when greater than or equal to the - `*_warn` threshold, otherwise `ok` — the same comparison the alert engine uses. + `*_warn` threshold, otherwise `ok` — the same comparison the alert engine uses. The levels are the same in every theme; only the color of `ok` differs: green in Clear, `--text` in Console, where color marks only `warn` and `crit` (*Look*, [0043](../decisions/0043-console-colour-only-for-warn-and-crit.md)). - The load-average gauges (1, 5 and 15 min) run from 0 to the CPU core count and are colored against the CPU thresholds scaled to the core count (`cores × cpu_warn_percent / 100`, `cores × cpu_crit_percent / 100`). - Coloring follows the raw value of every poll and is independent of alert state: a value can be colored `crit` @@ -207,7 +207,7 @@ client of the [HTTP API](http-api.md) like any third-party integration and has n ### Theme - A theme is a named set of palette tokens, chosen independently of the mode. The registered themes are listed once, - in `PIMONITOR_THEMES` of `theme-init.js`; today only Clear exists, and it is the default (the first entry). + in `PIMONITOR_THEMES` of `theme-init.js`: Clear, the default (the first entry), and Console (`console`), second. - The default is the absence of the `data-style` attribute on ``; any other theme sets `data-style` to its id. - The choice is stored in `localStorage` under `pimonitor-style` (the theme's id). A stored value that is no registered id (unknown, differently cased, unreadable) means Clear; it is kept in storage unchanged, never @@ -220,28 +220,50 @@ client of the [HTTP API](http-api.md) like any third-party integration and has n - All themes are in the one `style.css`, one DOM. A theme other than Clear has three palette blocks (`:root[data-style=""]`, with `[data-theme="dark"]`, and inside `prefers-color-scheme: dark` with `:not([data-theme])`), each with the same token set as Clear's. -- Reasons: [0041](../decisions/0041-theme-choice-per-browser-as-pimonitor-style.md), +- Console follows the Clear blocks with its own palette (monospace look, outlined buttons, no shadows). Its + `--accent` and every `--accent-` are the mode's `--text` hex, `--on-accent` the `--card-bg` hex, and + `--shadow` and `--dialog-shadow` are `0 0 0 transparent` instead of `none`, which would make the + `box-shadow` list of `.theme-toggle:hover` invalid. Its look beyond tokens is rules in `style.css` whose every + selector starts with `:root[data-style="console"]`; only the unit gap `.metric-unit::before` is an unscoped rule. +- Reasons: [0038](../decisions/0038-dashboard-mode-and-theme-as-token-overrides.md), + [0041](../decisions/0041-theme-choice-per-browser-as-pimonitor-style.md), [0042](../decisions/0042-theme-registry-in-theme-init-js.md). ### Look -- The canvas renderers read exactly these custom properties through `themeColor`/`themeNumber` of `theme-init.js`: - `--accent`, `--muted`, `--card-bg`, `--track`, `--ok`, `--warn`, `--crit` and `--spark-fill-alpha`. An empty or - unparseable value, or a `--spark-fill-alpha` outside 0 to 1, is replaced by Clear's light value; any valid value, - `0` included, is used as is. +- The canvas renderers read exactly these custom properties through `themeColor`, `themeNumber` and `themeKeyword` + of `theme-init.js`: the colors `--accent`, `--muted`, `--card-bg`, `--track`, `--border`, `--ok-fill`, `--warn` and + `--crit`; the numbers `--spark-fill-alpha` (0 to 1), `--spark-line-width` (0.5 to 6) and `--gauge-width` (1 to + 12); the keywords `--spark-shape` (`line`, `step`), `--spark-fill` (`fade`, `flat`), `--spark-dot` (`ring`, + `none`), `--spark-grid` (`none`, `lines`) and `--gauge-cap` (`round`, `butt`). An empty or unparseable value, a + number outside its range or a word outside its list is replaced by Clear's light value; any valid value, `0` + included, is used as is. - Every color comes from a token of the palette blocks of `style.css` (light, explicit dark and OS dark, the two dark blocks identical); every palette block of every theme, Clear's three included, declares the same token keys, and no rule outside them holds a color literal. Both the text and the fills of the page use these tokens, so a mode switch changes tokens only. - Status text (values, percentages, badges, the alert banner, the API-key error) uses the `*-ink` variant of - `ok`/`warn`/`crit`; bar fills and the card border of an alerting card use the base color. Text on an accent fill + `ok`/`warn`/`crit`; bar fills and the card border of an alerting card use the base color (for `ok`: `--ok` on Clear's bars, `--ok-fill` on the gauge arc and on Console's bars). Text on an accent fill uses `--on-accent`. +- Console colors `ok` text with `--text` (`.metric-ok` and the new-version column of the update list); color + appears only for `warn` and `crit`, plus the Pi-hole status dot (green when up to date, amber with an update). + The ok fill of the gauge arc and of Console's bars is `--ok-fill`: Clear's green (equal to its `--ok`, which Clear's bars use), Console's `--text` + ([0043](../decisions/0043-console-colour-only-for-warn-and-crit.md)). - A bar label shows the percentage emphasised, colored only at `warn` and `crit`, and the byte sizes muted beside it. -- The load gauge draws a 9 px arc on the `--track` color and shows its value inside the arc; the value's font size - scales with the gauge width. -- A sparkline is a 2 px line with a fill that fades towards the bottom and a dot with a ring at the newest point, - inset so the dot is never clipped. -- A card's own color appears only on its 28 px icon chip and on the hover/focus highlight of a clickable card; cards +- The load gauge draws an arc of `--gauge-width` (Clear 9 px with round caps, Console 5 px with butt caps) on the + `--track` color and shows its value inside the arc; the value's font size scales with the gauge width in both + themes. +- A sparkline (and the detail chart) is a line of `--spark-line-width` with a fill and, as the theme says, a dot + with a ring at the newest point, inset so the dot is never clipped. Clear: a 2 px line, a fill fading towards the + bottom and the dot. Console: a 1.5 px stepped line (each point held to the next x), a flat low-opacity fill, a + solid baseline with two dashed lines at 25 % and 62.5 % of the height, and no dot. +- A metric value of CPU usage and temperature is a number element (`.metric-number`) and, when there is a unit, a + unit element (`.metric-unit`), built by `createElement` and `textContent`. Clear separates them by a space + (`.metric-unit::before`); Console sets the unit 15 px, muted, 4 px apart without the space. +- Console's bars are 12 px high and square, drawn as a repeating gradient of 6 px segments with 2 px gaps on the + track and on the fill, both from the track's left edge; its card heading is a 44 px strip with a bottom border + across the card, and in a clickable card the whole strip is the button. +- A card's own color appears only on its icon chip (28 px in Clear; Console draws a 14 px icon in `--muted` without a chip and sets every card color to `--text`) and on the hover/focus highlight of a clickable card; cards have no colored top border. Clickable cards end their heading with a chevron. - Icons are inline SVG drawn in `currentColor`, hidden from assistive technology; the page uses no emoji. The mode button shows the moon in light mode and the sun in dark mode, chosen by CSS. The network card names its rates @@ -292,6 +314,7 @@ client of the [HTTP API](http-api.md) like any third-party integration and has n - [0040](../decisions/0040-dashboard-icons-inline-svg.md): inline SVG icons - [0041](../decisions/0041-theme-choice-per-browser-as-pimonitor-style.md): theme choice per browser - [0042](../decisions/0042-theme-registry-in-theme-init-js.md): theme registry in `theme-init.js` +- [0043](../decisions/0043-console-colour-only-for-warn-and-crit.md): in Console, colour only for `warn` and `crit` ## Not here diff --git a/docs/dashboard-console-dark.png b/docs/dashboard-console-dark.png new file mode 100644 index 0000000..4634f1c Binary files /dev/null and b/docs/dashboard-console-dark.png differ diff --git a/docs/dashboard-console-light.png b/docs/dashboard-console-light.png new file mode 100644 index 0000000..bc8d74f Binary files /dev/null and b/docs/dashboard-console-light.png differ diff --git a/docs/decisions/0038-dashboard-mode-and-theme-as-token-overrides.md b/docs/decisions/0038-dashboard-mode-and-theme-as-token-overrides.md index 94b49fe..1e84898 100644 --- a/docs/decisions/0038-dashboard-mode-and-theme-as-token-overrides.md +++ b/docs/decisions/0038-dashboard-mode-and-theme-as-token-overrides.md @@ -3,7 +3,7 @@ - **Status:** Accepted - **Date:** 2026-11-28 - **Area:** Dashboard -- **Source:** Issue #217, issue #218 +- **Source:** Issue #217, issue #218, issue #219 - **Supersedes:** — ## Context @@ -28,19 +28,25 @@ names, and each design has to be swappable without touching the markup or the sc — a theme is reviewed in one file. Cost: one more request revalidated on every load per theme, a link and asset tests per file, and the token key set test reads several files; the blocks are short, so `style.css` keeps them after Clear's palette. +5. **Everything a theme changes as a token, so a theme is palette blocks only** (considered for Console, issue #219) + — no theme ever adds a rule. Cost: Console changes fonts, border styles, bar patterns, heading layout and hidden + elements; about thirty more tokens would enter all three blocks of every theme, Clear's included, where most + name a property Clear does not use. ## Decision Option 1. Light/dark is the *mode*; its storage key `pimonitor-theme` and attribute `data-theme` keep their names, since renaming them would reset every stored choice. The look is described in [Dashboard](../areas/dashboard.md#look); the per-card colours are tokens of the palette blocks like every other -colour. +colour. What a theme changes beyond a token value (a font family, a dashed border, a segmented bar, a hidden element) +is a rule scoped to `:root[data-style=""]` that uses only tokens (issue #219). ## Consequences -- A later theme only adds override blocks — light, explicit dark and OS dark under its `data-style` value - ([0041](0041-theme-choice-per-browser-as-pimonitor-style.md)) — to `style.css`, after Clear's palette; `style.css` - holds no colour literal outside the palette blocks. The page has one DOM for every theme: no per-theme markup and +- A later theme adds override blocks — light, explicit dark and OS dark under its `data-style` value + ([0041](0041-theme-choice-per-browser-as-pimonitor-style.md)) — to `style.css`, after Clear's palette, and its + scoped rules after those; `style.css` holds no colour literal outside the palette blocks, and no unscoped rule + changes for a theme other than Clear, so a mode switch still changes tokens only. The page has one DOM for every theme: no per-theme markup and no per-theme HTML rendering in `app.js`. - Canvas widgets take a theme's differences only from custom properties; every one they read is a palette token in every block of every theme, so all blocks share one token key set (issue #218, diff --git a/docs/decisions/0042-theme-registry-in-theme-init-js.md b/docs/decisions/0042-theme-registry-in-theme-init-js.md index 9dfe11a..e88f756 100644 --- a/docs/decisions/0042-theme-registry-in-theme-init-js.md +++ b/docs/decisions/0042-theme-registry-in-theme-init-js.md @@ -3,7 +3,7 @@ - **Status:** Accepted - **Date:** 2026-11-28 - **Area:** Dashboard -- **Source:** Issue #218 +- **Source:** Issue #218, issue #219 - **Supersedes:** — ## Context @@ -27,12 +27,17 @@ literal today, one of which turns a valid `0` into `0.28` and one of which has n without blocking, so `theme-init.js` would still need its own copy. 4. **Readers and fallbacks in `chart.js`** — loaded before `gauge.js` and `app.js`. Cost: `chart.js` is a drawing library; the fallbacks belong next to the theme list they describe. +5. **Non-colour drawing switches (line shape, end dot, fill, grid, caps) as numbers read by `themeNumber`** + (issue #219) — no third reader. Cost: a `0`/`1` that stands for a shape means nothing between its values, and a + theme that writes `step` silently gets Clear's drawing; a keyword reader names the shapes and checks them + against a fixed list. ## Decision Option 1. `theme-init.js` declares the registry `PIMONITOR_THEMES` (id and display name, default first), the map `DRAWING_FALLBACKS` of every custom property a canvas renderer reads with Clear's light value, and the readers -`themeColor` and `themeNumber`; `app.js`, `chart.js` and `gauge.js` use only these. The rules are in +`themeColor`, `themeNumber` and `themeKeyword` (a value from a fixed list, issue #219); `app.js`, `chart.js` and +`gauge.js` use only these. The rules are in [Dashboard](../areas/dashboard.md#theme). ## Consequences diff --git a/docs/decisions/0043-console-colour-only-for-warn-and-crit.md b/docs/decisions/0043-console-colour-only-for-warn-and-crit.md new file mode 100644 index 0000000..406ead3 --- /dev/null +++ b/docs/decisions/0043-console-colour-only-for-warn-and-crit.md @@ -0,0 +1,41 @@ +# 0043: In Console, colour is reserved for `warn` and `crit` + +- **Status:** Accepted +- **Date:** 2026-11-29 +- **Area:** Dashboard +- **Source:** Issue #219 +- **Supersedes:** — + +## Context + +The theme Console (issue #219) is a dense, instrument-panel look. In Clear a value in the normal range is green +(`--ok-ink` for text, `--ok` for fills), and every card has its own colour on its icon chip +([0039](0039-card-colour-only-on-icon-chip.md)), so a page in good health is full of colour, and a `warn` stands out +less than it could. The levels and thresholds are shared by every theme and must not change. + +## Options considered + +1. **`ok` in the text colour, colour only for `warn` and `crit`** — text and fills of the `ok` level take `--text`, + `warn` and `crit` keep their ink and base colours; green remains only as the small Pi-hole status dot. Anything + coloured needs attention. Cost: a healthy value no longer says "healthy" by colour, and a fill token of its own + (`--ok-fill`) is needed, because `--ok` stays green for the dot. +2. **`ok` in a muted green, as a quieter Clear** — keeps the colour cue. Cost: three hues still compete on every card, + which is the opposite of the look the issue asks for. +3. **`ok` in `--muted`** — even calmer. Cost: values and bars, the content of the page, would be drawn in the + secondary text colour and lose contrast against the labels around them. + +## Decision + +Option 1. In Console the `ok` level is drawn in `--text` for text and in `--ok-fill` (equal to `--text`) for bars and +gauge arcs; `warn` and `crit` are unchanged. The rules are in [Dashboard](../areas/dashboard.md#value-coloring). + +## Consequences + +- The level of every value is still computed as in Clear; only the colour of `ok` differs per theme, so the alert + engine and the dashboard keep agreeing. +- Console sets every per-card accent token to its `--text` value and its chip tint to `0%`: the icon-chip rule of + 0039 still holds, but shows no colour. +- Colour that marks an event keeps its level colour: the restart markers of the openHAB memory chart stay `--warn`. + Text that had no level colour before (the gauge value) gets none; the new-version column of the updates list, + green in Clear, is `--text`. +- A later theme picks its own `ok` colour through `--ok-fill` (the gauge arc), a scoped `.bar-fill` rule and its scoped text rules; Clear's `--ok-fill` equals its `--ok`, which Clear's bars keep. diff --git a/docs/decisions/README.md b/docs/decisions/README.md index da72155..7fa982e 100644 --- a/docs/decisions/README.md +++ b/docs/decisions/README.md @@ -104,4 +104,5 @@ once in the [area document](../areas/README.md) of the record's area, and the re | [0040](0040-dashboard-icons-inline-svg.md) | Dashboard icons are inline stroke SVG in the page, not emoji | Accepted | 2026-11-28 | | [0041](0041-theme-choice-per-browser-as-pimonitor-style.md) | The theme choice is a per-browser preference, stored as `pimonitor-style` and applied as `data-style` | Accepted | 2026-11-28 | | [0042](0042-theme-registry-in-theme-init-js.md) | The theme registry and the canvas token readers live once in `theme-init.js` | Accepted | 2026-11-28 | +| [0043](0043-console-colour-only-for-warn-and-crit.md) | In Console, colour is reserved for `warn` and `crit` | Accepted | 2026-11-29 | diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 8b1fcc8..4691cad 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -201,6 +201,24 @@ if (el) el.textContent = text; } + // setMetricValue shows a metric value as a number element and, only when + // `unit` is non-empty, a unit element, so a theme can style the unit apart. + function setMetricValue(id, number, unit) { + const el = document.getElementById(id); + if (!el) return; + const num = document.createElement('span'); + num.className = 'metric-number'; + num.textContent = number; + const nodes = [num]; + if (unit) { + const u = document.createElement('span'); + u.className = 'metric-unit'; + u.textContent = unit; + nodes.push(u); + } + el.replaceChildren(...nodes); + } + function fmtBytes(bytes) { if (bytes === undefined || bytes === null) return '–'; const units = ['B', 'KB', 'MB', 'GB', 'TB']; @@ -298,7 +316,7 @@ setText('uptime-value', fmtUptime(snap.uptime_seconds)); // CPU - setText('cpu-overall', snap.cpu.overall_percent.toFixed(1) + ' %'); + setMetricValue('cpu-overall', snap.cpu.overall_percent.toFixed(1), '%'); document.getElementById('cpu-overall').className = 'metric-value ' + levelClass(snap.cpu.overall_percent, t.cpu_warn_percent, t.cpu_crit_percent); if (snap.cpu.per_core_percent?.length) { @@ -316,17 +334,17 @@ // Temperature const tempEl = document.getElementById('temp-value'); if (snap.temperature?.zone) { - setText('temp-value', snap.temperature.celsius.toFixed(1) + ' °C'); + setMetricValue('temp-value', snap.temperature.celsius.toFixed(1), '°C'); tempEl.className = 'metric-value ' + levelClass(snap.temperature.celsius, t.temperature_warn_c, t.temperature_crit_c); } else { - setText('temp-value', 'n/a'); + setMetricValue('temp-value', 'n/a', ''); tempEl.className = 'metric-value'; } // Secondary temperature readings, each present only when vcgencmd // answered for it: the GPU/SoC die sensor, and the Pi 4/5 PMIC. const tempExtras = []; - if (snap.gpu_temperature) tempExtras.push('GPU: ' + snap.gpu_temperature.celsius.toFixed(1) + ' °C'); - if (snap.pmic_temperature) tempExtras.push('PMIC: ' + snap.pmic_temperature.celsius.toFixed(1) + ' °C'); + if (snap.gpu_temperature) tempExtras.push(`GPU: ${snap.gpu_temperature.celsius.toFixed(1)} °C`); + if (snap.pmic_temperature) tempExtras.push(`PMIC: ${snap.pmic_temperature.celsius.toFixed(1)} °C`); setText('temp-gpu', tempExtras.join(' · ')); // Memory & swap (show absolute sizes alongside the percentage, like @@ -982,7 +1000,7 @@ title: 'CPU Usage', historyKey: 'cpu_percent', opts: { min: 0, max: 100 }, - fmt: v => v.toFixed(1) + ' %', + fmt: v => `${v.toFixed(1)} %`, }, load: { title: 'Load Average (1 min)', @@ -994,13 +1012,13 @@ title: 'Temperature', historyKey: 'temperature', opts: {}, - fmt: v => v.toFixed(1) + ' °C', + fmt: v => `${v.toFixed(1)} °C`, }, memory: { title: 'Memory Usage', historyKey: 'memory_used_percent', opts: { min: 0, max: 100 }, - fmt: v => v.toFixed(1) + ' %', + fmt: v => `${v.toFixed(1)} %`, }, }; diff --git a/internal/web/assets/chart.js b/internal/web/assets/chart.js index 7fb1bfe..75fef14 100644 --- a/internal/web/assets/chart.js +++ b/internal/web/assets/chart.js @@ -1,6 +1,105 @@ // Minimal canvas sparkline renderer. Deliberately hand-rolled instead of // vendoring a charting library: the dashboard only needs a handful of // small trend lines, not zoom/pan/tooltip interactivity. + +// sparkStyle reads the sparkline drawing properties of the active theme and +// returns { shape, lineWidth, fill, fillAlpha, dot, grid, cardBg, border }. +function sparkStyle() { + return { + shape: themeKeyword('--spark-shape', ['line', 'step']), + lineWidth: themeNumber('--spark-line-width', 0.5, 6), + fill: themeKeyword('--spark-fill', ['fade', 'flat']), + fillAlpha: themeNumber('--spark-fill-alpha', 0, 1), + dot: themeKeyword('--spark-dot', ['ring', 'none']), + grid: themeKeyword('--spark-grid', ['none', 'lines']), + cardBg: themeColor('--card-bg'), + border: themeColor('--border'), + }; +} + +// drawSparkGrid draws the baseline and the two dashed grid lines in `color`. +function drawSparkGrid(ctx, width, height, color) { + ctx.save(); + ctx.strokeStyle = color; + ctx.lineWidth = 1; + ctx.lineCap = 'butt'; + ctx.setLineDash([]); + ctx.beginPath(); + ctx.moveTo(0, height - 0.5); + ctx.lineTo(width, height - 0.5); + ctx.stroke(); + ctx.setLineDash([2, 4]); + [0.25, 0.625].forEach(f => { + const y = Math.round(height * f) + 0.5; + ctx.beginPath(); + ctx.moveTo(0, y); + ctx.lineTo(width, y); + ctx.stroke(); + }); + ctx.setLineDash([]); + ctx.restore(); +} + +// traceSparkline adds the path through the points xy ({ x, y }) to ctx, held +// horizontally to the next x when `stepped`. +function traceSparkline(ctx, xy, stepped) { + ctx.beginPath(); + xy.forEach((p, i) => { + if (i === 0) { + ctx.moveTo(p.x, p.y); + return; + } + if (stepped) { + ctx.lineTo(p.x, xy[i - 1].y); + } + ctx.lineTo(p.x, p.y); + }); +} + +// fillSparkline fills the area under the line through xy in `color` as +// `style` (see sparkStyle) says: flat at the fill alpha, or faded to +// transparent towards the bottom. The gradient erases the fill +// (destination-out) from none at the top to all of it at the bottom, so any +// CSS colour works. +function fillSparkline(ctx, xy, height, color, style) { + const first = xy[0]; + const last = xy[xy.length - 1]; + traceSparkline(ctx, xy, style.shape === 'step'); + ctx.lineTo(last.x, height); + ctx.lineTo(first.x, height); + ctx.closePath(); + ctx.save(); + ctx.clip(); + ctx.globalAlpha = style.fillAlpha; + ctx.fillStyle = color; + ctx.fill(); + // A 'flat' fill (any word but 'fade') keeps the fill alpha alone. + if (style.fill === 'fade') { + ctx.globalAlpha = 1; + const fade = ctx.createLinearGradient(0, 0, 0, height); + fade.addColorStop(0, 'rgba(0, 0, 0, 0)'); + fade.addColorStop(1, 'rgba(0, 0, 0, 1)'); + ctx.globalCompositeOperation = 'destination-out'; + ctx.fillStyle = fade; + ctx.fillRect(first.x, 0, last.x - first.x, height); + ctx.globalCompositeOperation = 'source-over'; + } + ctx.restore(); +} + +// drawSparkDot draws the newest-point dot at `last` ({ x, y }) with a ring in +// `cardBg`. +function drawSparkDot(ctx, last, color, cardBg) { + ctx.beginPath(); + ctx.arc(last.x, last.y, 7, 0, 2 * Math.PI); + ctx.fillStyle = cardBg; + ctx.fill(); + ctx.beginPath(); + ctx.arc(last.x, last.y, 4, 0, 2 * Math.PI); + ctx.fillStyle = color; + ctx.fill(); +} + function drawSparkline(canvas, points, opts) { opts = opts || {}; const color = opts.color || themeColor('--accent'); @@ -39,62 +138,24 @@ function drawSparkline(canvas, points, opts) { return cssHeight - pad - t * (cssHeight - pad * 2); }; - const fillAlpha = themeNumber('--spark-fill-alpha', 0, 1); - const cardBg = themeColor('--card-bg'); - const lastX = pad + (points.length - 1) * stepX; - const lastY = yFor(points[points.length - 1].v); - - const linePath = () => { - ctx.beginPath(); - points.forEach((p, i) => { - const x = pad + i * stepX; - const y = yFor(p.v); - if (i === 0) { - ctx.moveTo(x, y); - } else { - ctx.lineTo(x, y); - } - }); - }; + const style = sparkStyle(); + const xy = points.map((p, i) => ({ x: pad + i * stepX, y: yFor(p.v) })); - // Fill under the line in the line colour, faded to transparent towards - // the bottom: the gradient erases the fill (destination-out) from none at - // the top to all of it at the bottom, so any CSS colour works. - linePath(); - ctx.lineTo(lastX, cssHeight); - ctx.lineTo(pad, cssHeight); - ctx.closePath(); - ctx.save(); - ctx.clip(); - ctx.globalAlpha = fillAlpha; - ctx.fillStyle = color; - ctx.fill(); - ctx.globalAlpha = 1; - const fade = ctx.createLinearGradient(0, 0, 0, cssHeight); - fade.addColorStop(0, 'rgba(0, 0, 0, 0)'); - fade.addColorStop(1, 'rgba(0, 0, 0, 1)'); - ctx.globalCompositeOperation = 'destination-out'; - ctx.fillStyle = fade; - ctx.fillRect(0, 0, cssWidth, cssHeight); - ctx.globalCompositeOperation = 'source-over'; - ctx.restore(); + if (style.grid !== 'none') { + drawSparkGrid(ctx, cssWidth, cssHeight, style.border); + } + fillSparkline(ctx, xy, cssHeight, color, style); - linePath(); + traceSparkline(ctx, xy, style.shape === 'step'); ctx.strokeStyle = color; - ctx.lineWidth = 2; + ctx.lineWidth = style.lineWidth; ctx.lineJoin = 'round'; ctx.lineCap = 'round'; ctx.stroke(); - // Newest point: a dot with a ring in the card colour. - ctx.beginPath(); - ctx.arc(lastX, lastY, 7, 0, 2 * Math.PI); - ctx.fillStyle = cardBg; - ctx.fill(); - ctx.beginPath(); - ctx.arc(lastX, lastY, 4, 0, 2 * Math.PI); - ctx.fillStyle = color; - ctx.fill(); + if (style.dot !== 'none') { + drawSparkDot(ctx, xy[xy.length - 1], color, style.cardBg); + } } // Time-scaled line chart for series whose points are not evenly spaced diff --git a/internal/web/assets/gauge.js b/internal/web/assets/gauge.js index 3eb93a2..cc7f6fb 100644 --- a/internal/web/assets/gauge.js +++ b/internal/web/assets/gauge.js @@ -10,7 +10,7 @@ function gaugeColorVar(colorClass) { if (colorClass === 'metric-crit') return '--crit'; if (colorClass === 'metric-warn') return '--warn'; - return '--ok'; + return '--ok-fill'; } function drawGauge(canvas, value, max, colorClass) { @@ -25,7 +25,8 @@ function drawGauge(canvas, value, max, colorClass) { ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, cssWidth, cssHeight); - const lineWidth = 9; + const lineWidth = themeNumber('--gauge-width', 1, 12); + const lineCap = themeKeyword('--gauge-cap', ['round', 'butt']); const margin = 4; const cx = cssWidth / 2; const cy = cssHeight - margin - lineWidth / 2; @@ -48,7 +49,7 @@ function drawGauge(canvas, value, max, colorClass) { ctx.arc(cx, cy, radius, startAngle, endAngle, false); ctx.lineWidth = lineWidth; ctx.strokeStyle = trackColor; - ctx.lineCap = 'round'; + ctx.lineCap = lineCap; ctx.stroke(); // Value arc (proportional sweep from the left). @@ -58,6 +59,6 @@ function drawGauge(canvas, value, max, colorClass) { ctx.arc(cx, cy, radius, startAngle, startAngle + Math.PI * clamped, false); ctx.lineWidth = lineWidth; ctx.strokeStyle = fillColor; - ctx.lineCap = 'round'; + ctx.lineCap = lineCap; ctx.stroke(); } diff --git a/internal/web/assets/style.css b/internal/web/assets/style.css index c7855fc..fa01fea 100644 --- a/internal/web/assets/style.css +++ b/internal/web/assets/style.css @@ -37,6 +37,14 @@ --radius-chip: 8px; --card-padding: 8px 18px 18px; --gap: 16px; + --ok-fill: #16a34a; + --spark-line-width: 2; + --spark-shape: line; + --spark-fill: fade; + --spark-dot: ring; + --spark-grid: none; + --gauge-width: 9; + --gauge-cap: round; } /* Dark palette. Applied either by an explicit user choice @@ -81,6 +89,14 @@ --radius-chip: 8px; --card-padding: 8px 18px 18px; --gap: 16px; + --ok-fill: #22c55e; + --spark-line-width: 2; + --spark-shape: line; + --spark-fill: fade; + --spark-dot: ring; + --spark-grid: none; + --gauge-width: 9; + --gauge-cap: round; } @media (prefers-color-scheme: dark) { @@ -122,6 +138,162 @@ --radius-chip: 8px; --card-padding: 8px 18px 18px; --gap: 16px; + --ok-fill: #22c55e; + --spark-line-width: 2; + --spark-shape: line; + --spark-fill: fade; + --spark-dot: ring; + --spark-grid: none; + --gauge-width: 9; + --gauge-cap: round; + } +} + +/* Console theme (`data-style="console"`): the same three blocks as Clear. Accent, on-accent + and the ok fill are literals so the contrast checks can read them. */ +:root[data-style="console"] { + --bg: #f4f5f6; + --card-bg: #ffffff; + --text: #111418; + --muted: #5a626e; + --border: #d5d9df; + --track: #dde0e5; + --accent: #111418; + --on-accent: #ffffff; + --ok: #16a34a; + --ok-ink: #15803d; + --warn: #d97706; + --warn-ink: #92400e; + --crit: #dc2626; + --crit-ink: #b91c1c; + --shadow: 0 0 0 transparent; + --accent-system: #111418; + --accent-updates: #111418; + --accent-uptime: #111418; + --accent-cpu: #111418; + --accent-load: #111418; + --accent-temperature: #111418; + --accent-memory: #111418; + --accent-disks: #111418; + --accent-network: #111418; + --accent-openhab: #111418; + --chip-tint: 0%; + --btn-tint: 0%; + --btn-border-tint: 100%; + --spark-fill-alpha: 0.07; + --dialog-shadow: 0 0 0 transparent; + --backdrop: rgba(0, 0, 0, 0.5); + --radius-card: 6px; + --radius-control: 6px; + --radius-button: 6px; + --radius-chip: 0; + --card-padding: 0 14px 14px; + --gap: 12px; + --ok-fill: #111418; + --spark-line-width: 1.5; + --spark-shape: step; + --spark-fill: flat; + --spark-dot: none; + --spark-grid: lines; + --gauge-width: 5; + --gauge-cap: butt; +} + +:root[data-style="console"][data-theme="dark"] { + --bg: #0b0c0e; + --card-bg: #121417; + --text: #e6e8eb; + --muted: #98a0ab; + --border: #282c33; + --track: #2b2f36; + --accent: #e6e8eb; + --on-accent: #121417; + --ok: #22c55e; + --ok-ink: #4ade80; + --warn: #f59e0b; + --warn-ink: #fbbf24; + --crit: #ef4444; + --crit-ink: #f87171; + --shadow: 0 0 0 transparent; + --accent-system: #e6e8eb; + --accent-updates: #e6e8eb; + --accent-uptime: #e6e8eb; + --accent-cpu: #e6e8eb; + --accent-load: #e6e8eb; + --accent-temperature: #e6e8eb; + --accent-memory: #e6e8eb; + --accent-disks: #e6e8eb; + --accent-network: #e6e8eb; + --accent-openhab: #e6e8eb; + --chip-tint: 0%; + --btn-tint: 0%; + --btn-border-tint: 100%; + --spark-fill-alpha: 0.09; + --dialog-shadow: 0 0 0 transparent; + --backdrop: rgba(0, 0, 0, 0.5); + --radius-card: 6px; + --radius-control: 6px; + --radius-button: 6px; + --radius-chip: 0; + --card-padding: 0 14px 14px; + --gap: 12px; + --ok-fill: #e6e8eb; + --spark-line-width: 1.5; + --spark-shape: step; + --spark-fill: flat; + --spark-dot: none; + --spark-grid: lines; + --gauge-width: 5; + --gauge-cap: butt; +} + +@media (prefers-color-scheme: dark) { + :root[data-style="console"]:not([data-theme]) { + --bg: #0b0c0e; + --card-bg: #121417; + --text: #e6e8eb; + --muted: #98a0ab; + --border: #282c33; + --track: #2b2f36; + --accent: #e6e8eb; + --on-accent: #121417; + --ok: #22c55e; + --ok-ink: #4ade80; + --warn: #f59e0b; + --warn-ink: #fbbf24; + --crit: #ef4444; + --crit-ink: #f87171; + --shadow: 0 0 0 transparent; + --accent-system: #e6e8eb; + --accent-updates: #e6e8eb; + --accent-uptime: #e6e8eb; + --accent-cpu: #e6e8eb; + --accent-load: #e6e8eb; + --accent-temperature: #e6e8eb; + --accent-memory: #e6e8eb; + --accent-disks: #e6e8eb; + --accent-network: #e6e8eb; + --accent-openhab: #e6e8eb; + --chip-tint: 0%; + --btn-tint: 0%; + --btn-border-tint: 100%; + --spark-fill-alpha: 0.09; + --dialog-shadow: 0 0 0 transparent; + --backdrop: rgba(0, 0, 0, 0.5); + --radius-card: 6px; + --radius-control: 6px; + --radius-button: 6px; + --radius-chip: 0; + --card-padding: 0 14px 14px; + --gap: 12px; + --ok-fill: #e6e8eb; + --spark-line-width: 1.5; + --spark-shape: step; + --spark-fill: flat; + --spark-dot: none; + --spark-grid: lines; + --gauge-width: 5; + --gauge-cap: butt; } } @@ -453,6 +625,12 @@ main { line-height: 1.1; } +/* Clear sets the unit apart from the number by a space; Console replaces it + by a margin (scoped rules below). */ +.metric-unit::before { + content: " "; +} + .metric-sub { color: var(--muted); font-size: 0.8125rem; @@ -1097,3 +1275,305 @@ footer { width: 14px; height: 14px; } + +/* Console theme rules: every selector is scoped to the theme and every colour is a token. The monospace stack is written once. */ + +:root[data-style="console"] body, +:root[data-style="console"] button, +:root[data-style="console"] input { + font-family: ui-monospace, "SF Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace; +} + +:root[data-style="console"] body { + font-size: 13px; +} + +/* Header and banner */ + +:root[data-style="console"] header { + padding: 16px 20px 8px; +} + +:root[data-style="console"] main { + padding: 16px 20px 32px; +} + +:root[data-style="console"] .logo-tile { + display: none; +} + +:root[data-style="console"] header h1 { + font-size: 18px; + font-weight: 700; +} + +:root[data-style="console"] header .subtitle { + font-size: 12px; + color: var(--muted); +} + +:root[data-style="console"] .header-buttons .icon { + stroke-width: 1.75; +} + +:root[data-style="console"] .alert-banner { + margin: 0 20px 16px; +} + +:root[data-style="console"] .alert-banner.metric-warn { + border: 1px solid var(--warn); + background: color-mix(in srgb, var(--warn) 10%, transparent); +} + +:root[data-style="console"] .alert-banner.metric-crit { + border: 1px solid var(--crit); + background: color-mix(in srgb, var(--crit) 10%, transparent); +} + +:root[data-style="console"] .alert-badge, +:root[data-style="console"] .stale-badge { + border-radius: 0; + text-transform: uppercase; + letter-spacing: 0.06em; +} + +:root[data-style="console"] .alert-badge { + font-size: 11px; +} + +:root[data-style="console"] .stale-badge { + font-size: 11px; + border: 1px solid var(--warn); + background: color-mix(in srgb, var(--warn) 10%, transparent); +} + +:root[data-style="console"] .alert-badge.metric-warn { + border: 1px solid var(--warn); + background: color-mix(in srgb, var(--warn) 10%, transparent); +} + +:root[data-style="console"] .alert-badge.metric-crit { + border: 1px solid var(--crit); + background: color-mix(in srgb, var(--crit) 10%, transparent); +} + +:root[data-style="console"] .alert-badge::before, +:root[data-style="console"] .stale-badge::before { + content: ""; + display: inline-block; + width: 6px; + height: 6px; + margin-right: 6px; + background: currentColor; + vertical-align: middle; +} + +/* Cards: the heading is a strip; in a clickable card the whole strip is the button */ + +:root[data-style="console"] .card h2 { + min-height: 44px; + margin: 0 -14px 12px; + padding: 0 14px; + border-bottom: 1px solid var(--border); + color: var(--text); + font-size: 12px; + font-weight: 600; + text-transform: uppercase; + letter-spacing: 0.08em; +} + +:root[data-style="console"] .card-clickable h2 { + padding: 0; +} + +:root[data-style="console"] .card-clickable .card-header-btn { + padding: 0 14px; +} + +:root[data-style="console"] .card-icon { + width: 14px; + height: 14px; + color: var(--muted); + background: none; +} + +:root[data-style="console"] .card-icon .icon { + width: 14px; + height: 14px; +} + +:root[data-style="console"] .card-header-btn .chevron { + width: 14px; + height: 14px; + color: var(--muted); +} + +:root[data-style="console"] #card-system, +:root[data-style="console"] #card-uptime { + padding: 0 14px 10px; +} + +:root[data-style="console"] #card-system h2, +:root[data-style="console"] #card-uptime h2 { + margin-bottom: 8px; +} + +/* Rows */ + +:root[data-style="console"] .kv-row { + font-size: 13px; + padding: 6px 0; + border-bottom: 1px dashed var(--border); +} + +:root[data-style="console"] .kv-row:last-child, +:root[data-style="console"] .pihole-section .kv-row { + border-bottom: none; +} + +:root[data-style="console"] .kv-val { + font-weight: 400; +} + +:root[data-style="console"] .net-row { + border-bottom: 1px dashed var(--border); +} + +:root[data-style="console"] .net-row:last-child { + border-bottom: none; +} + +:root[data-style="console"] .net-rate .net-icon { + width: 13px; + height: 13px; +} + +:root[data-style="console"] .pihole-section { + border-top: 1px dashed var(--border); +} + +:root[data-style="console"] .pihole-section .metric-sub { + font-size: 12px; +} + +/* Colour only for warn and crit: ok text is --text; the Pi-hole status keeps a green dot */ + +:root[data-style="console"] .metric-ok, +:root[data-style="console"] .kv-val.metric-ok, +:root[data-style="console"] .updates-table td.pkg-new { + color: var(--text); +} + +:root[data-style="console"] #pihole-status::before { + content: ""; + display: inline-block; + width: 6px; + height: 6px; + margin-right: 6px; + background: var(--muted); +} + +:root[data-style="console"] #pihole-status.metric-ok::before { + background: var(--ok); +} + +:root[data-style="console"] #pihole-status.metric-warn::before { + background: var(--warn); +} + +/* Metric value, number and unit */ + +:root[data-style="console"] .metric-value { + font-size: 30px; + font-weight: 600; + letter-spacing: -0.03em; + line-height: 1.1; +} + +:root[data-style="console"] .metric-unit { + font-size: 15px; + font-weight: 500; + color: var(--muted); + margin-left: 4px; +} + +:root[data-style="console"] .metric-unit::before { + content: none; +} + +:root[data-style="console"] .metric-sub { + font-size: 12px; +} + +/* Segmented bars: 6 px segments, 2 px gaps, both patterns start at the track's left edge */ + +:root[data-style="console"] .bar-track { + height: 12px; + border-radius: 0; + background: repeating-linear-gradient(90deg, var(--track) 0 6px, transparent 6px 8px); +} + +:root[data-style="console"] .bar-fill { + height: 12px; + border-radius: 0; + background: repeating-linear-gradient(90deg, var(--ok-fill) 0 6px, transparent 6px 8px); +} + +:root[data-style="console"] .bar-fill.metric-warn { + background: repeating-linear-gradient(90deg, var(--warn) 0 6px, transparent 6px 8px); +} + +:root[data-style="console"] .bar-fill.metric-crit { + background: repeating-linear-gradient(90deg, var(--crit) 0 6px, transparent 6px 8px); +} + +:root[data-style="console"] .bar-row { + margin-bottom: 12px; +} + +:root[data-style="console"] .bar-row:last-child { + margin-bottom: 0; +} + +:root[data-style="console"] .bar-label { + font-size: 12px; +} + +/* Charts and gauges */ + +:root[data-style="console"] .sparkline { + height: 48px; +} + +:root[data-style="console"] .gauge .gauge-label { + font-size: 11px; + text-transform: uppercase; + letter-spacing: 0.06em; + color: var(--muted); +} + +:root[data-style="console"] .gauge .gauge-value { + font-weight: 600; +} + +/* Buttons, dialogs and footer */ + +:root[data-style="console"] .updates-button { + font-size: 13px; +} + +:root[data-style="console"] .apikey-submit { + color: var(--text); + background: transparent; + border: 1px solid var(--accent); +} + +:root[data-style="console"] footer { + font-size: 12px; + color: var(--muted); +} + +:root[data-style="console"] footer a { + color: var(--text); + text-decoration: underline; +} diff --git a/internal/web/assets/theme-init.js b/internal/web/assets/theme-init.js index cd69f0a..428bd3b 100644 --- a/internal/web/assets/theme-init.js +++ b/internal/web/assets/theme-init.js @@ -2,6 +2,7 @@ // (Clear). A theme's id is its `data-style` value and the stored choice. const PIMONITOR_THEMES = Object.freeze([ Object.freeze({ id: 'clear', name: 'Clear' }), + Object.freeze({ id: 'console', name: 'Console' }), ]); // Clear's light values of the custom properties the canvas renderers read. @@ -10,10 +11,18 @@ const DRAWING_FALLBACKS = Object.freeze({ '--muted': '#5f6876', '--card-bg': '#ffffff', '--track': '#e8eaef', - '--ok': '#16a34a', '--warn': '#d97706', '--crit': '#dc2626', '--spark-fill-alpha': '0.28', + '--border': '#e4e7ec', + '--ok-fill': '#16a34a', + '--spark-line-width': '2', + '--spark-shape': 'line', + '--spark-fill': 'fade', + '--spark-dot': 'ring', + '--spark-grid': 'none', + '--gauge-width': '9', + '--gauge-cap': 'round', }); // themeValue returns the trimmed computed value of the custom property `name`. @@ -37,6 +46,13 @@ function themeNumber(name, min, max) { return v !== '' && Number.isFinite(n) && n >= min && n <= max ? n : Number(DRAWING_FALLBACKS[name]); } +// themeKeyword returns the trimmed computed value of the custom property +// `name` when `allowed` holds it, else Clear's value. +function themeKeyword(name, allowed) { + const v = themeValue(name); + return allowed.includes(v) ? v : DRAWING_FALLBACKS[name]; +} + // Apply a previously chosen theme before the first paint to avoid a flash // of the wrong palette. With no stored choice we leave `data-theme` unset // so the CSS `prefers-color-scheme` default takes over. diff --git a/internal/web/chart_test.go b/internal/web/chart_test.go index f34de39..c17fa0a 100644 --- a/internal/web/chart_test.go +++ b/internal/web/chart_test.go @@ -13,11 +13,12 @@ import ( // least this much. const sparkDotReach = 4 + 3 -// TestChartJS_Sparkline_AC7 guards the sparkline style of feature 217: a 2 px -// round-joined accent line, a fill fading to transparent built without string -// concatenation on the colour, a dot with a card-coloured ring at the newest -// point, nothing drawn below two points, and a plot inset that keeps the dot -// inside the canvas. +// TestChartJS_Sparkline_AC7 guards the sparkline style of feature 217 as the +// feature 219 declares it: Clear's 2 px round-joined accent line (the width +// through the --spark-line-width token), a fill fading to transparent built +// without string concatenation on the colour, a dot with a card-coloured ring +// at the newest point, nothing drawn below two points, and a plot inset that +// keeps the dot inside the canvas. func TestChartJS_Sparkline_AC7(t *testing.T) { js := readAsset(t, "chart.js") body := jsFunctionBody(js, "function drawSparkline(") @@ -26,7 +27,7 @@ func TestChartJS_Sparkline_AC7(t *testing.T) { } t.Run("AC7 line, gradient fade and dot", func(t *testing.T) { - checkSparklineStyle(t, body) + checkSparklineStyle(t, js, body) }) t.Run("AC7 fewer than two points draw nothing", func(t *testing.T) { @@ -38,20 +39,62 @@ func TestChartJS_Sparkline_AC7(t *testing.T) { }) } -// checkSparklineStyle checks the line, the gradient fade and the dot of drawSparkline. -func checkSparklineStyle(t *testing.T, body string) { +// checkSparklineStyle checks the line, the gradient fade and the dot of the +// sparkline: drawSparkline takes its style from sparkStyle and draws grid, +// fill, line and dot in that order; Clear's values are asserted from its block. +func checkSparklineStyle(t *testing.T, js, body string) { t.Helper() checkAssetParts(t, []assetCheck{ {"drawSparkline", body, []string{ - "ctx.lineWidth = 2;", "ctx.lineJoin = 'round'", "createLinearGradient(", - "destination-out", "source-over", "--spark-fill-alpha", "--card-bg", "arc(", + "sparkStyle(", "drawSparkGrid(", "fillSparkline(", "traceSparkline(", "drawSparkDot(", + "ctx.lineJoin = 'round'", "ctx.stroke()", "themeColor('--accent')", + }}, + {"fillSparkline (fade)", jsFunctionBody(js, "function fillSparkline("), []string{ + "createLinearGradient(", "destination-out", "source-over", "globalAlpha", + }}, + {"drawSparkDot (ring)", jsFunctionBody(js, "function drawSparkDot("), []string{"arc(", "cardBg"}}, + {"sparkStyle", jsFunctionBody(js, "function sparkStyle("), []string{ + "themeNumber('--spark-line-width'", "themeNumber('--spark-fill-alpha'", "themeColor('--card-bg')", }}, }) checkAssetPartsAbsent(t, []assetCheck{ - {"drawSparkline (fill works for any CSS colour)", body, []string{"color + '", "lineWidth = 1.5"}}, + {"drawSparkline (width through the token, fill works for any CSS colour)", body, []string{"color + '", "ctx.lineWidth = 2;", "lineWidth = 1.5"}}, }) - if n := strings.Count(body, "arc("); n < 2 { - t.Errorf("chart.js drawSparkline: %d arc( calls, want at least 2 (ring and dot)", n) + if !regexp.MustCompile(`ctx\.lineWidth\s*=\s*\w+\.lineWidth\s*;`).MatchString(body) { + t.Error("chart.js drawSparkline: expected ctx.lineWidth =