Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

34.6 KB
Raw Blame
use crate::updates::UpdateChannel;
use crate::window_exclusion::WindowExclusion;
use scap_targets::DisplayId;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use specta::Type;
use std::collections::BTreeMap;
use std::io::Write;
use std::path::{Path, PathBuf};
#[cfg(target_os = "macos")]
use tauri::Listener;
use tauri::{AppHandle, Manager, Wry};
use tauri_plugin_store::StoreExt;
use tracing::{error, instrument};
use uuid::Uuid;

#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum PostStudioRecordingBehaviour {
    #[default]
    OpenEditor,
    ShowOverlay,
}

#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum MainWindowRecordingStartBehaviour {
    #[default]
    Close,
    Minimise,
}

#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum PostDeletionBehaviour {
    #[default]
    DoNothing,
    ReopenRecordingWindow,
}

#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum EditorPreviewQuality {
    Quarter,
    #[default]
    Half,
    Full,
}

#[derive(Serialize, Deserialize, Type, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum StudioRecordingQuality {
    Compatibility,
    Balanced,
    Ultra,
}

impl Default for StudioRecordingQuality {
    fn default() -> Self {
        default_studio_recording_quality()
    }
}

impl From<cap_recording::StudioQuality> for StudioRecordingQuality {
    fn from(value: cap_recording::StudioQuality) -> Self {
        match value {
            cap_recording::StudioQuality::Compatibility => Self::Compatibility,
            cap_recording::StudioQuality::Balanced => Self::Balanced,
            cap_recording::StudioQuality::Ultra => Self::Ultra,
        }
    }
}

impl From<StudioRecordingQuality> for cap_recording::StudioQuality {
    fn from(value: StudioRecordingQuality) -> Self {
        match value {
            StudioRecordingQuality::Compatibility => Self::Compatibility,
            StudioRecordingQuality::Balanced => Self::Balanced,
            StudioRecordingQuality::Ultra => Self::Ultra,
        }
    }
}

pub fn default_studio_recording_quality() -> StudioRecordingQuality {
    cap_recording::default_studio_recording_quality().into()
}

impl MainWindowRecordingStartBehaviour {
    pub fn perform(&self, window: &tauri::WebviewWindow) -> tauri::Result<()> {
        match self {
            Self::Close => {
                // On Windows, hide() leaves the DirectComposition surface composited on screen as
                // a white ghost box. minimize() releases the surface without leaving an artifact.
                #[cfg(windows)]
                return window.minimize();
                #[cfg(not(windows))]
                {
                    crate::hide_main_window(window.app_handle());
                    Ok(())
                }
            }
            Self::Minimise => window.minimize(),
        }
    }
}

// NOTE: Do not add "Cap Target Select" here — on Windows, WDA_EXCLUDEFROMCAPTURE applied to that
// hidden window causes it to reappear as a ghost overlay after recording ends.
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<WindowExclusion> {
    DEFAULT_EXCLUDED_WINDOW_TITLES
        .iter()
        .map(|title| WindowExclusion {
            bundle_identifier: None,
            owner_name: None,
            window_title: Some((*title).to_string()),
        })
        .collect()
}

fn append_missing_default_excluded_windows(excluded_windows: &mut Vec<WindowExclusion>) -> bool {
    let mut changed = false;

    for default in default_excluded_windows() {
        if !excluded_windows.contains(&default) {
            excluded_windows.push(default);
            changed = true;
        }
    }

    changed
}

// When adding fields here, #[serde(default)] defines the value to use for existing configurations,
// and `Default::default` defines the value to use for new configurations.
// Things that affect the user experience should only be enabled by default for new configurations.
#[derive(Serialize, Deserialize, Type, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct WindowPosition {
    pub x: f64,
    pub y: f64,
    #[serde(default)]
    pub display_id: Option<DisplayId>,
}

#[derive(Serialize, Deserialize, Type, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GeneralSettingsStore {
    #[serde(default = "uuid::Uuid::new_v4")]
    pub instance_id: Uuid,
    #[serde(default)]
    pub upload_individual_files: bool,
    #[serde(default)]
    pub hide_dock_icon: bool,
    #[serde(default)]
    pub auto_create_shareable_link: bool,
    #[serde(default = "default_true")]
    pub enable_notifications: bool,
    #[serde(default)]
    pub disable_auto_open_links: bool,
    #[serde(default = "default_true")]
    pub has_completed_startup: bool,
    #[serde(default)]
    pub theme: AppTheme,
    #[serde(default)]
    pub commercial_license: Option<CommercialLicense>,
    #[serde(default)]
    pub last_version: Option<String>,
    #[serde(default)]
    pub window_transparency: bool,
    #[serde(default)]
    pub post_studio_recording_behaviour: PostStudioRecordingBehaviour,
    #[serde(default)]
    pub main_window_recording_start_behaviour: MainWindowRecordingStartBehaviour,
    #[serde(
        default = "default_custom_cursor_capture",
        rename = "custom_cursor_capture2"
    )]
    pub custom_cursor_capture: bool,
    #[serde(default = "default_server_url")]
    pub server_url: String,
    #[serde(default)]
    pub recording_countdown: Option<u32>,
    #[serde(
        default = "default_enable_native_camera_preview",
        skip_serializing_if = "no"
    )]
    pub enable_native_camera_preview: bool,
    #[serde(default = "default_true")]
    pub auto_zoom_on_clicks: bool,
    #[serde(default)]
    pub default_zoom_amount: Option<f64>,
    /// `None` until [`init`] seeds it from whether this machine has a notched
    /// display. From then on it is the user's preference and nothing re-reads
    /// the hardware, so moving between machines can't silently flip it.
    #[serde(default)]
    pub macbook_notch_overlay: Option<bool>,
    #[serde(default = "default_capture_keyboard_events")]
    pub capture_keyboard_events: bool,
    #[serde(default)]
    pub post_deletion_behaviour: PostDeletionBehaviour,
    #[serde(default = "default_excluded_windows")]
    pub excluded_windows: Vec<WindowExclusion>,
    #[serde(default)]
    pub delete_instant_recordings_after_upload: bool,
    #[serde(default = "default_instant_mode_max_resolution")]
    pub instant_mode_max_resolution: u32,
    #[serde(default)]
    pub default_project_name_template: Option<String>,
    #[serde(default = "default_crash_recovery_recording")]
    pub crash_recovery_recording: bool,
    #[serde(default = "default_max_fps")]
    pub max_fps: u32,
    #[serde(default = "default_transcription_hints")]
    pub transcription_hints: Vec<String>,
    #[serde(default)]
    pub editor_preview_quality: EditorPreviewQuality,
    #[serde(default)]
    pub studio_recording_quality: StudioRecordingQuality,
    #[serde(default)]
    pub main_window_position: Option<WindowPosition>,
    #[serde(default)]
    pub camera_window_position: Option<WindowPosition>,
    #[serde(default)]
    pub camera_window_positions_by_monitor_name: BTreeMap<String, WindowPosition>,
    #[serde(default = "default_true")]
    pub has_completed_onboarding: bool,
    #[serde(default = "default_true")]
    pub enable_telemetry: bool,
    #[serde(default)]
    pub out_of_process_muxer: bool,
    #[serde(default)]
    pub recordings_path: Option<String>,
    /// Custom recordings folders that were used before; recordings left in
    /// them stay visible in the library. Most recent last.
    #[serde(default)]
    pub previous_recordings_paths: Vec<String>,
    /// App version at which camera background blur was disabled after a crash
    /// was attributed to the blur pipeline; `None` means blur is allowed.
    /// Cleared automatically when the app version changes (one retry per
    /// update, since a new ort/wgpu/driver stack may have fixed the crash).
    #[serde(default)]
    pub camera_blur_disabled_by_crash: Option<String>,
    #[serde(default)]
    pub update_channel: UpdateChannel,
    /// Run the experimental gpui-native app (`cap-gpui`) *instead of* this one:
    /// while enabled, startup hands off to it and exits, and the native app's
    /// own Experimental page hands back. See `gpui_app.rs`.
    #[serde(default)]
    pub enable_gpui_app: bool,
}

fn default_enable_native_camera_preview() -> bool {
    false
}

fn no(_: &bool) -> bool {
    false
}

fn default_true() -> bool {
    true
}

fn default_instant_mode_max_resolution() -> u32 {
    cap_recording::DEFAULT_INSTANT_MODE_MAX_RESOLUTION
}

fn default_max_fps() -> u32 {
    cap_recording::DEFAULT_STUDIO_MAX_FPS
}

fn default_custom_cursor_capture() -> bool {
    cap_recording::DEFAULT_CUSTOM_CURSOR_CAPTURE
}

fn default_capture_keyboard_events() -> bool {
    cap_recording::DEFAULT_CAPTURE_KEYBOARD_EVENTS
}

fn default_crash_recovery_recording() -> bool {
    cap_recording::DEFAULT_CRASH_RECOVERY_RECORDING
}

fn default_transcription_hints() -> Vec<String> {
    vec![
        "Cap".to_string(),
        "TypeScript".to_string(),
        "My Brand Name".to_string(),
        "mywebsite.com".to_string(),
    ]
}

fn default_server_url() -> String {
    std::option_env!("VITE_SERVER_URL")
        .unwrap_or("https://cap.so")
        .to_string()
}

#[derive(Serialize, Deserialize, Type, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CommercialLicense {
    license_key: String,
    expiry_date: Option<f64>,
    refresh: f64,
    activated_on: f64,
}

impl Default for GeneralSettingsStore {
    fn default() -> Self {
        Self {
            instance_id: uuid::Uuid::new_v4(),
            upload_individual_files: false,
            hide_dock_icon: false,
            auto_create_shareable_link: false,
            enable_notifications: true,
            disable_auto_open_links: false,
            has_completed_startup: false,
            theme: AppTheme::System,
            commercial_license: None,
            last_version: None,
            window_transparency: false,
            post_studio_recording_behaviour: PostStudioRecordingBehaviour::OpenEditor,
            main_window_recording_start_behaviour: MainWindowRecordingStartBehaviour::Close,
            custom_cursor_capture: cap_recording::DEFAULT_CUSTOM_CURSOR_CAPTURE,
            server_url: default_server_url(),
            recording_countdown: Some(3),
            enable_native_camera_preview: default_enable_native_camera_preview(),
            // Keep aligned with the field's serde `default_true`: auto zooms
            // are on by default, matching configs that never stored the key.
            auto_zoom_on_clicks: true,
            default_zoom_amount: None,
            macbook_notch_overlay: None,
            capture_keyboard_events: cap_recording::DEFAULT_CAPTURE_KEYBOARD_EVENTS,
            post_deletion_behaviour: PostDeletionBehaviour::DoNothing,
            excluded_windows: default_excluded_windows(),
            delete_instant_recordings_after_upload: false,
            instant_mode_max_resolution: cap_recording::DEFAULT_INSTANT_MODE_MAX_RESOLUTION,
            default_project_name_template: None,
            crash_recovery_recording: cap_recording::DEFAULT_CRASH_RECOVERY_RECORDING,
            max_fps: cap_recording::DEFAULT_STUDIO_MAX_FPS,
            transcription_hints: default_transcription_hints(),
            editor_preview_quality: EditorPreviewQuality::Half,
            studio_recording_quality: default_studio_recording_quality(),
            main_window_position: None,
            camera_window_position: None,
            camera_window_positions_by_monitor_name: BTreeMap::new(),
            has_completed_onboarding: false,
            enable_telemetry: true,
            out_of_process_muxer: cap_recording::DEFAULT_OUT_OF_PROCESS_MUXER,
            recordings_path: None,
            previous_recordings_paths: Vec::new(),
            camera_blur_disabled_by_crash: None,
            update_channel: UpdateChannel::Stable,
            enable_gpui_app: false,
        }
    }
}

#[derive(Default, Debug, Copy, Clone, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AppTheme {
    #[default]
    System,
    Light,
    Dark,
}

struct GeneralSettingsSnapshot {
    original: Option<Value>,
    settings: GeneralSettingsStore,
    invalid_fields: Vec<String>,
}

impl GeneralSettingsSnapshot {
    fn load(original: Option<Value>) -> Result<Self, String> {
        let Some(raw) = original.as_ref() else {
            return Ok(Self {
                original,
                settings: GeneralSettingsStore::default(),
                invalid_fields: Vec::new(),
            });
        };
        if raw.is_object()
            && let Ok(settings) = serde_json::from_value(raw.clone())
        {
            return Ok(Self {
                original,
                settings,
                invalid_fields: Vec::new(),
            });
        }

        let mut invalid_fields = Vec::new();
        let settings = if let Some(fields) = raw.as_object() {
            let mut recovered = fields.clone();
            for (key, value) in fields {
                let field = Value::Object([(key.clone(), value.clone())].into_iter().collect());
                if serde_json::from_value::<GeneralSettingsStore>(field).is_err() {
                    let _ = recovered.remove(key);
                    invalid_fields.push(key.clone());
                }
            }
            serde_json::from_value(Value::Object(recovered))
                .map_err(|_| "Could not recover general settings fields".to_string())?
        } else {
            invalid_fields.push("general_settings".to_string());
            GeneralSettingsStore::default()
        };

        Ok(Self {
            original,
            settings,
            invalid_fields,
        })
    }

    fn persist(
        &self,
        settings: &GeneralSettingsStore,
        backup: impl FnOnce(&Value) -> Result<(), String>,
        save: impl FnOnce(Value) -> Result<(), String>,
    ) -> Result<(), String> {
        let before = serde_json::to_value(&self.settings).map_err(|error| error.to_string())?;
        let after = serde_json::to_value(settings).map_err(|error| error.to_string())?;
        let mut original = self.original.clone().unwrap_or(Value::Null);
        if let Some(fields) = original.as_object_mut() {
            for key in &self.invalid_fields {
                let _ = fields.remove(key);
            }
        }
        let merged = merge_settings_changes(&original, &before, &after);

        if !self.invalid_fields.is_empty()
            && let Some(raw) = self.original.as_ref()
        {
            backup(raw)?;
        }
        save(merged)
    }
}

fn merge_settings_changes(original: &Value, before: &Value, after: &Value) -> Value {
    if let (Some(original), Some(before), Some(after)) =
        (original.as_object(), before.as_object(), after.as_object())
    {
        let mut merged = original.clone();
        for (key, value) in after {
            let next = match (original.get(key), before.get(key)) {
                (Some(original), Some(before)) if before == value => original.clone(),
                (Some(original), Some(before)) => merge_settings_changes(original, before, value),
                _ => value.clone(),
            };
            let _ = merged.insert(key.clone(), next);
        }
        for key in before.keys() {
            if !after.contains_key(key) {
                let _ = merged.remove(key);
            }
        }
        Value::Object(merged)
    } else if let (Some(original), Some(before), Some(after)) =
        (original.as_array(), before.as_array(), after.as_array())
    {
        let mut used = vec![false; before.len()];
        Value::Array(
            after
                .iter()
                .map(|value| {
                    let matching = before
                        .iter()
                        .enumerate()
                        .find(|(index, previous)| !used[*index] && *previous == value);
                    if let Some((index, _)) = matching {
                        used[index] = true;
                        original
                            .get(index)
                            .cloned()
                            .unwrap_or_else(|| value.clone())
                    } else {
                        value.clone()
                    }
                })
                .collect(),
        )
    } else {
        after.clone()
    }
}

fn backup_general_settings(directory: &Path, original: &Value) -> Result<PathBuf, String> {
    std::fs::create_dir_all(directory).map_err(|error| error.to_string())?;
    let path = directory.join(format!("general-settings-recovery-{}.json", Uuid::new_v4()));
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let mut file = options.open(&path).map_err(|error| error.to_string())?;
    let bytes = serde_json::to_vec_pretty(original).map_err(|error| error.to_string())?;
    file.write_all(&bytes).map_err(|error| error.to_string())?;
    file.sync_all().map_err(|error| error.to_string())?;
    Ok(path)
}

fn persist_general_settings(
    app: &AppHandle,
    store: &tauri_plugin_store::Store<Wry>,
    snapshot: &GeneralSettingsSnapshot,
    settings: &GeneralSettingsStore,
) -> Result<(), String> {
    snapshot.persist(
        settings,
        |original| {
            let directory = app
                .path()
                .app_data_dir()
                .map_err(|error| error.to_string())?;
            let path = backup_general_settings(&directory, original).map_err(|error| {
                format!("Could not back up malformed general settings: {error}")
            })?;
            tracing::warn!(
                fields = ?snapshot.invalid_fields,
                backup = %path.display(),
                "Recovered malformed general settings fields"
            );
            Ok(())
        },
        |value| {
            store.set("general_settings", value);
            store.save().map_err(|error| error.to_string())
        },
    )
}

impl GeneralSettingsStore {
    pub fn recordings_dir(app: &AppHandle<Wry>) -> std::path::PathBuf {
        let custom = Self::get(app)
            .map_err(|e| tracing::warn!("Failed to read general settings for recordings_dir: {e}"))
            .ok()
            .flatten()
            .and_then(|s| s.recordings_path)
            .and_then(|p| {
                let path = std::path::PathBuf::from(&p);
                if path.is_absolute() { Some(path) } else { None }
            });

        // A custom folder can become unavailable (unplugged drive, deleted
        // path). Recording must keep working, so fall back to the default
        // location instead of failing; the library lists recordings from
        // every known folder, so nothing goes missing when this happens.
        if let Some(path) = custom {
            match std::fs::create_dir_all(&path) {
                Ok(()) => return path,
                Err(e) => {
                    tracing::warn!(
                        ?path, %e,
                        "Custom recordings directory unavailable; falling back to default"
                    );
                }
            }
        }

        let path = app.path().app_data_dir().unwrap().join("recordings");
        if let Err(e) = std::fs::create_dir_all(&path) {
            tracing::warn!(?path, %e, "Failed to create recordings directory");
        }
        path
    }

    // The effective value: the native preview is macOS-only; it is not
    // reliable on Windows, so the stored setting is ignored there and the
    // websocket preview is always used.
    pub fn native_camera_preview_enabled(app: &AppHandle<Wry>) -> bool {
        if cfg!(not(target_os = "macos")) {
            return false;
        }
        Self::get(app)
            .ok()
            .flatten()
            .map(|settings| settings.enable_native_camera_preview)
            .unwrap_or_else(default_enable_native_camera_preview)
    }

    pub fn get(app: &AppHandle<Wry>) -> Result<Option<Self>, String> {
        match app.store("store").map(|s| s.get("general_settings")) {
            Ok(Some(raw)) => {
                GeneralSettingsSnapshot::load(Some(raw)).map(|snapshot| Some(snapshot.settings))
            }
            _ => Ok(None),
        }
    }

    // i don't trust anyone to not overwrite the whole store lols
    pub fn update(app: &AppHandle, update: impl FnOnce(&mut Self)) -> Result<(), String> {
        let Ok(store) = app.store("store") else {
            return Err("Store not found".to_string());
        };

        let snapshot = GeneralSettingsSnapshot::load(store.get("general_settings"))?;
        let mut settings = snapshot.settings.clone();
        update(&mut settings);
        persist_general_settings(app, &store, &snapshot, &settings)?;

        crate::telemetry::set_telemetry_enabled(settings.enable_telemetry);

        #[cfg(target_os = "macos")]
        crate::permissions::sync_macos_dock_visibility(app);

        Ok(())
    }

    fn save(&self, app: &AppHandle) -> Result<(), String> {
        let Ok(store) = app.store("store") else {
            return Err("Store not found".to_string());
        };

        let snapshot = GeneralSettingsSnapshot::load(store.get("general_settings"))?;
        persist_general_settings(app, &store, &snapshot, self)
    }
}

#[cfg(target_os = "macos")]
#[derive(Deserialize)]
struct StoreChangePayload {
    key: String,
}

#[cfg(target_os = "macos")]
fn sync_dock_visibility_on_general_settings_change(app: &AppHandle) {
    let app_for_listener = app.clone();
    app.listen("store://change", move |event| {
        let Ok(payload) = serde_json::from_str::<StoreChangePayload>(event.payload()) else {
            return;
        };

        if payload.key == "general_settings" {
            crate::permissions::schedule_macos_dock_visibility_sync(&app_for_listener);
        }
    });
}

/// Always false off macOS, so the overlay starts off and waits to be asked for.
fn machine_has_notched_display() -> bool {
    scap_targets::Display::list()
        .iter()
        .any(|display| display.notch().is_some())
}

pub fn init(app: &AppHandle) {
    let mut store = match GeneralSettingsStore::get(app) {
        Ok(Some(store)) => store,
        Ok(None) => GeneralSettingsStore::default(),
        Err(e) => {
            error!("Failed to deserialize general settings store: {}", e);
            GeneralSettingsStore::default()
        }
    };

    append_missing_default_excluded_windows(&mut store.excluded_windows);

    if store.macbook_notch_overlay.is_none() {
        store.macbook_notch_overlay = Some(machine_has_notched_display());
    }

    const REMOVE_TARGET_SELECT_MIGRATION_KEY: &str = "remove_cap_target_select_exclusion_v1";
    if let Ok(raw_store) = app.store("store")
        && raw_store.get(REMOVE_TARGET_SELECT_MIGRATION_KEY).is_none()
    {
        store
            .excluded_windows
            .retain(|w| w.window_title.as_deref() != Some("Cap Target Select"));
        raw_store.set(REMOVE_TARGET_SELECT_MIGRATION_KEY, json!(true));
    }

    crate::telemetry::set_telemetry_enabled(store.enable_telemetry);
    register_bundled_muxer_binary(app);

    #[cfg(target_os = "macos")]
    {
        const NATIVE_PREVIEW_MIGRATION_KEY: &str = "native_camera_preview_default_rollback_v1";
        if let Ok(raw_store) = app.store("store")
            && raw_store.get(NATIVE_PREVIEW_MIGRATION_KEY).is_none()
        {
            store.enable_native_camera_preview = false;
            raw_store.set(NATIVE_PREVIEW_MIGRATION_KEY, json!(true));
        }
    }

    if let Err(e) = store.save(app) {
        error!("Failed to save general settings: {}", e);
    }

    #[cfg(target_os = "macos")]
    sync_dock_visibility_on_general_settings_change(app);

    #[cfg(target_os = "macos")]
    crate::permissions::sync_macos_dock_visibility(app);
}

fn register_bundled_muxer_binary(_app: &AppHandle) {
    if std::env::var_os(cap_recording::oop_muxer::ENV_BIN_PATH).is_some() {
        return;
    }

    if let Ok(exe) = std::env::current_exe()
        && let Some(dir) = exe.parent()
    {
        let candidate = dir.join(bundled_muxer_bin_name());
        if candidate.is_file() {
            match cap_recording::oop_muxer::set_muxer_binary_override(candidate.clone()) {
                Ok(()) => {
                    tracing::info!(
                        path = %candidate.display(),
                        "Registered executable-adjacent cap-muxer binary for out-of-process muxer"
                    );
                }
                Err(existing) => {
                    tracing::debug!(
                        existing = %existing.display(),
                        candidate = %candidate.display(),
                        "cap-muxer override already registered; keeping existing"
                    );
                }
            }
        }
    }
}

fn bundled_muxer_bin_name() -> &'static str {
    if cfg!(windows) {
        "cap-muxer.exe"
    } else {
        "cap-muxer"
    }
}

#[tauri::command]
#[specta::specta]
#[instrument]
pub fn get_default_excluded_windows() -> Vec<WindowExclusion> {
    default_excluded_windows()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn settings_with_preserved_values() -> Value {
        json!({
            "instanceId": "123e4567-e89b-42d3-a456-426614174000",
            "commercialLicense": {
                "licenseKey": "test-license",
                "expiryDate": null,
                "refresh": 123.0,
                "activatedOn": 100.0,
                "futureLicenseField": { "preserve": true }
            },
            "recordingsPath": "/Volumes/Test Recordings",
            "previousRecordingsPaths": ["/Volumes/Previous"],
            "theme": "dark",
            "futureSetting": { "preserve": [1, 2, 3] },
            "mainWindowPosition": { "x": 20.0, "y": 30.0, "futurePositionField": true },
            "excludedWindows": [{ "windowTitle": "Custom", "futureExclusionField": true }]
        })
    }

    #[test]
    fn malformed_field_recovery_preserves_identity_license_paths_and_unknown_fields() {
        let mut original = settings_with_preserved_values();
        original["theme"] = json!({ "system": {}, "dark": {} });
        original["maxFps"] = json!("invalid");
        let snapshot = GeneralSettingsSnapshot::load(Some(original.clone())).unwrap();
        assert_eq!(snapshot.invalid_fields, ["maxFps", "theme"]);
        assert_eq!(
            snapshot.settings.instance_id.to_string(),
            original["instanceId"]
        );
        assert_eq!(
            snapshot.settings.recordings_path.as_deref(),
            original["recordingsPath"].as_str()
        );
        snapshot
            .persist(
                &snapshot.settings,
                |backup| {
                    assert_eq!(backup, &original);
                    Ok(())
                },
                |saved| {
                    for key in [
                        "instanceId",
                        "commercialLicense",
                        "recordingsPath",
                        "previousRecordingsPaths",
                        "futureSetting",
                        "mainWindowPosition",
                        "excludedWindows",
                    ] {
                        assert_eq!(saved[key], original[key], "{key}");
                    }
                    assert_eq!(saved["theme"], "system");
                    assert_eq!(saved["maxFps"], cap_recording::DEFAULT_STUDIO_MAX_FPS);
                    assert!(serde_json::from_value::<GeneralSettingsStore>(saved).is_ok());
                    Ok(())
                },
            )
            .unwrap();
    }

    #[test]
    fn settings_updates_preserve_unknown_nested_fields_and_unchanged_array_entries() {
        let original = settings_with_preserved_values();
        let snapshot = GeneralSettingsSnapshot::load(Some(original.clone())).unwrap();
        let mut updated = snapshot.settings.clone();
        updated.hide_dock_icon = true;
        updated.main_window_position.as_mut().unwrap().x = 90.0;
        updated.commercial_license.as_mut().unwrap().refresh = 456.0;
        append_missing_default_excluded_windows(&mut updated.excluded_windows);
        snapshot
            .persist(
                &updated,
                |_| panic!("valid settings must not need recovery"),
                |saved| {
                    assert_eq!(saved["hideDockIcon"], true);
                    assert_eq!(saved["mainWindowPosition"]["x"], 90.0);
                    assert_eq!(saved["mainWindowPosition"]["futurePositionField"], true);
                    assert_eq!(saved["commercialLicense"]["refresh"], 456.0);
                    assert_eq!(
                        saved["commercialLicense"]["futureLicenseField"],
                        original["commercialLicense"]["futureLicenseField"]
                    );
                    assert_eq!(saved["futureSetting"], original["futureSetting"]);
                    assert_eq!(saved["excludedWindows"][0], original["excludedWindows"][0]);
                    Ok(())
                },
            )
            .unwrap();
    }

    #[test]
    fn healthy_settings_keep_existing_values_and_do_not_create_backups() {
        let original = settings_with_preserved_values();
        let snapshot = GeneralSettingsSnapshot::load(Some(original.clone())).unwrap();
        snapshot
            .persist(
                &snapshot.settings,
                |_| panic!("valid settings must not need recovery"),
                |saved| {
                    for (key, value) in original.as_object().unwrap() {
                        assert_eq!(&saved[key], value, "{key}");
                    }
                    Ok(())
                },
            )
            .unwrap();
    }

    #[test]
    fn failed_backup_does_not_overwrite_malformed_section() {
        let original = json!({ "theme": "future-theme", "recordingsPath": "/Volumes/Test" });
        let snapshot = GeneralSettingsSnapshot::load(Some(original.clone())).unwrap();
        let result = snapshot.persist(
            &snapshot.settings,
            |backup| {
                assert_eq!(backup, &original);
                Err("injected backup failure".into())
            },
            |_| panic!("the original section must survive a failed backup"),
        );
        assert_eq!(result.unwrap_err(), "injected backup failure");
    }

    #[test]
    fn non_object_section_is_backed_up_before_recovery() {
        let original = json!(["unreadable", 123]);
        let snapshot = GeneralSettingsSnapshot::load(Some(original.clone())).unwrap();
        let backed_up = std::cell::Cell::new(false);
        snapshot
            .persist(
                &snapshot.settings,
                |backup| {
                    assert_eq!(backup, &original);
                    backed_up.set(true);
                    Ok(())
                },
                |saved| {
                    assert!(backed_up.get());
                    assert!(serde_json::from_value::<GeneralSettingsStore>(saved).is_ok());
                    Ok(())
                },
            )
            .unwrap();
    }

    #[test]
    fn missing_section_keeps_new_install_defaults_without_recovery() {
        let snapshot = GeneralSettingsSnapshot::load(None).unwrap();
        assert!(!snapshot.settings.has_completed_onboarding);
        assert!(!snapshot.settings.has_completed_startup);
        assert_eq!(snapshot.settings.recording_countdown, Some(3));
        snapshot
            .persist(
                &snapshot.settings,
                |_| panic!("new settings must not need recovery"),
                |saved| {
                    assert_eq!(saved["hasCompletedOnboarding"], false);
                    assert_eq!(saved["recordingCountdown"], 3);
                    Ok(())
                },
            )
            .unwrap();
    }

    #[test]
    fn recovery_backups_are_exact_unique_and_private() {
        let directory = tempfile::tempdir().unwrap();
        let original = settings_with_preserved_values();
        let first = backup_general_settings(directory.path(), &original).unwrap();
        let second = backup_general_settings(directory.path(), &json!("another section")).unwrap();
        assert_ne!(first, second);
        let saved: Value = serde_json::from_slice(&std::fs::read(&first).unwrap()).unwrap();
        assert_eq!(saved, original);
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            assert_eq!(
                std::fs::metadata(first).unwrap().permissions().mode() & 0o777,
                0o600
            );
        }
    }

    #[test]
    fn recovery_backup_reports_unwritable_destination() {
        let directory = tempfile::tempdir().unwrap();
        let blocker = directory.path().join("file");
        std::fs::write(&blocker, "keep").unwrap();
        assert!(backup_general_settings(&blocker, &json!({ "theme": [] })).is_err());
        assert_eq!(std::fs::read_to_string(blocker).unwrap(), "keep");
    }

    fn title_exclusion(title: &str) -> WindowExclusion {
        WindowExclusion {
            bundle_identifier: None,
            owner_name: None,
            window_title: Some(title.to_string()),
        }
    }

    #[test]
    fn appends_missing_default_excluded_windows() {
        let mut excluded_windows = vec![
            title_exclusion("Cap"),
            WindowExclusion {
                bundle_identifier: None,
                owner_name: Some("Preview".to_string()),
                window_title: Some("Private Preview".to_string()),
            },
        ];

        let changed = append_missing_default_excluded_windows(&mut excluded_windows);

        assert!(changed);
        assert!(
            default_excluded_windows()
                .iter()
                .all(|default| excluded_windows.contains(default))
        );
        assert!(excluded_windows.iter().any(|entry| {
            entry.owner_name.as_deref() == Some("Preview")
                && entry.window_title.as_deref() == Some("Private Preview")
        }));
    }

    #[test]
    fn does_not_duplicate_default_excluded_windows() {
        let mut excluded_windows = default_excluded_windows();
        let len = excluded_windows.len();

        let changed = append_missing_default_excluded_windows(&mut excluded_windows);

        assert!(!changed);
        assert_eq!(excluded_windows.len(), len);
    }
}