Skip to content

Commit 4998a4c

Browse files
committed
fix: recalculate default theme based on native theme updates
1 parent f4ce3a1 commit 4998a4c

1 file changed

Lines changed: 89 additions & 35 deletions

File tree

src/config.js

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,69 @@ const DEFAULT_SEARCH_PROVIDER = 'https://noai.duckduckgo.com/?ia=web&q=%s'
3232
const DEFAULT_CONFIG_FILE_NAME = '.agregorerc'
3333
export const MAIN_RC_FILE = join(os.homedir(), DEFAULT_CONFIG_FILE_NAME)
3434

35-
let DEFAULT_BACKGROUND = 'var(--ag-color-black)'
36-
let DEFAULT_TEXT = 'var(--ag-color-white)'
37-
let DEFAULT_PAGE_THEME = 'var(--ag-color-black)'
3835
const DEFAULT_BORDER_RADIUS = '0.25em'
3936
const DEFAULT_BORDER_COLOR = 'var(--ag-theme-secondary)'
4037

41-
const initialRawConfig = RC('agregore', {
42-
theme: { themeSource: 'system' }
43-
}
44-
)
45-
46-
// Apply the themeSource from config on startup
47-
nativeTheme.themeSource = initialRawConfig.theme.themeSource
48-
49-
const { shouldUseDarkColors } = nativeTheme
50-
51-
if (shouldUseDarkColors === false) {
52-
DEFAULT_BACKGROUND = 'var(--ag-color-white)'
53-
DEFAULT_TEXT = 'var(--ag-color-black)'
54-
DEFAULT_PAGE_THEME = 'var(--ag-color-white)'
55-
56-
if (isMac) {
57-
DEFAULT_BACKGROUND = '#F5F5F7'
58-
DEFAULT_TEXT = '#1D1D1F'
59-
}
60-
if (isWindows) {
61-
DEFAULT_BACKGROUND = '#F3F3F3'
62-
DEFAULT_TEXT = '#1a1a1a'
38+
/**
39+
* Calculate the default theme values based on the current system theme and platform
40+
* @param {boolean} shouldUseDarkColors
41+
* @returns {{background: string, text: string, page: string}}
42+
*/
43+
export function calculateDefaultTheme (shouldUseDarkColors) {
44+
let DEFAULT_BACKGROUND = 'var(--ag-color-black)'
45+
let DEFAULT_TEXT = 'var(--ag-color-white)'
46+
let DEFAULT_PAGE_THEME = 'var(--ag-color-black)'
47+
48+
if (shouldUseDarkColors === false) {
49+
DEFAULT_BACKGROUND = 'var(--ag-color-white)'
50+
DEFAULT_TEXT = 'var(--ag-color-black)'
51+
DEFAULT_PAGE_THEME = 'var(--ag-color-white)'
52+
53+
if (isMac) {
54+
DEFAULT_BACKGROUND = '#F5F5F7'
55+
DEFAULT_TEXT = '#1D1D1F'
56+
}
57+
if (isWindows) {
58+
DEFAULT_BACKGROUND = '#F3F3F3'
59+
DEFAULT_TEXT = '#1a1a1a'
60+
}
61+
} else {
62+
if (isMac) {
63+
DEFAULT_BACKGROUND = '#2C2C2E'
64+
}
65+
if (isWindows) {
66+
DEFAULT_BACKGROUND = '#202020'
67+
}
6368
}
64-
} else {
65-
if (isMac) {
66-
DEFAULT_BACKGROUND = '#2C2C2E'
69+
70+
if (isMac || isWindows) {
71+
DEFAULT_PAGE_THEME = 'none'
6772
}
68-
if (isWindows) {
69-
DEFAULT_BACKGROUND = '#202020'
73+
74+
return {
75+
background: DEFAULT_BACKGROUND,
76+
text: DEFAULT_TEXT,
77+
page: DEFAULT_PAGE_THEME
7078
}
7179
}
7280

73-
if (isMac || isWindows) {
74-
DEFAULT_PAGE_THEME = 'none'
81+
const initialRawConfig = RC('agregore', {
82+
theme: { themeSource: 'system' }
83+
})
84+
85+
// Apply the themeSource from config on startup
86+
if (initialRawConfig.theme.themeSource) {
87+
nativeTheme.themeSource = initialRawConfig.theme.themeSource
7588
}
7689

90+
// Listen for system theme changes when themeSource is 'system'
91+
nativeTheme.on('updated', () => {
92+
console.log("native theme updated")
93+
applyDefaultThemeIfNeeded()
94+
})
95+
96+
const defaultThemeValues = calculateDefaultTheme(nativeTheme.shouldUseDarkColors)
97+
7798
const Config = RC('agregore', {
7899
llm: {
79100
enabled: true,
@@ -107,9 +128,9 @@ const Config = RC('agregore', {
107128

108129
theme: {
109130
'font-family': 'system-ui',
110-
background: DEFAULT_BACKGROUND,
111-
text: DEFAULT_TEXT,
112-
page: DEFAULT_PAGE_THEME,
131+
background: defaultThemeValues.background,
132+
text: defaultThemeValues.text,
133+
page: defaultThemeValues.page,
113134
primary: 'var(--ag-color-purple)',
114135
secondary: 'var(--ag-color-green)',
115136
indent: '16px',
@@ -262,6 +283,39 @@ function getFrom (path, object) {
262283
}
263284
}
264285

286+
/**
287+
* Check if the current theme is using the default values for the given shouldUseDarkColors
288+
* @param {boolean} shouldUseDarkColors
289+
* @returns {boolean}
290+
*/
291+
function isUsingOldDefaultTheme (shouldUseDarkColors) {
292+
const defaultTheme = calculateDefaultTheme(shouldUseDarkColors)
293+
const currentTheme = Config.theme
294+
return (
295+
currentTheme.background === defaultTheme.background &&
296+
currentTheme.text === defaultTheme.text &&
297+
currentTheme.page === defaultTheme.page
298+
)
299+
}
300+
301+
/**
302+
* Apply the default theme for the current system theme if using defaults
303+
*/
304+
function applyDefaultThemeIfNeeded () {
305+
const {shouldUseDarkColors} = nativeTheme
306+
const shouldChange = isUsingOldDefaultTheme(!shouldUseDarkColors)
307+
console.log({shouldChange, shouldUseDarkColors})
308+
if (shouldChange) {
309+
const newDefaultTheme = calculateDefaultTheme(shouldUseDarkColors)
310+
Config.theme.background = newDefaultTheme.background
311+
Config.theme.text = newDefaultTheme.text
312+
Config.theme.page = newDefaultTheme.page
313+
onChange({
314+
theme: newDefaultTheme
315+
})
316+
}
317+
}
318+
265319
/**
266320
* @param {(config: ConfigData) => void} newOnChange
267321
*/

0 commit comments

Comments
 (0)