diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index dbac882..721aaf7 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -569,9 +569,18 @@ pub fn import_custom_themes( } #[tauri::command] -pub fn set_font_size(app: AppHandle, state: State<'_, AppState>, size: u32) -> Result<(), String> { +pub fn set_font_size( + app: AppHandle, + state: State<'_, AppState>, + size: u32, + preferred: Option, +) -> Result<(), String> { let mut config = state.config.lock().map_err(|e| e.to_string())?; config.font_size = Some(size); + // Only a choice made in Appearance becomes the size that resetting the zoom returns to. + if preferred == Some(true) { + config.preferred_font_size = Some(size); + } save_app_config(&config)?; drop(config); @@ -4222,12 +4231,24 @@ pub fn load_app_config() -> AppConfig { .and_then(|p| std::fs::read_to_string(p).ok()) .and_then(|s| serde_json::from_str(&s).ok()) .unwrap_or_default(); - if migrate_global_sync_to_vault(&mut config) { + let migrated_sync = migrate_global_sync_to_vault(&mut config); + let migrated_font = adopt_font_size_as_preference(&mut config); + if migrated_sync || migrated_font { let _ = save_app_config(&config); } config } +// One-time migration: the size in use when the preference was introduced becomes the preference, +// so resetting the zoom does not fall back to the built-in default for existing users. +fn adopt_font_size_as_preference(config: &mut AppConfig) -> bool { + if config.preferred_font_size.is_some() || config.font_size.is_none() { + return false; + } + config.preferred_font_size = config.font_size; + true +} + // One-time migration: WebDAV sync moved from global AppConfig to per-vault VaultConfig. // Copy the old global settings into the active vault's config if it has none yet. Idempotent. fn migrate_global_sync_to_vault(config: &mut AppConfig) -> bool { @@ -4368,6 +4389,32 @@ pub fn get_install_type() -> String { } } +#[cfg(test)] +mod font_size_preference_tests { + use super::*; + + #[test] + fn current_size_becomes_the_preference_once() { + let mut config = AppConfig { + font_size: Some(18), + ..AppConfig::default() + }; + assert!(adopt_font_size_as_preference(&mut config)); + assert_eq!(config.preferred_font_size, Some(18)); + // A later zoom changes only the size in use. + config.font_size = Some(22); + assert!(!adopt_font_size_as_preference(&mut config)); + assert_eq!(config.preferred_font_size, Some(18)); + } + + #[test] + fn nothing_is_adopted_without_a_saved_size() { + let mut config = AppConfig::default(); + assert!(!adopt_font_size_as_preference(&mut config)); + assert_eq!(config.preferred_font_size, None); + } +} + #[cfg(test)] mod vault_runtime_tests { use super::*; diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs index 44b6384..6ac4a7f 100644 --- a/src-tauri/src/types.rs +++ b/src-tauri/src/types.rs @@ -163,6 +163,10 @@ pub struct AppConfig { pub accent_color: Option, #[serde(default)] pub font_size: Option, + /// The size chosen in Appearance. Zooming changes `font_size` only, and resetting the zoom + /// returns to this. + #[serde(default)] + pub preferred_font_size: Option, #[serde(default = "default_true")] pub scroll_to_change_font_size: bool, #[serde(default)] @@ -337,6 +341,7 @@ impl Default for AppConfig { system_dark_theme: default_system_dark_theme(), accent_color: None, font_size: None, + preferred_font_size: None, scroll_to_change_font_size: true, toolbar_layout: None, font_family: None, diff --git a/src/lib/api.ts b/src/lib/api.ts index ec8d860..1507e7a 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -80,8 +80,9 @@ export async function importCustomThemes(path: string): Promise { return invoke("import_custom_themes", { path }); } -export async function setFontSize(size: number): Promise { - return invoke("set_font_size", { size }); +/** `preferred` marks a choice made in Appearance, which is what resetting the zoom returns to. */ +export async function setFontSize(size: number, preferred = false): Promise { + return invoke("set_font_size", { size, preferred }); } export async function setScrollToChangeFontSize(enabled: boolean): Promise { diff --git a/src/lib/components/SettingsPanel.svelte b/src/lib/components/SettingsPanel.svelte index 609e132..0e4f539 100644 --- a/src/lib/components/SettingsPanel.svelte +++ b/src/lib/components/SettingsPanel.svelte @@ -1099,8 +1099,11 @@ function selectFontSize(size: number) { activeFontSize = size; applyFontSize(size); - if ($appConfig) $appConfig.font_size = size; - setFontSize(size).catch((e) => console.error('Failed to save font size:', e)); + if ($appConfig) { + $appConfig.font_size = size; + $appConfig.preferred_font_size = size; + } + setFontSize(size, true).catch((e) => console.error('Failed to save font size:', e)); } function toggleScrollToChangeFontSize() { diff --git a/src/lib/types.ts b/src/lib/types.ts index 91e047b..7df00f3 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -113,6 +113,7 @@ export interface AppConfig { system_dark_theme: string; accent_color: string | null; font_size: number | null; + preferred_font_size?: number | null; scroll_to_change_font_size: boolean; toolbar_layout: ToolbarLayout | null; font_family: string | null; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 7fba781..bfacede 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -119,7 +119,8 @@ } else if (event.key === '0') { event.preventDefault(); event.stopPropagation(); - setEditorFontSize(defaultEditorFontSize); + // Back to the size chosen in Appearance, not the built-in default. + setEditorFontSize($appConfig?.preferred_font_size ?? defaultEditorFontSize); } };