Skip to content

feat: add Light/Dark/System theme support with title-bar synchronization (#513) - #547

Closed
carvalab wants to merge 1 commit into
FreeOpenSourcePOS:mainfrom
carvalab:feat/513-dark-theme-e2e
Closed

carvalab wants to merge 1 commit into
FreeOpenSourcePOS:mainfrom
carvalab:feat/513-dark-theme-e2e

Conversation

@carvalab

Copy link
Copy Markdown
Contributor

Summary

Implements #513 — end-to-end Light/Dark/System theme support across the renderer and Electron main process, with no new dependencies.

  • Settings & persistence: theme_mode (light | dark | system, default system) registered in both settings allowlists (ALLOWED_IPC_KEYS, ALLOWED_WILDCARD_KEYS) and value-validated in both write paths before persist. No migration, no seeding; absent/invalid resolves to system.
  • Renderer theme engine: new useThemeMode store + ThemeSync client component (HtmlLangSync pattern) — resolves system via matchMedia, toggles .dark on <html>, mirrors the resolved palette to localStorage, and pushes the effective theme to main. Follower windows (KDS/popup/browser) boot via ?theme= param → mirror → authenticated settings fetch → OS; mirror writes are ownership-gated so OS guesses never clobber the owner's value.
  • FOUC guard: blocking pre-paint script in the root layout (param → mirror → prefers-color-scheme, try/catch-wrapped); precedence matrix covered by a new vm-based test suite.
  • Native chrome sync: new narrow set-theme-effective IPC (sender-validated) → applyTitleBarOverlayTheme + setBackgroundColor on the main window; createWindow() reads the persisted theme via direct getDatabase() so initial overlay colors are correct on every platform (crash-recovery re-creation included). Windows/macOS re-color live; Linux picks up a theme change on next launch (documented platform contract); KDS windows get ?theme= at open.
  • Settings UI: new Appearance tab (ownerManager-gated, consistent with the wildcard PUT gate) with a Light/Dark/System segmented control — optimistic update, rollback + existing toast pattern on failure; labels in all four locales.
  • Docs: docs/title-bar-phase1.md dark-theme deferral updated; appended "Dark mode ([Feature] Implement End-to-End Dark Theme Support and Title Bar Synchronization #513)" section to docs/title-bar-platform-matrix.md.

Test Plan

  • npm run lint (0 errors), npm run build, npm run i18n:check
  • npm run test:titlebar-window-options — extended: theme-mode helpers (resolve/validate/URL-param rewrite)
  • npm run test:electron-api-contract — setThemeEffective typed on both sides
  • npm run test:theme-mode-settings (new) — theme_mode write-path validation, negative + persistence, both IPC and HTTP
  • npm run test:theme-fouc-script (new) — FOUC precedence matrix (10 cases) run against the extracted production script
  • npm run test:e2e:electron (new theme.electron.spec.ts) — Dark toggle → .dark + getStatus().effectiveTheme; System mode driven by real nativeTheme.themeSource overrides; full relaunch persistence (SQLite round-trip); evidence screenshots via FLO_E2E_EVIDENCE_DIR
  • Browser visual sweep in both themes (login/setup/POS/dashboard/orders/settings/KDS-standalone/customer-display); hardcoded-light surfaces tokenized; light mode pixel-stable (before/after pairs captured)
  • Windows/macOS live overlay re-color (code + unit verified; per-platform manual rows added to the matrix doc)

Offline-first

Theme resolution and persistence use only local SQLite + localStorage; zero network calls in the theme path.

@greptile-apps

greptile-apps Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds persisted Light, Dark, and System theme modes, synchronizes renderer and native Electron chrome, and introduces follower-window hydration and recovery behavior.

  • Adds renderer theme state, pre-paint initialization, settings controls, and localized labels.
  • Adds validated persistence and Electron IPC for effective title-bar colors.
  • Adds unit and Electron end-to-end coverage for theme resolution, persistence, and native synchronization.

Confidence Score: 0/5

The PR does not appear safe to merge because follower reconciliation and settings recovery can still leave users on stale themes or overwrite a newer persisted selection.

Authenticated followers can permanently retain stale mirrored state after bounded recovery ends, mirror authority still leaks across authentication reboots, and the settings recovery sequence can miss hydration, retain a stale rollback baseline, or override a later successful save.

Files Needing Attention: frontend/src/components/layout/ThemeSync.tsx; frontend/src/app/(dashboard)/settings/page.tsx

Important Files Changed

Filename Overview
frontend/src/components/layout/ThemeSync.tsx Adds theme boot resolution, mirror ownership, authentication reboot, and bounded follower reconciliation; previously reported authority and recovery-lifetime failures remain.
frontend/src/app/(dashboard)/settings/page.tsx Adds the Appearance controls and optimistic persistence, but previously reported recovery-arm and rollback-baseline races remain.
frontend/src/store/theme.ts Introduces the shared theme-mode store and a persistent in-window user-selection guard.
main/ipc.ts Adds validated theme settings support and the narrow effective-theme IPC path.
main/title-bar-theme.ts Adds helpers for validating and resolving theme mode and applying native title-bar colors.
frontend/src/app/layout.tsx Adds the blocking pre-paint theme resolver and mounts renderer synchronization.

Sequence Diagram

sequenceDiagram
  participant User
  participant Settings
  participant Store
  participant API
  participant ThemeSync
  participant Electron
  User->>Settings: Select theme
  Settings->>Store: Optimistic mode update
  Settings->>API: Persist theme_mode
  Store->>ThemeSync: Mode changed
  ThemeSync->>Electron: setThemeEffective(isDark)
  API-->>Settings: Success or recovery path
Loading

Reviews (11): Last reviewed commit: "feat: add Light/Dark/System theme suppor..." | Re-trigger Greptile

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch 2 times, most recently from d093cf4 to 126cb4f Compare August 30, 2026 18:49
@carvalab

Copy link
Copy Markdown
Contributor Author

Thanks @greptile-apps — both P1s are fixed in 126cb4f (branch squashed to a single commit):

P1-1 (late hydration overwrites user choice): the theme store now carries a per-window-session userSelected flag; saveThemeMode sets it before the optimistic flip, and every boot-time setMode in ThemeSync (owner getSettings() and follower branches) is gated on it — a late settings response can no longer revert an in-window choice. Locked by source tripwires in tests/theme-fouc-script.test.ts.

P1-2 (stale mirror bypasses persisted theme): follower chain reordered to DB → param → mirror → OS. The token-gated api.get('/settings/theme_mode') now runs before the mirror read whenever a POS token exists, so a same-origin popup can no longer pin a stale mirror value; the mirror remains the logged-out fallback (spec §8(b)). KDS behavior unchanged (no token → param wins).

Also aligned two stale test expectations that CI caught: tests/kds-window-hardening.test.ts (KDS URL now carries the theme param) and frontend/e2e/title-bar-platform.spec.ts (renderer-API key list includes setThemeEffective).

Verified: test:theme-fouc-script (10/10 + 5 new tripwires), test:kds-window-hardening, native Electron e2e (5 passed), i18n:check, lint (0 errors), npm run build.

@greptile-apps

greptile-apps Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Re-checked 126cb4f: both P1 findings are addressed.

  • ThemeSync.tsx now checks the session-level userSelected flag before applying either owner or follower hydration, while saveThemeMode sets that flag before the optimistic update.
  • Follower hydration now attempts the token-gated settings read before consulting the localStorage mirror, and resolves sources in the documented DB → URL param → mirror order.

The added tripwires cover both ordering/race invariants, and the reported build, lint, i18n, and Electron tests passed. I have no further blocking findings.

Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch 2 times, most recently from 06e01c8 to 1456df8 Compare August 30, 2026 19:32
@carvalab

Copy link
Copy Markdown
Contributor Author

Thanks @greptile-apps — both P1s from the second pass are fixed (1456df8, branch squashed):

P1 (settings rollback uses unhydrated baseline): on a failed PUT, the page now restores from server truth — it re-fetches /settings/theme_mode and applies whatever the DB holds (falling back to the previous local value only if that read also fails). Additionally a saveSeq ordering guard keeps the hydration response from clobbering a raced save's baseline. The wrong-baseline case self-heals instead of being blocked by the userTouched/userSelected guards.

P1 (failed fetch preserves stale mirror): the follower settings fetch now retries once after 1s (cancelled-safe), and the mirror fallback on a fetch-failed path no longer marks the value authoritative — the stale palette is painted best-effort but never written back to the mirror. The no-fetch path (logged-out popups, spec §8(b)) keeps its existing authoritative mirror behavior.

Both invariants are source-locked by new tripwires in tests/theme-fouc-script.test.ts. Verified: test:theme-fouc-script (all matrix + tripwires), i18n:check, native Electron e2e 5 passed (toggle/system/relaunch through the changed code), npm run build, lint 0 errors.

@greptile-apps

greptile-apps Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Re-checked commit 1456df8: both remaining P1s are addressed.

  • Settings rollback now re-reads the persisted value after a failed PUT, updates both the store and lastCommitted, and uses saveSeq to prevent a raced hydration response from overwriting the baseline.
  • Follower hydration now retries the authenticated settings read once, and a mirror used after a failed fetch is paint-only rather than authoritative, so it cannot be written back as stale state.

The new tripwires cover these invariants, and the reported theme FOUC, build, i18n, lint, and Electron checks passed. I have no further blocking findings.

@carvalab

Copy link
Copy Markdown
Contributor Author

Thanks @greptile-apps — both round-3 P1s fixed (branch squashed):

Follower reconciliation stops permanently: an authed follower whose startup fetch failed now schedules a single one-shot re-check after 60s that re-runs the bounded fetch; on recovery it applies the DB value, marks authoritative, and corrects the mirror. No recurring polling — reconciliation beyond that stays on next-open per the spec's §12 boundary for short-lived surfaces.

Rollback retains unhydrated baseline: a failed PUT and failed recovery GET now arm a needsServerTruth flag; when the initial hydration response finally lands, the server value is applied to the store and baseline even though userTouched is set — the page no longer stays stranded on System.

Both invariants are source-locked by new tripwires in tests/theme-fouc-script.test.ts. Verified: test:theme-fouc-script (all matrix + tripwires), i18n:check, native Electron e2e 5 passed, npm run build, lint 0 errors, all other suites green.

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch from 1456df8 to 56287fa Compare August 30, 2026 19:43
@greptile-apps

greptile-apps Bot commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Re-checked the latest squashed head and the round-3 fixes. Both reported P1s are addressed:

  • ThemeSync.tsx now performs a bounded startup fetch and schedules one cancellable 60-second re-check for authenticated followers after definitive failure; a recovered response updates the mode and authoritative mirror.
  • settings/page.tsx now arms needsServerTruth when both PUT and recovery GET fail, allowing the eventual hydration response to update the store and baseline despite userTouched.

The source tripwires cover both invariants, and the reported FOUC, build, i18n, lint, and Electron checks are green. I have no further blocking findings.

@khaira777 khaira777 linked an issue Aug 30, 2026 that may be closed by this pull request
7 tasks
@khaira777 khaira777 added the enhancement New feature or request label Aug 30, 2026
@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch 2 times, most recently from 32e0fa8 to 3698c94 Compare August 31, 2026 00:10
@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch from c573bea to 65c59f0 Compare August 31, 2026 00:28
@carvalab carvalab closed this Aug 31, 2026
@carvalab carvalab reopened this Aug 31, 2026
@greptile-apps

greptile-apps Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch from 65c59f0 to a768571 Compare August 31, 2026 00:39
@carvalab carvalab closed this Aug 31, 2026
@carvalab carvalab reopened this Aug 31, 2026
@khaira777

khaira777 commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

@carvalab if you resolve the conflicts and push the changes CI will trigger.

Let me know if you want me to take a look at anything.

@carvalab

Copy link
Copy Markdown
Contributor Author

@carvalab if you resolve the conflicts and push the changes CI will trigger.

Let me know if you want me to take a look at anything.

Hey there, @khaira777 Yes, there are quite a few changes happening, but I'm carefully checking to make sure nothing breaks and am running some end-to-end tests. Having a fresh pair of eyes would be wonderful!

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch 4 times, most recently from bbe5cba to dd405e5 Compare August 31, 2026 04:03
@carvalab

Copy link
Copy Markdown
Contributor Author

CI is green except one flaky upstream test. Fixed it in this push: the reports-insights fixtures were pinned to June dates and quietly fell out of the 90-day query window on Aug 31 (which is why it started failing for everyone). The fixtures now anchor to the current week, so the test is stable no matter when it runs. Also cleaned up a couple of stale test expectations from the rebase while I was in there.

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch 3 times, most recently from cb916f5 to 3dae2f4 Compare August 31, 2026 05:14
@khaira777

Copy link
Copy Markdown
Contributor

@carvalab if it works on your end and you have review/tested, you can go ahead and merge. We are out of greptile reviews for 2 days.

@carvalab
carvalab force-pushed the feat/513-dark-theme-e2e branch from 3dae2f4 to d148bc3 Compare August 31, 2026 12:01
…ion (FreeOpenSourcePOS#513)

- theme_mode setting (light|dark|system, default system) validated in both
  write paths (IPC set-setting + HTTP wildcard PUT); no migration, no seeding
- useThemeMode store + ThemeSync component: matchMedia resolution, .dark
  toggle, ownership-gated localStorage mirror, token-gated DB seed for
  follower windows (DB > param > mirror > OS, bounded retry with real delay
  + one-shot 60s re-check after transient failure), in-window user-choice
  guard, server-truth rollback with needsServerTruth hydration delivery,
  post-login re-hydration via flo:auth-changed, set-theme-effective push
- FOUC pre-paint guard (param > mirror > prefers-color-scheme) with vm-based
  precedence matrix test
- main-process wiring: direct getDatabase() initial isDark at window
  creation, sender-validated set-theme-effective handler (overlay +
  background color, main window only), effectiveTheme on get-status,
  ?theme= on the KDS window URL
- Appearance settings tab (ownerManager-gated, optimistic + rollback, 5 locales
  incl. French from FreeOpenSourcePOS#549)
- native Electron e2e: dark toggle, nativeTheme.themeSource system sync,
  relaunch persistence; theme-mode-settings + theme-fouc-script suites;
  KDS-hardening and renderer-API expectations aligned
- full dark-mode coverage: Settings page and panels, products, customers,
  tables, staff, addon-groups, POS modals, KDS components, dashboard —
  light mode pixel-stable; docs amended (title-bar-phase1, platform matrix)

Review fixes (greptile): in-window user-choice guard; DB fetch before
mirror; bounded retry with no stale mirror write-back; server-truth
rollback; one-shot follower re-check after transient outage; hydration
delivers server truth when a failed save armed it.

Ponytail cleanup: shared fetchThemeMode helper (boot + recheck dedup),
stdlib asserts in theme-mode-settings, trimmed VM stubs, optional relaunch
on relaunched harness, dead refs/conditions/comments removed (-144 lines).

Rebase follow-ups (FreeOpenSourcePOS#537/FreeOpenSourcePOS#549/FreeOpenSourcePOS#546): tokenize weighted-products/refund/
period-toggle surfaces; fr.json theme-key parity; auth-changed dispatch
guarded for non-browser envs (auth-ui-deterministic); themed order-card
selector in quick-customer-creation e2e; dashboard KPI values kept dark
on pastel tiles; reports-insights fixtures made date-independent (they
aged out of the 90-day window on Aug 31); comment hygiene (~200 lines of
review-narration stripped); test:refunds restored to the test chain;
allowScripts field declared for local npm install scripts.
@carvalab

Copy link
Copy Markdown
Contributor Author

Superseded by #563 (same commit, reopened from an in-repo branch so CI runs without the fork workflow-approval gate).

@carvalab carvalab closed this Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Implement End-to-End Dark Theme Support and Title Bar Synchronization

2 participants