mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 11:26:25 -05:00
ui: media filtering is a per-theme setting, not a night-mode hardcode
Filtering media was keyed on `data-theme="night"`, which made it a property of
the theme that no user could turn off. Some people want their media tinted to
match the interface; some want to see the actual image.
A theme now declares a filter and whether it starts on, and the user decides:
night night-red on by default. An unfiltered thumbnail undoes dark
adaptation, which is the whole point of the mode.
amber amber-mono off by default. Amber is a look, not a night-vision
aid, so media keeps its real colors until asked for.
light - no filter offered, so no toggle is shown.
dark -
The choice is stored per theme, so wanting amber's tint says nothing about
wanting night's. The toggle lives in the theme picker, which puts it on the
Settings page and in the gallery at once.
Mechanically the rules now key on a `data-media-filter` attribute rather than
`data-theme`: a rule keyed on the theme could not be turned off. index.html
stamps that attribute before first paint alongside data-theme -- without it,
night mode paints a screen of unfiltered thumbnails while the bundle arrives
over a hotspot, costing the user the adaptation the theme exists to protect.
Tests cover both the attribute rule and the fact that the inline script's
duplicated ids and defaults still agree with names.ts.
The amber matrix is the same luminance collapse tinted to #ff9500 (red full,
green 0.584, blue zero) with a gentler gamma than night's: amber carries far
more luminance and clips to a flat wash otherwise.
The gallery gains a Media filtering section showing the WROLPi logo filtered,
beside an unfiltered copy that opts out with `night-unfiltered`. That copy is
behind a click on purpose -- an unfiltered image is a bright patch, and night
mode should not spring one on someone reading in the dark.
ui-design.md gains the media filtering contract it never got in Phase 0,
including the note that a Phase 3 custom theme names an existing filter id and
cannot ship its own matrix, since that would be code rather than data.
This commit is contained in:
parent
973a61af28
commit
efe84d17f1
13 changed files with 546 additions and 39 deletions
|
|
@ -32,6 +32,29 @@ export function MediaFilterDefs() {
|
|||
<feFuncR type='gamma' amplitude='1' exponent='0.65' offset='0'/>
|
||||
</feComponentTransfer>
|
||||
</filter>
|
||||
|
||||
{/*
|
||||
Amber: the same luminance collapse, then tinted to the theme's amber
|
||||
(#ff9500) by scaling each channel to that hue's ratios — red at full,
|
||||
green at 0.584, blue at zero. Amber carries far more luminance than
|
||||
red, so it needs a gentler lift; 0.85 keeps highlights from clipping
|
||||
to a flat wash.
|
||||
|
||||
This is opt-in: amber is a look, not a night-vision aid.
|
||||
*/}
|
||||
<filter id='wrolpi-amber-mono' colorInterpolationFilters='sRGB'>
|
||||
<feColorMatrix
|
||||
type='matrix'
|
||||
values='0.2126 0.7152 0.0722 0 0
|
||||
0.1242 0.4177 0.0422 0 0
|
||||
0 0 0 0 0
|
||||
0 0 0 1 0'
|
||||
/>
|
||||
<feComponentTransfer>
|
||||
<feFuncR type='gamma' amplitude='1' exponent='0.85' offset='0'/>
|
||||
<feFuncG type='gamma' amplitude='1' exponent='0.85' offset='0'/>
|
||||
</feComponentTransfer>
|
||||
</filter>
|
||||
</defs>
|
||||
</svg>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import {SavedThemeName, ThemeName} from '../types/theme';
|
||||
import {SavedThemeName, ThemeMediaFilter, ThemeName} from '../types/theme';
|
||||
|
||||
/*
|
||||
* Theme names and the choices offered by the pickers.
|
||||
|
|
@ -29,6 +29,62 @@ export const isDarkTheme = (theme: ThemeName): boolean => darkThemes.includes(th
|
|||
export const isThemeName = (value: unknown): value is ThemeName =>
|
||||
themeNames.includes(value as ThemeName);
|
||||
|
||||
/*
|
||||
* Media filtering.
|
||||
*
|
||||
* Images, video, PDFs, embedded pages, and the map canvas cannot consume theme
|
||||
* tokens, so a monochrome theme can pass them through an SVG color matrix
|
||||
* instead (see MediaFilterDefs.tsx for the filters, tokens.css for the rule).
|
||||
*
|
||||
* A theme declares which filter it uses and whether it is on to begin with; the
|
||||
* user can then turn it on or off for that theme. Night defaults to on — an
|
||||
* unfiltered thumbnail undoes dark adaptation, which is the whole point of the
|
||||
* mode — and amber defaults to off, since it is a look rather than a night-vision
|
||||
* aid. Neither is forced: some users want their media tinted to match, some
|
||||
* want to see the actual image.
|
||||
*
|
||||
* The choice is stored per theme, so turning it off in amber does not disarm it
|
||||
* in night.
|
||||
*/
|
||||
export const themeMediaFilters: Partial<Record<ThemeName, ThemeMediaFilter>> = {
|
||||
[nightTheme]: {
|
||||
id: 'night-red',
|
||||
defaultOn: true,
|
||||
label: 'Filter media to red',
|
||||
description: 'Videos, images, documents, and maps are remapped to red. Turning this off '
|
||||
+ 'shows their real colors, which will spoil your night vision.',
|
||||
},
|
||||
[amberTheme]: {
|
||||
id: 'amber-mono',
|
||||
defaultOn: false,
|
||||
label: 'Filter media to amber',
|
||||
description: 'Videos, images, documents, and maps are remapped to amber, matching the '
|
||||
+ 'interface. Off by default so media keeps its real colors.',
|
||||
},
|
||||
};
|
||||
|
||||
/** The filter a theme offers, or undefined when it has none to offer. */
|
||||
export const themeMediaFilter = (theme: ThemeName): ThemeMediaFilter | undefined =>
|
||||
themeMediaFilters[theme];
|
||||
|
||||
export const mediaFilterSessionKey = 'media-filter';
|
||||
|
||||
/**
|
||||
* Resolve the filter id to stamp on `<html>` — '' when nothing should be filtered.
|
||||
*
|
||||
* `overrides` is the stored map of theme name to boolean; a theme missing from it
|
||||
* has never been changed and follows its own default.
|
||||
*/
|
||||
export const resolveMediaFilter = (
|
||||
theme: ThemeName,
|
||||
overrides: Partial<Record<ThemeName, boolean>>,
|
||||
): string => {
|
||||
const filter = themeMediaFilters[theme];
|
||||
if (!filter) return '';
|
||||
const enabled = overrides[theme] ?? filter.defaultOn;
|
||||
return enabled ? filter.id : '';
|
||||
}
|
||||
|
||||
export interface ThemeChoice {
|
||||
value: SavedThemeName;
|
||||
text: string;
|
||||
|
|
@ -52,12 +108,13 @@ export const themeChoices: ThemeChoice[] = [
|
|||
text: 'Night',
|
||||
icon: 'eye',
|
||||
description: 'Red only, to preserve your night vision in the dark. Videos, documents, ' +
|
||||
'and maps are filtered to red as well.',
|
||||
'and maps are filtered to red as well, unless you turn that off.',
|
||||
},
|
||||
{
|
||||
value: amberTheme,
|
||||
text: 'Amber',
|
||||
icon: 'terminal',
|
||||
description: 'Monochrome amber, like a classic terminal.',
|
||||
description: 'Monochrome amber, like a classic terminal. Media keeps its real colors '
|
||||
+ 'unless you ask for it to be tinted too.',
|
||||
},
|
||||
];
|
||||
|
|
|
|||
|
|
@ -169,15 +169,25 @@ body {
|
|||
/* ------------------------------------------------------------------------- */
|
||||
/*
|
||||
* Videos, images, PDFs, embedded pages, and the map canvas cannot consume
|
||||
* tokens, so night mode passes them through an SVG color matrix that maps
|
||||
* luminance onto the red channel and drives green and blue to zero. Nothing
|
||||
* a user opens can break their dark adaptation.
|
||||
* tokens, so a theme can pass them through an SVG color matrix instead. Night
|
||||
* maps luminance onto the red channel so nothing a user opens can break their
|
||||
* dark adaptation; amber tints media to match the interface.
|
||||
*
|
||||
* This targets the element types rather than an opt-in class on purpose: night
|
||||
* mode has to fail safe. A library of thousands of files reaches the screen
|
||||
* through many components, and one that forgot to opt in would undo half an
|
||||
* hour of dark adaptation. `.media` stays available for a wrapper that needs
|
||||
* filtering as a unit (a player with custom controls, say).
|
||||
* These rules key on `data-media-filter`, not `data-theme`, because filtering is
|
||||
* a per-theme setting the user controls rather than a property of the theme's
|
||||
* palette: night is filtered unless turned off, amber is unfiltered unless
|
||||
* turned on. An empty or absent attribute filters nothing, so light and dark —
|
||||
* which offer no filter — match no rule at all. ThemeProvider stamps the
|
||||
* attribute, and index.html stamps it before first paint (an unfiltered flash of
|
||||
* thumbnails would cost the user their dark adaptation before React even loads).
|
||||
*
|
||||
* The selectors target element types rather than an opt-in class on purpose:
|
||||
* filtering has to fail safe. A library of thousands of files reaches the
|
||||
* screen through many components, and one that forgot to opt in would undo half
|
||||
* an hour of dark adaptation. `.media` stays available for a wrapper that needs
|
||||
* filtering as a unit (a player with custom controls, say), and
|
||||
* `.night-unfiltered` opts a single element out — for media whose real colors
|
||||
* are the point, like the side-by-side comparison on /theme-sample.
|
||||
*
|
||||
* Only leaf media is filtered, never an app-level ancestor: a CSS filter
|
||||
* creates a containing block, which would break `position: fixed` descendants
|
||||
|
|
@ -186,14 +196,22 @@ body {
|
|||
*
|
||||
* Filter definitions live in <MediaFilterDefs/> (mounted once by App).
|
||||
*/
|
||||
html[data-theme="night"] :is(img, video, canvas, iframe, embed, object, .media):not(.night-unfiltered) {
|
||||
html[data-media-filter="night-red"] :is(img, video, canvas, iframe, embed, object, .media):not(.night-unfiltered) {
|
||||
filter: url(#wrolpi-night-red);
|
||||
}
|
||||
|
||||
html[data-media-filter="amber-mono"] :is(img, video, canvas, iframe, embed, object, .media):not(.night-unfiltered) {
|
||||
filter: url(#wrolpi-amber-mono);
|
||||
}
|
||||
|
||||
/*
|
||||
* A filter on an ancestor stops applying once the browser promotes a video to
|
||||
* native fullscreen, so the filter is restated on the fullscreened element.
|
||||
*/
|
||||
html[data-theme="night"] :fullscreen:not(.night-unfiltered) {
|
||||
html[data-media-filter="night-red"] :fullscreen:not(.night-unfiltered) {
|
||||
filter: url(#wrolpi-night-red);
|
||||
}
|
||||
|
||||
html[data-media-filter="amber-mono"] :fullscreen:not(.night-unfiltered) {
|
||||
filter: url(#wrolpi-amber-mono);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {themeNames, themeSessionKey} from '../components/Theme';
|
||||
import {mediaFilterSessionKey, themeMediaFilter} from './names';
|
||||
|
||||
const readFile = (...parts) => fs.readFileSync(path.join(__dirname, ...parts), 'utf8');
|
||||
|
||||
|
|
@ -73,4 +74,52 @@ describe('pre-paint theme script', () => {
|
|||
const head = indexHtml.slice(0, indexHtml.indexOf('</head>'));
|
||||
expect(head).toContain("document.documentElement.setAttribute('data-theme'");
|
||||
});
|
||||
|
||||
it('stamps the media filter before the bundle loads too', () => {
|
||||
// Otherwise night mode paints a screen of unfiltered thumbnails while the bundle
|
||||
// arrives, which costs the user the dark adaptation the theme exists to protect.
|
||||
const head = indexHtml.slice(0, indexHtml.indexOf('</head>'));
|
||||
expect(head).toContain("document.documentElement.setAttribute('data-media-filter'");
|
||||
expect(head).toContain(`localStorage.getItem('${mediaFilterSessionKey}')`);
|
||||
});
|
||||
|
||||
it('duplicates the same filter ids and defaults the app uses', () => {
|
||||
// The script cannot import from names.ts, so it restates both. These assertions
|
||||
// fail if a theme's filter id or default changes on only one side.
|
||||
themeNames.forEach(theme => {
|
||||
const filter = themeMediaFilter(theme);
|
||||
if (!filter) return;
|
||||
expect(indexHtml).toContain(`${theme}: '${filter.id}'`);
|
||||
expect(indexHtml).toContain(`${theme}: ${filter.defaultOn}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('media filter rules', () => {
|
||||
it('keys on the filter, not on the theme', () => {
|
||||
// Filtering is a per-theme setting the user controls, so a rule keyed on
|
||||
// `data-theme` could not be turned off.
|
||||
const filterRules = [...tokensCss.matchAll(/^html\[([^\]]+)][^{]*{\s*filter:/gm)]
|
||||
.map(match => match[1]);
|
||||
|
||||
expect(filterRules.length).toBeGreaterThan(0);
|
||||
filterRules.forEach(selector => expect(selector).toMatch(/^data-media-filter=/));
|
||||
});
|
||||
|
||||
it('has a rule for every filter a theme offers', () => {
|
||||
themeNames.forEach(theme => {
|
||||
const filter = themeMediaFilter(theme);
|
||||
if (!filter) return;
|
||||
expect(tokensCss).toContain(`html[data-media-filter="${filter.id}"]`);
|
||||
// Restated for native fullscreen, where an ancestor's filter stops applying.
|
||||
expect(tokensCss).toMatch(
|
||||
new RegExp(`html\\[data-media-filter="${filter.id}"] :fullscreen`));
|
||||
});
|
||||
});
|
||||
|
||||
it('never filters an ancestor, only leaf media', () => {
|
||||
// A CSS filter creates a containing block, which would break `position: fixed`
|
||||
// descendants — the nav bar and every modal.
|
||||
expect(tokensCss).not.toMatch(/data-media-filter="[^"]+"]\s*{\s*filter:/);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue