| import { getItem, setItem, removeItem } from '@/lib/storage'; |
| import { DEFAULT_FONT_VALUE } from '@/config/ui-font-options'; |
| import type { Project } from '@/lib/electron'; |
| import type { ThemeMode } from '../types/ui-types'; |
|
|
| |
| export const THEME_STORAGE_KEY = 'automaker:theme'; |
| const FONT_SANS_STORAGE_KEY = 'automaker:font-sans'; |
| const FONT_MONO_STORAGE_KEY = 'automaker:font-mono'; |
|
|
| |
| |
| |
| |
| export function getStoredTheme(): ThemeMode | null { |
| const stored = getItem(THEME_STORAGE_KEY); |
| if (stored) return stored as ThemeMode; |
|
|
| |
| |
| |
| try { |
| const legacy = getItem('automaker-storage'); |
| if (!legacy) return null; |
| interface LegacyStorageFormat { |
| state?: { theme?: string }; |
| theme?: string; |
| } |
| const parsed = JSON.parse(legacy) as LegacyStorageFormat; |
| const theme = parsed.state?.theme ?? parsed.theme; |
| if (typeof theme === 'string' && theme.length > 0) { |
| return theme as ThemeMode; |
| } |
| } catch { |
| |
| } |
|
|
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function getEffectiveFont( |
| projectFont: string | undefined, |
| globalFont: string | null, |
| fontOptions: readonly { value: string; label: string }[] |
| ): string | null { |
| const isValidFont = (font: string | null | undefined): boolean => { |
| if (!font || font === DEFAULT_FONT_VALUE) return true; |
| return fontOptions.some((opt) => opt.value === font); |
| }; |
|
|
| if (projectFont) { |
| if (isValidFont(projectFont)) { |
| return projectFont === DEFAULT_FONT_VALUE ? null : projectFont; |
| } |
| |
| } |
| if (!isValidFont(globalFont)) return null; |
| return globalFont === DEFAULT_FONT_VALUE ? null : globalFont; |
| } |
|
|
| |
| |
| |
| |
| export function saveThemeToStorage(theme: ThemeMode): void { |
| setItem(THEME_STORAGE_KEY, theme); |
| } |
|
|
| |
| |
| |
| |
| export function getStoredFontSans(): string | null { |
| return getItem(FONT_SANS_STORAGE_KEY); |
| } |
|
|
| export function getStoredFontMono(): string | null { |
| return getItem(FONT_MONO_STORAGE_KEY); |
| } |
|
|
| |
| |
| |
| |
| export function saveFontSansToStorage(fontFamily: string | null): void { |
| if (fontFamily) { |
| setItem(FONT_SANS_STORAGE_KEY, fontFamily); |
| } else { |
| |
| removeItem(FONT_SANS_STORAGE_KEY); |
| } |
| } |
|
|
| export function saveFontMonoToStorage(fontFamily: string | null): void { |
| if (fontFamily) { |
| setItem(FONT_MONO_STORAGE_KEY, fontFamily); |
| } else { |
| |
| removeItem(FONT_MONO_STORAGE_KEY); |
| } |
| } |
|
|
| export function persistEffectiveThemeForProject( |
| project: Project | null, |
| fallbackTheme: ThemeMode |
| ): void { |
| const projectTheme = project?.theme as ThemeMode | undefined; |
| const themeToStore = projectTheme ?? fallbackTheme; |
| saveThemeToStorage(themeToStore); |
| } |
|
|