Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(app): scope quick CSS and theme overrides per account

Hampusa0c4106d730a
diff --git a/fluxer_app/src/features/accessibility/state/Accessibility.ts b/fluxer_app/src/features/accessibility/state/Accessibility.ts
index 0aae87c4f..2f0286e97 100644
--- a/fluxer_app/src/features/accessibility/state/Accessibility.ts
+++ b/fluxer_app/src/features/accessibility/state/Accessibility.ts
@@ -39,8 +39,8 @@ const ZOOM_KEYBOARD_STEP_PCT = 10;
export const ZOOM_LEVEL_MARKERS = [0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0] as const;
const ACCESSIBILITY_STORE_STORAGE_KEY = 'Accessibility';
const ACCESSIBILITY_ZOOM_STORAGE_KEY = 'Accessibility:zoomLevel';
-const ACCESSIBILITY_CUSTOM_THEME_STORAGE_KEY = 'Accessibility:customThemeCss';
-const ACCESSIBILITY_CUSTOM_THEME_SYNC_STORAGE_KEY = 'Accessibility:customThemeCssSyncAcrossDevices';
+export const ACCESSIBILITY_CUSTOM_THEME_STORAGE_KEY = 'Accessibility:customThemeCss';
+export const ACCESSIBILITY_CUSTOM_THEME_SYNC_STORAGE_KEY = 'Accessibility:customThemeCssSyncAcrossDevices';
const ACCESSIBILITY_MOTION_STORAGE_KEY = 'Accessibility:motion';
const ACCESSIBILITY_SHOW_NEKO_STORAGE_KEY = 'Accessibility:showNeko';
const ACCESSIBILITY_KEEP_NEKO_STILL_STORAGE_KEY = 'Accessibility:keepNekoStill';
@@ -69,10 +69,6 @@ interface LegacyAccessibilityStartupSettings {
enableSmoothScrolling?: boolean;
}
-interface AccessibilityStartupSettingsOptions {
- preserveLegacyZoomLevel?: boolean;
-}
-
interface StartupSettingsStorage {
getItem(key: string): string | null;
}
@@ -379,7 +375,7 @@ function persistLocalVideoSeekPreviewThumbnails(value: boolean): void {
} catch {}
}
-function readLocalCustomThemeCss(storage: StartupSettingsStorage = AppStorage): string | null {
+export function readLocalCustomThemeCss(storage: StartupSettingsStorage = AppStorage): string | null {
let raw: string | null;
try {
raw = storage.getItem(ACCESSIBILITY_CUSTOM_THEME_STORAGE_KEY);
@@ -471,7 +467,6 @@ function shouldPreserveLegacyZoomLevel(): boolean {
function readAccessibilityStartupSettings(
storage: StartupSettingsStorage = AppStorage,
- options: AccessibilityStartupSettingsOptions = {},
): AccessibilityStartupSettings | null {
const localZoomLevel = readPrebootZoomFactor() ?? readLocalZoomLevel(storage);
const localMotionSettings = readLocalMotionSettings(storage);
@@ -555,7 +550,7 @@ function readLocalCustomThemeCssSyncAcrossDevices(): boolean {
}
}
-function normalizeCustomThemeCss(css: string | null | undefined): string | null {
+export function normalizeCustomThemeCss(css: string | null | undefined): string | null {
if (typeof css !== 'string') {
return null;
}
@@ -797,7 +792,6 @@ class Accessibility {
this.initializeShowNekoSessionSync();
this.initializeShowNekoStorageSync();
this.initializeVideoSeekPreviewThumbnailsStorageSync();
- this.initializeCustomThemeCssStorageSync();
this.applyStartupPresentationSettings();
}
@@ -1062,8 +1056,8 @@ class Accessibility {
private initializeZoomStorageSync(): void {
this.unsubscribeZoomStorage = AppStorage.subscribe(
() => {
- const zoomLevel = readLocalZoomLevel() ?? 1.0;
- if (zoomLevel === this.zoomLevel) {
+ const zoomLevel = readLocalZoomLevel();
+ if (zoomLevel === null || zoomLevel === this.zoomLevel) {
return;
}
runInAction(() => {
@@ -1504,23 +1498,9 @@ class Accessibility {
await this.applyZoom(zoomLevel);
}
- private initializeCustomThemeCssStorageSync(): void {
- AppStorage.subscribe(
- (event) => {
- if (
- event.key !== null &&
- event.key !== ACCESSIBILITY_CUSTOM_THEME_STORAGE_KEY &&
- event.key !== ACCESSIBILITY_CUSTOM_THEME_SYNC_STORAGE_KEY
- ) {
- return;
- }
- runInAction(() => {
- this.customThemeCss = readLocalCustomThemeCss();
- this.customThemeCssSyncAcrossDevices = readLocalCustomThemeCssSyncAcrossDevices();
- });
- },
- {source: 'external'},
- );
+ loadCustomThemeCssFromStorage(): void {
+ this.customThemeCss = readLocalCustomThemeCss();
+ this.customThemeCssSyncAcrossDevices = readLocalCustomThemeCssSyncAcrossDevices();
}
private applyStartupPresentationSettings(): void {
diff --git a/fluxer_app/src/features/auth/flow/client_intro/AuthClientPreferencesStep.tsx b/fluxer_app/src/features/auth/flow/client_intro/AuthClientPreferencesStep.tsx
index 84960e2d3..d1a27b159 100644
--- a/fluxer_app/src/features/auth/flow/client_intro/AuthClientPreferencesStep.tsx
+++ b/fluxer_app/src/features/auth/flow/client_intro/AuthClientPreferencesStep.tsx
@@ -2,17 +2,18 @@
import styles from '@app/features/auth/flow/client_intro/AuthClientPreferencesStep.module.css';
import {CONTINUE_DESCRIPTOR, THEME_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
+import * as ThemeProfileCommands from '@app/features/theme/commands/ThemeProfileCommands';
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
-import Theme from '@app/features/theme/state/Theme';
+import ThemeProfile from '@app/features/theme/state/ThemeProfile';
+import {ThemeSelector} from '@app/features/theme_settings/components/ThemeSelector';
import {Button, ButtonVariant} from '@app/features/ui/button/Button';
-import {ThemeSelector} from '@app/features/user/components/modals/tabs/appearance_tab/theme/ThemeTabContent';
+import {ArrowRightIcon} from '@app/features/ui/icons/DirectionalIcons';
import {LanguageSelector} from '@app/features/user/components/modals/tabs/LanguageTab';
import * as LocaleUtils from '@app/features/user/utils/LocaleUtils';
import {flxElementClassName} from '@app/lib/react';
import type {ThemeType} from '@fluxer/constants/src/UserConstants';
import {msg} from '@lingui/core/macro';
import {useLingui} from '@lingui/react/macro';
-import {ArrowRightIcon} from '@phosphor-icons/react';
import {observer} from 'mobx-react-lite';
import type React from 'react';
import {useCallback} from 'react';
@@ -45,9 +46,9 @@ export const AuthClientPreferencesStep = observer(function AuthClientPreferences
}: AuthClientPreferencesStepProps) {
const {i18n} = useLingui();
const currentLocale = LocaleUtils.getCurrentOrDetectedLocale();
- const theme = Theme.themePreference;
+ const theme = ThemeProfile.themePreference;
const handleThemeChange = useCallback((nextTheme: ThemeType) => {
- Theme.setTheme(nextTheme);
+ ThemeProfileCommands.setSignedOutThemePreference(nextTheme);
}, []);
const themeLabel = i18n._(THEME_DESCRIPTOR);
return (
diff --git a/fluxer_app/src/features/user/components/modals/tabs/appearance_tab/theme/ThemeUtils.ts b/fluxer_app/src/features/user/components/modals/tabs/appearance_tab/theme/ThemeUtils.ts
index 463123003..4d29765f9 100644
--- a/fluxer_app/src/features/user/components/modals/tabs/appearance_tab/theme/ThemeUtils.ts
+++ b/fluxer_app/src/features/user/components/modals/tabs/appearance_tab/theme/ThemeUtils.ts
@@ -1,17 +1,28 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-import {escapeRegex} from '@app/features/messaging/utils/RegexUtils';
import type {ThemeLibraryTheme} from '@app/features/theme/state/ThemeLibrary';
+import {THEME_VARIABLE_NAMES} from '@app/features/theme/variables/ThemeVariableManifest';
import {getElectronAPI} from '@app/features/ui/utils/NativeUtils';
+const ROOT_BLOCK_PATTERN = /:root\s*\{([^{}]*)\}/g;
+const DECLARATION_PATTERN = /(--[a-zA-Z0-9_-]+)\s*:\s*([^;{}]+);/g;
+const THEME_VARIABLES: ReadonlySet<string> = new Set(THEME_VARIABLE_NAMES);
+
+function replaceThemeDeclarations(css: string, replace: (declaration: string, name: string) => string): string {
+ return css.replace(ROOT_BLOCK_PATTERN, (block: string, body: string) => {
+ const nextBody = body.replace(DECLARATION_PATTERN, (declaration: string, name: string) =>
+ THEME_VARIABLES.has(name) ? replace(declaration, name) : declaration,
+ );
+ return `${block.slice(0, block.length - body.length - 1)}${nextBody}}`;
+ });
+}
+
export function extractThemeVariableOverrides(css: string): Record<string, string> {
const overrides: Record<string, string> = {};
- const variablePattern = /--([a-zA-Z0-9_-]+)\s*:\s*([^;]+);/g;
- let match: RegExpExecArray | null;
- while ((match = variablePattern.exec(css)) !== null) {
- const variableName = `--${match[1] as string}`;
- const value = match[2] as string;
- overrides[variableName] = value.trim();
+ for (const block of css.matchAll(ROOT_BLOCK_PATTERN)) {
+ for (const [, name, value] of (block[1] ?? '').matchAll(DECLARATION_PATTERN)) {
+ if (name && value && THEME_VARIABLES.has(name)) overrides[name] = value.trim();
+ }
}
return overrides;
}
@@ -23,14 +34,24 @@ function removeEmptyRootBlocks(css: string): string {
.trim();
}
+export function removeThemeVariableOverrides(css: string): string {
+ return removeEmptyRootBlocks(replaceThemeDeclarations(css, () => ''));
+}
+
export function updateCssForVariable(css: string, variableName: string, newValue: string | null): string {
- const variableNamePattern = escapeRegex(variableName);
- const propertyPattern = new RegExp(`(--${variableNamePattern.replace(/^--/, '')}\\s*:[^;]*;)`);
if (newValue === null) {
- return removeEmptyRootBlocks(css.replace(propertyPattern, ''));
+ return removeEmptyRootBlocks(
+ replaceThemeDeclarations(css, (declaration, name) => (name === variableName ? '' : declaration)),
+ );
}
- if (propertyPattern.test(css)) {
- return css.replace(propertyPattern, `${variableName}: ${newValue};`);
+ let replaced = false;
+ const updated = replaceThemeDeclarations(css, (declaration, name) => {
+ if (replaced || name !== variableName) return declaration;
+ replaced = true;
+ return `${variableName}: ${newValue};`;
+ });
+ if (replaced) {
+ return updated;
}
const trimmedCss = removeEmptyRootBlocks(css);
const prefix = trimmedCss.length > 0 && !trimmedCss.endsWith('\n') ? '\n' : '';