diff --git a/ts/components/leftpane/ActionsPanel.tsx b/ts/components/leftpane/ActionsPanel.tsx index 0b72c06e3..ccc7777e7 100644 --- a/ts/components/leftpane/ActionsPanel.tsx +++ b/ts/components/leftpane/ActionsPanel.tsx @@ -44,7 +44,6 @@ import { } from '../../session/apis/snode_api/snodePool'; import { ConfigurationSync } from '../../session/utils/job_runners/jobs/ConfigurationSyncJob'; import { isDarkTheme } from '../../state/selectors/theme'; -import { ensureThemeConsistency } from '../../themes/SessionTheme'; import { switchThemeTo } from '../../themes/switchTheme'; import { ReleasedFeatures } from '../../util/releaseFeature'; import { getOppositeTheme } from '../../util/theme'; @@ -149,29 +148,6 @@ const cleanUpMediasInterval = DURATION.MINUTES * 60; // * if there is a version on the fileserver more recent than our current, we fetch github to get the UpdateInfos and trigger an update as usual (asking user via dialog) const fetchReleaseFromFileServerInterval = 1000 * 60; // try to fetch the latest release from the fileserver every minute -const setupTheme = async () => { - const shouldFollowSystemTheme = window.getSettingValue(SettingsKey.hasFollowSystemThemeEnabled); - const theme = window.Events.getThemeSetting(); - const themeConfig = { - theme, - mainWindow: true, - usePrimaryColor: true, - dispatch: window?.inboxStore?.dispatch || undefined, - }; - - if (shouldFollowSystemTheme) { - // Check if system theme matches currently set theme, if not switch it and return true, if matching return false - const wasThemeSwitched = await ensureThemeConsistency(); - if (!wasThemeSwitched) { - // if theme wasn't switched them set theme to default - await switchThemeTo(themeConfig); - } - return; - } - - await switchThemeTo(themeConfig); -}; - // Do this only if we created a new account id, or if we already received the initial configuration message const triggerSyncIfNeeded = async () => { const us = UserUtils.getOurPubKeyStrFromCache(); @@ -199,7 +175,6 @@ const triggerAvatarReUploadIfNeeded = async () => { * This function is called only once: on app startup with a logged in user */ const doAppStartUp = async () => { - void setupTheme(); // this generates the key to encrypt attachments locally await Data.generateAttachmentKeyIfEmpty(); diff --git a/ts/themes/SessionTheme.tsx b/ts/themes/SessionTheme.tsx index ef0694aff..acd77ab42 100644 --- a/ts/themes/SessionTheme.tsx +++ b/ts/themes/SessionTheme.tsx @@ -1,27 +1,12 @@ import { ipcRenderer } from 'electron'; import { ReactNode } from 'react'; -import { createGlobalStyle } from 'styled-components'; +import useMount from 'react-use/lib/useMount'; +import { SettingsKey } from '../data/settings-key'; import { getOppositeTheme, isThemeMismatched } from '../util/theme'; -import { classicDark } from './classicDark'; -import { THEME_GLOBALS, declareCSSVariables } from './globals'; +import { THEME_GLOBALS, setThemeValues } from './globals'; import { switchThemeTo } from './switchTheme'; -// Defaults to Classic Dark theme -const SessionGlobalStyles = createGlobalStyle` - html { - ${declareCSSVariables(THEME_GLOBALS)} - ${declareCSSVariables(classicDark)} - }; -`; - -export const SessionTheme = ({ children }: { children: ReactNode }) => ( - <> - - {children} - -); - export async function ensureThemeConsistency(): Promise { const theme = window.Events.getThemeSetting(); @@ -44,3 +29,33 @@ export async function ensureThemeConsistency(): Promise { }); }); } + +const setupTheme = async () => { + const shouldFollowSystemTheme = window.getSettingValue(SettingsKey.hasFollowSystemThemeEnabled); + const theme = window.Events.getThemeSetting(); + const themeConfig = { + theme, + mainWindow: true, + usePrimaryColor: true, + dispatch: window?.inboxStore?.dispatch || undefined, + }; + + if (shouldFollowSystemTheme) { + // Check if system theme matches currently set theme, if not switch it and return true, if matching return false + const wasThemeSwitched = await ensureThemeConsistency(); + if (!wasThemeSwitched) { + // if theme wasn't switched them set theme to default + await switchThemeTo(themeConfig); + } + } else { + await switchThemeTo(themeConfig); + } +}; + +export const SessionTheme = ({ children }: { children: ReactNode }) => { + useMount(() => { + setThemeValues(THEME_GLOBALS); + void setupTheme(); + }); + return children; +}; diff --git a/ts/themes/globals.tsx b/ts/themes/globals.tsx index 1bba2c177..c1502505f 100644 --- a/ts/themes/globals.tsx +++ b/ts/themes/globals.tsx @@ -179,18 +179,15 @@ export const THEME_GLOBALS: ThemeGlobals = { '--right-panel-duration': setDuration('0.3s'), }; -// These should only be needed for the global style (at root). -export function declareCSSVariables(variables: Record) { - let output = ''; - // eslint-disable-next-line no-restricted-syntax - for (const [key, value] of Object.entries(variables)) { - output += `${key}: ${value};\n`; - } - - return output; -} - type ThemeKeys = ThemeGlobals & ThemeColorVariables; + export function getThemeValue(key: keyof ThemeKeys) { return getComputedStyle(document.documentElement).getPropertyValue(key); } + +export function setThemeValues(variables: ThemeGlobals | ThemeColorVariables) { + // eslint-disable-next-line no-restricted-syntax + for (const [key, value] of Object.entries(variables)) { + document.documentElement.style.setProperty(key, value); + } +} diff --git a/ts/themes/switchTheme.tsx b/ts/themes/switchTheme.tsx index 41423d8db..f82c2b3ef 100644 --- a/ts/themes/switchTheme.tsx +++ b/ts/themes/switchTheme.tsx @@ -1,8 +1,8 @@ import { Dispatch } from '@reduxjs/toolkit'; -import { applyTheme } from '../state/ducks/theme'; import { classicDark, classicLight, oceanDark, oceanLight } from '.'; -import { convertThemeStateToName, THEMES, ThemeStateType } from './constants/colors'; -import { loadThemeColors } from './variableColors'; +import { applyTheme } from '../state/ducks/theme'; +import { THEMES, ThemeStateType, convertThemeStateToName } from './constants/colors'; +import { setThemeValues } from './globals'; import { findPrimaryColorId, switchPrimaryColorTo } from './switchPrimaryColor'; type SwitchThemeProps = { @@ -18,19 +18,19 @@ export async function switchThemeTo(props: SwitchThemeProps) { switch (theme) { case 'classic-dark': - loadThemeColors(classicDark); + setThemeValues(classicDark); newTheme = 'classic-dark'; break; case 'classic-light': - loadThemeColors(classicLight); + setThemeValues(classicLight); newTheme = 'classic-light'; break; case 'ocean-dark': - loadThemeColors(oceanDark); + setThemeValues(oceanDark); newTheme = 'ocean-dark'; break; case 'ocean-light': - loadThemeColors(oceanLight); + setThemeValues(oceanLight); newTheme = 'ocean-light'; break; default: diff --git a/ts/themes/variableColors.tsx b/ts/themes/variableColors.tsx index e28f11cee..1f772316a 100644 --- a/ts/themes/variableColors.tsx +++ b/ts/themes/variableColors.tsx @@ -205,10 +205,3 @@ export type ThemeColorVariables = { '--file-dropzone-background-color': string; '--file-dropzone-border-color': string; }; - -export function loadThemeColors(variables: ThemeColorVariables) { - // eslint-disable-next-line no-restricted-syntax - for (const [key, value] of Object.entries(variables)) { - document.documentElement.style.setProperty(key, value); - } -}