Follow the system's reduced motion and contrast; reload sheets on save

@media (prefers-reduced-motion) and (prefers-contrast) are answered from the
OS (Windows settings, macOS accessibility, the desktop portal, the browser),
reduced motion makes every animation jump to its end, and reaktor.css
strengthens muted text and borders under more contrast. --motion and
--contrast override; --watch-css restyles when a sheet on disk is saved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-19 22:31:58 +02:00
co-authored by Claude Opus 5.5
parent 24b60c910a
commit 1730c91bd2
12 changed files with 444 additions and 13 deletions
+10
View File
@@ -5,6 +5,16 @@
--table-bg-alt: var(--background-alt);
}
@media (prefers-contrast: more) {
:root {
--text-main: var(--text-bright);
--text-muted: var(--text-bright);
--form-placeholder: var(--text-bright);
--border: var(--text-bright);
--focus: var(--links);
}
}
h4 {
font-size: 19px;
font-weight: 700;
+9 -1
View File
@@ -88,11 +88,19 @@ reaktor_ease_name(unsigned char curve)
return curve < REAKTOR_EASE_COUNT ? n[curve] : "?";
}
static int g_reduce;
void
reaktor_anim_reduce(int on)
{
g_reduce = on;
}
float
reaktor_animate(unsigned id, unsigned channel, float to, float ms,
unsigned char curve)
{
if (ms <= 0.0f || !g_anim) return to;
if (ms <= 0.0f || g_reduce || !g_anim) return to;
return nka_tween_float(g_anim, id, channel, to, ms / 1000.0f,
nka_ease(curve), NKA_POLICY_CROSSFADE, to);
}
+1
View File
@@ -9,5 +9,6 @@ void reaktor_anim_destroy(struct nka_context *a);
void reaktor_anim_frame(struct nka_context *a, float dt_ms);
int reaktor_anim_settle(void);
void reaktor_anim_reduce(int on);
#endif
+82 -5
View File
@@ -987,8 +987,79 @@ static void emit_one(void *c, const char *p, size_t plen,
free(conv.p);
}
static int g_reduce_motion, g_more_contrast;
void reaktor_css_media(int reduce_motion, int more_contrast)
{
g_reduce_motion = reduce_motion;
g_more_contrast = more_contrast;
}
static int is_word(const char *s, size_t n, const char *w)
{
return strlen(w) == n && strncmp(s, w, n) == 0;
}
/* 1 or 0 for a feature this runtime answers, -1 for any other. */
static int media_feature(const char *s, size_t n, const char *theme)
{
const char *colon = (const char *)memchr(s, ':', n);
size_t nl = colon ? (size_t)(colon - s) : n, vl = 0;
const char *v = colon ? colon + 1 : s + n;
while (nl && isspace((unsigned char)s[nl - 1])) nl--;
if (colon) {
while (v < s + n && isspace((unsigned char)*v)) v++;
vl = (size_t)(s + n - v);
while (vl && isspace((unsigned char)v[vl - 1])) vl--;
}
if (is_word(s, nl, "prefers-color-scheme"))
return colon && is_word(v, vl, theme);
if (is_word(s, nl, "prefers-reduced-motion")) {
if (!colon || is_word(v, vl, "reduce")) return g_reduce_motion;
return is_word(v, vl, "no-preference") && !g_reduce_motion;
}
if (is_word(s, nl, "prefers-contrast")) {
if (!colon || is_word(v, vl, "more")) return g_more_contrast;
return is_word(v, vl, "no-preference") && !g_more_contrast;
}
return -1;
}
/* Whether a lowercased @media prelude holds: one of its comma-separated
queries names only features answered here, each true. */
static int media_matches(const char *q, const char *theme)
{
while (*q) {
int ok = 1, features = 0;
while (*q && *q != ',' && *q != '{') {
const char *s;
if (isspace((unsigned char)*q)) { q++; continue; }
if (*q == '(') {
s = ++q;
while (*q && *q != ')') q++;
if (media_feature(s, (size_t)(q - s), theme) != 1) ok = 0;
features++;
if (*q) q++;
continue;
}
s = q;
while (*q && !isspace((unsigned char)*q) && *q != '(' && *q != ',' && *q != '{') q++;
if (!is_word(s, (size_t)(q - s), "only") && !is_word(s, (size_t)(q - s), "and") &&
!is_word(s, (size_t)(q - s), "screen") && !is_word(s, (size_t)(q - s), "all"))
ok = 0;
}
if (ok && features) return 1;
if (*q != ',') return 0;
q++;
}
return 0;
}
static void
unwrap_color_scheme(char *src, const char *theme)
unwrap_media(char *src, const char *theme)
{
const char *want = (theme && *theme) ? theme : "light";
size_t i = 0, len = strlen(src);
@@ -1016,8 +1087,7 @@ unwrap_color_scheme(char *src, const char *theme)
low[k] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c;
}
low[n] = '\0';
keep = strstr(low, "prefers-color-scheme") != NULL &&
strstr(low, want) != NULL;
keep = media_matches(low + 6, want);
}
body_s = prelude_e + 1;
@@ -1078,7 +1148,7 @@ static char *flatten(char *src, const char *theme, size_t token_max,
if (!m) { free(src); return NULL; }
strip_comments(src);
unwrap_color_scheme(src, theme);
unwrap_media(src, theme);
len = strlen(src);
i = 0;
@@ -1215,9 +1285,16 @@ static char *import_path(const char *from, const char *to, size_t n)
return p;
}
static void (*g_seen)(const char *path);
void reaktor_css_track(void (*seen)(const char *path)) { g_seen = seen; }
static int load_sheet(const char *path, int depth, buf *all)
{
char *t = reaktor_asset_load(path, NULL);
char *t;
if (g_seen) g_seen(path);
t = reaktor_asset_load(path, NULL);
size_t i = 0, from = 0, n;
int ok = 1;
+5
View File
@@ -9,6 +9,11 @@ typedef struct reaktor_cssvars reaktor_cssvars;
char *reaktor_css_flatten(const char *const *paths, int count, const char *theme,
size_t token_max, reaktor_cssvars **out_vars);
/* What @media (prefers-reduced-motion) and (prefers-contrast) answer. */
void reaktor_css_media(int reduce_motion, int more_contrast);
/* Told each sheet a flatten reads, @imports too, as the sheet names it. */
void reaktor_css_track(void (*seen)(const char *path));
int reaktor_cssvars_color(const reaktor_cssvars *vars, const char *name,
unsigned char rgba[4]);
+22 -2
View File
@@ -63,6 +63,7 @@ errors, so an internal function cannot slip back in.
| `reaktor_wake(app)` | Draw a frame of that App's window. Any thread |
| `reaktor_set_theme(app, theme)` | `REAKTOR_THEME_SYSTEM`, `_LIGHT` or `_DARK`, for every window |
| `reaktor_set_css(app, sheets, n)` | Replace the application's sheets; `--css` stays on top |
| `reaktor_reduced_motion()`, `reaktor_high_contrast()` | Whether the system asks for less motion or more contrast, after `--motion` and `--contrast` |
| `reaktor_set_title(app, title)` | Retitle that App's window |
| `reaktor_set_confirm_close(app, question)` | Ask `question` before quitting, or with `NULL` stop asking |
| `reaktor_user(app)` | The launch's `.user` |
@@ -672,8 +673,9 @@ and the accent. An icon on a button takes its label's color.
`color-mix(in srgb, …)`, writes every `rgb()` as `rgba()` with four
arguments, clamped, turns `em` and `rem` into pixels, works out `calc()`
over pixels and plain numbers (one with a percentage is left, and dropped),
keeps the `@media (prefers-color-scheme)` branch for the active scheme and
drops width and print queries, rewrites `[type=range]` as `.type-range`
keeps an `@media` block whose queries are all `prefers-color-scheme`,
`prefers-reduced-motion` or `prefers-contrast` and hold now, and drops
width and print queries, rewrites `[type=range]` as `.type-range`
and `[disabled]` as `.disabled`, and drops `:enabled`, since every widget
that asks is enabled and a disabled one asks for `:disabled`. An operator
form like `[href^="http"]` cannot be expressed, and its rule is dropped, as
@@ -684,6 +686,21 @@ and the accent. An icon on a button takes its label's color.
reaktor.css uses the scheme branch to strengthen light hovers and tint the
current sidebar entry from `--links`.
**The system's settings.** Reduced motion and high contrast are read from the
system: Windows' animation and high-contrast settings, macOS's accessibility
display options, the desktop portal's `reduced-motion` and `contrast` (GNOME's
own keys where the portal lacks them), and the browser's media queries. They
reach the sheets as `@media (prefers-reduced-motion: reduce)` and
`(prefers-contrast: more)`; reaktor.css answers more contrast by drawing muted
text, placeholders and borders in `--text-bright`. Under reduced motion every
`reaktor_animate` returns its end value at once. Both are read again when the
system theme changes and when the window regains focus.
**Live reload.** With `--watch-css` the runtime looks at every sheet it read
from disk, `@import`s included, twice a second, and restyles when one is
saved. A sheet compiled in is not watched; one on disk is preferred to it, so
in a checkout the built-in sheets are watched too.
`reaktor_set_css` swaps the application's sheets at the next frame. The
showcase's Styling page lays Simple.css over Water.css that way, which is the
test that the look is not in the code. reaktor.css is written in Water.css's
@@ -1100,6 +1117,9 @@ removes them from `argv` before the application sees it:
| `--log <path>` | Override `.log` |
| `--inspect`, `--no-inspector` | Open the [Inspector](#inspector) at start; detach it |
| `--css <path>` | A sheet after the application's; up to four |
| `--watch-css` | Restyle when a sheet read from disk is saved |
| `--motion <reduce\|full>` | Override the system's reduced motion |
| `--contrast <more\|normal>` | Override the system's contrast |
| `--font <path>`, `--font-bold <path>` | Replace the UI face and its bold |
| `--icons <dir>` | Look for `<name>.svg` here first; up to four |
+2
View File
@@ -144,6 +144,8 @@ void reaktor_request_quit(App *app);
void reaktor_wake(App *app);
void reaktor_set_theme(App *app, reaktor_theme theme);
void reaktor_set_css(App *app, const reaktor_asset *css, int count);
int reaktor_reduced_motion(void);
int reaktor_high_contrast(void);
void reaktor_set_title(App *app, const char *title);
void reaktor_set_confirm_close(App *app, const char *question);
void *reaktor_user(App *app);
+154
View File
@@ -4,6 +4,11 @@
#ifdef _WIN32
#include <windows.h>
#elif defined(__EMSCRIPTEN__)
#include <emscripten.h>
#elif defined(__APPLE__)
#include <objc/message.h>
#include <objc/runtime.h>
#endif
int reaktor_prefers_dark(void)
@@ -15,6 +20,155 @@ int reaktor_prefers_dark(void)
}
}
#if defined(__APPLE__) && !defined(__EMSCRIPTEN__)
static int workspace_says(const char *what)
{
id (*send)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
BOOL (*ask)(id, SEL) = (BOOL (*)(id, SEL))objc_msgSend;
Class c = objc_getClass("NSWorkspace");
id ws = c ? send((id)c, sel_registerName("sharedWorkspace")) : nil;
return ws && ask(ws, sel_registerName(what));
}
#elif !defined(_WIN32) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
/* The desktop portal's settings, through a libdbus loaded when first asked. */
typedef struct { void *a, *b; unsigned c; void *d; } dbus_error;
typedef union { void *p[16]; long long l[16]; } dbus_iter;
static struct {
int tried;
void *lib, *conn;
void (*error_init)(dbus_error *);
void (*error_free)(dbus_error *);
void *(*bus_get)(int, dbus_error *);
void *(*method_call)(const char *, const char *, const char *, const char *);
int (*append_args)(void *, int, ...);
void *(*send_block)(void *, void *, int, dbus_error *);
void (*message_unref)(void *);
int (*iter_init)(void *, dbus_iter *);
int (*iter_type)(dbus_iter *);
void (*iter_recurse)(dbus_iter *, dbus_iter *);
void (*iter_basic)(dbus_iter *, void *);
} g_dbus;
static int dbus_ready(void)
{
dbus_error e;
if (g_dbus.tried) return g_dbus.conn != NULL;
g_dbus.tried = 1;
if (!(g_dbus.lib = SDL_LoadObject("libdbus-1.so.3"))) return 0;
#define DBUS_FN(f, name) if (!(*(void **)&g_dbus.f = (void *)SDL_LoadFunction(g_dbus.lib, name))) return 0
DBUS_FN(error_init, "dbus_error_init");
DBUS_FN(error_free, "dbus_error_free");
DBUS_FN(bus_get, "dbus_bus_get");
DBUS_FN(method_call, "dbus_message_new_method_call");
DBUS_FN(append_args, "dbus_message_append_args");
DBUS_FN(send_block, "dbus_connection_send_with_reply_and_block");
DBUS_FN(message_unref, "dbus_message_unref");
DBUS_FN(iter_init, "dbus_message_iter_init");
DBUS_FN(iter_type, "dbus_message_iter_get_arg_type");
DBUS_FN(iter_recurse, "dbus_message_iter_recurse");
DBUS_FN(iter_basic, "dbus_message_iter_get_basic");
#undef DBUS_FN
g_dbus.error_init(&e);
g_dbus.conn = g_dbus.bus_get(0, &e);
g_dbus.error_free(&e);
return g_dbus.conn != NULL;
}
/* A setting as a number, or -1 when the portal does not have it. */
static int portal_read(const char *ns, const char *key)
{
static const char *const how[2] = { "ReadOne", "Read" };
int i, out = -1;
if (!dbus_ready()) return -1;
for (i = 0; i < 2 && out < 0; i++) {
void *msg, *reply;
dbus_error e;
dbus_iter it, in;
int type;
msg = g_dbus.method_call("org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings", how[i]);
if (!msg) return -1;
if (!g_dbus.append_args(msg, 's', &ns, 's', &key, 0)) {
g_dbus.message_unref(msg);
return -1;
}
g_dbus.error_init(&e);
reply = g_dbus.send_block(g_dbus.conn, msg, 300, &e);
g_dbus.error_free(&e);
g_dbus.message_unref(msg);
if (!reply) continue;
if (g_dbus.iter_init(reply, &it)) {
while ((type = g_dbus.iter_type(&it)) == 'v') {
g_dbus.iter_recurse(&it, &in);
it = in;
}
if (type == 'u' || type == 'i' || type == 'b') {
Uint32 v = 0;
g_dbus.iter_basic(&it, &v);
out = (int)v;
}
}
g_dbus.message_unref(reply);
}
return out;
}
#endif
int reaktor_prefers_reduced_motion(void)
{
#if defined(_WIN32)
BOOL on = TRUE;
return SystemParametersInfoW(SPI_GETCLIENTAREAANIMATION, 0, &on, 0) && !on;
#elif defined(__EMSCRIPTEN__)
return EM_ASM_INT({
return window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches ? 1 : 0;
});
#elif defined(__APPLE__)
return workspace_says("accessibilityDisplayShouldReduceMotion");
#elif !defined(__ANDROID__)
int v = portal_read("org.freedesktop.appearance", "reduced-motion");
if (v >= 0) return v == 1;
v = portal_read("org.gnome.desktop.interface", "enable-animations");
return v == 0;
#else
return 0;
#endif
}
int reaktor_prefers_contrast(void)
{
#if defined(_WIN32)
HIGHCONTRASTW hc;
hc.cbSize = sizeof hc;
return SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof hc, &hc, 0) &&
(hc.dwFlags & HCF_HIGHCONTRASTON);
#elif defined(__EMSCRIPTEN__)
return EM_ASM_INT({
return window.matchMedia && (matchMedia('(prefers-contrast: more)').matches ||
matchMedia('(forced-colors: active)').matches) ? 1 : 0;
});
#elif defined(__APPLE__)
return workspace_says("accessibilityDisplayShouldIncreaseContrast");
#elif !defined(__ANDROID__)
int v = portal_read("org.freedesktop.appearance", "contrast");
if (v >= 0) return v == 1;
return portal_read("org.gnome.desktop.a11y.interface", "high-contrast") == 1;
#else
return 0;
#endif
}
int reaktor_window_set_dark(SDL_Window *win, int dark)
{
#ifdef _WIN32
+2
View File
@@ -2,6 +2,8 @@
#define REAKTOR_THEME_H
int reaktor_prefers_dark(void);
int reaktor_prefers_reduced_motion(void);
int reaktor_prefers_contrast(void);
struct SDL_Window;
+117 -1
View File
@@ -6,6 +6,7 @@
#include "locale.h"
#include "reaktor/launch.h"
#include "lifecycle.h"
#include "cssflat.h"
#ifdef __EMSCRIPTEN__
#include "keyboard_web.h"
#endif
@@ -39,6 +40,7 @@ static const char *g_fallbacks[REAKTOR_LAYER_MAX];
static int g_fallback_n;
static const char *g_icon_dirs[REAKTOR_LAYER_MAX], *g_cli_icons[REAKTOR_LAYER_MAX];
static int g_inspect_flag;
static int g_motion_flag, g_contrast_flag, g_reduced, g_contrast, g_watch;
static SDL_IOStream *g_log;
static SDL_LogOutputFunction g_log_next;
static void *g_log_next_user;
@@ -286,6 +288,15 @@ take_flags(App *app, int argc, char **argv)
else if (v && SDL_strcmp(a, "--log") == 0) { log_open(v); i++; }
else if (SDL_strcmp(a, "--inspect") == 0) g_inspect_flag = 1;
else if (SDL_strcmp(a, "--no-inspector") == 0) g_inspect_flag = -1;
else if (SDL_strcmp(a, "--watch-css") == 0) g_watch = 1;
else if (v && SDL_strcmp(a, "--motion") == 0) {
g_motion_flag = SDL_strcmp(v, "reduce") == 0 ? 1 : SDL_strcmp(v, "full") == 0 ? -1 : 0;
i++;
}
else if (v && SDL_strcmp(a, "--contrast") == 0) {
g_contrast_flag = SDL_strcmp(v, "more") == 0 ? 1 : SDL_strcmp(v, "normal") == 0 ? -1 : 0;
i++;
}
else if (v && SDL_strcmp(a, "--renderer") == 0) { app->renderer_pref = v; i++; }
else if (v && SDL_strcmp(a, "--video") == 0) { app->video_pref = v; i++; }
else if (v && SDL_strcmp(a, "--popups") == 0) {
@@ -362,6 +373,102 @@ theme_sheet(int dark)
: "external/watercss/src/variables-light.css";
}
/* Whether the system's reduced motion or contrast changed what applies. */
static int
read_prefs(void)
{
int r = g_motion_flag ? g_motion_flag > 0 : reaktor_prefers_reduced_motion();
int c = g_contrast_flag ? g_contrast_flag > 0 : reaktor_prefers_contrast();
int changed = r != g_reduced || c != g_contrast;
g_reduced = r;
g_contrast = c;
reaktor_css_media(r, c);
reaktor_anim_reduce(r);
return changed;
}
int reaktor_reduced_motion(void) { return g_reduced; }
int reaktor_high_contrast(void) { return g_contrast; }
#define WATCH_MAX 64
static struct {
SDL_Mutex *lock;
SDL_TimerID timer;
char *path[WATCH_MAX];
SDL_Time when[WATCH_MAX];
int n;
} g_watched;
static void
watch_reload(App *app, void *user)
{
(void)app; (void)user;
SDL_Log("css: a sheet changed on disk; restyling");
reaktor_restyle();
}
static Uint32 SDLCALL
watch_tick(void *user, SDL_TimerID id, Uint32 interval)
{
int i, changed = 0;
(void)user; (void)id;
SDL_LockMutex(g_watched.lock);
for (i = 0; i < g_watched.n; i++) {
SDL_PathInfo info;
if (SDL_GetPathInfo(g_watched.path[i], &info) && info.modify_time != g_watched.when[i]) {
g_watched.when[i] = info.modify_time;
changed = 1;
}
}
SDL_UnlockMutex(g_watched.lock);
if (changed && g_app) reaktor_post(g_app, watch_reload, NULL);
return interval;
}
static void
watch_seen(const char *name)
{
char full[1024];
SDL_PathInfo info;
int i;
if (reaktor_path_absolute(name)) SDL_strlcpy(full, name, sizeof full);
else if (!reaktor_path(full, sizeof full, name)) return;
if (!SDL_GetPathInfo(full, &info) || info.type != SDL_PATHTYPE_FILE) return;
SDL_LockMutex(g_watched.lock);
for (i = 0; i < g_watched.n && SDL_strcmp(g_watched.path[i], full) != 0; i++) {}
if (i == g_watched.n && g_watched.n < WATCH_MAX && (g_watched.path[i] = SDL_strdup(full))) {
g_watched.when[i] = info.modify_time;
g_watched.n++;
}
SDL_UnlockMutex(g_watched.lock);
}
static void
watch_begin(void)
{
if (!g_watched.lock && !(g_watched.lock = SDL_CreateMutex())) return;
SDL_LockMutex(g_watched.lock);
while (g_watched.n) SDL_free(g_watched.path[--g_watched.n]);
SDL_UnlockMutex(g_watched.lock);
reaktor_css_track(watch_seen);
if (!g_watched.timer) g_watched.timer = SDL_AddTimer(500, watch_tick, NULL);
}
static void
watch_stop(void)
{
if (g_watched.timer) SDL_RemoveTimer(g_watched.timer);
g_watched.timer = 0;
while (g_watched.n) SDL_free(g_watched.path[--g_watched.n]);
SDL_DestroyMutex(g_watched.lock);
g_watched.lock = NULL;
}
static void
load_theme(App *app)
{
@@ -371,6 +478,8 @@ load_theme(App *app)
Uint64 t0, t1;
app->dark = effective_dark(app);
read_prefs();
if (g_watch) watch_begin();
sheets[n++] = theme_sheet(app->dark);
sheets[n++] = CORE_SHEET;
@@ -915,6 +1024,12 @@ reaktor_take_event(App *app, SDL_Event *event)
reaktor_hint(SDL_HINT_MAIN_CALLBACK_RATE, app->drag_rate);
break;
/* Windows and most desktops send nothing when reduced motion or
contrast changes; coming back from their settings is the cue. */
case SDL_EVENT_WINDOW_FOCUS_GAINED:
if (!app->secondary && read_prefs()) load_theme(app);
break;
case SDL_EVENT_WINDOW_FOCUS_LOST:
if (app->dragging) {
drag_end(app);
@@ -937,7 +1052,7 @@ reaktor_take_event(App *app, SDL_Event *event)
break;
case SDL_EVENT_SYSTEM_THEME_CHANGED:
if (app->theme_mode == REAKTOR_THEME_SYSTEM) load_theme(app);
if (app->theme_mode == REAKTOR_THEME_SYSTEM || read_prefs()) load_theme(app);
break;
case SDL_EVENT_LOCALE_CHANGED:
@@ -1448,6 +1563,7 @@ app_quit(void *appstate, SDL_AppResult result)
return;
}
reaktor_inspector_stop(app);
watch_stop();
run_stop(app);
if (remembers(app)) reaktor_window_remember(app);
reaktor_timers_drop(NULL);
+9 -1
View File
@@ -136,7 +136,11 @@ page_diagnostics(App *app, struct reaktor_context *ctx, showcase_state *st)
section(app, ctx, "Style and text",
"The stylesheets are parsed by LCUI's libcss on every theme "
"change, and the font atlas is baked per size at the display's "
"scale.");
"scale. The system's reduced motion and contrast reach the sheets as "
"@media queries, and with --watch-css a sheet saved on disk restyles "
"the window.");
api(app, ctx, "reaktor_reduced_motion / reaktor_high_contrast / "
"--motion, --contrast, --watch-css");
SDL_snprintf(v, sizeof(v), "%s", d.dark ? "dark" : "light");
DIAG_ROWS(ctx) {
@@ -147,6 +151,10 @@ page_diagnostics(App *app, struct reaktor_context *ctx, showcase_state *st)
SDL_snprintf(v, sizeof(v), "%.2fx", (double)d.scale);
diag_row(app, ctx, "display scale", v);
diag_row(app, ctx, "font", d.font);
diag_row(app, ctx, "reduced motion",
reaktor_reduced_motion() ? "on: animations jump to their end" : "off");
diag_row(app, ctx, "contrast",
reaktor_high_contrast() ? "more: @media (prefers-contrast: more)" : "no preference");
}
section(app, ctx, "Memory",
+31 -3
View File
@@ -28,16 +28,28 @@ static const char g_sheet[] =
"background-color: color-mix(in srgb, hsl(120, 100%, 50%), black); "
"border-top-color: color-mix(in srgb, #ff000080, rgba(0, 0, 255, 1)); }\n";
static const char g_media[] =
"@media (prefers-color-scheme: dark) { h1 { color: #000001; } }\n"
"@media (prefers-reduced-motion: reduce) { h2 { color: #000002; } }\n"
"@media (prefers-reduced-motion: no-preference) { h3 { color: #000003; } }\n"
"@media (prefers-contrast: more) { h4 { color: #000004; } }\n"
"@media screen and (prefers-contrast) { h5 { color: #000005; } }\n"
"@media print, (prefers-reduced-motion) { h6 { color: #000006; } }\n"
"@media (prefers-contrast: more) and (max-width: 720px) { ol { color: #000007; } }\n"
"@media print { ul { color: #000008; } }\n";
char *
reaktor_asset_load(const char *name, size_t *len)
{
char *out = NULL;
long n = 0;
if (strcmp(name, "test.css") == 0) {
n = (long)strlen(g_sheet);
if (strcmp(name, "test.css") == 0 || strcmp(name, "media.css") == 0) {
const char *s = name[0] == 't' ? g_sheet : g_media;
n = (long)strlen(s);
out = malloc((size_t)n + 1);
if (out) memcpy(out, g_sheet, (size_t)n + 1);
if (out) memcpy(out, s, (size_t)n + 1);
} else {
FILE *f = fopen(name, "rb");
@@ -81,6 +93,7 @@ int
main(void)
{
static const char *const test[1] = { "test.css" };
static const char *const media[1] = { "media.css" };
static const char *const simple[1] = { "external/simplecss/simple.css" };
char *out;
int i;
@@ -125,6 +138,21 @@ main(void)
free(out);
}
out = reaktor_css_flatten(media, 1, "light", 0, NULL);
ok("with no preference, only no-preference queries apply",
out && strstr(out, "#000003") && !strstr(out, "#000001") && !strstr(out, "#000002") &&
!strstr(out, "#000004") && !strstr(out, "#000005") && !strstr(out, "#000006"));
free(out);
reaktor_css_media(1, 1);
out = reaktor_css_flatten(media, 1, "dark", 0, NULL);
ok("reduced motion and more contrast apply theirs",
out && strstr(out, "#000001") && strstr(out, "#000002") && !strstr(out, "#000003") &&
strstr(out, "#000004") && strstr(out, "#000005") && strstr(out, "#000006"));
ok(" but not with a feature not answered here, nor print",
out && !strstr(out, "#000007") && !strstr(out, "#000008"));
free(out);
reaktor_css_media(0, 0);
for (i = 0; i < 2; i++) {
out = reaktor_css_flatten(simple, 1, i ? "dark" : "light", 0, NULL);
ok(i ? "Simple.css flattens, dark" : "Simple.css flattens, light",