Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

40.0 KB
Raw Blame
//! Cap desktop, rewritten in gpui.
//!

#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]

mod app_sounds;
mod app_windows;
mod assets;
mod auth;
mod blur_recovery;
#[cfg(target_os = "macos")]
mod camera_bench;
#[cfg(target_os = "macos")]
mod camera_blur;
#[cfg(any(not(target_os = "macos"), test))]
mod camera_blur_portable;
mod camera_loading;
mod camera_window;
mod controls_window;
mod crash_capture;
mod deeplink;
mod dev_restore;
mod devices;
mod diagnostics;
mod doodle;
mod draw_bench;
mod editor_audio;
mod editor_bench;
mod editor_canvas;
mod editor_clips;
mod editor_color;
mod editor_crop;
mod editor_edits;
mod editor_export;
mod editor_hover_bench;
#[cfg(target_os = "linux")]
mod editor_modal;
mod editor_panels;
mod editor_preparing;
mod editor_sidebar;
mod editor_tabs;
mod editor_timeline;
mod editor_window;
mod feeds;
mod hotkeys;
mod i18n;
mod import;
mod installer;
mod jpeg_thumbnail;
mod library;
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
mod linux_dialog;
#[cfg(target_os = "linux")]
mod linux_display;
#[cfg(target_os = "linux")]
mod linux_portal_id;
#[cfg(target_os = "linux")]
mod linux_wallpaper;
mod main_window;
mod media_access;
mod menus;
mod mode_select_window;
mod onboarding_audio;
mod onboarding_doodles;
mod onboarding_window;
mod permission_guide;
mod permissions;
mod permissions_ui;
#[cfg(test)]
mod picker_benchmark;
#[cfg(debug_assertions)]
mod glass_probe;
#[cfg(debug_assertions)]
mod picker_probe;
#[cfg(debug_assertions)]
mod picker_ui_benchmark;
mod platform;
mod presets;
mod project_save;
mod recording;
mod screenshot;
mod screenshot_annotations;
mod screenshot_crop;
mod screenshot_editor;
mod screenshot_export;
mod script_text;
mod session;
mod settings_pages;
mod settings_window;
mod sign_in;
mod single_instance;
#[cfg(debug_assertions)]
mod snapshot;
mod store;
mod target_overlay;
mod target_thumbnails;
mod teleprompter_window;
mod theme;
mod transcription;
mod tray;
mod ui;
mod updates;
mod upload;
mod foreign_window;
mod window_arrange;

use gpui::{App, AppContext as _, Bounds, WindowBounds, WindowOptions, px, size};

use crate::{assets::Assets, main_window::MainWindow, session::RecordingSession};

const MAIN_WINDOW_WIDTH: f32 = 330.;
const MAIN_WINDOW_HEIGHT: f32 = 435.;

/// The corner radius the native material is clipped to. `radius = 16` for
/// material `"panel"` on both visual systems in
/// `apps/desktop/src/utils/macos-window-material.ts`, and the same 16 the
/// shell paints with (`rounded-[16px]`).
#[cfg(target_os = "macos")]
const MAIN_WINDOW_MATERIAL_RADIUS: f64 = 16.;

fn parse_auto_record(spec: &str) -> Option<(main_window::Mode, u64)> {
    if spec == "screenshot" {
        // A capture has no duration; the harness arms the target, captures,
        // and the stop half of the driver is a no-op on an idle session.
        return Some((main_window::Mode::Screenshot, 0));
    }
    let (mode, secs) = spec.split_once(':')?;
    let mode = match mode {
        "studio" => main_window::Mode::Studio,
        "instant" => main_window::Mode::Instant,
        _ => return None,
    };
    Some((mode, secs.parse().ok()?))
}

/// `CAP_GPUI_AUTO_EDITOR`: a `.cap` path, or `1` for the newest studio
/// recording the library scan finds. The library is the same one Recents
/// reads, so `=1` opens exactly the card that would be first in the carousel
/// (skipping instant recordings and screenshots, which the editor rejects).
fn resolve_auto_editor(target: &str) -> Option<std::path::PathBuf> {
    if target != "1" {
        let path = std::path::PathBuf::from(target);
        return path.is_dir().then_some(path);
    }
    library::recent_media()
        .into_iter()
        .find(|item| item.kind == library::MediaKind::Studio)
        .map(|item| item.bundle)
}

/// `CAP_GPUI_AUTO_SCREENSHOT_EDITOR`: a screenshot `.cap` path, or `1` for
/// the newest screenshot in the library -- [`resolve_auto_editor`]'s shape
/// for the screenshot editor.
fn resolve_auto_screenshot_editor(target: &str) -> Option<std::path::PathBuf> {
    if target != "1" {
        let path = std::path::PathBuf::from(target);
        return path.is_dir().then_some(path);
    }
    library::recent_media()
        .into_iter()
        .find(|item| item.kind == library::MediaKind::Screenshot)
        .map(|item| item.bundle)
}

/// Windows ends the process at logoff or shutdown without running
/// `on_app_quit`, so both markers that mean "this session crashed" clear here.
#[cfg(target_os = "windows")]
fn end_windows_session() {
    crate::store::clear_handoff_marker();
    crash_capture::mark_clean_exit();
}

/// zbus warns whenever a portal request object is gone before its
/// properties are cached, which every finished portal request is; at `warn`
/// that filled each Linux log with lines that say nothing.
const DEFAULT_LOG_FILTER: &str =
    "warn,cap_gpui=info,wgpu_core=error,wgpu_hal=error,zbus::proxy=error";

/// stdout, plus a rolling daily file the Feedback page can upload.
///
/// Mirrors `src-tauri/src/main.rs`: `tracing_appender::rolling::daily` into
/// `~/Library/Logs/so.cap.desktop` on macOS (the directory the Tauri app
/// already writes into) under a **different** filename prefix, so the two apps
/// never interleave lines into one file. Returns the non-blocking writer's
/// guard, which has to outlive `main` or the last lines never reach the disk.
fn init_logging() -> Option<tracing_appender::non_blocking::WorkerGuard> {
    use tracing_subscriber::{Layer as _, layer::SubscriberExt as _, util::SubscriberInitExt as _};

    // The binary is `cap-gpui`, so the crate these spans are recorded under is
    // `cap_gpui` -- not `cap_desktop_gpui`, which is the package name and
    // matches nothing.
    let filter = || {
        tracing_subscriber::EnvFilter::try_from_default_env()
            .unwrap_or_else(|_| DEFAULT_LOG_FILTER.into())
    };

    let file = create_log_appender(&diagnostics::logs_dir(), diagnostics::LOG_FILE_PREFIX).map(
        |appender| {
            let (writer, guard) = tracing_appender::non_blocking(
                cap_utils::diagnostic_writer::DiagnosticWriter::new(
                    appender,
                    &diagnostics::logs_dir(),
                    diagnostics::LOG_FILE_PREFIX,
                ),
            );
            let queue_errors = writer.error_counter();
            cap_utils::operation_diagnostics::install_queue_loss_counter(move || {
                queue_errors.dropped_lines()
            });
            let diagnostic_writer = writer.clone();
            cap_utils::operation_diagnostics::install_sink(
                cap_utils::operation_diagnostics::AppInfo {
                    flavor: "gpui",
                    version: env!("CARGO_PKG_VERSION"),
                    source_revision: option_env!("CAP_BUILD_REVISION"),
                    debug_build: cfg!(debug_assertions),
                    source_dirty: option_env!("CAP_BUILD_DIRTY").map(|value| value == "true"),
                },
                move |bytes| {
                    use std::io::Write;
                    let _ = diagnostic_writer.clone().write_all(bytes);
                },
            );
            (
                tracing_subscriber::fmt::layer()
                    .with_ansi(false)
                    .with_writer(writer)
                    .with_filter(filter()),
                guard,
            )
        },
    );
    let (file_layer, guard) = match file {
        Some((layer, guard)) => (Some(layer), Some(guard)),
        None => (None, None),
    };

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer().with_filter(filter()))
        .with(file_layer)
        .init();
    guard
}

fn create_log_appender(
    directory: &std::path::Path,
    prefix: &str,
) -> Option<tracing_appender::rolling::RollingFileAppender> {
    use std::io::Write;

    match tracing_appender::rolling::RollingFileAppender::builder()
        .rotation(tracing_appender::rolling::Rotation::DAILY)
        .filename_prefix(prefix)
        .build(directory)
    {
        Ok(appender) => Some(appender),
        Err(error) => {
            let _ = writeln!(
                std::io::stderr(),
                "Could not open {prefix} in {}: {error}; console logging remains enabled",
                directory.display()
            );
            None
        }
    }
}

/// Stack for every thread that renders, decodes, encodes or runs camera
/// effects. Render and export futures are generic over this crate's closures,
/// so they compile at this crate's dev opt-level and need far more stack than
/// Rust's 2 MiB default: the export estimate overflowed a 2 MiB tokio worker
/// on macOS (2026-10-10). Only address space is reserved; pages are committed
/// as the thread actually uses them.
pub(crate) const MEDIA_THREAD_STACK_SIZE: usize = 16 * 1024 * 1024;

/// gpui_tokio's own runtime keeps tokio's 2 MiB worker stacks; this one
/// matches the Tauri app's runtime on every platform.
fn app_runtime() -> tokio::runtime::Runtime {
    tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .thread_stack_size(MEDIA_THREAD_STACK_SIZE)
        .enable_all()
        .build()
        .expect("Failed to initialize Tokio")
}

fn main() {
    #[cfg(target_os = "linux")]
    if let Some(threads) = cap_utils::linux_runtime::llvmpipe_thread_count() {
        // Mesa counts host CPUs inside containers; apply the process limit before logging starts threads.
        unsafe {
            std::env::set_var("LP_NUM_THREADS", threads.to_string());
        }
    }

    #[cfg(target_os = "linux")]
    if let Some(config) = cap_utils::linux_package::appimage_alsa_config_path() {
        // Logging starts a worker thread, so configure the process environment first.
        unsafe {
            std::env::set_var("ALSA_CONFIG_PATH", config);
        }
    }

    #[cfg(target_os = "linux")]
    let stale_wayland_display = linux_display::stale_wayland_display();
    #[cfg(target_os = "linux")]
    if stale_wayland_display.is_some() {
        // gpui and screen capture both choose Wayland from this variable; drop
        // it before logging starts threads.
        unsafe {
            std::env::remove_var("WAYLAND_DISPLAY");
        }
    }

    // The AppImage CLI launcher re-enters the AppImage with `--cap-cli`; hand
    // it to the bundled CLI before logging, single-instance or the GUI start.
    #[cfg(target_os = "linux")]
    if let Err(error) = cap_cli_install::appimage::dispatch_cli() {
        eprintln!("{error}");
        std::process::exit(1);
    }

    // A confirmation or alert shown for the running app on a desktop without
    // zenity (`linux_dialog`): not a second Cap, so before logging and the
    // single-instance check.
    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    if let Some(status) = linux_dialog::run_from_args() {
        std::process::exit(status);
    }

    let _log_guard = init_logging();
    crash_capture::install(&diagnostics::logs_dir());
    #[cfg(target_os = "linux")]
    if let Some(wayland_display) = stale_wayland_display {
        tracing::warn!(
            wayland_display = %wayland_display.to_string_lossy(),
            "WAYLAND_DISPLAY names no running compositor; using the X display"
        );
    }

    single_instance::acquire();
    let previous_session = crash_capture::begin_session(&diagnostics::logs_dir());
    blur_recovery::configure(previous_session);
    if cfg!(debug_assertions) {
        store::mark_handoff_session();
    }

    #[cfg(target_os = "macos")]
    platform::install_appkit_exception_guards();
    platform::install_url_scheme_handler();
    for argument in std::env::args().skip(1) {
        if argument.contains("token") || argument.contains("api_key") {
            crate::auth::submit_deep_link(&argument);
        }
    }

    let runtime = app_runtime();
    // Before gpui exists: its appearance read is a portal call, and the
    // portals only take an app id before the first one.
    #[cfg(target_os = "linux")]
    runtime.block_on(linux_portal_id::register());

    let app = gpui_platform::application().with_assets(Assets);
    // `RunEvent::Reopen`: the dock icon clicked while the app runs. Must be
    // registered on the builder -- gpui exposes it nowhere else -- with the
    // handler guarding against firing before the window registry exists.
    app.on_reopen(crate::app_windows::handle_dock_reopen);
    #[cfg(target_os = "macos")]
    app.on_open_urls(|urls| {
        for url in urls {
            crate::deeplink::submit_deep_link(&url);
        }
    });
    let runtime_handle = runtime.handle().clone();

    app.run(move |cx: &mut App| {
        gpui_tokio::init_from_handle(cx, runtime_handle);
        gpui_tokio::Tokio::spawn(cx, cap_utils::operation_diagnostics::run_checkpoints()).detach();
        // The dock icon: an unbundled dev binary shows the generic terminal
        // document without it. The bytes are the shipping app's icon.png.
        platform::set_dock_icon(include_bytes!("../assets/dock-icon.png"));

        if let Err(error) = Assets.load_fonts(cx) {
            tracing::error!("failed to load embedded fonts: {error:#}");
        }

        // 330x395 is what `CapWindowId::Main::min_size` uses in the Tauri app,
        // and the window is fixed at that size there too.
        // The text-input key map. Every editing chord in the app -- Backspace,
        // the arrows, Cmd-A/C/X/V/Z -- is an action scoped to the `TextInput`
        // key context, which is what stops a focused field's Backspace from
        // reaching the editor's delete-the-selection handler: a matched
        // binding consumes the keystroke before any `on_key_down` listener on
        // the path runs (`gpui/src/window.rs:5280-5296`).
        ui::bind_text_input_keys(cx);
        let general_settings = crate::store::GeneralSettings::load();
        crate::i18n::init(general_settings.ui_language.as_deref());
        crate::theme::init(general_settings.theme, cx);
        platform::follow_system_reduce_motion(cx);

        let session = RecordingSession::init(cx);
        crate::sign_in::SignIn::init(cx);
        crate::feeds::Feeds::init(cx);
        crate::media_access::MediaAccess::init(cx);
        crate::target_overlay::TargetSelect::init(cx);

        let dev_restore = dev_restore::load();
        let main_size = size(px(MAIN_WINDOW_WIDTH), px(MAIN_WINDOW_HEIGHT));
        let bounds = dev_restore
            .as_ref()
            .and_then(|restore| restore.main_window_bounds(main_size, cx))
            .unwrap_or_else(|| Bounds::centered(None, main_size, cx));
        // A first run always lands in onboarding, and these two flags are a
        // store read rather than the gate's TCC queries -- so the main window
        // starts hidden instead of flashing up before onboarding replaces it.
        // Linux keeps the gate: its main window tracks retained visibility.
        let first_run = cfg!(not(target_os = "linux"))
            && (!crate::store::has_completed_startup()
                || !crate::store::has_completed_onboarding());
        let window_handle = cx
            .open_window(
                WindowOptions {
                    window_bounds: Some(WindowBounds::Windowed(bounds)),
                    // `None`, not a transparent titlebar with repositioned traffic
                    // lights: the Tauri main window returns `None` from
                    // `traffic_lights_position`, which routes it to
                    // `.decorations(false)`. There is no titlebar and there are no
                    // traffic lights -- the whole shell is custom-drawn. In gpui a
                    // `None` titlebar drops NSClosable/NSMiniaturizable/NSResizable
                    // from the style mask, which is the equivalent.
                    #[cfg(not(any(target_os = "linux", target_os = "windows")))]
                    titlebar: None,
                    #[cfg(target_os = "windows")]
                    titlebar: Some(gpui::TitlebarOptions {
                        title: Some("Cap".into()),
                        appears_transparent: true,
                        ..Default::default()
                    }),
                    #[cfg(target_os = "linux")]
                    titlebar: Some(gpui::TitlebarOptions {
                        title: Some("Cap".into()),
                        ..Default::default()
                    }),
                    #[cfg(target_os = "linux")]
                    app_id: Some(crate::app_windows::app_id().into()),
                    #[cfg(target_os = "linux")]
                    window_min_size: Some(size(px(MAIN_WINDOW_WIDTH), px(MAIN_WINDOW_HEIGHT))),
                    #[cfg(target_os = "linux")]
                    window_decorations: Some(gpui::WindowDecorations::Client),
                    #[cfg(target_os = "macos")]
                    kind: gpui::WindowKind::Floating,
                    #[cfg(not(target_os = "macos"))]
                    kind: gpui::WindowKind::Normal,
                    // The header is dragged by the app via `start_window_move`
                    // rather than by AppKit, so mark the content view as app-owned
                    // titlebar content.
                    app_owns_titlebar_drag: true,
                    window_background: if cfg!(target_os = "windows") {
                        gpui::WindowBackgroundAppearance::Opaque
                    } else {
                        gpui::WindowBackgroundAppearance::Transparent
                    },
                    is_resizable: false,
                    is_minimizable: cfg!(target_os = "windows"),
                    focus: !first_run,
                    show: !first_run,
                    ..Default::default()
                },
                {
                    let session = session.clone();
                    move |window, cx| cx.new(|cx| MainWindow::new(session, window, cx))
                },
            )
            .inspect_err(|error| tracing::error!("failed to open the main window: {error:#}"));
        let Ok(window_handle) = window_handle else {
            cx.quit();
            return;
        };

        if cfg!(debug_assertions) {
            cx.on_app_quit(|_| async {
                crate::store::clear_handoff_marker();
            })
            .detach();
        }
        cx.on_app_quit(|_| async {
            crash_capture::mark_clean_exit();
        })
        .detach();

        app_windows::init(window_handle, session, cx);
        #[cfg(target_os = "macos")]
        match platform::install_native_quit_handler() {
            Ok(requests) => {
                cx.spawn(async move |cx| {
                    while requests.recv_async().await.is_ok() {
                        cx.update(menus::quit);
                    }
                })
                .detach();
            }
            Err(error) => {
                tracing::error!(%error, "Could not install safe native Quit handling");
                menus::quit(cx);
                return;
            }
        }
        #[cfg(target_os = "linux")]
        single_instance::init_linux_reopen(cx);
        updates::schedule_startup_check(cx);
        app_windows::schedule_launch_render_warmup(cx);
        upload::queue::init(cx);
        upload::instant_cleanup::init(cx);

        // The app menu (and with it ⌘W/⌘M/⌘Q) and the status-bar item. Both
        // reach into the window registry, so they come after it -- and the menu
        // before the tray, because the tray's Quit item is the menu's Quit.
        if !std::env::var("CAP_GPUI_NO_MENUS").is_ok_and(|v| v == "1") {
            menus::init(cx);
        }
        if !std::env::var("CAP_GPUI_NO_TRAY").is_ok_and(|v| v == "1") {
            tray::init(cx);
        }
        // The store's global shortcuts, after the registry they dispatch into.
        hotkeys::init(cx);
        // The gate's TCC queries (screen recording preflight, AXIsProcessTrusted)
        // are ~30ms of synchronous XPC on this Mac -- the whole startup delta
        // between the 08-17 and 08-18 builds. Both are thread-safe reads, so
        // the gate runs off-main and onboarding opens a beat after the main
        // window instead of holding it back. A first run skips the gate.
        if first_run {
            app_windows::open_onboarding(cx);
        } else {
            cx.spawn(async move |cx| {
                let show_onboarding = cx
                    .background_executor()
                    .spawn(async { crate::store::should_show_onboarding() })
                    .await;
                if show_onboarding {
                    cx.update(app_windows::open_onboarding);
                }
            })
            .detach();
        }
        // `CAP_GPUI_AUTO_TRAY` / `CAP_GPUI_TRAY_DUMP`: the tray's harness path.
        tray::drive_from_env(cx);
        // `CAP_GPUI_AUTO_CLOSE=settings|main:<secs>`: run the ⌘W body against
        // that window after a delay -- exercises the exact deferred close path
        // without synthetic keypresses (which race whatever the user has
        // focused).
        if let Ok(spec) = std::env::var("CAP_GPUI_AUTO_CLOSE")
            && let Some((which, secs)) = spec.split_once(':')
            && let Ok(secs) = secs.parse::<u64>()
        {
            let which = which.to_string();
            cx.spawn(async move |cx| {
                cx.background_executor()
                    .timer(std::time::Duration::from_secs(secs))
                    .await;
                cx.update(|cx| {
                    let windows = cx.global::<app_windows::AppWindows>();
                    let handle: Option<gpui::AnyWindowHandle> = match which.as_str() {
                        "settings" => windows.settings.map(Into::into),
                        "main" => Some(windows.main.into()),
                        _ => None,
                    };
                    if let Some(handle) = handle {
                        menus::close_window_by_handle(handle, cx);
                    }
                });
            })
            .detach();
        }
        // The Tauri app syncs the dock the moment a dock-activating window is
        // shown; the main window is up by here.
        menus::sync_dock_visibility(cx);

        // `CAP_GPUI_DEV_RESTORE=<state file>`: `dev.sh`'s relaunch loop.
        // Reopens the previous process's windows in place and keeps the
        // state file current for the next swap.
        if let Some(dev_restore) = dev_restore {
            dev_restore.init(cx);
        }

        // Enumeration is started here rather than in `MainWindow::new`, which
        // runs before the window is fully built -- see `start_enumeration`.
        // The panel behavior (`MAIN_PANEL_LEVEL`, all Spaces -- what the Tauri
        // app does via tauri_nspanel) is applied here for the same reason: the
        // NSWindow does not exist yet inside the builder closure.
        let native_main = window_handle
            .update(cx, |view, window, cx| {
                platform::apply_panel_behavior(
                    window,
                    platform::PanelBehavior {
                        #[cfg(target_os = "macos")]
                        level: objc2_app_kit::NSFloatingWindowLevel,
                        #[cfg(not(target_os = "macos"))]
                        level: platform::MAIN_WINDOW_LEVEL,
                        join_all_spaces: true,
                        shadow: true,
                    },
                );
                #[cfg(target_os = "linux")]
                if let Err(error) = platform::remove_x11_window_decorations(window) {
                    tracing::warn!(%error, "could not remove X11 main window decorations");
                }
                tracing::info!(
                    number = platform::window_number(window),
                    "main window opened"
                );
                view.start_enumeration(window, cx);
                view.start_recovery_check(window, cx);
                // The AppKit work below must not run inside this update:
                // inserting a subview and mutating the content view's layer
                // synchronously re-enters gpui's own window callbacks, which
                // re-borrow the App. Grab the retained NSWindow here, act on
                // it from a task (the `place_overlay_panel` rule).
                platform::native_window(window)
            })
            .expect("failed to start device enumeration");

        // Before the launch thumbnail sweep can reach ReplayKit, which would
        // otherwise add the standard titlebar buttons next to the hand-drawn
        // close light (`platform::lock_borderless_style`).
        #[cfg(target_os = "macos")]
        if let Some(native) = &native_main {
            platform::lock_borderless_style(native);
        }

        // The native window material: `NSGlassEffectView` on macOS 26+,
        // `NSVisualEffectView` vibrancy below that -- what
        // `applyMacOSWindowMaterial("panel")` does in the Tauri app. Nothing
        // paints it; the shell paints a translucent tint *over* it, so the
        // window has to be told which one landed.
        #[cfg(target_os = "macos")]
        cx.spawn(async move |cx| {
            let Some(native) = native_main else {
                tracing::error!("no NSWindow behind the main window; material not installed");
                return;
            };
            let kind = platform::install_window_material(&native, MAIN_WINDOW_MATERIAL_RADIUS);
            match kind {
                Some(kind) => tracing::info!(?kind, "installed main window material"),
                None => tracing::info!("no native window material available"),
            }
            cx.update(|cx| {
                cx.set_global(platform::WindowMaterial(kind));
                // The palette is resolved in `render`, which has already run
                // by now -- nudge it so the tint replaces the opaque shell.
                window_handle.update(cx, |_, _, cx| cx.notify()).ok();
            });
        })
        .detach();
        #[cfg(target_os = "windows")]
        cx.spawn(async move |_| {
            let Some(native) = native_main else {
                return;
            };
            if let Err(error) = platform::install_main_window_frame_policy(&native) {
                tracing::warn!(%error, "could not install Main window frame policy");
            }
            if let Err(error) = platform::install_session_end_handler(&native, end_windows_session)
            {
                tracing::warn!(%error, "could not install the Windows session end handler");
            }
        })
        .detach();
        #[cfg(not(any(target_os = "macos", target_os = "windows")))]
        let _ = native_main;

        // `CAP_GPUI_DEBUG_LIGHTS=1`: poll the main window's style mask and
        // standard-button set, logging on change -- pins down *when* AppKit
        // materializes titlebar buttons on the buttonless main window.
        if std::env::var("CAP_GPUI_DEBUG_LIGHTS").is_ok_and(|v| v == "1") {
            cx.spawn(async move |cx| {
                let mut last = None::<String>;
                loop {
                    cx.background_executor()
                        .timer(std::time::Duration::from_millis(250))
                        .await;
                    let state = cx.update(|cx| {
                        window_handle
                            .update(cx, |_, window, _| platform::debug_titlebar_state(window))
                            .ok()
                            .flatten()
                    });
                    if state != last {
                        tracing::info!(?state, "titlebar state changed");
                        last = state;
                    }
                }
            })
            .detach();
        }

        // `CAP_GPUI_AUTO_SETTINGS=1` (or a page slug, e.g. `hotkeys`): open
        // the settings window the way the header gear does. Same reason as
        // every other `CAP_GPUI_AUTO_*` hook -- unprivileged synthetic clicks
        // are dropped, so the screenshot harness needs a way in.
        if let Ok(page) = std::env::var("CAP_GPUI_AUTO_SETTINGS")
            && !page.is_empty()
        {
            let page =
                settings_window::Page::from_slug(&page).unwrap_or(settings_window::Page::General);
            app_windows::open_settings(page, cx);
        }

        permissions_ui::auto_open_from_env(cx); // `CAP_GPUI_AUTO_PERMISSIONS=1`: the permissions surface.
        #[cfg(debug_assertions)]
        snapshot::arm_from_env(cx);

        // `CAP_GPUI_AUTO_MODE_SELECT=1`: open the 580x340 mode picker the way
        // the mode dot does, main window hidden included. A mode name instead
        // of `1` also clicks that card.
        if let Ok(mode) = std::env::var("CAP_GPUI_AUTO_MODE_SELECT")
            && !mode.is_empty()
        {
            app_windows::open_mode_select(cx);
            if let Some(mode) = match mode.as_str() {
                "instant" => Some(main_window::Mode::Instant),
                "studio" => Some(main_window::Mode::Studio),
                "screenshot" => Some(main_window::Mode::Screenshot),
                _ => None,
            } {
                app_windows::choose_mode_in_mode_select(mode, cx);
            }
        }

        // `CAP_GPUI_AUTO_CAMERA_BENCH=1`: open the camera bubble and pump
        // synthetic frames through the production delivery path, reporting
        // gpui draw timings for that window. See `camera_bench`.
        #[cfg(target_os = "macos")]
        if std::env::var("CAP_GPUI_AUTO_CAMERA_BENCH").is_ok_and(|v| v == "1") {
            camera_bench::run(cx);
        }

        // `CAP_GPUI_AUTO_TELEPROMPTER=1`: open the teleprompter the way the
        // header's scan-text button does. Same reason as the other hooks:
        // unprivileged synthetic clicks are dropped. Any other value is typed
        // into the script through the same path a keystroke takes, which is how
        // the persistence round trip is checked.
        if let Ok(script) = std::env::var("CAP_GPUI_AUTO_TELEPROMPTER")
            && !script.is_empty()
        {
            app_windows::open_teleprompter(cx);
            if script != "1" {
                app_windows::type_into_teleprompter(script, cx);
            }
            // `CAP_GPUI_AUTO_PLAY=1`: press play once the window has painted --
            // the scrollable height, which is what decides whether playback can
            // start at all, does not exist before the first frame.
            if std::env::var("CAP_GPUI_AUTO_PLAY").is_ok_and(|value| value == "1") {
                cx.spawn(async move |cx| {
                    cx.background_executor()
                        .timer(std::time::Duration::from_millis(1200))
                        .await;
                    cx.update(app_windows::play_teleprompter);
                })
                .detach();
            }
        }

        // `CAP_GPUI_AUTO_EDITOR=<path-to-.cap>`: open the editor on that
        // bundle the way a Recents card does. `=1` picks the newest studio
        // recording in the library. Same reason as the other `CAP_GPUI_AUTO_*`
        // hooks -- unprivileged synthetic clicks are dropped, so the
        // screenshot harness needs a way in.
        if let Ok(target) = std::env::var("CAP_GPUI_AUTO_EDITOR")
            && !target.is_empty()
        {
            match resolve_auto_editor(&target) {
                Some(path) => app_windows::open_editor(path, cx),
                None => tracing::error!(target, "CAP_GPUI_AUTO_EDITOR: no studio .cap to open"),
            }
        }

        // `CAP_GPUI_AUTO_HOTKEY=<store key>`: run a hotkey action's dispatch
        // arm (e.g. `screenshotDisplay`) as if the OS shortcut fired.
        if let Ok(action) = std::env::var("CAP_GPUI_AUTO_HOTKEY")
            && !action.is_empty()
        {
            let action = action.clone();
            cx.spawn(async move |cx| {
                // Give enumeration and the first paint the same beat the
                // other harness hooks give them.
                cx.background_executor()
                    .timer(std::time::Duration::from_secs(2))
                    .await;
                cx.update(|cx| hotkeys::dispatch_for_harness(&action, cx));
            })
            .detach();
        }

        // `CAP_GPUI_AUTO_SCREENSHOT_EDITOR=<path-to-.cap>` (or `1` for the
        // newest screenshot): open the screenshot editor the way a Recents
        // card does.
        if let Ok(target) = std::env::var("CAP_GPUI_AUTO_SCREENSHOT_EDITOR")
            && !target.is_empty()
        {
            match resolve_auto_screenshot_editor(&target) {
                Some(path) => app_windows::open_screenshot_editor(path, cx),
                None => tracing::error!(
                    target,
                    "CAP_GPUI_AUTO_SCREENSHOT_EDITOR: no screenshot .cap to open"
                ),
            }
        }

        // `CAP_GPUI_AUTO_RECORD=studio:5` / `instant:4` / `screenshot`: arm
        // the primary display and record for N seconds (or capture once). The
        // end-to-end check drives the recorder this way because unprivileged
        // synthetic clicks are dropped.
        #[cfg(debug_assertions)]
        picker_ui_benchmark::run(window_handle, cx);
        #[cfg(debug_assertions)]
        picker_probe::run(window_handle, cx);
        #[cfg(debug_assertions)]
        glass_probe::run(cx);
        if let Ok(auto) = std::env::var("CAP_GPUI_AUTO_RECORD")
            && let Some((mode, secs)) = parse_auto_record(&auto)
        {
            window_handle
                .update(cx, |view, window, cx| {
                    view.auto_record(mode, secs, window, cx)
                })
                .expect("failed to arm auto-record");
        } else if let Some(kind) = main_window::auto_overlay_kind() {
            // `CAP_GPUI_AUTO_OVERLAY` without a recording: put the overlays up
            // and leave them there, which is how they get screenshotted.
            window_handle
                .update(cx, |view, window, cx| {
                    view.auto_open_overlay(kind, window, cx)
                })
                .expect("failed to arm the target overlay");
        }
        // Under the dev loop the app relaunches on every save; activating
        // would yank focus from the code editor each time. The restored
        // windows come forward with `orderFrontRegardless` instead, and the
        // main window floats at panel level 100 regardless of activation.
        if !dev_restore::enabled() {
            cx.activate(true);
        }
    });
    runtime.shutdown_background();
}

#[cfg(all(
    test,
    any(target_os = "windows", target_os = "macos", target_os = "linux")
))]
mod runtime_tests {
    #[cfg(target_os = "windows")]
    fn current_stack_size() -> usize {
        let (mut low, mut high) = (0, 0);
        unsafe {
            windows_sys::Win32::System::Threading::GetCurrentThreadStackLimits(&mut low, &mut high);
        }
        high.saturating_sub(low)
    }

    #[cfg(target_os = "macos")]
    fn current_stack_size() -> usize {
        unsafe { libc::pthread_get_stacksize_np(libc::pthread_self()) }
    }

    #[cfg(target_os = "linux")]
    fn current_stack_size() -> usize {
        unsafe {
            let mut attr = std::mem::zeroed::<libc::pthread_attr_t>();
            assert_eq!(libc::pthread_getattr_np(libc::pthread_self(), &mut attr), 0);
            let mut address = std::ptr::null_mut();
            let mut size = 0;
            assert_eq!(
                libc::pthread_attr_getstack(&attr, &mut address, &mut size),
                0
            );
            libc::pthread_attr_destroy(&mut attr);
            size
        }
    }

    #[test]
    fn media_workers_and_blocking_tasks_have_large_stacks() {
        let runtime = super::app_runtime();
        runtime.block_on(async {
            let worker_size = tokio::spawn(async { current_stack_size() }).await.unwrap();
            let blocking_size = tokio::task::spawn_blocking(current_stack_size)
                .await
                .unwrap();
            assert!(
                worker_size >= super::MEDIA_THREAD_STACK_SIZE,
                "worker stack: {worker_size}"
            );
            assert!(
                blocking_size >= super::MEDIA_THREAD_STACK_SIZE,
                "blocking stack: {blocking_size}"
            );
        });
        runtime.shutdown_background();
    }
}

#[cfg(test)]
mod logging_tests {
    use super::create_log_appender;
    use std::{io::Write, path::PathBuf};
    use tracing_subscriber::{Layer as _, layer::SubscriberExt as _};

    struct LogDirectory(PathBuf);

    impl LogDirectory {
        fn new() -> Self {
            let nonce = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos();
            let directory = std::env::temp_dir()
                .join(format!("cap-gpui-logging-{}-{nonce}", std::process::id()));
            std::fs::create_dir_all(&directory).unwrap();
            Self(directory)
        }
    }

    impl Drop for LogDirectory {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn the_default_filter_drops_zbus_property_cache_warnings_only() {
        use std::sync::{Arc, Mutex};

        #[derive(Clone, Default)]
        struct Lines(Arc<Mutex<Vec<u8>>>);
        impl Write for Lines {
            fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
                self.0.lock().unwrap().extend_from_slice(bytes);
                Ok(bytes.len())
            }
            fn flush(&mut self) -> std::io::Result<()> {
                Ok(())
            }
        }

        let lines = Lines::default();
        let writer = lines.clone();
        let subscriber = tracing_subscriber::registry().with(
            tracing_subscriber::fmt::layer()
                .with_ansi(false)
                .with_writer(move || writer.clone())
                .with_filter(tracing_subscriber::EnvFilter::new(super::DEFAULT_LOG_FILTER)),
        );
        tracing::subscriber::with_default(subscriber, || {
            tracing::warn!(target: "zbus::proxy", "Failed to populate properties cache");
            tracing::error!(target: "zbus::proxy", "a zbus error still shows");
            tracing::warn!(target: "zbus::connection", "other zbus warnings still show");
            tracing::info!(target: "cap_gpui::session", "recording started");
        });
        let logged = String::from_utf8(lines.0.lock().unwrap().clone()).unwrap();
        assert!(!logged.contains("Failed to populate properties cache"), "{logged}");
        assert!(logged.contains("a zbus error still shows"), "{logged}");
        assert!(logged.contains("other zbus warnings still show"), "{logged}");
        assert!(logged.contains("recording started"), "{logged}");
    }

    #[test]
    fn healthy_log_destination_preserves_existing_records() {
        let directory = LogDirectory::new();
        let destination = directory.0.join("nested");
        for record in ["first\n", "second\n"] {
            let mut appender = create_log_appender(&destination, "cap.log").unwrap();
            appender.write_all(record.as_bytes()).unwrap();
            appender.flush().unwrap();
        }
        let records: String = std::fs::read_dir(destination)
            .unwrap()
            .map(|entry| std::fs::read_to_string(entry.unwrap().path()).unwrap())
            .collect();
        assert!(records.contains("first\n"));
        assert!(records.contains("second\n"));
    }

    #[test]
    fn unavailable_log_directory_disables_only_file_logging() {
        let directory = LogDirectory::new();
        let destination = directory.0.join("blocked");
        std::fs::write(&destination, "existing file").unwrap();
        assert!(create_log_appender(&destination, "cap.log").is_none());
        assert_eq!(
            std::fs::read_to_string(destination).unwrap(),
            "existing file"
        );
    }

    #[test]
    fn unavailable_daily_log_file_disables_only_file_logging() {
        let directory = LogDirectory::new();
        let today = chrono::Utc::now().date_naive();
        for days in [-1, 0, 1] {
            let date = today + chrono::Duration::days(days);
            std::fs::create_dir(directory.0.join(format!("cap.log.{date}"))).unwrap();
        }
        assert!(create_log_appender(&directory.0, "cap.log").is_none());
        assert!(create_log_appender(&directory.0, "other.log").is_some());
    }
}