From 7ed0ce05e2e7f38877704795acb2d285058c85de Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:49:29 +0000 Subject: [PATCH 1/9] feat(editor): default preview quality to Full on capable machines --- apps/desktop-gpui/Cargo.lock | 1 + apps/desktop-gpui/Cargo.toml | 2 + apps/desktop-gpui/src/camera_blur_portable.rs | 2 +- apps/desktop-gpui/src/editor_window.rs | 7 +- apps/desktop-gpui/src/store.rs | 142 +++++++++++++++++- 5 files changed, 146 insertions(+), 8 deletions(-) diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock index 0fc64486f79..747fe3c967e 100644 --- a/apps/desktop-gpui/Cargo.lock +++ b/apps/desktop-gpui/Cargo.lock @@ -1564,6 +1564,7 @@ dependencies = [ "cap-camera-effects", "cap-cli-install", "cap-cursor-info", + "cap-d3d-adapter", "cap-editor", "cap-enc-ffmpeg", "cap-export", diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml index 8203d9806b1..a9e61768b59 100644 --- a/apps/desktop-gpui/Cargo.toml +++ b/apps/desktop-gpui/Cargo.toml @@ -207,6 +207,8 @@ tempfile = "3" libc = "0.2" [target.'cfg(windows)'.dependencies] +# The editor's default preview quality reads the GPU's dedicated memory. +cap-d3d-adapter = { path = "../../crates/d3d-adapter" } clipboard-rs = "0.2.4" raw-window-handle = "0.6" same-file = "1.0.6" diff --git a/apps/desktop-gpui/src/camera_blur_portable.rs b/apps/desktop-gpui/src/camera_blur_portable.rs index f5080c2156f..ab433473e6f 100644 --- a/apps/desktop-gpui/src/camera_blur_portable.rs +++ b/apps/desktop-gpui/src/camera_blur_portable.rs @@ -21,7 +21,7 @@ fn total_memory_bytes() -> Option { } #[cfg(windows)] -fn total_memory_bytes() -> Option { +pub(crate) fn total_memory_bytes() -> Option { use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index be77af85eeb..5b1e5c17f85 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -8406,12 +8406,7 @@ impl EditorWindow { if let Some(transport) = &self.transport { transport.set_resolution(resolution); } - let value = serde_json::Value::String(quality.as_json().to_string()); - if !crate::store::set_store_setting( - crate::store::GENERAL_SETTINGS, - "editorPreviewQuality", - value, - ) { + if !crate::store::set_editor_preview_quality(quality) { tracing::warn!("failed to persist preview quality"); } if was_playing { diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index bce4c10042c..1008a97821e 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -501,6 +501,22 @@ pub fn set_store_setting(section: &str, key: &str, value: Value) -> bool { }) } +/// The editor's quality control: the value and the mark that it was chosen, +/// in one write. +pub fn set_editor_preview_quality(quality: EditorPreviewQuality) -> bool { + update_store_section(GENERAL_SETTINGS, |general| { + general.insert( + "editorPreviewQuality".to_string(), + Value::from(quality.as_json()), + ); + general.insert( + EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), + Value::Bool(true), + ); + true + }) +} + /// Replace or remove one top-level store section. `authStore.set(null)` in /// the Tauri app drops the whole `auth` object; writing keys one at a time /// would leave a half-signed-out session behind. @@ -883,6 +899,71 @@ impl EditorPreviewQuality { Self::Quarter => 0.25, } } + + /// Every earlier build wrote the whole settings object, so a stored `half` + /// without [`EDITOR_PREVIEW_QUALITY_CHOSEN_KEY`] is the old default, not a + /// choice. Full and Quarter were only ever picked by hand. + fn from_general(general: &Map) -> Self { + let stored = general + .get("editorPreviewQuality") + .and_then(Value::as_str) + .and_then(Self::from_json); + match stored { + Some(quality) + if quality != Self::Half + || bool_at(general, EDITOR_PREVIEW_QUALITY_CHOSEN_KEY, false) => + { + quality + } + _ => Self::for_this_machine(), + } + } + + /// Full where the GPU draws it at no measurable cost, Half where Full + /// drops playback frames (software and integrated GPUs). + pub fn for_this_machine() -> Self { + static QUALITY: std::sync::OnceLock = std::sync::OnceLock::new(); + *QUALITY.get_or_init(|| { + if full_preview_is_cheap() { + Self::Full + } else { + Self::Half + } + }) + } +} + +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] +fn full_preview_is_cheap() -> bool { + true +} + +/// A dedicated GPU (the editor renders on the high-performance adapter) and +/// 16 GB of memory. Integrated GPUs report a few hundred MB of dedicated +/// memory at most. A 16 GB machine reports slightly under 16 GiB. +#[cfg(target_os = "windows")] +fn full_preview_is_cheap() -> bool { + const MIN_DEDICATED_VIDEO_MEMORY: u64 = 2 << 30; + const MIN_MEMORY: u64 = 15 << 30; + let video_memory = cap_d3d_adapter::select_capture_adapter(None) + .map_or(0, |adapter| adapter.dedicated_vram_bytes); + let memory = crate::camera_blur_portable::total_memory_bytes().unwrap_or(0); + let cheap = video_memory >= MIN_DEDICATED_VIDEO_MEMORY && memory >= MIN_MEMORY; + tracing::info!( + video_memory_mb = video_memory >> 20, + memory_mb = memory >> 20, + cheap, + "editor preview quality default" + ); + cheap +} + +#[cfg(not(any( + all(target_os = "macos", target_arch = "aarch64"), + target_os = "windows" +)))] +fn full_preview_is_cheap() -> bool { + false } /// One entry of `general_settings.excludedWindows` (`WindowExclusion`). @@ -1095,6 +1176,8 @@ pub struct GeneralSettings { pub const SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY: &str = "showMainWindowAfterInstantRecording"; pub const EDITOR_PREVIEW_SPEED_KEY: &str = "editorPreviewSpeed"; +/// Written when someone picks a preview quality in the editor. +pub const EDITOR_PREVIEW_QUALITY_CHOSEN_KEY: &str = "editorPreviewQualityChosen"; pub const RECORDING_MARKER_SOUND_KEY: &str = "recordingMarkerSound"; pub const KEEP_DRAWINGS_KEY: &str = "keepDrawingsOnScreen"; @@ -1532,7 +1615,7 @@ impl GeneralSettings { update_download_policy: enum_at(general, "updateDownloadPolicy"), server_url: string_at(general, "serverUrl", DEFAULT_SERVER_URL), enable_telemetry: bool_at(general, "enableTelemetry", true), - editor_preview_quality: enum_at(general, "editorPreviewQuality"), + editor_preview_quality: EditorPreviewQuality::from_general(general), editor_preview_speed: general .get(EDITOR_PREVIEW_SPEED_KEY) .and_then(Value::as_f64) @@ -3716,6 +3799,63 @@ mod tests { assert!(bundle_first.matches(Some("com.cap.desktop"), None, Some("Wrong"))); } + #[test] + fn editor_preview_quality_defaults_per_machine_until_chosen() { + let read = |quality: Option<&str>, chosen: Option| { + let mut general = Map::new(); + if let Some(quality) = quality { + general.insert("editorPreviewQuality".to_string(), Value::from(quality)); + } + if let Some(chosen) = chosen { + general.insert( + EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), + Value::from(chosen), + ); + } + GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) + .editor_preview_quality + }; + let machine = EditorPreviewQuality::for_this_machine(); + if cfg!(all(target_os = "macos", target_arch = "aarch64")) { + assert_eq!(machine, EditorPreviewQuality::Full); + } + if cfg!(target_os = "linux") { + assert_eq!(machine, EditorPreviewQuality::Half); + } + assert_eq!(read(None, None), machine); + assert_eq!(read(Some("half"), None), machine); + assert_eq!(read(Some("half"), Some(false)), machine); + assert_eq!(read(Some("bogus"), Some(true)), machine); + assert_eq!(read(Some("half"), Some(true)), EditorPreviewQuality::Half); + assert_eq!(read(Some("full"), None), EditorPreviewQuality::Full); + assert_eq!(read(Some("quarter"), None), EditorPreviewQuality::Quarter); + assert_eq!(read(Some("full"), Some(true)), EditorPreviewQuality::Full); + } + + #[test] + fn choosing_half_keeps_it_over_the_machine_default() { + let store = TempStore::new( + "preview-quality", + Some(r#"{ "general_settings": { "editorPreviewQuality": "half", "theme": "dark" } }"#), + ); + assert_eq!( + GeneralSettings::load().editor_preview_quality, + EditorPreviewQuality::for_this_machine() + ); + assert!(set_editor_preview_quality(EditorPreviewQuality::Half)); + let written = store.read(); + assert_eq!(written["general_settings"]["editorPreviewQuality"], "half"); + assert_eq!( + written["general_settings"][EDITOR_PREVIEW_QUALITY_CHOSEN_KEY], + true + ); + assert_eq!(written["general_settings"]["theme"], "dark"); + assert_eq!( + GeneralSettings::load().editor_preview_quality, + EditorPreviewQuality::Half + ); + } + #[test] fn editor_preview_speed_reads_offered_steps_and_falls_back_to_normal() { let read = |value: Option| { From c7b71300fdd5d397156ecccb27b6d007c38ddef0 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:49:29 +0000 Subject: [PATCH 2/9] fix(editor): redraw a paused Full preview at the canvas size --- apps/desktop-gpui/src/editor_window.rs | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index 5b1e5c17f85..6f8cf765e67 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -4787,11 +4787,11 @@ impl EditorWindow { /// Redraw the paused frame once at the canvas's device-pixel size. /// /// Playback and paused interactions keep rendering at the saved preview - /// quality, so their cost does not change. With the default Half quality - /// that frame is smaller than a Retina canvas and gets magnified, so once - /// the preview has been still for `delay` (a pause, the end of a scrub or - /// an edit, a resize) the same frame is rendered again at the canvas size. - /// Full and Quarter are explicit choices and are drawn as chosen. + /// quality, so their cost does not change. Half and Full frames are + /// smaller than a large Retina canvas and get magnified, so once the + /// preview has been still for `delay` (a pause, the end of a scrub or an + /// edit, a resize) the same frame is rendered again at the canvas size. + /// Quarter is an explicit choice for speed and is drawn as chosen. pub(crate) fn schedule_sharp_preview( &mut self, delay: Duration, @@ -4801,7 +4801,7 @@ impl EditorWindow { if self.playing { return SharpPreview::Playing; } - if self.preview_quality != crate::store::EditorPreviewQuality::Half { + if self.preview_quality == crate::store::EditorPreviewQuality::Quarter { return SharpPreview::ChosenQuality; } if self.preparing_presentation.is_some() @@ -14390,6 +14390,26 @@ mod tests { }); } + #[gpui::test] + fn full_gets_the_sharp_redraw_and_quarter_is_drawn_as_chosen( + cx: &mut gpui::TestAppContext, + ) { + let (editor, cx) = super::side_panel::fixture(cx); + editor.update(cx, |editor, cx| { + editor.playing = false; + editor.preview_quality = crate::store::EditorPreviewQuality::Full; + assert_eq!( + editor.schedule_sharp_preview(Duration::ZERO, cx), + SharpPreview::NoEngine + ); + editor.preview_quality = crate::store::EditorPreviewQuality::Quarter; + assert_eq!( + editor.schedule_sharp_preview(Duration::ZERO, cx), + SharpPreview::ChosenQuality + ); + }); + } + #[gpui::test] fn a_committed_adoption_asks_for_the_sharp_redraw_when_paused( cx: &mut gpui::TestAppContext, From 1506c9386fe45ba37b47fb60f098552abd6ceb50 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:54:53 +0000 Subject: [PATCH 3/9] fix(editor): keep Half when the editor renders on a software adapter --- apps/desktop-gpui/src/app_windows.rs | 2 ++ apps/desktop-gpui/src/editor_window.rs | 3 +++ apps/desktop-gpui/src/store.rs | 29 ++++++++++++++++++-------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index 11d5cf3d3c3..aad87a62424 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -5531,12 +5531,14 @@ pub(crate) async fn shared_wgpu_device() -> Option { tracing::warn!(%error, "could not precreate shared wgpu device"); + crate::store::EditorPreviewQuality::note_rendering_adapter(true); None } } diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index 6f8cf765e67..5affde9ba91 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -3600,6 +3600,9 @@ impl EditorWindow { window: &mut Window, cx: &mut Context, ) { + // The render device exists by now, so a software fallback has been + // noted and an unchosen default can step down to Half. + self.preview_quality = crate::store::GeneralSettings::load().editor_preview_quality; transport.set_resolution(self.preview_resolution()); self.transport = Some(transport); self.stats = Some(stats); diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 1008a97821e..4ae5d53b87e 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -920,19 +920,30 @@ impl EditorPreviewQuality { } /// Full where the GPU draws it at no measurable cost, Half where Full - /// drops playback frames (software and integrated GPUs). + /// drops playback frames (software and integrated GPUs). A renderer that + /// fell back to (or was forced onto) a software adapter reads back every + /// frame on the CPU, whatever GPU is installed. pub fn for_this_machine() -> Self { - static QUALITY: std::sync::OnceLock = std::sync::OnceLock::new(); - *QUALITY.get_or_init(|| { - if full_preview_is_cheap() { - Self::Full - } else { - Self::Half - } - }) + static FULL_IS_CHEAP: std::sync::OnceLock = std::sync::OnceLock::new(); + let software = SOFTWARE_RENDERING.load(std::sync::atomic::Ordering::Acquire) + || cap_rendering::force_software_wgpu_adapter(); + if !software && *FULL_IS_CHEAP.get_or_init(full_preview_is_cheap) { + Self::Full + } else { + Self::Half + } + } + + /// Called with the shared render device's adapter, or `true` when no + /// hardware device could be made. + pub fn note_rendering_adapter(software: bool) { + SOFTWARE_RENDERING.store(software, std::sync::atomic::Ordering::Release); } } +static SOFTWARE_RENDERING: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + #[cfg(all(target_os = "macos", target_arch = "aarch64"))] fn full_preview_is_cheap() -> bool { true From a4540e3efbac073e06b0435eb673a7bc709b433c Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:54:53 +0000 Subject: [PATCH 4/9] fix(editor): save the preview quality choice on every click --- apps/desktop-gpui/src/editor_window.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index 5affde9ba91..a87239c072f 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -8399,6 +8399,9 @@ impl EditorWindow { quality: crate::store::EditorPreviewQuality, cx: &mut Context, ) { + if !crate::store::set_editor_preview_quality(quality) { + tracing::warn!("failed to persist preview quality"); + } if quality == self.preview_quality { return; } @@ -8409,9 +8412,6 @@ impl EditorWindow { if let Some(transport) = &self.transport { transport.set_resolution(resolution); } - if !crate::store::set_editor_preview_quality(quality) { - tracing::warn!("failed to persist preview quality"); - } if was_playing { self.stop_playback(cx); self.start_playback(from, cx); From d7ea8917f12e90839230a60064553aa716554ad1 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:54:53 +0000 Subject: [PATCH 5/9] style(editor): drop a comment that restates the setter --- apps/desktop-gpui/src/store.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 4ae5d53b87e..23b8546af62 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -501,8 +501,6 @@ pub fn set_store_setting(section: &str, key: &str, value: Value) -> bool { }) } -/// The editor's quality control: the value and the mark that it was chosen, -/// in one write. pub fn set_editor_preview_quality(quality: EditorPreviewQuality) -> bool { update_store_section(GENERAL_SETTINGS, |general| { general.insert( From 38c5868b47d7ea63f0c0f16ba387f758af3de0b0 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:57:56 +0000 Subject: [PATCH 6/9] style(editor): drop a comment that restates the chosen key --- apps/desktop-gpui/src/store.rs | 4019 -------------------------------- 1 file changed, 4019 deletions(-) diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 23b8546af62..e69de29bb2d 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -1,4019 +0,0 @@ -//! Persisted state: this app's own, and the settings store it shares with -//! 0.6.0 and earlier (the Tauri app, still downloadable and able to open the -//! same data after a downgrade). -//! -//! Two files, both inside `so.cap.desktop`'s app-data dir: -//! -//! - `gpui-state.json` -- ours. The Tauri app kept the camera window's chrome -//! state in the webview's `localStorage` (`cameraWindowState`); there is no -//! webview here, so the same shape lives in a file. Reads happen once at -//! open; writes are whole-file rewrites on a background thread -- the state -//! is a handful of scalars, atomicity beyond rename is not worth plumbing. -//! - `store` -- the tauri-plugin-store file 0.6.0 loads -//! (`Store.load("store")` in `apps/desktop/src/store.ts` at `cap-v0.6.2`), -//! holding `general_settings`, `recording_start_safety`, `auth`, `presets` -//! and the rest. Both apps read and write it, so every write here is a -//! read-modify-write on the raw JSON that touches exactly one key: see -//! [`set_store_setting`]. - -use std::{ - collections::HashMap, - path::{Path, PathBuf}, - sync::{Arc, LazyLock, Mutex, MutexGuard, PoisonError}, - time::{Duration, SystemTime}, -}; - -use serde::{Deserialize, Serialize}; -use serde_json::{Map, Value}; - -use crate::i18n::tr; - -/// `CameraPreviewShape` in the web app. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] -#[serde(rename_all = "lowercase")] -pub enum CameraShape { - #[default] - Round, - Square, - Full, -} - -/// `BackgroundBlurMode`. Cycled and persisted like the Tauri bubble's, and -/// processed live by the preview (`camera_blur.rs`). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] -#[serde(rename_all = "lowercase")] -pub enum BlurMode { - #[default] - Off, - Light, - Heavy, - Remove, -} - -impl BlurMode { - pub fn cycle(self) -> Self { - match self { - Self::Off => Self::Light, - Self::Light => Self::Heavy, - Self::Heavy if cfg!(target_os = "macos") => Self::Remove, - Self::Heavy | Self::Remove => Self::Off, - } - } - - /// The tiny label under the person glyph: `Light` / `Heavy`, nothing when - /// off. - pub fn label(self) -> Option<&'static str> { - match self { - Self::Off => None, - Self::Light => Some(tr::camera::blur::light()), - Self::Heavy => Some(tr::camera::blur::heavy()), - Self::Remove => Some(tr::camera::blur::cutout()), - } - } -} - -/// `CameraWindowState` from `CameraPreviewChrome.tsx`. -#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] -pub struct CameraWindowState { - pub size: f32, - pub shape: CameraShape, - pub mirrored: bool, - pub background_blur: BlurMode, -} - -impl Default for CameraWindowState { - fn default() -> Self { - Self { - size: crate::camera_window::CAMERA_DEFAULT_SIZE, - shape: CameraShape::Round, - mirrored: false, - background_blur: BlurMode::Off, - } - } -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct PersistedState { - pub camera_window: Option, - #[serde(default)] - pub export: Option, - /// `screenshotEditorLayersPanelOpen` -- the Tauri screenshot editor keeps - /// it in `localStorage` (`makePersisted` in - /// `screenshot-editor/context.tsx`); no webview here, so it lives in this - /// file. - #[serde(default)] - pub screenshot_layers_panel_open: Option, - /// `editorCropSnapToRatio` -- the crop dialog's persisted snap toggle, - /// also `localStorage` over there. - #[serde(default)] - pub screenshot_crop_snap_to_ratio: Option, - #[serde(default)] - pub editor_window: Option, -} - -/// Where the editor window last sat on each display, keyed by display UUID. -/// Offsets are measured from the display's top-left corner, so they survive -/// the display being moved around in the arrangement. -#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] -pub struct EditorWindowMemory { - #[serde(default)] - pub last_display: Option, - #[serde(default)] - pub displays: Vec, -} - -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct RememberedEditorFrame { - pub display: String, - pub left: f64, - pub top: f64, - pub width: f64, - pub height: f64, -} - -/// `export_settings` in the Tauri editor (`ExportPage.tsx` localStorage). -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct ExportPrefs { - pub format: String, - pub fps: u32, - pub export_to: String, - pub resolution: String, - pub compression: String, - #[serde(default)] - pub optimize_filesize: bool, - #[serde(default)] - pub cursor_only: bool, - pub custom_bpp: Option, - #[serde(default)] - pub force_ffmpeg_decoder: bool, - #[serde(default)] - pub advanced_open: bool, - #[serde(default)] - pub organization_id: Option, - #[serde(default)] - pub turbo: bool, - #[serde(default)] - pub gif_quality: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub excluded_tracks: Option>, - #[serde(default)] - pub resolution_from_source: bool, - #[serde(default)] - pub fps_from_source: bool, -} - -/// `so.cap.desktop`'s app-data dir -- where both stores live. -/// -/// The identifier every Cap release has used (`so.cap.desktop`), resolved -/// the way each OS's app-data convention spells it, so recordings and -/// settings from 0.6.0 and earlier are found where those builds left them. -pub fn app_data_dir() -> PathBuf { - if let Ok(dir) = std::env::var("CAP_GPUI_APP_DATA_DIR") - && !dir.trim().is_empty() - { - return PathBuf::from(dir); - } - - #[cfg(target_os = "macos")] - { - PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".into())) - .join("Library/Application Support/so.cap.desktop") - } - #[cfg(target_os = "windows")] - { - std::env::var_os("APPDATA") - .map(PathBuf::from) - .unwrap_or_else(|| PathBuf::from(".")) - .join("so.cap.desktop") - } - #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] - { - let base = std::env::var_os("XDG_DATA_HOME") - .map(PathBuf::from) - .unwrap_or_else(|| { - PathBuf::from(std::env::var_os("HOME").unwrap_or_else(|| ".".into())) - .join(".local/share") - }); - base.join("so.cap.desktop") - } -} - -pub(crate) fn bundled_resource_dirs() -> Vec { - let executable = std::env::current_exe().ok(); - let override_dir = std::env::var_os("CAP_GPUI_RESOURCES_DIR").map(PathBuf::from); - bundled_resource_dirs_for(executable.as_deref(), override_dir.as_deref()) -} - -fn bundled_resource_dirs_for( - executable: Option<&Path>, - override_dir: Option<&Path>, -) -> Vec { - let mut directories = Vec::new(); - if let Some(directory) = override_dir { - directories.push(directory.to_path_buf()); - } - - if let Some(parent) = executable.and_then(Path::parent) { - if parent.file_name().is_some_and(|name| name == "MacOS") - && let Some(contents) = parent.parent() - { - directories.push(contents.join("Resources")); - } - if parent.file_name().is_some_and(|name| name == "bin") - && let Some(prefix) = parent.parent() - { - directories.push(prefix.join("lib").join("cap")); - } - directories.push(parent.join("resources")); - directories.push(parent.to_path_buf()); - } - - directories -} - -fn state_path() -> PathBuf { - app_data_dir().join("gpui-state.json") -} - -pub fn load() -> PersistedState { - std::fs::read(state_path()) - .ok() - .and_then(|bytes| serde_json::from_slice(&bytes).ok()) - .unwrap_or_default() -} - -static UPDATE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - -/// Read-modify-write on the caller's thread; callers hand this to the -/// background executor. Writers are serialized and the file is replaced by -/// rename, because [`load`] treats a half-written file as empty and the next -/// update would then persist defaults over every other preference. -pub fn update(mutate: impl FnOnce(&mut PersistedState)) { - let _guard = UPDATE_LOCK - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let path = state_path(); - let mut state = load(); - mutate(&mut state); - if let Some(parent) = path.parent() { - let _ = std::fs::create_dir_all(parent); - } - match serde_json::to_vec_pretty(&state) { - Ok(bytes) => { - let temp = path.with_extension("json.tmp"); - if let Err(error) = - std::fs::write(&temp, bytes).and_then(|()| std::fs::rename(&temp, &path)) - { - tracing::warn!("persisting gpui state: {error}"); - } - } - Err(error) => tracing::warn!("serializing gpui state: {error}"), - } -} - -// --------------------------------------------------------------------------- -// The Tauri settings store -// --------------------------------------------------------------------------- - -#[cfg(test)] -thread_local! { - static TEST_TAURI_STORE_PATH: std::cell::RefCell> = const { - std::cell::RefCell::new(None) - }; -} - -#[cfg(test)] -thread_local! { - static TEST_STORE_FILE_READS: std::cell::Cell = const { std::cell::Cell::new(0) }; - pub(crate) static TEST_AFTER_AUTH_SNAPSHOT: std::cell::RefCell>> = const { - std::cell::RefCell::new(None) - }; -} - -#[cfg(test)] -pub(crate) fn set_test_tauri_store_path(path: Option) { - TEST_TAURI_STORE_PATH.with(|current| *current.borrow_mut() = path); -} - -/// The tauri-plugin-store file, shared with the shipping app. -/// -/// `CAP_GPUI_TAURI_STORE` redirects it at a copy, which is how the tests -- -/// and any verification run that must not touch the user's real settings -- -/// work. -pub fn tauri_store_path() -> PathBuf { - #[cfg(test)] - if let Some(path) = TEST_TAURI_STORE_PATH.with(|path| path.borrow().clone()) { - return path; - } - - match std::env::var("CAP_GPUI_TAURI_STORE") { - Ok(path) if !path.is_empty() => PathBuf::from(path), - // `Store.load("store")`: no extension, and the sibling `store.json` - // is a different (stale) file. - _ => app_data_dir().join("store"), - } -} - -static STORE_UPDATE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - -/// The whole store as raw JSON. -/// -/// A missing or whitespace-only file is an empty store. Nonempty invalid JSON -/// is returned as `None` so no write path can silently replace -/// someone's settings with a fresh object. -fn read_store(path: &std::path::Path) -> Option> { - #[cfg(test)] - TEST_STORE_FILE_READS.with(|reads| reads.set(reads.get() + 1)); - let bytes = match std::fs::read(path) { - Ok(bytes) => bytes, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(Map::new()), - Err(error) => { - tracing::warn!("reading the Tauri store: {error}"); - return None; - } - }; - if bytes.iter().all(u8::is_ascii_whitespace) { - return Some(Map::new()); - } - match serde_json::from_slice::(&bytes) { - Ok(Value::Object(map)) => Some(map), - Ok(_) => { - tracing::error!("the Tauri store is not a JSON object; refusing to write to it"); - None - } - Err(error) => { - tracing::error!("the Tauri store did not parse ({error}); refusing to write to it"); - None - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -struct StoreFileStamp { - len: u64, - modified: Option, - #[cfg(unix)] - inode: (u64, u64), - #[cfg(unix)] - changed: (i64, i64), -} - -/// Filesystem mtimes are only as fine as the clock that stamps them (a -/// scheduler tick on Linux, two seconds on FAT), so a rewrite that lands in -/// the same tick as the read we cached can leave every field of the stamp -/// unchanged. A cached parse is only trusted once the file's mtime is older -/// than the read by more than that, the same rule git uses for racily clean -/// index entries. -const STORE_MTIME_SETTLE: Duration = Duration::from_secs(3); - -impl StoreFileStamp { - fn of(metadata: &std::fs::Metadata) -> Self { - #[cfg(unix)] - use std::os::unix::fs::MetadataExt; - - Self { - len: metadata.len(), - modified: metadata.modified().ok(), - #[cfg(unix)] - inode: (metadata.dev(), metadata.ino()), - #[cfg(unix)] - changed: (metadata.ctime(), metadata.ctime_nsec()), - } - } - - fn settled_before(&self, read_at: SystemTime) -> bool { - self.modified - .and_then(|modified| modified.checked_add(STORE_MTIME_SETTLE)) - .is_some_and(|settled| settled < read_at) - } -} - -struct CachedStore { - stamp: StoreFileStamp, - read_at: SystemTime, - store: Arc>, -} - -static STORE_CACHE: LazyLock>> = - LazyLock::new(Default::default); - -fn store_cache() -> MutexGuard<'static, HashMap> { - STORE_CACHE.lock().unwrap_or_else(PoisonError::into_inner) -} - -/// [`read_store`] for readers, which run inside render: the export page asks -/// for the auth section on every progress tick. The file is shared with the -/// Tauri app, which can rewrite it at any time, so every call still stats it -/// and re-reads when anything about the file changed; only the read and parse -/// are skipped. Writers keep reading the disk directly. -fn cached_store(path: &Path) -> Option>> { - let read_at = SystemTime::now(); - let stamp = match std::fs::metadata(path) { - Ok(metadata) => StoreFileStamp::of(&metadata), - Err(_) => { - store_cache().remove(path); - return read_store(path).map(Arc::new); - } - }; - - if let Some(cached) = store_cache().get(path) - && cached.stamp == stamp - && cached.stamp.settled_before(cached.read_at) - { - return Some(cached.store.clone()); - } - - let Some(store) = read_store(path).map(Arc::new) else { - store_cache().remove(path); - return None; - }; - store_cache().insert( - path.to_path_buf(), - CachedStore { - stamp, - read_at, - store: store.clone(), - }, - ); - Some(store) -} - -/// One section of the store (`general_settings`, `recording_start_safety`, -/// ...) as raw JSON. Empty when absent or the wrong shape. -pub fn store_section(section: &str) -> Map { - cached_store(&tauri_store_path()) - .and_then(|store| match store.get(section) { - Some(Value::Object(map)) => Some(map.clone()), - _ => None, - }) - .unwrap_or_default() -} - -pub fn notification_sounds_enabled() -> bool { - cached_store(&tauri_store_path()) - .and_then(|store| { - store - .get(GENERAL_SETTINGS) - .and_then(Value::as_object) - .map(|settings| bool_at(settings, "enableNotifications", true)) - }) - .unwrap_or(false) -} - -/// Read once per stroke, so it goes through the cached store too. -pub fn keep_drawings() -> bool { - cached_store(&tauri_store_path()) - .and_then(|store| { - store - .get(GENERAL_SETTINGS) - .and_then(Value::as_object) - .map(|settings| bool_at(settings, KEEP_DRAWINGS_KEY, false)) - }) - .unwrap_or(false) -} - -/// Write one key of one section, preserving every other byte of meaning in -/// the file. -/// -/// This is the whole compatibility contract with the Tauri app. It reads the -/// raw JSON, replaces exactly `store[section][key]`, and writes it back, so -/// keys this app has never heard of -- everything under `auth`, `presets`, -/// `teleprompter`, the migration flags, and the two thirds of -/// `general_settings` no page here renders -- survive untouched. Serializing -/// a typed struct back over the file would drop them all. -/// -/// Returns false when the store could not be read or written; the caller has -/// already updated its in-memory copy, which is the same optimistic order -/// `handleChange` uses in `general.tsx`. -pub fn set_store_setting(section: &str, key: &str, value: Value) -> bool { - update_store(|store| { - let entry = store - .entry(section.to_string()) - .or_insert_with(|| Value::Object(Map::new())); - if !entry.is_object() { - *entry = Value::Object(Map::new()); - } - entry - .as_object_mut() - .expect("just replaced any non-object with an object") - .insert(key.to_string(), value); - true - }) -} - -pub fn set_editor_preview_quality(quality: EditorPreviewQuality) -> bool { - update_store_section(GENERAL_SETTINGS, |general| { - general.insert( - "editorPreviewQuality".to_string(), - Value::from(quality.as_json()), - ); - general.insert( - EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), - Value::Bool(true), - ); - true - }) -} - -/// Replace or remove one top-level store section. `authStore.set(null)` in -/// the Tauri app drops the whole `auth` object; writing keys one at a time -/// would leave a half-signed-out session behind. -pub fn set_store_value(section: &str, value: Value) -> bool { - update_store(|store| { - if value.is_null() { - store.remove(section); - } else { - store.insert(section.to_string(), value); - } - true - }) -} - -/// Replace one top-level section only if `update` agrees, deciding from the -/// section as it is on disk at write time rather than from a copy read before -/// a network round trip. -pub fn update_store_section( - section: &str, - update: impl FnOnce(&mut Map) -> bool, -) -> bool { - update_store(|store| { - let mut current = match store.remove(section) { - Some(Value::Object(map)) => map, - _ => Map::new(), - }; - let keep = update(&mut current); - store.insert(section.to_string(), Value::Object(current)); - keep - }) -} - -/// Reads, mutates and rewrites the store as one step, so two writers cannot -/// both start from the same snapshot and drop each other's changes. Returning -/// false from `mutate` leaves the file untouched. -fn update_store(mutate: impl FnOnce(&mut Map) -> bool) -> bool { - let _guard = STORE_UPDATE_LOCK - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let path = tauri_store_path(); - let Some(mut store) = read_store(&path) else { - return false; - }; - if !mutate(&mut store) { - return false; - } - let written = write_store(&path, &Value::Object(store)); - store_cache().remove(&path); - written -} - -/// A store that reads but does not parse as a JSON object (zero-filled after a -/// power loss, truncated by a crash) refuses every write, so onboarding could -/// never save its progress and stayed on "Could not save onboarding progress" -/// forever. While it does not parse, its onboarding flags, auth and presets are -/// already invisible to Cap, so its bytes are kept beside it and the progress -/// is saved to a fresh store. A store that could not be read at all is left -/// alone. -fn set_aside_unparseable_store(path: &Path) -> bool { - let _guard = STORE_UPDATE_LOCK - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let Ok(bytes) = std::fs::read(path) else { - return false; - }; - if bytes.iter().all(u8::is_ascii_whitespace) { - return false; - } - if matches!( - serde_json::from_slice::(&bytes), - Ok(Value::Object(_)) - ) { - return false; - } - let stamp = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .map(|elapsed| elapsed.as_millis()) - .unwrap_or_default(); - let mut name = path.file_name().unwrap_or_default().to_os_string(); - name.push(format!(".unreadable-{stamp}")); - let kept = path.with_file_name(name); - match std::fs::rename(path, &kept) { - Ok(()) => { - store_cache().remove(path); - tracing::error!( - kept = %kept.display(), - "the settings store did not parse; kept it aside and started a new one" - ); - true - } - Err(error) => { - tracing::error!(%error, "could not set aside the unparseable settings store"); - false - } - } -} - -/// Pretty JSON, two-space indent, no trailing newline -- byte-shape identical -/// to what tauri-plugin-store writes, so a store the two apps take turns on -/// does not churn. -/// -/// Written to a sibling temp file and renamed, so a crash mid-write cannot -/// leave a half-written store behind (the Tauri app would refuse to parse it, -/// and the user would lose every setting). -fn write_store(path: &std::path::Path, store: &Value) -> bool { - let bytes = match serde_json::to_vec_pretty(store) { - Ok(bytes) => bytes, - Err(error) => { - tracing::error!("serializing the Tauri store: {error}"); - return false; - } - }; - - if let Some(parent) = path.parent() - && let Err(error) = std::fs::create_dir_all(parent) - { - tracing::warn!("creating the store directory: {error}"); - return false; - } - - let temp = path.with_extension("gpui-tmp"); - if let Err(error) = std::fs::write(&temp, bytes) { - tracing::warn!("writing the Tauri store: {error}"); - return false; - } - if let Err(error) = std::fs::rename(&temp, path) { - tracing::warn!("replacing the Tauri store: {error}"); - let _ = std::fs::remove_file(&temp); - return false; - } - true -} - -// -- Typed reads ------------------------------------------------------------ -// -// Field by field rather than `serde::Deserialize` on a struct: one setting -// written by a newer Tauri build with a value this enum does not know would -// fail a whole-struct deserialize and blank the entire page. Here it falls -// back to its default and every other row still shows the user's real value. - -fn bool_at(map: &Map, key: &str, default: bool) -> bool { - map.get(key).and_then(Value::as_bool).unwrap_or(default) -} - -fn u32_at(map: &Map, key: &str, default: u32) -> u32 { - map.get(key) - .and_then(Value::as_u64) - .map(|value| value as u32) - .unwrap_or(default) -} - -fn opt_u32_at(map: &Map, key: &str) -> Option { - map.get(key) - .and_then(Value::as_u64) - .map(|value| value as u32) -} - -fn opt_f32_at(map: &Map, key: &str) -> Option { - map.get(key) - .and_then(Value::as_f64) - .map(|value| value as f32) -} - -fn f32_at(map: &Map, key: &str, default: f32) -> f32 { - opt_f32_at(map, key).unwrap_or(default) -} - -fn opt_string_at(map: &Map, key: &str) -> Option { - map.get(key) - .and_then(Value::as_str) - .map(|value| value.to_string()) -} - -fn string_at(map: &Map, key: &str, default: &str) -> String { - opt_string_at(map, key).unwrap_or_else(|| default.to_string()) -} - -fn strings_at(map: &Map, key: &str) -> Vec { - map.get(key) - .and_then(Value::as_array) - .map(|entries| { - entries - .iter() - .filter_map(Value::as_str) - .map(|value| value.to_string()) - .collect() - }) - .unwrap_or_default() -} - -fn enum_at(map: &Map, key: &str) -> T { - map.get(key) - .and_then(Value::as_str) - .and_then(T::from_json) - .unwrap_or_default() -} - -/// A string-valued setting with a fixed option list -- the shape every -/// `#[serde(rename_all = "camelCase")]` enum in -/// `apps/desktop/src-tauri/src/general_settings.rs` serializes to. -pub trait SettingsEnum: Sized + Default + Copy + PartialEq + 'static { - const ALL: &'static [Self]; - fn from_json(value: &str) -> Option; - fn as_json(self) -> &'static str; - /// The option's text in the settings UI, which is not derivable from the - /// JSON name ("openEditor" is shown as "Open editor"). - fn label(self) -> &'static str; -} - -macro_rules! settings_enum { - ( - $(#[$meta:meta])* - $name:ident { - $($variant:ident = $json:literal, $label:expr;)+ - } - default = $default:ident - ) => { - $(#[$meta])* - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - pub enum $name { - $($variant,)+ - } - - impl Default for $name { - fn default() -> Self { - Self::$default - } - } - - impl SettingsEnum for $name { - const ALL: &'static [Self] = &[$(Self::$variant,)+]; - - fn from_json(value: &str) -> Option { - match value { - $($json => Some(Self::$variant),)+ - _ => None, - } - } - - fn as_json(self) -> &'static str { - match self { - $(Self::$variant => $json,)+ - } - } - - fn label(self) -> &'static str { - match self { - $(Self::$variant => $label,)+ - } - } - } - }; -} - -settings_enum! { - /// `AppTheme`. The previews are labelled System/Light/Dark in - /// `AppearanceSection`. - AppTheme { - System = "system", tr::settings::general::appearance::system(); - Light = "light", tr::settings::general::appearance::light(); - Dark = "dark", tr::settings::general::appearance::dark(); - } - default = System -} - -settings_enum! { - /// `StudioRecordingQuality`, whose Rust default is `balanced`. - StudioQuality { - Compatibility = "compatibility", tr::settings::options::studio_quality::compatibility(); - Balanced = "balanced", tr::settings::options::studio_quality::balanced(); - Ultra = "ultra", tr::settings::options::studio_quality::ultra(); - } - default = Balanced -} - -impl From for cap_recording::StudioQuality { - fn from(quality: StudioQuality) -> Self { - match quality { - StudioQuality::Compatibility => Self::Compatibility, - StudioQuality::Balanced => Self::Balanced, - StudioQuality::Ultra => Self::Ultra, - } - } -} - -settings_enum! { - /// `MainWindowRecordingStartBehaviour`. British spelling in both the JSON - /// and the label, as shipped. - MainWindowStartBehaviour { - Close = "close", tr::settings::options::main_window_start::close(); - Minimise = "minimise", tr::settings::options::main_window_start::minimise(); - } - default = Close -} - -settings_enum! { - /// How a window recording captures the picked window: the region of the - /// screen it covers, or (Focused) the window by itself, centred by the - /// picker and followed if it moves. - WindowCaptureStyle { - ScreenRegion = "screenRegion", tr::settings::options::window_capture::screen_region(); - FocusedWindow = "focusedWindow", tr::settings::options::window_capture::focused_window(); - } - default = ScreenRegion -} - -settings_enum! { - /// `PostStudioRecordingBehaviour`. - PostStudioBehaviour { - OpenEditor = "openEditor", tr::settings::options::post_studio::open_editor(); - ShowOverlay = "showOverlay", tr::settings::options::post_studio::show_overlay(); - DoNothing = "doNothing", tr::settings::options::post_studio::do_nothing(); - } - default = OpenEditor -} - -settings_enum! { - /// What a finished screenshot does next. GPUI-only: the Tauri app always - /// opened the editor and never reads this key. - PostScreenshotBehaviour { - OpenEditor = "openEditor", tr::settings::options::post_screenshot::open_editor(); - Copy = "copy", tr::settings::options::post_screenshot::copy(); - CopyAndOpenEditor = "copyAndOpenEditor", tr::settings::options::post_screenshot::copy_and_open_editor(); - Save = "save", tr::settings::options::post_screenshot::do_nothing(); - } - default = OpenEditor -} - -impl PostScreenshotBehaviour { - pub fn copies(self) -> bool { - matches!(self, Self::Copy | Self::CopyAndOpenEditor) - } - - pub fn opens_editor(self) -> bool { - matches!(self, Self::OpenEditor | Self::CopyAndOpenEditor) - } -} - -settings_enum! { - /// `PostDeletionBehaviour`. - PostDeletionBehaviour { - DoNothing = "doNothing", tr::settings::options::post_deletion::do_nothing(); - ReopenRecordingWindow = "reopenRecordingWindow", tr::settings::options::post_deletion::reopen(); - } - default = DoNothing -} - -settings_enum! { - /// `UpdateChannel` from `src-tauri/src/updates.rs`. - UpdateChannel { - Stable = "stable", "Stable"; - Nightly = "nightly", "Nightly"; - } - default = Stable -} - -settings_enum! { - UpdateDownloadPolicy { - Manual = "manual", "Manual"; - Auto = "auto", "Auto"; - Never = "never", "Never"; - } - default = Manual -} - -settings_enum! { - EditorPreviewQuality { - Quarter = "quarter", tr::settings::options::preview_quality::quarter(); - Half = "half", tr::settings::options::preview_quality::half(); - Full = "full", tr::settings::options::preview_quality::full(); - } - default = Half -} - -impl EditorPreviewQuality { - pub fn scale(self) -> f32 { - match self { - Self::Full => 1.0, - Self::Half => 0.65, - Self::Quarter => 0.25, - } - } - - /// Every earlier build wrote the whole settings object, so a stored `half` - /// without [`EDITOR_PREVIEW_QUALITY_CHOSEN_KEY`] is the old default, not a - /// choice. Full and Quarter were only ever picked by hand. - fn from_general(general: &Map) -> Self { - let stored = general - .get("editorPreviewQuality") - .and_then(Value::as_str) - .and_then(Self::from_json); - match stored { - Some(quality) - if quality != Self::Half - || bool_at(general, EDITOR_PREVIEW_QUALITY_CHOSEN_KEY, false) => - { - quality - } - _ => Self::for_this_machine(), - } - } - - /// Full where the GPU draws it at no measurable cost, Half where Full - /// drops playback frames (software and integrated GPUs). A renderer that - /// fell back to (or was forced onto) a software adapter reads back every - /// frame on the CPU, whatever GPU is installed. - pub fn for_this_machine() -> Self { - static FULL_IS_CHEAP: std::sync::OnceLock = std::sync::OnceLock::new(); - let software = SOFTWARE_RENDERING.load(std::sync::atomic::Ordering::Acquire) - || cap_rendering::force_software_wgpu_adapter(); - if !software && *FULL_IS_CHEAP.get_or_init(full_preview_is_cheap) { - Self::Full - } else { - Self::Half - } - } - - /// Called with the shared render device's adapter, or `true` when no - /// hardware device could be made. - pub fn note_rendering_adapter(software: bool) { - SOFTWARE_RENDERING.store(software, std::sync::atomic::Ordering::Release); - } -} - -static SOFTWARE_RENDERING: std::sync::atomic::AtomicBool = - std::sync::atomic::AtomicBool::new(false); - -#[cfg(all(target_os = "macos", target_arch = "aarch64"))] -fn full_preview_is_cheap() -> bool { - true -} - -/// A dedicated GPU (the editor renders on the high-performance adapter) and -/// 16 GB of memory. Integrated GPUs report a few hundred MB of dedicated -/// memory at most. A 16 GB machine reports slightly under 16 GiB. -#[cfg(target_os = "windows")] -fn full_preview_is_cheap() -> bool { - const MIN_DEDICATED_VIDEO_MEMORY: u64 = 2 << 30; - const MIN_MEMORY: u64 = 15 << 30; - let video_memory = cap_d3d_adapter::select_capture_adapter(None) - .map_or(0, |adapter| adapter.dedicated_vram_bytes); - let memory = crate::camera_blur_portable::total_memory_bytes().unwrap_or(0); - let cheap = video_memory >= MIN_DEDICATED_VIDEO_MEMORY && memory >= MIN_MEMORY; - tracing::info!( - video_memory_mb = video_memory >> 20, - memory_mb = memory >> 20, - cheap, - "editor preview quality default" - ); - cheap -} - -#[cfg(not(any( - all(target_os = "macos", target_arch = "aarch64"), - target_os = "windows" -)))] -fn full_preview_is_cheap() -> bool { - false -} - -/// One entry of `general_settings.excludedWindows` (`WindowExclusion`). -#[derive(Debug, Clone, PartialEq, Eq, Default)] -pub struct WindowExclusion { - pub bundle_identifier: Option, - pub owner_name: Option, - pub window_title: Option, -} - -impl WindowExclusion { - fn from_json(value: &Value) -> Option { - let map = value.as_object()?; - let exclusion = Self { - bundle_identifier: opt_string_at(map, "bundleIdentifier"), - owner_name: opt_string_at(map, "ownerName"), - window_title: opt_string_at(map, "windowTitle"), - }; - (exclusion != Self::default()).then_some(exclusion) - } - - fn to_json(&self) -> Value { - let field = |value: &Option| match value { - Some(value) => Value::String(value.clone()), - None => Value::Null, - }; - Value::Object(Map::from_iter([ - ( - "bundleIdentifier".to_string(), - field(&self.bundle_identifier), - ), - ("ownerName".to_string(), field(&self.owner_name)), - ("windowTitle".to_string(), field(&self.window_title)), - ])) - } - - /// `getExclusionPrimaryLabel` in `general.tsx`. - pub fn primary_label(&self) -> &str { - self.owner_name - .as_deref() - .or(self.window_title.as_deref()) - .or(self.bundle_identifier.as_deref()) - .unwrap_or("Unknown") - } - - /// `getExclusionSecondaryLabel`. - pub fn secondary_label(&self) -> Option<&str> { - if self.owner_name.is_some() && self.window_title.is_some() { - return self.window_title.as_deref(); - } - if self.bundle_identifier.is_some() - && (self.owner_name.is_some() || self.window_title.is_some()) - { - return self.bundle_identifier.as_deref(); - } - self.bundle_identifier.as_deref() - } - - /// `WindowExclusion::matches` in `src-tauri/src/window_exclusion.rs`, - /// verbatim: bundle id alone matches; owner+title requires both; owner or - /// title alone match on their own. - pub fn matches( - &self, - bundle_identifier: Option<&str>, - owner_name: Option<&str>, - window_title: Option<&str>, - ) -> bool { - if let Some(identifier) = self.bundle_identifier.as_deref() - && bundle_identifier == Some(identifier) - { - return true; - } - - if let Some(expected_owner) = self.owner_name.as_deref() { - let owner_matches = owner_name == Some(expected_owner); - if let Some(expected_title) = self.window_title.as_deref() { - return owner_matches && window_title == Some(expected_title); - } - if owner_matches { - return true; - } - } - - if let Some(expected_title) = self.window_title.as_deref() { - return window_title == Some(expected_title); - } - - false - } -} - -/// `DEFAULT_EXCLUDED_WINDOW_TITLES` in `general_settings.rs`, which is what -/// the page's Reset button asks the backend for. -pub const DEFAULT_EXCLUDED_WINDOW_TITLES: &[&str] = &[ - "Cap", - "Cap Settings", - "Cap Recording Controls", - "Cap Camera", - "Cap Window Capture Occluder", - "Cap Capture Area", - "Cap Mode Selection", - "Cap Recordings Overlay", - "Cap Teleprompter", -]; - -pub fn default_excluded_windows() -> Vec { - DEFAULT_EXCLUDED_WINDOW_TITLES - .iter() - .map(|title| WindowExclusion { - bundle_identifier: None, - owner_name: None, - window_title: Some((*title).to_string()), - }) - .collect() -} - -pub fn excluded_windows_to_json(windows: &[WindowExclusion]) -> Value { - Value::Array(windows.iter().map(WindowExclusion::to_json).collect()) -} - -/// `https://cap.so` -- `default_server_url()` in `general_settings.rs`, which -/// is also what `clientEnv.VITE_SERVER_URL` resolves to in a release build. -pub const DEFAULT_SERVER_URL: &str = "https://cap.so"; - -/// `DEFAULT_FILENAME_TEMPLATE` in `src-tauri/src/recording.rs`, spelled the -/// same way `general.tsx` spells it. -pub const DEFAULT_PROJECT_NAME_TEMPLATE: &str = "{target_name} ({target_kind}) {date} {time}"; - -/// The `general_settings` section, as the General page reads it. -/// -/// The defaults are `createDefaultGeneralSettings()` plus the per-row `??` -/// fallbacks in `general.tsx` -- i.e. what the shipping page *shows* for a key -/// that is not in the file. Two of them disagree with the Rust struct's own -/// `#[serde(default)]`: `autoZoomOnClicks` (Rust `default_true`, the page -/// false) and `enableNativeCameraPreview`. The page is the authority here -/// because this is a page. -#[derive(Debug, Clone, PartialEq)] -pub struct GeneralSettings { - pub theme: AppTheme, - /// `uiLanguage`: absent, `"system"`, or a catalog code such as `"fr"`. A - /// GPUI-only key; the Tauri app keeps it as an unknown field. - pub ui_language: Option, - pub hide_dock_icon: bool, - pub enable_notifications: bool, - pub instant_mode_max_resolution: u32, - pub disable_auto_open_links: bool, - /// GPUI-only `showMainWindowAfterInstantRecording`: whether the main - /// window comes back after an Instant recording whose link did not open. - /// Kept as stored, absent included, so every reader resolves it the same - /// way in [`Self::post_instant_behaviour`]. - pub show_main_window_after_instant: Option, - pub studio_recording_quality: StudioQuality, - pub recording_countdown: Option, - pub main_window_recording_start_behaviour: MainWindowStartBehaviour, - pub window_capture_style: WindowCaptureStyle, - pub focus_center: bool, - pub post_studio_recording_behaviour: PostStudioBehaviour, - pub post_screenshot_behaviour: PostScreenshotBehaviour, - /// GPUI-only: a cancelled picker leaves the main window hidden unless it - /// was on screen when the picker opened. - pub keep_main_hidden_on_cancel: bool, - /// GPUI-only: closing the last editor leaves the main window hidden. - pub keep_main_hidden_after_editor: bool, - pub post_deletion_behaviour: PostDeletionBehaviour, - pub delete_instant_recordings_after_upload: bool, - pub crash_recovery_recording: bool, - pub custom_cursor_capture: bool, - pub auto_zoom_on_clicks: bool, - pub default_zoom_amount: Option, - pub capture_keyboard_events: bool, - pub macbook_notch_overlay: bool, - /// GPUI-only `recordingMarkerSound`: a short cue when a marker is added - /// during a Studio recording. Off by default, because a microphone or - /// system audio capture can record the cue. - pub recording_marker_sound: bool, - /// GPUI-only `keepDrawingsOnScreen`: strokes drawn on screen while - /// recording stay until cleared instead of fading out. - pub keep_drawings: bool, - pub max_fps: u32, - pub recordings_path: Option, - /// Custom folders the library has been pointed at before. Not rendered by - /// any page -- the recordings scan reads it, because - /// `known_recordings_dirs` keeps listing recordings that were left behind - /// in a folder the user has since switched away from. - pub previous_recordings_paths: Vec, - pub default_project_name_template: Option, - pub excluded_windows: Vec, - pub update_channel: UpdateChannel, - pub update_download_policy: UpdateDownloadPolicy, - pub server_url: String, - pub enable_telemetry: bool, - pub editor_preview_quality: EditorPreviewQuality, - /// How fast the editor preview plays. Preview only: export and the - /// project never see it. Anything but one of the offered steps reads as 1x. - pub editor_preview_speed: cap_editor::PreviewSpeed, - /// The Experimental page's two toggles. Both default false the way the - /// page shows them (`!!settings.enableNativeCameraPreview` / - /// `!!settings.outOfProcessMuxer` in experimental.tsx). - pub enable_native_camera_preview: bool, - pub out_of_process_muxer: bool, - /// Not part of `general_settings` at all: the mic-confirmation toggle - /// lives in the store's own `recording_start_safety` section - /// (`RECORDING_START_SAFETY_DEFAULTS`), and the page renders it in the - /// middle of the Recording card as if it were one of them. - pub confirm_without_microphone: bool, - /// Also outside `general_settings`: the `audio_enhancement` section's - /// `enabledByDefault`, rendered in the Recording card next to it. - pub studio_sound_by_default: bool, -} - -pub const SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY: &str = "showMainWindowAfterInstantRecording"; -pub const EDITOR_PREVIEW_SPEED_KEY: &str = "editorPreviewSpeed"; -/// Written when someone picks a preview quality in the editor. -pub const EDITOR_PREVIEW_QUALITY_CHOSEN_KEY: &str = "editorPreviewQualityChosen"; - -pub const RECORDING_MARKER_SOUND_KEY: &str = "recordingMarkerSound"; -pub const KEEP_DRAWINGS_KEY: &str = "keepDrawingsOnScreen"; - -/// Read on every marker press, so it goes through the cached store rather -/// than a full [`GeneralSettings::load`]. -pub fn recording_marker_sound_enabled() -> bool { - cached_store(&tauri_store_path()) - .and_then(|store| { - store - .get(GENERAL_SETTINGS) - .and_then(Value::as_object) - .map(|settings| bool_at(settings, RECORDING_MARKER_SOUND_KEY, false)) - }) - .unwrap_or(false) -} - -/// What follows a cleanly stopped Instant recording, the link always copied. -/// Not one key: `disableAutoOpenLinks` (shared with the Tauri app) decides -/// whether the link opens, `showMainWindowAfterInstantRecording` whether the -/// main window comes back when it does not. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum PostInstantBehaviour { - OpenLink, - ShowCap, - CopyLink, -} - -impl PostInstantBehaviour { - /// `(disableAutoOpenLinks, showMainWindowAfterInstantRecording)`. - pub fn store_values(self) -> (bool, bool) { - match self { - Self::OpenLink => (false, false), - Self::ShowCap => (true, true), - Self::CopyLink => (true, false), - } - } -} - -/// The section names, so the write calls read as the store keys they are. -pub const GENERAL_SETTINGS: &str = "general_settings"; -pub const RECORDING_START_SAFETY: &str = "recording_start_safety"; - -/// Changes the recordings folder and remembers the old one, like Tauri's -/// `remember_previous_recordings_path`, so the library keeps listing projects -/// left in earlier folders. -pub fn set_recordings_path(path: Option<&str>) -> bool { - const MAX_PREVIOUS_PATHS: usize = 20; - - update_store_section(GENERAL_SETTINGS, |settings| { - let mut previous_paths = strings_at(settings, "previousRecordingsPaths"); - if let Some(path) = path { - previous_paths.retain(|previous| previous != path); - } - if let Some(previous) = opt_string_at(settings, "recordingsPath") - && Some(previous.as_str()) != path - && !previous_paths.contains(&previous) - { - previous_paths.push(previous); - let len = previous_paths.len(); - if len > MAX_PREVIOUS_PATHS { - previous_paths.drain(..len - MAX_PREVIOUS_PATHS); - } - } - settings.insert( - "recordingsPath".into(), - path.map_or(Value::Null, Value::from), - ); - settings.insert( - "previousRecordingsPaths".into(), - Value::from(previous_paths), - ); - true - }) -} - -/// `audioEnhancementStore` (`apps/desktop/src/store.ts`): whether new Studio -/// recordings start with Studio Sound on. Defaults on; the editor's Audio tab -/// and the Recording settings card both write it. -pub const AUDIO_ENHANCEMENT: &str = "audio_enhancement"; -pub const STUDIO_SOUND_BY_DEFAULT_KEY: &str = "enabledByDefault"; - -pub fn studio_sound_by_default() -> bool { - bool_at( - &store_section(AUDIO_ENHANCEMENT), - STUDIO_SOUND_BY_DEFAULT_KEY, - true, - ) -} - -pub fn set_studio_sound_by_default(enabled: bool) -> bool { - set_store_setting( - AUDIO_ENHANCEMENT, - STUDIO_SOUND_BY_DEFAULT_KEY, - Value::Bool(enabled), - ) -} - -pub fn studio_sound_isolation() -> cap_project::VoiceIsolation { - store_section(AUDIO_ENHANCEMENT) - .get("isolation") - .cloned() - .and_then(|value| serde_json::from_value(value).ok()) - .unwrap_or_default() -} - -pub fn set_studio_sound_isolation(isolation: cap_project::VoiceIsolation) -> bool { - set_store_setting( - AUDIO_ENHANCEMENT, - "isolation", - serde_json::to_value(isolation).unwrap(), - ) -} - -pub fn set_studio_sound_defaults(enabled: bool, isolation: cap_project::VoiceIsolation) -> bool { - let mut section = store_section(AUDIO_ENHANCEMENT); - section.insert(STUDIO_SOUND_BY_DEFAULT_KEY.into(), Value::Bool(enabled)); - section.insert("isolation".into(), serde_json::to_value(isolation).unwrap()); - set_store_value(AUDIO_ENHANCEMENT, Value::Object(section)) -} -/// `RecordingSettingsStore::KEY` (`src-tauri/src/recording_settings.rs:37`) -- -/// the section the tray's mode picker reads and writes. -pub const RECORDING_SETTINGS: &str = "recording_settings"; - -#[derive(Clone, Copy, Debug, Default, PartialEq)] -pub struct RecordingDeviceSettings { - pub camera: Option, - pub microphone: Option, -} - -impl RecordingDeviceSettings { - pub fn for_camera( - device_id: &str, - model_id: Option<&cap_camera::ModelID>, - ) -> Option { - let settings = store_section(RECORDING_SETTINGS); - let formats = settings.get("cameraDeviceSettings")?; - std::iter::once(format!("device:{device_id}")) - .chain(model_id.map(|model| format!("model:{model}"))) - .find_map(|key| { - formats - .get(key) - .cloned() - .and_then(|value| serde_json::from_value(value).ok()) - }) - } - - pub fn for_microphone( - name: &str, - ) -> Option { - store_section(RECORDING_SETTINGS) - .get("microphoneDeviceSettings")? - .get(name) - .cloned() - .and_then(|value| serde_json::from_value(value).ok()) - } -} - -pub fn set_camera_device_settings( - device_id: &str, - model_id: Option<&cap_camera::ModelID>, - settings: cap_recording::feeds::camera::CameraDeviceSettings, -) -> bool { - let keys = std::iter::once(format!("device:{device_id}")) - .chain(model_id.map(|model| format!("model:{model}"))); - set_device_format_settings("cameraDeviceSettings", keys, settings) -} - -pub fn set_microphone_device_settings( - name: &str, - settings: cap_recording::feeds::microphone::MicrophoneDeviceSettings, -) -> bool { - set_device_format_settings("microphoneDeviceSettings", [name.to_string()], settings) -} - -fn set_device_format_settings( - map_key: &str, - keys: impl IntoIterator, - settings: impl serde::Serialize, -) -> bool { - let Ok(Value::Object(fields)) = serde_json::to_value(settings) else { - return false; - }; - update_store(|store| { - let section = store - .entry(RECORDING_SETTINGS.to_string()) - .or_insert_with(|| Value::Object(Map::new())); - let Some(section) = section.as_object_mut() else { - return false; - }; - let map = section - .entry(map_key.to_string()) - .or_insert_with(|| Value::Object(Map::new())); - let Some(map) = map.as_object_mut() else { - return false; - }; - for key in keys { - let entry = map.entry(key).or_insert_with(|| Value::Object(Map::new())); - let Some(entry) = entry.as_object_mut() else { - return false; - }; - for (key, value) in &fields { - if value.is_null() { - entry.remove(key); - } else { - entry.insert(key.clone(), value.clone()); - } - } - } - true - }) -} - -#[derive(Clone, Debug, Default, PartialEq)] -pub struct RecordingInputSettings { - pub camera_id: Option, - pub microphone_name: Option, -} - -impl RecordingInputSettings { - pub fn load() -> Self { - let settings = store_section(RECORDING_SETTINGS); - Self { - camera_id: settings.get("cameraId").and_then(recording_camera_id), - microphone_name: settings - .get("micName") - .and_then(Value::as_str) - .filter(|name| !name.is_empty()) - .map(str::to_string), - } - } -} - -fn recording_camera_id(value: &Value) -> Option { - use cap_recording::feeds::camera::DeviceOrModelID; - - let object = value.as_object()?; - if object.len() != 1 { - return None; - } - if let Some(id) = object.get("DeviceID").and_then(Value::as_str) { - return (!id.is_empty()).then(|| DeviceOrModelID::DeviceID(id.to_string())); - } - let model = object.get("ModelID")?.as_str()?.to_string(); - cap_camera::ModelID::try_from(model) - .ok() - .map(DeviceOrModelID::ModelID) -} - -pub fn set_recording_camera_id(id: Option<&cap_recording::feeds::camera::DeviceOrModelID>) -> bool { - let Ok(value) = serde_json::to_value(id) else { - return false; - }; - set_store_setting(RECORDING_SETTINGS, "cameraId", value) -} - -/// `RecordingSettingsStore.systemAudio`, which the Tauri app restored on -/// launch. Anything but a boolean reads as off. -pub fn recording_system_audio() -> bool { - store_section(RECORDING_SETTINGS) - .get("systemAudio") - .and_then(Value::as_bool) - .unwrap_or(false) -} - -pub fn set_recording_system_audio(enabled: bool) -> bool { - set_store_setting(RECORDING_SETTINGS, "systemAudio", Value::Bool(enabled)) -} - -pub fn set_recording_microphone_name(name: Option<&str>) -> bool { - set_store_setting( - RECORDING_SETTINGS, - "micName", - name.map_or(Value::Null, |name| Value::String(name.to_string())), - ) -} - -/// `RecordingSettingsStore.mode`, i.e. `cap_recording::RecordingMode` under -/// `#[serde(rename_all = "camelCase")]` -- one lowercase word per variant -/// (`studio`, `instant`, `screenshot`). `None` when the key is absent, which -/// `get_current_mode` turns into `RecordingMode::default()` (Instant). -pub fn recording_mode_slug() -> Option { - store_section(RECORDING_SETTINGS) - .get("mode") - .and_then(Value::as_str) - .map(str::to_string) -} - -/// `RecordingSettingsStore::set_mode`. -/// -/// The Tauri version re-serialises the whole `recording_settings` object, which -/// fills in defaults for every field it does not know about; -/// [`set_store_setting`] replaces only `mode` and leaves the target, the device -/// settings maps and the rest byte-identical. Strictly the safer half of the -/// same write, and the two apps read the same key. -pub fn set_recording_mode_slug(slug: &str) -> bool { - set_store_setting(RECORDING_SETTINGS, "mode", Value::String(slug.to_string())) -} - -pub fn has_completed_startup() -> bool { - store_section(GENERAL_SETTINGS) - .get("hasCompletedStartup") - .and_then(Value::as_bool) - .unwrap_or(false) -} - -pub fn set_has_completed_startup(value: bool) -> bool { - set_onboarding_progress("hasCompletedStartup", value) -} - -fn set_onboarding_progress(key: &str, value: bool) -> bool { - set_store_setting(GENERAL_SETTINGS, key, Value::Bool(value)) - || (set_aside_unparseable_store(&tauri_store_path()) - && set_store_setting(GENERAL_SETTINGS, key, Value::Bool(value))) -} - -pub fn has_completed_onboarding() -> bool { - store_section(GENERAL_SETTINGS) - .get("hasCompletedOnboarding") - .and_then(Value::as_bool) - .unwrap_or(false) -} - -pub fn set_has_completed_onboarding(value: bool) -> bool { - set_onboarding_progress("hasCompletedOnboarding", value) -} - -/// `should_show_onboarding` (`lib.rs:463-477`): first-run flags or missing -/// macOS screen-recording + accessibility. -pub fn should_show_onboarding() -> bool { - !has_completed_startup() - || !has_completed_onboarding() - || !crate::permissions::necessary_granted() -} - -impl Default for GeneralSettings { - fn default() -> Self { - Self::from_sections(&Map::new(), &Map::new(), &Map::new()) - } -} - -impl GeneralSettings { - pub fn post_instant_behaviour(&self) -> PostInstantBehaviour { - if !self.disable_auto_open_links { - PostInstantBehaviour::OpenLink - } else if self.show_main_window_after_instant.unwrap_or(true) { - // Absent: "copy link only" brought the main window back before - // the key existed, so a store that never set it keeps doing that. - PostInstantBehaviour::ShowCap - } else { - PostInstantBehaviour::CopyLink - } - } - - /// `defaultZoomAmount ?? 1.5`: what the settings slider shows and what a - /// zoom segment drawn on the timeline starts at (`TL/ZoomTrack.tsx:226`). - pub fn new_zoom_amount(&self) -> f32 { - self.default_zoom_amount.unwrap_or(1.5) - } - - pub fn load() -> Self { - let store = cached_store(&tauri_store_path()).unwrap_or_default(); - let empty = Map::new(); - let section = |name: &str| store.get(name).and_then(Value::as_object).unwrap_or(&empty); - Self::from_sections( - section(GENERAL_SETTINGS), - section(RECORDING_START_SAFETY), - section(AUDIO_ENHANCEMENT), - ) - } - - fn from_sections( - general: &Map, - safety: &Map, - audio_enhancement: &Map, - ) -> Self { - Self { - theme: enum_at(general, "theme"), - ui_language: opt_string_at(general, crate::i18n::SETTING_KEY), - hide_dock_icon: bool_at(general, "hideDockIcon", false), - enable_notifications: bool_at(general, "enableNotifications", true), - instant_mode_max_resolution: u32_at(general, "instantModeMaxResolution", 1920), - disable_auto_open_links: bool_at(general, "disableAutoOpenLinks", false), - show_main_window_after_instant: general - .get(SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY) - .and_then(Value::as_bool), - studio_recording_quality: enum_at(general, "studioRecordingQuality"), - recording_countdown: opt_u32_at(general, "recordingCountdown"), - main_window_recording_start_behaviour: enum_at( - general, - "mainWindowRecordingStartBehaviour", - ), - window_capture_style: enum_at(general, "windowCaptureStyle"), - focus_center: bool_at(general, "focusCenter", false), - post_studio_recording_behaviour: enum_at(general, "postStudioRecordingBehaviour"), - post_screenshot_behaviour: enum_at(general, "postScreenshotBehaviour"), - keep_main_hidden_on_cancel: bool_at(general, "keepMainHiddenOnCancel", false), - keep_main_hidden_after_editor: bool_at(general, "keepMainHiddenAfterEditor", false), - post_deletion_behaviour: enum_at(general, "postDeletionBehaviour"), - delete_instant_recordings_after_upload: bool_at( - general, - "deleteInstantRecordingsAfterUpload", - false, - ), - crash_recovery_recording: bool_at(general, "crashRecoveryRecording", true), - // The one snake_case key in the store: `#[serde(rename = - // "custom_cursor_capture2")]` on `custom_cursor_capture`, which - // is how the setting was re-defaulted without inheriting the old - // key's values. - custom_cursor_capture: bool_at(general, "custom_cursor_capture2", true), - auto_zoom_on_clicks: bool_at(general, "autoZoomOnClicks", false), - default_zoom_amount: opt_f32_at(general, "defaultZoomAmount"), - capture_keyboard_events: bool_at(general, "captureKeyboardEvents", true), - macbook_notch_overlay: bool_at(general, "macbookNotchOverlay", false), - recording_marker_sound: bool_at(general, RECORDING_MARKER_SOUND_KEY, false), - keep_drawings: bool_at(general, KEEP_DRAWINGS_KEY, false), - max_fps: u32_at(general, "maxFps", 60), - recordings_path: opt_string_at(general, "recordingsPath"), - previous_recordings_paths: strings_at(general, "previousRecordingsPaths"), - default_project_name_template: opt_string_at(general, "defaultProjectNameTemplate"), - // Key absent -> the default exclusion list, matching the Rust - // struct's `#[serde(default = "default_excluded_windows")]`. Key - // present but empty means the user removed every entry. - excluded_windows: general - .get("excludedWindows") - .and_then(Value::as_array) - .map(|entries| { - entries - .iter() - .filter_map(WindowExclusion::from_json) - .collect() - }) - .unwrap_or_else(default_excluded_windows), - update_channel: enum_at(general, "updateChannel"), - update_download_policy: enum_at(general, "updateDownloadPolicy"), - server_url: string_at(general, "serverUrl", DEFAULT_SERVER_URL), - enable_telemetry: bool_at(general, "enableTelemetry", true), - editor_preview_quality: EditorPreviewQuality::from_general(general), - editor_preview_speed: general - .get(EDITOR_PREVIEW_SPEED_KEY) - .and_then(Value::as_f64) - .and_then(cap_editor::PreviewSpeed::from_f64) - .unwrap_or_default(), - enable_native_camera_preview: bool_at(general, "enableNativeCameraPreview", false), - out_of_process_muxer: bool_at(general, "outOfProcessMuxer", false), - confirm_without_microphone: bool_at( - safety, - "confirmBeforeRecordingWithoutMicrophone", - true, - ), - studio_sound_by_default: bool_at(audio_enhancement, STUDIO_SOUND_BY_DEFAULT_KEY, true), - } - } -} - -// -- The teleprompter section ----------------------------------------------- - -/// `teleprompterStore`'s key in the shared store file: -/// `declareStore("teleprompter")` in -/// `apps/desktop/src/store.ts`. A top-level section of its own, like -/// `general_settings`, not a key inside one. -pub const TELEPROMPTER: &str = "teleprompter"; - -/// `TeleprompterStore` (`store.ts:17-35`), with `teleprompterDefaults` as the -/// defaults: `{ script: "", fontSize: 30, wordsPerMinute: 150, lineHeight: 1.5, -/// showCueMarkers: true, mirror: false, windowOpacityPercent: 92 }`. -/// -/// Read field by field for the same reason `GeneralSettings` is: a value -/// written by a newer build that this app cannot parse costs that one field its -/// value, not the whole script. -#[derive(Debug, Clone, PartialEq)] -pub struct TeleprompterState { - pub script: String, - pub font_size: f32, - pub words_per_minute: u32, - pub line_height: f32, - pub show_cue_markers: bool, - pub mirror: bool, - pub window_opacity_percent: u32, -} - -impl TeleprompterState { - /// The store keys, so a write call reads as the key it writes. - pub const SCRIPT: &'static str = "script"; - pub const FONT_SIZE: &'static str = "fontSize"; - pub const WORDS_PER_MINUTE: &'static str = "wordsPerMinute"; - pub const LINE_HEIGHT: &'static str = "lineHeight"; - pub const SHOW_CUE_MARKERS: &'static str = "showCueMarkers"; - pub const MIRROR: &'static str = "mirror"; - pub const WINDOW_OPACITY_PERCENT: &'static str = "windowOpacityPercent"; - - pub fn load() -> Self { - Self::from_section(&store_section(TELEPROMPTER)) - } - - fn from_section(map: &Map) -> Self { - let defaults = Self::default(); - Self { - script: opt_string_at(map, Self::SCRIPT).unwrap_or(defaults.script), - font_size: f32_at(map, Self::FONT_SIZE, defaults.font_size), - words_per_minute: u32_at(map, Self::WORDS_PER_MINUTE, defaults.words_per_minute), - line_height: f32_at(map, Self::LINE_HEIGHT, defaults.line_height), - show_cue_markers: bool_at(map, Self::SHOW_CUE_MARKERS, defaults.show_cue_markers), - mirror: bool_at(map, Self::MIRROR, defaults.mirror), - window_opacity_percent: u32_at( - map, - Self::WINDOW_OPACITY_PERCENT, - defaults.window_opacity_percent, - ), - } - } - - /// One field as the JSON the Tauri app would have written, so the debounced - /// flush can write exactly the keys that changed. - pub fn value_for(&self, key: &str) -> Value { - match key { - Self::SCRIPT => Value::from(self.script.clone()), - Self::FONT_SIZE => Value::from(self.font_size), - Self::WORDS_PER_MINUTE => Value::from(self.words_per_minute), - Self::LINE_HEIGHT => Value::from(self.line_height), - Self::SHOW_CUE_MARKERS => Value::Bool(self.show_cue_markers), - Self::MIRROR => Value::Bool(self.mirror), - Self::WINDOW_OPACITY_PERCENT => Value::from(self.window_opacity_percent), - _ => Value::Null, - } - } -} - -impl Default for TeleprompterState { - fn default() -> Self { - Self { - script: String::new(), - font_size: 30., - words_per_minute: 150, - line_height: 1.5, - show_cue_markers: true, - mirror: false, - window_opacity_percent: 92, - } - } -} - -// -- The hotkeys section ------------------------------------------------------ - -/// `hotkeysStore`'s key: `declareStore("hotkeys")` in -/// `apps/desktop/src/store.ts`. The section is `{ "hotkeys": { action: Hotkey } }`. -pub const HOTKEYS: &str = "hotkeys"; - -/// `Hotkey` (`src-tauri/src/hotkeys.rs:22-29`). `code` is a W3C -/// `KeyboardEvent.code` string, which is how `global_hotkey::Code` serializes -/// and what hotkeys.tsx captures from the DOM event. -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct Hotkey { - pub code: String, - pub meta: bool, - pub ctrl: bool, - pub alt: bool, - pub shift: bool, -} - -/// The raw `hotkeys` map, kept as JSON so bindings for actions this build does -/// not know about (a newer Tauri's `HotkeyAction`) survive a rewrite. -pub fn hotkeys_raw() -> Map { - store_section(HOTKEYS) - .remove("hotkeys") - .and_then(|value| match value { - Value::Object(map) => Some(map), - _ => None, - }) - .unwrap_or_default() -} - -/// `hotkeysStore.set({ hotkeys })`: replace the map, leave the rest of the -/// section (there is none today) alone. -pub fn set_hotkeys_raw(map: &Map) -> bool { - set_store_setting(HOTKEYS, "hotkeys", Value::Object(map.clone())) -} - -pub fn hotkey_from_value(value: &Value) -> Option { - serde_json::from_value(value.clone()).ok() -} - -pub const RECORDING_MARKER_HOTKEY: &str = "addRecordingMarker"; - -/// Set once the marker shortcut's default has been offered, so clearing the -/// shortcut in Settings sticks across launches. -const RECORDING_MARKER_HOTKEY_SEEDED: &str = "recordingMarkerSeeded"; - -/// ⌃⌘M on macOS. Elsewhere Ctrl+Alt is AltGr on many layouts (AltGr+M types -/// µ in German), so Alt+Shift+M instead. -pub fn default_recording_marker_hotkey() -> Hotkey { - let macos = cfg!(target_os = "macos"); - Hotkey { - code: "KeyM".into(), - meta: macos, - ctrl: macos, - alt: !macos, - shift: !macos, - } -} - -/// Gives the marker shortcut its default the first time this build runs, -/// unless the user already bound it, or bound something else to the default -/// combo. Later launches leave the map alone. -pub fn seed_recording_marker_hotkey() { - seed_default_hotkey( - RECORDING_MARKER_HOTKEY, - RECORDING_MARKER_HOTKEY_SEEDED, - default_recording_marker_hotkey(), - ); -} - -pub const DRAW_HOTKEY: &str = "toggleDrawing"; - -const DRAW_HOTKEY_SEEDED: &str = "toggleDrawingSeeded"; - -/// ⌃⌘D is the system's look-up shortcut, so drawing takes ⌃⌘P (pen). -pub fn default_draw_hotkey() -> Hotkey { - Hotkey { - code: "KeyP".into(), - meta: true, - ctrl: true, - alt: false, - shift: false, - } -} - -/// The drawing shortcut's default, offered once the same way as the -/// marker's. Only where drawing on screen is offered. -pub fn seed_draw_hotkey() { - if crate::live_draw::SUPPORTED { - seed_default_hotkey(DRAW_HOTKEY, DRAW_HOTKEY_SEEDED, default_draw_hotkey()); - } -} - -fn seed_default_hotkey(key: &str, seeded: &str, default: Hotkey) { - if store_section(HOTKEYS).get(seeded).and_then(Value::as_bool) == Some(true) { - return; - } - let mut map = hotkeys_raw(); - let taken = map - .values() - .filter_map(hotkey_from_value) - .any(|hotkey| hotkey == default); - if !map.contains_key(key) && !taken { - let Ok(value) = serde_json::to_value(&default) else { - return; - }; - map.insert(key.into(), value); - if !set_hotkeys_raw(&map) { - return; - } - } - set_store_setting(HOTKEYS, seeded, Value::Bool(true)); -} - -// -- Transcription hints -------------------------------------------------------- - -/// `DEFAULT_TRANSCRIPTION_HINTS` (`apps/desktop/src/utils/general-settings.ts`). -pub const DEFAULT_TRANSCRIPTION_HINTS: &[&str] = - &["Cap", "TypeScript", "My Brand Name", "mywebsite.com"]; - -/// `normalizeTranscriptionHints`: strip NULs, trim, drop empties and -/// duplicates, first occurrence wins. -pub fn normalize_transcription_hints(hints: I) -> Vec -where - I: IntoIterator, - I::Item: AsRef, -{ - let mut normalized: Vec = Vec::new(); - for hint in hints { - let value = hint.as_ref().replace('\0', ""); - let value = value.trim(); - if value.is_empty() || normalized.iter().any(|existing| existing == value) { - continue; - } - normalized.push(value.to_string()); - } - normalized -} - -/// `deriveGeneralSettings(store).transcriptionHints ?? []`: the stored array -/// as-is when the key exists (normalisation happens on write, not read), the -/// four defaults when it does not. -pub fn transcription_hints() -> Vec { - match store_section(GENERAL_SETTINGS).get("transcriptionHints") { - Some(Value::Array(entries)) => entries - .iter() - .filter_map(Value::as_str) - .map(str::to_string) - .collect(), - Some(_) => Vec::new(), - None => DEFAULT_TRANSCRIPTION_HINTS - .iter() - .map(|hint| (*hint).to_string()) - .collect(), - } -} - -// -- The auth section ----------------------------------------------------------- - -/// What the settings pages need from `AuthStore` (`src-tauri/src/auth.rs`): -/// the bearer token `protectedHeaders()` sends, and `plan.upgraded`. -#[derive(Debug, Clone, Default)] -pub struct AuthSnapshot { - pub server_url: String, - pub token: Option, - pub plan_upgraded: bool, - pub plan_manual: bool, - pub organizations: Vec, -} - -#[derive(Debug, Clone)] -pub struct AuthOrganization { - pub id: String, - pub name: String, -} - -impl AuthSnapshot { - pub fn signed_in(&self) -> bool { - self.token.is_some() - } - - pub fn is_upgraded(&self) -> bool { - self.plan_upgraded || self.plan_manual - } -} - -/// `maybeProtectedHeaders` (`utils/web-api.ts:64-75`): `secret.api_key` wins -/// over `secret.token`, and `plan.upgraded` is the Pro gate. -pub fn auth_snapshot() -> AuthSnapshot { - let store = cached_store(&tauri_store_path()).unwrap_or_default(); - let empty = Map::new(); - let auth = store - .get("auth") - .and_then(Value::as_object) - .unwrap_or(&empty); - let server_url = string_at( - store - .get(GENERAL_SETTINGS) - .and_then(Value::as_object) - .unwrap_or(&empty), - "serverUrl", - DEFAULT_SERVER_URL, - ); - let token = auth - .get("secret") - .and_then(Value::as_object) - .and_then(|secret| { - secret - .get("api_key") - .or_else(|| secret.get("token")) - .and_then(Value::as_str) - }) - .map(str::to_string); - let plan = auth.get("plan").and_then(Value::as_object); - let plan_upgraded = plan - .and_then(|plan| plan.get("upgraded")) - .and_then(Value::as_bool) - .unwrap_or(false); - let plan_manual = plan - .and_then(|plan| plan.get("manual")) - .and_then(Value::as_bool) - .unwrap_or(false); - let organizations = auth - .get("organizations") - .and_then(Value::as_array) - .map(|entries| { - entries - .iter() - .filter_map(|entry| { - let object = entry.as_object()?; - Some(AuthOrganization { - id: object.get("id")?.as_str()?.to_string(), - name: object - .get("name") - .and_then(Value::as_str) - .unwrap_or("Organization") - .to_string(), - }) - }) - .collect() - }) - .unwrap_or_default(); - #[cfg(test)] - if let Some(after_snapshot) = TEST_AFTER_AUTH_SNAPSHOT.with(|hook| hook.take()) { - after_snapshot(); - } - AuthSnapshot { - server_url, - token, - plan_upgraded, - plan_manual, - organizations, - } -} - -pub fn set_auth(value: Option) -> bool { - set_store_value("auth", value.unwrap_or(Value::Null)) -} - -// -- The commercial license ------------------------------------------------------- - -/// `CommercialLicense` (`src-tauri/src/general_settings.rs:301-308`), stored -/// at `general_settings.commercialLicense`. -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CommercialLicense { - pub license_key: String, - pub expiry_date: Option, - pub refresh: f64, - pub activated_on: f64, -} - -pub fn commercial_license() -> Option { - store_section(GENERAL_SETTINGS) - .get("commercialLicense") - .and_then(|value| serde_json::from_value(value.clone()).ok()) -} - -/// license.tsx deactivates by writing `commercialLicense: undefined`, which -/// JSON.stringify drops; `null` reads back the same on both sides -/// (`Option` and the page's falsiness check). -pub fn set_commercial_license(license: Option<&CommercialLicense>) -> bool { - let value = match license { - Some(license) => serde_json::to_value(license).unwrap_or(Value::Null), - None => Value::Null, - }; - set_store_setting(GENERAL_SETTINGS, "commercialLicense", value) -} - -/// `general_settings.instanceId` -- `#[serde(default = "uuid::Uuid::new_v4")]` -/// over there, so a store the Tauri app has saved always carries one. When -/// only this app has ever run, one is minted and persisted the same way the -/// Tauri default would be on its next save. -pub fn instance_id_or_create() -> Option { - if let Some(id) = opt_string_at(&store_section(GENERAL_SETTINGS), "instanceId") { - return Some(id); - } - let id = new_uuid_v4(); - set_store_setting(GENERAL_SETTINGS, "instanceId", Value::String(id.clone())).then_some(id) -} - -/// A v4 UUID from std's own entropy (`RandomState` is randomly seeded per -/// thread) -- no `uuid`/`rand` dependency for two identifiers a year. -pub fn new_uuid_v4() -> String { - use std::hash::{BuildHasher, Hasher}; - - let word = |salt: u64| { - let mut hasher = std::hash::RandomState::new().build_hasher(); - hasher.write_u64(salt); - hasher.write_u128( - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|elapsed| elapsed.as_nanos()) - .unwrap_or(0), - ); - hasher.finish() - }; - let mut bytes = [0u8; 16]; - bytes[..8].copy_from_slice(&word(0x9e37_79b9).to_le_bytes()); - bytes[8..].copy_from_slice(&word(0x85eb_ca6b).to_le_bytes()); - // Version 4, variant 10xx. - bytes[6] = (bytes[6] & 0x0f) | 0x40; - bytes[8] = (bytes[8] & 0x3f) | 0x80; - format!( - "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", - bytes[0], - bytes[1], - bytes[2], - bytes[3], - bytes[4], - bytes[5], - bytes[6], - bytes[7], - bytes[8], - bytes[9], - bytes[10], - bytes[11], - bytes[12], - bytes[13], - bytes[14], - bytes[15], - ) -} - -// -- The automations section -------------------------------------------------------- - -/// The store key: `automationsStore` in `store.ts`, written wholesale by -/// `set_automations` (`src-tauri/src/automation.rs:834-837`). -pub const AUTOMATIONS: &str = "automations"; - -/// The model, serde-identical to `crates/automation/src/types.rs` (which this -/// standalone workspace cannot depend on). Every attribute below is copied -/// from there so the JSON both apps exchange stays byte-compatible. -#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)] -#[serde(rename_all = "camelCase")] -pub struct AutomationsStore { - #[serde(default)] - pub version: u32, - #[serde(default)] - pub rules: Vec, -} - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] -#[serde(rename_all = "camelCase")] -pub struct AutomationRule { - pub id: String, - pub name: String, - #[serde(default = "default_true")] - pub enabled: bool, - pub trigger: Trigger, - #[serde(default)] - pub match_mode: MatchMode, - #[serde(default)] - pub conditions: Vec, - #[serde(default)] - pub actions: Vec, -} - -fn default_true() -> bool { - true -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] -#[serde(rename_all = "camelCase")] -pub enum MatchMode { - #[default] - All, - Any, -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] -#[serde(rename_all = "camelCase")] -pub enum Trigger { - ScreenshotTaken, - StudioRecordingFinished, - InstantRecordingFinished, - RecordingStarted, - UploadCompleted, - VideoImported, - RecordingDeleted, -} - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] -#[serde(rename_all = "camelCase", tag = "type")] -pub enum Condition { - CaptureTargetIs { target: CaptureTargetKind }, - RecordingModeIs { mode: AutomationRecordingMode }, - DurationAtLeast { secs: f64 }, - DurationAtMost { secs: f64 }, - WindowTitleContains { pattern: String }, - OrganizationIs { id: String }, -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub enum CaptureTargetKind { - Display, - Window, - Area, -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub enum AutomationRecordingMode { - Studio, - Instant, -} - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] -#[serde(rename_all = "camelCase", tag = "type")] -pub enum Action { - CopyToClipboard { - #[serde(default)] - source: ClipboardSource, - }, - #[serde(rename_all = "camelCase")] - SaveToLocation { - dir: String, - #[serde(default)] - filename_template: Option, - }, - Export { - profile: ExportProfile, - #[serde(default)] - destination: ExportDestination, - }, - #[serde(rename_all = "camelCase")] - Upload { - #[serde(default)] - organization_id: Option, - #[serde(default = "default_true")] - copy_link: bool, - #[serde(default)] - open_in_browser: bool, - }, - RevealInFileManager, - OpenFile, - #[serde(rename_all = "camelCase")] - RunCommand { - program: String, - #[serde(default)] - args: Vec, - #[serde(default)] - cwd: Option, - #[serde(default)] - env: std::collections::HashMap, - #[serde(default)] - use_shell: bool, - }, - #[serde(rename_all = "camelCase")] - Webhook { - url: String, - #[serde(default = "default_post")] - method: String, - #[serde(default)] - headers: std::collections::HashMap, - #[serde(default)] - body_template: Option, - }, - RecognizeTextToClipboard, - #[serde(rename_all = "camelCase")] - Notify { - #[serde(default = "default_notify_title")] - title_template: String, - #[serde(default)] - body_template: String, - }, - OpenEditor, - SkipEditor, - ApplyPreset { - name: String, - }, - DeleteLocalFiles, -} - -fn default_post() -> String { - "POST".to_string() -} - -fn default_notify_title() -> String { - "Cap Automation".to_string() -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] -#[serde(rename_all = "camelCase")] -pub enum ClipboardSource { - #[default] - Raw, - Rendered, -} - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] -#[serde(rename_all = "camelCase")] -pub struct ExportProfile { - pub format: ExportFormat, - #[serde(default = "default_fps")] - pub fps: u32, - #[serde(default = "default_resolution")] - pub resolution_base: cap_project::XY, - #[serde(default)] - pub compression: Option, - #[serde(default)] - pub preset_name: Option, -} - -fn default_fps() -> u32 { - 30 -} - -fn default_resolution() -> cap_project::XY { - cap_project::XY { x: 1920, y: 1080 } -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub enum ExportFormat { - Mp4, - Gif, - Mov, -} - -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub enum AutomationExportCompression { - Maximum, - Social, - Web, - Potato, -} - -#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub enum ExportDestination { - #[default] - ProjectFolder, - CustomPath { - dir: String, - }, -} - -/// `load_automations` (`automation.rs:812-825`): the whole `automations` key, -/// defaulting -- with a log line -- when absent or unparseable. Same -/// overwrite-on-next-save semantics as the Tauri app. -pub fn automations() -> AutomationsStore { - let Some(value) = - cached_store(&tauri_store_path()).and_then(|store| store.get(AUTOMATIONS).cloned()) - else { - return AutomationsStore::default(); - }; - match serde_json::from_value(value) { - Ok(store) => store, - Err(error) => { - tracing::error!("deserializing the automations store: {error}"); - AutomationsStore::default() - } - } -} - -pub fn automations_value() -> Option { - cached_store(&tauri_store_path()).and_then(|store| store.get(AUTOMATIONS).cloned()) -} - -/// `set_automations` (`automation.rs:834-837`): `store.set("automations", -/// json!(store))` -- the key replaced wholesale, unlike the per-key -/// [`set_store_setting`] writes. -pub fn set_automations(automations: &AutomationsStore) -> bool { - let value = match serde_json::to_value(automations) { - Ok(value) => value, - Err(error) => { - tracing::error!("serializing the automations store: {error}"); - return false; - } - }; - set_store_value(AUTOMATIONS, value) -} - -/// The preset names `applyPreset` offers -- `presetsStore.createQuery()` in -/// automations.tsx, reading `store["presets"].presets[].name`. -pub fn preset_names() -> Vec { - store_section("presets") - .get("presets") - .and_then(Value::as_array) - .map(|presets| { - presets - .iter() - .filter_map(|preset| preset.get("name").and_then(Value::as_str)) - .map(str::to_string) - .collect() - }) - .unwrap_or_default() -} - -#[cfg(test)] -mod tests { - use super::*; - - struct TempStore { - path: PathBuf, - } - - impl TempStore { - fn new(name: &str, contents: Option<&str>) -> Self { - let path = std::env::temp_dir() - .join(format!("cap-gpui-store-test-{}-{name}", std::process::id())); - match contents { - Some(contents) => std::fs::write(&path, contents).unwrap(), - None => { - let _ = std::fs::remove_file(&path); - } - } - TEST_TAURI_STORE_PATH.with(|current| { - assert!(current.borrow().is_none()); - *current.borrow_mut() = Some(path.clone()); - }); - Self { path } - } - - fn read(&self) -> Value { - serde_json::from_slice(&std::fs::read(&self.path).unwrap()).unwrap() - } - } - - impl Drop for TempStore { - fn drop(&mut self) { - let _ = std::fs::remove_file(&self.path); - TEST_TAURI_STORE_PATH.with(|current| *current.borrow_mut() = None); - } - } - - fn load_general_settings_separately() -> GeneralSettings { - GeneralSettings::from_sections( - &store_section(GENERAL_SETTINGS), - &store_section(RECORDING_START_SAFETY), - &store_section(AUDIO_ENHANCEMENT), - ) - } - - #[test] - fn general_settings_load_matches_separate_section_reads() { - for contents in [ - None, - Some("invalid json"), - Some("[]"), - Some(r#"{"general_settings":["wrong shape"],"audio_enhancement":null}"#), - Some( - r#"{"general_settings":{"theme":"dark","maxFps":24},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false},"audio_enhancement":{"enabledByDefault":false}}"#, - ), - ] { - let _store = TempStore::new("general-settings-load", contents); - assert_eq!(GeneralSettings::load(), load_general_settings_separately()); - } - } - - fn backdate(path: &Path, modified: SystemTime) { - std::fs::File::options() - .write(true) - .open(path) - .unwrap() - .set_modified(modified) - .unwrap(); - } - - fn seconds_ago(seconds: u64) -> SystemTime { - SystemTime::now() - .checked_sub(Duration::from_secs(seconds)) - .unwrap() - } - - fn store_file_reads() -> usize { - TEST_STORE_FILE_READS.with(std::cell::Cell::get) - } - - #[test] - fn render_reads_parse_the_store_once_until_the_file_changes() { - let store = TempStore::new( - "cache-render-reads", - Some( - r#"{"auth":{"secret":{"token":"aaaa"},"organizations":[{"id":"1","name":"One"},{"id":"2","name":"Two"}]}}"#, - ), - ); - backdate(&store.path, seconds_ago(60)); - let before = store_file_reads(); - for _ in 0..100 { - assert!(auth_snapshot().signed_in()); - assert_eq!(auth_snapshot().organizations.len(), 2); - assert_eq!(auth_snapshot().organizations.len(), 2); - } - assert_eq!(store_file_reads() - before, 1); - - std::fs::write( - &store.path, - r#"{"auth":{"secret":{"token":"bbbb"},"organizations":[{"id":"1","name":"One"},{"id":"2","name":"Two"}]}}"#, - ) - .unwrap(); - backdate(&store.path, seconds_ago(30)); - assert_eq!(auth_snapshot().token.as_deref(), Some("bbbb")); - assert_eq!(auth_snapshot().token.as_deref(), Some("bbbb")); - assert_eq!(store_file_reads() - before, 2); - } - - #[test] - fn a_same_length_rewrite_within_the_cached_mtime_tick_is_still_read() { - let store = TempStore::new( - "cache-racy-rewrite", - Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#), - ); - assert!(has_completed_startup()); - let modified = std::fs::metadata(&store.path).unwrap().modified().unwrap(); - std::fs::write( - &store.path, - r#"{"general_settings":{"hasCompletedStartup":null}}"#, - ) - .unwrap(); - backdate(&store.path, modified); - assert!(!has_completed_startup()); - } - - #[cfg(unix)] - #[test] - fn an_in_place_rewrite_that_restores_the_old_mtime_is_still_read() { - let store = TempStore::new( - "cache-restored-mtime", - Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#), - ); - let modified = seconds_ago(60); - backdate(&store.path, modified); - assert!(has_completed_startup()); - std::thread::sleep(Duration::from_millis(50)); - std::fs::write( - &store.path, - r#"{"general_settings":{"hasCompletedStartup":null}}"#, - ) - .unwrap(); - backdate(&store.path, modified); - assert!(!has_completed_startup()); - } - - #[test] - fn a_store_that_stops_parsing_or_disappears_is_not_served_from_the_cache() { - let store = TempStore::new( - "cache-broken", - Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#), - ); - backdate(&store.path, seconds_ago(60)); - assert!(has_completed_startup()); - std::fs::write(&store.path, "{ not json").unwrap(); - backdate(&store.path, seconds_ago(30)); - assert!(!has_completed_startup()); - assert!(!set_store_setting( - GENERAL_SETTINGS, - "hideDockIcon", - Value::Bool(true) - )); - std::fs::remove_file(&store.path).unwrap(); - assert!(!has_completed_startup()); - assert!(store_section(GENERAL_SETTINGS).is_empty()); - } - - #[test] - fn every_store_writer_is_visible_to_the_next_cached_read() { - let store = TempStore::new("cache-writers", Some("{}")); - let settle = || { - backdate(&store.path, seconds_ago(60)); - store_section(GENERAL_SETTINGS); - }; - - settle(); - assert!(set_store_setting( - GENERAL_SETTINGS, - "hasCompletedStartup", - Value::Bool(true) - )); - assert!(has_completed_startup()); - assert_eq!(GeneralSettings::load(), load_general_settings_separately()); - - settle(); - assert!(set_has_completed_onboarding(true)); - assert!(has_completed_onboarding()); - - settle(); - assert!(set_store_value( - "presets", - serde_json::json!({"presets": [{"name": "Mine", "config": {}}], "default": 0}) - )); - assert_eq!(preset_names(), vec!["Mine".to_string()]); - - settle(); - let mut presets = crate::presets::PresetsStore::load(); - presets.create("Second".into(), serde_json::json!({}), true); - assert!(presets.save()); - assert_eq!(crate::presets::PresetsStore::load(), presets); - - settle(); - assert!(update_store_section(RECORDING_SETTINGS, |section| { - section.insert("mode".into(), Value::String("studio".into())); - true - })); - assert_eq!(recording_mode_slug().as_deref(), Some("studio")); - - settle(); - assert!(set_auth(Some( - serde_json::json!({"secret": {"token": "abc"}, "plan": {"upgraded": true}}) - ))); - assert!(auth_snapshot().signed_in()); - assert!(auth_snapshot().is_upgraded()); - - settle(); - assert!(set_auth(None)); - assert!(!auth_snapshot().signed_in()); - - settle(); - let mut hotkeys = Map::new(); - hotkeys.insert("startStudioRecording".into(), serde_json::json!({})); - assert!(set_hotkeys_raw(&hotkeys)); - assert_eq!(hotkeys_raw(), hotkeys); - - settle(); - let rules = AutomationsStore { - version: 7, - ..AutomationsStore::default() - }; - assert!(set_automations(&rules)); - assert_eq!(automations().version, 7); - - settle(); - assert!(set_store_setting( - GENERAL_SETTINGS, - "enableNotifications", - Value::Bool(false) - )); - assert!(!notification_sounds_enabled()); - } - - #[test] - #[ignore] - fn benchmark_cached_store_section_reads() { - use std::{hint::black_box, time::Instant}; - - let config = crate::presets::preset_config(&cap_project::ProjectConfiguration::default()); - let contents = serde_json::to_string_pretty(&serde_json::json!({ - "general_settings": {"theme": "dark", "maxFps": 60, "hasCompletedStartup": true}, - "recording_start_safety": {"confirmBeforeRecordingWithoutMicrophone": false}, - "audio_enhancement": {"enabledByDefault": true}, - "auth": { - "secret": {"api_key": "k".repeat(64)}, - "plan": {"upgraded": true, "manual": false}, - "organizations": [{"id": "1", "name": "One"}, {"id": "2", "name": "Two"}] - }, - "presets": {"presets": [{"name": "Preset", "config": config}], "default": null}, - "teleprompter": {"script": "A recording script. ".repeat(60)} - })) - .unwrap(); - let store = TempStore::new("cache-benchmark", Some(&contents)); - backdate(&store.path, seconds_ago(60)); - let uncached = || { - read_store(&tauri_store_path()) - .and_then(|mut store| store.remove("auth")) - .and_then(|value| match value { - Value::Object(map) => Some(map), - _ => None, - }) - .unwrap_or_default() - }; - assert_eq!(uncached(), store_section("auth")); - let measure = |read: &dyn Fn() -> Map| { - let start = Instant::now(); - for _ in 0..1000 { - black_box(read()); - } - start.elapsed().as_secs_f64() * 1_000_000.0 / 1000.0 - }; - let mut before = Vec::new(); - let mut after = Vec::new(); - for _ in 0..9 { - before.push(measure(&uncached)); - after.push(measure(&|| store_section("auth"))); - } - before.sort_by(f64::total_cmp); - after.sort_by(f64::total_cmp); - eprintln!( - "store_bytes={} uncached_median_us={:.3} cached_median_us={:.3} uncached_runs_us={before:?} cached_runs_us={after:?}", - contents.len(), - before[4], - after[4] - ); - } - - #[test] - #[ignore] - fn benchmark_general_settings_load() { - use std::{hint::black_box, time::Instant}; - - for preset_count in [0, 10, 50] { - let config = - crate::presets::preset_config(&cap_project::ProjectConfiguration::default()); - let presets: Vec<_> = (0..preset_count) - .map(|index| serde_json::json!({"name": format!("Preset {index}"), "config": config})) - .collect(); - let contents = serde_json::to_string_pretty(&serde_json::json!({ - "general_settings": {"theme": "dark", "maxFps": 60}, - "recording_start_safety": {"confirmBeforeRecordingWithoutMicrophone": false}, - "audio_enhancement": {"enabledByDefault": true}, - "presets": {"presets": presets, "default": null}, - "teleprompter": {"script": "A recording script. ".repeat(100)} - })) - .unwrap(); - let _store = TempStore::new("general-settings-benchmark", Some(&contents)); - assert_eq!(GeneralSettings::load(), load_general_settings_separately()); - let measure = |load: fn() -> GeneralSettings| { - let start = Instant::now(); - for _ in 0..200 { - black_box(load()); - } - start.elapsed().as_secs_f64() * 1_000_000.0 / 200.0 - }; - let mut separate = Vec::new(); - let mut current = Vec::new(); - for round in 0..9 { - if round % 2 == 0 { - separate.push(measure(load_general_settings_separately)); - current.push(measure(GeneralSettings::load)); - } else { - current.push(measure(GeneralSettings::load)); - separate.push(measure(load_general_settings_separately)); - } - } - separate.sort_by(f64::total_cmp); - current.sort_by(f64::total_cmp); - eprintln!( - "presets={preset_count} bytes={} separate_median_us={:.3} current_median_us={:.3} separate_runs_us={separate:?} current_runs_us={current:?}", - contents.len(), - separate[4], - current[4] - ); - } - } - - #[test] - fn section_updates_merge_into_disk_state_and_can_decline() { - let store = TempStore::new( - "section-update", - Some(r#"{"auth":{"user_id":"a","plan":null},"general_settings":{"x":1}}"#), - ); - - assert!(update_store_section("auth", |auth| { - auth.insert("organizations".into(), serde_json::json!([])); - true - })); - let written = store.read(); - assert_eq!(written["auth"]["user_id"], "a"); - assert_eq!(written["auth"]["organizations"], serde_json::json!([])); - assert_eq!(written["general_settings"]["x"], 1); - - assert!(!update_store_section("auth", |auth| { - auth.insert("user_id".into(), "b".into()); - false - })); - assert_eq!(store.read()["auth"]["user_id"], "a"); - } - - #[test] - fn recordings_folder_changes_remember_previous_locations() { - let store = TempStore::new( - "recordings-folder-history", - Some( - r#"{"general_settings":{"recordingsPath":"old","previousRecordingsPaths":["older","new"],"maxFps":30,"future":true},"auth":{"user_id":"keep"}}"#, - ), - ); - - assert!(set_recordings_path(Some("new"))); - let reloaded = store_section(GENERAL_SETTINGS); - assert_eq!(reloaded["recordingsPath"], "new"); - assert_eq!( - strings_at(&reloaded, "previousRecordingsPaths"), - ["older", "old"] - ); - - assert!(set_recordings_path(Some("new"))); - assert_eq!(store_section(GENERAL_SETTINGS), reloaded); - - assert!(set_recordings_path(None)); - let reloaded = store_section(GENERAL_SETTINGS); - assert_eq!(reloaded["recordingsPath"], Value::Null); - assert_eq!( - strings_at(&reloaded, "previousRecordingsPaths"), - ["older", "old", "new"] - ); - - assert!(set_recordings_path(Some("old"))); - let reloaded = store_section(GENERAL_SETTINGS); - assert_eq!(reloaded["recordingsPath"], "old"); - assert_eq!( - strings_at(&reloaded, "previousRecordingsPaths"), - ["older", "new"] - ); - assert_eq!(reloaded["maxFps"], 30); - assert_eq!(store.read()[GENERAL_SETTINGS]["future"], true); - assert_eq!(store.read()["auth"]["user_id"], "keep"); - } - - #[test] - fn recordings_folder_changes_limit_history_without_duplicates() { - let _store = TempStore::new("recordings-folder-history-limit", None); - for index in 0..25 { - assert!(set_recordings_path(Some(&format!("folder-{index}")))); - } - let expected = (4..24) - .map(|index| format!("folder-{index}")) - .collect::>(); - assert_eq!( - strings_at(&store_section(GENERAL_SETTINGS), "previousRecordingsPaths"), - expected - ); - - assert!(set_recordings_path(Some("folder-23"))); - assert!(set_recordings_path(Some("folder-24"))); - assert_eq!( - strings_at(&store_section(GENERAL_SETTINGS), "previousRecordingsPaths"), - expected - ); - } - - #[test] - fn recordings_folder_changes_merge_concurrent_updates() { - let store = TempStore::new( - "recordings-folder-concurrent", - Some(r#"{"general_settings":{"recordingsPath":"original","maxFps":30}}"#), - ); - let barrier = Arc::new(std::sync::Barrier::new(8)); - let workers = (0..8) - .map(|index| { - let path = store.path.clone(); - let barrier = barrier.clone(); - std::thread::spawn(move || { - set_test_tauri_store_path(Some(path)); - barrier.wait(); - let saved = set_recordings_path(Some(&format!("folder-{index}"))); - set_test_tauri_store_path(None); - saved - }) - }) - .collect::>(); - for worker in workers { - assert!(worker.join().unwrap()); - } - let reloaded = store_section(GENERAL_SETTINGS); - let mut paths = strings_at(&reloaded, "previousRecordingsPaths"); - assert_eq!(paths.remove(0), "original"); - paths.push(opt_string_at(&reloaded, "recordingsPath").unwrap()); - paths.sort(); - assert_eq!( - paths, - (0..8) - .map(|index| format!("folder-{index}")) - .collect::>() - ); - assert_eq!(reloaded["maxFps"], 30); - } - - #[test] - fn recordings_folder_changes_preserve_preferences_when_saving_fails() { - let contents = r#"{"general_settings":{"recordingsPath":"old","maxFps":30}}"#; - let store = TempStore::new("recordings-folder-write-failure", Some(contents)); - let staged = store.path.with_extension("gpui-tmp"); - std::fs::create_dir(&staged).unwrap(); - let saved = set_recordings_path(Some("new")); - std::fs::remove_dir(staged).unwrap(); - - assert!(!saved); - assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents); - assert_eq!(store_section(GENERAL_SETTINGS)["recordingsPath"], "old"); - } - - #[test] - fn recordings_folder_changes_preserve_unreadable_stores() { - let store = TempStore::new("recordings-folder-corrupt", Some("not JSON")); - assert!(!set_recordings_path(Some("new"))); - assert!(!set_recordings_path(None)); - assert_eq!(std::fs::read_to_string(&store.path).unwrap(), "not JSON"); - } - - #[test] - fn notification_sounds_follow_tauri_settings_without_writing() { - for (contents, enabled) in [ - (None, false), - (Some("{}"), false), - (Some("invalid json"), false), - (Some(r#"{"general_settings":{}}"#), true), - ( - Some(r#"{"general_settings":{"enableNotifications":false}}"#), - false, - ), - ( - Some(r#"{"general_settings":{"enableNotifications":true}}"#), - true, - ), - ] { - let store = TempStore::new("notification-sounds", contents); - assert_eq!(notification_sounds_enabled(), enabled); - assert_eq!( - std::fs::read_to_string(&store.path).ok().as_deref(), - contents - ); - } - } - - #[test] - fn device_format_preferences_use_tauri_keys_and_preserve_other_fields() { - use cap_recording::feeds::{ - camera::CameraDeviceSettings, microphone::MicrophoneDeviceSettings, - }; - let store = TempStore::new( - "device-format-preferences", - Some( - r#"{"recording_settings":{"mode":"studio","cameraDeviceSettings":{"device:camera-1":{"width":1280,"height":720,"frameRate":30,"future":7},"model:vendor:product":{"frameRate":60},"device:other":{"width":640}},"microphoneDeviceSettings":{"Desk":{"sampleRate":48000,"channels":1,"future":9},"Other":{"channels":2}},"future":{"keep":true}},"unrelated":{"keep":true}}"#, - ), - ); - let before = store.read(); - let model = cap_camera::ModelID::try_from("vendor:product".to_string()).unwrap(); - let snapshot = RecordingDeviceSettings { - camera: RecordingDeviceSettings::for_camera("camera-1", Some(&model)), - microphone: RecordingDeviceSettings::for_microphone("Desk"), - }; - assert_eq!(snapshot.camera.unwrap().frame_rate, Some(30.)); - assert_eq!( - RecordingDeviceSettings::for_camera("replugged", Some(&model)) - .unwrap() - .frame_rate, - Some(60.) - ); - let camera = CameraDeviceSettings { - width: Some(1920), - height: Some(1080), - frame_rate: Some(30.), - }; - assert!(set_camera_device_settings("camera-1", Some(&model), camera)); - assert!(set_microphone_device_settings( - "Desk", - MicrophoneDeviceSettings { - sample_rate: Some(44100), - channels: Some(2) - } - )); - assert_eq!( - RecordingDeviceSettings::for_camera("camera-1", Some(&model)), - Some(camera) - ); - assert_eq!( - RecordingDeviceSettings::for_camera("replugged", Some(&model)), - Some(camera) - ); - assert_eq!(snapshot.camera.unwrap().width, Some(1280)); - assert_eq!(snapshot.microphone.unwrap().sample_rate, Some(48000)); - let after = store.read(); - assert_eq!( - after[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:camera-1"]["future"], - 7 - ); - assert_eq!( - after[RECORDING_SETTINGS]["microphoneDeviceSettings"]["Desk"]["future"], - 9 - ); - assert_eq!( - after[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:other"], - before[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:other"] - ); - assert_eq!( - after[RECORDING_SETTINGS]["microphoneDeviceSettings"]["Other"], - before[RECORDING_SETTINGS]["microphoneDeviceSettings"]["Other"] - ); - assert_eq!(after["unrelated"], before["unrelated"]); - assert_eq!( - after[RECORDING_SETTINGS]["future"], - before[RECORDING_SETTINGS]["future"] - ); - assert!(set_camera_device_settings( - "camera-1", - Some(&model), - CameraDeviceSettings::default() - )); - assert!( - store.read()[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:camera-1"] - .get("frameRate") - .is_none() - ); - assert_eq!( - store.read()[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:camera-1"]["future"], - 7 - ); - } - - #[test] - fn device_format_save_failure_does_not_replace_unreadable_preferences() { - let store = TempStore::new("device-format-invalid-store", Some("{broken")); - let before = std::fs::read(&store.path).unwrap(); - assert!(!set_microphone_device_settings("Desk", Default::default())); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - } - - #[test] - fn recording_inputs_round_trip_without_replacing_unknown_settings() { - use cap_recording::feeds::camera::DeviceOrModelID; - - let store = TempStore::new( - "recording-inputs-round-trip", - Some( - r#"{"recording_settings":{"mode":"studio","cameraId":{"ModelID":"vendor:product"},"micName":"Desk microphone","cameraDeviceSettings":{"model:vendor:product":{"frameRate":30}},"unknown":{"keep":true}},"other":{"keep":[1,2]}}"#, - ), - ); - let original = store.read(); - let model = cap_camera::ModelID::try_from("vendor:product".to_string()).unwrap(); - assert_eq!( - RecordingInputSettings::load(), - RecordingInputSettings { - camera_id: Some(DeviceOrModelID::ModelID(model)), - microphone_name: Some("Desk microphone".to_string()), - } - ); - - let camera = DeviceOrModelID::DeviceID("replacement-camera".to_string()); - assert!(set_recording_camera_id(Some(&camera))); - assert!(set_recording_microphone_name(Some( - "Replacement microphone" - ))); - assert_eq!( - RecordingInputSettings::load(), - RecordingInputSettings { - camera_id: Some(camera), - microphone_name: Some("Replacement microphone".to_string()), - } - ); - - assert!(set_recording_camera_id(None)); - assert!(set_recording_microphone_name(None)); - assert_eq!( - RecordingInputSettings::load(), - RecordingInputSettings::default() - ); - let mut expected = original; - expected[RECORDING_SETTINGS]["cameraId"] = Value::Null; - expected[RECORDING_SETTINGS]["micName"] = Value::Null; - assert_eq!(store.read(), expected); - } - - #[test] - fn system_audio_restores_and_saves_without_touching_devices() { - let store = TempStore::new( - "recording-inputs-system-audio", - Some(r#"{"recording_settings":{"systemAudio":true,"micName":"Desk"}}"#), - ); - assert!(recording_system_audio()); - - assert!(set_recording_system_audio(false)); - assert!(!recording_system_audio()); - assert_eq!(store.read()[RECORDING_SETTINGS]["systemAudio"], false); - assert_eq!(store.read()[RECORDING_SETTINGS]["micName"], "Desk"); - drop(store); - - let _store = TempStore::new( - "recording-inputs-system-audio-malformed", - Some(r#"{"recording_settings":{"systemAudio":"yes"}}"#), - ); - assert!(!recording_system_audio()); - } - - #[test] - fn recording_inputs_keep_saved_identity_without_device_enumeration() { - let store = TempStore::new( - "recording-inputs-absent-device", - Some( - r#"{"recording_settings":{"cameraId":{"DeviceID":"unplugged-camera"},"micName":"Unplugged microphone"}}"#, - ), - ); - let before = std::fs::read(&store.path).unwrap(); - let first = RecordingInputSettings::load(); - assert_eq!( - first.microphone_name.as_deref(), - Some("Unplugged microphone") - ); - assert_eq!( - first.camera_id, - Some(cap_recording::feeds::camera::DeviceOrModelID::DeviceID( - "unplugged-camera".into() - )) - ); - assert_eq!(RecordingInputSettings::load(), first); - assert_eq!(std::fs::read(&store.path).unwrap(), before); - } - - #[test] - fn recording_inputs_malformed_camera_does_not_panic_or_clear_other_settings() { - let store = TempStore::new( - "recording-inputs-malformed-model", - Some( - r#"{"recording_settings":{"cameraId":{"ModelID":"missing-separator"},"micName":"Valid microphone","future":42}}"#, - ), - ); - let before = store.read(); - let settings = RecordingInputSettings::load(); - assert!(settings.camera_id.is_none()); - assert_eq!( - settings.microphone_name.as_deref(), - Some("Valid microphone") - ); - assert!(set_recording_microphone_name(None)); - let after = store.read(); - assert_eq!( - after[RECORDING_SETTINGS]["cameraId"], - before[RECORDING_SETTINGS]["cameraId"] - ); - assert_eq!(after[RECORDING_SETTINGS]["future"], 42); - } - - #[test] - fn concurrent_store_writers_preserve_each_others_changes() { - let contents = serde_json::json!({"unknown": "x".repeat(64 * 1024)}).to_string(); - let store = TempStore::new("concurrent-writers", Some(&contents)); - let barrier = std::sync::Arc::new(std::sync::Barrier::new(16)); - let workers = (0..16) - .map(|index| { - let path = store.path.clone(); - let barrier = barrier.clone(); - std::thread::spawn(move || { - set_test_tauri_store_path(Some(path)); - barrier.wait(); - let saved = match index { - 0..=3 => set_store_setting( - GENERAL_SETTINGS, - &format!("setting-{index}"), - Value::from(index), - ), - 4..=7 => set_store_value(&format!("section-{index}"), Value::from(index)), - 8..=11 => set_microphone_device_settings( - &format!("microphone-{index}"), - cap_recording::feeds::microphone::MicrophoneDeviceSettings { - sample_rate: Some(48000), - channels: Some(2), - }, - ), - _ => set_automations(&AutomationsStore::default()), - }; - set_test_tauri_store_path(None); - saved - }) - }) - .collect::>(); - let results = workers - .into_iter() - .map(|worker| worker.join().unwrap()) - .collect::>(); - assert!(results.into_iter().all(|saved| saved)); - - let after = store.read(); - assert_eq!(after["unknown"], "x".repeat(64 * 1024)); - for index in 0..4 { - assert_eq!(after[GENERAL_SETTINGS][format!("setting-{index}")], index); - } - for index in 4..8 { - assert_eq!(after[format!("section-{index}")], index); - } - for index in 8..12 { - assert_eq!( - after[RECORDING_SETTINGS]["microphoneDeviceSettings"] - [format!("microphone-{index}")], - serde_json::json!({"sampleRate": 48000, "channels": 2}) - ); - } - assert_eq!( - after[AUTOMATIONS], - serde_json::to_value(AutomationsStore::default()).unwrap() - ); - } - - #[test] - fn temporary_stores_are_thread_local_without_mutating_process_environment() { - let original = std::env::var_os("CAP_GPUI_TAURI_STORE"); - let barrier = std::sync::Arc::new(std::sync::Barrier::new(4)); - let workers = (0..4) - .map(|index| { - let barrier = barrier.clone(); - let original = original.clone(); - std::thread::spawn(move || { - let store = TempStore::new(&format!("thread-local-{index}"), None); - assert_eq!(super::tauri_store_path(), store.path); - assert!(super::set_store_setting( - GENERAL_SETTINGS, - "maxFps", - Value::from(24 + index) - )); - barrier.wait(); - assert_eq!(GeneralSettings::load().max_fps, 24 + index); - assert_eq!(std::env::var_os("CAP_GPUI_TAURI_STORE"), original); - }) - }) - .collect::>(); - - for worker in workers { - worker.join().unwrap(); - } - assert_eq!(std::env::var_os("CAP_GPUI_TAURI_STORE"), original); - } - - /// The compatibility contract: writing one setting must leave every key - /// this app has never heard of exactly where it was -- other sections, - /// other `general_settings` keys, and nested objects inside them. - #[test] - fn writing_one_setting_preserves_unknown_keys() { - let store = TempStore::new( - "unknown-keys", - Some( - r#"{ - "auth": { "secret": { "token": "keep-me" }, "user_id": "u_1" }, - "presets": { "presets": [], "default": 0 }, - "general_settings": { - "hideDockIcon": false, - "instanceId": "fe9cd6a0-fbb0-49b0-b321-5593cb3d4190", - "cameraWindowPositionsByMonitorName": { "Built-in Retina Display": { "x": 1.0, "y": 2.0 } }, - "someSettingFromANewerBuild": [1, 2, 3] - }, - "a_migration_flag_v1": true -}"#, - ), - ); - - assert!(super::set_store_setting( - GENERAL_SETTINGS, - "hideDockIcon", - Value::Bool(true) - )); - - let after = store.read(); - // The touched key changed... - assert_eq!(after["general_settings"]["hideDockIcon"], Value::Bool(true)); - // ...and nothing else did. - assert_eq!(after["auth"]["secret"]["token"], "keep-me"); - assert_eq!(after["auth"]["user_id"], "u_1"); - assert_eq!(after["presets"]["default"], 0); - assert_eq!(after["a_migration_flag_v1"], Value::Bool(true)); - assert_eq!( - after["general_settings"]["instanceId"], - "fe9cd6a0-fbb0-49b0-b321-5593cb3d4190" - ); - assert_eq!( - after["general_settings"]["cameraWindowPositionsByMonitorName"]["Built-in Retina Display"] - ["x"], - 1.0 - ); - assert_eq!( - after["general_settings"]["someSettingFromANewerBuild"], - serde_json::json!([1, 2, 3]) - ); - - // A second write against a store that already round-tripped once must - // still see the first one -- i.e. the read half is reading what the - // write half wrote. - assert!(super::set_store_setting( - RECORDING_START_SAFETY, - "confirmBeforeRecordingWithoutMicrophone", - Value::Bool(false) - )); - assert!(super::set_studio_sound_by_default(false)); - let settings = GeneralSettings::load(); - assert!(settings.hide_dock_icon); - assert!(!settings.confirm_without_microphone); - assert!(!settings.studio_sound_by_default); - // The section it created did not disturb the others. - assert_eq!(store.read()["auth"]["user_id"], "u_1"); - } - - #[test] - fn ui_language_round_trips_without_touching_other_keys() { - let store = TempStore::new( - "ui-language", - Some( - r#"{"general_settings":{"theme":"dark","uiLanguage":"pt-BR","futureSetting":{"keep":[1]}}}"#, - ), - ); - assert_eq!( - GeneralSettings::load().ui_language.as_deref(), - Some("pt-BR") - ); - assert!(super::set_store_setting( - GENERAL_SETTINGS, - crate::i18n::SETTING_KEY, - Value::from("fr") - )); - let after = store.read(); - assert_eq!(after["general_settings"]["uiLanguage"], "fr"); - assert_eq!(after["general_settings"]["theme"], "dark"); - assert_eq!( - after["general_settings"]["futureSetting"], - serde_json::json!({ "keep": [1] }) - ); - assert_eq!(GeneralSettings::load().ui_language.as_deref(), Some("fr")); - } - - /// A store that does not parse is someone's settings in a state we do not - /// understand. Writing a fresh object over it would delete their auth - /// token and every preset. - #[test] - fn a_corrupt_store_is_never_overwritten() { - for contents in ["{ this is not json", r#"{"auth":"token""#, "null", "[]"] { - let store = TempStore::new("corrupt", Some(contents)); - assert!(!super::set_store_setting( - GENERAL_SETTINGS, - "hideDockIcon", - Value::Bool(true) - )); - assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents); - } - } - - /// A zero-filled store (NTFS after a power loss) or one cut off mid-object - /// blocked onboarding forever. Onboarding sets the unreadable bytes aside - /// and saves its progress; every other writer still refuses. - #[test] - fn onboarding_progress_sets_aside_an_unparseable_store() { - for contents in [ - "\0\0\0\0\0\0\0\0", - "{ this is not json", - r#"{"auth":"token""#, - "null", - "[]", - ] { - let store = TempStore::new("unparseable-onboarding", Some(contents)); - assert!(!set_store_setting( - GENERAL_SETTINGS, - "hideDockIcon", - Value::Bool(true) - )); - assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents); - assert!(!has_completed_startup()); - - assert!(set_has_completed_startup(true)); - assert!(set_has_completed_onboarding(true)); - assert!(has_completed_startup()); - assert!(has_completed_onboarding()); - assert_eq!( - store.read(), - serde_json::json!({ - "general_settings": { - "hasCompletedStartup": true, - "hasCompletedOnboarding": true - } - }) - ); - - let directory = store.path.parent().unwrap(); - let prefix = format!( - "{}.unreadable-", - store.path.file_name().unwrap().to_string_lossy() - ); - let kept = std::fs::read_dir(directory) - .unwrap() - .filter_map(Result::ok) - .filter(|entry| entry.file_name().to_string_lossy().starts_with(&prefix)) - .map(|entry| entry.path()) - .collect::>(); - assert_eq!(kept.len(), 1, "{kept:?}"); - assert_eq!(std::fs::read_to_string(&kept[0]).unwrap(), contents); - std::fs::remove_file(&kept[0]).unwrap(); - } - } - - #[test] - fn onboarding_progress_leaves_an_unreadable_store_alone() { - let store = TempStore::new("unreadable-onboarding", None); - std::fs::create_dir_all(&store.path).unwrap(); - assert!(!set_has_completed_startup(true)); - assert!(store.path.is_dir()); - std::fs::remove_dir(&store.path).unwrap(); - } - - #[test] - fn onboarding_progress_recovers_an_empty_store() { - for contents in ["", " \t\r\n"] { - let store = TempStore::new("empty-onboarding", Some(contents)); - assert_eq!(GeneralSettings::load(), GeneralSettings::default()); - assert!(!has_completed_startup()); - assert!(!has_completed_onboarding()); - - assert!(set_has_completed_startup(true)); - assert!(has_completed_startup()); - assert!(set_has_completed_onboarding(true)); - assert!(has_completed_onboarding()); - assert_eq!( - store.read(), - serde_json::json!({ - "general_settings": { - "hasCompletedStartup": true, - "hasCompletedOnboarding": true - } - }) - ); - } - } - - /// Every value the page can read back has to survive the round trip, and - /// an unknown enum value must degrade to that one row's default rather - /// than blanking the page. - #[test] - fn typed_reads_match_the_store_shape() { - let _store = TempStore::new( - "typed-reads", - Some( - r#"{ - "general_settings": { - "theme": "dark", - "studioRecordingQuality": "ultra", - "postDeletionBehaviour": "somethingFromTheFuture", - "recordingCountdown": 5, - "custom_cursor_capture2": false, - "defaultZoomAmount": 2.5, - "maxFps": 120, - "serverUrl": "https://cap.example.com", - "excludedWindows": [{ "bundleIdentifier": null, "ownerName": null, "windowTitle": "Cap" }] - } -}"#, - ), - ); - - let settings = GeneralSettings::load(); - assert_eq!(settings.theme, AppTheme::Dark); - assert_eq!(settings.studio_recording_quality, StudioQuality::Ultra); - // Unknown value -> this row's default, everything else intact. - assert_eq!( - settings.post_deletion_behaviour, - PostDeletionBehaviour::DoNothing - ); - assert_eq!(settings.recording_countdown, Some(5)); - assert!(!settings.custom_cursor_capture); - assert_eq!(settings.default_zoom_amount, Some(2.5)); - assert_eq!(settings.new_zoom_amount(), 2.5); - assert_eq!(settings.max_fps, 120); - assert_eq!(settings.server_url, "https://cap.example.com"); - assert_eq!(settings.excluded_windows.len(), 1); - assert_eq!(settings.excluded_windows[0].primary_label(), "Cap"); - // Absent keys take the page's defaults, not the struct's zero values. - assert!(settings.enable_notifications); - assert!(settings.crash_recovery_recording); - assert!(settings.confirm_without_microphone); - assert!(settings.studio_sound_by_default); - assert_eq!(settings.instant_mode_max_resolution, 1920); - assert_eq!(GeneralSettings::default().new_zoom_amount(), 1.5); - } - - /// The teleprompter's own section: defaults when absent, and a per-key - /// write that leaves the rest of the section (and the rest of the store) - /// alone -- the window writes only the fields the user touched. - #[test] - fn the_teleprompter_section_round_trips_per_key() { - let store = TempStore::new( - "teleprompter", - Some(r#"{ "auth": { "user_id": "u_1" }, "teleprompter": { "script": "hello" } }"#), - ); - - let loaded = TeleprompterState::load(); - assert_eq!(loaded.script, "hello"); - // Everything the section does not carry takes `teleprompterDefaults`. - assert_eq!(loaded.font_size, 30.); - assert_eq!(loaded.words_per_minute, 150); - assert_eq!(loaded.line_height, 1.5); - assert!(loaded.show_cue_markers); - assert!(!loaded.mirror); - assert_eq!(loaded.window_opacity_percent, 92); - - let mut next = loaded.clone(); - next.script = "hello there".into(); - next.font_size = 34.; - for key in [TeleprompterState::SCRIPT, TeleprompterState::FONT_SIZE] { - assert!(super::set_store_setting( - TELEPROMPTER, - key, - next.value_for(key) - )); - } - - let after = store.read(); - assert_eq!(after["teleprompter"]["script"], "hello there"); - assert_eq!(after["teleprompter"]["fontSize"], 34.0); - // The two untouched keys were never written, and the rest of the store - // is where it was. - assert!(after["teleprompter"].get("mirror").is_none()); - assert_eq!(after["auth"]["user_id"], "u_1"); - - let reloaded = TeleprompterState::load(); - assert_eq!(reloaded.script, "hello there"); - assert_eq!(reloaded.font_size, 34.); - assert!(reloaded.show_cue_markers); - } - - /// An absent store is an empty one -- a fresh install must not be a write - /// failure, and the file appears on the first change. - #[test] - fn a_missing_store_is_created() { - let _store = TempStore::new("missing", None); - - assert_eq!(GeneralSettings::load(), GeneralSettings::default()); - assert!(super::set_store_setting( - GENERAL_SETTINGS, - "enableTelemetry", - Value::Bool(false) - )); - assert!(!GeneralSettings::load().enable_telemetry); - } - - #[test] - fn bundled_resource_paths_follow_the_installed_executable() { - let executable = std::path::Path::new("/Applications/Cap.app/Contents/MacOS/cap-gpui"); - let override_dir = std::path::Path::new("/tmp/cap-resources"); - - assert_eq!( - super::bundled_resource_dirs_for(Some(executable), Some(override_dir)), - vec![ - override_dir.to_path_buf(), - std::path::PathBuf::from("/Applications/Cap.app/Contents/Resources"), - std::path::PathBuf::from("/Applications/Cap.app/Contents/MacOS/resources"), - std::path::PathBuf::from("/Applications/Cap.app/Contents/MacOS"), - ] - ); - - let executable = std::path::Path::new("/opt/cap/cap-gpui"); - assert_eq!( - super::bundled_resource_dirs_for(Some(executable), None), - vec![ - std::path::PathBuf::from("/opt/cap/resources"), - std::path::PathBuf::from("/opt/cap"), - ] - ); - - let executable = std::path::Path::new("/usr/bin/cap-gpui"); - assert_eq!( - super::bundled_resource_dirs_for(Some(executable), None), - vec![ - std::path::PathBuf::from("/usr/lib/cap"), - std::path::PathBuf::from("/usr/bin/resources"), - std::path::PathBuf::from("/usr/bin"), - ] - ); - } - - /// `normalizeTranscriptionHints`: NULs stripped, whitespace trimmed, - /// empties and duplicates dropped, first occurrence wins. - #[test] - fn transcription_hints_normalize_like_the_web_app() { - assert_eq!( - normalize_transcription_hints([" Cap ", "Cap", "", "\0", "Type\0Script", "cap"]), - ["Cap", "TypeScript", "cap"] - ); - - let _store = TempStore::new( - "hints", - Some(r#"{ "general_settings": { "transcriptionHints": ["Cap", "Acme"] } }"#), - ); - assert_eq!(transcription_hints(), ["Cap", "Acme"]); - } - - /// An absent `transcriptionHints` key shows the four defaults - /// (`createDefaultGeneralSettings`), an empty array shows none. - #[test] - fn transcription_hints_default_only_when_absent() { - { - let _store = TempStore::new("hints-absent", Some(r#"{ "general_settings": {} }"#)); - assert_eq!(transcription_hints(), DEFAULT_TRANSCRIPTION_HINTS); - } - { - let _store = TempStore::new( - "hints-empty", - Some(r#"{ "general_settings": { "transcriptionHints": [] } }"#), - ); - assert!(transcription_hints().is_empty()); - } - } - - /// The hotkeys map round-trips through the exact JSON the Tauri app's - /// `HotkeysStore` writes, and unknown actions survive a rewrite. - #[test] - fn hotkeys_round_trip_the_tauri_shape() { - let store = TempStore::new( - "hotkeys", - Some( - r#"{ "hotkeys": { "hotkeys": { - "stopRecording": { "code": "KeyS", "meta": true, "ctrl": false, "alt": false, "shift": true }, - "someFutureAction": { "code": "F13", "meta": false, "ctrl": false, "alt": false, "shift": false, "extra": 1 } -} } }"#, - ), - ); - - let mut map = hotkeys_raw(); - let stop = hotkey_from_value(map.get("stopRecording").unwrap()).unwrap(); - assert_eq!(stop.code, "KeyS"); - assert!(stop.meta && stop.shift && !stop.ctrl && !stop.alt); - - map.insert( - "screenshotArea".to_string(), - serde_json::to_value(Hotkey { - code: "Digit4".into(), - meta: true, - ctrl: false, - alt: false, - shift: true, - }) - .unwrap(), - ); - assert!(set_hotkeys_raw(&map)); - - let after = store.read(); - assert_eq!( - after["hotkeys"]["hotkeys"]["screenshotArea"]["code"], - "Digit4" - ); - // The unknown action's binding -- including the field this build does - // not model -- is still there. - assert_eq!(after["hotkeys"]["hotkeys"]["someFutureAction"]["extra"], 1); - } - - #[test] - fn the_marker_shortcut_default_is_offered_once() { - let store = TempStore::new("marker-seed", Some(r#"{ "hotkeys": { "hotkeys": {} } }"#)); - seed_recording_marker_hotkey(); - let default = default_recording_marker_hotkey(); - assert_eq!( - hotkey_from_value(&hotkeys_raw()[RECORDING_MARKER_HOTKEY]), - Some(default.clone()) - ); - assert_eq!( - store.read()["hotkeys"][RECORDING_MARKER_HOTKEY_SEEDED], - true - ); - - let mut cleared = hotkeys_raw(); - cleared.remove(RECORDING_MARKER_HOTKEY); - assert!(set_hotkeys_raw(&cleared)); - seed_recording_marker_hotkey(); - assert!(!hotkeys_raw().contains_key(RECORDING_MARKER_HOTKEY)); - } - - #[test] - fn the_marker_shortcut_default_never_takes_a_combo_in_use() { - let default = serde_json::to_string(&default_recording_marker_hotkey()).unwrap(); - let store = TempStore::new( - "marker-seed-taken", - Some(&format!( - r#"{{ "hotkeys": {{ "hotkeys": {{ "stopRecording": {default} }} }} }}"# - )), - ); - seed_recording_marker_hotkey(); - assert!(!hotkeys_raw().contains_key(RECORDING_MARKER_HOTKEY)); - assert_eq!( - store.read()["hotkeys"][RECORDING_MARKER_HOTKEY_SEEDED], - true - ); - } - - #[test] - fn the_draw_shortcut_default_is_offered_once_and_never_over_a_combo_in_use() { - let store = TempStore::new("draw-seed", Some(r#"{ "hotkeys": { "hotkeys": {} } }"#)); - seed_draw_hotkey(); - if !crate::live_draw::SUPPORTED { - assert!(!hotkeys_raw().contains_key(DRAW_HOTKEY)); - return; - } - assert_eq!( - hotkey_from_value(&hotkeys_raw()[DRAW_HOTKEY]), - Some(default_draw_hotkey()) - ); - assert_eq!(store.read()["hotkeys"][DRAW_HOTKEY_SEEDED], true); - let mut cleared = hotkeys_raw(); - cleared.remove(DRAW_HOTKEY); - assert!(set_hotkeys_raw(&cleared)); - seed_draw_hotkey(); - assert!(!hotkeys_raw().contains_key(DRAW_HOTKEY)); - drop(store); - - let default = serde_json::to_string(&default_draw_hotkey()).unwrap(); - let _taken = TempStore::new( - "draw-seed-taken", - Some(&format!( - r#"{{ "hotkeys": {{ "hotkeys": {{ "stopRecording": {default} }} }} }}"# - )), - ); - seed_draw_hotkey(); - assert!(!hotkeys_raw().contains_key(DRAW_HOTKEY)); - } - - #[test] - fn drawings_fade_unless_kept() { - let _store = TempStore::new("keep-drawings", Some(r#"{ "general_settings": {} }"#)); - assert!(!keep_drawings()); - assert!(!GeneralSettings::load().keep_drawings); - assert!(set_store_setting( - GENERAL_SETTINGS, - KEEP_DRAWINGS_KEY, - Value::Bool(true) - )); - assert!(keep_drawings()); - assert!(GeneralSettings::load().keep_drawings); - } - - #[test] - fn the_marker_sound_is_off_until_turned_on() { - let _store = TempStore::new("marker-sound", Some(r#"{ "general_settings": {} }"#)); - assert!(!recording_marker_sound_enabled()); - assert!(!GeneralSettings::load().recording_marker_sound); - assert!(set_store_setting( - GENERAL_SETTINGS, - RECORDING_MARKER_SOUND_KEY, - Value::Bool(true) - )); - assert!(recording_marker_sound_enabled()); - assert!(GeneralSettings::load().recording_marker_sound); - } - - /// The automations model against the JSON `crates/automation/src/types.rs` - /// serializes: camelCase everywhere, `type`-tagged conditions/actions, and - /// the whole key written wholesale. - #[test] - fn automations_round_trip_the_tauri_shape() { - let store = TempStore::new( - "automations", - Some( - r#"{ "auth": { "user_id": "u_1" }, "automations": { "version": 1, "rules": [ { - "id": "r1", "name": "", "enabled": true, "trigger": "screenshotTaken", "matchMode": "all", - "conditions": [ { "type": "windowTitleContains", "pattern": "Slack" } ], - "actions": [ - { "type": "copyToClipboard", "source": "raw" }, - { "type": "export", "profile": { "format": "mp4", "fps": 30, "resolutionBase": { "x": 1920, "y": 1080 }, "compression": "web", "presetName": null }, "destination": "projectFolder" }, - { "type": "webhook", "url": "https://x.test", "method": "POST", "headers": {}, "bodyTemplate": null } - ] } ] } }"#, - ), - ); - - let mut automations = automations(); - assert_eq!(automations.rules.len(), 1); - let rule = &automations.rules[0]; - assert_eq!(rule.trigger, Trigger::ScreenshotTaken); - assert_eq!(rule.match_mode, MatchMode::All); - assert_eq!( - rule.conditions[0], - Condition::WindowTitleContains { - pattern: "Slack".into() - } - ); - assert!(matches!( - &rule.actions[1], - Action::Export { - profile, - destination: ExportDestination::ProjectFolder, - } if profile.format == ExportFormat::Mp4 && profile.resolution_base.x == 1920 - )); - - automations.rules[0].actions.push(Action::SaveToLocation { - dir: "/tmp/shots".into(), - filename_template: None, - }); - assert!(set_automations(&automations)); - - let after = store.read(); - let actions = &after["automations"]["rules"][0]["actions"]; - assert_eq!(actions[3]["type"], "saveToLocation"); - assert_eq!(actions[3]["dir"], "/tmp/shots"); - assert_eq!(actions[3]["filenameTemplate"], Value::Null); - assert_eq!(actions[1]["profile"]["resolutionBase"]["x"], 1920); - // The rest of the store is untouched by the wholesale key write. - assert_eq!(after["auth"]["user_id"], "u_1"); - } - - /// The commercial license key, the shape license.tsx writes on activation. - #[test] - fn commercial_license_round_trips() { - let store = TempStore::new( - "license", - Some(r#"{ "general_settings": { "instanceId": "abc" } }"#), - ); - - assert_eq!(commercial_license(), None); - assert_eq!(instance_id_or_create().as_deref(), Some("abc")); - - let license = CommercialLicense { - license_key: "key-1".into(), - expiry_date: None, - refresh: 123.0, - activated_on: 456.0, - }; - assert!(set_commercial_license(Some(&license))); - assert_eq!(commercial_license(), Some(license)); - let written = store.read(); - assert_eq!( - written["general_settings"]["commercialLicense"]["licenseKey"], - "key-1" - ); - assert_eq!( - written["general_settings"]["commercialLicense"]["activatedOn"], - 456.0 - ); - - // Deactivation nulls the key, which both apps read as "no license". - assert!(set_commercial_license(None)); - assert_eq!(commercial_license(), None); - } - - #[test] - fn uuids_are_v4_shaped_and_distinct() { - let a = new_uuid_v4(); - let b = new_uuid_v4(); - assert_ne!(a, b); - assert_eq!(a.len(), 36); - assert_eq!(a.as_bytes()[14], b'4'); - assert!(matches!(a.as_bytes()[19], b'8' | b'9' | b'a' | b'b')); - } - - /// The `window_exclusion.rs` matching tests, ported with the module: the - /// two apps must agree on which windows a rule excludes. - #[test] - fn exclusion_matches_by_each_field_and_owner_title_requires_both() { - let title = WindowExclusion { - window_title: Some("Cap Camera".into()), - ..Default::default() - }; - assert!(title.matches(None, None, Some("Cap Camera"))); - assert!(!title.matches(None, None, Some("Other Window"))); - assert!(!title.matches(None, None, None)); - - let bundle = WindowExclusion { - bundle_identifier: Some("com.cap.desktop".into()), - ..Default::default() - }; - assert!(bundle.matches(Some("com.cap.desktop"), None, None)); - assert!(!bundle.matches(Some("com.other.app"), None, None)); - - let owner = WindowExclusion { - owner_name: Some("Cap".into()), - ..Default::default() - }; - assert!(owner.matches(None, Some("Cap"), None)); - assert!(!owner.matches(None, Some("Other"), None)); - - let owner_and_title = WindowExclusion { - owner_name: Some("Cap".into()), - window_title: Some("Cap Camera".into()), - ..Default::default() - }; - assert!(owner_and_title.matches(None, Some("Cap"), Some("Cap Camera"))); - assert!(!owner_and_title.matches(None, Some("Cap"), Some("Wrong Title"))); - assert!(!owner_and_title.matches(None, Some("Wrong Owner"), Some("Cap Camera"))); - assert!(!owner_and_title.matches(None, None, Some("Cap Camera"))); - - let empty = WindowExclusion::default(); - assert!(!empty.matches(None, None, None)); - assert!(!empty.matches(Some("any"), Some("any"), Some("any"))); - - let bundle_first = WindowExclusion { - bundle_identifier: Some("com.cap.desktop".into()), - window_title: Some("Cap Camera".into()), - ..Default::default() - }; - assert!(bundle_first.matches(Some("com.cap.desktop"), None, Some("Wrong"))); - } - - #[test] - fn editor_preview_quality_defaults_per_machine_until_chosen() { - let read = |quality: Option<&str>, chosen: Option| { - let mut general = Map::new(); - if let Some(quality) = quality { - general.insert("editorPreviewQuality".to_string(), Value::from(quality)); - } - if let Some(chosen) = chosen { - general.insert( - EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), - Value::from(chosen), - ); - } - GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) - .editor_preview_quality - }; - let machine = EditorPreviewQuality::for_this_machine(); - if cfg!(all(target_os = "macos", target_arch = "aarch64")) { - assert_eq!(machine, EditorPreviewQuality::Full); - } - if cfg!(target_os = "linux") { - assert_eq!(machine, EditorPreviewQuality::Half); - } - assert_eq!(read(None, None), machine); - assert_eq!(read(Some("half"), None), machine); - assert_eq!(read(Some("half"), Some(false)), machine); - assert_eq!(read(Some("bogus"), Some(true)), machine); - assert_eq!(read(Some("half"), Some(true)), EditorPreviewQuality::Half); - assert_eq!(read(Some("full"), None), EditorPreviewQuality::Full); - assert_eq!(read(Some("quarter"), None), EditorPreviewQuality::Quarter); - assert_eq!(read(Some("full"), Some(true)), EditorPreviewQuality::Full); - } - - #[test] - fn choosing_half_keeps_it_over_the_machine_default() { - let store = TempStore::new( - "preview-quality", - Some(r#"{ "general_settings": { "editorPreviewQuality": "half", "theme": "dark" } }"#), - ); - assert_eq!( - GeneralSettings::load().editor_preview_quality, - EditorPreviewQuality::for_this_machine() - ); - assert!(set_editor_preview_quality(EditorPreviewQuality::Half)); - let written = store.read(); - assert_eq!(written["general_settings"]["editorPreviewQuality"], "half"); - assert_eq!( - written["general_settings"][EDITOR_PREVIEW_QUALITY_CHOSEN_KEY], - true - ); - assert_eq!(written["general_settings"]["theme"], "dark"); - assert_eq!( - GeneralSettings::load().editor_preview_quality, - EditorPreviewQuality::Half - ); - } - - #[test] - fn editor_preview_speed_reads_offered_steps_and_falls_back_to_normal() { - let read = |value: Option| { - let mut general = Map::new(); - if let Some(value) = value { - general.insert(EDITOR_PREVIEW_SPEED_KEY.to_string(), value); - } - GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) - .editor_preview_speed - .as_f64() - }; - assert_eq!(read(None), 1.0); - for speed in cap_editor::PreviewSpeed::ALL { - assert_eq!( - read(Some(serde_json::json!(speed.as_f64()))), - speed.as_f64() - ); - } - assert_eq!(read(Some(serde_json::json!(2))), 2.0); - assert_eq!(read(Some(serde_json::json!(2.5))), 1.0); - assert_eq!(read(Some(serde_json::json!(-1))), 1.0); - assert_eq!(read(Some(serde_json::json!("2"))), 1.0); - assert_eq!(read(Some(Value::Null)), 1.0); - } - - /// Key absent -> the default exclusion list (the Rust struct's serde - /// default); key present but empty -> the user cleared every entry. - #[test] - fn excluded_windows_default_only_when_key_is_absent() { - let absent = GeneralSettings::from_sections(&Map::new(), &Map::new(), &Map::new()); - assert_eq!(absent.excluded_windows, default_excluded_windows()); - assert!( - absent - .excluded_windows - .iter() - .any(|e| e.window_title.as_deref() == Some("Cap Camera")) - ); - - let mut general = Map::new(); - general.insert("excludedWindows".to_string(), Value::Array(Vec::new())); - let cleared = GeneralSettings::from_sections(&general, &Map::new(), &Map::new()); - assert!(cleared.excluded_windows.is_empty()); - } - - /// 0.6.0 (Tauri) wrote the whole `general_settings` object with - /// `"showOverlay"` for its overlay; 0.6.1 to 0.6.3 wrote only this key, - /// with the same value, for the option labelled "Show Cap". Both read - /// back as the overlay, labelled as the Tauri app labelled it, and the - /// value written for it stays one 0.6.0 can read after a downgrade. - #[test] - fn post_studio_overlay_reads_tauri_and_gpui_stores() { - let tauri: Map = serde_json::from_str( - r#"{ - "uploadIndividualFiles": false, - "hideDockIcon": false, - "autoCreateShareableLink": false, - "enableNotifications": true, - "disableAutoOpenLinks": false, - "hasCompletedStartup": true, - "theme": "system", - "recordingCountdown": 3, - "mainWindowRecordingStartBehaviour": "close", - "postStudioRecordingBehaviour": "showOverlay", - "postDeletionBehaviour": "doNothing" - }"#, - ) - .unwrap(); - let gpui: Map = - serde_json::from_str(r#"{ "postStudioRecordingBehaviour": "showOverlay" }"#).unwrap(); - for general in [&tauri, &gpui] { - let settings = GeneralSettings::from_sections(general, &Map::new(), &Map::new()); - assert_eq!( - settings.post_studio_recording_behaviour, - PostStudioBehaviour::ShowOverlay - ); - } - assert_eq!(PostStudioBehaviour::ShowOverlay.as_json(), "showOverlay"); - assert_eq!(PostStudioBehaviour::ShowOverlay.label(), "Show in overlay"); - assert_eq!( - PostStudioBehaviour::from_json("showOverlay"), - Some(PostStudioBehaviour::ShowOverlay) - ); - - let unknown: Map = - serde_json::from_str(r#"{ "postStudioRecordingBehaviour": "showCap" }"#).unwrap(); - assert_eq!( - GeneralSettings::from_sections(&unknown, &Map::new(), &Map::new()) - .post_studio_recording_behaviour, - PostStudioBehaviour::OpenEditor - ); - assert_eq!( - GeneralSettings::from_sections(&Map::new(), &Map::new(), &Map::new()) - .post_studio_recording_behaviour, - PostStudioBehaviour::OpenEditor - ); - } - - /// A store written before `showMainWindowAfterInstantRecording` keeps - /// what it did: "copy link only" brought the main window back, so it reads - /// as Show Cap; every outcome round-trips through its two keys. - #[test] - fn post_instant_behaviour_reads_old_stores_and_round_trips() { - let settings = |pairs: &[(&str, bool)]| { - let general = pairs - .iter() - .map(|(key, value)| (key.to_string(), Value::Bool(*value))) - .collect::>(); - GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) - .post_instant_behaviour() - }; - assert_eq!(settings(&[]), PostInstantBehaviour::OpenLink); - assert_eq!( - settings(&[("disableAutoOpenLinks", false)]), - PostInstantBehaviour::OpenLink - ); - assert_eq!( - settings(&[("disableAutoOpenLinks", true)]), - PostInstantBehaviour::ShowCap - ); - assert_eq!( - settings(&[ - ("disableAutoOpenLinks", false), - (SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY, true) - ]), - PostInstantBehaviour::OpenLink - ); - // The Quality page's auto-open toggle writes only - // `disableAutoOpenLinks`; the open window and a reload must agree. - let toggled = GeneralSettings { - disable_auto_open_links: true, - ..GeneralSettings::default() - }; - assert_eq!( - toggled.post_instant_behaviour(), - settings(&[("disableAutoOpenLinks", true)]) - ); - for behaviour in [ - PostInstantBehaviour::OpenLink, - PostInstantBehaviour::ShowCap, - PostInstantBehaviour::CopyLink, - ] { - let (disable_open, show_main) = behaviour.store_values(); - assert_eq!( - settings(&[ - ("disableAutoOpenLinks", disable_open), - (SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY, show_main), - ]), - behaviour - ); - } - } -} From abd1e17efbd68e6ab327ef5ac6d816a66d995045 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:58:16 +0000 Subject: [PATCH 7/9] fix(editor): put the settings store module back --- apps/desktop-gpui/src/store.rs | 4018 ++++++++++++++++++++++++++++++++ 1 file changed, 4018 insertions(+) diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index e69de29bb2d..be9f9ffa12f 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -0,0 +1,4018 @@ +//! Persisted state: this app's own, and the settings store it shares with +//! 0.6.0 and earlier (the Tauri app, still downloadable and able to open the +//! same data after a downgrade). +//! +//! Two files, both inside `so.cap.desktop`'s app-data dir: +//! +//! - `gpui-state.json` -- ours. The Tauri app kept the camera window's chrome +//! state in the webview's `localStorage` (`cameraWindowState`); there is no +//! webview here, so the same shape lives in a file. Reads happen once at +//! open; writes are whole-file rewrites on a background thread -- the state +//! is a handful of scalars, atomicity beyond rename is not worth plumbing. +//! - `store` -- the tauri-plugin-store file 0.6.0 loads +//! (`Store.load("store")` in `apps/desktop/src/store.ts` at `cap-v0.6.2`), +//! holding `general_settings`, `recording_start_safety`, `auth`, `presets` +//! and the rest. Both apps read and write it, so every write here is a +//! read-modify-write on the raw JSON that touches exactly one key: see +//! [`set_store_setting`]. + +use std::{ + collections::HashMap, + path::{Path, PathBuf}, + sync::{Arc, LazyLock, Mutex, MutexGuard, PoisonError}, + time::{Duration, SystemTime}, +}; + +use serde::{Deserialize, Serialize}; +use serde_json::{Map, Value}; + +use crate::i18n::tr; + +/// `CameraPreviewShape` in the web app. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] +#[serde(rename_all = "lowercase")] +pub enum CameraShape { + #[default] + Round, + Square, + Full, +} + +/// `BackgroundBlurMode`. Cycled and persisted like the Tauri bubble's, and +/// processed live by the preview (`camera_blur.rs`). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] +#[serde(rename_all = "lowercase")] +pub enum BlurMode { + #[default] + Off, + Light, + Heavy, + Remove, +} + +impl BlurMode { + pub fn cycle(self) -> Self { + match self { + Self::Off => Self::Light, + Self::Light => Self::Heavy, + Self::Heavy if cfg!(target_os = "macos") => Self::Remove, + Self::Heavy | Self::Remove => Self::Off, + } + } + + /// The tiny label under the person glyph: `Light` / `Heavy`, nothing when + /// off. + pub fn label(self) -> Option<&'static str> { + match self { + Self::Off => None, + Self::Light => Some(tr::camera::blur::light()), + Self::Heavy => Some(tr::camera::blur::heavy()), + Self::Remove => Some(tr::camera::blur::cutout()), + } + } +} + +/// `CameraWindowState` from `CameraPreviewChrome.tsx`. +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +pub struct CameraWindowState { + pub size: f32, + pub shape: CameraShape, + pub mirrored: bool, + pub background_blur: BlurMode, +} + +impl Default for CameraWindowState { + fn default() -> Self { + Self { + size: crate::camera_window::CAMERA_DEFAULT_SIZE, + shape: CameraShape::Round, + mirrored: false, + background_blur: BlurMode::Off, + } + } +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct PersistedState { + pub camera_window: Option, + #[serde(default)] + pub export: Option, + /// `screenshotEditorLayersPanelOpen` -- the Tauri screenshot editor keeps + /// it in `localStorage` (`makePersisted` in + /// `screenshot-editor/context.tsx`); no webview here, so it lives in this + /// file. + #[serde(default)] + pub screenshot_layers_panel_open: Option, + /// `editorCropSnapToRatio` -- the crop dialog's persisted snap toggle, + /// also `localStorage` over there. + #[serde(default)] + pub screenshot_crop_snap_to_ratio: Option, + #[serde(default)] + pub editor_window: Option, +} + +/// Where the editor window last sat on each display, keyed by display UUID. +/// Offsets are measured from the display's top-left corner, so they survive +/// the display being moved around in the arrangement. +#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] +pub struct EditorWindowMemory { + #[serde(default)] + pub last_display: Option, + #[serde(default)] + pub displays: Vec, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct RememberedEditorFrame { + pub display: String, + pub left: f64, + pub top: f64, + pub width: f64, + pub height: f64, +} + +/// `export_settings` in the Tauri editor (`ExportPage.tsx` localStorage). +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ExportPrefs { + pub format: String, + pub fps: u32, + pub export_to: String, + pub resolution: String, + pub compression: String, + #[serde(default)] + pub optimize_filesize: bool, + #[serde(default)] + pub cursor_only: bool, + pub custom_bpp: Option, + #[serde(default)] + pub force_ffmpeg_decoder: bool, + #[serde(default)] + pub advanced_open: bool, + #[serde(default)] + pub organization_id: Option, + #[serde(default)] + pub turbo: bool, + #[serde(default)] + pub gif_quality: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub excluded_tracks: Option>, + #[serde(default)] + pub resolution_from_source: bool, + #[serde(default)] + pub fps_from_source: bool, +} + +/// `so.cap.desktop`'s app-data dir -- where both stores live. +/// +/// The identifier every Cap release has used (`so.cap.desktop`), resolved +/// the way each OS's app-data convention spells it, so recordings and +/// settings from 0.6.0 and earlier are found where those builds left them. +pub fn app_data_dir() -> PathBuf { + if let Ok(dir) = std::env::var("CAP_GPUI_APP_DATA_DIR") + && !dir.trim().is_empty() + { + return PathBuf::from(dir); + } + + #[cfg(target_os = "macos")] + { + PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".into())) + .join("Library/Application Support/so.cap.desktop") + } + #[cfg(target_os = "windows")] + { + std::env::var_os("APPDATA") + .map(PathBuf::from) + .unwrap_or_else(|| PathBuf::from(".")) + .join("so.cap.desktop") + } + #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] + { + let base = std::env::var_os("XDG_DATA_HOME") + .map(PathBuf::from) + .unwrap_or_else(|| { + PathBuf::from(std::env::var_os("HOME").unwrap_or_else(|| ".".into())) + .join(".local/share") + }); + base.join("so.cap.desktop") + } +} + +pub(crate) fn bundled_resource_dirs() -> Vec { + let executable = std::env::current_exe().ok(); + let override_dir = std::env::var_os("CAP_GPUI_RESOURCES_DIR").map(PathBuf::from); + bundled_resource_dirs_for(executable.as_deref(), override_dir.as_deref()) +} + +fn bundled_resource_dirs_for( + executable: Option<&Path>, + override_dir: Option<&Path>, +) -> Vec { + let mut directories = Vec::new(); + if let Some(directory) = override_dir { + directories.push(directory.to_path_buf()); + } + + if let Some(parent) = executable.and_then(Path::parent) { + if parent.file_name().is_some_and(|name| name == "MacOS") + && let Some(contents) = parent.parent() + { + directories.push(contents.join("Resources")); + } + if parent.file_name().is_some_and(|name| name == "bin") + && let Some(prefix) = parent.parent() + { + directories.push(prefix.join("lib").join("cap")); + } + directories.push(parent.join("resources")); + directories.push(parent.to_path_buf()); + } + + directories +} + +fn state_path() -> PathBuf { + app_data_dir().join("gpui-state.json") +} + +pub fn load() -> PersistedState { + std::fs::read(state_path()) + .ok() + .and_then(|bytes| serde_json::from_slice(&bytes).ok()) + .unwrap_or_default() +} + +static UPDATE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +/// Read-modify-write on the caller's thread; callers hand this to the +/// background executor. Writers are serialized and the file is replaced by +/// rename, because [`load`] treats a half-written file as empty and the next +/// update would then persist defaults over every other preference. +pub fn update(mutate: impl FnOnce(&mut PersistedState)) { + let _guard = UPDATE_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let path = state_path(); + let mut state = load(); + mutate(&mut state); + if let Some(parent) = path.parent() { + let _ = std::fs::create_dir_all(parent); + } + match serde_json::to_vec_pretty(&state) { + Ok(bytes) => { + let temp = path.with_extension("json.tmp"); + if let Err(error) = + std::fs::write(&temp, bytes).and_then(|()| std::fs::rename(&temp, &path)) + { + tracing::warn!("persisting gpui state: {error}"); + } + } + Err(error) => tracing::warn!("serializing gpui state: {error}"), + } +} + +// --------------------------------------------------------------------------- +// The Tauri settings store +// --------------------------------------------------------------------------- + +#[cfg(test)] +thread_local! { + static TEST_TAURI_STORE_PATH: std::cell::RefCell> = const { + std::cell::RefCell::new(None) + }; +} + +#[cfg(test)] +thread_local! { + static TEST_STORE_FILE_READS: std::cell::Cell = const { std::cell::Cell::new(0) }; + pub(crate) static TEST_AFTER_AUTH_SNAPSHOT: std::cell::RefCell>> = const { + std::cell::RefCell::new(None) + }; +} + +#[cfg(test)] +pub(crate) fn set_test_tauri_store_path(path: Option) { + TEST_TAURI_STORE_PATH.with(|current| *current.borrow_mut() = path); +} + +/// The tauri-plugin-store file, shared with the shipping app. +/// +/// `CAP_GPUI_TAURI_STORE` redirects it at a copy, which is how the tests -- +/// and any verification run that must not touch the user's real settings -- +/// work. +pub fn tauri_store_path() -> PathBuf { + #[cfg(test)] + if let Some(path) = TEST_TAURI_STORE_PATH.with(|path| path.borrow().clone()) { + return path; + } + + match std::env::var("CAP_GPUI_TAURI_STORE") { + Ok(path) if !path.is_empty() => PathBuf::from(path), + // `Store.load("store")`: no extension, and the sibling `store.json` + // is a different (stale) file. + _ => app_data_dir().join("store"), + } +} + +static STORE_UPDATE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +/// The whole store as raw JSON. +/// +/// A missing or whitespace-only file is an empty store. Nonempty invalid JSON +/// is returned as `None` so no write path can silently replace +/// someone's settings with a fresh object. +fn read_store(path: &std::path::Path) -> Option> { + #[cfg(test)] + TEST_STORE_FILE_READS.with(|reads| reads.set(reads.get() + 1)); + let bytes = match std::fs::read(path) { + Ok(bytes) => bytes, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Some(Map::new()), + Err(error) => { + tracing::warn!("reading the Tauri store: {error}"); + return None; + } + }; + if bytes.iter().all(u8::is_ascii_whitespace) { + return Some(Map::new()); + } + match serde_json::from_slice::(&bytes) { + Ok(Value::Object(map)) => Some(map), + Ok(_) => { + tracing::error!("the Tauri store is not a JSON object; refusing to write to it"); + None + } + Err(error) => { + tracing::error!("the Tauri store did not parse ({error}); refusing to write to it"); + None + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct StoreFileStamp { + len: u64, + modified: Option, + #[cfg(unix)] + inode: (u64, u64), + #[cfg(unix)] + changed: (i64, i64), +} + +/// Filesystem mtimes are only as fine as the clock that stamps them (a +/// scheduler tick on Linux, two seconds on FAT), so a rewrite that lands in +/// the same tick as the read we cached can leave every field of the stamp +/// unchanged. A cached parse is only trusted once the file's mtime is older +/// than the read by more than that, the same rule git uses for racily clean +/// index entries. +const STORE_MTIME_SETTLE: Duration = Duration::from_secs(3); + +impl StoreFileStamp { + fn of(metadata: &std::fs::Metadata) -> Self { + #[cfg(unix)] + use std::os::unix::fs::MetadataExt; + + Self { + len: metadata.len(), + modified: metadata.modified().ok(), + #[cfg(unix)] + inode: (metadata.dev(), metadata.ino()), + #[cfg(unix)] + changed: (metadata.ctime(), metadata.ctime_nsec()), + } + } + + fn settled_before(&self, read_at: SystemTime) -> bool { + self.modified + .and_then(|modified| modified.checked_add(STORE_MTIME_SETTLE)) + .is_some_and(|settled| settled < read_at) + } +} + +struct CachedStore { + stamp: StoreFileStamp, + read_at: SystemTime, + store: Arc>, +} + +static STORE_CACHE: LazyLock>> = + LazyLock::new(Default::default); + +fn store_cache() -> MutexGuard<'static, HashMap> { + STORE_CACHE.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// [`read_store`] for readers, which run inside render: the export page asks +/// for the auth section on every progress tick. The file is shared with the +/// Tauri app, which can rewrite it at any time, so every call still stats it +/// and re-reads when anything about the file changed; only the read and parse +/// are skipped. Writers keep reading the disk directly. +fn cached_store(path: &Path) -> Option>> { + let read_at = SystemTime::now(); + let stamp = match std::fs::metadata(path) { + Ok(metadata) => StoreFileStamp::of(&metadata), + Err(_) => { + store_cache().remove(path); + return read_store(path).map(Arc::new); + } + }; + + if let Some(cached) = store_cache().get(path) + && cached.stamp == stamp + && cached.stamp.settled_before(cached.read_at) + { + return Some(cached.store.clone()); + } + + let Some(store) = read_store(path).map(Arc::new) else { + store_cache().remove(path); + return None; + }; + store_cache().insert( + path.to_path_buf(), + CachedStore { + stamp, + read_at, + store: store.clone(), + }, + ); + Some(store) +} + +/// One section of the store (`general_settings`, `recording_start_safety`, +/// ...) as raw JSON. Empty when absent or the wrong shape. +pub fn store_section(section: &str) -> Map { + cached_store(&tauri_store_path()) + .and_then(|store| match store.get(section) { + Some(Value::Object(map)) => Some(map.clone()), + _ => None, + }) + .unwrap_or_default() +} + +pub fn notification_sounds_enabled() -> bool { + cached_store(&tauri_store_path()) + .and_then(|store| { + store + .get(GENERAL_SETTINGS) + .and_then(Value::as_object) + .map(|settings| bool_at(settings, "enableNotifications", true)) + }) + .unwrap_or(false) +} + +/// Read once per stroke, so it goes through the cached store too. +pub fn keep_drawings() -> bool { + cached_store(&tauri_store_path()) + .and_then(|store| { + store + .get(GENERAL_SETTINGS) + .and_then(Value::as_object) + .map(|settings| bool_at(settings, KEEP_DRAWINGS_KEY, false)) + }) + .unwrap_or(false) +} + +/// Write one key of one section, preserving every other byte of meaning in +/// the file. +/// +/// This is the whole compatibility contract with the Tauri app. It reads the +/// raw JSON, replaces exactly `store[section][key]`, and writes it back, so +/// keys this app has never heard of -- everything under `auth`, `presets`, +/// `teleprompter`, the migration flags, and the two thirds of +/// `general_settings` no page here renders -- survive untouched. Serializing +/// a typed struct back over the file would drop them all. +/// +/// Returns false when the store could not be read or written; the caller has +/// already updated its in-memory copy, which is the same optimistic order +/// `handleChange` uses in `general.tsx`. +pub fn set_store_setting(section: &str, key: &str, value: Value) -> bool { + update_store(|store| { + let entry = store + .entry(section.to_string()) + .or_insert_with(|| Value::Object(Map::new())); + if !entry.is_object() { + *entry = Value::Object(Map::new()); + } + entry + .as_object_mut() + .expect("just replaced any non-object with an object") + .insert(key.to_string(), value); + true + }) +} + +pub fn set_editor_preview_quality(quality: EditorPreviewQuality) -> bool { + update_store_section(GENERAL_SETTINGS, |general| { + general.insert( + "editorPreviewQuality".to_string(), + Value::from(quality.as_json()), + ); + general.insert( + EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), + Value::Bool(true), + ); + true + }) +} + +/// Replace or remove one top-level store section. `authStore.set(null)` in +/// the Tauri app drops the whole `auth` object; writing keys one at a time +/// would leave a half-signed-out session behind. +pub fn set_store_value(section: &str, value: Value) -> bool { + update_store(|store| { + if value.is_null() { + store.remove(section); + } else { + store.insert(section.to_string(), value); + } + true + }) +} + +/// Replace one top-level section only if `update` agrees, deciding from the +/// section as it is on disk at write time rather than from a copy read before +/// a network round trip. +pub fn update_store_section( + section: &str, + update: impl FnOnce(&mut Map) -> bool, +) -> bool { + update_store(|store| { + let mut current = match store.remove(section) { + Some(Value::Object(map)) => map, + _ => Map::new(), + }; + let keep = update(&mut current); + store.insert(section.to_string(), Value::Object(current)); + keep + }) +} + +/// Reads, mutates and rewrites the store as one step, so two writers cannot +/// both start from the same snapshot and drop each other's changes. Returning +/// false from `mutate` leaves the file untouched. +fn update_store(mutate: impl FnOnce(&mut Map) -> bool) -> bool { + let _guard = STORE_UPDATE_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let path = tauri_store_path(); + let Some(mut store) = read_store(&path) else { + return false; + }; + if !mutate(&mut store) { + return false; + } + let written = write_store(&path, &Value::Object(store)); + store_cache().remove(&path); + written +} + +/// A store that reads but does not parse as a JSON object (zero-filled after a +/// power loss, truncated by a crash) refuses every write, so onboarding could +/// never save its progress and stayed on "Could not save onboarding progress" +/// forever. While it does not parse, its onboarding flags, auth and presets are +/// already invisible to Cap, so its bytes are kept beside it and the progress +/// is saved to a fresh store. A store that could not be read at all is left +/// alone. +fn set_aside_unparseable_store(path: &Path) -> bool { + let _guard = STORE_UPDATE_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let Ok(bytes) = std::fs::read(path) else { + return false; + }; + if bytes.iter().all(u8::is_ascii_whitespace) { + return false; + } + if matches!( + serde_json::from_slice::(&bytes), + Ok(Value::Object(_)) + ) { + return false; + } + let stamp = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .map(|elapsed| elapsed.as_millis()) + .unwrap_or_default(); + let mut name = path.file_name().unwrap_or_default().to_os_string(); + name.push(format!(".unreadable-{stamp}")); + let kept = path.with_file_name(name); + match std::fs::rename(path, &kept) { + Ok(()) => { + store_cache().remove(path); + tracing::error!( + kept = %kept.display(), + "the settings store did not parse; kept it aside and started a new one" + ); + true + } + Err(error) => { + tracing::error!(%error, "could not set aside the unparseable settings store"); + false + } + } +} + +/// Pretty JSON, two-space indent, no trailing newline -- byte-shape identical +/// to what tauri-plugin-store writes, so a store the two apps take turns on +/// does not churn. +/// +/// Written to a sibling temp file and renamed, so a crash mid-write cannot +/// leave a half-written store behind (the Tauri app would refuse to parse it, +/// and the user would lose every setting). +fn write_store(path: &std::path::Path, store: &Value) -> bool { + let bytes = match serde_json::to_vec_pretty(store) { + Ok(bytes) => bytes, + Err(error) => { + tracing::error!("serializing the Tauri store: {error}"); + return false; + } + }; + + if let Some(parent) = path.parent() + && let Err(error) = std::fs::create_dir_all(parent) + { + tracing::warn!("creating the store directory: {error}"); + return false; + } + + let temp = path.with_extension("gpui-tmp"); + if let Err(error) = std::fs::write(&temp, bytes) { + tracing::warn!("writing the Tauri store: {error}"); + return false; + } + if let Err(error) = std::fs::rename(&temp, path) { + tracing::warn!("replacing the Tauri store: {error}"); + let _ = std::fs::remove_file(&temp); + return false; + } + true +} + +// -- Typed reads ------------------------------------------------------------ +// +// Field by field rather than `serde::Deserialize` on a struct: one setting +// written by a newer Tauri build with a value this enum does not know would +// fail a whole-struct deserialize and blank the entire page. Here it falls +// back to its default and every other row still shows the user's real value. + +fn bool_at(map: &Map, key: &str, default: bool) -> bool { + map.get(key).and_then(Value::as_bool).unwrap_or(default) +} + +fn u32_at(map: &Map, key: &str, default: u32) -> u32 { + map.get(key) + .and_then(Value::as_u64) + .map(|value| value as u32) + .unwrap_or(default) +} + +fn opt_u32_at(map: &Map, key: &str) -> Option { + map.get(key) + .and_then(Value::as_u64) + .map(|value| value as u32) +} + +fn opt_f32_at(map: &Map, key: &str) -> Option { + map.get(key) + .and_then(Value::as_f64) + .map(|value| value as f32) +} + +fn f32_at(map: &Map, key: &str, default: f32) -> f32 { + opt_f32_at(map, key).unwrap_or(default) +} + +fn opt_string_at(map: &Map, key: &str) -> Option { + map.get(key) + .and_then(Value::as_str) + .map(|value| value.to_string()) +} + +fn string_at(map: &Map, key: &str, default: &str) -> String { + opt_string_at(map, key).unwrap_or_else(|| default.to_string()) +} + +fn strings_at(map: &Map, key: &str) -> Vec { + map.get(key) + .and_then(Value::as_array) + .map(|entries| { + entries + .iter() + .filter_map(Value::as_str) + .map(|value| value.to_string()) + .collect() + }) + .unwrap_or_default() +} + +fn enum_at(map: &Map, key: &str) -> T { + map.get(key) + .and_then(Value::as_str) + .and_then(T::from_json) + .unwrap_or_default() +} + +/// A string-valued setting with a fixed option list -- the shape every +/// `#[serde(rename_all = "camelCase")]` enum in +/// `apps/desktop/src-tauri/src/general_settings.rs` serializes to. +pub trait SettingsEnum: Sized + Default + Copy + PartialEq + 'static { + const ALL: &'static [Self]; + fn from_json(value: &str) -> Option; + fn as_json(self) -> &'static str; + /// The option's text in the settings UI, which is not derivable from the + /// JSON name ("openEditor" is shown as "Open editor"). + fn label(self) -> &'static str; +} + +macro_rules! settings_enum { + ( + $(#[$meta:meta])* + $name:ident { + $($variant:ident = $json:literal, $label:expr;)+ + } + default = $default:ident + ) => { + $(#[$meta])* + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub enum $name { + $($variant,)+ + } + + impl Default for $name { + fn default() -> Self { + Self::$default + } + } + + impl SettingsEnum for $name { + const ALL: &'static [Self] = &[$(Self::$variant,)+]; + + fn from_json(value: &str) -> Option { + match value { + $($json => Some(Self::$variant),)+ + _ => None, + } + } + + fn as_json(self) -> &'static str { + match self { + $(Self::$variant => $json,)+ + } + } + + fn label(self) -> &'static str { + match self { + $(Self::$variant => $label,)+ + } + } + } + }; +} + +settings_enum! { + /// `AppTheme`. The previews are labelled System/Light/Dark in + /// `AppearanceSection`. + AppTheme { + System = "system", tr::settings::general::appearance::system(); + Light = "light", tr::settings::general::appearance::light(); + Dark = "dark", tr::settings::general::appearance::dark(); + } + default = System +} + +settings_enum! { + /// `StudioRecordingQuality`, whose Rust default is `balanced`. + StudioQuality { + Compatibility = "compatibility", tr::settings::options::studio_quality::compatibility(); + Balanced = "balanced", tr::settings::options::studio_quality::balanced(); + Ultra = "ultra", tr::settings::options::studio_quality::ultra(); + } + default = Balanced +} + +impl From for cap_recording::StudioQuality { + fn from(quality: StudioQuality) -> Self { + match quality { + StudioQuality::Compatibility => Self::Compatibility, + StudioQuality::Balanced => Self::Balanced, + StudioQuality::Ultra => Self::Ultra, + } + } +} + +settings_enum! { + /// `MainWindowRecordingStartBehaviour`. British spelling in both the JSON + /// and the label, as shipped. + MainWindowStartBehaviour { + Close = "close", tr::settings::options::main_window_start::close(); + Minimise = "minimise", tr::settings::options::main_window_start::minimise(); + } + default = Close +} + +settings_enum! { + /// How a window recording captures the picked window: the region of the + /// screen it covers, or (Focused) the window by itself, centred by the + /// picker and followed if it moves. + WindowCaptureStyle { + ScreenRegion = "screenRegion", tr::settings::options::window_capture::screen_region(); + FocusedWindow = "focusedWindow", tr::settings::options::window_capture::focused_window(); + } + default = ScreenRegion +} + +settings_enum! { + /// `PostStudioRecordingBehaviour`. + PostStudioBehaviour { + OpenEditor = "openEditor", tr::settings::options::post_studio::open_editor(); + ShowOverlay = "showOverlay", tr::settings::options::post_studio::show_overlay(); + DoNothing = "doNothing", tr::settings::options::post_studio::do_nothing(); + } + default = OpenEditor +} + +settings_enum! { + /// What a finished screenshot does next. GPUI-only: the Tauri app always + /// opened the editor and never reads this key. + PostScreenshotBehaviour { + OpenEditor = "openEditor", tr::settings::options::post_screenshot::open_editor(); + Copy = "copy", tr::settings::options::post_screenshot::copy(); + CopyAndOpenEditor = "copyAndOpenEditor", tr::settings::options::post_screenshot::copy_and_open_editor(); + Save = "save", tr::settings::options::post_screenshot::do_nothing(); + } + default = OpenEditor +} + +impl PostScreenshotBehaviour { + pub fn copies(self) -> bool { + matches!(self, Self::Copy | Self::CopyAndOpenEditor) + } + + pub fn opens_editor(self) -> bool { + matches!(self, Self::OpenEditor | Self::CopyAndOpenEditor) + } +} + +settings_enum! { + /// `PostDeletionBehaviour`. + PostDeletionBehaviour { + DoNothing = "doNothing", tr::settings::options::post_deletion::do_nothing(); + ReopenRecordingWindow = "reopenRecordingWindow", tr::settings::options::post_deletion::reopen(); + } + default = DoNothing +} + +settings_enum! { + /// `UpdateChannel` from `src-tauri/src/updates.rs`. + UpdateChannel { + Stable = "stable", "Stable"; + Nightly = "nightly", "Nightly"; + } + default = Stable +} + +settings_enum! { + UpdateDownloadPolicy { + Manual = "manual", "Manual"; + Auto = "auto", "Auto"; + Never = "never", "Never"; + } + default = Manual +} + +settings_enum! { + EditorPreviewQuality { + Quarter = "quarter", tr::settings::options::preview_quality::quarter(); + Half = "half", tr::settings::options::preview_quality::half(); + Full = "full", tr::settings::options::preview_quality::full(); + } + default = Half +} + +impl EditorPreviewQuality { + pub fn scale(self) -> f32 { + match self { + Self::Full => 1.0, + Self::Half => 0.65, + Self::Quarter => 0.25, + } + } + + /// Every earlier build wrote the whole settings object, so a stored `half` + /// without [`EDITOR_PREVIEW_QUALITY_CHOSEN_KEY`] is the old default, not a + /// choice. Full and Quarter were only ever picked by hand. + fn from_general(general: &Map) -> Self { + let stored = general + .get("editorPreviewQuality") + .and_then(Value::as_str) + .and_then(Self::from_json); + match stored { + Some(quality) + if quality != Self::Half + || bool_at(general, EDITOR_PREVIEW_QUALITY_CHOSEN_KEY, false) => + { + quality + } + _ => Self::for_this_machine(), + } + } + + /// Full where the GPU draws it at no measurable cost, Half where Full + /// drops playback frames (software and integrated GPUs). A renderer that + /// fell back to (or was forced onto) a software adapter reads back every + /// frame on the CPU, whatever GPU is installed. + pub fn for_this_machine() -> Self { + static FULL_IS_CHEAP: std::sync::OnceLock = std::sync::OnceLock::new(); + let software = SOFTWARE_RENDERING.load(std::sync::atomic::Ordering::Acquire) + || cap_rendering::force_software_wgpu_adapter(); + if !software && *FULL_IS_CHEAP.get_or_init(full_preview_is_cheap) { + Self::Full + } else { + Self::Half + } + } + + /// Called with the shared render device's adapter, or `true` when no + /// hardware device could be made. + pub fn note_rendering_adapter(software: bool) { + SOFTWARE_RENDERING.store(software, std::sync::atomic::Ordering::Release); + } +} + +static SOFTWARE_RENDERING: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] +fn full_preview_is_cheap() -> bool { + true +} + +/// A dedicated GPU (the editor renders on the high-performance adapter) and +/// 16 GB of memory. Integrated GPUs report a few hundred MB of dedicated +/// memory at most. A 16 GB machine reports slightly under 16 GiB. +#[cfg(target_os = "windows")] +fn full_preview_is_cheap() -> bool { + const MIN_DEDICATED_VIDEO_MEMORY: u64 = 2 << 30; + const MIN_MEMORY: u64 = 15 << 30; + let video_memory = cap_d3d_adapter::select_capture_adapter(None) + .map_or(0, |adapter| adapter.dedicated_vram_bytes); + let memory = crate::camera_blur_portable::total_memory_bytes().unwrap_or(0); + let cheap = video_memory >= MIN_DEDICATED_VIDEO_MEMORY && memory >= MIN_MEMORY; + tracing::info!( + video_memory_mb = video_memory >> 20, + memory_mb = memory >> 20, + cheap, + "editor preview quality default" + ); + cheap +} + +#[cfg(not(any( + all(target_os = "macos", target_arch = "aarch64"), + target_os = "windows" +)))] +fn full_preview_is_cheap() -> bool { + false +} + +/// One entry of `general_settings.excludedWindows` (`WindowExclusion`). +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct WindowExclusion { + pub bundle_identifier: Option, + pub owner_name: Option, + pub window_title: Option, +} + +impl WindowExclusion { + fn from_json(value: &Value) -> Option { + let map = value.as_object()?; + let exclusion = Self { + bundle_identifier: opt_string_at(map, "bundleIdentifier"), + owner_name: opt_string_at(map, "ownerName"), + window_title: opt_string_at(map, "windowTitle"), + }; + (exclusion != Self::default()).then_some(exclusion) + } + + fn to_json(&self) -> Value { + let field = |value: &Option| match value { + Some(value) => Value::String(value.clone()), + None => Value::Null, + }; + Value::Object(Map::from_iter([ + ( + "bundleIdentifier".to_string(), + field(&self.bundle_identifier), + ), + ("ownerName".to_string(), field(&self.owner_name)), + ("windowTitle".to_string(), field(&self.window_title)), + ])) + } + + /// `getExclusionPrimaryLabel` in `general.tsx`. + pub fn primary_label(&self) -> &str { + self.owner_name + .as_deref() + .or(self.window_title.as_deref()) + .or(self.bundle_identifier.as_deref()) + .unwrap_or("Unknown") + } + + /// `getExclusionSecondaryLabel`. + pub fn secondary_label(&self) -> Option<&str> { + if self.owner_name.is_some() && self.window_title.is_some() { + return self.window_title.as_deref(); + } + if self.bundle_identifier.is_some() + && (self.owner_name.is_some() || self.window_title.is_some()) + { + return self.bundle_identifier.as_deref(); + } + self.bundle_identifier.as_deref() + } + + /// `WindowExclusion::matches` in `src-tauri/src/window_exclusion.rs`, + /// verbatim: bundle id alone matches; owner+title requires both; owner or + /// title alone match on their own. + pub fn matches( + &self, + bundle_identifier: Option<&str>, + owner_name: Option<&str>, + window_title: Option<&str>, + ) -> bool { + if let Some(identifier) = self.bundle_identifier.as_deref() + && bundle_identifier == Some(identifier) + { + return true; + } + + if let Some(expected_owner) = self.owner_name.as_deref() { + let owner_matches = owner_name == Some(expected_owner); + if let Some(expected_title) = self.window_title.as_deref() { + return owner_matches && window_title == Some(expected_title); + } + if owner_matches { + return true; + } + } + + if let Some(expected_title) = self.window_title.as_deref() { + return window_title == Some(expected_title); + } + + false + } +} + +/// `DEFAULT_EXCLUDED_WINDOW_TITLES` in `general_settings.rs`, which is what +/// the page's Reset button asks the backend for. +pub const DEFAULT_EXCLUDED_WINDOW_TITLES: &[&str] = &[ + "Cap", + "Cap Settings", + "Cap Recording Controls", + "Cap Camera", + "Cap Window Capture Occluder", + "Cap Capture Area", + "Cap Mode Selection", + "Cap Recordings Overlay", + "Cap Teleprompter", +]; + +pub fn default_excluded_windows() -> Vec { + DEFAULT_EXCLUDED_WINDOW_TITLES + .iter() + .map(|title| WindowExclusion { + bundle_identifier: None, + owner_name: None, + window_title: Some((*title).to_string()), + }) + .collect() +} + +pub fn excluded_windows_to_json(windows: &[WindowExclusion]) -> Value { + Value::Array(windows.iter().map(WindowExclusion::to_json).collect()) +} + +/// `https://cap.so` -- `default_server_url()` in `general_settings.rs`, which +/// is also what `clientEnv.VITE_SERVER_URL` resolves to in a release build. +pub const DEFAULT_SERVER_URL: &str = "https://cap.so"; + +/// `DEFAULT_FILENAME_TEMPLATE` in `src-tauri/src/recording.rs`, spelled the +/// same way `general.tsx` spells it. +pub const DEFAULT_PROJECT_NAME_TEMPLATE: &str = "{target_name} ({target_kind}) {date} {time}"; + +/// The `general_settings` section, as the General page reads it. +/// +/// The defaults are `createDefaultGeneralSettings()` plus the per-row `??` +/// fallbacks in `general.tsx` -- i.e. what the shipping page *shows* for a key +/// that is not in the file. Two of them disagree with the Rust struct's own +/// `#[serde(default)]`: `autoZoomOnClicks` (Rust `default_true`, the page +/// false) and `enableNativeCameraPreview`. The page is the authority here +/// because this is a page. +#[derive(Debug, Clone, PartialEq)] +pub struct GeneralSettings { + pub theme: AppTheme, + /// `uiLanguage`: absent, `"system"`, or a catalog code such as `"fr"`. A + /// GPUI-only key; the Tauri app keeps it as an unknown field. + pub ui_language: Option, + pub hide_dock_icon: bool, + pub enable_notifications: bool, + pub instant_mode_max_resolution: u32, + pub disable_auto_open_links: bool, + /// GPUI-only `showMainWindowAfterInstantRecording`: whether the main + /// window comes back after an Instant recording whose link did not open. + /// Kept as stored, absent included, so every reader resolves it the same + /// way in [`Self::post_instant_behaviour`]. + pub show_main_window_after_instant: Option, + pub studio_recording_quality: StudioQuality, + pub recording_countdown: Option, + pub main_window_recording_start_behaviour: MainWindowStartBehaviour, + pub window_capture_style: WindowCaptureStyle, + pub focus_center: bool, + pub post_studio_recording_behaviour: PostStudioBehaviour, + pub post_screenshot_behaviour: PostScreenshotBehaviour, + /// GPUI-only: a cancelled picker leaves the main window hidden unless it + /// was on screen when the picker opened. + pub keep_main_hidden_on_cancel: bool, + /// GPUI-only: closing the last editor leaves the main window hidden. + pub keep_main_hidden_after_editor: bool, + pub post_deletion_behaviour: PostDeletionBehaviour, + pub delete_instant_recordings_after_upload: bool, + pub crash_recovery_recording: bool, + pub custom_cursor_capture: bool, + pub auto_zoom_on_clicks: bool, + pub default_zoom_amount: Option, + pub capture_keyboard_events: bool, + pub macbook_notch_overlay: bool, + /// GPUI-only `recordingMarkerSound`: a short cue when a marker is added + /// during a Studio recording. Off by default, because a microphone or + /// system audio capture can record the cue. + pub recording_marker_sound: bool, + /// GPUI-only `keepDrawingsOnScreen`: strokes drawn on screen while + /// recording stay until cleared instead of fading out. + pub keep_drawings: bool, + pub max_fps: u32, + pub recordings_path: Option, + /// Custom folders the library has been pointed at before. Not rendered by + /// any page -- the recordings scan reads it, because + /// `known_recordings_dirs` keeps listing recordings that were left behind + /// in a folder the user has since switched away from. + pub previous_recordings_paths: Vec, + pub default_project_name_template: Option, + pub excluded_windows: Vec, + pub update_channel: UpdateChannel, + pub update_download_policy: UpdateDownloadPolicy, + pub server_url: String, + pub enable_telemetry: bool, + pub editor_preview_quality: EditorPreviewQuality, + /// How fast the editor preview plays. Preview only: export and the + /// project never see it. Anything but one of the offered steps reads as 1x. + pub editor_preview_speed: cap_editor::PreviewSpeed, + /// The Experimental page's two toggles. Both default false the way the + /// page shows them (`!!settings.enableNativeCameraPreview` / + /// `!!settings.outOfProcessMuxer` in experimental.tsx). + pub enable_native_camera_preview: bool, + pub out_of_process_muxer: bool, + /// Not part of `general_settings` at all: the mic-confirmation toggle + /// lives in the store's own `recording_start_safety` section + /// (`RECORDING_START_SAFETY_DEFAULTS`), and the page renders it in the + /// middle of the Recording card as if it were one of them. + pub confirm_without_microphone: bool, + /// Also outside `general_settings`: the `audio_enhancement` section's + /// `enabledByDefault`, rendered in the Recording card next to it. + pub studio_sound_by_default: bool, +} + +pub const SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY: &str = "showMainWindowAfterInstantRecording"; +pub const EDITOR_PREVIEW_SPEED_KEY: &str = "editorPreviewSpeed"; +pub const EDITOR_PREVIEW_QUALITY_CHOSEN_KEY: &str = "editorPreviewQualityChosen"; + +pub const RECORDING_MARKER_SOUND_KEY: &str = "recordingMarkerSound"; +pub const KEEP_DRAWINGS_KEY: &str = "keepDrawingsOnScreen"; + +/// Read on every marker press, so it goes through the cached store rather +/// than a full [`GeneralSettings::load`]. +pub fn recording_marker_sound_enabled() -> bool { + cached_store(&tauri_store_path()) + .and_then(|store| { + store + .get(GENERAL_SETTINGS) + .and_then(Value::as_object) + .map(|settings| bool_at(settings, RECORDING_MARKER_SOUND_KEY, false)) + }) + .unwrap_or(false) +} + +/// What follows a cleanly stopped Instant recording, the link always copied. +/// Not one key: `disableAutoOpenLinks` (shared with the Tauri app) decides +/// whether the link opens, `showMainWindowAfterInstantRecording` whether the +/// main window comes back when it does not. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PostInstantBehaviour { + OpenLink, + ShowCap, + CopyLink, +} + +impl PostInstantBehaviour { + /// `(disableAutoOpenLinks, showMainWindowAfterInstantRecording)`. + pub fn store_values(self) -> (bool, bool) { + match self { + Self::OpenLink => (false, false), + Self::ShowCap => (true, true), + Self::CopyLink => (true, false), + } + } +} + +/// The section names, so the write calls read as the store keys they are. +pub const GENERAL_SETTINGS: &str = "general_settings"; +pub const RECORDING_START_SAFETY: &str = "recording_start_safety"; + +/// Changes the recordings folder and remembers the old one, like Tauri's +/// `remember_previous_recordings_path`, so the library keeps listing projects +/// left in earlier folders. +pub fn set_recordings_path(path: Option<&str>) -> bool { + const MAX_PREVIOUS_PATHS: usize = 20; + + update_store_section(GENERAL_SETTINGS, |settings| { + let mut previous_paths = strings_at(settings, "previousRecordingsPaths"); + if let Some(path) = path { + previous_paths.retain(|previous| previous != path); + } + if let Some(previous) = opt_string_at(settings, "recordingsPath") + && Some(previous.as_str()) != path + && !previous_paths.contains(&previous) + { + previous_paths.push(previous); + let len = previous_paths.len(); + if len > MAX_PREVIOUS_PATHS { + previous_paths.drain(..len - MAX_PREVIOUS_PATHS); + } + } + settings.insert( + "recordingsPath".into(), + path.map_or(Value::Null, Value::from), + ); + settings.insert( + "previousRecordingsPaths".into(), + Value::from(previous_paths), + ); + true + }) +} + +/// `audioEnhancementStore` (`apps/desktop/src/store.ts`): whether new Studio +/// recordings start with Studio Sound on. Defaults on; the editor's Audio tab +/// and the Recording settings card both write it. +pub const AUDIO_ENHANCEMENT: &str = "audio_enhancement"; +pub const STUDIO_SOUND_BY_DEFAULT_KEY: &str = "enabledByDefault"; + +pub fn studio_sound_by_default() -> bool { + bool_at( + &store_section(AUDIO_ENHANCEMENT), + STUDIO_SOUND_BY_DEFAULT_KEY, + true, + ) +} + +pub fn set_studio_sound_by_default(enabled: bool) -> bool { + set_store_setting( + AUDIO_ENHANCEMENT, + STUDIO_SOUND_BY_DEFAULT_KEY, + Value::Bool(enabled), + ) +} + +pub fn studio_sound_isolation() -> cap_project::VoiceIsolation { + store_section(AUDIO_ENHANCEMENT) + .get("isolation") + .cloned() + .and_then(|value| serde_json::from_value(value).ok()) + .unwrap_or_default() +} + +pub fn set_studio_sound_isolation(isolation: cap_project::VoiceIsolation) -> bool { + set_store_setting( + AUDIO_ENHANCEMENT, + "isolation", + serde_json::to_value(isolation).unwrap(), + ) +} + +pub fn set_studio_sound_defaults(enabled: bool, isolation: cap_project::VoiceIsolation) -> bool { + let mut section = store_section(AUDIO_ENHANCEMENT); + section.insert(STUDIO_SOUND_BY_DEFAULT_KEY.into(), Value::Bool(enabled)); + section.insert("isolation".into(), serde_json::to_value(isolation).unwrap()); + set_store_value(AUDIO_ENHANCEMENT, Value::Object(section)) +} +/// `RecordingSettingsStore::KEY` (`src-tauri/src/recording_settings.rs:37`) -- +/// the section the tray's mode picker reads and writes. +pub const RECORDING_SETTINGS: &str = "recording_settings"; + +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct RecordingDeviceSettings { + pub camera: Option, + pub microphone: Option, +} + +impl RecordingDeviceSettings { + pub fn for_camera( + device_id: &str, + model_id: Option<&cap_camera::ModelID>, + ) -> Option { + let settings = store_section(RECORDING_SETTINGS); + let formats = settings.get("cameraDeviceSettings")?; + std::iter::once(format!("device:{device_id}")) + .chain(model_id.map(|model| format!("model:{model}"))) + .find_map(|key| { + formats + .get(key) + .cloned() + .and_then(|value| serde_json::from_value(value).ok()) + }) + } + + pub fn for_microphone( + name: &str, + ) -> Option { + store_section(RECORDING_SETTINGS) + .get("microphoneDeviceSettings")? + .get(name) + .cloned() + .and_then(|value| serde_json::from_value(value).ok()) + } +} + +pub fn set_camera_device_settings( + device_id: &str, + model_id: Option<&cap_camera::ModelID>, + settings: cap_recording::feeds::camera::CameraDeviceSettings, +) -> bool { + let keys = std::iter::once(format!("device:{device_id}")) + .chain(model_id.map(|model| format!("model:{model}"))); + set_device_format_settings("cameraDeviceSettings", keys, settings) +} + +pub fn set_microphone_device_settings( + name: &str, + settings: cap_recording::feeds::microphone::MicrophoneDeviceSettings, +) -> bool { + set_device_format_settings("microphoneDeviceSettings", [name.to_string()], settings) +} + +fn set_device_format_settings( + map_key: &str, + keys: impl IntoIterator, + settings: impl serde::Serialize, +) -> bool { + let Ok(Value::Object(fields)) = serde_json::to_value(settings) else { + return false; + }; + update_store(|store| { + let section = store + .entry(RECORDING_SETTINGS.to_string()) + .or_insert_with(|| Value::Object(Map::new())); + let Some(section) = section.as_object_mut() else { + return false; + }; + let map = section + .entry(map_key.to_string()) + .or_insert_with(|| Value::Object(Map::new())); + let Some(map) = map.as_object_mut() else { + return false; + }; + for key in keys { + let entry = map.entry(key).or_insert_with(|| Value::Object(Map::new())); + let Some(entry) = entry.as_object_mut() else { + return false; + }; + for (key, value) in &fields { + if value.is_null() { + entry.remove(key); + } else { + entry.insert(key.clone(), value.clone()); + } + } + } + true + }) +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct RecordingInputSettings { + pub camera_id: Option, + pub microphone_name: Option, +} + +impl RecordingInputSettings { + pub fn load() -> Self { + let settings = store_section(RECORDING_SETTINGS); + Self { + camera_id: settings.get("cameraId").and_then(recording_camera_id), + microphone_name: settings + .get("micName") + .and_then(Value::as_str) + .filter(|name| !name.is_empty()) + .map(str::to_string), + } + } +} + +fn recording_camera_id(value: &Value) -> Option { + use cap_recording::feeds::camera::DeviceOrModelID; + + let object = value.as_object()?; + if object.len() != 1 { + return None; + } + if let Some(id) = object.get("DeviceID").and_then(Value::as_str) { + return (!id.is_empty()).then(|| DeviceOrModelID::DeviceID(id.to_string())); + } + let model = object.get("ModelID")?.as_str()?.to_string(); + cap_camera::ModelID::try_from(model) + .ok() + .map(DeviceOrModelID::ModelID) +} + +pub fn set_recording_camera_id(id: Option<&cap_recording::feeds::camera::DeviceOrModelID>) -> bool { + let Ok(value) = serde_json::to_value(id) else { + return false; + }; + set_store_setting(RECORDING_SETTINGS, "cameraId", value) +} + +/// `RecordingSettingsStore.systemAudio`, which the Tauri app restored on +/// launch. Anything but a boolean reads as off. +pub fn recording_system_audio() -> bool { + store_section(RECORDING_SETTINGS) + .get("systemAudio") + .and_then(Value::as_bool) + .unwrap_or(false) +} + +pub fn set_recording_system_audio(enabled: bool) -> bool { + set_store_setting(RECORDING_SETTINGS, "systemAudio", Value::Bool(enabled)) +} + +pub fn set_recording_microphone_name(name: Option<&str>) -> bool { + set_store_setting( + RECORDING_SETTINGS, + "micName", + name.map_or(Value::Null, |name| Value::String(name.to_string())), + ) +} + +/// `RecordingSettingsStore.mode`, i.e. `cap_recording::RecordingMode` under +/// `#[serde(rename_all = "camelCase")]` -- one lowercase word per variant +/// (`studio`, `instant`, `screenshot`). `None` when the key is absent, which +/// `get_current_mode` turns into `RecordingMode::default()` (Instant). +pub fn recording_mode_slug() -> Option { + store_section(RECORDING_SETTINGS) + .get("mode") + .and_then(Value::as_str) + .map(str::to_string) +} + +/// `RecordingSettingsStore::set_mode`. +/// +/// The Tauri version re-serialises the whole `recording_settings` object, which +/// fills in defaults for every field it does not know about; +/// [`set_store_setting`] replaces only `mode` and leaves the target, the device +/// settings maps and the rest byte-identical. Strictly the safer half of the +/// same write, and the two apps read the same key. +pub fn set_recording_mode_slug(slug: &str) -> bool { + set_store_setting(RECORDING_SETTINGS, "mode", Value::String(slug.to_string())) +} + +pub fn has_completed_startup() -> bool { + store_section(GENERAL_SETTINGS) + .get("hasCompletedStartup") + .and_then(Value::as_bool) + .unwrap_or(false) +} + +pub fn set_has_completed_startup(value: bool) -> bool { + set_onboarding_progress("hasCompletedStartup", value) +} + +fn set_onboarding_progress(key: &str, value: bool) -> bool { + set_store_setting(GENERAL_SETTINGS, key, Value::Bool(value)) + || (set_aside_unparseable_store(&tauri_store_path()) + && set_store_setting(GENERAL_SETTINGS, key, Value::Bool(value))) +} + +pub fn has_completed_onboarding() -> bool { + store_section(GENERAL_SETTINGS) + .get("hasCompletedOnboarding") + .and_then(Value::as_bool) + .unwrap_or(false) +} + +pub fn set_has_completed_onboarding(value: bool) -> bool { + set_onboarding_progress("hasCompletedOnboarding", value) +} + +/// `should_show_onboarding` (`lib.rs:463-477`): first-run flags or missing +/// macOS screen-recording + accessibility. +pub fn should_show_onboarding() -> bool { + !has_completed_startup() + || !has_completed_onboarding() + || !crate::permissions::necessary_granted() +} + +impl Default for GeneralSettings { + fn default() -> Self { + Self::from_sections(&Map::new(), &Map::new(), &Map::new()) + } +} + +impl GeneralSettings { + pub fn post_instant_behaviour(&self) -> PostInstantBehaviour { + if !self.disable_auto_open_links { + PostInstantBehaviour::OpenLink + } else if self.show_main_window_after_instant.unwrap_or(true) { + // Absent: "copy link only" brought the main window back before + // the key existed, so a store that never set it keeps doing that. + PostInstantBehaviour::ShowCap + } else { + PostInstantBehaviour::CopyLink + } + } + + /// `defaultZoomAmount ?? 1.5`: what the settings slider shows and what a + /// zoom segment drawn on the timeline starts at (`TL/ZoomTrack.tsx:226`). + pub fn new_zoom_amount(&self) -> f32 { + self.default_zoom_amount.unwrap_or(1.5) + } + + pub fn load() -> Self { + let store = cached_store(&tauri_store_path()).unwrap_or_default(); + let empty = Map::new(); + let section = |name: &str| store.get(name).and_then(Value::as_object).unwrap_or(&empty); + Self::from_sections( + section(GENERAL_SETTINGS), + section(RECORDING_START_SAFETY), + section(AUDIO_ENHANCEMENT), + ) + } + + fn from_sections( + general: &Map, + safety: &Map, + audio_enhancement: &Map, + ) -> Self { + Self { + theme: enum_at(general, "theme"), + ui_language: opt_string_at(general, crate::i18n::SETTING_KEY), + hide_dock_icon: bool_at(general, "hideDockIcon", false), + enable_notifications: bool_at(general, "enableNotifications", true), + instant_mode_max_resolution: u32_at(general, "instantModeMaxResolution", 1920), + disable_auto_open_links: bool_at(general, "disableAutoOpenLinks", false), + show_main_window_after_instant: general + .get(SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY) + .and_then(Value::as_bool), + studio_recording_quality: enum_at(general, "studioRecordingQuality"), + recording_countdown: opt_u32_at(general, "recordingCountdown"), + main_window_recording_start_behaviour: enum_at( + general, + "mainWindowRecordingStartBehaviour", + ), + window_capture_style: enum_at(general, "windowCaptureStyle"), + focus_center: bool_at(general, "focusCenter", false), + post_studio_recording_behaviour: enum_at(general, "postStudioRecordingBehaviour"), + post_screenshot_behaviour: enum_at(general, "postScreenshotBehaviour"), + keep_main_hidden_on_cancel: bool_at(general, "keepMainHiddenOnCancel", false), + keep_main_hidden_after_editor: bool_at(general, "keepMainHiddenAfterEditor", false), + post_deletion_behaviour: enum_at(general, "postDeletionBehaviour"), + delete_instant_recordings_after_upload: bool_at( + general, + "deleteInstantRecordingsAfterUpload", + false, + ), + crash_recovery_recording: bool_at(general, "crashRecoveryRecording", true), + // The one snake_case key in the store: `#[serde(rename = + // "custom_cursor_capture2")]` on `custom_cursor_capture`, which + // is how the setting was re-defaulted without inheriting the old + // key's values. + custom_cursor_capture: bool_at(general, "custom_cursor_capture2", true), + auto_zoom_on_clicks: bool_at(general, "autoZoomOnClicks", false), + default_zoom_amount: opt_f32_at(general, "defaultZoomAmount"), + capture_keyboard_events: bool_at(general, "captureKeyboardEvents", true), + macbook_notch_overlay: bool_at(general, "macbookNotchOverlay", false), + recording_marker_sound: bool_at(general, RECORDING_MARKER_SOUND_KEY, false), + keep_drawings: bool_at(general, KEEP_DRAWINGS_KEY, false), + max_fps: u32_at(general, "maxFps", 60), + recordings_path: opt_string_at(general, "recordingsPath"), + previous_recordings_paths: strings_at(general, "previousRecordingsPaths"), + default_project_name_template: opt_string_at(general, "defaultProjectNameTemplate"), + // Key absent -> the default exclusion list, matching the Rust + // struct's `#[serde(default = "default_excluded_windows")]`. Key + // present but empty means the user removed every entry. + excluded_windows: general + .get("excludedWindows") + .and_then(Value::as_array) + .map(|entries| { + entries + .iter() + .filter_map(WindowExclusion::from_json) + .collect() + }) + .unwrap_or_else(default_excluded_windows), + update_channel: enum_at(general, "updateChannel"), + update_download_policy: enum_at(general, "updateDownloadPolicy"), + server_url: string_at(general, "serverUrl", DEFAULT_SERVER_URL), + enable_telemetry: bool_at(general, "enableTelemetry", true), + editor_preview_quality: EditorPreviewQuality::from_general(general), + editor_preview_speed: general + .get(EDITOR_PREVIEW_SPEED_KEY) + .and_then(Value::as_f64) + .and_then(cap_editor::PreviewSpeed::from_f64) + .unwrap_or_default(), + enable_native_camera_preview: bool_at(general, "enableNativeCameraPreview", false), + out_of_process_muxer: bool_at(general, "outOfProcessMuxer", false), + confirm_without_microphone: bool_at( + safety, + "confirmBeforeRecordingWithoutMicrophone", + true, + ), + studio_sound_by_default: bool_at(audio_enhancement, STUDIO_SOUND_BY_DEFAULT_KEY, true), + } + } +} + +// -- The teleprompter section ----------------------------------------------- + +/// `teleprompterStore`'s key in the shared store file: +/// `declareStore("teleprompter")` in +/// `apps/desktop/src/store.ts`. A top-level section of its own, like +/// `general_settings`, not a key inside one. +pub const TELEPROMPTER: &str = "teleprompter"; + +/// `TeleprompterStore` (`store.ts:17-35`), with `teleprompterDefaults` as the +/// defaults: `{ script: "", fontSize: 30, wordsPerMinute: 150, lineHeight: 1.5, +/// showCueMarkers: true, mirror: false, windowOpacityPercent: 92 }`. +/// +/// Read field by field for the same reason `GeneralSettings` is: a value +/// written by a newer build that this app cannot parse costs that one field its +/// value, not the whole script. +#[derive(Debug, Clone, PartialEq)] +pub struct TeleprompterState { + pub script: String, + pub font_size: f32, + pub words_per_minute: u32, + pub line_height: f32, + pub show_cue_markers: bool, + pub mirror: bool, + pub window_opacity_percent: u32, +} + +impl TeleprompterState { + /// The store keys, so a write call reads as the key it writes. + pub const SCRIPT: &'static str = "script"; + pub const FONT_SIZE: &'static str = "fontSize"; + pub const WORDS_PER_MINUTE: &'static str = "wordsPerMinute"; + pub const LINE_HEIGHT: &'static str = "lineHeight"; + pub const SHOW_CUE_MARKERS: &'static str = "showCueMarkers"; + pub const MIRROR: &'static str = "mirror"; + pub const WINDOW_OPACITY_PERCENT: &'static str = "windowOpacityPercent"; + + pub fn load() -> Self { + Self::from_section(&store_section(TELEPROMPTER)) + } + + fn from_section(map: &Map) -> Self { + let defaults = Self::default(); + Self { + script: opt_string_at(map, Self::SCRIPT).unwrap_or(defaults.script), + font_size: f32_at(map, Self::FONT_SIZE, defaults.font_size), + words_per_minute: u32_at(map, Self::WORDS_PER_MINUTE, defaults.words_per_minute), + line_height: f32_at(map, Self::LINE_HEIGHT, defaults.line_height), + show_cue_markers: bool_at(map, Self::SHOW_CUE_MARKERS, defaults.show_cue_markers), + mirror: bool_at(map, Self::MIRROR, defaults.mirror), + window_opacity_percent: u32_at( + map, + Self::WINDOW_OPACITY_PERCENT, + defaults.window_opacity_percent, + ), + } + } + + /// One field as the JSON the Tauri app would have written, so the debounced + /// flush can write exactly the keys that changed. + pub fn value_for(&self, key: &str) -> Value { + match key { + Self::SCRIPT => Value::from(self.script.clone()), + Self::FONT_SIZE => Value::from(self.font_size), + Self::WORDS_PER_MINUTE => Value::from(self.words_per_minute), + Self::LINE_HEIGHT => Value::from(self.line_height), + Self::SHOW_CUE_MARKERS => Value::Bool(self.show_cue_markers), + Self::MIRROR => Value::Bool(self.mirror), + Self::WINDOW_OPACITY_PERCENT => Value::from(self.window_opacity_percent), + _ => Value::Null, + } + } +} + +impl Default for TeleprompterState { + fn default() -> Self { + Self { + script: String::new(), + font_size: 30., + words_per_minute: 150, + line_height: 1.5, + show_cue_markers: true, + mirror: false, + window_opacity_percent: 92, + } + } +} + +// -- The hotkeys section ------------------------------------------------------ + +/// `hotkeysStore`'s key: `declareStore("hotkeys")` in +/// `apps/desktop/src/store.ts`. The section is `{ "hotkeys": { action: Hotkey } }`. +pub const HOTKEYS: &str = "hotkeys"; + +/// `Hotkey` (`src-tauri/src/hotkeys.rs:22-29`). `code` is a W3C +/// `KeyboardEvent.code` string, which is how `global_hotkey::Code` serializes +/// and what hotkeys.tsx captures from the DOM event. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Hotkey { + pub code: String, + pub meta: bool, + pub ctrl: bool, + pub alt: bool, + pub shift: bool, +} + +/// The raw `hotkeys` map, kept as JSON so bindings for actions this build does +/// not know about (a newer Tauri's `HotkeyAction`) survive a rewrite. +pub fn hotkeys_raw() -> Map { + store_section(HOTKEYS) + .remove("hotkeys") + .and_then(|value| match value { + Value::Object(map) => Some(map), + _ => None, + }) + .unwrap_or_default() +} + +/// `hotkeysStore.set({ hotkeys })`: replace the map, leave the rest of the +/// section (there is none today) alone. +pub fn set_hotkeys_raw(map: &Map) -> bool { + set_store_setting(HOTKEYS, "hotkeys", Value::Object(map.clone())) +} + +pub fn hotkey_from_value(value: &Value) -> Option { + serde_json::from_value(value.clone()).ok() +} + +pub const RECORDING_MARKER_HOTKEY: &str = "addRecordingMarker"; + +/// Set once the marker shortcut's default has been offered, so clearing the +/// shortcut in Settings sticks across launches. +const RECORDING_MARKER_HOTKEY_SEEDED: &str = "recordingMarkerSeeded"; + +/// ⌃⌘M on macOS. Elsewhere Ctrl+Alt is AltGr on many layouts (AltGr+M types +/// µ in German), so Alt+Shift+M instead. +pub fn default_recording_marker_hotkey() -> Hotkey { + let macos = cfg!(target_os = "macos"); + Hotkey { + code: "KeyM".into(), + meta: macos, + ctrl: macos, + alt: !macos, + shift: !macos, + } +} + +/// Gives the marker shortcut its default the first time this build runs, +/// unless the user already bound it, or bound something else to the default +/// combo. Later launches leave the map alone. +pub fn seed_recording_marker_hotkey() { + seed_default_hotkey( + RECORDING_MARKER_HOTKEY, + RECORDING_MARKER_HOTKEY_SEEDED, + default_recording_marker_hotkey(), + ); +} + +pub const DRAW_HOTKEY: &str = "toggleDrawing"; + +const DRAW_HOTKEY_SEEDED: &str = "toggleDrawingSeeded"; + +/// ⌃⌘D is the system's look-up shortcut, so drawing takes ⌃⌘P (pen). +pub fn default_draw_hotkey() -> Hotkey { + Hotkey { + code: "KeyP".into(), + meta: true, + ctrl: true, + alt: false, + shift: false, + } +} + +/// The drawing shortcut's default, offered once the same way as the +/// marker's. Only where drawing on screen is offered. +pub fn seed_draw_hotkey() { + if crate::live_draw::SUPPORTED { + seed_default_hotkey(DRAW_HOTKEY, DRAW_HOTKEY_SEEDED, default_draw_hotkey()); + } +} + +fn seed_default_hotkey(key: &str, seeded: &str, default: Hotkey) { + if store_section(HOTKEYS).get(seeded).and_then(Value::as_bool) == Some(true) { + return; + } + let mut map = hotkeys_raw(); + let taken = map + .values() + .filter_map(hotkey_from_value) + .any(|hotkey| hotkey == default); + if !map.contains_key(key) && !taken { + let Ok(value) = serde_json::to_value(&default) else { + return; + }; + map.insert(key.into(), value); + if !set_hotkeys_raw(&map) { + return; + } + } + set_store_setting(HOTKEYS, seeded, Value::Bool(true)); +} + +// -- Transcription hints -------------------------------------------------------- + +/// `DEFAULT_TRANSCRIPTION_HINTS` (`apps/desktop/src/utils/general-settings.ts`). +pub const DEFAULT_TRANSCRIPTION_HINTS: &[&str] = + &["Cap", "TypeScript", "My Brand Name", "mywebsite.com"]; + +/// `normalizeTranscriptionHints`: strip NULs, trim, drop empties and +/// duplicates, first occurrence wins. +pub fn normalize_transcription_hints(hints: I) -> Vec +where + I: IntoIterator, + I::Item: AsRef, +{ + let mut normalized: Vec = Vec::new(); + for hint in hints { + let value = hint.as_ref().replace('\0', ""); + let value = value.trim(); + if value.is_empty() || normalized.iter().any(|existing| existing == value) { + continue; + } + normalized.push(value.to_string()); + } + normalized +} + +/// `deriveGeneralSettings(store).transcriptionHints ?? []`: the stored array +/// as-is when the key exists (normalisation happens on write, not read), the +/// four defaults when it does not. +pub fn transcription_hints() -> Vec { + match store_section(GENERAL_SETTINGS).get("transcriptionHints") { + Some(Value::Array(entries)) => entries + .iter() + .filter_map(Value::as_str) + .map(str::to_string) + .collect(), + Some(_) => Vec::new(), + None => DEFAULT_TRANSCRIPTION_HINTS + .iter() + .map(|hint| (*hint).to_string()) + .collect(), + } +} + +// -- The auth section ----------------------------------------------------------- + +/// What the settings pages need from `AuthStore` (`src-tauri/src/auth.rs`): +/// the bearer token `protectedHeaders()` sends, and `plan.upgraded`. +#[derive(Debug, Clone, Default)] +pub struct AuthSnapshot { + pub server_url: String, + pub token: Option, + pub plan_upgraded: bool, + pub plan_manual: bool, + pub organizations: Vec, +} + +#[derive(Debug, Clone)] +pub struct AuthOrganization { + pub id: String, + pub name: String, +} + +impl AuthSnapshot { + pub fn signed_in(&self) -> bool { + self.token.is_some() + } + + pub fn is_upgraded(&self) -> bool { + self.plan_upgraded || self.plan_manual + } +} + +/// `maybeProtectedHeaders` (`utils/web-api.ts:64-75`): `secret.api_key` wins +/// over `secret.token`, and `plan.upgraded` is the Pro gate. +pub fn auth_snapshot() -> AuthSnapshot { + let store = cached_store(&tauri_store_path()).unwrap_or_default(); + let empty = Map::new(); + let auth = store + .get("auth") + .and_then(Value::as_object) + .unwrap_or(&empty); + let server_url = string_at( + store + .get(GENERAL_SETTINGS) + .and_then(Value::as_object) + .unwrap_or(&empty), + "serverUrl", + DEFAULT_SERVER_URL, + ); + let token = auth + .get("secret") + .and_then(Value::as_object) + .and_then(|secret| { + secret + .get("api_key") + .or_else(|| secret.get("token")) + .and_then(Value::as_str) + }) + .map(str::to_string); + let plan = auth.get("plan").and_then(Value::as_object); + let plan_upgraded = plan + .and_then(|plan| plan.get("upgraded")) + .and_then(Value::as_bool) + .unwrap_or(false); + let plan_manual = plan + .and_then(|plan| plan.get("manual")) + .and_then(Value::as_bool) + .unwrap_or(false); + let organizations = auth + .get("organizations") + .and_then(Value::as_array) + .map(|entries| { + entries + .iter() + .filter_map(|entry| { + let object = entry.as_object()?; + Some(AuthOrganization { + id: object.get("id")?.as_str()?.to_string(), + name: object + .get("name") + .and_then(Value::as_str) + .unwrap_or("Organization") + .to_string(), + }) + }) + .collect() + }) + .unwrap_or_default(); + #[cfg(test)] + if let Some(after_snapshot) = TEST_AFTER_AUTH_SNAPSHOT.with(|hook| hook.take()) { + after_snapshot(); + } + AuthSnapshot { + server_url, + token, + plan_upgraded, + plan_manual, + organizations, + } +} + +pub fn set_auth(value: Option) -> bool { + set_store_value("auth", value.unwrap_or(Value::Null)) +} + +// -- The commercial license ------------------------------------------------------- + +/// `CommercialLicense` (`src-tauri/src/general_settings.rs:301-308`), stored +/// at `general_settings.commercialLicense`. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CommercialLicense { + pub license_key: String, + pub expiry_date: Option, + pub refresh: f64, + pub activated_on: f64, +} + +pub fn commercial_license() -> Option { + store_section(GENERAL_SETTINGS) + .get("commercialLicense") + .and_then(|value| serde_json::from_value(value.clone()).ok()) +} + +/// license.tsx deactivates by writing `commercialLicense: undefined`, which +/// JSON.stringify drops; `null` reads back the same on both sides +/// (`Option` and the page's falsiness check). +pub fn set_commercial_license(license: Option<&CommercialLicense>) -> bool { + let value = match license { + Some(license) => serde_json::to_value(license).unwrap_or(Value::Null), + None => Value::Null, + }; + set_store_setting(GENERAL_SETTINGS, "commercialLicense", value) +} + +/// `general_settings.instanceId` -- `#[serde(default = "uuid::Uuid::new_v4")]` +/// over there, so a store the Tauri app has saved always carries one. When +/// only this app has ever run, one is minted and persisted the same way the +/// Tauri default would be on its next save. +pub fn instance_id_or_create() -> Option { + if let Some(id) = opt_string_at(&store_section(GENERAL_SETTINGS), "instanceId") { + return Some(id); + } + let id = new_uuid_v4(); + set_store_setting(GENERAL_SETTINGS, "instanceId", Value::String(id.clone())).then_some(id) +} + +/// A v4 UUID from std's own entropy (`RandomState` is randomly seeded per +/// thread) -- no `uuid`/`rand` dependency for two identifiers a year. +pub fn new_uuid_v4() -> String { + use std::hash::{BuildHasher, Hasher}; + + let word = |salt: u64| { + let mut hasher = std::hash::RandomState::new().build_hasher(); + hasher.write_u64(salt); + hasher.write_u128( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_nanos()) + .unwrap_or(0), + ); + hasher.finish() + }; + let mut bytes = [0u8; 16]; + bytes[..8].copy_from_slice(&word(0x9e37_79b9).to_le_bytes()); + bytes[8..].copy_from_slice(&word(0x85eb_ca6b).to_le_bytes()); + // Version 4, variant 10xx. + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + format!( + "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", + bytes[0], + bytes[1], + bytes[2], + bytes[3], + bytes[4], + bytes[5], + bytes[6], + bytes[7], + bytes[8], + bytes[9], + bytes[10], + bytes[11], + bytes[12], + bytes[13], + bytes[14], + bytes[15], + ) +} + +// -- The automations section -------------------------------------------------------- + +/// The store key: `automationsStore` in `store.ts`, written wholesale by +/// `set_automations` (`src-tauri/src/automation.rs:834-837`). +pub const AUTOMATIONS: &str = "automations"; + +/// The model, serde-identical to `crates/automation/src/types.rs` (which this +/// standalone workspace cannot depend on). Every attribute below is copied +/// from there so the JSON both apps exchange stays byte-compatible. +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct AutomationsStore { + #[serde(default)] + pub version: u32, + #[serde(default)] + pub rules: Vec, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct AutomationRule { + pub id: String, + pub name: String, + #[serde(default = "default_true")] + pub enabled: bool, + pub trigger: Trigger, + #[serde(default)] + pub match_mode: MatchMode, + #[serde(default)] + pub conditions: Vec, + #[serde(default)] + pub actions: Vec, +} + +fn default_true() -> bool { + true +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] +#[serde(rename_all = "camelCase")] +pub enum MatchMode { + #[default] + All, + Any, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[serde(rename_all = "camelCase")] +pub enum Trigger { + ScreenshotTaken, + StudioRecordingFinished, + InstantRecordingFinished, + RecordingStarted, + UploadCompleted, + VideoImported, + RecordingDeleted, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[serde(rename_all = "camelCase", tag = "type")] +pub enum Condition { + CaptureTargetIs { target: CaptureTargetKind }, + RecordingModeIs { mode: AutomationRecordingMode }, + DurationAtLeast { secs: f64 }, + DurationAtMost { secs: f64 }, + WindowTitleContains { pattern: String }, + OrganizationIs { id: String }, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum CaptureTargetKind { + Display, + Window, + Area, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum AutomationRecordingMode { + Studio, + Instant, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[serde(rename_all = "camelCase", tag = "type")] +pub enum Action { + CopyToClipboard { + #[serde(default)] + source: ClipboardSource, + }, + #[serde(rename_all = "camelCase")] + SaveToLocation { + dir: String, + #[serde(default)] + filename_template: Option, + }, + Export { + profile: ExportProfile, + #[serde(default)] + destination: ExportDestination, + }, + #[serde(rename_all = "camelCase")] + Upload { + #[serde(default)] + organization_id: Option, + #[serde(default = "default_true")] + copy_link: bool, + #[serde(default)] + open_in_browser: bool, + }, + RevealInFileManager, + OpenFile, + #[serde(rename_all = "camelCase")] + RunCommand { + program: String, + #[serde(default)] + args: Vec, + #[serde(default)] + cwd: Option, + #[serde(default)] + env: std::collections::HashMap, + #[serde(default)] + use_shell: bool, + }, + #[serde(rename_all = "camelCase")] + Webhook { + url: String, + #[serde(default = "default_post")] + method: String, + #[serde(default)] + headers: std::collections::HashMap, + #[serde(default)] + body_template: Option, + }, + RecognizeTextToClipboard, + #[serde(rename_all = "camelCase")] + Notify { + #[serde(default = "default_notify_title")] + title_template: String, + #[serde(default)] + body_template: String, + }, + OpenEditor, + SkipEditor, + ApplyPreset { + name: String, + }, + DeleteLocalFiles, +} + +fn default_post() -> String { + "POST".to_string() +} + +fn default_notify_title() -> String { + "Cap Automation".to_string() +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] +#[serde(rename_all = "camelCase")] +pub enum ClipboardSource { + #[default] + Raw, + Rendered, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct ExportProfile { + pub format: ExportFormat, + #[serde(default = "default_fps")] + pub fps: u32, + #[serde(default = "default_resolution")] + pub resolution_base: cap_project::XY, + #[serde(default)] + pub compression: Option, + #[serde(default)] + pub preset_name: Option, +} + +fn default_fps() -> u32 { + 30 +} + +fn default_resolution() -> cap_project::XY { + cap_project::XY { x: 1920, y: 1080 } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum ExportFormat { + Mp4, + Gif, + Mov, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum AutomationExportCompression { + Maximum, + Social, + Web, + Potato, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum ExportDestination { + #[default] + ProjectFolder, + CustomPath { + dir: String, + }, +} + +/// `load_automations` (`automation.rs:812-825`): the whole `automations` key, +/// defaulting -- with a log line -- when absent or unparseable. Same +/// overwrite-on-next-save semantics as the Tauri app. +pub fn automations() -> AutomationsStore { + let Some(value) = + cached_store(&tauri_store_path()).and_then(|store| store.get(AUTOMATIONS).cloned()) + else { + return AutomationsStore::default(); + }; + match serde_json::from_value(value) { + Ok(store) => store, + Err(error) => { + tracing::error!("deserializing the automations store: {error}"); + AutomationsStore::default() + } + } +} + +pub fn automations_value() -> Option { + cached_store(&tauri_store_path()).and_then(|store| store.get(AUTOMATIONS).cloned()) +} + +/// `set_automations` (`automation.rs:834-837`): `store.set("automations", +/// json!(store))` -- the key replaced wholesale, unlike the per-key +/// [`set_store_setting`] writes. +pub fn set_automations(automations: &AutomationsStore) -> bool { + let value = match serde_json::to_value(automations) { + Ok(value) => value, + Err(error) => { + tracing::error!("serializing the automations store: {error}"); + return false; + } + }; + set_store_value(AUTOMATIONS, value) +} + +/// The preset names `applyPreset` offers -- `presetsStore.createQuery()` in +/// automations.tsx, reading `store["presets"].presets[].name`. +pub fn preset_names() -> Vec { + store_section("presets") + .get("presets") + .and_then(Value::as_array) + .map(|presets| { + presets + .iter() + .filter_map(|preset| preset.get("name").and_then(Value::as_str)) + .map(str::to_string) + .collect() + }) + .unwrap_or_default() +} + +#[cfg(test)] +mod tests { + use super::*; + + struct TempStore { + path: PathBuf, + } + + impl TempStore { + fn new(name: &str, contents: Option<&str>) -> Self { + let path = std::env::temp_dir() + .join(format!("cap-gpui-store-test-{}-{name}", std::process::id())); + match contents { + Some(contents) => std::fs::write(&path, contents).unwrap(), + None => { + let _ = std::fs::remove_file(&path); + } + } + TEST_TAURI_STORE_PATH.with(|current| { + assert!(current.borrow().is_none()); + *current.borrow_mut() = Some(path.clone()); + }); + Self { path } + } + + fn read(&self) -> Value { + serde_json::from_slice(&std::fs::read(&self.path).unwrap()).unwrap() + } + } + + impl Drop for TempStore { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.path); + TEST_TAURI_STORE_PATH.with(|current| *current.borrow_mut() = None); + } + } + + fn load_general_settings_separately() -> GeneralSettings { + GeneralSettings::from_sections( + &store_section(GENERAL_SETTINGS), + &store_section(RECORDING_START_SAFETY), + &store_section(AUDIO_ENHANCEMENT), + ) + } + + #[test] + fn general_settings_load_matches_separate_section_reads() { + for contents in [ + None, + Some("invalid json"), + Some("[]"), + Some(r#"{"general_settings":["wrong shape"],"audio_enhancement":null}"#), + Some( + r#"{"general_settings":{"theme":"dark","maxFps":24},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false},"audio_enhancement":{"enabledByDefault":false}}"#, + ), + ] { + let _store = TempStore::new("general-settings-load", contents); + assert_eq!(GeneralSettings::load(), load_general_settings_separately()); + } + } + + fn backdate(path: &Path, modified: SystemTime) { + std::fs::File::options() + .write(true) + .open(path) + .unwrap() + .set_modified(modified) + .unwrap(); + } + + fn seconds_ago(seconds: u64) -> SystemTime { + SystemTime::now() + .checked_sub(Duration::from_secs(seconds)) + .unwrap() + } + + fn store_file_reads() -> usize { + TEST_STORE_FILE_READS.with(std::cell::Cell::get) + } + + #[test] + fn render_reads_parse_the_store_once_until_the_file_changes() { + let store = TempStore::new( + "cache-render-reads", + Some( + r#"{"auth":{"secret":{"token":"aaaa"},"organizations":[{"id":"1","name":"One"},{"id":"2","name":"Two"}]}}"#, + ), + ); + backdate(&store.path, seconds_ago(60)); + let before = store_file_reads(); + for _ in 0..100 { + assert!(auth_snapshot().signed_in()); + assert_eq!(auth_snapshot().organizations.len(), 2); + assert_eq!(auth_snapshot().organizations.len(), 2); + } + assert_eq!(store_file_reads() - before, 1); + + std::fs::write( + &store.path, + r#"{"auth":{"secret":{"token":"bbbb"},"organizations":[{"id":"1","name":"One"},{"id":"2","name":"Two"}]}}"#, + ) + .unwrap(); + backdate(&store.path, seconds_ago(30)); + assert_eq!(auth_snapshot().token.as_deref(), Some("bbbb")); + assert_eq!(auth_snapshot().token.as_deref(), Some("bbbb")); + assert_eq!(store_file_reads() - before, 2); + } + + #[test] + fn a_same_length_rewrite_within_the_cached_mtime_tick_is_still_read() { + let store = TempStore::new( + "cache-racy-rewrite", + Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#), + ); + assert!(has_completed_startup()); + let modified = std::fs::metadata(&store.path).unwrap().modified().unwrap(); + std::fs::write( + &store.path, + r#"{"general_settings":{"hasCompletedStartup":null}}"#, + ) + .unwrap(); + backdate(&store.path, modified); + assert!(!has_completed_startup()); + } + + #[cfg(unix)] + #[test] + fn an_in_place_rewrite_that_restores_the_old_mtime_is_still_read() { + let store = TempStore::new( + "cache-restored-mtime", + Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#), + ); + let modified = seconds_ago(60); + backdate(&store.path, modified); + assert!(has_completed_startup()); + std::thread::sleep(Duration::from_millis(50)); + std::fs::write( + &store.path, + r#"{"general_settings":{"hasCompletedStartup":null}}"#, + ) + .unwrap(); + backdate(&store.path, modified); + assert!(!has_completed_startup()); + } + + #[test] + fn a_store_that_stops_parsing_or_disappears_is_not_served_from_the_cache() { + let store = TempStore::new( + "cache-broken", + Some(r#"{"general_settings":{"hasCompletedStartup":true}}"#), + ); + backdate(&store.path, seconds_ago(60)); + assert!(has_completed_startup()); + std::fs::write(&store.path, "{ not json").unwrap(); + backdate(&store.path, seconds_ago(30)); + assert!(!has_completed_startup()); + assert!(!set_store_setting( + GENERAL_SETTINGS, + "hideDockIcon", + Value::Bool(true) + )); + std::fs::remove_file(&store.path).unwrap(); + assert!(!has_completed_startup()); + assert!(store_section(GENERAL_SETTINGS).is_empty()); + } + + #[test] + fn every_store_writer_is_visible_to_the_next_cached_read() { + let store = TempStore::new("cache-writers", Some("{}")); + let settle = || { + backdate(&store.path, seconds_ago(60)); + store_section(GENERAL_SETTINGS); + }; + + settle(); + assert!(set_store_setting( + GENERAL_SETTINGS, + "hasCompletedStartup", + Value::Bool(true) + )); + assert!(has_completed_startup()); + assert_eq!(GeneralSettings::load(), load_general_settings_separately()); + + settle(); + assert!(set_has_completed_onboarding(true)); + assert!(has_completed_onboarding()); + + settle(); + assert!(set_store_value( + "presets", + serde_json::json!({"presets": [{"name": "Mine", "config": {}}], "default": 0}) + )); + assert_eq!(preset_names(), vec!["Mine".to_string()]); + + settle(); + let mut presets = crate::presets::PresetsStore::load(); + presets.create("Second".into(), serde_json::json!({}), true); + assert!(presets.save()); + assert_eq!(crate::presets::PresetsStore::load(), presets); + + settle(); + assert!(update_store_section(RECORDING_SETTINGS, |section| { + section.insert("mode".into(), Value::String("studio".into())); + true + })); + assert_eq!(recording_mode_slug().as_deref(), Some("studio")); + + settle(); + assert!(set_auth(Some( + serde_json::json!({"secret": {"token": "abc"}, "plan": {"upgraded": true}}) + ))); + assert!(auth_snapshot().signed_in()); + assert!(auth_snapshot().is_upgraded()); + + settle(); + assert!(set_auth(None)); + assert!(!auth_snapshot().signed_in()); + + settle(); + let mut hotkeys = Map::new(); + hotkeys.insert("startStudioRecording".into(), serde_json::json!({})); + assert!(set_hotkeys_raw(&hotkeys)); + assert_eq!(hotkeys_raw(), hotkeys); + + settle(); + let rules = AutomationsStore { + version: 7, + ..AutomationsStore::default() + }; + assert!(set_automations(&rules)); + assert_eq!(automations().version, 7); + + settle(); + assert!(set_store_setting( + GENERAL_SETTINGS, + "enableNotifications", + Value::Bool(false) + )); + assert!(!notification_sounds_enabled()); + } + + #[test] + #[ignore] + fn benchmark_cached_store_section_reads() { + use std::{hint::black_box, time::Instant}; + + let config = crate::presets::preset_config(&cap_project::ProjectConfiguration::default()); + let contents = serde_json::to_string_pretty(&serde_json::json!({ + "general_settings": {"theme": "dark", "maxFps": 60, "hasCompletedStartup": true}, + "recording_start_safety": {"confirmBeforeRecordingWithoutMicrophone": false}, + "audio_enhancement": {"enabledByDefault": true}, + "auth": { + "secret": {"api_key": "k".repeat(64)}, + "plan": {"upgraded": true, "manual": false}, + "organizations": [{"id": "1", "name": "One"}, {"id": "2", "name": "Two"}] + }, + "presets": {"presets": [{"name": "Preset", "config": config}], "default": null}, + "teleprompter": {"script": "A recording script. ".repeat(60)} + })) + .unwrap(); + let store = TempStore::new("cache-benchmark", Some(&contents)); + backdate(&store.path, seconds_ago(60)); + let uncached = || { + read_store(&tauri_store_path()) + .and_then(|mut store| store.remove("auth")) + .and_then(|value| match value { + Value::Object(map) => Some(map), + _ => None, + }) + .unwrap_or_default() + }; + assert_eq!(uncached(), store_section("auth")); + let measure = |read: &dyn Fn() -> Map| { + let start = Instant::now(); + for _ in 0..1000 { + black_box(read()); + } + start.elapsed().as_secs_f64() * 1_000_000.0 / 1000.0 + }; + let mut before = Vec::new(); + let mut after = Vec::new(); + for _ in 0..9 { + before.push(measure(&uncached)); + after.push(measure(&|| store_section("auth"))); + } + before.sort_by(f64::total_cmp); + after.sort_by(f64::total_cmp); + eprintln!( + "store_bytes={} uncached_median_us={:.3} cached_median_us={:.3} uncached_runs_us={before:?} cached_runs_us={after:?}", + contents.len(), + before[4], + after[4] + ); + } + + #[test] + #[ignore] + fn benchmark_general_settings_load() { + use std::{hint::black_box, time::Instant}; + + for preset_count in [0, 10, 50] { + let config = + crate::presets::preset_config(&cap_project::ProjectConfiguration::default()); + let presets: Vec<_> = (0..preset_count) + .map(|index| serde_json::json!({"name": format!("Preset {index}"), "config": config})) + .collect(); + let contents = serde_json::to_string_pretty(&serde_json::json!({ + "general_settings": {"theme": "dark", "maxFps": 60}, + "recording_start_safety": {"confirmBeforeRecordingWithoutMicrophone": false}, + "audio_enhancement": {"enabledByDefault": true}, + "presets": {"presets": presets, "default": null}, + "teleprompter": {"script": "A recording script. ".repeat(100)} + })) + .unwrap(); + let _store = TempStore::new("general-settings-benchmark", Some(&contents)); + assert_eq!(GeneralSettings::load(), load_general_settings_separately()); + let measure = |load: fn() -> GeneralSettings| { + let start = Instant::now(); + for _ in 0..200 { + black_box(load()); + } + start.elapsed().as_secs_f64() * 1_000_000.0 / 200.0 + }; + let mut separate = Vec::new(); + let mut current = Vec::new(); + for round in 0..9 { + if round % 2 == 0 { + separate.push(measure(load_general_settings_separately)); + current.push(measure(GeneralSettings::load)); + } else { + current.push(measure(GeneralSettings::load)); + separate.push(measure(load_general_settings_separately)); + } + } + separate.sort_by(f64::total_cmp); + current.sort_by(f64::total_cmp); + eprintln!( + "presets={preset_count} bytes={} separate_median_us={:.3} current_median_us={:.3} separate_runs_us={separate:?} current_runs_us={current:?}", + contents.len(), + separate[4], + current[4] + ); + } + } + + #[test] + fn section_updates_merge_into_disk_state_and_can_decline() { + let store = TempStore::new( + "section-update", + Some(r#"{"auth":{"user_id":"a","plan":null},"general_settings":{"x":1}}"#), + ); + + assert!(update_store_section("auth", |auth| { + auth.insert("organizations".into(), serde_json::json!([])); + true + })); + let written = store.read(); + assert_eq!(written["auth"]["user_id"], "a"); + assert_eq!(written["auth"]["organizations"], serde_json::json!([])); + assert_eq!(written["general_settings"]["x"], 1); + + assert!(!update_store_section("auth", |auth| { + auth.insert("user_id".into(), "b".into()); + false + })); + assert_eq!(store.read()["auth"]["user_id"], "a"); + } + + #[test] + fn recordings_folder_changes_remember_previous_locations() { + let store = TempStore::new( + "recordings-folder-history", + Some( + r#"{"general_settings":{"recordingsPath":"old","previousRecordingsPaths":["older","new"],"maxFps":30,"future":true},"auth":{"user_id":"keep"}}"#, + ), + ); + + assert!(set_recordings_path(Some("new"))); + let reloaded = store_section(GENERAL_SETTINGS); + assert_eq!(reloaded["recordingsPath"], "new"); + assert_eq!( + strings_at(&reloaded, "previousRecordingsPaths"), + ["older", "old"] + ); + + assert!(set_recordings_path(Some("new"))); + assert_eq!(store_section(GENERAL_SETTINGS), reloaded); + + assert!(set_recordings_path(None)); + let reloaded = store_section(GENERAL_SETTINGS); + assert_eq!(reloaded["recordingsPath"], Value::Null); + assert_eq!( + strings_at(&reloaded, "previousRecordingsPaths"), + ["older", "old", "new"] + ); + + assert!(set_recordings_path(Some("old"))); + let reloaded = store_section(GENERAL_SETTINGS); + assert_eq!(reloaded["recordingsPath"], "old"); + assert_eq!( + strings_at(&reloaded, "previousRecordingsPaths"), + ["older", "new"] + ); + assert_eq!(reloaded["maxFps"], 30); + assert_eq!(store.read()[GENERAL_SETTINGS]["future"], true); + assert_eq!(store.read()["auth"]["user_id"], "keep"); + } + + #[test] + fn recordings_folder_changes_limit_history_without_duplicates() { + let _store = TempStore::new("recordings-folder-history-limit", None); + for index in 0..25 { + assert!(set_recordings_path(Some(&format!("folder-{index}")))); + } + let expected = (4..24) + .map(|index| format!("folder-{index}")) + .collect::>(); + assert_eq!( + strings_at(&store_section(GENERAL_SETTINGS), "previousRecordingsPaths"), + expected + ); + + assert!(set_recordings_path(Some("folder-23"))); + assert!(set_recordings_path(Some("folder-24"))); + assert_eq!( + strings_at(&store_section(GENERAL_SETTINGS), "previousRecordingsPaths"), + expected + ); + } + + #[test] + fn recordings_folder_changes_merge_concurrent_updates() { + let store = TempStore::new( + "recordings-folder-concurrent", + Some(r#"{"general_settings":{"recordingsPath":"original","maxFps":30}}"#), + ); + let barrier = Arc::new(std::sync::Barrier::new(8)); + let workers = (0..8) + .map(|index| { + let path = store.path.clone(); + let barrier = barrier.clone(); + std::thread::spawn(move || { + set_test_tauri_store_path(Some(path)); + barrier.wait(); + let saved = set_recordings_path(Some(&format!("folder-{index}"))); + set_test_tauri_store_path(None); + saved + }) + }) + .collect::>(); + for worker in workers { + assert!(worker.join().unwrap()); + } + let reloaded = store_section(GENERAL_SETTINGS); + let mut paths = strings_at(&reloaded, "previousRecordingsPaths"); + assert_eq!(paths.remove(0), "original"); + paths.push(opt_string_at(&reloaded, "recordingsPath").unwrap()); + paths.sort(); + assert_eq!( + paths, + (0..8) + .map(|index| format!("folder-{index}")) + .collect::>() + ); + assert_eq!(reloaded["maxFps"], 30); + } + + #[test] + fn recordings_folder_changes_preserve_preferences_when_saving_fails() { + let contents = r#"{"general_settings":{"recordingsPath":"old","maxFps":30}}"#; + let store = TempStore::new("recordings-folder-write-failure", Some(contents)); + let staged = store.path.with_extension("gpui-tmp"); + std::fs::create_dir(&staged).unwrap(); + let saved = set_recordings_path(Some("new")); + std::fs::remove_dir(staged).unwrap(); + + assert!(!saved); + assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents); + assert_eq!(store_section(GENERAL_SETTINGS)["recordingsPath"], "old"); + } + + #[test] + fn recordings_folder_changes_preserve_unreadable_stores() { + let store = TempStore::new("recordings-folder-corrupt", Some("not JSON")); + assert!(!set_recordings_path(Some("new"))); + assert!(!set_recordings_path(None)); + assert_eq!(std::fs::read_to_string(&store.path).unwrap(), "not JSON"); + } + + #[test] + fn notification_sounds_follow_tauri_settings_without_writing() { + for (contents, enabled) in [ + (None, false), + (Some("{}"), false), + (Some("invalid json"), false), + (Some(r#"{"general_settings":{}}"#), true), + ( + Some(r#"{"general_settings":{"enableNotifications":false}}"#), + false, + ), + ( + Some(r#"{"general_settings":{"enableNotifications":true}}"#), + true, + ), + ] { + let store = TempStore::new("notification-sounds", contents); + assert_eq!(notification_sounds_enabled(), enabled); + assert_eq!( + std::fs::read_to_string(&store.path).ok().as_deref(), + contents + ); + } + } + + #[test] + fn device_format_preferences_use_tauri_keys_and_preserve_other_fields() { + use cap_recording::feeds::{ + camera::CameraDeviceSettings, microphone::MicrophoneDeviceSettings, + }; + let store = TempStore::new( + "device-format-preferences", + Some( + r#"{"recording_settings":{"mode":"studio","cameraDeviceSettings":{"device:camera-1":{"width":1280,"height":720,"frameRate":30,"future":7},"model:vendor:product":{"frameRate":60},"device:other":{"width":640}},"microphoneDeviceSettings":{"Desk":{"sampleRate":48000,"channels":1,"future":9},"Other":{"channels":2}},"future":{"keep":true}},"unrelated":{"keep":true}}"#, + ), + ); + let before = store.read(); + let model = cap_camera::ModelID::try_from("vendor:product".to_string()).unwrap(); + let snapshot = RecordingDeviceSettings { + camera: RecordingDeviceSettings::for_camera("camera-1", Some(&model)), + microphone: RecordingDeviceSettings::for_microphone("Desk"), + }; + assert_eq!(snapshot.camera.unwrap().frame_rate, Some(30.)); + assert_eq!( + RecordingDeviceSettings::for_camera("replugged", Some(&model)) + .unwrap() + .frame_rate, + Some(60.) + ); + let camera = CameraDeviceSettings { + width: Some(1920), + height: Some(1080), + frame_rate: Some(30.), + }; + assert!(set_camera_device_settings("camera-1", Some(&model), camera)); + assert!(set_microphone_device_settings( + "Desk", + MicrophoneDeviceSettings { + sample_rate: Some(44100), + channels: Some(2) + } + )); + assert_eq!( + RecordingDeviceSettings::for_camera("camera-1", Some(&model)), + Some(camera) + ); + assert_eq!( + RecordingDeviceSettings::for_camera("replugged", Some(&model)), + Some(camera) + ); + assert_eq!(snapshot.camera.unwrap().width, Some(1280)); + assert_eq!(snapshot.microphone.unwrap().sample_rate, Some(48000)); + let after = store.read(); + assert_eq!( + after[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:camera-1"]["future"], + 7 + ); + assert_eq!( + after[RECORDING_SETTINGS]["microphoneDeviceSettings"]["Desk"]["future"], + 9 + ); + assert_eq!( + after[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:other"], + before[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:other"] + ); + assert_eq!( + after[RECORDING_SETTINGS]["microphoneDeviceSettings"]["Other"], + before[RECORDING_SETTINGS]["microphoneDeviceSettings"]["Other"] + ); + assert_eq!(after["unrelated"], before["unrelated"]); + assert_eq!( + after[RECORDING_SETTINGS]["future"], + before[RECORDING_SETTINGS]["future"] + ); + assert!(set_camera_device_settings( + "camera-1", + Some(&model), + CameraDeviceSettings::default() + )); + assert!( + store.read()[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:camera-1"] + .get("frameRate") + .is_none() + ); + assert_eq!( + store.read()[RECORDING_SETTINGS]["cameraDeviceSettings"]["device:camera-1"]["future"], + 7 + ); + } + + #[test] + fn device_format_save_failure_does_not_replace_unreadable_preferences() { + let store = TempStore::new("device-format-invalid-store", Some("{broken")); + let before = std::fs::read(&store.path).unwrap(); + assert!(!set_microphone_device_settings("Desk", Default::default())); + assert_eq!(std::fs::read(&store.path).unwrap(), before); + } + + #[test] + fn recording_inputs_round_trip_without_replacing_unknown_settings() { + use cap_recording::feeds::camera::DeviceOrModelID; + + let store = TempStore::new( + "recording-inputs-round-trip", + Some( + r#"{"recording_settings":{"mode":"studio","cameraId":{"ModelID":"vendor:product"},"micName":"Desk microphone","cameraDeviceSettings":{"model:vendor:product":{"frameRate":30}},"unknown":{"keep":true}},"other":{"keep":[1,2]}}"#, + ), + ); + let original = store.read(); + let model = cap_camera::ModelID::try_from("vendor:product".to_string()).unwrap(); + assert_eq!( + RecordingInputSettings::load(), + RecordingInputSettings { + camera_id: Some(DeviceOrModelID::ModelID(model)), + microphone_name: Some("Desk microphone".to_string()), + } + ); + + let camera = DeviceOrModelID::DeviceID("replacement-camera".to_string()); + assert!(set_recording_camera_id(Some(&camera))); + assert!(set_recording_microphone_name(Some( + "Replacement microphone" + ))); + assert_eq!( + RecordingInputSettings::load(), + RecordingInputSettings { + camera_id: Some(camera), + microphone_name: Some("Replacement microphone".to_string()), + } + ); + + assert!(set_recording_camera_id(None)); + assert!(set_recording_microphone_name(None)); + assert_eq!( + RecordingInputSettings::load(), + RecordingInputSettings::default() + ); + let mut expected = original; + expected[RECORDING_SETTINGS]["cameraId"] = Value::Null; + expected[RECORDING_SETTINGS]["micName"] = Value::Null; + assert_eq!(store.read(), expected); + } + + #[test] + fn system_audio_restores_and_saves_without_touching_devices() { + let store = TempStore::new( + "recording-inputs-system-audio", + Some(r#"{"recording_settings":{"systemAudio":true,"micName":"Desk"}}"#), + ); + assert!(recording_system_audio()); + + assert!(set_recording_system_audio(false)); + assert!(!recording_system_audio()); + assert_eq!(store.read()[RECORDING_SETTINGS]["systemAudio"], false); + assert_eq!(store.read()[RECORDING_SETTINGS]["micName"], "Desk"); + drop(store); + + let _store = TempStore::new( + "recording-inputs-system-audio-malformed", + Some(r#"{"recording_settings":{"systemAudio":"yes"}}"#), + ); + assert!(!recording_system_audio()); + } + + #[test] + fn recording_inputs_keep_saved_identity_without_device_enumeration() { + let store = TempStore::new( + "recording-inputs-absent-device", + Some( + r#"{"recording_settings":{"cameraId":{"DeviceID":"unplugged-camera"},"micName":"Unplugged microphone"}}"#, + ), + ); + let before = std::fs::read(&store.path).unwrap(); + let first = RecordingInputSettings::load(); + assert_eq!( + first.microphone_name.as_deref(), + Some("Unplugged microphone") + ); + assert_eq!( + first.camera_id, + Some(cap_recording::feeds::camera::DeviceOrModelID::DeviceID( + "unplugged-camera".into() + )) + ); + assert_eq!(RecordingInputSettings::load(), first); + assert_eq!(std::fs::read(&store.path).unwrap(), before); + } + + #[test] + fn recording_inputs_malformed_camera_does_not_panic_or_clear_other_settings() { + let store = TempStore::new( + "recording-inputs-malformed-model", + Some( + r#"{"recording_settings":{"cameraId":{"ModelID":"missing-separator"},"micName":"Valid microphone","future":42}}"#, + ), + ); + let before = store.read(); + let settings = RecordingInputSettings::load(); + assert!(settings.camera_id.is_none()); + assert_eq!( + settings.microphone_name.as_deref(), + Some("Valid microphone") + ); + assert!(set_recording_microphone_name(None)); + let after = store.read(); + assert_eq!( + after[RECORDING_SETTINGS]["cameraId"], + before[RECORDING_SETTINGS]["cameraId"] + ); + assert_eq!(after[RECORDING_SETTINGS]["future"], 42); + } + + #[test] + fn concurrent_store_writers_preserve_each_others_changes() { + let contents = serde_json::json!({"unknown": "x".repeat(64 * 1024)}).to_string(); + let store = TempStore::new("concurrent-writers", Some(&contents)); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(16)); + let workers = (0..16) + .map(|index| { + let path = store.path.clone(); + let barrier = barrier.clone(); + std::thread::spawn(move || { + set_test_tauri_store_path(Some(path)); + barrier.wait(); + let saved = match index { + 0..=3 => set_store_setting( + GENERAL_SETTINGS, + &format!("setting-{index}"), + Value::from(index), + ), + 4..=7 => set_store_value(&format!("section-{index}"), Value::from(index)), + 8..=11 => set_microphone_device_settings( + &format!("microphone-{index}"), + cap_recording::feeds::microphone::MicrophoneDeviceSettings { + sample_rate: Some(48000), + channels: Some(2), + }, + ), + _ => set_automations(&AutomationsStore::default()), + }; + set_test_tauri_store_path(None); + saved + }) + }) + .collect::>(); + let results = workers + .into_iter() + .map(|worker| worker.join().unwrap()) + .collect::>(); + assert!(results.into_iter().all(|saved| saved)); + + let after = store.read(); + assert_eq!(after["unknown"], "x".repeat(64 * 1024)); + for index in 0..4 { + assert_eq!(after[GENERAL_SETTINGS][format!("setting-{index}")], index); + } + for index in 4..8 { + assert_eq!(after[format!("section-{index}")], index); + } + for index in 8..12 { + assert_eq!( + after[RECORDING_SETTINGS]["microphoneDeviceSettings"] + [format!("microphone-{index}")], + serde_json::json!({"sampleRate": 48000, "channels": 2}) + ); + } + assert_eq!( + after[AUTOMATIONS], + serde_json::to_value(AutomationsStore::default()).unwrap() + ); + } + + #[test] + fn temporary_stores_are_thread_local_without_mutating_process_environment() { + let original = std::env::var_os("CAP_GPUI_TAURI_STORE"); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(4)); + let workers = (0..4) + .map(|index| { + let barrier = barrier.clone(); + let original = original.clone(); + std::thread::spawn(move || { + let store = TempStore::new(&format!("thread-local-{index}"), None); + assert_eq!(super::tauri_store_path(), store.path); + assert!(super::set_store_setting( + GENERAL_SETTINGS, + "maxFps", + Value::from(24 + index) + )); + barrier.wait(); + assert_eq!(GeneralSettings::load().max_fps, 24 + index); + assert_eq!(std::env::var_os("CAP_GPUI_TAURI_STORE"), original); + }) + }) + .collect::>(); + + for worker in workers { + worker.join().unwrap(); + } + assert_eq!(std::env::var_os("CAP_GPUI_TAURI_STORE"), original); + } + + /// The compatibility contract: writing one setting must leave every key + /// this app has never heard of exactly where it was -- other sections, + /// other `general_settings` keys, and nested objects inside them. + #[test] + fn writing_one_setting_preserves_unknown_keys() { + let store = TempStore::new( + "unknown-keys", + Some( + r#"{ + "auth": { "secret": { "token": "keep-me" }, "user_id": "u_1" }, + "presets": { "presets": [], "default": 0 }, + "general_settings": { + "hideDockIcon": false, + "instanceId": "fe9cd6a0-fbb0-49b0-b321-5593cb3d4190", + "cameraWindowPositionsByMonitorName": { "Built-in Retina Display": { "x": 1.0, "y": 2.0 } }, + "someSettingFromANewerBuild": [1, 2, 3] + }, + "a_migration_flag_v1": true +}"#, + ), + ); + + assert!(super::set_store_setting( + GENERAL_SETTINGS, + "hideDockIcon", + Value::Bool(true) + )); + + let after = store.read(); + // The touched key changed... + assert_eq!(after["general_settings"]["hideDockIcon"], Value::Bool(true)); + // ...and nothing else did. + assert_eq!(after["auth"]["secret"]["token"], "keep-me"); + assert_eq!(after["auth"]["user_id"], "u_1"); + assert_eq!(after["presets"]["default"], 0); + assert_eq!(after["a_migration_flag_v1"], Value::Bool(true)); + assert_eq!( + after["general_settings"]["instanceId"], + "fe9cd6a0-fbb0-49b0-b321-5593cb3d4190" + ); + assert_eq!( + after["general_settings"]["cameraWindowPositionsByMonitorName"]["Built-in Retina Display"] + ["x"], + 1.0 + ); + assert_eq!( + after["general_settings"]["someSettingFromANewerBuild"], + serde_json::json!([1, 2, 3]) + ); + + // A second write against a store that already round-tripped once must + // still see the first one -- i.e. the read half is reading what the + // write half wrote. + assert!(super::set_store_setting( + RECORDING_START_SAFETY, + "confirmBeforeRecordingWithoutMicrophone", + Value::Bool(false) + )); + assert!(super::set_studio_sound_by_default(false)); + let settings = GeneralSettings::load(); + assert!(settings.hide_dock_icon); + assert!(!settings.confirm_without_microphone); + assert!(!settings.studio_sound_by_default); + // The section it created did not disturb the others. + assert_eq!(store.read()["auth"]["user_id"], "u_1"); + } + + #[test] + fn ui_language_round_trips_without_touching_other_keys() { + let store = TempStore::new( + "ui-language", + Some( + r#"{"general_settings":{"theme":"dark","uiLanguage":"pt-BR","futureSetting":{"keep":[1]}}}"#, + ), + ); + assert_eq!( + GeneralSettings::load().ui_language.as_deref(), + Some("pt-BR") + ); + assert!(super::set_store_setting( + GENERAL_SETTINGS, + crate::i18n::SETTING_KEY, + Value::from("fr") + )); + let after = store.read(); + assert_eq!(after["general_settings"]["uiLanguage"], "fr"); + assert_eq!(after["general_settings"]["theme"], "dark"); + assert_eq!( + after["general_settings"]["futureSetting"], + serde_json::json!({ "keep": [1] }) + ); + assert_eq!(GeneralSettings::load().ui_language.as_deref(), Some("fr")); + } + + /// A store that does not parse is someone's settings in a state we do not + /// understand. Writing a fresh object over it would delete their auth + /// token and every preset. + #[test] + fn a_corrupt_store_is_never_overwritten() { + for contents in ["{ this is not json", r#"{"auth":"token""#, "null", "[]"] { + let store = TempStore::new("corrupt", Some(contents)); + assert!(!super::set_store_setting( + GENERAL_SETTINGS, + "hideDockIcon", + Value::Bool(true) + )); + assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents); + } + } + + /// A zero-filled store (NTFS after a power loss) or one cut off mid-object + /// blocked onboarding forever. Onboarding sets the unreadable bytes aside + /// and saves its progress; every other writer still refuses. + #[test] + fn onboarding_progress_sets_aside_an_unparseable_store() { + for contents in [ + "\0\0\0\0\0\0\0\0", + "{ this is not json", + r#"{"auth":"token""#, + "null", + "[]", + ] { + let store = TempStore::new("unparseable-onboarding", Some(contents)); + assert!(!set_store_setting( + GENERAL_SETTINGS, + "hideDockIcon", + Value::Bool(true) + )); + assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents); + assert!(!has_completed_startup()); + + assert!(set_has_completed_startup(true)); + assert!(set_has_completed_onboarding(true)); + assert!(has_completed_startup()); + assert!(has_completed_onboarding()); + assert_eq!( + store.read(), + serde_json::json!({ + "general_settings": { + "hasCompletedStartup": true, + "hasCompletedOnboarding": true + } + }) + ); + + let directory = store.path.parent().unwrap(); + let prefix = format!( + "{}.unreadable-", + store.path.file_name().unwrap().to_string_lossy() + ); + let kept = std::fs::read_dir(directory) + .unwrap() + .filter_map(Result::ok) + .filter(|entry| entry.file_name().to_string_lossy().starts_with(&prefix)) + .map(|entry| entry.path()) + .collect::>(); + assert_eq!(kept.len(), 1, "{kept:?}"); + assert_eq!(std::fs::read_to_string(&kept[0]).unwrap(), contents); + std::fs::remove_file(&kept[0]).unwrap(); + } + } + + #[test] + fn onboarding_progress_leaves_an_unreadable_store_alone() { + let store = TempStore::new("unreadable-onboarding", None); + std::fs::create_dir_all(&store.path).unwrap(); + assert!(!set_has_completed_startup(true)); + assert!(store.path.is_dir()); + std::fs::remove_dir(&store.path).unwrap(); + } + + #[test] + fn onboarding_progress_recovers_an_empty_store() { + for contents in ["", " \t\r\n"] { + let store = TempStore::new("empty-onboarding", Some(contents)); + assert_eq!(GeneralSettings::load(), GeneralSettings::default()); + assert!(!has_completed_startup()); + assert!(!has_completed_onboarding()); + + assert!(set_has_completed_startup(true)); + assert!(has_completed_startup()); + assert!(set_has_completed_onboarding(true)); + assert!(has_completed_onboarding()); + assert_eq!( + store.read(), + serde_json::json!({ + "general_settings": { + "hasCompletedStartup": true, + "hasCompletedOnboarding": true + } + }) + ); + } + } + + /// Every value the page can read back has to survive the round trip, and + /// an unknown enum value must degrade to that one row's default rather + /// than blanking the page. + #[test] + fn typed_reads_match_the_store_shape() { + let _store = TempStore::new( + "typed-reads", + Some( + r#"{ + "general_settings": { + "theme": "dark", + "studioRecordingQuality": "ultra", + "postDeletionBehaviour": "somethingFromTheFuture", + "recordingCountdown": 5, + "custom_cursor_capture2": false, + "defaultZoomAmount": 2.5, + "maxFps": 120, + "serverUrl": "https://cap.example.com", + "excludedWindows": [{ "bundleIdentifier": null, "ownerName": null, "windowTitle": "Cap" }] + } +}"#, + ), + ); + + let settings = GeneralSettings::load(); + assert_eq!(settings.theme, AppTheme::Dark); + assert_eq!(settings.studio_recording_quality, StudioQuality::Ultra); + // Unknown value -> this row's default, everything else intact. + assert_eq!( + settings.post_deletion_behaviour, + PostDeletionBehaviour::DoNothing + ); + assert_eq!(settings.recording_countdown, Some(5)); + assert!(!settings.custom_cursor_capture); + assert_eq!(settings.default_zoom_amount, Some(2.5)); + assert_eq!(settings.new_zoom_amount(), 2.5); + assert_eq!(settings.max_fps, 120); + assert_eq!(settings.server_url, "https://cap.example.com"); + assert_eq!(settings.excluded_windows.len(), 1); + assert_eq!(settings.excluded_windows[0].primary_label(), "Cap"); + // Absent keys take the page's defaults, not the struct's zero values. + assert!(settings.enable_notifications); + assert!(settings.crash_recovery_recording); + assert!(settings.confirm_without_microphone); + assert!(settings.studio_sound_by_default); + assert_eq!(settings.instant_mode_max_resolution, 1920); + assert_eq!(GeneralSettings::default().new_zoom_amount(), 1.5); + } + + /// The teleprompter's own section: defaults when absent, and a per-key + /// write that leaves the rest of the section (and the rest of the store) + /// alone -- the window writes only the fields the user touched. + #[test] + fn the_teleprompter_section_round_trips_per_key() { + let store = TempStore::new( + "teleprompter", + Some(r#"{ "auth": { "user_id": "u_1" }, "teleprompter": { "script": "hello" } }"#), + ); + + let loaded = TeleprompterState::load(); + assert_eq!(loaded.script, "hello"); + // Everything the section does not carry takes `teleprompterDefaults`. + assert_eq!(loaded.font_size, 30.); + assert_eq!(loaded.words_per_minute, 150); + assert_eq!(loaded.line_height, 1.5); + assert!(loaded.show_cue_markers); + assert!(!loaded.mirror); + assert_eq!(loaded.window_opacity_percent, 92); + + let mut next = loaded.clone(); + next.script = "hello there".into(); + next.font_size = 34.; + for key in [TeleprompterState::SCRIPT, TeleprompterState::FONT_SIZE] { + assert!(super::set_store_setting( + TELEPROMPTER, + key, + next.value_for(key) + )); + } + + let after = store.read(); + assert_eq!(after["teleprompter"]["script"], "hello there"); + assert_eq!(after["teleprompter"]["fontSize"], 34.0); + // The two untouched keys were never written, and the rest of the store + // is where it was. + assert!(after["teleprompter"].get("mirror").is_none()); + assert_eq!(after["auth"]["user_id"], "u_1"); + + let reloaded = TeleprompterState::load(); + assert_eq!(reloaded.script, "hello there"); + assert_eq!(reloaded.font_size, 34.); + assert!(reloaded.show_cue_markers); + } + + /// An absent store is an empty one -- a fresh install must not be a write + /// failure, and the file appears on the first change. + #[test] + fn a_missing_store_is_created() { + let _store = TempStore::new("missing", None); + + assert_eq!(GeneralSettings::load(), GeneralSettings::default()); + assert!(super::set_store_setting( + GENERAL_SETTINGS, + "enableTelemetry", + Value::Bool(false) + )); + assert!(!GeneralSettings::load().enable_telemetry); + } + + #[test] + fn bundled_resource_paths_follow_the_installed_executable() { + let executable = std::path::Path::new("/Applications/Cap.app/Contents/MacOS/cap-gpui"); + let override_dir = std::path::Path::new("/tmp/cap-resources"); + + assert_eq!( + super::bundled_resource_dirs_for(Some(executable), Some(override_dir)), + vec![ + override_dir.to_path_buf(), + std::path::PathBuf::from("/Applications/Cap.app/Contents/Resources"), + std::path::PathBuf::from("/Applications/Cap.app/Contents/MacOS/resources"), + std::path::PathBuf::from("/Applications/Cap.app/Contents/MacOS"), + ] + ); + + let executable = std::path::Path::new("/opt/cap/cap-gpui"); + assert_eq!( + super::bundled_resource_dirs_for(Some(executable), None), + vec![ + std::path::PathBuf::from("/opt/cap/resources"), + std::path::PathBuf::from("/opt/cap"), + ] + ); + + let executable = std::path::Path::new("/usr/bin/cap-gpui"); + assert_eq!( + super::bundled_resource_dirs_for(Some(executable), None), + vec![ + std::path::PathBuf::from("/usr/lib/cap"), + std::path::PathBuf::from("/usr/bin/resources"), + std::path::PathBuf::from("/usr/bin"), + ] + ); + } + + /// `normalizeTranscriptionHints`: NULs stripped, whitespace trimmed, + /// empties and duplicates dropped, first occurrence wins. + #[test] + fn transcription_hints_normalize_like_the_web_app() { + assert_eq!( + normalize_transcription_hints([" Cap ", "Cap", "", "\0", "Type\0Script", "cap"]), + ["Cap", "TypeScript", "cap"] + ); + + let _store = TempStore::new( + "hints", + Some(r#"{ "general_settings": { "transcriptionHints": ["Cap", "Acme"] } }"#), + ); + assert_eq!(transcription_hints(), ["Cap", "Acme"]); + } + + /// An absent `transcriptionHints` key shows the four defaults + /// (`createDefaultGeneralSettings`), an empty array shows none. + #[test] + fn transcription_hints_default_only_when_absent() { + { + let _store = TempStore::new("hints-absent", Some(r#"{ "general_settings": {} }"#)); + assert_eq!(transcription_hints(), DEFAULT_TRANSCRIPTION_HINTS); + } + { + let _store = TempStore::new( + "hints-empty", + Some(r#"{ "general_settings": { "transcriptionHints": [] } }"#), + ); + assert!(transcription_hints().is_empty()); + } + } + + /// The hotkeys map round-trips through the exact JSON the Tauri app's + /// `HotkeysStore` writes, and unknown actions survive a rewrite. + #[test] + fn hotkeys_round_trip_the_tauri_shape() { + let store = TempStore::new( + "hotkeys", + Some( + r#"{ "hotkeys": { "hotkeys": { + "stopRecording": { "code": "KeyS", "meta": true, "ctrl": false, "alt": false, "shift": true }, + "someFutureAction": { "code": "F13", "meta": false, "ctrl": false, "alt": false, "shift": false, "extra": 1 } +} } }"#, + ), + ); + + let mut map = hotkeys_raw(); + let stop = hotkey_from_value(map.get("stopRecording").unwrap()).unwrap(); + assert_eq!(stop.code, "KeyS"); + assert!(stop.meta && stop.shift && !stop.ctrl && !stop.alt); + + map.insert( + "screenshotArea".to_string(), + serde_json::to_value(Hotkey { + code: "Digit4".into(), + meta: true, + ctrl: false, + alt: false, + shift: true, + }) + .unwrap(), + ); + assert!(set_hotkeys_raw(&map)); + + let after = store.read(); + assert_eq!( + after["hotkeys"]["hotkeys"]["screenshotArea"]["code"], + "Digit4" + ); + // The unknown action's binding -- including the field this build does + // not model -- is still there. + assert_eq!(after["hotkeys"]["hotkeys"]["someFutureAction"]["extra"], 1); + } + + #[test] + fn the_marker_shortcut_default_is_offered_once() { + let store = TempStore::new("marker-seed", Some(r#"{ "hotkeys": { "hotkeys": {} } }"#)); + seed_recording_marker_hotkey(); + let default = default_recording_marker_hotkey(); + assert_eq!( + hotkey_from_value(&hotkeys_raw()[RECORDING_MARKER_HOTKEY]), + Some(default.clone()) + ); + assert_eq!( + store.read()["hotkeys"][RECORDING_MARKER_HOTKEY_SEEDED], + true + ); + + let mut cleared = hotkeys_raw(); + cleared.remove(RECORDING_MARKER_HOTKEY); + assert!(set_hotkeys_raw(&cleared)); + seed_recording_marker_hotkey(); + assert!(!hotkeys_raw().contains_key(RECORDING_MARKER_HOTKEY)); + } + + #[test] + fn the_marker_shortcut_default_never_takes_a_combo_in_use() { + let default = serde_json::to_string(&default_recording_marker_hotkey()).unwrap(); + let store = TempStore::new( + "marker-seed-taken", + Some(&format!( + r#"{{ "hotkeys": {{ "hotkeys": {{ "stopRecording": {default} }} }} }}"# + )), + ); + seed_recording_marker_hotkey(); + assert!(!hotkeys_raw().contains_key(RECORDING_MARKER_HOTKEY)); + assert_eq!( + store.read()["hotkeys"][RECORDING_MARKER_HOTKEY_SEEDED], + true + ); + } + + #[test] + fn the_draw_shortcut_default_is_offered_once_and_never_over_a_combo_in_use() { + let store = TempStore::new("draw-seed", Some(r#"{ "hotkeys": { "hotkeys": {} } }"#)); + seed_draw_hotkey(); + if !crate::live_draw::SUPPORTED { + assert!(!hotkeys_raw().contains_key(DRAW_HOTKEY)); + return; + } + assert_eq!( + hotkey_from_value(&hotkeys_raw()[DRAW_HOTKEY]), + Some(default_draw_hotkey()) + ); + assert_eq!(store.read()["hotkeys"][DRAW_HOTKEY_SEEDED], true); + let mut cleared = hotkeys_raw(); + cleared.remove(DRAW_HOTKEY); + assert!(set_hotkeys_raw(&cleared)); + seed_draw_hotkey(); + assert!(!hotkeys_raw().contains_key(DRAW_HOTKEY)); + drop(store); + + let default = serde_json::to_string(&default_draw_hotkey()).unwrap(); + let _taken = TempStore::new( + "draw-seed-taken", + Some(&format!( + r#"{{ "hotkeys": {{ "hotkeys": {{ "stopRecording": {default} }} }} }}"# + )), + ); + seed_draw_hotkey(); + assert!(!hotkeys_raw().contains_key(DRAW_HOTKEY)); + } + + #[test] + fn drawings_fade_unless_kept() { + let _store = TempStore::new("keep-drawings", Some(r#"{ "general_settings": {} }"#)); + assert!(!keep_drawings()); + assert!(!GeneralSettings::load().keep_drawings); + assert!(set_store_setting( + GENERAL_SETTINGS, + KEEP_DRAWINGS_KEY, + Value::Bool(true) + )); + assert!(keep_drawings()); + assert!(GeneralSettings::load().keep_drawings); + } + + #[test] + fn the_marker_sound_is_off_until_turned_on() { + let _store = TempStore::new("marker-sound", Some(r#"{ "general_settings": {} }"#)); + assert!(!recording_marker_sound_enabled()); + assert!(!GeneralSettings::load().recording_marker_sound); + assert!(set_store_setting( + GENERAL_SETTINGS, + RECORDING_MARKER_SOUND_KEY, + Value::Bool(true) + )); + assert!(recording_marker_sound_enabled()); + assert!(GeneralSettings::load().recording_marker_sound); + } + + /// The automations model against the JSON `crates/automation/src/types.rs` + /// serializes: camelCase everywhere, `type`-tagged conditions/actions, and + /// the whole key written wholesale. + #[test] + fn automations_round_trip_the_tauri_shape() { + let store = TempStore::new( + "automations", + Some( + r#"{ "auth": { "user_id": "u_1" }, "automations": { "version": 1, "rules": [ { + "id": "r1", "name": "", "enabled": true, "trigger": "screenshotTaken", "matchMode": "all", + "conditions": [ { "type": "windowTitleContains", "pattern": "Slack" } ], + "actions": [ + { "type": "copyToClipboard", "source": "raw" }, + { "type": "export", "profile": { "format": "mp4", "fps": 30, "resolutionBase": { "x": 1920, "y": 1080 }, "compression": "web", "presetName": null }, "destination": "projectFolder" }, + { "type": "webhook", "url": "https://x.test", "method": "POST", "headers": {}, "bodyTemplate": null } + ] } ] } }"#, + ), + ); + + let mut automations = automations(); + assert_eq!(automations.rules.len(), 1); + let rule = &automations.rules[0]; + assert_eq!(rule.trigger, Trigger::ScreenshotTaken); + assert_eq!(rule.match_mode, MatchMode::All); + assert_eq!( + rule.conditions[0], + Condition::WindowTitleContains { + pattern: "Slack".into() + } + ); + assert!(matches!( + &rule.actions[1], + Action::Export { + profile, + destination: ExportDestination::ProjectFolder, + } if profile.format == ExportFormat::Mp4 && profile.resolution_base.x == 1920 + )); + + automations.rules[0].actions.push(Action::SaveToLocation { + dir: "/tmp/shots".into(), + filename_template: None, + }); + assert!(set_automations(&automations)); + + let after = store.read(); + let actions = &after["automations"]["rules"][0]["actions"]; + assert_eq!(actions[3]["type"], "saveToLocation"); + assert_eq!(actions[3]["dir"], "/tmp/shots"); + assert_eq!(actions[3]["filenameTemplate"], Value::Null); + assert_eq!(actions[1]["profile"]["resolutionBase"]["x"], 1920); + // The rest of the store is untouched by the wholesale key write. + assert_eq!(after["auth"]["user_id"], "u_1"); + } + + /// The commercial license key, the shape license.tsx writes on activation. + #[test] + fn commercial_license_round_trips() { + let store = TempStore::new( + "license", + Some(r#"{ "general_settings": { "instanceId": "abc" } }"#), + ); + + assert_eq!(commercial_license(), None); + assert_eq!(instance_id_or_create().as_deref(), Some("abc")); + + let license = CommercialLicense { + license_key: "key-1".into(), + expiry_date: None, + refresh: 123.0, + activated_on: 456.0, + }; + assert!(set_commercial_license(Some(&license))); + assert_eq!(commercial_license(), Some(license)); + let written = store.read(); + assert_eq!( + written["general_settings"]["commercialLicense"]["licenseKey"], + "key-1" + ); + assert_eq!( + written["general_settings"]["commercialLicense"]["activatedOn"], + 456.0 + ); + + // Deactivation nulls the key, which both apps read as "no license". + assert!(set_commercial_license(None)); + assert_eq!(commercial_license(), None); + } + + #[test] + fn uuids_are_v4_shaped_and_distinct() { + let a = new_uuid_v4(); + let b = new_uuid_v4(); + assert_ne!(a, b); + assert_eq!(a.len(), 36); + assert_eq!(a.as_bytes()[14], b'4'); + assert!(matches!(a.as_bytes()[19], b'8' | b'9' | b'a' | b'b')); + } + + /// The `window_exclusion.rs` matching tests, ported with the module: the + /// two apps must agree on which windows a rule excludes. + #[test] + fn exclusion_matches_by_each_field_and_owner_title_requires_both() { + let title = WindowExclusion { + window_title: Some("Cap Camera".into()), + ..Default::default() + }; + assert!(title.matches(None, None, Some("Cap Camera"))); + assert!(!title.matches(None, None, Some("Other Window"))); + assert!(!title.matches(None, None, None)); + + let bundle = WindowExclusion { + bundle_identifier: Some("com.cap.desktop".into()), + ..Default::default() + }; + assert!(bundle.matches(Some("com.cap.desktop"), None, None)); + assert!(!bundle.matches(Some("com.other.app"), None, None)); + + let owner = WindowExclusion { + owner_name: Some("Cap".into()), + ..Default::default() + }; + assert!(owner.matches(None, Some("Cap"), None)); + assert!(!owner.matches(None, Some("Other"), None)); + + let owner_and_title = WindowExclusion { + owner_name: Some("Cap".into()), + window_title: Some("Cap Camera".into()), + ..Default::default() + }; + assert!(owner_and_title.matches(None, Some("Cap"), Some("Cap Camera"))); + assert!(!owner_and_title.matches(None, Some("Cap"), Some("Wrong Title"))); + assert!(!owner_and_title.matches(None, Some("Wrong Owner"), Some("Cap Camera"))); + assert!(!owner_and_title.matches(None, None, Some("Cap Camera"))); + + let empty = WindowExclusion::default(); + assert!(!empty.matches(None, None, None)); + assert!(!empty.matches(Some("any"), Some("any"), Some("any"))); + + let bundle_first = WindowExclusion { + bundle_identifier: Some("com.cap.desktop".into()), + window_title: Some("Cap Camera".into()), + ..Default::default() + }; + assert!(bundle_first.matches(Some("com.cap.desktop"), None, Some("Wrong"))); + } + + #[test] + fn editor_preview_quality_defaults_per_machine_until_chosen() { + let read = |quality: Option<&str>, chosen: Option| { + let mut general = Map::new(); + if let Some(quality) = quality { + general.insert("editorPreviewQuality".to_string(), Value::from(quality)); + } + if let Some(chosen) = chosen { + general.insert( + EDITOR_PREVIEW_QUALITY_CHOSEN_KEY.to_string(), + Value::from(chosen), + ); + } + GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) + .editor_preview_quality + }; + let machine = EditorPreviewQuality::for_this_machine(); + if cfg!(all(target_os = "macos", target_arch = "aarch64")) { + assert_eq!(machine, EditorPreviewQuality::Full); + } + if cfg!(target_os = "linux") { + assert_eq!(machine, EditorPreviewQuality::Half); + } + assert_eq!(read(None, None), machine); + assert_eq!(read(Some("half"), None), machine); + assert_eq!(read(Some("half"), Some(false)), machine); + assert_eq!(read(Some("bogus"), Some(true)), machine); + assert_eq!(read(Some("half"), Some(true)), EditorPreviewQuality::Half); + assert_eq!(read(Some("full"), None), EditorPreviewQuality::Full); + assert_eq!(read(Some("quarter"), None), EditorPreviewQuality::Quarter); + assert_eq!(read(Some("full"), Some(true)), EditorPreviewQuality::Full); + } + + #[test] + fn choosing_half_keeps_it_over_the_machine_default() { + let store = TempStore::new( + "preview-quality", + Some(r#"{ "general_settings": { "editorPreviewQuality": "half", "theme": "dark" } }"#), + ); + assert_eq!( + GeneralSettings::load().editor_preview_quality, + EditorPreviewQuality::for_this_machine() + ); + assert!(set_editor_preview_quality(EditorPreviewQuality::Half)); + let written = store.read(); + assert_eq!(written["general_settings"]["editorPreviewQuality"], "half"); + assert_eq!( + written["general_settings"][EDITOR_PREVIEW_QUALITY_CHOSEN_KEY], + true + ); + assert_eq!(written["general_settings"]["theme"], "dark"); + assert_eq!( + GeneralSettings::load().editor_preview_quality, + EditorPreviewQuality::Half + ); + } + + #[test] + fn editor_preview_speed_reads_offered_steps_and_falls_back_to_normal() { + let read = |value: Option| { + let mut general = Map::new(); + if let Some(value) = value { + general.insert(EDITOR_PREVIEW_SPEED_KEY.to_string(), value); + } + GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) + .editor_preview_speed + .as_f64() + }; + assert_eq!(read(None), 1.0); + for speed in cap_editor::PreviewSpeed::ALL { + assert_eq!( + read(Some(serde_json::json!(speed.as_f64()))), + speed.as_f64() + ); + } + assert_eq!(read(Some(serde_json::json!(2))), 2.0); + assert_eq!(read(Some(serde_json::json!(2.5))), 1.0); + assert_eq!(read(Some(serde_json::json!(-1))), 1.0); + assert_eq!(read(Some(serde_json::json!("2"))), 1.0); + assert_eq!(read(Some(Value::Null)), 1.0); + } + + /// Key absent -> the default exclusion list (the Rust struct's serde + /// default); key present but empty -> the user cleared every entry. + #[test] + fn excluded_windows_default_only_when_key_is_absent() { + let absent = GeneralSettings::from_sections(&Map::new(), &Map::new(), &Map::new()); + assert_eq!(absent.excluded_windows, default_excluded_windows()); + assert!( + absent + .excluded_windows + .iter() + .any(|e| e.window_title.as_deref() == Some("Cap Camera")) + ); + + let mut general = Map::new(); + general.insert("excludedWindows".to_string(), Value::Array(Vec::new())); + let cleared = GeneralSettings::from_sections(&general, &Map::new(), &Map::new()); + assert!(cleared.excluded_windows.is_empty()); + } + + /// 0.6.0 (Tauri) wrote the whole `general_settings` object with + /// `"showOverlay"` for its overlay; 0.6.1 to 0.6.3 wrote only this key, + /// with the same value, for the option labelled "Show Cap". Both read + /// back as the overlay, labelled as the Tauri app labelled it, and the + /// value written for it stays one 0.6.0 can read after a downgrade. + #[test] + fn post_studio_overlay_reads_tauri_and_gpui_stores() { + let tauri: Map = serde_json::from_str( + r#"{ + "uploadIndividualFiles": false, + "hideDockIcon": false, + "autoCreateShareableLink": false, + "enableNotifications": true, + "disableAutoOpenLinks": false, + "hasCompletedStartup": true, + "theme": "system", + "recordingCountdown": 3, + "mainWindowRecordingStartBehaviour": "close", + "postStudioRecordingBehaviour": "showOverlay", + "postDeletionBehaviour": "doNothing" + }"#, + ) + .unwrap(); + let gpui: Map = + serde_json::from_str(r#"{ "postStudioRecordingBehaviour": "showOverlay" }"#).unwrap(); + for general in [&tauri, &gpui] { + let settings = GeneralSettings::from_sections(general, &Map::new(), &Map::new()); + assert_eq!( + settings.post_studio_recording_behaviour, + PostStudioBehaviour::ShowOverlay + ); + } + assert_eq!(PostStudioBehaviour::ShowOverlay.as_json(), "showOverlay"); + assert_eq!(PostStudioBehaviour::ShowOverlay.label(), "Show in overlay"); + assert_eq!( + PostStudioBehaviour::from_json("showOverlay"), + Some(PostStudioBehaviour::ShowOverlay) + ); + + let unknown: Map = + serde_json::from_str(r#"{ "postStudioRecordingBehaviour": "showCap" }"#).unwrap(); + assert_eq!( + GeneralSettings::from_sections(&unknown, &Map::new(), &Map::new()) + .post_studio_recording_behaviour, + PostStudioBehaviour::OpenEditor + ); + assert_eq!( + GeneralSettings::from_sections(&Map::new(), &Map::new(), &Map::new()) + .post_studio_recording_behaviour, + PostStudioBehaviour::OpenEditor + ); + } + + /// A store written before `showMainWindowAfterInstantRecording` keeps + /// what it did: "copy link only" brought the main window back, so it reads + /// as Show Cap; every outcome round-trips through its two keys. + #[test] + fn post_instant_behaviour_reads_old_stores_and_round_trips() { + let settings = |pairs: &[(&str, bool)]| { + let general = pairs + .iter() + .map(|(key, value)| (key.to_string(), Value::Bool(*value))) + .collect::>(); + GeneralSettings::from_sections(&general, &Map::new(), &Map::new()) + .post_instant_behaviour() + }; + assert_eq!(settings(&[]), PostInstantBehaviour::OpenLink); + assert_eq!( + settings(&[("disableAutoOpenLinks", false)]), + PostInstantBehaviour::OpenLink + ); + assert_eq!( + settings(&[("disableAutoOpenLinks", true)]), + PostInstantBehaviour::ShowCap + ); + assert_eq!( + settings(&[ + ("disableAutoOpenLinks", false), + (SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY, true) + ]), + PostInstantBehaviour::OpenLink + ); + // The Quality page's auto-open toggle writes only + // `disableAutoOpenLinks`; the open window and a reload must agree. + let toggled = GeneralSettings { + disable_auto_open_links: true, + ..GeneralSettings::default() + }; + assert_eq!( + toggled.post_instant_behaviour(), + settings(&[("disableAutoOpenLinks", true)]) + ); + for behaviour in [ + PostInstantBehaviour::OpenLink, + PostInstantBehaviour::ShowCap, + PostInstantBehaviour::CopyLink, + ] { + let (disable_open, show_main) = behaviour.store_values(); + assert_eq!( + settings(&[ + ("disableAutoOpenLinks", disable_open), + (SHOW_MAIN_WINDOW_AFTER_INSTANT_KEY, show_main), + ]), + behaviour + ); + } + } +} From d33f5deae5521876d1ee66b68d076c36706d9cd4 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:40:45 +0000 Subject: [PATCH 8/9] fix(editor): keep Windows on Half until Full is measured there Full is proven free on Apple Silicon. Windows previews go through a CPU readback that costs more at Full, and no real Windows GPU has been measured yet, so the dedicated-GPU rule waits for that measurement. --- apps/desktop-gpui/Cargo.lock | 1 - apps/desktop-gpui/Cargo.toml | 2 - apps/desktop-gpui/src/camera_blur_portable.rs | 2 +- apps/desktop-gpui/src/store.rs | 44 +++---------------- 4 files changed, 6 insertions(+), 43 deletions(-) diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock index 64b1125fe29..33ea76a47c0 100644 --- a/apps/desktop-gpui/Cargo.lock +++ b/apps/desktop-gpui/Cargo.lock @@ -1564,7 +1564,6 @@ dependencies = [ "cap-camera-effects", "cap-cli-install", "cap-cursor-info", - "cap-d3d-adapter", "cap-editor", "cap-enc-ffmpeg", "cap-export", diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml index 9e90facbebe..db0159e9937 100644 --- a/apps/desktop-gpui/Cargo.toml +++ b/apps/desktop-gpui/Cargo.toml @@ -211,8 +211,6 @@ tempfile = "3" libc = "0.2" [target.'cfg(windows)'.dependencies] -# The editor's default preview quality reads the GPU's dedicated memory. -cap-d3d-adapter = { path = "../../crates/d3d-adapter" } clipboard-rs = "0.2.4" raw-window-handle = "0.6" same-file = "1.0.6" diff --git a/apps/desktop-gpui/src/camera_blur_portable.rs b/apps/desktop-gpui/src/camera_blur_portable.rs index ab433473e6f..f5080c2156f 100644 --- a/apps/desktop-gpui/src/camera_blur_portable.rs +++ b/apps/desktop-gpui/src/camera_blur_portable.rs @@ -21,7 +21,7 @@ fn total_memory_bytes() -> Option { } #[cfg(windows)] -pub(crate) fn total_memory_bytes() -> Option { +fn total_memory_bytes() -> Option { use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index cc3338a4dcb..6d9ca491bf3 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -919,15 +919,14 @@ impl EditorPreviewQuality { } } - /// Full where the GPU draws it at no measurable cost, Half where Full - /// drops playback frames (software and integrated GPUs). A renderer that - /// fell back to (or was forced onto) a software adapter reads back every - /// frame on the CPU, whatever GPU is installed. + /// Full on Apple Silicon, where the GPU draws it at no measurable cost, + /// and Half elsewhere until Full is measured there. A renderer that fell + /// back to (or was forced onto) a software adapter reads back every frame + /// on the CPU, so it stays on Half too. pub fn for_this_machine() -> Self { - static FULL_IS_CHEAP: std::sync::OnceLock = std::sync::OnceLock::new(); let software = SOFTWARE_RENDERING.load(std::sync::atomic::Ordering::Acquire) || cap_rendering::force_software_wgpu_adapter(); - if !software && *FULL_IS_CHEAP.get_or_init(full_preview_is_cheap) { + if cfg!(all(target_os = "macos", target_arch = "aarch64")) && !software { Self::Full } else { Self::Half @@ -944,39 +943,6 @@ impl EditorPreviewQuality { static SOFTWARE_RENDERING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); -#[cfg(all(target_os = "macos", target_arch = "aarch64"))] -fn full_preview_is_cheap() -> bool { - true -} - -/// A dedicated GPU (the editor renders on the high-performance adapter) and -/// 16 GB of memory. Integrated GPUs report a few hundred MB of dedicated -/// memory at most. A 16 GB machine reports slightly under 16 GiB. -#[cfg(target_os = "windows")] -fn full_preview_is_cheap() -> bool { - const MIN_DEDICATED_VIDEO_MEMORY: u64 = 2 << 30; - const MIN_MEMORY: u64 = 15 << 30; - let video_memory = cap_d3d_adapter::select_capture_adapter(None) - .map_or(0, |adapter| adapter.dedicated_vram_bytes); - let memory = crate::camera_blur_portable::total_memory_bytes().unwrap_or(0); - let cheap = video_memory >= MIN_DEDICATED_VIDEO_MEMORY && memory >= MIN_MEMORY; - tracing::info!( - video_memory_mb = video_memory >> 20, - memory_mb = memory >> 20, - cheap, - "editor preview quality default" - ); - cheap -} - -#[cfg(not(any( - all(target_os = "macos", target_arch = "aarch64"), - target_os = "windows" -)))] -fn full_preview_is_cheap() -> bool { - false -} - /// One entry of `general_settings.excludedWindows` (`WindowExclusion`). #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct WindowExclusion { From e40752e9aefbefe72f167b72101520ba8a033772 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:44:34 +0000 Subject: [PATCH 9/9] test(editor): expect Half on Apple Silicon when software rendering is forced --- apps/desktop-gpui/src/store.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 6d9ca491bf3..175f40c75bd 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -3812,7 +3812,9 @@ mod tests { .editor_preview_quality }; let machine = EditorPreviewQuality::for_this_machine(); - if cfg!(all(target_os = "macos", target_arch = "aarch64")) { + if cfg!(all(target_os = "macos", target_arch = "aarch64")) + && !cap_rendering::force_software_wgpu_adapter() + { assert_eq!(machine, EditorPreviewQuality::Full); } if cfg!(target_os = "linux") {