Skip to content

refactor: theme controller becomes derived theme state and theme/ (@miodec) - #8610

Open
Miodec wants to merge 2 commits into
masterfrom
theme-controller-removal
Open

Miodec wants to merge 2 commits into
masterfrom
theme-controller-removal

Conversation

@Miodec

@Miodec Miodec commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

The theme controller dissolves: the applied theme is derived state, its behaviour lives in theme/.

 src/ts/
-├── controllers/theme-controller.ts   # imperative apply/set, preview + random bookkeeping
+├── theme/
+│   ├── index.ts      # API + effects: drop random/preview/draft on a configured change, first-paint cache, theme list refresh
+│   ├── preview.ts    # previewTheme (250ms debounce), clearThemePreview
+│   ├── random.ts     # randomizeTheme
+│   ├── indicator.ts  # getThemeIndicator memo (was a signal in states/core)
+│   └── state.ts      # signals + getTheme memo (was states/theme.ts)
 └── utils/colors.ts   # + custom colors <-> Theme conversions
getTheme =
  preview
  ?? random
  ?? (configured is custom ? editor draft : null)
  ?? configured            # autoSwitch tracks a prefers-color-scheme signal
  • The settings custom theme editor writes a draft signal (setCustomThemeDraft, updateCustomThemeDraftColor) instead of setTheme/updateThemeColor.
  • Engine lint: bans theme/, still allows theme/state.
  • Dead applyPreset deleted.

Intended behaviour changes:

  • Closing a theme preview (Esc) keeps unsaved custom theme edits. Before, it re-applied the saved colors.
  • The indicator follows custom theme renames right away.

Evidence

  • Before: custom theme on, picker bg → #ff0000 (unsaved), commandline preview a theme, Esc → bg back to the saved #323437.
    After: same steps → bg stays #ff0000, picker shows #ff0000, first-paint cache stays #323437.
  • Browser check: configured theme + cache, favorite star, preview + Esc, random in on/fav/light/dark/auto, editor (picker, load from preset, custom off/on), auto switch, no console errors, no first-paint flash on reload. A live OS scheme flip wasn't tested.
  • __tests__/theme/index.spec.ts: existing controller tests kept, plus preview precedence/debounce, draft only while custom, draft survives a cleared preview, draft dropped on save, previews and random themes never cached. Full frontend suite: 1917 passed.

Merge Danger

Door: two-way

Pure frontend refactor, revert restores the controller.

Blast Radius: theming

Every getTheme() consumer (charts, keymap, favicon, theme CSS) now reads a memo. Wrong precedence would show the wrong theme, e.g. random vs preview vs draft.

🤖 Generated with Claude Code

…iodec)

getTheme is a memo: preview, then random, then the custom theme editor's
draft (only while custom is on), then the configured theme. Auto switch
tracks a prefers-color-scheme signal; the indicator is a memo. Colour
conversions move to utils/colors; dead applyPreset deleted; the engine
lint ban covers theme/.

Behaviour: closing a theme preview keeps unsaved custom theme edits; the
indicator follows custom theme renames.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 10:41
@monkeytypegeorge monkeytypegeorge added frontend User interface or web stuff local dev Related to the build process or dependencies labels Nov 21, 2026
@github-actions github-actions Bot added the waiting for review Pull requests that require a review before continuing label Nov 21, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical active finding reports that the random theme picker can index themes with custom IDs and then access nextTheme.bg on an undefined value in frontend/src/ts/theme/random.ts (lines 56 and 78), which may cause a runtime crash.

1 open issue
What changed in this PR

Refactors frontend theming by removing the imperative theme controller and replacing it with derived theme state plus a dedicated theme/ module that owns preview/random/indicator behavior and related effects.

Changes:

  • Introduce src/ts/theme/* to centralize theme preview, randomization, indicator derivation, and config-driven side effects.
  • Rework states/theme into derived memos/signals with explicit precedence (preview > random > draft > configured).
  • Update UI/commandline call sites, lint rules, and tests to align with the new theme API and draft-based custom-theme editing.
File Description
frontend/​src/​ts/​utils/​colors.ts Adds helpers to convert between custom theme color shapes and the runtime Theme representation.
frontend/​src/​ts/​theme/​random.ts Implements random theme selection logic and refreshes the candidate theme list used for randomization.
frontend/​src/​ts/​theme/​preview.ts Introduces a debounced theme preview API and a clear-preview helper.
frontend/​src/​ts/​theme/​indicator.ts Derives the footer theme indicator (name/favorite) from theme/config state via a memoized accessor.
frontend/​src/​ts/​theme/​index.ts Provides the theme module entry point, exporting the API and wiring global effects like cache/preview/random/draft resets and list refresh.
frontend/​src/​ts/​states/​theme.ts Refactors theme state into derived memos and signals for configured theme, random theme, preview theme, and custom-theme drafts.
frontend/​src/​ts/​states/​core.ts Removes the prior theme-indicator signal from core state now that the indicator is derived elsewhere.
frontend/​src/​ts/​controllers/​theme-controller.ts Removes the old imperative theme controller implementation that has been superseded by derived state and theme/ modules.
frontend/​src/​ts/​components/​pages/​test/​TestPage.tsx Updates test page interactions to use the new theme randomization API.
frontend/​src/​ts/​components/​pages/​settings/​custom-setting/​Theme.tsx Switches the custom theme editor to draft-based editing and uses the new color conversion utilities.
frontend/​src/​ts/​components/​modals/​CommandlineModal.tsx Updates commandline modal behavior to clear theme previews via the new theme API.
frontend/​src/​ts/​components/​layout/​footer/​ThemeIndicator.tsx Switches the footer component to consume the derived theme indicator from theme/indicator.
frontend/​src/​ts/​commandline/​lists/​themes.ts Updates the themes command list to preview themes using the new preview API.
frontend/​src/​ts/​commandline/​lists/​custom-themes-list.ts Updates the custom-themes command list to preview custom themes through the new preview API.
frontend/​src/​ts/​commandline/​lists.ts Routes theme-related command actions (e.g., randomize) through the new theme/ API surface.
frontend/​.oxlintrc.json Adjusts frontend lint rules to account for the new theme/ module and enforce import boundaries.
frontend/​__tests__/​theme/​index.spec.ts Adds/updates unit tests covering derived theme precedence, preview debounce behavior, draft semantics, and caching rules.
frontend/​__tests__/​test/​words-wrapper-height.spec.ts Updates test setup/mocks to reference the new theme module instead of the removed controller.
frontend/​__tests__/​input/​after-input-words.spec.tsx Updates test setup/mocks to reference the new theme module instead of the removed controller.
frontend/​__tests__/​controllers/​theme-controller.spec.ts Removes controller-focused tests now that the controller has been deleted and behavior is covered elsewhere.
frontend/​__tests__/​components/​pages/​test/​WordsInput.spec.tsx Adjusts component tests to mock or import the new theme module entry points.
frontend/​__tests__/​commandline/​lists/​themes.spec.ts Updates commandline list tests to align with the new theme preview/random APIs.
frontend/​__tests__/​__harness__/​setup-test-page.ts Updates the test harness wiring/mocks to use the new src/ts/theme entry points.

Review effort: Balanced
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

Comment on lines +56 to +67
let randomTheme: ThemeIdentifier;
let nextTheme = null;
do {
randomTheme = themesList[randomThemeIndex] as ThemeIdentifier;
nextTheme = themes[themesList[randomThemeIndex] as ThemeName];
randomThemeIndex++;
if (randomThemeIndex >= themesList.length) {
Arrays.shuffle(themesList);
randomThemeIndex = 0;
}
} while (!filter(nextTheme.bg));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend User interface or web stuff local dev Related to the build process or dependencies waiting for review Pull requests that require a review before continuing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants