Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src-tauri/src/desktop/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ pub struct DesktopSettings {
pub close_to_background_on_close: bool,
pub show_system_tray_icon: bool,
pub use_custom_title_bar: bool,
pub spellcheck: bool,
}

pub(crate) const DESKTOP_SETTINGS_PATH: &str = "desktop-preferences.json";
pub(crate) const CLOSE_TO_BACKGROUND_ON_CLOSE_KEY: &str = "closeToBackgroundOnClose";
pub(crate) const SHOW_SYSTEM_TRAY_ICON_KEY: &str = "showSystemTrayIcon";
pub(crate) const USE_CUSTOM_TITLE_BAR_KEY: &str = "useCustomTitleBar";
pub(crate) const SPELLCHECK_KEY: &str = "spellcheck";
pub(crate) const LEGACY_KEEP_BACKGROUND_RUNNING_KEY: &str = "keepBackgroundRunning";

pub(crate) const fn use_custom_title_bar_default() -> bool {
Expand All @@ -28,13 +30,15 @@ pub(crate) fn desktop_settings_from_values(
close_to_background_on_close: Option<bool>,
show_system_tray_icon: Option<bool>,
use_custom_title_bar: Option<bool>,
spellcheck: Option<bool>,
keep_background_running: Option<bool>,
) -> DesktopSettings {
DesktopSettings {
close_to_background_on_close: close_to_background_on_close.unwrap_or(true)
|| keep_background_running.unwrap_or(false),
show_system_tray_icon: show_system_tray_icon.unwrap_or(true),
use_custom_title_bar: use_custom_title_bar.unwrap_or(use_custom_title_bar_default()),
spellcheck: spellcheck.unwrap_or(true),
}
}

Expand All @@ -50,6 +54,7 @@ mod tests {
close_to_background_on_close: true,
show_system_tray_icon: false,
use_custom_title_bar: true,
spellcheck: false,
};

assert_eq!(
Expand All @@ -58,6 +63,7 @@ mod tests {
"closeToBackgroundOnClose": true,
"showSystemTrayIcon": false,
"useCustomTitleBar": true,
"spellcheck": false,
})
);
}
Expand All @@ -69,12 +75,14 @@ mod tests {
"closeToBackgroundOnClose": true,
"showSystemTrayIcon": false,
"useCustomTitleBar": true,
"spellcheck": false,
}))
.unwrap(),
DesktopSettings {
close_to_background_on_close: true,
show_system_tray_icon: false,
use_custom_title_bar: true,
spellcheck: false,
}
);
}
Expand All @@ -87,38 +95,48 @@ mod tests {
assert!(output.contains("closeToBackgroundOnClose: boolean"));
assert!(output.contains("showSystemTrayIcon: boolean"));
assert!(output.contains("useCustomTitleBar: boolean"));
assert!(output.contains("spellcheck: boolean"));
assert!(!output.contains("keepBackgroundRunning: boolean"));
}

#[test]
fn legacy_background_setting_keeps_close_behavior_enabled() {
assert_eq!(
desktop_settings_from_values(Some(false), Some(false), Some(false), Some(true)),
desktop_settings_from_values(Some(false), Some(false), Some(false), None, Some(true)),
DesktopSettings {
close_to_background_on_close: true,
show_system_tray_icon: false,
use_custom_title_bar: false,
spellcheck: true,
}
);
}

#[test]
fn explicit_close_setting_stays_disabled_when_legacy_background_is_off() {
assert_eq!(
desktop_settings_from_values(Some(false), Some(true), Some(true), Some(false)),
desktop_settings_from_values(Some(false), Some(true), Some(true), None, Some(false)),
DesktopSettings {
close_to_background_on_close: false,
show_system_tray_icon: true,
use_custom_title_bar: true,
spellcheck: true,
}
);
}

#[test]
fn missing_custom_title_bar_setting_uses_platform_default() {
assert_eq!(
desktop_settings_from_values(Some(true), Some(true), None, None).use_custom_title_bar,
desktop_settings_from_values(Some(true), Some(true), None, None, None)
.use_custom_title_bar,
cfg!(target_os = "windows")
);
}

#[test]
fn missing_spellcheck_setting_defaults_to_enabled() {
assert!(desktop_settings_from_values(None, None, None, None, None).spellcheck);
assert!(!desktop_settings_from_values(None, None, None, Some(false), None).spellcheck);
}
}
24 changes: 20 additions & 4 deletions src-tauri/src/desktop/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::desktop::runtime_state::DesktopRuntimeState;
use crate::desktop::settings::{
desktop_settings_from_values, tray_available_for_session, use_custom_title_bar_default,
DesktopSettings, CLOSE_TO_BACKGROUND_ON_CLOSE_KEY, DESKTOP_SETTINGS_PATH,
LEGACY_KEEP_BACKGROUND_RUNNING_KEY, SHOW_SYSTEM_TRAY_ICON_KEY, USE_CUSTOM_TITLE_BAR_KEY,
LEGACY_KEEP_BACKGROUND_RUNNING_KEY, SHOW_SYSTEM_TRAY_ICON_KEY, SPELLCHECK_KEY,
USE_CUSTOM_TITLE_BAR_KEY,
};
use serde_json::json;
use tauri::{
Expand All @@ -26,6 +27,7 @@ pub struct DesktopSettingsState {
close_to_background_on_close: AtomicBool,
show_system_tray_icon: AtomicBool,
use_custom_title_bar: AtomicBool,
spellcheck: AtomicBool,
tray_available: AtomicBool,
}

Expand All @@ -35,6 +37,7 @@ impl Default for DesktopSettingsState {
close_to_background_on_close: AtomicBool::new(true),
show_system_tray_icon: AtomicBool::new(true),
use_custom_title_bar: AtomicBool::new(use_custom_title_bar_default()),
spellcheck: AtomicBool::new(true),
tray_available: AtomicBool::new(false),
}
}
Expand Down Expand Up @@ -98,6 +101,7 @@ pub(crate) fn load_desktop_settings(
USE_CUSTOM_TITLE_BAR_KEY.into(),
json!(use_custom_title_bar_default()),
),
(SPELLCHECK_KEY.into(), json!(true)),
]))
.build()
.map_err(|error| tauri::Error::PluginInitialization("store".into(), error.to_string()))?;
Expand All @@ -112,6 +116,7 @@ pub(crate) fn load_desktop_settings(
store
.get(USE_CUSTOM_TITLE_BAR_KEY)
.and_then(|value| value.as_bool()),
store.get(SPELLCHECK_KEY).and_then(|value| value.as_bool()),
store
.get(LEGACY_KEEP_BACKGROUND_RUNNING_KEY)
.and_then(|value| value.as_bool()),
Expand All @@ -124,6 +129,7 @@ fn current_desktop_settings(app: &AppHandle<crate::BrowserEngine>) -> DesktopSet
close_to_background_on_close: state.close_to_background_on_close.load(Ordering::Relaxed),
show_system_tray_icon: state.show_system_tray_icon.load(Ordering::Relaxed),
use_custom_title_bar: state.use_custom_title_bar.load(Ordering::Relaxed),
spellcheck: state.spellcheck.load(Ordering::Relaxed),
}
}

Expand Down Expand Up @@ -171,6 +177,9 @@ fn apply_desktop_settings(
state
.use_custom_title_bar
.store(settings.use_custom_title_bar, Ordering::Relaxed);
state
.spellcheck
.store(settings.spellcheck, Ordering::Relaxed);

apply_main_window_title_bar_settings(app, settings)?;

Expand Down Expand Up @@ -395,6 +404,7 @@ mod tests {
close_to_background_on_close: true,
show_system_tray_icon: true,
use_custom_title_bar: false,
spellcheck: true,
};

assert_eq!(
Expand All @@ -409,6 +419,7 @@ mod tests {
show_system_tray_icon: true,
close_to_background_on_close: false,
use_custom_title_bar: false,
spellcheck: true,
};

assert_eq!(
Expand All @@ -423,6 +434,7 @@ mod tests {
close_to_background_on_close: true,
show_system_tray_icon: true,
use_custom_title_bar: false,
spellcheck: true,
};

assert!(!tray_available_for_session(settings, false));
Expand Down Expand Up @@ -451,6 +463,7 @@ mod tests {
close_to_background_on_close: true,
show_system_tray_icon: true,
use_custom_title_bar: false,
spellcheck: true,
};

assert_eq!(
Expand All @@ -462,35 +475,38 @@ mod tests {
#[test]
fn missing_store_values_default_to_enabled() {
assert_eq!(
desktop_settings_from_values(None, None, None, None),
desktop_settings_from_values(None, None, None, None, None),
DesktopSettings {
close_to_background_on_close: true,
show_system_tray_icon: true,
use_custom_title_bar: use_custom_title_bar_default(),
spellcheck: true,
}
);
}

#[test]
fn legacy_background_store_value_migrates_to_close_behavior() {
assert_eq!(
desktop_settings_from_values(Some(false), Some(false), Some(false), Some(true)),
desktop_settings_from_values(Some(false), Some(false), Some(false), None, Some(true)),
DesktopSettings {
close_to_background_on_close: true,
show_system_tray_icon: false,
use_custom_title_bar: false,
spellcheck: true,
}
);
}

#[test]
fn explicit_store_values_are_preserved_when_legacy_background_is_off() {
assert_eq!(
desktop_settings_from_values(Some(false), Some(false), Some(true), Some(false)),
desktop_settings_from_values(Some(false), Some(false), Some(true), None, Some(false)),
DesktopSettings {
show_system_tray_icon: false,
close_to_background_on_close: false,
use_custom_title_bar: true,
spellcheck: true,
}
);
}
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ mod tests {
close_to_background_on_close: true,
show_system_tray_icon: true,
use_custom_title_bar: false,
spellcheck: true,
};
let _ = crate::desktop::runtime_state::DesktopRuntimeState {
tray_available: true,
Expand Down
2 changes: 2 additions & 0 deletions src/app/components/app-shell/AppShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { QueryClientProvider } from '@tanstack/react-query';
import { isTauri } from '@tauri-apps/api/core';
import { type as osType } from '@tauri-apps/plugin-os';

import { DesktopSpellcheck } from '$components/tauri/DesktopSpellcheck';
import { TauriFrontendReady } from '$components/tauri/TauriFrontendReady';
import { TauriWindowFocus } from '$components/tauri/TauriWindowFocus';
import { DesktopTitleBar } from '$components/tauri/DesktopTitleBar';
Expand Down Expand Up @@ -89,6 +90,7 @@ function AppShellFrame({ children, portalContainer, onPortalContainerChange }: A
<>
<TauriFrontendReady />
<TauriWindowFocus />
<DesktopSpellcheck />
<div
style={{
display: 'flex',
Expand Down
66 changes: 66 additions & 0 deletions src/app/components/tauri/DesktopSpellcheck.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { render } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { DesktopSpellcheck } from './DesktopSpellcheck';

const { mockIsDesktopTauri, mockUseDesktopSetting } = vi.hoisted(() => ({
mockIsDesktopTauri: vi.fn<() => boolean>(),
mockUseDesktopSetting: vi.fn<() => readonly [boolean, (value: boolean) => void]>(),
}));

vi.mock('$utils/platform', () => ({
isDesktopTauri: mockIsDesktopTauri,
}));

vi.mock('$state/hooks/desktopSettings', () => ({
useDesktopSetting: mockUseDesktopSetting,
}));

describe('DesktopSpellcheck', () => {
beforeEach(() => {
vi.clearAllMocks();
mockUseDesktopSetting.mockReturnValue([true, vi.fn<(value: boolean) => void>()]);
});

afterEach(() => {
document.body.removeAttribute('spellcheck');
});

it('disables inherited spellcheck on the body when the setting is off', () => {
mockIsDesktopTauri.mockReturnValue(true);
mockUseDesktopSetting.mockReturnValue([false, vi.fn<(value: boolean) => void>()]);

render(<DesktopSpellcheck />);

expect(document.body.spellcheck).toBe(false);
});

it('keeps inherited spellcheck enabled on the body when the setting is on', () => {
mockIsDesktopTauri.mockReturnValue(true);

render(<DesktopSpellcheck />);

expect(document.body.spellcheck).toBe(true);
});

it('follows setting changes after mount', () => {
mockIsDesktopTauri.mockReturnValue(true);
mockUseDesktopSetting.mockReturnValue([true, vi.fn<(value: boolean) => void>()]);

const { rerender } = render(<DesktopSpellcheck />);
expect(document.body.spellcheck).toBe(true);

mockUseDesktopSetting.mockReturnValue([false, vi.fn<(value: boolean) => void>()]);
rerender(<DesktopSpellcheck />);

expect(document.body.spellcheck).toBe(false);
});

it('does not touch the body outside desktop Tauri builds', () => {
mockIsDesktopTauri.mockReturnValue(false);
mockUseDesktopSetting.mockReturnValue([false, vi.fn<(value: boolean) => void>()]);

render(<DesktopSpellcheck />);

expect(document.body.hasAttribute('spellcheck')).toBe(false);
});
});
16 changes: 16 additions & 0 deletions src/app/components/tauri/DesktopSpellcheck.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect } from 'react';
import { isDesktopTauri } from '$utils/platform';
import { useDesktopSetting } from '$state/hooks/desktopSettings';

// Editable fields inherit their spellcheck state from <body>, so a single
// attribute governs the composer and every text input.
export function DesktopSpellcheck() {
const [spellcheck] = useDesktopSetting('spellcheck');

useEffect(() => {
if (!isDesktopTauri()) return;
document.body.spellcheck = spellcheck;
}, [spellcheck]);

return null;
}
Loading
Loading