Skip to content

Commit 45a2154

Browse files
committed
feat: follow system theme by default
1 parent 1fdd193 commit 45a2154

18 files changed

Lines changed: 156 additions & 25 deletions

File tree

src-tauri/src/storage/config.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Default for AppConfig {
7070
write_mode: "append".to_string(),
7171
history_limit: 50,
7272
locale: None,
73-
theme: "light".to_string(),
73+
theme: "system".to_string(),
7474
choice_mode: 2,
7575
show_title_on_tray: false,
7676
hide_at_launch: false,
@@ -95,6 +95,12 @@ impl Default for AppConfig {
9595
}
9696

9797
impl AppConfig {
98+
fn normalize(&mut self) {
99+
if !matches!(self.theme.as_str(), "light" | "dark" | "system") {
100+
self.theme = "system".to_string();
101+
}
102+
}
103+
98104
/// Load `internal/config.json`.
99105
///
100106
/// - Missing file → default config in memory, no write.
@@ -107,7 +113,10 @@ impl AppConfig {
107113
}
108114
match std::fs::read(path) {
109115
Ok(bytes) => match serde_json::from_slice::<AppConfig>(&bytes) {
110-
Ok(cfg) => cfg,
116+
Ok(mut cfg) => {
117+
cfg.normalize();
118+
cfg
119+
}
111120
Err(e) => {
112121
log::warn!(
113122
"config.json at {} failed to parse: {e}. Falling back to defaults in memory; the file will not be overwritten until the next explicit write.",
@@ -180,12 +189,13 @@ impl AppConfig {
180189
merged_obj.insert(k.clone(), v.clone());
181190
}
182191

183-
let next: AppConfig = serde_json::from_value(merged).map_err(|e| {
192+
let mut next: AppConfig = serde_json::from_value(merged).map_err(|e| {
184193
StorageError::InvalidConfigValue {
185194
key: "<patch>".into(),
186195
reason: e.to_string(),
187196
}
188197
})?;
198+
next.normalize();
189199
*self = next;
190200
Ok(())
191201
}
@@ -198,3 +208,34 @@ impl AppConfig {
198208
}
199209
}
200210

211+
#[cfg(test)]
212+
mod tests {
213+
use super::*;
214+
215+
#[test]
216+
fn load_normalizes_invalid_theme_to_system() {
217+
let path = std::env::temp_dir().join(format!(
218+
"switchhosts-config-test-{}-{}.json",
219+
std::process::id(),
220+
std::time::SystemTime::now()
221+
.duration_since(std::time::UNIX_EPOCH)
222+
.unwrap()
223+
.as_nanos()
224+
));
225+
std::fs::write(&path, r#"{ "theme": "sepia" }"#).unwrap();
226+
227+
let cfg = AppConfig::load(&path);
228+
let _ = std::fs::remove_file(path);
229+
230+
assert_eq!(cfg.theme, "system");
231+
}
232+
233+
#[test]
234+
fn apply_partial_normalizes_invalid_theme_to_system() {
235+
let mut cfg = AppConfig::default();
236+
237+
cfg.apply_partial(&json!({ "theme": "sepia" })).unwrap();
238+
239+
assert_eq!(cfg.theme, "system");
240+
}
241+
}

src/common/default_configs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const configs = {
1818
write_mode: 'append' as WriteModeType,
1919
history_limit: 50,
2020
locale: undefined as DefaultLocaleType,
21-
theme: 'light' as ThemeType,
21+
theme: 'system' as ThemeType,
2222
choice_mode: 2 as FolderModeType,
2323
show_title_on_tray: false,
2424
hide_at_launch: false,

src/common/i18n/languages/de.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ const lang: LanguageDict = {
166166
theme: 'Thema',
167167
theme_dark: 'Dunkel',
168168
theme_light: 'Hell',
169+
theme_system: 'System folgen',
169170
title: 'Titel',
170171
to_show_source: 'Durch Doppelklick wird der Quellcode angezeigt.',
171172
toggle_developer_tools: 'Entwicklerwerkzeuge umschalten',

src/common/i18n/languages/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export default {
167167
theme: 'Theme',
168168
theme_dark: 'Dark',
169169
theme_light: 'Light',
170+
theme_system: 'Follow system',
170171
title: 'Title',
171172
to_show_source: 'Double-click to show the source code.',
172173
toggle_developer_tools: 'Toggle Developer Tools',

src/common/i18n/languages/fr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ const lang: LanguageDict = {
168168
theme: 'Thème',
169169
theme_dark: 'Sombre',
170170
theme_light: 'Clair',
171+
theme_system: 'Suivre le système',
171172
title: 'Titre',
172173
to_show_source: 'Double-cliquez pour afficher le code source',
173174
toggle_developer_tools: 'Afficher/Masquer les outils de développement',

src/common/i18n/languages/ja.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ const lang: LanguageDict = {
163163
theme: 'テーマ',
164164
theme_dark: 'ダーク',
165165
theme_light: 'ライト',
166+
theme_system: 'システムに合わせる',
166167
title: 'タイトル',
167168
to_show_source: 'ダブルクリックでソースコードを表示する。',
168169
toggle_developer_tools: '開発者ツールの表示/非表示',

src/common/i18n/languages/ko.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export default {
165165
theme: '테마',
166166
theme_dark: '다크',
167167
theme_light: '라이트',
168+
theme_system: '시스템 설정 따름',
168169
title: '제목',
169170
to_show_source: '더블 클릭하여 소스코드를 표시합니다.',
170171
toggle_developer_tools: '개발자 도구 전환',

src/common/i18n/languages/pl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export default {
157157
theme: 'Motyw',
158158
theme_dark: 'Ciemny',
159159
theme_light: 'Jasny',
160+
theme_system: 'Zgodnie z systemem',
160161
title: 'Tytuł',
161162
to_show_source: 'Kliknij dwukrotnie, aby wyświetlić kod źródłowy.',
162163
toggle_developer_tools: 'Przełącz narzędzia deweloperskie',

src/common/i18n/languages/tr.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ export default {
157157
system_hosts_history_limit: 'Maksimum kayıt sayısı: ',
158158
test: 'Test',
159159
theme: 'Tema',
160-
theme_dark: 'Karanlık',
161-
theme_light: 'Aydınlık',
160+
theme_dark: 'Koyu',
161+
theme_light: 'Açık',
162+
theme_system: 'Sistemi takip et',
162163
title: 'Başlık',
163164
to_show_source: 'Kaynak kodunu göstermek için çift tıklayın.',
164165
toggle_developer_tools: 'Geliştirici araçlarını aç/kapat',

src/common/i18n/languages/zh-hant.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ const lang: LanguageDict = {
156156
test: '測試',
157157
theme: '主題',
158158
theme_dark: '深色',
159-
theme_light: '亮色',
159+
theme_light: '淺色',
160+
theme_system: '跟隨系統',
160161
title: '標題',
161162
to_show_source: '雙擊顯示原始碼。',
162163
toggle_developer_tools: '切換開發者工具',

0 commit comments

Comments
 (0)