Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

284 KB
Raw Blame
//! The main recording window.
//!
//! Every metric here is transcribed from the Tauri implementation
//! (`apps/desktop/src/routes/(window-chrome)/new-main/index.tsx` and its
//! siblings) so the two windows are pixel-comparable. Tailwind classes are
//! quoted next to the values they turn into, because `pl-3` and `gap-2.5` are
//! considerably easier to check against the original than `12.` and `10.`.

use cap_recording::sources::screen_capture::ScreenCaptureTarget;
use gpui::{
    AppContext as _, Context, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement,
    Render, SharedString, StatefulInteractiveElement, Styled, Window, div, img,
    prelude::FluentBuilder, px, rgb, svg,
};
use std::{cell::Cell, rc::Rc};

use crate::i18n::tr;
use crate::theme::LocaleFont as _;
use crate::{
    MAIN_WINDOW_WIDTH, app_windows, devices,
    devices::{CameraOption, DeviceSnapshot, DisplayOption, MicrophoneOption, WindowOption},
    feeds::{self, Feeds},
    library::{self, RecordingItem, ScreenshotItem},
    media_access::MediaAccess,
    permissions::OSPermission,
    recording,
    session::{Phase, RecordingSession},
    settings_window::Page,
    sign_in::{SignIn, SignInEvent, SignInOrigin, SignInOutcome},
    target_thumbnails,
    theme::Theme,
    ui,
};
use gpui::{Entity, Task};

/// `h-9` on `.cap-window-header`.
const HEADER_HEIGHT: f32 = 36.;

/// `h-[42px]` in deviceRowStyles.ts.
const DEVICE_ROW_HEIGHT: f32 = 42.;

fn remembered_camera(id: &recording::DeviceOrModelID, cameras: &[CameraOption]) -> CameraOption {
    cameras
        .iter()
        .find(|camera| match id {
            recording::DeviceOrModelID::DeviceID(id) => camera.device_id == *id,
            recording::DeviceOrModelID::ModelID(model) => camera.model_id.as_ref() == Some(model),
        })
        .cloned()
        .unwrap_or_else(|| CameraOption {
            device_id: match id {
                recording::DeviceOrModelID::DeviceID(id) => id.clone(),
                recording::DeviceOrModelID::ModelID(_) => String::new(),
            },
            model_id: match id {
                recording::DeviceOrModelID::ModelID(model) => Some(model.clone()),
                recording::DeviceOrModelID::DeviceID(_) => None,
            },
            label: String::new(),
            best_format: None,
            formats: Vec::new(),
        })
}

/// A remembered camera that isn't connected has no device name, so its row
/// reads the fallback through the active locale on every render.
pub(crate) fn camera_label(label: &str) -> String {
    if label.is_empty() {
        tr::main::device::fallback_camera().to_string()
    } else {
        label.to_string()
    }
}

fn remembered_microphone(name: &str, microphones: &[MicrophoneOption]) -> MicrophoneOption {
    microphones
        .iter()
        .find(|microphone| microphone.name == name)
        .cloned()
        .unwrap_or_else(|| MicrophoneOption {
            name: name.to_string(),
            sample_rate: None,
            channels: None,
        })
}

fn take_pending_recording_inputs(
    pending: &mut crate::store::RecordingInputSettings,
    enumerating: bool,
    suspended: bool,
) -> Option<crate::store::RecordingInputSettings> {
    if enumerating || suspended || pending.camera_id.is_none() && pending.microphone_name.is_none()
    {
        return None;
    }
    Some(std::mem::take(pending))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Instant,
    Studio,
    Screenshot,
}

fn effective_recording_mode(preferred: Mode, editor_recording: bool) -> Mode {
    if editor_recording {
        Mode::Studio
    } else {
        preferred
    }
}

impl Mode {
    /// `cap_recording::RecordingMode`'s serialized spelling -- the value the
    /// store's `recording_settings.mode` holds, shared with the Tauri app.
    pub fn slug(self) -> &'static str {
        match self {
            Self::Instant => "instant",
            Self::Studio => "studio",
            Self::Screenshot => "screenshot",
        }
    }

    pub fn from_slug(slug: &str) -> Option<Self> {
        match slug {
            "instant" => Some(Self::Instant),
            "studio" => Some(Self::Studio),
            "screenshot" => Some(Self::Screenshot),
            _ => None,
        }
    }

    /// `get_current_mode` (`src-tauri/src/tray.rs:355-361`): the stored mode,
    /// falling back to `RecordingMode::default()`, which is Instant.
    pub fn from_store() -> Self {
        crate::store::recording_mode_slug()
            .as_deref()
            .and_then(Self::from_slug)
            .unwrap_or(Self::Instant)
    }

    pub fn icon(self) -> &'static str {
        match self {
            Self::Instant => "icons/instant.svg",
            Self::Studio => "icons/film-cut.svg",
            Self::Screenshot => "icons/screenshot.svg",
        }
    }

    /// `size-4` for instant, `size-[0.9rem]` for the other two.
    fn icon_size(self) -> f32 {
        match self {
            Self::Instant => 16.,
            Self::Studio | Self::Screenshot => 14.4,
        }
    }

    /// `ModeInfoPanel`'s `modeOptions`, which is *not* `MODE_BUTTONS` -- the
    /// hover cards and the info panel describe the modes differently and the
    /// app carries both sets of strings.
    pub fn panel_title(self) -> &'static str {
        match self {
            Self::Instant => "Instant",
            Self::Studio => "Studio",
            Self::Screenshot => "Screenshot",
        }
    }

    /// [`Self::panel_title`] in the UI language. `panel_title` stays English:
    /// it names element ids and log fields.
    fn display_name(self) -> &'static str {
        match self {
            Self::Instant => tr::main::mode::name::instant(),
            Self::Studio => tr::main::mode::name::studio(),
            Self::Screenshot => tr::main::mode::name::screenshot(),
        }
    }

    fn hover_title(self) -> &'static str {
        match self {
            Self::Instant => tr::main::mode::hover::instant(),
            Self::Studio => tr::main::mode::hover::studio(),
            Self::Screenshot => tr::main::mode::hover::screenshot(),
        }
    }

    fn header_title(self) -> &'static str {
        match self {
            Self::Instant => tr::main::mode::header::instant(),
            Self::Studio => tr::main::mode::header::studio(),
            Self::Screenshot => tr::main::mode::header::screenshot(),
        }
    }

    fn panel_description(self) -> &'static str {
        match self {
            Self::Instant => tr::main::mode::info::instant(),
            Self::Studio => tr::main::mode::info::studio(),
            Self::Screenshot => tr::main::mode::info::screenshot(),
        }
    }
}

fn glass_tint(theme: Theme, light: f32, dark: f32) -> Hsla {
    if theme.is_dark() {
        gpui::hsla(0., 0., 1., dark)
    } else {
        gpui::hsla(0., 0., 0., light)
    }
}

fn glass_hover(theme: Theme) -> Hsla {
    glass_tint(theme, 0.06, 0.07)
}

fn glass_press(theme: Theme) -> Hsla {
    glass_tint(theme, 0.1, 0.11)
}

fn glass_selected(theme: Theme) -> Hsla {
    glass_tint(theme, 0.08, 0.1)
}

fn shadow(color: Hsla, y: f32, blur: f32, spread: f32, inset: bool) -> gpui::BoxShadow {
    gpui::BoxShadow {
        color,
        offset: gpui::point(px(0.), px(y)),
        blur_radius: px(blur),
        spread_radius: px(spread),
        inset,
    }
}

/// Drawn in gpui rather than with an `NSGlassEffectView` per button, which
/// would have to track gpui's layout under the Metal layer. Two shadows, one
/// unblurred, keep it cheap on a window that repaints with the mic meter.
fn glass_button<E: Styled>(element: E, theme: Theme, selected: bool) -> E {
    let dark = theme.is_dark();
    let (top, bottom, rim) = match (selected, dark) {
        (false, false) => (
            gpui::hsla(0., 0., 1., 0.7),
            gpui::hsla(0., 0., 1., 0.58),
            gpui::hsla(0., 0., 0., 0.08),
        ),
        (false, true) => (
            gpui::hsla(0., 0., 1., 0.085),
            gpui::hsla(0., 0., 1., 0.06),
            gpui::hsla(0., 0., 1., 0.07),
        ),
        (true, false) => (
            Theme::with_alpha(theme.blue_3, 0.9),
            Theme::with_alpha(theme.blue_3, 0.75),
            Theme::with_alpha(theme.blue_8, 0.4),
        ),
        (true, true) => (
            Theme::with_alpha(theme.blue_9, 0.24),
            Theme::with_alpha(theme.blue_9, 0.18),
            Theme::with_alpha(theme.blue_9, 0.3),
        ),
    };
    let (highlight, lift) = if dark {
        (gpui::hsla(0., 0., 1., 0.07), gpui::hsla(0., 0., 0., 0.2))
    } else {
        (gpui::hsla(0., 0., 1., 0.8), gpui::hsla(0., 0., 0., 0.04))
    };
    element
        .bg(gpui::linear_gradient(
            180.,
            gpui::linear_color_stop(top, 0.),
            gpui::linear_color_stop(bottom, 1.),
        ))
        .border_1()
        .border_color(rim)
        .shadow(vec![
            shadow(highlight, 1., 0., 0., true),
            shadow(lift, 1., 2., 0., false),
        ])
}

fn capture_hover_fill(theme: Theme, selected: bool) -> Hsla {
    if selected {
        Theme::with_alpha(theme.blue_9, 0.06)
    } else {
        glass_tint(theme, 0.04, 0.05)
    }
}

fn capture_press_fill(theme: Theme, selected: bool) -> Hsla {
    if selected {
        Theme::with_alpha(theme.blue_9, 0.12)
    } else {
        glass_tint(theme, 0.07, 0.09)
    }
}

#[derive(Default)]
struct ModeHoverState {
    trigger: Option<Mode>,
    card: Option<Mode>,
    visible: Option<Mode>,
}

impl ModeHoverState {
    fn target(&self) -> Option<Mode> {
        self.trigger.or(self.card)
    }

    fn update(&mut self, mode: Mode, card: bool, hovered: bool) {
        if !card && hovered && self.card != Some(mode) {
            self.card = None;
        }
        let current = if card {
            &mut self.card
        } else {
            &mut self.trigger
        };
        if hovered {
            *current = Some(mode);
        } else if *current == Some(mode) {
            *current = None;
        }
    }
}

#[derive(IntoElement)]
struct ModeHoverCard {
    mode: Mode,
    theme: Theme,
}

impl gpui::RenderOnce for ModeHoverCard {
    fn render(self, _window: &mut Window, _cx: &mut gpui::App) -> impl IntoElement {
        let theme = self.theme;
        let mode = self.mode;
        let description = match mode {
            Mode::Instant => tr::main::mode::hover::instant_description(),
            Mode::Studio => tr::main::mode::hover::studio_description(),
            Mode::Screenshot => tr::main::mode::hover::screenshot_description(),
        };
        div()
            .flex()
            .flex_col()
            .gap(px(8.))
            .w(px(240.))
            .px(px(12.))
            .py(px(10.))
            .rounded(px(8.))
            .border_1()
            .border_color(theme.gray_3)
            .bg(theme.gray_12)
            .text_color(theme.gray_1)
            .shadow_lg()
            .child(div().text_size(px(12.)).child(mode.hover_title()))
            .child(
                div()
                    .text_size(px(10.))
                    .line_height(px(13.75))
                    .text_color(theme.gray_4)
                    .child(description),
            )
            .when(mode != Mode::Screenshot, |this| {
                this.child(
                    div()
                        .id("mode-quality-settings")
                        .group("mode-quality-settings")
                        .tab_index(0)
                        .flex()
                        .items_center()
                        .gap(px(6.))
                        .mx(px(-4.))
                        .px(px(8.))
                        .py(px(4.))
                        .rounded(px(6.))
                        .text_size(px(11.))
                        .text_color(theme.gray_4)
                        .hover(|style| style.bg(theme.gray_11).text_color(theme.gray_1))
                        .child(
                            svg()
                                .path("icons/settings.svg")
                                .size(px(12.))
                                .text_color(theme.gray_4)
                                .group_hover("mode-quality-settings", |style| {
                                    style.text_color(theme.gray_1)
                                }),
                        )
                        .child(tr::main::mode::hover::quality_settings())
                        .on_click(move |_, _, cx| {
                            cx.stop_propagation();
                            cx.defer(move |cx| app_windows::open_quality_settings(mode, cx));
                        }),
                )
            })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetType {
    Display,
    Window,
    Area,
    CameraOnly,
}

impl TargetType {
    fn label(self) -> &'static str {
        match self {
            Self::Display => "Display",
            Self::Window => "Window",
            Self::Area => "Area",
            Self::CameraOnly => "Camera Only",
        }
    }

    /// [`Self::label`] in the UI language. `label` stays English: it names
    /// element ids.
    fn display_label(self) -> &'static str {
        match self {
            Self::Display => tr::main::target::display(),
            Self::Window => tr::main::target::window(),
            Self::Area => tr::main::target::area(),
            Self::CameraOnly => tr::main::target::camera_only(),
        }
    }

    fn icon(self) -> &'static str {
        match self {
            Self::Display => "icons/screen.svg",
            Self::Window => "icons/window.svg",
            Self::Area => "icons/area.svg",
            Self::CameraOnly => "icons/camera.svg",
        }
    }
}

/// Which device picker has taken over the window body, if any.
///
/// Clicking a device row does not open a popup in the Tauri app either: it
/// swaps the whole body for a full-height panel and offers a Back button.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceMenu {
    Camera,
    Microphone,
}

fn camera_format_choices(
    formats: Result<Vec<devices::CameraFormat>, String>,
) -> (Vec<DeviceFormat>, Option<String>) {
    let mut choices = vec![DeviceFormat::Camera(Default::default())];
    match formats {
        Ok(formats) => {
            choices.extend(
                formats
                    .into_iter()
                    .map(|format| DeviceFormat::Camera(format.settings())),
            );
            (choices, None)
        }
        Err(error) => (choices, Some(error)),
    }
}

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

    #[test]
    fn failed_probe_keeps_automatic_available() {
        let (choices, notice) = camera_format_choices(Err("Camera access denied".into()));
        assert_eq!(choices, vec![DeviceFormat::Camera(Default::default())]);
        assert_eq!(notice.as_deref(), Some("Camera access denied"));
    }

    #[test]
    fn empty_formats_keep_automatic_available() {
        let (choices, notice) = camera_format_choices(Ok(Vec::new()));
        assert_eq!(choices, vec![DeviceFormat::Camera(Default::default())]);
        assert!(notice.is_none());
    }

    #[test]
    fn successful_retry_adds_current_formats_and_clears_error() {
        let format = devices::CameraFormat {
            width: 1280,
            height: 720,
            frame_rate: 60.0,
        };
        let (choices, notice) = camera_format_choices(Ok(vec![format]));
        assert_eq!(
            choices,
            vec![
                DeviceFormat::Camera(Default::default()),
                DeviceFormat::Camera(format.settings())
            ]
        );
        assert!(notice.is_none());
    }
}

fn input_refresh_is_current(
    current_generation: u64,
    current_menu: Option<DeviceMenu>,
    generation: u64,
    menu: DeviceMenu,
) -> bool {
    current_generation == generation && current_menu == Some(menu)
}

fn input_refresh_scan_allowed(current: bool, visible_idle_and_owned: bool) -> Option<bool> {
    current.then_some(visible_idle_and_owned)
}

#[cfg(test)]
mod input_refresh_tests {
    use super::{DeviceMenu, input_refresh_is_current, input_refresh_scan_allowed};

    #[test]
    fn hidden_or_busy_picker_pauses_and_resumes_without_a_scan() {
        assert_eq!(input_refresh_scan_allowed(true, false), Some(false));
        assert_eq!(input_refresh_scan_allowed(true, true), Some(true));
        assert_eq!(input_refresh_scan_allowed(false, true), None);
        assert_eq!(input_refresh_scan_allowed(false, false), None);
    }

    #[test]
    fn late_refresh_cannot_replace_a_reopened_picker() {
        assert!(input_refresh_is_current(
            2,
            Some(DeviceMenu::Camera),
            2,
            DeviceMenu::Camera
        ));
        assert!(!input_refresh_is_current(
            3,
            Some(DeviceMenu::Camera),
            2,
            DeviceMenu::Camera
        ));
        assert!(!input_refresh_is_current(2, None, 2, DeviceMenu::Camera));
        assert!(!input_refresh_is_current(
            2,
            Some(DeviceMenu::Microphone),
            2,
            DeviceMenu::Camera
        ));
    }
}

#[derive(Clone, Debug, PartialEq)]
enum DeviceFormatTarget {
    Camera(CameraOption),
    Microphone(String),
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum DeviceFormat {
    Camera(cap_recording::feeds::camera::CameraDeviceSettings),
    Microphone(cap_recording::feeds::microphone::MicrophoneDeviceSettings),
}

impl DeviceFormat {
    fn label(self) -> String {
        match self {
            Self::Camera(settings) => {
                match (settings.width, settings.height, settings.frame_rate) {
                    (Some(width), Some(height), Some(rate)) => {
                        format!("{width}×{height} @ {rate:.0}fps")
                    }
                    _ => tr::main::device::format::default().into(),
                }
            }
            Self::Microphone(settings) => match (settings.sample_rate, settings.channels) {
                (Some(rate), Some(channels)) => {
                    let channels = match channels {
                        1 => tr::main::device::format::mono().into(),
                        2 => tr::main::device::format::stereo().into(),
                        count => tr::main::device::format::channels(usize::from(count)),
                    };
                    format!("{}kHz {channels}", rate as f32 / 1000.)
                }
                _ => tr::main::device::format::default().into(),
            },
        }
    }
}

struct PendingDeviceFormat {
    target: DeviceFormatTarget,
    format: DeviceFormat,
    epoch: u64,
}

fn complete_format_request(
    pending: &mut Option<PendingDeviceFormat>,
    result: Option<Result<(), String>>,
    still_owned: bool,
    save: impl FnOnce(&DeviceFormatTarget, DeviceFormat) -> bool,
) -> Option<Result<DeviceFormat, String>> {
    let result = result?;
    let pending = pending.take()?;
    if !still_owned {
        return None;
    }
    Some(result.and_then(|()| {
        if save(&pending.target, pending.format) {
            Ok(pending.format)
        } else {
            Err(tr::main::device::format::save_failed().to_string())
        }
    }))
}

fn save_device_format(target: &DeviceFormatTarget, format: DeviceFormat) -> bool {
    match (target, format) {
        (DeviceFormatTarget::Camera(camera), DeviceFormat::Camera(settings)) => {
            crate::store::set_camera_device_settings(
                &camera.device_id,
                camera.model_id.as_ref(),
                settings,
            )
        }
        (DeviceFormatTarget::Microphone(name), DeviceFormat::Microphone(settings)) => {
            crate::store::set_microphone_device_settings(name, settings)
        }
        _ => false,
    }
}

impl DeviceMenu {
    fn permission(self) -> OSPermission {
        match self {
            Self::Camera => OSPermission::Camera,
            Self::Microphone => OSPermission::Microphone,
        }
    }

    /// Element ids; [`Self::title`] is the label.
    fn id_name(self) -> &'static str {
        match self {
            Self::Camera => "Camera",
            Self::Microphone => "Microphone",
        }
    }

    fn title(self) -> &'static str {
        match self {
            Self::Camera => tr::main::device::camera(),
            Self::Microphone => tr::main::device::microphone(),
        }
    }

    fn none_label(self) -> &'static str {
        match self {
            Self::Camera => tr::main::device::no_camera(),
            Self::Microphone => tr::main::device::no_microphone(),
        }
    }

    fn icon(self) -> &'static str {
        match self {
            Self::Camera => "icons/camera.svg",
            Self::Microphone => "icons/microphone.svg",
        }
    }

    fn empty_message(self, searching: bool) -> &'static str {
        match (self, searching) {
            (Self::Camera, false) => tr::main::device::no_cameras(),
            (Self::Camera, true) => tr::main::device::no_matching_cameras(),
            (Self::Microphone, false) => tr::main::device::no_microphones(),
            (Self::Microphone, true) => tr::main::device::no_matching_microphones(),
        }
    }
}

/// Whatever has taken over the window body in place of the home screen.
///
/// The Tauri app opens some of these as separate windows (mode info is the
/// 580x340 ModeSelect window) and some as in-place panels. There is only one
/// window here, so they are all panels; the ones that differ are called out
/// in the README's deviations table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Panel {
    /// Pick a camera or a microphone.
    Device(DeviceMenu),
    /// Pick which display or which window to capture.
    Target(TargetType),
    /// What the three recording modes do.
    ModeInfo,
    /// The header recordings / screenshots library.
    Library(LibraryKind),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LibraryKind {
    Recordings,
    Screenshots,
}

impl LibraryKind {
    fn settings_page(self) -> Page {
        match self {
            Self::Recordings => Page::Recordings,
            Self::Screenshots => Page::Screenshots,
        }
    }

    fn import_label(self) -> &'static str {
        match self {
            Self::Recordings => tr::main::library::import_recordings(),
            Self::Screenshots => tr::main::library::import_screenshots(),
        }
    }

    fn empty_title(self) -> &'static str {
        match self {
            Self::Recordings => tr::main::library::no_recordings(),
            Self::Screenshots => tr::main::library::no_screenshots(),
        }
    }

    fn empty_description(self) -> &'static str {
        match self {
            Self::Recordings => tr::main::library::recordings_description(),
            Self::Screenshots => tr::main::library::screenshots_description(),
        }
    }

    fn empty_icon(self) -> &'static str {
        match self {
            Self::Recordings => "icons/square-play.svg",
            Self::Screenshots => "icons/image.svg",
        }
    }

    fn view_all_label(self) -> &'static str {
        match self {
            Self::Recordings => tr::main::library::view_all_recordings(),
            Self::Screenshots => tr::main::library::view_all_screenshots(),
        }
    }

    fn no_match(self) -> &'static str {
        match self {
            Self::Recordings => tr::main::library::no_matching_recordings(),
            Self::Screenshots => tr::main::library::no_matching_screenshots(),
        }
    }
}

enum LibraryItems {
    Recordings(Vec<LibraryRow<RecordingItem>>),
    Screenshots(Vec<LibraryRow<ScreenshotItem>>),
}

struct LibraryRow<T> {
    item: T,
    thumbnail: Option<std::sync::Arc<gpui::RenderImage>>,
    thumbnail_stale: bool,
}

#[derive(Clone)]
pub(crate) struct RecordingStartPermit(std::rc::Rc<std::cell::Cell<bool>>);

impl RecordingStartPermit {
    fn prepare(phase: Phase, clean_capture_owned: bool, preparing: bool) -> Result<Self, String> {
        if phase != Phase::Idle || clean_capture_owned || preparing {
            return Err("Another recording or recording preparation owns the inputs".into());
        }
        Ok(Self(std::rc::Rc::new(std::cell::Cell::new(true))))
    }

    fn allows(&self, phase: Phase, clean_capture_owned: bool) -> bool {
        if phase != Phase::Idle || clean_capture_owned {
            self.cancel();
        }
        self.0.get()
    }

    pub(crate) fn is_current(&self, cx: &gpui::App) -> bool {
        self.allows(
            RecordingSession::global(cx).read(cx).phase,
            app_windows::clean_capture_owned(cx),
        )
    }

    pub(crate) fn cancel(&self) {
        self.0.set(false);
    }

    fn same(&self, other: &Self) -> bool {
        std::rc::Rc::ptr_eq(&self.0, &other.0)
    }

    fn cancel_current(current: &mut Option<Self>) -> bool {
        let Some(permit) = current.take() else {
            return false;
        };
        permit.cancel();
        true
    }
}

/// Instant Mode's sign-in gate, shown here when the overlay's "Sign In To
/// Use" is pressed: the overlays close so the browser is reachable, and they
/// re-open for `kind` once the account is signed in.
struct SignInPrompt {
    kind: TargetType,
    error: Option<String>,
}

struct MicrophoneWarning {
    config: recording::StartConfig,
    permit: RecordingStartPermit,
    dont_show_again: bool,
    error: Option<String>,
}

pub struct MainWindow {
    theme: Theme,
    mode: Mode,
    mode_hover: ModeHoverState,
    mode_hover_task: Option<Task<()>>,
    mode_hover_bounds: [Rc<Cell<Option<gpui::Bounds<gpui::Pixels>>>>; 3],
    target: Option<TargetType>,
    devices: DeviceSnapshot,
    camera: Option<CameraOption>,
    camera_id: Option<recording::DeviceOrModelID>,
    microphone: Option<MicrophoneOption>,
    microphone_level: Entity<ui::MicrophoneLevel>,
    device_microphone_level: Entity<ui::MicrophoneLevel>,
    pending_device_restore: crate::store::RecordingInputSettings,
    device_restore_suspended: bool,
    device_format_target: Option<DeviceFormatTarget>,
    device_formats: Option<Result<Vec<DeviceFormat>, String>>,
    device_format_value: Option<DeviceFormat>,
    device_format_generation: u64,
    device_format_pending: Option<PendingDeviceFormat>,
    device_format_notice: Option<String>,
    system_audio: bool,
    /// Which display/window is selected for each split target.
    selected_display: Option<DisplayOption>,
    selected_window: Option<WindowOption>,
    panel: Option<Panel>,
    /// Live filter text for the device and target panels -- a mirror of
    /// `search_input`'s value, kept as a plain `String` because every list in
    /// the panel filters against it from a `&self` method.
    search: String,
    /// The real field. `ui::TextInputState` owns the caret, the selection and
    /// the clipboard; this window owns what Escape means.
    search_input: Entity<ui::TextInputState>,
    _search_events: gpui::Subscription,
    /// True until the background enumeration has reported back, so the panel can
    /// say "Loading..." rather than "No cameras found".
    enumerating: bool,
    /// The app-wide recording session; the lifecycle itself lives there so the
    /// controls bar window can drive the same recording.
    session: Entity<RecordingSession>,
    checking_storage: bool,
    deep_link_start: Option<RecordingStartPermit>,
    microphone_warning: Option<MicrophoneWarning>,
    sign_in_prompt: Option<SignInPrompt>,
    copied_error: Option<String>,
    /// Header recordings / screenshots panel. Scanned only while that panel
    /// is open so a large library is not walked on every home paint.
    library: Option<LibraryItems>,
    library_task: Option<gpui::Task<()>>,
    incomplete_recording: Option<library::IncompleteRecordingItem>,
    recovery_error: Option<String>,
    recovery_pending: bool,
    recovery_scan_task: Option<Task<()>>,
    recovery_action_task: Option<Task<()>>,
    plan: PlanBadge,
    /// Display/window thumbnails and app icons for the target cards. See
    /// `target_thumbnails::ThumbnailCache` for why this is per-view rather
    /// than an app global.
    thumbnails: target_thumbnails::ThumbnailCache,
    /// The in-flight capture sweep for each kind. Assigning over one drops the
    /// previous, which aborts its tokio task; the `*_inflight` flags on the
    /// cache are what actually stop refreshes stacking.
    display_thumbnail_task: Option<gpui::Task<()>>,
    window_thumbnail_task: Option<gpui::Task<()>>,
    /// The `staleTime: 5_000` re-read of the cheap target list that runs while
    /// a target panel is open, and nothing else. Dropped by `close_panel`.
    target_poll_task: Option<gpui::Task<()>>,
    input_poll_task: Option<gpui::Task<()>>,
    input_poll_generation: u64,
    input_enumeration_gate: devices::InputEnumerationGate,
    /// `scheduleTargetListPrewarm` (`new-main/index.tsx:1897-1965`).
    prewarm_task: Option<gpui::Task<()>>,
    keyboard_nav: bool,
    _keyboard_nav: gpui::Subscription,
}

/// `createLicenseQuery` (`utils/queries.ts:257-273`): pro from the auth
/// plan first, then a commercial license, then personal.
#[derive(Clone, Copy, PartialEq, Eq)]
enum PlanBadge {
    Personal,
    Pro,
    Commercial,
}

impl PlanBadge {
    fn current() -> Self {
        if crate::store::auth_snapshot().is_upgraded() {
            Self::Pro
        } else if crate::store::commercial_license().is_some() {
            Self::Commercial
        } else {
            Self::Personal
        }
    }
}

impl MainWindow {
    pub fn new(
        session: Entity<RecordingSession>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        crate::theme::bind_window(window, cx);
        window.on_window_should_close(cx, |_, cx| {
            // The custom Windows X hides to the tray; native close requests must still quit.
            #[cfg(target_os = "windows")]
            cx.defer(crate::menus::quit);
            #[cfg(not(target_os = "windows"))]
            cx.defer(app_windows::request_close_main);
            false
        });
        cx.observe_window_activation(window, |this, window, cx| {
            if !window.is_window_active() {
                this.clear_mode_hover();
            } else {
                MediaAccess::global(cx).update(cx, |access, cx| access.refresh(cx));
            }
            #[cfg(target_os = "windows")]
            if window.is_window_active() && !this.devices.displays.is_empty() {
                this.schedule_target_prewarm(window, cx);
            }
            cx.notify();
        })
        .detach();
        let theme = Theme::for_window(window, cx, true);
        let mut previous_phase = Phase::Idle;
        cx.observe_in(&session, window, move |this, session, window, cx| {
            let phase = session.read(cx).phase;
            if phase != Phase::Idle {
                this.cancel_deep_link_start();
                this.clear_mode_hover();
            }
            if phase == Phase::Idle && previous_phase != Phase::Idle {
                this.scan_incomplete_recordings(window, cx, std::time::Duration::ZERO);
                if let Some(notice) = session.read(cx).storage_notice.clone() {
                    let main = cx.global::<app_windows::AppWindows>().main;
                    cx.spawn(async move |_, cx| {
                        let receiver = cx.update(|cx| {
                            app_windows::show_main_window(cx);
                            cx.activate(true);
                            main.update(cx, |_, window, cx| {
                                window.prompt(
                                    gpui::PromptLevel::Warning,
                                    tr::main::storage::title(),
                                    Some(&notice),
                                    &[gpui::PromptButton::cancel(tr::common::ok())],
                                    cx,
                                )
                            })
                        });
                        if let Ok(receiver) = receiver {
                            let _ = receiver.await;
                        }
                    })
                    .detach();
                }
            }
            previous_phase = phase;
            cx.notify();
        })
        .detach();

        // Track the app-scoped feeds: the camera bubble's close button
        // deselects the camera there, and this window's selection has to
        // follow. Repaints are gated to what is actually visible.
        let feeds = Feeds::global(cx);
        let mic_level = feeds.read(cx).mic_level().clone();
        let microphone_level = cx.new(|cx| {
            ui::MicrophoneLevel::new(
                ui::MicrophoneLevelKind::CaptureButton,
                &mic_level,
                window,
                cx,
            )
        });
        let device_microphone_level = cx.new(|cx| {
            ui::MicrophoneLevel::new(ui::MicrophoneLevelKind::DeviceRow, &mic_level, window, cx)
        });
        cx.observe(&feeds, |this: &mut Self, feeds, cx| {
            let feeds = feeds.read(cx);
            let camera_id = feeds.camera.as_ref().map(|camera| &camera.id);
            let camera_changed = this.camera_id.as_ref() != camera_id;
            if camera_changed {
                this.pending_device_restore.camera_id = None;
                this.camera_id = camera_id.cloned();
                this.camera = feeds.camera.as_ref().map(|selected| {
                    let mut camera = remembered_camera(&selected.id, &this.devices.cameras);
                    camera.label = selected.label.clone();
                    camera
                });
                if !crate::store::set_recording_camera_id(this.camera_id.as_ref()) {
                    tracing::warn!("Could not save the selected camera");
                }
            }
            let microphone_changed = this.microphone.as_ref().map(|microphone| &microphone.name)
                != feeds.microphone.as_ref();
            if microphone_changed {
                this.pending_device_restore.microphone_name = None;
                this.microphone = feeds
                    .microphone
                    .as_deref()
                    .map(|name| remembered_microphone(name, &this.devices.microphones));
                if !crate::store::set_recording_microphone_name(feeds.microphone.as_deref()) {
                    tracing::warn!("Could not save the selected microphone");
                }
            }
            let format_result =
                this.device_format_pending
                    .as_ref()
                    .and_then(|pending| match pending.format {
                        DeviceFormat::Camera(_) => feeds.camera_configuration_result(pending.epoch),
                        DeviceFormat::Microphone(_) => {
                            feeds.microphone_configuration_result(pending.epoch)
                        }
                    });
            if camera_changed || microphone_changed || matches!(this.panel, Some(Panel::Device(_)))
            {
                cx.notify();
            }
            this.finish_device_format_change(format_result, cx);
        })
        .detach();

        // Enumeration hits AVFoundation and the window server, so it must not
        // run on the main thread -- doing it inline here costs ~180ms of a
        // blank window on this machine, and more on a machine with more
        // capture devices.

        // The filter field. Constructing it here (rather than lazily, per
        // panel) is what lets one focus handle survive a panel change, and the
        // blur listener inside `TextInputState` needs a `&mut Window` anyway.
        let search_input = cx.new(|cx| ui::TextInputState::single_line(window, cx));
        let search_events = cx.subscribe(&search_input, Self::on_search_event);

        let media = MediaAccess::global(cx);
        let mut media_allowed = (
            media.read(cx).allowed(OSPermission::Camera),
            media.read(cx).allowed(OSPermission::Microphone),
        );
        cx.observe(&media, move |this: &mut Self, media, cx| {
            let allowed = (
                media.read(cx).allowed(OSPermission::Camera),
                media.read(cx).allowed(OSPermission::Microphone),
            );
            // A grant that lands after a device was picked (the prompt
            // answered, or the switch flipped in System Settings) retries
            // that device instead of leaving its failed start behind.
            if allowed.0 && !media_allowed.0 && this.camera.is_some() {
                let (camera, id) = (this.camera.clone(), this.camera_id.clone());
                this.apply_camera_selection(camera, id, cx);
            }
            if allowed.1 && !media_allowed.1 && this.microphone.is_some() {
                let microphone = this.microphone.clone();
                this.apply_microphone_selection(microphone, cx);
            }
            media_allowed = allowed;
            cx.notify();
        })
        .detach();

        let sign_in = SignIn::global(cx);
        cx.observe(&sign_in, |_, _, cx| cx.notify()).detach();
        cx.subscribe(&sign_in, |this, _, event, cx| {
            let SignInEvent::Finished { origin, outcome } = event else {
                return;
            };
            let Some(prompt) = this.sign_in_prompt.as_mut() else {
                return;
            };
            match outcome {
                SignInOutcome::SignedIn => {
                    let kind = prompt.kind;
                    this.sign_in_prompt = None;
                    let session = this.session.read(cx);
                    if session.phase == Phase::Idle && session.editor_recording_target().is_none() {
                        this.arm_overlay(kind, cx);
                    }
                }
                SignInOutcome::Failed(error) if matches!(origin, SignInOrigin::Overlay(_)) => {
                    prompt.error = Some(error.clone());
                }
                SignInOutcome::TimedOut if matches!(origin, SignInOrigin::Overlay(_)) => {
                    prompt.error = Some(tr::main::sign_in::timed_out().into());
                }
                _ => {}
            }
            cx.notify();
        })
        .detach();

        let keyboard_nav = ui::keyboard_nav::install(window, cx, Self::keyboard_nav_blocked);

        Self {
            theme,
            // `rawOptions.mode` -- the recording mode is a persisted setting,
            // and the tray's mode picker writes the same key, so the
            // window has to start where the store left it rather than at a
            // hardcoded Instant.
            mode: Mode::from_store(),
            mode_hover: ModeHoverState::default(),
            mode_hover_task: None,
            mode_hover_bounds: std::array::from_fn(|_| Rc::new(Cell::new(None))),
            target: None,
            devices: DeviceSnapshot::default(),
            camera: None,
            camera_id: None,
            microphone: None,
            microphone_level,
            device_microphone_level,
            pending_device_restore: crate::store::RecordingInputSettings::load(),
            device_restore_suspended: false,
            device_format_target: None,
            device_formats: None,
            device_format_value: None,
            device_format_generation: 0,
            device_format_pending: None,
            device_format_notice: None,
            system_audio: crate::store::recording_system_audio(),
            selected_display: None,
            selected_window: None,
            panel: None,
            search: String::new(),
            search_input,
            _search_events: search_events,
            enumerating: true,
            session,
            checking_storage: false,
            deep_link_start: None,
            microphone_warning: None,
            sign_in_prompt: None,
            copied_error: None,
            library: None,
            library_task: None,
            incomplete_recording: None,
            recovery_error: None,
            recovery_pending: false,
            recovery_scan_task: None,
            recovery_action_task: None,
            plan: PlanBadge::current(),
            thumbnails: target_thumbnails::ThumbnailCache::default(),
            display_thumbnail_task: None,
            window_thumbnail_task: None,
            target_poll_task: None,
            input_poll_task: None,
            input_poll_generation: 0,
            input_enumeration_gate: devices::InputEnumerationGate::default(),
            prewarm_task: None,
            keyboard_nav: false,
            _keyboard_nav: keyboard_nav,
        }
    }

    fn keyboard_nav_blocked(&self, cx: &gpui::App) -> bool {
        self.session.read(cx).phase != Phase::Idle
            || self.microphone_warning.is_some()
            || self.sign_in_prompt.is_some()
            || self.idle_error(cx).is_some()
            || app_windows::clean_capture_pending(cx)
    }

    /// Kick off device enumeration.
    ///
    /// Deliberately *not* called from `new`. `new` runs inside `open_window`'s
    /// builder closure, before the window is fully constructed, and a task
    /// spawned there resolves against a window whose invalidator is not yet
    /// wired to the platform window -- the update runs, the model updates, and
    /// no frame is ever scheduled, so the panels stay on "Loading..." until
    /// some unrelated event forces a redraw. Calling this once the window
    /// handle exists is what makes the refresh land.
    pub fn start_enumeration(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        cx.spawn_in(window, async move |this, cx| {
            let snapshot = cx
                .background_executor()
                .spawn(async { DeviceSnapshot::enumerate() })
                .await;

            this.update_in(cx, |this, window, cx| {
                tracing::info!(
                    cameras = snapshot.cameras.len(),
                    microphones = snapshot.microphones.len(),
                    displays = snapshot.displays.len(),
                    windows = snapshot.windows.len(),
                    "enumerated capture devices"
                );
                this.devices = snapshot;
                this.enumerating = false;

                this.restore_recording_inputs(cx);

                // `CAP_GPUI_AUTO_CAMERA=1`: select the first camera the way a
                // click would -- the automated check drives the preview window
                // this way because synthetic clicks are dropped.
                if std::env::var("CAP_GPUI_AUTO_CAMERA").is_ok_and(|v| v == "1")
                    && this.camera.is_none()
                    && let Some(first) = this.devices.cameras.first().cloned()
                {
                    tracing::info!(camera = %first.label, "auto-selecting camera");
                    this.set_camera_selection(Some(first), cx);
                }
                if std::env::var("CAP_GPUI_AUTO_MIC").is_ok_and(|value| value == "1")
                    && this.microphone.is_none()
                {
                    let selected =
                        cap_recording::feeds::microphone::MicrophoneFeed::default_device()
                            .and_then(|(name, _, _)| {
                                this.devices
                                    .microphones
                                    .iter()
                                    .find(|microphone| microphone.name == name)
                                    .cloned()
                            })
                            .or_else(|| this.devices.microphones.first().cloned());
                    if let Some(microphone) = selected {
                        tracing::info!(microphone = %microphone.name, "auto-selecting microphone");
                        this.set_microphone_selection(Some(microphone), cx);
                    }
                }
                // `CAP_GPUI_AUTO_PANEL=display|window`: open that target
                // picker panel the way the chevron click does. Same reason as
                // `CAP_GPUI_AUTO_CAMERA` above -- the chrome screenshots need
                // the card grid on screen and synthetic clicks are dropped.
                match std::env::var("CAP_GPUI_AUTO_PANEL").as_deref() {
                    Ok("display") => {
                        this.open_panel(Panel::Target(TargetType::Display), window, cx)
                    }
                    Ok("window") => this.open_panel(Panel::Target(TargetType::Window), window, cx),
                    Ok("camera") => this.open_panel(Panel::Device(DeviceMenu::Camera), window, cx),
                    Ok("microphone") => {
                        this.open_panel(Panel::Device(DeviceMenu::Microphone), window, cx)
                    }
                    _ => {}
                }
                // `if (!targetMode) scheduleTargetListPrewarm()` on
                // `main-window-ready` (`new-main/index.tsx:2528`) -- the point
                // where the window has its lists and can afford background
                // work. Here that point is enumeration landing.
                this.schedule_target_prewarm(window, cx);
                cx.notify();
            })
            .unwrap_or_else(|error| tracing::error!("device enumeration update failed: {error:#}"));
        })
        .detach();
    }

    // -- Target thumbnails (`thumbnails/mod.rs` + the queries around it) -----

    fn target_prewarm_allowed(&self, window: &Window, cx: &Context<Self>) -> bool {
        let visible = if cfg!(target_os = "macos") {
            crate::platform::window_is_visible(window)
        } else {
            window.is_window_active()
        };
        self.session.read(cx).phase == Phase::Idle
            && visible
            && !crate::app_windows::onboarding_is_open(cx)
            && crate::store::has_completed_startup()
            && crate::store::has_completed_onboarding()
    }

    fn schedule_target_prewarm(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if !self.target_prewarm_allowed(window, cx) || !self.thumbnails.take_prewarm() {
            return;
        }

        self.prewarm_task = Some(cx.spawn_in(window, async move |this, cx| {
            cx.background_executor()
                .timer(target_thumbnails::PREWARM_DELAY)
                .await;

            // Displays first, then windows -- the source's two awaited
            // `prefetchQuery` calls, in that order. Each sweep re-lists, so the
            // separate cheap-list prefetch it opens with is already covered.
            for kind in [TargetType::Display, TargetType::Window] {
                let Ok(sweep) = this.update_in(cx, |this, window, cx| {
                    if this.target_prewarm_allowed(window, cx) {
                        this.start_capture(kind, window, cx)
                    } else {
                        None
                    }
                }) else {
                    return;
                };
                if let Some(sweep) = sweep {
                    sweep.await;
                }
            }
        }));
    }

    /// Install a fresh cheap list without disturbing the thumbnails already on
    /// screen: reconciliation is by id, so a target that is still there keeps
    /// its image and only targets that went away are evicted. Blanking the
    /// cache on every 5s tick would make the grid flicker once per poll.
    fn install_target_lists(
        &mut self,
        targets: devices::TargetSnapshot,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.install_displays(targets.displays, window, cx);
        self.install_windows(targets.windows, window, cx);
    }

    fn install_displays(
        &mut self,
        displays: Vec<DisplayOption>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let dropped = self.thumbnails.retain_displays(&displays);
        let changed = self.devices.install_displays(displays) || !dropped.is_empty();
        for image in dropped {
            let _ = window.drop_image(image);
        }
        if changed {
            cx.notify();
        }
    }

    fn install_windows(
        &mut self,
        windows: Vec<WindowOption>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let dropped = self.thumbnails.retain_windows(&windows);
        let changed = self.devices.install_windows(windows) || !dropped.is_empty();
        for image in dropped {
            let _ = window.drop_image(image);
        }
        if changed {
            cx.notify();
        }
    }

    /// Start one capture sweep, or return `None` because one is already in
    /// flight -- the `fetchStatus !== "idle"` guard on the source's refetch
    /// effects (`new-main/index.tsx:2626, 2635`). Refreshes never stack.
    ///
    /// The returned task is handed to the caller rather than stored, because
    /// the prewarm has to *await* the display sweep before starting the window
    /// sweep (`prefetchQuery` displays, then `prefetchQuery` windows) while the
    /// panel poll just parks it in a field.
    ///
    /// Everything expensive happens off the main thread: the enumeration, the
    /// ScreenCaptureKit capture (tokio, because the cidre future needs a
    /// reactor) and the RGBA-to-`RenderImage` swap all run inside the spawned
    /// task; the entity update only swaps `Arc`s.
    fn start_capture(
        &mut self,
        kind: TargetType,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<()>> {
        if target_thumbnails::captures_paused() {
            return None;
        }
        match kind {
            TargetType::Display => {
                if self.thumbnails.display_inflight() {
                    return None;
                }
                self.thumbnails.set_display_inflight(true);

                let (events_tx, events) = flume::unbounded();
                // The tokio handle rides along in the returned task so that
                // dropping the task tears the sweep down: the `flume` receiver
                // goes with it and the sweep's next `send` fails, which is what
                // actually stops it (see `run_capture` -- the capture itself is
                // on a blocking thread and cannot be aborted mid-screenshot).
                let capture = gpui_tokio::Tokio::spawn(cx, async move {
                    target_thumbnails::capture_displays(events_tx).await;
                });

                Some(cx.spawn_in(window, async move |this, cx| {
                    let _capture = capture;
                    let mut swept = None;
                    while let Ok(first) = events.recv_async().await {
                        let mut batch = vec![first];
                        batch.extend(events.try_iter());
                        for event in &batch {
                            if let target_thumbnails::DisplayEvent::Listed(list) = event {
                                swept = Some(target_thumbnails::display_signature(list));
                            }
                        }
                        if this
                            .update_in(cx, |this, window, cx| {
                                this.apply_display_events(batch, window, cx)
                            })
                            .is_err()
                        {
                            return;
                        }
                    }
                    // The channel closing is the sweep finishing, which is when
                    // the Tauri query resolves and
                    // `setDisplayThumbnailsSignature` runs
                    // (`new-main/index.tsx:2617-2620`). Committing the
                    // signature at `Listed` time instead would mark a sweep
                    // that never landed as up to date. Likewise a sweep that a
                    // capture hang cut short: it stays stale so the picker
                    // retries it once the pause lifts.
                    this.update(cx, |this, cx| {
                        if let Some(signature) = swept
                            && !target_thumbnails::captures_paused()
                        {
                            this.thumbnails.set_display_signature(signature);
                        }
                        this.thumbnails.set_display_inflight(false);
                        // In-process SCK screenshot sweeps can provoke the
                        // macOS 26 style-mask mutation on the main window
                        // while it is visible (prewarm at launch, panel-open
                        // refreshes) -- re-assert the borderless mask.
                        crate::app_windows::heal_main_window_style(cx);
                    })
                    .ok();
                }))
            }
            TargetType::Window => {
                if self.thumbnails.window_inflight() {
                    return None;
                }
                self.thumbnails.set_window_inflight(true);

                let (events_tx, events) = flume::unbounded();
                let current = self.thumbnails.captured_windows();
                let capture = gpui_tokio::Tokio::spawn(cx, async move {
                    target_thumbnails::capture_windows(events_tx, current).await;
                });

                Some(cx.spawn_in(window, async move |this, cx| {
                    let _capture = capture;
                    let mut swept = None;
                    while let Ok(first) = events.recv_async().await {
                        let mut batch = vec![first];
                        batch.extend(events.try_iter());
                        for event in &batch {
                            if let target_thumbnails::WindowEvent::Listed(list) = event {
                                swept = Some(target_thumbnails::window_signature(list));
                            }
                        }
                        if this
                            .update_in(cx, |this, window, cx| {
                                this.apply_window_events(batch, window, cx)
                            })
                            .is_err()
                        {
                            return;
                        }
                    }
                    this.update(cx, |this, cx| {
                        if let Some(signature) = swept
                            && !target_thumbnails::captures_paused()
                        {
                            this.thumbnails.set_window_signature(signature);
                        }
                        this.thumbnails.set_window_inflight(false);
                        // Same macOS 26 style-mask heal as the display sweep.
                        crate::app_windows::heal_main_window_style(cx);
                    })
                    .ok();
                }))
            }
            // Area and Camera Only have no cards to fill.
            TargetType::Area | TargetType::CameraOnly => None,
        }
    }

    /// The while-a-picker-is-open loop.
    ///
    /// (a) an immediate cheap re-list, which is what makes opening the panel
    /// show windows opened since launch; (b) every `CAPTURE_LIST_STALE_TIME`,
    /// another cheap re-list plus the signature comparison from
    /// `new-main/index.tsx:2621-2639` -- refetch the thumbnails when the list
    /// they were captured from no longer matches the live one; (c) for
    /// displays only, a `CAPTURE_THUMBNAIL_STALE_TIME` floor as well, the
    /// `refetchInterval: 10_000` that `listDisplaysWithThumbnails`
    /// (`utils/queries.ts:75`) carries and the editor sidebar inherits.
    ///
    /// Nothing here runs while no target panel is open: `close_panel` drops the
    /// task, which is the `enabled:` on every one of those queries.
    fn start_target_poll(&mut self, kind: TargetType, window: &mut Window, cx: &mut Context<Self>) {
        if matches!(kind, TargetType::Area | TargetType::CameraOnly) {
            self.target_poll_task = None;
            return;
        }

        self.target_poll_task = Some(cx.spawn_in(window, async move |this, cx| {
            let mut last_display_capture: Option<std::time::Instant> = None;
            loop {
                let list = cx
                    .background_executor()
                    .spawn(async { devices::TargetSnapshot::enumerate() })
                    .await;

                let Ok(()) = this.update_in(cx, |this, window, cx| {
                    this.install_target_lists(list, window, cx);

                    let stale = match kind {
                        TargetType::Display => {
                            this.thumbnails.displays_stale(&this.devices.displays)
                                || last_display_capture.is_none_or(|at| {
                                    at.elapsed() >= target_thumbnails::THUMBNAIL_STALE_TIME
                                })
                        }
                        _ => this.thumbnails.windows_stale(&this.devices.windows),
                    };
                    if !stale {
                        return;
                    }
                    if let Some(task) = this.start_capture(kind, window, cx) {
                        if kind == TargetType::Display {
                            last_display_capture = Some(std::time::Instant::now());
                            this.display_thumbnail_task = Some(task);
                        } else {
                            this.window_thumbnail_task = Some(task);
                        }
                    }
                }) else {
                    return;
                };

                cx.background_executor()
                    .timer(target_thumbnails::LIST_STALE_TIME)
                    .await;
            }
        }));
    }

    fn start_input_poll(&mut self, menu: DeviceMenu, window: &mut Window, cx: &mut Context<Self>) {
        let generation = self.input_poll_generation;
        let gate = self.input_enumeration_gate.clone();
        self.input_poll_task = Some(cx.spawn_in(window, async move |this, cx| {
            loop {
                let Ok(allowed) = this.update_in(cx, |this, window, cx| {
                    let current_menu = match this.panel {
                        Some(Panel::Device(menu)) => Some(menu),
                        _ => None,
                    };
                    input_refresh_scan_allowed(
                        input_refresh_is_current(
                            this.input_poll_generation,
                            current_menu,
                            generation,
                            menu,
                        ),
                        !this.device_restore_suspended && this.target_prewarm_allowed(window, cx),
                    )
                    .map(|allowed| {
                        let cameras = if allowed && menu == DeviceMenu::Camera {
                            this.devices.cameras.clone()
                        } else {
                            Vec::new()
                        };
                        (allowed, cameras)
                    })
                }) else {
                    return;
                };
                let Some((allowed, previous_cameras)) = allowed else {
                    return;
                };
                if allowed && let Some(permit) = gate.try_enter() {
                    let snapshot = cx
                        .background_executor()
                        .spawn(async move {
                            let _permit = permit;
                            match menu {
                                DeviceMenu::Camera => {
                                    devices::InputSnapshot::cameras(&previous_cameras)
                                }
                                DeviceMenu::Microphone => devices::InputSnapshot::microphones(),
                            }
                        })
                        .await;
                    let Ok(current) = this.update_in(cx, |this, window, cx| {
                        let current_menu = match this.panel {
                            Some(Panel::Device(menu)) => Some(menu),
                            _ => None,
                        };
                        if !input_refresh_is_current(
                            this.input_poll_generation,
                            current_menu,
                            generation,
                            menu,
                        ) {
                            return false;
                        }
                        if !this.device_restore_suspended
                            && this.target_prewarm_allowed(window, cx)
                            && snapshot.install(&mut this.devices)
                        {
                            cx.notify();
                        }
                        true
                    }) else {
                        return;
                    };
                    if !current {
                        return;
                    }
                }
                cx.background_executor()
                    .timer(std::time::Duration::from_secs(2))
                    .await;
            }
        }));
    }

    fn apply_display_events(
        &mut self,
        batch: Vec<target_thumbnails::DisplayEvent>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        for event in batch {
            match event {
                target_thumbnails::DisplayEvent::Listed(list) => {
                    self.install_displays(list, window, cx);
                }
                target_thumbnails::DisplayEvent::Captured(id, image) => {
                    if let Some(old) = self.thumbnails.insert_display(&id, image) {
                        let _ = window.drop_image(old);
                    }
                }
            }
        }
        cx.notify();
        // The main window is not necessarily active while a sweep lands (the
        // prewarm runs at launch, behind whatever the user was doing), and an
        // inactive window only repaints when asked.
        window.refresh();
    }

    fn apply_window_events(
        &mut self,
        batch: Vec<target_thumbnails::WindowEvent>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        for event in batch {
            match event {
                target_thumbnails::WindowEvent::Listed(list) => {
                    self.install_windows(list, window, cx);
                }
                target_thumbnails::WindowEvent::Captured {
                    id,
                    image,
                    app_icon,
                    entry,
                } => {
                    let icon = app_icon.map(|bytes| {
                        std::sync::Arc::new(gpui::Image::from_bytes(gpui::ImageFormat::Png, bytes))
                    });
                    if let Some(old) = self.thumbnails.insert_window(&id, image, icon, entry) {
                        let _ = window.drop_image(old);
                    }
                }
            }
        }
        cx.notify();
        window.refresh();
    }

    pub fn start_recovery_check(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.scan_incomplete_recordings(window, cx, std::time::Duration::from_secs(2));
    }

    fn scan_incomplete_recordings(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        delay: std::time::Duration,
    ) {
        self.recovery_scan_task = Some(cx.spawn_in(window, async move |this, cx| {
            if !delay.is_zero() {
                cx.background_executor().timer(delay).await;
            }

            let idle = this
                .update_in(cx, |this, _, cx| this.session.read(cx).phase == Phase::Idle)
                .unwrap_or(false);
            if !idle {
                return;
            }

            let incomplete = cx
                .background_executor()
                .spawn(async { library::find_incomplete_recordings() })
                .await;

            this.update_in(cx, |this, window, cx| {
                if this.session.read(cx).phase != Phase::Idle {
                    return;
                }
                this.incomplete_recording = incomplete.into_iter().next();
                if this.incomplete_recording.is_none() {
                    this.recovery_error = None;
                } else if std::env::var("CAP_GPUI_AUTO_RECOVER").is_ok_and(|value| value == "1") {
                    // The crash harness presses Recover, since unprivileged
                    // synthetic clicks are dropped (as for every
                    // `CAP_GPUI_AUTO_*` hook).
                    this.process_incomplete_recording(true, window, cx);
                }
                cx.notify();
                window.refresh();
            })
            .ok();
        }));
    }

    fn process_incomplete_recording(
        &mut self,
        recover: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.recovery_pending || self.session.read(cx).phase != Phase::Idle {
            return;
        }
        let Some(recording) = self.incomplete_recording.clone() else {
            return;
        };
        if !recover
            && !crate::platform::confirm_dialog(
                "Cap",
                tr::main::delete_recording_confirm(),
                tr::common::yes(),
                tr::common::no(),
                false,
            )
        {
            return;
        }

        self.recovery_pending = true;
        self.recovery_error = None;
        cx.notify();
        window.refresh();

        self.recovery_action_task = Some(cx.spawn_in(window, async move |this, cx| {
            let result = if recover {
                cx.background_executor().spawn(async move {
                    library::recover_incomplete_recording(&recording.project_path)
                }).await
            } else {
                let path = recording.project_path;
                let Ok(task) = cx.update(|_, cx| {
                    gpui_tokio::Tokio::spawn(
                        cx,
                        crate::upload::queue::delete_recording(path.clone()),
                    )
                }) else {
                    return;
                };
                task.await
                    .unwrap_or_else(|error| Err(error.to_string()))
                    .map(|()| path)
            };

            this.update_in(cx, |this, window, cx| {
                this.recovery_pending = false;
                match result {
                    Ok(project_path) => {
                        this.incomplete_recording = None;
                        this.recovery_error = None;
                        this.refresh_open_library(window, cx);
                        this.scan_incomplete_recordings(window, cx, std::time::Duration::ZERO);
                        if recover {
                            if project_path.join("content/output.mp4").is_file() {
                                library::reveal_path(&project_path.join("content/output.mp4"), cx);
                            } else {
                                cx.defer(move |cx| app_windows::open_editor(project_path, cx));
                            }
                        }
                    }
                    Err(error) => {
                        tracing::warn!(%error, "incomplete recording action failed");
                        this.recovery_error = Some(error);
                    }
                }
                cx.notify();
                window.refresh();
            })
            .ok();
        }));
    }

    /// Re-resolve the palette when the system appearance flips, or when the
    /// native material lands.
    ///
    /// The material is installed from a spawned task after the window exists
    /// (see `main`), so the first frames paint before it is known -- the
    /// global is polled here rather than pushed, and the install notifies the
    /// window once so this runs again.
    fn sync_appearance(&mut self, window: &Window, cx: &gpui::App) {
        self.theme.refresh(window, cx, true);
    }

    /// Bring the target-select overlays in line with the armed target.
    ///
    /// Deferred, because opening a window inside an entity update paints it
    /// synchronously and double-leases this very view. This mirrors
    /// `toggleTargetMode` / `selectDisplayTarget` / `selectWindowTarget` in
    /// the Tauri main window, which each call `openTargetSelectOverlays` (or
    /// `closeTargetSelectOverlays`) right after setting the mode.
    fn sync_overlays(&mut self, cx: &mut Context<Self>) {
        self.cancel_deep_link_start();
        let Some(mode) = self.target else {
            // Toggling the armed tile off is a dismissal ("cancelled" in the
            // Tauri dismissal vocabulary), so it takes the same path Escape
            // does -- which also reveals the main window if the picker hid it.
            cx.defer(app_windows::dismiss_target_overlays);
            return;
        };
        let request = app_windows::OverlayRequest {
            mode,
            recording_mode: self.effective_mode(cx),
            // A display picked from the dropdown narrows the overlays to that
            // display; otherwise every display gets one.
            display: match mode {
                TargetType::Display => self
                    .selected_display
                    .as_ref()
                    .map(|display| display.id.clone()),
                _ => None,
            },
            pinned_window: match mode {
                TargetType::Window => self
                    .selected_window
                    .as_ref()
                    .map(|window| window.id.clone()),
                _ => None,
            },
        };
        let editor_target = self.session.read(cx).editor_recording_target();
        cx.defer(move |cx: &mut gpui::App| {
            if RecordingSession::global(cx)
                .read(cx)
                .editor_recording_target()
                != editor_target
            {
                return;
            }
            if let Some(path) = editor_target {
                app_windows::open_editor_target_overlays(path, request, cx);
            } else {
                app_windows::open_target_overlays(request, cx);
            }
        });
    }

    /// Called when the overlays are dismissed (Escape, their close button) so
    /// the armed tile stops looking armed.
    pub fn clear_target(&mut self, cx: &mut Context<Self>) {
        if self.target.take().is_some() {
            cx.notify();
        }
    }

    /// The recording mode, for whoever is showing it -- the mode select window
    /// seeds its cards from here, the way that route reads `rawOptions.mode`.
    pub fn mode(&self) -> Mode {
        self.mode
    }

    pub(crate) fn effective_mode(&self, cx: &gpui::App) -> Mode {
        effective_recording_mode(
            self.mode(),
            self.session.read(cx).editor_recording_target().is_some(),
        )
    }

    /// `handleModeChange`: `setOptions({ mode })` plus
    /// `commands.setRecordingMode(mode)`. The pill, the info panel and the mode
    /// select window all land here, so there is one place a mode change
    /// happens.
    pub fn set_mode(&mut self, mode: Mode, cx: &mut Context<Self>) {
        self.clear_mode_hover();
        cx.notify();
        if self.mode == mode
            || self.is_preparing_recording()
            || self.session.read(cx).editor_recording_target().is_some()
        {
            return;
        }
        self.mode = mode;
        // Open overlays label their start button with the mode.
        let select = crate::target_overlay::TargetSelect::global(cx);
        select.update(cx, |select, cx| select.set_recording_mode(mode, cx));
        tracing::info!(
            mode = mode.panel_title(),
            target_select = select.read(cx).recording_mode.panel_title(),
            "recording mode changed"
        );
        // `commands.setRecordingMode(mode)`, which is
        // `RecordingSettingsStore::set_mode` plus the two tray refreshes it
        // triggers (`handle_mode_selection`). Every mode affordance -- the
        // pill, the info panel, the mode select window and the tray's own
        // mode picker -- reaches this method, so this is the one place
        // the setting is written.
        // Written inline, like every other store write in this app (and like
        // `RecordingSettingsStore::set_mode` itself): a background write would
        // land *after* the tray re-read the setting, and the tick would stay on
        // the old mode.
        crate::store::set_recording_mode_slug(mode.slug());
        cx.defer(move |cx: &mut gpui::App| crate::tray::mode_changed(mode, cx));
        cx.notify();
    }

    /// The concrete capture target the current UI state describes, or `None`
    /// when starting makes no sense yet (no target mode, Window mode with no
    /// window picked, Area mode -- which is drawn on the overlay).
    pub(crate) fn armed_target(&self) -> Option<ScreenCaptureTarget> {
        match self.target? {
            TargetType::Display => {
                // The Tauri flow preselects the primary display the moment the
                // Display tile is clicked; mirror that when nothing specific
                // was picked from the dropdown.
                let id = self
                    .selected_display
                    .as_ref()
                    .map(|display| display.id.clone())
                    .unwrap_or_else(|| scap_targets::Display::primary().id());
                Some(ScreenCaptureTarget::Display { id })
            }
            TargetType::Window => {
                self.selected_window
                    .as_ref()
                    .map(|window| ScreenCaptureTarget::Window {
                        id: window.id.clone(),
                    })
            }
            TargetType::Area => None,
            TargetType::CameraOnly => Some(ScreenCaptureTarget::CameraOnly),
        }
    }

    /// The recording mode the Mode pill maps to, `None` for Screenshot (that
    /// path does not go through the recording actors at all).
    fn recording_mode(&self, cx: &gpui::App) -> Option<recording::RecordingMode> {
        match self.effective_mode(cx) {
            Mode::Instant => Some(recording::RecordingMode::Instant),
            Mode::Studio => Some(recording::RecordingMode::Studio),
            Mode::Screenshot => None,
        }
    }

    /// Dev-only end-to-end driver (`CAP_GPUI_AUTO_RECORD=studio:5`): synthetic
    /// clicks are dropped without Accessibility permission, so the automated
    /// check arms a target and drives start/stop through the same methods the
    /// buttons call.
    ///
    /// `CAP_GPUI_AUTO_OVERLAY=display|window|area` routes the start through the
    /// target-select overlay instead of straight off this window --
    /// `CAP_GPUI_AUTO_AREA=x,y,w,h` seeds the crop the drag would have drawn,
    /// and the window variant pins the first enumerated window the way picking
    /// one from the dropdown does.
    pub fn auto_record(
        &mut self,
        mode: Mode,
        record_secs: u64,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let overlay = auto_overlay_kind();
        let area = auto_area_rect();

        self.mode = mode;
        self.target = Some(overlay.unwrap_or(TargetType::Display));
        self.system_audio =
            std::env::var("CAP_GPUI_AUTO_SYSTEM_AUDIO").is_ok_and(|value| value == "1");
        cx.notify();

        // `CAP_GPUI_AUTO_PAUSE=1`: wiggle pause/resume in the middle third, so
        // ffprobe duration < wall time proves the pause reached the engine.
        let pause_wiggle = std::env::var("CAP_GPUI_AUTO_PAUSE").is_ok_and(|v| v == "1");

        let skip_mic = std::env::var("CAP_GPUI_AUTO_NO_MIC").is_ok_and(|v| v == "1")
            || mode == Mode::Screenshot;
        if skip_mic {
            self.set_microphone_selection(None, cx);
        }
        let required_window = auto_window_title();
        // Pause and stop go to the session: on Linux the main window is not
        // there while a window or Instant recording runs.
        let session = self.session.clone();

        cx.spawn_in(window, async move |this, cx| {
            // Give enumeration and the first paint a moment; the recorder
            // itself does not depend on it, but the screenshots should show
            // real device rows.
            cx.background_executor()
                .timer(std::time::Duration::from_secs(2))
                .await;
            if !skip_mic {
                // Exercise the microphone path too: record with the system default
                // input. "First in the list" is wrong on a machine with a
                // Continuity iPhone mic -- it enumerates but dies on stream start,
                // which kills the whole recording.
                let default_mic = cx
                    .background_executor()
                    .spawn(async {
                        cap_recording::feeds::microphone::MicrophoneFeed::default_device()
                            .map(|(name, _, _)| name)
                    })
                    .await;
                if this
                    .update_in(cx, |this, _window, cx| {
                        let microphone = default_mic
                            .and_then(|name| {
                                this.devices
                                    .microphones
                                    .iter()
                                    .find(|mic| mic.name == name)
                                    .cloned()
                            })
                            .or_else(|| this.devices.microphones.first().cloned());
                        if let Some(mic) = &microphone {
                            tracing::info!(mic = %mic.name, "auto-record microphone");
                        }
                        this.set_microphone_selection(microphone, cx);
                    })
                    .is_err()
                {
                    return;
                }
                // Give the app-scoped feed a moment to connect its input; locking
                // an input-less feed would fall back to a per-recording one and
                // dodge the path this harness exists to exercise.
                cx.background_executor()
                    .timer(std::time::Duration::from_millis(1500))
                    .await;
            }

            if overlay == Some(TargetType::Window)
                && let Some(wanted) = required_window.clone()
            {
                let mut found = false;
                for _ in 0..20 {
                    found = this
                        .update_in(cx, |this, _window, cx| {
                            this.arm_overlay(TargetType::Window, cx);
                            this.selected_window
                                .as_ref()
                                .is_some_and(|window| window_matches(window, &wanted))
                        })
                        .unwrap_or(false);
                    if found {
                        break;
                    }
                    cx.background_executor()
                        .timer(std::time::Duration::from_millis(250))
                        .await;
                }
                if !found {
                    tracing::error!(
                        title = %wanted,
                        "CAP_GPUI_AUTO_WINDOW: no matching window"
                    );
                    return;
                }
            }

            let started = match overlay {
                None => {
                    tracing::info!("auto-record start requested");
                    this.update_in(cx, |this, window, cx| this.start_recording(window, cx))
                        .is_ok()
                }
                Some(kind) => {
                    // The overlay route: arm the mode (which opens the
                    // overlays), let them come up, seed what a drag or a hover
                    // would have produced, then press their Start button.
                    let wanted = required_window.clone();
                    let armed = this
                        .update_in(cx, |this, _window, cx| {
                            this.arm_overlay(kind, cx);
                            kind != TargetType::Window
                                || wanted.as_ref().is_none_or(|wanted| {
                                    this.selected_window
                                        .as_ref()
                                        .is_some_and(|selected| window_matches(selected, wanted))
                                })
                        })
                        .unwrap_or(false);
                    if !armed {
                        tracing::error!("auto-record selected window is no longer available");
                        return;
                    }
                    cx.background_executor()
                        .timer(std::time::Duration::from_millis(1500))
                        .await;
                    if let Some(area) = area {
                        tracing::info!(?area, "seeding area selection");
                        cx.update(|_, cx| app_windows::seed_area_selection(None, area, cx))
                            .ok();
                        cx.background_executor()
                            .timer(std::time::Duration::from_millis(300))
                            .await;
                    }
                    tracing::info!("auto-record start requested");
                    cx.update(|_, cx| app_windows::start_from_overlay(None, cx))
                        .unwrap_or(false)
                }
            };
            if !started {
                tracing::error!("auto-record could not start");
                return;
            }

            let third = std::time::Duration::from_secs(record_secs.div_ceil(3));
            let toggle = |cx: &gpui::AsyncApp| {
                cx.update(|cx| session.update(cx, |session, cx| session.toggle_pause(cx)));
            };
            if pause_wiggle {
                cx.background_executor().timer(third).await;
                toggle(cx);
                cx.background_executor().timer(third).await;
                toggle(cx);
                cx.background_executor().timer(third).await;
            } else {
                cx.background_executor()
                    .timer(std::time::Duration::from_secs(record_secs))
                    .await;
            }
            tracing::info!("auto-record stop requested");
            let app: &gpui::AsyncApp = cx;
            app.update(|cx| session.update(cx, |session, cx| session.stop(cx)));
        })
        .detach();
    }

    /// Arm a target mode the way clicking its tile does, picking a concrete
    /// window for the window variant since the harness cannot hover one.
    ///
    /// Also the tray's Record Display/Window/Area path: those items are
    /// `crate::open_target_picker(&app, RecordingTargetMode::*)` over there,
    /// which is the same "set the target mode, open the overlays" pair.
    pub fn arm_overlay(&mut self, kind: TargetType, cx: &mut Context<Self>) {
        if kind == TargetType::Window {
            self.selected_window = match auto_window_title() {
                Some(title) => self
                    .devices
                    .windows
                    .iter()
                    .find(|window| window_matches(window, &title))
                    .cloned(),
                None => self.devices.windows.first().cloned(),
            };
            if let Some(window) = &self.selected_window {
                tracing::info!(app = %window.app, title = %window.label, "auto window target");
            }
        }
        self.target = Some(kind);
        self.sync_overlays(cx);
        cx.notify();
    }

    /// `CAP_GPUI_AUTO_OVERLAY` on its own (no `CAP_GPUI_AUTO_RECORD`): open the
    /// overlays and leave them up, which is how the screenshots are taken.
    pub fn auto_open_overlay(
        &mut self,
        kind: TargetType,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let crop = auto_area_rect();
        cx.spawn_in(window, async move |this, cx| {
            // Enumeration first: the window variant pins a real window.
            cx.background_executor()
                .timer(std::time::Duration::from_secs(2))
                .await;
            if this
                .update_in(cx, |this, _window, cx| this.arm_overlay(kind, cx))
                .is_err()
            {
                return;
            }
            if let Some(crop) = crop {
                cx.background_executor()
                    .timer(std::time::Duration::from_millis(1500))
                    .await;
                cx.update(|_, cx| app_windows::seed_area_selection(None, crop, cx))
                    .ok();
            }
        })
        .detach();
    }

    /// Start with the target the UI state describes. The overlays own the real
    /// start affordance now; this is the harness path
    /// (`CAP_GPUI_AUTO_RECORD` without `CAP_GPUI_AUTO_OVERLAY`).
    pub fn start_recording(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let Some(target) = self.armed_target() else {
            return;
        };
        if self.effective_mode(cx) == Mode::Screenshot {
            // Screenshot mode never reaches the recording actors -- the
            // target goes straight to the capture path (`take_screenshot`).
            cx.defer(move |cx: &mut gpui::App| crate::screenshot::take_screenshot(target, cx));
            return;
        }
        self.start_recording_with_target(target, Vec::new(), window, cx);
    }

    /// Collect the UI state into a start config and hand it to the
    /// orchestrator, which opens the controls bar, hides this window, and
    /// starts the engine. Deferred because the orchestrator updates this very
    /// window (hide), which would re-enter the update we are inside of.
    ///
    /// The target comes in rather than being read off this view: it is the
    /// overlay that knows which display was clicked, which window is under the
    /// cursor, or what area was drawn.
    pub fn start_recording_with_target(
        &mut self,
        target: ScreenCaptureTarget,
        excluded_windows: Vec<scap_targets::WindowId>,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.session.read(cx).phase != Phase::Idle || self.checking_storage {
            return;
        }
        // An armed editor recording target forces Studio before this window's
        // own mode is consulted -- the front half of the same rule
        // `start_recording` applies (`src-tauri/src/recording.rs:1492-1494`),
        // needed here because a Screenshot-mode pill maps to no recording
        // mode at all and would otherwise bail out of an editor-flow start.
        let editor_flow = self.session.read(cx).editor_recording_target().is_some();
        let mode = if editor_flow {
            recording::RecordingMode::Studio
        } else {
            match self.recording_mode(cx) {
                Some(mode) => mode,
                None => return,
            }
        };
        if mode == recording::RecordingMode::Instant && !crate::store::auth_snapshot().signed_in() {
            let kind = match &target {
                ScreenCaptureTarget::Display { .. } => TargetType::Display,
                ScreenCaptureTarget::Window { .. } => TargetType::Window,
                ScreenCaptureTarget::Area { .. } => TargetType::Area,
                ScreenCaptureTarget::CameraOnly => TargetType::CameraOnly,
            };
            self.show_sign_in_prompt(kind, cx);
            cx.defer(app_windows::show_main_window);
            return;
        }
        if matches!(target, ScreenCaptureTarget::CameraOnly) && self.camera.is_none() {
            self.session.update(cx, |session, cx| {
                session.error = Some(tr::main::error::camera_only_needs_camera().into());
                cx.notify();
            });
            if editor_flow {
                // No session phase transition is coming; restore the hidden
                // editor the way a cancelled picker would.
                cx.defer(app_windows::abort_editor_recording_flow);
            }
            return;
        }

        let (camera_feed, mic_feed, mic_meter, input_readiness, device_settings) = {
            let feeds = Feeds::global(cx);
            feeds.update(cx, |feeds, cx| feeds.resume_camera_preview(cx));
            let feeds = feeds.read(cx);
            (
                feeds.camera_actor(),
                feeds.mic_actor(),
                feeds.mic_meter(),
                feeds.input_readiness(),
                feeds.requested_device_settings(),
            )
        };
        let config = recording::StartConfig {
            mode,
            target,
            device_settings,
            input_readiness,
            microphone: self.microphone.as_ref().map(|mic| mic.name.clone()),
            camera: self.camera_id.clone(),
            system_audio: self.system_audio,
            excluded_windows,
            camera_feed,
            mic_feed,
            mic_meter,
            #[cfg(target_os = "linux")]
            linux_instant_camera: None,
            start_gate: None,
        };

        // `RecordingSettingsStore.target`: what the Start shortcuts fall back
        // to when nothing is selected here.
        if let Ok(value) = serde_json::to_value(&config.target)
            && !crate::store::set_store_setting(crate::store::RECORDING_SETTINGS, "target", value)
        {
            tracing::warn!("saving the last recording target failed");
        }

        self.start_recording_config(config, cx);
    }

    pub(crate) fn start_recording_config(
        &mut self,
        config: recording::StartConfig,
        cx: &mut Context<Self>,
    ) {
        if self.enumerating
            || self.device_format_pending.is_some()
            || self.pending_device_restore.camera_id.is_some()
            || self.pending_device_restore.microphone_name.is_some()
        {
            self.session.update(cx, |session, cx| {
                session.error = Some(tr::main::error::devices_not_ready().into());
                cx.notify();
            });
            return;
        }
        let Ok(permit) = self.prepare_deep_link_start(cx) else {
            return;
        };
        let microphone_available = config.microphone.as_ref().is_some_and(|name| {
            self.devices
                .microphones
                .iter()
                .any(|device| &device.name == name)
        });
        if !microphone_available && crate::store::GeneralSettings::load().confirm_without_microphone
        {
            self.microphone_warning = Some(MicrophoneWarning {
                config,
                permit,
                dont_show_again: false,
                error: None,
            });
            cx.defer(app_windows::show_main_window);
            cx.notify();
            return;
        }
        self.check_storage_before_start(config, false, permit, cx);
    }

    pub(crate) fn prepare_deep_link_start(
        &mut self,
        cx: &mut Context<Self>,
    ) -> Result<RecordingStartPermit, String> {
        if self.device_format_pending.is_some() {
            return Err("Wait for the device format change to finish before recording".into());
        }
        let permit = RecordingStartPermit::prepare(
            self.session.read(cx).phase,
            app_windows::clean_capture_owned(cx),
            self.checking_storage,
        )?;
        self.pending_device_restore = crate::store::RecordingInputSettings::default();
        self.checking_storage = true;
        self.deep_link_start = Some(permit.clone());
        Ok(permit)
    }

    pub(crate) fn cancel_deep_link_start(&mut self) {
        self.microphone_warning = None;
        if RecordingStartPermit::cancel_current(&mut self.deep_link_start) {
            self.checking_storage = false;
        }
    }

    pub(crate) fn is_preparing_recording(&self) -> bool {
        self.checking_storage
            || self.deep_link_start.is_some()
            || self.device_format_pending.is_some()
    }

    fn device_changes_allowed(&self, cx: &gpui::App) -> bool {
        self.session.read(cx).phase == Phase::Idle
            && !self.is_preparing_recording()
            && !self.device_restore_suspended
            && !app_windows::clean_capture_owned(cx)
    }

    pub(crate) fn finish_deep_link_start(&mut self, permit: &RecordingStartPermit) {
        if self
            .deep_link_start
            .as_ref()
            .is_some_and(|current| current.same(permit))
        {
            self.cancel_deep_link_start();
        }
    }

    pub(crate) fn start_recording_config_with_permit(
        &mut self,
        config: recording::StartConfig,
        permit: RecordingStartPermit,
        cx: &mut Context<Self>,
    ) {
        self.check_storage_before_start(config, false, permit, cx);
    }

    fn recording_start_is_current(&self, permit: &RecordingStartPermit, cx: &gpui::App) -> bool {
        self.session.read(cx).phase == Phase::Idle
            && self
                .deep_link_start
                .as_ref()
                .is_some_and(|current| current.same(permit))
            && permit.is_current(cx)
    }

    fn finish_recording_start(&mut self, permit: &RecordingStartPermit) {
        self.finish_deep_link_start(permit);
    }

    fn confirm_microphone_warning(&mut self, cx: &mut Context<Self>) {
        let Some(mut warning) = self.microphone_warning.take() else {
            return;
        };
        if !self.recording_start_is_current(&warning.permit, cx) {
            self.finish_recording_start(&warning.permit);
            cx.notify();
            return;
        }
        if warning.dont_show_again
            && !crate::store::set_store_setting(
                crate::store::RECORDING_START_SAFETY,
                "confirmBeforeRecordingWithoutMicrophone",
                serde_json::Value::Bool(false),
            )
        {
            warning.error = Some(tr::main::mic_warning::save_failed().into());
            self.microphone_warning = Some(warning);
            cx.notify();
            return;
        }
        warning.config.microphone = None;
        warning.config.mic_feed = None;
        self.check_storage_before_start(warning.config, false, warning.permit, cx);
        cx.notify();
    }

    fn dismiss_microphone_warning(&mut self, cx: &mut Context<Self>) {
        self.cancel_deep_link_start();
        cx.defer(app_windows::restore_arranged_windows_if_idle);
        cx.notify();
    }

    fn check_storage_before_start(
        &mut self,
        mut config: recording::StartConfig,
        acknowledged: bool,
        permit: RecordingStartPermit,
        cx: &mut Context<Self>,
    ) {
        if !self.recording_start_is_current(&permit, cx) {
            self.finish_recording_start(&permit);
            return;
        }
        if matches!(config.target, ScreenCaptureTarget::CameraOnly) {
            config.system_audio = false;
        }
        self.checking_storage = true;
        let main = cx.global::<app_windows::AppWindows>().main;
        cx.spawn(async move |this, cx| {
            let result = cx
                .background_executor()
                .spawn(async { recording::available_recording_storage() })
                .await;
            if !this
                .update(cx, |this, cx| {
                    if !this.recording_start_is_current(&permit, cx) {
                        this.finish_recording_start(&permit);
                        return false;
                    }
                    true
                })
                .unwrap_or(false)
            {
                return;
            }
            let storage = match result {
                Ok(storage) => storage,
                Err(error) => {
                    this.update(cx, |this, cx| {
                        if !this.recording_start_is_current(&permit, cx) {
                            this.finish_recording_start(&permit);
                            return;
                        }
                        this.finish_recording_start(&permit);
                        cx.defer(app_windows::restore_arranged_windows_if_idle);
                        this.session.update(cx, |session, cx| {
                            session.error = Some(tr::main::storage::check_failed(&error));
                            cx.notify();
                        });
                        cx.defer(app_windows::show_main_window);
                        if this.session.read(cx).editor_recording_target().is_some() {
                            cx.defer(app_windows::abort_editor_recording_flow);
                        }
                    })
                    .ok();
                    return;
                }
            };
            let can_start = storage.status() != cap_utils::disk_space::DiskSpaceStatus::Exhausted;
            if storage.status() == cap_utils::disk_space::DiskSpaceStatus::Ok
                || acknowledged && can_start
            {
                this.update(cx, |this, cx| {
                    if !this.recording_start_is_current(&permit, cx) {
                        this.finish_recording_start(&permit);
                        return;
                    }
                    cx.defer(move |cx| {
                        let allowed = main
                            .update(cx, |this, _, cx| {
                                let allowed = this.recording_start_is_current(&permit, cx);
                                this.finish_deep_link_start(&permit);
                                allowed
                            })
                            .unwrap_or(false);
                        if !allowed {
                            return;
                        }
                        app_windows::begin_recording(config, cx);
                    });
                })
                .ok();
                return;
            }
            let available = format!("{:.2}", storage.available_bytes as f64 / 1_073_741_824.0);
            let detail = if can_start {
                tr::main::storage::can_start(&available)
            } else {
                tr::main::storage::blocked(&available)
            };
            let buttons = if can_start {
                vec![
                    gpui::PromptButton::ok(tr::main::storage::record_anyway()),
                    gpui::PromptButton::cancel(tr::common::go_back()),
                ]
            } else {
                vec![gpui::PromptButton::cancel(tr::common::ok())]
            };
            let receiver = cx.update(|cx| {
                if RecordingSession::global(cx).read(cx).phase != Phase::Idle
                    || !permit.is_current(cx)
                {
                    return Err(anyhow::anyhow!(
                        "Recording preparation is no longer current."
                    ));
                }
                app_windows::show_main_window(cx);
                cx.activate(true);
                main.update(cx, |_, window, cx| {
                    window.prompt(
                        gpui::PromptLevel::Warning,
                        tr::main::storage::title(),
                        Some(&detail),
                        &buttons,
                        cx,
                    )
                })
            });
            let confirmed = match receiver {
                Ok(receiver) => receiver.await == Ok(0) && can_start,
                Err(_) => false,
            };
            this.update(cx, |this, cx| {
                if !this.recording_start_is_current(&permit, cx) {
                    this.finish_recording_start(&permit);
                    return;
                }
                if confirmed {
                    this.check_storage_before_start(config, true, permit, cx);
                } else {
                    this.finish_recording_start(&permit);
                    cx.defer(app_windows::restore_arranged_windows_if_idle);
                    if this.session.read(cx).editor_recording_target().is_some() {
                        cx.defer(app_windows::abort_editor_recording_flow);
                    }
                }
                cx.notify();
            })
            .ok();
        })
        .detach();
    }

    /// `await commands.focusWindow(target.id)` at the end of
    /// `selectWindowTarget` (`new-main/index.tsx:2431-2450`): picking a
    /// *window* from the list brings that window's application forward, so the
    /// thing about to be recorded is the thing on screen behind the overlay.
    /// `selectDisplayTarget` (`:2416-2429`) has no such call, and neither has a
    /// click on the overlay itself -- so this is wired to the window rows only.
    ///
    /// Deferred (so the overlays this selection opens go up first, the order
    /// the source awaits its calls in) and then run on the background executor:
    /// the Tauri command is an async command, and an AppKit activation inside
    /// this update would re-enter gpui's window callbacks.
    fn focus_selected_window(&self, cx: &mut Context<Self>) {
        let Some(window) = self.selected_window.as_ref() else {
            return;
        };
        let id = window.id.clone();
        cx.defer(move |cx: &mut gpui::App| {
            cx.background_executor()
                .spawn(async move {
                    crate::platform::focus_capture_target_window(&id);
                })
                .detach();
        });
    }

    // -- The editor record modal's device seams ------------------------------
    //
    // The Tauri editor modal reads and writes the *same* recording-options
    // store the main window uses (`useRecordingOptions` + `createCameraMutation`
    // + `setMicInput`, `ClipsSidebar.tsx:1084-1123`); in this app that store is
    // this window's state, so the modal reaches it through these.

    /// The camera the next recording will use, for whoever else renders it.
    pub fn camera_selection(&self) -> Option<&CameraOption> {
        self.camera.as_ref()
    }

    /// The microphone the next recording will use.
    pub fn microphone_selection(&self) -> Option<&MicrophoneOption> {
        self.microphone.as_ref()
    }

    pub(crate) fn device_snapshot(&self) -> &DeviceSnapshot {
        &self.devices
    }

    pub(crate) fn is_enumerating_devices(&self) -> bool {
        self.enumerating
    }

    pub(crate) fn suspend_device_restore(&mut self) {
        self.clear_mode_hover();
        self.device_restore_suspended = true;
        self.device_format_pending = None;
    }

    pub(crate) fn resume_device_restore(&mut self, cx: &mut Context<Self>) {
        self.device_restore_suspended = false;
        self.restore_recording_inputs(cx);
        if let Some(microphone) = &self.microphone {
            let name = microphone.name.clone();
            Feeds::global(cx).update(cx, |feeds, cx| feeds.set_microphone(Some(name), cx));
        }
    }

    fn restore_recording_inputs(&mut self, cx: &mut Context<Self>) {
        let Some(settings) = take_pending_recording_inputs(
            &mut self.pending_device_restore,
            self.enumerating,
            self.device_restore_suspended,
        ) else {
            return;
        };
        if let Some(id) = settings.camera_id {
            let camera = remembered_camera(&id, &self.devices.cameras);
            self.apply_camera_selection(Some(camera), Some(id), cx);
        }
        if let Some(name) = settings.microphone_name {
            let microphone = remembered_microphone(&name, &self.devices.microphones);
            self.apply_microphone_selection(Some(microphone), cx);
        }
    }

    /// Select (or clear) the camera -- the same wiring as the camera panel's
    /// rows: this window's state plus the app-scoped feed, which opens or
    /// closes the preview bubble.
    fn access_pill(&self, permission: OSPermission, cx: &gpui::App) -> Option<PillState> {
        let access = MediaAccess::global(cx);
        let access = access.read(cx);
        if access.allowed(permission) {
            return None;
        }
        Some(PillState::Access(if access.requesting(permission) {
            tr::main::device::pill::asking()
        } else if access.status(permission) == crate::permissions::OSPermissionStatus::Denied {
            tr::main::device::pill::allow_in_settings()
        } else {
            tr::main::device::pill::allow()
        }))
    }

    fn ensure_media_access(&mut self, permission: OSPermission, cx: &mut Context<Self>) {
        let access = MediaAccess::global(cx);
        if !access.read(cx).allowed(permission) {
            access.update(cx, |access, cx| access.request(permission, cx));
        }
    }

    pub fn set_camera_selection(&mut self, camera: Option<CameraOption>, cx: &mut Context<Self>) {
        let id = camera.as_ref().map(|camera| {
            camera
                .model_id
                .clone()
                .map(recording::DeviceOrModelID::ModelID)
                .unwrap_or_else(|| recording::DeviceOrModelID::DeviceID(camera.device_id.clone()))
        });
        self.pending_device_restore.camera_id = None;
        if !crate::store::set_recording_camera_id(id.as_ref()) {
            tracing::warn!("Could not save the selected camera");
        }
        self.apply_camera_selection(camera, id, cx);
    }

    fn apply_camera_selection(
        &mut self,
        camera: Option<CameraOption>,
        id: Option<recording::DeviceOrModelID>,
        cx: &mut Context<Self>,
    ) {
        let selection =
            camera
                .as_ref()
                .zip(id.as_ref())
                .map(|(camera, id)| feeds::SelectedCamera {
                    id: id.clone(),
                    label: camera.label.clone(),
                    device_id: camera.device_id.clone(),
                    model_id: camera.model_id.clone(),
                });
        self.camera_id = id;
        self.camera = camera;
        Feeds::global(cx).update(cx, |feeds, cx| {
            let selected = selection.is_some();
            feeds.set_camera(selection, cx);
            if selected {
                feeds.resume_camera_preview(cx);
            }
        });
        cx.notify();
    }

    /// Select (or clear) the microphone -- state plus the app-scoped feed, the
    /// mic panel rows' wiring.
    pub fn set_microphone_selection(
        &mut self,
        microphone: Option<MicrophoneOption>,
        cx: &mut Context<Self>,
    ) {
        self.pending_device_restore.microphone_name = None;
        if !crate::store::set_recording_microphone_name(
            microphone.as_ref().map(|mic| mic.name.as_str()),
        ) {
            tracing::warn!("Could not save the selected microphone");
        }
        self.apply_microphone_selection(microphone, cx);
    }

    fn apply_microphone_selection(
        &mut self,
        microphone: Option<MicrophoneOption>,
        cx: &mut Context<Self>,
    ) {
        let label = microphone.as_ref().map(|mic| mic.name.clone());
        self.microphone = microphone;
        Feeds::global(cx).update(cx, |feeds, cx| feeds.set_microphone(label, cx));
        cx.notify();
    }

    /// Toggle system audio capture -- the system-audio row is a plain flag,
    /// persisted as `recording_settings.systemAudio` like the Tauri app did.
    pub fn set_system_audio(&mut self, enabled: bool, cx: &mut Context<Self>) {
        if self.system_audio != enabled {
            self.system_audio = enabled;
            if !crate::store::set_recording_system_audio(enabled) {
                tracing::warn!("Could not save the system audio setting");
            }
            cx.notify();
        }
    }
}

impl Render for MainWindow {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        self.sync_appearance(window, cx);
        if self.keyboard_nav_blocked(cx) {
            ui::keyboard_nav::leave(window, cx);
        }
        self.keyboard_nav = ui::keyboard_nav::active(window, cx);
        let theme = self.theme;
        let meter_active = self.panel.is_none()
            && self.microphone.is_some()
            && self.session.read(cx).phase == Phase::Idle;
        self.microphone_level.update(cx, |meter, cx| {
            meter.configure(
                meter_active,
                theme.blue_9.into(),
                gpui::transparent_black(),
                cx,
            );
        });
        let device_meter_active = self.microphone.is_some()
            && matches!(self.panel, Some(Panel::Device(DeviceMenu::Microphone)));
        self.device_microphone_level.update(cx, |meter, cx| {
            meter.configure(
                device_meter_active,
                theme.blue_500.into(),
                if theme.is_dark() {
                    gpui::hsla(0., 0., 1., 0.1)
                } else {
                    gpui::hsla(0., 0., 0., 0.07)
                },
                cx,
            );
        });

        div()
            .size_full()
            .relative()
            .flex()
            .flex_col()
            .overflow_hidden()
            // Windows keeps its native frame, so DWM supplies the outer corners.
            .when(!cfg!(target_os = "windows"), |shell| shell.rounded(px(16.)))
            // Opaque `bg-gray-1` with no material; a translucent tint over the
            // live `NSGlassEffectView`/`NSVisualEffectView` backdrop with one.
            .bg(theme.shell_bg())
            // Only the vibrancy path draws a shell border -- Liquid Glass sets
            // `border: 0`.
            .when_some(theme.shell_border(), |this, color| {
                this.border_1().border_color(color)
            })
            .font_family(crate::theme::UI_FONT)
            .locale_font()
            // `body { font-weight: 500 }` (`ui-solid/src/main.css:189-192`). The shipping app renders
            // *everything* Medium unless a `font-*` class says otherwise, so
            // Medium -- not Regular -- is the inherited default at every root.
            .font_weight(FontWeight::MEDIUM)
            .text_color(theme.text_primary)
            .child(self.render_header(window, cx))
            .child(self.render_body(cx))
            .children(self.render_mode_hover(cx))
            .when(
                // The controls bar owns the live-recording UI; this overlay is
                // the fallback for when the bar window failed to open.
                {
                    let session = self.session.read(cx);
                    session.phase != Phase::Idle && !session.controls_open
                },
                |this| this.child(self.render_recording_overlay(cx)),
            )
            .when_some(
                self.incomplete_recording
                    .clone()
                    .filter(|_| self.session.read(cx).phase == Phase::Idle),
                |this, recording| this.child(self.render_recovery_toast(recording, cx)),
            )
            .when(app_windows::clean_capture_pending(cx), |this| {
                this.child(self.render_clean_capture_preflight(cx))
            })
            .when(self.microphone_warning.is_some(), |this| {
                this.child(self.render_microphone_warning(cx))
            })
            .when(self.sign_in_prompt.is_some(), |this| {
                this.child(self.render_sign_in_prompt(cx))
            })
            // A failed start has nowhere else to surface: the overlays are
            // gone by then and the bar closed itself.
            .when_some(self.idle_error(cx), |this, error| {
                this.child(self.render_idle_error(error, cx))
            })
            .on_key_down(cx.listener(|this, event: &gpui::KeyDownEvent, _, cx| {
                if event.keystroke.key == "escape" && this.dismiss_main(cx) {
                    cx.stop_propagation();
                }
            }))
            .child(ui::keyboard_nav::pointer_watcher())
    }
}

impl MainWindow {
    fn render_microphone_warning(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let checked = self
            .microphone_warning
            .as_ref()
            .is_some_and(|warning| warning.dont_show_again);
        let error = self
            .microphone_warning
            .as_ref()
            .and_then(|warning| warning.error.clone());
        div()
            .id("microphone-warning")
            .absolute()
            .inset_0()
            .when(cfg!(target_os = "windows"), |overlay| {
                overlay.top(px(HEADER_HEIGHT))
            })
            .rounded(px(16.))
            .occlude()
            .flex()
            .items_center()
            .justify_center()
            .p(px(16.))
            .bg(theme.shell_bg())
            .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| cx.stop_propagation())
            .child(
                div()
                    .w_full()
                    .max_w(px(360.))
                    .rounded(px(12.))
                    .border_1()
                    .border_color(theme.body_border(6))
                    .bg(theme.gray_1)
                    .p(px(16.))
                    .flex()
                    .flex_col()
                    .gap(px(12.))
                    .child(
                        div()
                            .text_size(px(15.))
                            .text_color(theme.gray_12)
                            .child(tr::main::mic_warning::title()),
                    )
                    .child(
                        div()
                            .text_size(px(12.))
                            .line_height(px(18.))
                            .text_color(theme.gray_11)
                            .child(tr::main::mic_warning::body()),
                    )
                    .child(
                        div()
                            .id("microphone-warning-dont-show-again")
                            .flex()
                            .items_center()
                            .gap(px(8.))
                            .py(px(4.))
                            .cursor_pointer()
                            .child(
                                div()
                                    .size(px(16.))
                                    .rounded(px(4.))
                                    .border_1()
                                    .border_color(if checked { theme.blue_9 } else { theme.gray_7 })
                                    .bg(if checked { theme.blue_9 } else { theme.gray_2 })
                                    .when(checked, |this| {
                                        this.child(
                                            svg()
                                                .path("icons/check.svg")
                                                .size(px(14.))
                                                .text_color(rgb(0xffffff)),
                                        )
                                    }),
                            )
                            .child(
                                div()
                                    .text_size(px(12.))
                                    .text_color(theme.gray_11)
                                    .child(tr::main::mic_warning::dont_show_again()),
                            )
                            .on_click(cx.listener(|this, _, _, cx| {
                                if let Some(warning) = &mut this.microphone_warning {
                                    warning.dont_show_again = !warning.dont_show_again;
                                    warning.error = None;
                                }
                                cx.notify();
                            })),
                    )
                    .when_some(error, |this, error| {
                        this.child(
                            div()
                                .text_size(px(11.))
                                .text_color(theme.red_9)
                                .child(error),
                        )
                    })
                    .child(
                        div()
                            .flex()
                            .flex_col()
                            .gap(px(8.))
                            .child(
                                ui::Button::plain(
                                    &theme,
                                    "confirm-without-microphone",
                                    ui::ButtonVariant::Blue,
                                    ui::ButtonSize::Lg,
                                )
                                .label(tr::main::mic_warning::record_without())
                                .on_click(
                                    cx.listener(|this, _, _, cx| {
                                        this.confirm_microphone_warning(cx)
                                    }),
                                ),
                            )
                            .child(
                                ui::Button::plain(
                                    &theme,
                                    "cancel-without-microphone",
                                    ui::ButtonVariant::Gray,
                                    ui::ButtonSize::Lg,
                                )
                                .label(tr::common::go_back())
                                .on_click(
                                    cx.listener(|this, _, _, cx| {
                                        this.dismiss_microphone_warning(cx)
                                    }),
                                ),
                            ),
                    ),
            )
    }

    pub fn show_sign_in_prompt(&mut self, kind: TargetType, cx: &mut Context<Self>) {
        self.clear_mode_hover();
        self.sign_in_prompt = Some(SignInPrompt { kind, error: None });
        cx.notify();
    }

    fn dismiss_sign_in_prompt(&mut self, cx: &mut Context<Self>) {
        if self.sign_in_prompt.take().is_none() {
            return;
        }
        // Only the attempt this prompt started; one from Settings or an
        // editor keeps running.
        let sign_in = SignIn::global(cx);
        if matches!(
            sign_in.read(cx).pending_origin(),
            Some(SignInOrigin::Overlay(_))
        ) {
            sign_in.update(cx, |sign_in, cx| sign_in.cancel(cx));
        }
        cx.notify();
    }

    /// The dialog frame the sign-in prompt and the error card share: a dimmed
    /// backdrop over the home view (clicking it dismisses), and a raised card
    /// with an icon badge, a title and a close button.
    fn render_modal(
        &self,
        id: &'static str,
        badge: (&'static str, gpui::Rgba, gpui::Rgba),
        title: &'static str,
        on_dismiss: fn(&mut Self, &mut Context<Self>),
        body: gpui::AnyElement,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let theme = self.theme;
        let (icon, badge_bg, badge_fg) = badge;
        div()
            .id(id)
            .absolute()
            .inset_0()
            .when(cfg!(target_os = "windows"), |overlay| {
                overlay.top(px(HEADER_HEIGHT))
            })
            .when(!cfg!(target_os = "windows"), |overlay| {
                overlay.rounded(px(16.))
            })
            .occlude()
            .flex()
            .items_center()
            .justify_center()
            .p(px(14.))
            .bg(gpui::hsla(
                0.,
                0.,
                0.,
                if theme.is_dark() { 0.55 } else { 0.32 },
            ))
            .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| cx.stop_propagation())
            .on_click(cx.listener(move |this, _, _, cx| on_dismiss(this, cx)))
            .child(
                div()
                    .id("modal-card")
                    .relative()
                    .w_full()
                    .rounded(px(14.))
                    .border_1()
                    .border_color(theme.body_border(6))
                    .bg(theme.gray_1)
                    .shadow_lg()
                    .p(px(16.))
                    .flex()
                    .flex_col()
                    .gap(px(14.))
                    .on_click(|_, _, cx| cx.stop_propagation())
                    .child(
                        div()
                            .flex()
                            .items_center()
                            .gap(px(10.))
                            .pr(px(24.))
                            .child(
                                div()
                                    .flex()
                                    .flex_shrink_0()
                                    .items_center()
                                    .justify_center()
                                    .size(px(36.))
                                    .rounded_full()
                                    .bg(badge_bg)
                                    .child(svg().path(icon).size(px(18.)).text_color(badge_fg)),
                            )
                            .child(
                                div()
                                    .text_size(px(15.))
                                    .line_height(px(20.))
                                    .font_weight(FontWeight::SEMIBOLD)
                                    .text_color(theme.gray_12)
                                    .child(title),
                            ),
                    )
                    .child(body)
                    .child(
                        div()
                            .id("modal-close")
                            .absolute()
                            .top(px(10.))
                            .right(px(10.))
                            .flex()
                            .items_center()
                            .justify_center()
                            .size(px(24.))
                            .rounded_full()
                            .cursor_pointer()
                            .hover(|style| style.bg(theme.gray_3))
                            .tooltip(move |_, cx| {
                                ui::Tooltip::new(&theme, tr::common::close()).view(cx)
                            })
                            .child(
                                svg()
                                    .path("icons/x.svg")
                                    .size(px(12.))
                                    .text_color(theme.gray_10),
                            )
                            .on_click(cx.listener(move |this, _, _, cx| {
                                cx.stop_propagation();
                                on_dismiss(this, cx);
                            })),
                    ),
            )
    }

    fn render_sign_in_prompt(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let pending = SignIn::global(cx).read(cx).is_pending();
        let error = self
            .sign_in_prompt
            .as_ref()
            .and_then(|prompt| prompt.error.clone())
            .filter(|_| !pending);

        let message = if pending {
            tr::main::sign_in::pending_message()
        } else {
            tr::main::sign_in::message()
        };
        let primary = if pending {
            ui::Button::plain(
                &theme,
                "sign-in-prompt-reopen",
                ui::ButtonVariant::Blue,
                ui::ButtonSize::Lg,
            )
            .label(tr::main::sign_in::open_browser_again())
            .full_width()
            .on_click(cx.listener(|_, _, _, cx| {
                SignIn::global(cx).update(cx, |sign_in, cx| sign_in.reopen(cx));
            }))
        } else {
            ui::Button::plain(
                &theme,
                "sign-in-prompt-sign-in",
                ui::ButtonVariant::Blue,
                ui::ButtonSize::Lg,
            )
            .icon("icons/user-round.svg")
            .label(tr::main::sign_in::sign_in())
            .full_width()
            .on_click(cx.listener(|this, _, _, cx| {
                let Some(prompt) = this.sign_in_prompt.as_mut() else {
                    return;
                };
                prompt.error = None;
                let origin = SignInOrigin::Overlay(prompt.kind);
                SignIn::global(cx).update(cx, |sign_in, cx| sign_in.start(origin, cx));
                cx.notify();
            }))
        };
        let secondary = if pending {
            div().child(
                ui::Button::plain(
                    &theme,
                    "sign-in-prompt-cancel",
                    ui::ButtonVariant::Gray,
                    ui::ButtonSize::Lg,
                )
                .label(tr::common::cancel())
                .full_width()
                .on_click(cx.listener(|_, _, _, cx| {
                    SignIn::global(cx).update(cx, |sign_in, cx| sign_in.cancel(cx));
                })),
            )
        } else {
            div()
                .flex()
                .gap(px(8.))
                .child(
                    div().flex_1().child(
                        ui::Button::plain(
                            &theme,
                            "sign-in-prompt-studio",
                            ui::ButtonVariant::Gray,
                            ui::ButtonSize::Lg,
                        )
                        .label(tr::main::sign_in::use_studio())
                        .full_width()
                        .on_click(cx.listener(|this, _, _, cx| {
                            let Some(prompt) = this.sign_in_prompt.take() else {
                                return;
                            };
                            this.set_mode(Mode::Studio, cx);
                            this.arm_overlay(prompt.kind, cx);
                        })),
                    ),
                )
                .child(
                    div().flex_1().child(
                        ui::Button::plain(
                            &theme,
                            "sign-in-prompt-dismiss",
                            ui::ButtonVariant::Gray,
                            ui::ButtonSize::Lg,
                        )
                        .label(tr::main::sign_in::not_now())
                        .full_width()
                        .on_click(cx.listener(|this, _, _, cx| this.dismiss_sign_in_prompt(cx))),
                    ),
                )
        };

        let body = div()
            .flex()
            .flex_col()
            .gap(px(14.))
            .child(
                div()
                    .text_size(px(12.5))
                    .line_height(px(19.))
                    .text_color(theme.gray_11)
                    .child(message),
            )
            .when_some(error, |this, error| {
                this.child(
                    div()
                        .rounded(px(8.))
                        .bg(theme.red_3)
                        .px(px(10.))
                        .py(px(7.))
                        .text_size(px(12.))
                        .line_height(px(16.))
                        .text_color(theme.red_11)
                        .child(error),
                )
            })
            .child(
                div()
                    .flex()
                    .flex_col()
                    .gap(px(8.))
                    .child(primary)
                    .child(secondary),
            )
            .into_any_element();

        self.render_modal(
            "sign-in-prompt",
            ("icons/instant.svg", theme.blue_3, theme.blue_9),
            if pending {
                tr::main::sign_in::waiting()
            } else {
                tr::main::sign_in::title()
            },
            Self::dismiss_sign_in_prompt,
            body,
            cx,
        )
    }

    fn render_clean_capture_preflight(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        div()
            .absolute()
            .inset_0()
            .bg(theme.gray_1)
            .flex()
            .flex_col()
            .justify_center()
            .gap(px(16.))
            .p(px(24.))
            .child(
                div()
                    .text_size(px(18.))
                    .child(tr::main::clean_capture::title()),
            )
            .child(
                div()
                    .text_size(px(14.))
                    .child(app_windows::clean_capture_camera_message(cx)),
            )
            .child(
                div()
                    .text_size(px(14.))
                    .child(app_windows::clean_capture_shortcut_message(cx)),
            )
            .child(
                div()
                    .id("cancel-clean-capture")
                    .rounded(px(8.))
                    .p(px(12.))
                    .bg(theme.gray_3)
                    .cursor_pointer()
                    .child(tr::common::cancel())
                    .on_click(
                        cx.listener(|_, _, _, cx| cx.defer(app_windows::cancel_clean_capture)),
                    ),
            )
    }

    fn render_recovery_toast(
        &self,
        recording: library::IncompleteRecordingItem,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let theme = self.theme;
        let pending = self.recovery_pending;
        let duration = recording.estimated_duration_secs.round() as u64;
        let segments = recovery_segments_label(recording.segment_count as usize, duration);

        div()
            .absolute()
            .bottom(px(12.))
            .left(px(12.))
            .right(px(12.))
            .rounded(px(8.))
            .border_1()
            .border_color(theme.red_4)
            .bg(theme.red_2)
            .p(px(10.))
            .shadow_md()
            .child(
                div()
                    .flex()
                    .flex_row()
                    .items_center()
                    .gap(px(8.))
                    .child(
                        div()
                            .flex_1()
                            .min_w_0()
                            .flex()
                            .flex_col()
                            .child(
                                div()
                                    .text_size(px(10.))
                                    .font_weight(FontWeight::MEDIUM)
                                    .text_color(theme.red_11)
                                    .child(tr::main::recovery::title()),
                            )
                            .child(
                                div()
                                    .truncate()
                                    .text_size(px(12.))
                                    .font_weight(FontWeight::MEDIUM)
                                    .text_color(theme.gray_12)
                                    .child(recording.pretty_name),
                            )
                            .child(
                                div()
                                    .text_size(px(10.))
                                    .text_color(theme.gray_11)
                                    .child(segments),
                            )
                            .when_some(self.recovery_error.clone(), |this, error| {
                                this.child(
                                    div()
                                        .mt(px(4.))
                                        .text_size(px(10.))
                                        .text_color(theme.red_11)
                                        .child(error),
                                )
                            }),
                    )
                    .child(
                        div()
                            .flex()
                            .flex_row()
                            .flex_shrink_0()
                            .gap(px(6.))
                            .child(
                                ui::Button::plain(
                                    &theme,
                                    "recover-incomplete-recording",
                                    ui::ButtonVariant::Primary,
                                    ui::ButtonSize::Xs,
                                )
                                .label(if pending {
                                    "..."
                                } else {
                                    tr::main::recovery::recover()
                                })
                                .disabled(pending)
                                .on_click(cx.listener(
                                    |this, _, window, cx| {
                                        this.process_incomplete_recording(true, window, cx);
                                    },
                                )),
                            )
                            .child(
                                ui::Button::plain(
                                    &theme,
                                    "discard-incomplete-recording",
                                    ui::ButtonVariant::Gray,
                                    ui::ButtonSize::Xs,
                                )
                                .label(tr::main::recovery::discard())
                                .disabled(pending)
                                .on_click(cx.listener(
                                    |this, _, window, cx| {
                                        this.process_incomplete_recording(false, window, cx);
                                    },
                                )),
                            ),
                    ),
            )
    }

    fn render_header(&self, _window: &Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;

        let header = div()
            .when(cfg!(target_os = "windows"), |header| {
                header.window_control_area(gpui::WindowControlArea::Drag)
            })
            .flex()
            .flex_row()
            .items_center()
            .w_full()
            .h(px(HEADER_HEIGHT))
            .flex_shrink_0()
            .bg(theme.header_bg())
            // `divide-y divide-gray-5` between header and body.
            .border_b_1()
            .border_color(theme.header_border())
            .when(!cfg!(target_os = "windows"), |header| {
                header.child(self.render_traffic_lights(cx))
            })
            .child(self.render_header_actions(cx));

        #[cfg(target_os = "windows")]
        let header = header.child(self.render_windows_caption_controls(_window, cx));

        header
    }

    #[cfg(target_os = "windows")]
    fn render_windows_caption_controls(
        &self,
        window: &Window,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let dark = self.theme.is_dark();
        let foreground = Theme::with_alpha(
            rgb(if dark { 0xffffff } else { 0x12161f }),
            if window.is_window_active() { 0.8 } else { 0.4 },
        );
        let hover = gpui::rgba(if dark { 0xffffff0d } else { 0x0000000d });
        let pressed = gpui::rgba(if dark { 0xe9e9e908 } else { 0x00000008 });
        let button = |id: &'static str, icon: &'static str, height: f32| {
            div()
                .id(id)
                .group(id)
                .tab_index(0)
                .w(px(46.))
                .h_full()
                .flex_shrink_0()
                .flex()
                .items_center()
                .justify_center()
                .cursor_default()
                .hover(move |style| {
                    style.bg(if id == "caption-close" {
                        rgb(0xc42b1c)
                    } else {
                        hover
                    })
                })
                .active(move |style| {
                    style.bg(if id == "caption-close" {
                        gpui::rgba(0xc42b1ce6)
                    } else {
                        pressed
                    })
                })
                .child(
                    svg()
                        .path(icon)
                        .id("caption-glyph")
                        .w(px(10.))
                        .h(px(height))
                        .text_color(foreground)
                        .when(id == "caption-close", |icon| {
                            icon.group_hover("caption-close", |style| {
                                style.text_color(gpui::white())
                            })
                            .group_active("caption-close", |style| style.text_color(gpui::white()))
                        }),
                )
        };

        div()
            .occlude()
            .flex()
            .h_full()
            .flex_shrink_0()
            .child(
                button("caption-minimize", "icons/caption-minimize-windows.svg", 1.)
                    .on_click(|_, window, _| window.minimize_window()),
            )
            .child(
                button("caption-close", "icons/caption-close-windows.svg", 10.).on_click(
                    cx.listener(|_, _, _, cx| {
                        cx.defer(app_windows::request_close_main);
                    }),
                ),
            )
    }

    fn render_traffic_lights(&self, cx: &mut Context<Self>) -> impl IntoElement {
        // base, brightness(0.95), brightness(0.90) -- precomputed per light.
        let light = |base: u32,
                     hover: u32,
                     press: u32,
                     icon: &'static str,
                     icon_px: f32,
                     id: &'static str| {
            div()
                .id(id)
                .size(px(14.))
                .rounded_full()
                .flex()
                .items_center()
                .justify_center()
                .bg(rgb(base))
                .hover(move |style| style.bg(rgb(hover)))
                .active(move |style| style.bg(rgb(press)))
                .cursor_default()
                .child(
                    svg()
                        .path(icon)
                        .size(px(icon_px))
                        .text_color(gpui::rgba(0x0000_0080))
                        .invisible()
                        .group_hover("traffic-lights", |style| style.visible()),
                )
        };

        div()
            .flex()
            .flex_row()
            .items_center()
            .gap(px(10.))
            .h_full()
            .ml(px(12.))
            .flex_shrink_0()
            .group("traffic-lights")
            .child(
                light(
                    Theme::TRAFFIC_CLOSE,
                    0xf25a53,
                    0xe6564e,
                    "icons/traffic-close.svg",
                    10.,
                    "traffic-close",
                )
                // `getCurrentWindow().close()` in `CaptionControlsMacOS`, which
                // reaches `CapWindowId::Main`'s `CloseRequested` arm -- and
                // that arm *prevents* the close and hides the window
                // (`lib.rs:5644-5697`). With the tray present, closing the main
                // window must not quit Cap. Deferred out of the listener: the
                // hide path touches the window registry and orders the NSWindow
                // out, neither of which may happen inside this update.
                .on_click(cx.listener(|_, _, _window, cx| {
                    cx.defer(crate::app_windows::request_close_main);
                })),
            )
    }

    /// The teleported header content: a help button, a drag spacer, then the
    /// right-hand cluster. 20px hit targets (`size-5`) 4px apart (`gap-1`),
    /// 8px from the window edges (`mx-2`).
    fn render_header_actions(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let keyboard_nav = self.keyboard_nav;

        // `IconButton::header`: a 20px hit box with no fill, `text-gray-11`
        // going to `text-gray-12` on hover.
        let icon_button = |id: &'static str, path: &'static str, size: f32| {
            let label = match id {
                "help" => tr::main::header::help(),
                "settings" => tr::main::header::settings(),
                "screenshots" => tr::main::header::screenshots(),
                "recordings" => tr::main::header::recordings(),
                "teleprompter" => tr::main::header::teleprompter(),
                "changelog" => tr::main::header::changelog(),
                _ => unreachable!(),
            };
            div()
                .id(id)
                .group(id)
                .when(cfg!(target_os = "windows"), |this| this.occlude())
                .tab_index(0)
                .when(keyboard_nav, ui::keyboard_nav::ring)
                .role(gpui::Role::Button)
                .aria_label(label)
                .flex()
                .items_center()
                .justify_center()
                .size(px(20.))
                .flex_shrink_0()
                .rounded_full()
                .text_color(theme.gray_11)
                .hover(|style| style.text_color(theme.gray_12))
                .tooltip(move |_, cx| ui::Tooltip::new(&theme, label).view(cx))
                .tooltip_show_delay(ui::TOOLTIP_SHOW_DELAY)
                .child(
                    svg()
                        .path(path)
                        .size(px(size))
                        .text_color(theme.gray_11)
                        .group_hover(id, |style| style.text_color(theme.gray_12)),
                )
        };

        let actions = div()
            .flex()
            .flex_1()
            .items_center()
            .gap(px(4.))
            .mx(px(8.))
            .min_w_0()
            .child(
                icon_button("help", "icons/circle-help.svg", 16.).on_click(cx.listener(
                    |_, _, _window, cx| {
                        cx.defer(app_windows::open_onboarding);
                    },
                )),
            )
            // Keep drag handlers off the header root: starting native dragging
            // on a button's mouse-down consumes its later click.
            .child(div().id("drag-region").flex_1().min_w_0().h_full().when(
                !cfg!(target_os = "windows"),
                |region| {
                    region.on_mouse_down(gpui::MouseButton::Left, |_, window, _| {
                        window.start_window_move();
                    })
                },
            ))
            .child(
                div()
                    .flex()
                    .flex_row()
                    .items_center()
                    .gap(px(4.))
                    .flex_shrink_0()
                    .child(icon_button("settings", "icons/settings.svg", 16.).on_click(
                        cx.listener(|_, _, _window, cx| {
                            // `await commands.showWindow({ Settings: {
                            //  page: "general" } }); getCurrentWindow()
                            //  .hide()` -- both halves live in
                            // `open_settings`. Deferred because opening a
                            // window inside this update would double-lease
                            // the view (the `sync_overlays` rule).
                            cx.defer(|cx: &mut gpui::App| {
                                app_windows::open_settings(
                                    crate::settings_window::Page::General,
                                    cx,
                                )
                            });
                        }),
                    ))
                    .child(icon_button("screenshots", "icons/image.svg", 16.).on_click(
                        cx.listener(|this, _, window, cx| {
                            this.open_panel(Panel::Library(LibraryKind::Screenshots), window, cx);
                        }),
                    ))
                    .child(
                        icon_button("recordings", "icons/play-circle.svg", 16.).on_click(
                            cx.listener(|this, _, window, cx| {
                                this.open_panel(
                                    Panel::Library(LibraryKind::Recordings),
                                    window,
                                    cx,
                                );
                            }),
                        ),
                    )
                    .child(
                        icon_button("teleprompter", "icons/scan-text.svg", 16.).on_click(
                            cx.listener(|_, _, _window, cx| {
                                // `onClick={() => void openTeleprompter()}` on
                                // the header's `IconLucideScanText` button.
                                // Deferred for the same reason the gear is.
                                cx.defer(app_windows::open_teleprompter);
                            }),
                        ),
                    )
                    .child(
                        icon_button("changelog", "icons/bell.svg", 16.).on_click(cx.listener(
                            |_, _, _window, cx| {
                                cx.defer(|cx: &mut gpui::App| {
                                    app_windows::open_settings(Page::Changelog, cx)
                                });
                            },
                        )),
                    ),
            );

        #[cfg(target_os = "windows")]
        let actions = actions.h_full();

        actions
    }

    fn idle_error(&self, cx: &gpui::App) -> Option<String> {
        let session = self.session.read(cx);
        session
            .error
            .clone()
            .filter(|_| session.phase == Phase::Idle)
    }

    fn dismiss_idle_error(&mut self, cx: &mut Context<Self>) {
        let Some(shown) = self.idle_error(cx) else {
            return;
        };
        self.copied_error = None;
        self.session.update(cx, |session, cx| {
            if clear_shown_idle_error(session.phase, &mut session.error, &shown) {
                cx.notify();
            }
        });
    }

    fn render_idle_error(&self, error: String, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let copied = self.copied_error.as_deref() == Some(error.as_str());
        let copy_error = error.clone();

        let body = div()
            .flex()
            .flex_col()
            .gap(px(14.))
            .child(
                recording_error_text(error)
                    .rounded(px(8.))
                    .bg(theme.gray_3)
                    .px(px(10.))
                    .py(px(8.))
                    .text_size(px(12.5))
                    .line_height(px(18.))
                    .text_color(theme.gray_12),
            )
            .child(
                div()
                    .flex()
                    .gap(px(8.))
                    .child(
                        div().flex_1().child(
                            ui::Button::plain(
                                &theme,
                                "copy-recording-error",
                                ui::ButtonVariant::Gray,
                                ui::ButtonSize::Lg,
                            )
                            .icon(if copied {
                                "icons/check.svg"
                            } else {
                                "icons/copy.svg"
                            })
                            .label(if copied {
                                tr::common::copied()
                            } else {
                                tr::main::error::copy()
                            })
                            .full_width()
                            .on_click(cx.listener(
                                move |this, _, _, cx| {
                                    cx.write_to_clipboard(gpui::ClipboardItem::new_string(
                                        copy_error.clone(),
                                    ));
                                    this.copied_error = Some(copy_error.clone());
                                    cx.notify();
                                },
                            )),
                        ),
                    )
                    .child(
                        div().flex_1().child(
                            ui::Button::plain(
                                &theme,
                                "dismiss-recording-error",
                                ui::ButtonVariant::Blue,
                                ui::ButtonSize::Lg,
                            )
                            .label(tr::common::dismiss())
                            .full_width()
                            .on_click(cx.listener(|this, _, _, cx| this.dismiss_idle_error(cx))),
                        ),
                    ),
            )
            .into_any_element();

        self.render_modal(
            "recording-error-panel",
            ("icons/triangle-alert.svg", theme.red_3, theme.red_9),
            tr::main::error::title(),
            Self::dismiss_idle_error,
            body,
            cx,
        )
    }

    /// Page root: `px-[13px] gap-2 pb-[8px]`.
    fn render_body(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let root = div()
            .flex()
            .flex_col()
            .flex_1()
            .min_h_0()
            .px(px(13.))
            .pb(px(8.))
            .gap(px(8.))
            // `.cap-window-body { color: var(--macos-settings-text) }` under
            // panel glass; `--text-primary` otherwise.
            .text_color(self.theme.body_text());

        // The logo/mode row is hidden while a picker is open -- the panel takes
        // the full body, exactly as `!activeMenu() && ...` does in index.tsx.
        match self.panel {
            Some(panel) => root.child(self.render_panel(panel, cx)),
            None => root.child(self.render_logo_row(cx)).child(
                div()
                    .id("home-scroll")
                    .flex()
                    .flex_col()
                    .flex_1()
                    .min_h_0()
                    .w_full()
                    .pb(px(4.))
                    .gap(px(8.))
                    .overflow_y_scroll()
                    .child(self.render_targets(cx))
                    .child(self.render_base_controls(cx)),
            ),
        }
    }

    /// The recording takeover, from index.tsx 3510-3529: an absolute overlay
    /// (`bg-gray-1/80 backdrop-blur-xs` -- the blur is skipped here, this gpui
    /// rev has no per-element backdrop blur hook) with the Stop button pinned
    /// to the bottom (`px-6 pb-8`, `h-11 rounded-xl bg-red-9`).
    fn render_recording_overlay(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let phase = self.session.read(cx).phase;
        let stopping = phase == Phase::Stopping;
        let starting = phase == Phase::Starting;
        let countdown = self.session.read(cx).countdown_remaining();
        let can_stop = !stopping && (!starting || countdown.is_some());
        #[cfg(target_os = "linux")]
        let can_resume = clean_capture_resume_available(
            phase,
            app_windows::clean_capture_active(cx),
            self.session.read(cx).clean_capture_controls_safe(),
        );
        #[cfg(not(target_os = "linux"))]
        let can_resume = false;

        let mut wash: Hsla = theme.gray_1.into();
        wash.a = 0.8;

        div()
            .absolute()
            .inset_0()
            .when(cfg!(target_os = "windows"), |overlay| {
                overlay.top(px(HEADER_HEIGHT))
            })
            .rounded(px(16.))
            .flex()
            .flex_col()
            .justify_end()
            .px(px(24.))
            .pb(px(32.))
            .bg(wash)
            .when_some(countdown, |this, remaining| {
                this.child(div().mb(px(16.)).text_size(px(18.)).text_center().child(
                    if remaining == 0 {
                        tr::main::recording::starting().to_string()
                    } else {
                        tr::main::recording::starts_in(remaining)
                    },
                ))
            })
            .when_some(self.session.read(cx).error.clone(), |this, error| {
                this.child(
                    div()
                        .flex_shrink_0()
                        .mb(px(8.))
                        .text_size(px(11.))
                        .text_color(theme.red_9)
                        .text_center()
                        .child(error),
                )
            })
            .when(can_resume, |this| {
                this.child(
                    div()
                        .mb(px(12.))
                        .flex()
                        .flex_col()
                        .gap(px(8.))
                        .text_size(px(12.))
                        .child(tr::main::recording::paused_notice())
                        .child(
                            div()
                                .id("resume-clean-recording")
                                .h(px(36.))
                                .w_full()
                                .flex()
                                .items_center()
                                .justify_center()
                                .rounded(px(8.))
                                .border_1()
                                .border_color(theme.gray_5)
                                .bg(theme.gray_3)
                                .child(tr::main::recording::resume())
                                .on_click(cx.listener(|this, _, _, cx| {
                                    #[cfg(target_os = "linux")]
                                    {
                                        let session = this.session.clone();
                                        cx.defer(move |cx| {
                                            if clean_capture_resume_available(
                                                session.read(cx).phase,
                                                app_windows::clean_capture_active(cx),
                                                session.read(cx).clean_capture_controls_safe(),
                                            ) {
                                                session.update(cx, |session, cx| {
                                                    session.toggle_pause(cx)
                                                });
                                            }
                                        });
                                    }
                                    #[cfg(not(target_os = "linux"))]
                                    let _ = (this, cx);
                                })),
                        ),
                )
            })
            .child(
                div()
                    .id("stop-recording")
                    .h(px(44.))
                    .w_full()
                    .flex()
                    .flex_row()
                    .items_center()
                    .justify_center()
                    .gap(px(8.))
                    .rounded(px(12.))
                    .bg(theme.red_9)
                    .text_size(px(14.))
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(gpui::white())
                    // `disabled:opacity-60` while pending.
                    .when(!can_stop, |this| this.opacity(0.6))
                    .when(can_stop, |this| {
                        this.hover(|style| style.bg(theme.red_10))
                            .on_click(cx.listener(|this, _, _window, cx| {
                                this.session.update(cx, |session, cx| session.stop(cx));
                            }))
                    })
                    .child(
                        svg()
                            .path("icons/stop-circle.svg")
                            .size(px(16.))
                            .text_color(gpui::white()),
                    )
                    .child(match phase {
                        Phase::Starting if countdown.is_some() => tr::main::recording::cancel(),
                        Phase::Starting => tr::main::recording::starting(),
                        Phase::Stopping => tr::main::recording::stopping(),
                        _ => tr::main::recording::stop(),
                    }),
            )
    }

    /// Shared panel chrome: a Back button, then either a title or a search
    /// field, then a scrolling body. `TargetMenuPanel` and `ModeInfoPanel` in
    /// the Tauri app are the same shape, so they share one implementation here.
    fn render_panel(&self, panel: Panel, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;

        // Mode info has a static title where the target and device panels put a
        // search field.
        let header_trailing = match panel {
            Panel::ModeInfo => div()
                .flex_1()
                .min_w_0()
                .text_size(px(12.))
                .font_weight(FontWeight::MEDIUM)
                .text_color(theme.gray_11)
                .child(tr::main::mode::info::title())
                .into_any_element(),
            Panel::Library(kind) => self.render_library_header(kind, cx).into_any_element(),
            Panel::Device(_) if self.device_format_target.is_some() => div()
                .flex_1()
                .min_w_0()
                .truncate()
                .text_size(px(12.))
                .text_color(theme.gray_11)
                .child(match self.device_format_target.as_ref().unwrap() {
                    DeviceFormatTarget::Camera(camera) => camera_label(&camera.label),
                    DeviceFormatTarget::Microphone(name) => name.clone(),
                })
                .into_any_element(),
            Panel::Device(_) | Panel::Target(_) => self.render_search_field(cx).into_any_element(),
        };

        div()
            .flex()
            .flex_col()
            .flex_1()
            .min_h_0()
            .child(
                div()
                    .flex()
                    .flex_row()
                    .items_center()
                    .gap(px(12.))
                    .mt(px(12.))
                    // `min-h-[36px]`.
                    .h(px(36.))
                    .flex_shrink_0()
                    .child(
                        div()
                            .id("panel-back")
                            .when(self.keyboard_nav, ui::keyboard_nav::stop)
                            .role(gpui::Role::Button)
                            .aria_label("Back")
                            .flex()
                            .flex_row()
                            .items_center()
                            .gap(px(4.))
                            .h(px(36.))
                            .px(px(8.))
                            .flex_shrink_0()
                            .rounded(px(6.))
                            .text_size(px(12.))
                            .text_color(theme.gray_11)
                            .child(
                                svg()
                                    .path("icons/move-left.svg")
                                    .size(px(12.))
                                    .text_color(theme.gray_11),
                            )
                            .child(
                                div()
                                    .font_weight(FontWeight::MEDIUM)
                                    .text_color(theme.gray_12)
                                    .child(tr::common::back()),
                            )
                            .hover(move |style| style.bg(glass_hover(theme)))
                            .active(move |style| style.bg(glass_press(theme)))
                            .on_click(cx.listener(|this, _, _window, cx| this.back_panel(cx))),
                    )
                    .child(header_trailing),
            )
            .child(
                div()
                    .flex()
                    .flex_col()
                    .flex_1()
                    .min_h_0()
                    .pt(px(16.))
                    .overflow_hidden()
                    .child(
                        div()
                            .id("panel-body")
                            .flex()
                            .flex_col()
                            .flex_1()
                            .min_h_0()
                            .px(px(8.))
                            .gap(px(8.))
                            .overflow_y_scroll()
                            .child(match panel {
                                Panel::Device(menu) => {
                                    self.render_device_list(menu, cx).into_any_element()
                                }
                                Panel::Target(target) => {
                                    self.render_target_grid(target, cx).into_any_element()
                                }
                                Panel::ModeInfo => self.render_mode_info(cx).into_any_element(),
                                Panel::Library(kind) => {
                                    self.render_library_grid(kind, cx).into_any_element()
                                }
                            }),
                    ),
            )
    }

    fn close_panel(&mut self, cx: &mut Context<Self>) {
        self.input_poll_task = None;
        self.input_poll_generation = self.input_poll_generation.wrapping_add(1);
        self.device_format_target = None;
        self.device_formats = None;
        self.device_format_generation += 1;
        self.panel = None;
        self.search.clear();
        self.search_input
            .update(cx, |input, cx| input.set_text("", cx));
        self.library_task = None;
        // `enabled: displayMenuOpen()` / `windowMenuOpen()` -- nothing polls
        // and nothing captures while no picker is on screen. A sweep already in
        // flight is left to land, the way a TanStack refetch is.
        self.target_poll_task = None;
        cx.notify();
    }

    fn back_panel(&mut self, cx: &mut Context<Self>) {
        if self.device_format_target.take().is_some() {
            self.device_formats = None;
            self.device_format_generation += 1;
            cx.notify();
        } else {
            self.close_panel(cx);
        }
    }

    pub(crate) fn show_recorder(&mut self, cx: &mut Context<Self>) {
        self.plan = PlanBadge::current();
        cx.notify();
        if self.panel.is_some() {
            self.close_panel(cx);
        }
    }

    fn dismiss_main(&mut self, cx: &mut Context<Self>) -> bool {
        if self.microphone_warning.is_some() {
            self.dismiss_microphone_warning(cx);
        } else if self.sign_in_prompt.is_some() {
            self.dismiss_sign_in_prompt(cx);
        } else if self.idle_error(cx).is_some() {
            self.dismiss_idle_error(cx);
        } else if self.mode_hover.visible.is_some() {
            self.clear_mode_hover();
            cx.notify();
        } else if self.panel.is_some() {
            if self.search.is_empty() {
                self.back_panel(cx);
            } else {
                self.clear_search(cx);
            }
        } else if self.session.read(cx).editor_recording_target().is_some() {
            self.cancel_deep_link_start();
            cx.defer(app_windows::abort_editor_recording_flow);
        } else {
            return false;
        }
        true
    }

    pub fn open_panel(&mut self, panel: Panel, window: &mut Window, cx: &mut Context<Self>) {
        self.input_poll_task = None;
        self.input_poll_generation = self.input_poll_generation.wrapping_add(1);
        self.clear_mode_hover();
        self.device_format_target = None;
        self.device_formats = None;
        self.device_format_generation += 1;
        self.panel = Some(panel);
        self.search.clear();
        self.search_input
            .update(cx, |input, cx| input.set_text("", cx));
        if matches!(
            panel,
            Panel::Device(_) | Panel::Target(_) | Panel::Library(_)
        ) {
            let focus = self.search_input.read(cx).focus_handle();
            window.focus(&focus, cx);
        }
        if let Panel::Library(kind) = panel {
            self.refresh_library(kind, window, cx);
        } else {
            self.library_task = None;
        }
        // Opening a target picker re-reads the list immediately and then keeps
        // it fresh -- until this, the grid showed whatever `start_enumeration`
        // found at launch, so a window opened since was simply missing.
        match panel {
            Panel::Target(kind) => self.start_target_poll(kind, window, cx),
            _ => self.target_poll_task = None,
        }
        if let Panel::Device(menu) = panel {
            self.start_input_poll(menu, window, cx);
        }
        cx.notify();
    }

    /// The panel filter -- `ui::TextInput::search`.
    ///
    /// A real field now: caret movement, selection, click-to-position,
    /// double-click-a-word, the clipboard and undo all come from
    /// `TextInputState`. What stays here is the only part the component cannot
    /// know, which is that Escape clears the filter before it closes the panel.
    fn render_search_field(&self, cx: &mut Context<Self>) -> gpui::Div {
        self.search_input
            .update(cx, |input, _| input.set_placeholder(tr::common::search()));

        div().flex().flex_1().min_w_0().child(ui::TextInput::search(
            &self.theme,
            "panel-search",
            &self.search_input,
        ))
    }

    fn render_library_header(&self, kind: LibraryKind, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;
        div()
            .flex()
            .flex_1()
            .min_w_0()
            .gap(px(8.))
            .items_center()
            .child(self.render_search_field(cx))
            .child(
                ui::Button::plain(
                    &theme,
                    "library-import",
                    ui::ButtonVariant::Gray,
                    ui::ButtonSize::Sm,
                )
                .label(kind.import_label())
                .icon("icons/import.svg")
                .height(px(36.))
                .on_click(cx.listener(move |_, _, _window, cx| {
                    // `importVideoFromPicker` / `importImageFromPicker`
                    // (`utils/importMedia.ts:58-86`). Deferred out of the
                    // listener because the picker task must start with a
                    // clean App borrow, like every other panel opener.
                    cx.defer(move |cx| match kind {
                        LibraryKind::Recordings => crate::import::pick_and_import_video(cx),
                        LibraryKind::Screenshots => crate::import::pick_and_import_image(cx),
                    });
                })),
            )
    }

    /// Re-scan whichever library panel is open -- the import pipeline's
    /// refresh seam: a finished import has to land in the panel the user is
    /// probably watching it from.
    pub fn refresh_open_library(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if let Some(Panel::Library(kind)) = self.panel {
            self.refresh_library(kind, window, cx);
        }
        cx.notify();
    }

    fn refresh_library(&mut self, kind: LibraryKind, window: &mut Window, cx: &mut Context<Self>) {
        self.library_task = Some(cx.spawn_in(window, async move |this, cx| {
            let (recordings, screenshots) = cx
                .background_executor()
                .spawn(async move {
                    match kind {
                        LibraryKind::Recordings => {
                            let items =
                                library::list_recordings_limited(library::LIBRARY_PANEL_LIMIT);
                            (Some(items), None)
                        }
                        LibraryKind::Screenshots => {
                            let items =
                                library::list_screenshots_limited(library::LIBRARY_PANEL_LIMIT);
                            (None, Some(items))
                        }
                    }
                })
                .await;

            let thumbnails = match this.update_in(cx, |this, window, cx| {
                this.set_library(kind, recordings, screenshots, window, cx)
            }) {
                Ok(thumbnails) => thumbnails,
                Err(_) => return,
            };

            let (_decodes, results) = library::spawn_decode_pool(
                cx.background_executor(),
                thumbnails,
                |(index, path, version)| {
                    library::decode_thumbnail(&path).map(|image| (index, path, version, image))
                },
            );
            while let Ok(first) = results.recv_async().await {
                let mut batch = vec![first];
                batch.extend(results.try_iter());
                if this
                    .update_in(cx, |this, window, cx| {
                        for (index, path, version, image) in batch {
                            this.set_library_thumbnail(
                                kind, index, path, version, image, window, cx,
                            );
                        }
                    })
                    .is_err()
                {
                    return;
                }
            }
        }));
    }

    fn set_library(
        &mut self,
        kind: LibraryKind,
        recordings: Option<Vec<RecordingItem>>,
        screenshots: Option<Vec<ScreenshotItem>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Vec<(usize, std::path::PathBuf, Option<u128>)> {
        let mut cached_recordings: std::collections::HashMap<_, _> =
            std::collections::HashMap::new();
        let mut cached_screenshots: std::collections::HashMap<_, _> =
            std::collections::HashMap::new();
        match (kind, self.library.take()) {
            (LibraryKind::Recordings, Some(LibraryItems::Recordings(rows))) => {
                cached_recordings = rows
                    .into_iter()
                    .map(|row| (row.item.path.clone(), row))
                    .collect();
            }
            (LibraryKind::Screenshots, Some(LibraryItems::Screenshots(rows))) => {
                cached_screenshots = rows
                    .into_iter()
                    .map(|row| (row.item.path.clone(), row))
                    .collect();
            }
            (_, previous) => {
                self.library = previous;
                self.drop_library_images(window);
            }
        }
        let mut pending = Vec::new();
        self.library = Some(match kind {
            LibraryKind::Recordings => LibraryItems::Recordings(
                recordings
                    .unwrap_or_default()
                    .into_iter()
                    .enumerate()
                    .map(|(index, item)| {
                        let previous = cached_recordings.remove(&item.path);
                        let changed = previous.as_ref().is_none_or(|row| {
                            row.item.thumbnail != item.thumbnail
                                || row.item.thumbnail_version != item.thumbnail_version
                                || row.thumbnail_stale
                                || row.thumbnail.is_none()
                        });
                        if changed && let Some(path) = item.thumbnail.clone() {
                            pending.push((index, path, item.thumbnail_version));
                        }
                        LibraryRow {
                            item,
                            thumbnail: previous.and_then(|row| row.thumbnail),
                            thumbnail_stale: changed,
                        }
                    })
                    .collect(),
            ),
            LibraryKind::Screenshots => LibraryItems::Screenshots(
                screenshots
                    .unwrap_or_default()
                    .into_iter()
                    .enumerate()
                    .map(|(index, item)| {
                        let previous = cached_screenshots.remove(&item.path);
                        let has_thumbnail =
                            previous.as_ref().is_some_and(|row| row.thumbnail.is_some());
                        let previous_stale =
                            previous.as_ref().is_some_and(|row| row.thumbnail_stale);
                        let changed = !library::screenshot_thumbnail_is_current(
                            &item,
                            previous.as_ref().map(|row| &row.item),
                            has_thumbnail,
                            previous_stale,
                        );
                        if changed && let Some(path) = item.thumbnail.clone() {
                            pending.push((index, path, item.thumbnail_version));
                        }
                        LibraryRow {
                            item,
                            thumbnail: previous.and_then(|row| row.thumbnail),
                            thumbnail_stale: changed,
                        }
                    })
                    .collect(),
            ),
        });
        for (_, row) in cached_recordings {
            if let Some(image) = row.thumbnail {
                let _ = window.drop_image(image);
            }
        }
        for (_, row) in cached_screenshots {
            if let Some(image) = row.thumbnail {
                let _ = window.drop_image(image);
            }
        }
        cx.notify();
        window.refresh();
        pending
    }

    fn set_library_thumbnail(
        &mut self,
        kind: LibraryKind,
        index: usize,
        path: std::path::PathBuf,
        version: Option<u128>,
        image: std::sync::Arc<gpui::RenderImage>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let replaced = match (&mut self.library, kind) {
            (Some(LibraryItems::Recordings(rows)), LibraryKind::Recordings) => rows
                .get_mut(index)
                .filter(|row| row.item.thumbnail.as_deref() == Some(path.as_path()))
                .and_then(|row| {
                    row.thumbnail_stale = false;
                    row.thumbnail.replace(image)
                }),
            (Some(LibraryItems::Screenshots(rows)), LibraryKind::Screenshots) => rows
                .get_mut(index)
                .filter(|row| {
                    row.item.thumbnail.as_deref() == Some(path.as_path())
                        && row.item.thumbnail_version == version
                })
                .and_then(|row| {
                    row.thumbnail_stale = false;
                    row.thumbnail.replace(image)
                }),
            _ => None,
        };
        if let Some(old) = replaced {
            let _ = window.drop_image(old);
        }
        cx.notify();
        window.refresh();
    }

    fn drop_library_images(&mut self, window: &mut Window) {
        match self.library.take() {
            Some(LibraryItems::Recordings(rows)) => {
                for row in rows {
                    if let Some(image) = row.thumbnail {
                        let _ = window.drop_image(image);
                    }
                }
            }
            Some(LibraryItems::Screenshots(rows)) => {
                for row in rows {
                    if let Some(image) = row.thumbnail {
                        let _ = window.drop_image(image);
                    }
                }
            }
            None => {}
        }
    }

    fn render_library_grid(&self, kind: LibraryKind, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;
        let grid = div().flex().flex_col().gap(px(8.)).w_full();

        if self.library.is_none() {
            let mut skeletons = Vec::new();
            for _ in 0..4 {
                skeletons.push(
                    div()
                        .flex_1()
                        .min_w_0()
                        .h(px(128.))
                        .rounded(px(8.))
                        .bg(theme.body_fill(3))
                        .into_any_element(),
                );
            }
            return grid.children(skeletons.chunks_mut(2).map(|pair| {
                let mut row = div().flex().flex_row().gap(px(8.)).w_full();
                for card in pair.iter_mut() {
                    row = row.child(std::mem::replace(card, div().into_any_element()));
                }
                if pair.len() == 1 {
                    row = row.child(div().flex_1());
                }
                row
            }));
        }

        let mut cards: Vec<gpui::AnyElement> = Vec::new();
        // Running imports lead the grid they will land in, the way an
        // in-progress recording leads the Tauri recordings list.
        for (index, entry) in crate::import::imports_snapshot(cx).iter().enumerate() {
            let wanted = match kind {
                LibraryKind::Recordings => entry.kind == crate::import::ImportKind::Video,
                LibraryKind::Screenshots => entry.kind == crate::import::ImportKind::Image,
            };
            if wanted {
                cards.push(self.render_import_card(index, entry).into_any_element());
            }
        }
        match (kind, &self.library) {
            (LibraryKind::Recordings, Some(LibraryItems::Recordings(rows))) => {
                for row in rows {
                    if !self.matches_search(&row.item.pretty_name) {
                        continue;
                    }
                    cards.push(self.render_recording_card(row, cx).into_any_element());
                }
            }
            (LibraryKind::Screenshots, Some(LibraryItems::Screenshots(rows))) => {
                for row in rows {
                    if !self.matches_search(&row.item.pretty_name) {
                        continue;
                    }
                    cards.push(self.render_screenshot_card(row, cx).into_any_element());
                }
            }
            _ => {}
        }

        if cards.is_empty() {
            return grid.child(self.render_library_empty(kind, cx));
        }

        let view_all = kind.view_all_label();
        let page = kind.settings_page();
        cards.push(
            div()
                .id("library-view-all")
                .flex()
                .flex_1()
                .min_w_0()
                .h(px(76.))
                .items_center()
                .justify_center()
                .rounded(px(8.))
                .border_1()
                .border_color(theme.body_border(5))
                .bg(theme.body_fill(3))
                .text_size(px(12.))
                .font_weight(FontWeight::MEDIUM)
                .text_color(theme.gray_12)
                .cursor_pointer()
                .hover(|style| style.bg(theme.body_hover_fill(4)))
                .child(view_all)
                .on_click(cx.listener(move |_, _, _window, cx| {
                    cx.defer(move |cx| app_windows::open_settings(page, cx));
                }))
                .into_any_element(),
        );

        grid.children(cards.chunks_mut(2).map(|pair| {
            let mut row = div().flex().flex_row().gap(px(8.)).w_full().items_stretch();
            for card in pair.iter_mut() {
                row = row.child(std::mem::replace(card, div().into_any_element()));
            }
            if pair.len() == 1 {
                row = row.child(div().flex_1());
            }
            row
        }))
    }

    fn render_library_empty(&self, kind: LibraryKind, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;
        let searching = !self.search.is_empty();
        let page = kind.settings_page();
        div()
            .flex()
            .flex_col()
            .w_full()
            .items_center()
            .justify_center()
            .gap(px(8.))
            .py(px(32.))
            .child(
                svg()
                    .path(kind.empty_icon())
                    .size(px(20.))
                    .text_color(theme.gray_10),
            )
            .child(
                div()
                    .text_size(px(14.))
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(theme.gray_12)
                    .child(if searching {
                        kind.no_match()
                    } else {
                        kind.empty_title()
                    }),
            )
            .when(!searching, |this| {
                this.child(
                    div()
                        .text_size(px(12.))
                        .text_color(theme.gray_11)
                        .text_center()
                        .child(kind.empty_description()),
                )
                .child(
                    ui::Button::plain(
                        &theme,
                        "library-view-all-empty",
                        ui::ButtonVariant::Gray,
                        ui::ButtonSize::Sm,
                    )
                    .label(kind.view_all_label())
                    .on_click(cx.listener(move |_, _, _window, cx| {
                        cx.defer(move |cx| app_windows::open_settings(page, cx));
                    })),
                )
            })
    }

    /// The importing card: `ImportProgress.tsx`'s ring drawn in the library
    /// card's shell, with the clips-badge pill relabelled "Importing" -- the
    /// same look the library's in-progress recordings wear.
    fn render_import_card(
        &self,
        index: usize,
        entry: &crate::import::ImportProgress,
    ) -> impl IntoElement {
        let theme = self.theme;
        let converting = entry.stage == crate::import::ImportStage::Converting;
        let ring = {
            let ring =
                ui::CircularProgress::new(px(36.), px(3.), theme.gray(4), theme.blue_9.into());
            if converting {
                ring.progress(entry.progress as f32)
                    .label(theme.gray_12.into(), px(9.))
            } else {
                ring.indeterminate()
            }
        };

        div()
            .id(("library-importing", index))
            .flex()
            .flex_col()
            .flex_1()
            .min_w_0()
            .overflow_hidden()
            .rounded(px(8.))
            .bg(theme.body_fill(3))
            .child(
                div()
                    .relative()
                    .w_full()
                    .h(px(76.))
                    .rounded_t(px(8.))
                    .overflow_hidden()
                    .bg(theme.body_fill(4))
                    .flex()
                    .items_center()
                    .justify_center()
                    .child(ring)
                    .child(
                        div()
                            .absolute()
                            .left(px(4.))
                            .top(px(4.))
                            .rounded_full()
                            .bg(black_alpha(0.55))
                            .px(px(6.))
                            .py(px(2.))
                            .text_size(px(10.))
                            .font_weight(FontWeight::MEDIUM)
                            .text_color(gpui::white())
                            .child(tr::main::library::importing()),
                    ),
            )
            .child(
                div()
                    .flex()
                    .flex_col()
                    .w_full()
                    .min_w_0()
                    .px(px(8.))
                    .py(px(6.))
                    .pb(px(10.))
                    .text_size(px(11.))
                    .child(
                        div()
                            .w_full()
                            .truncate()
                            .font_weight(FontWeight::MEDIUM)
                            .text_color(theme.gray_12)
                            .child(entry.pretty_name.clone()),
                    )
                    .child(
                        div()
                            .w_full()
                            .truncate()
                            .text_color(theme.gray_11)
                            .child(entry.message.clone()),
                    ),
            )
    }

    fn render_recording_card(
        &self,
        row: &LibraryRow<RecordingItem>,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let theme = self.theme;
        let item = &row.item;
        let path = item.path.clone();
        let studio = item.mode == library::RecordingMode::Studio;
        let sharing = item.sharing.clone();
        let mode = item.mode;
        let opens_editor = item.opens_editor();
        let subtitle = item.upload.as_ref().map_or_else(
            || item.mode.label().to_string(),
            |upload| {
                upload
                    .last_error
                    .clone()
                    .unwrap_or_else(|| upload.label().to_string())
            },
        );
        let actions = self.render_recording_card_actions(item, cx);

        self.library_card(
            gpui::ElementId::Path(item.path.as_path().into()),
            row.thumbnail.clone(),
            "icons/square-play.svg",
            item.pretty_name.clone(),
            Some(subtitle),
            item.clip_count,
            cx.listener(move |_, _, _window, cx| {
                if studio && opens_editor {
                    let path = path.clone();
                    cx.defer(move |cx| app_windows::open_editor(path, cx));
                } else if !studio && let Some(url) = &sharing {
                    cx.open_url(url);
                } else if !studio {
                    library::open_recording_folder(&path, mode, cx);
                }
            }),
            actions,
            theme,
        )
    }

    fn render_recording_card_actions(
        &self,
        item: &RecordingItem,
        cx: &mut Context<Self>,
    ) -> gpui::Div {
        let path = item.path.clone();
        let mode = item.mode;
        let sharing = item.sharing.clone();
        let mut row = div()
            .flex()
            .flex_row()
            .items_center()
            .justify_between()
            .px(px(8.))
            .pb(px(6.))
            .gap(px(4.));
        if item.opens_editor() {
            let editor = path.clone();
            row = row.child(self.library_action(
                "lib-rec-edit",
                "icons/edit.svg",
                cx.listener(move |_, _, _window, cx| {
                    let editor = editor.clone();
                    cx.defer(move |cx| app_windows::open_editor(editor, cx));
                }),
            ));
        }
        if let Some(url) = sharing {
            row = row.child(self.library_action(
                "lib-rec-link",
                "icons/link.svg",
                move |_, _, cx| cx.open_url(&url),
            ));
        }
        if item
            .upload
            .as_ref()
            .is_some_and(crate::upload::queue::UploadState::can_retry)
        {
            let retry_path = path.clone();
            row = row.child(self.library_action(
                "lib-rec-retry",
                "icons/rotate-ccw.svg",
                cx.listener(move |_, _, window, cx| {
                    let path = retry_path.clone();
                    cx.spawn_in(window, async move |this, cx| {
                        let Ok(task) = cx.update(|_, cx| {
                            gpui_tokio::Tokio::spawn(cx, crate::upload::queue::retry(path))
                        }) else {
                            return;
                        };
                        if let Err(error) =
                            task.await.unwrap_or_else(|error| Err(error.to_string()))
                        {
                            tracing::warn!(%error, "Upload retry deferred");
                        }
                        let _ = this.update_in(cx, |this, window, cx| {
                            this.refresh_open_library(window, cx)
                        });
                    })
                    .detach();
                }),
            ));
        }
        let folder = path.clone();
        row = row.child(self.library_action(
            "lib-rec-folder",
            "icons/folder.svg",
            move |_, _, cx| library::open_recording_folder(&folder, mode, cx),
        ));
        row.child(self.library_action(
            "lib-rec-delete",
            "icons/trash.svg",
            cx.listener(move |this, _, window, cx| {
                this.delete_library_recording(path.clone(), window, cx);
            }),
        ))
    }

    fn render_screenshot_card(
        &self,
        row: &LibraryRow<ScreenshotItem>,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let theme = self.theme;
        let item = &row.item;
        let png = item.path.clone();
        let actions = self.render_screenshot_card_actions(item, cx);
        self.library_card(
            gpui::ElementId::Path(item.path.as_path().into()),
            row.thumbnail.clone(),
            "icons/image.svg",
            item.pretty_name.clone(),
            None,
            0,
            move |_, _, cx| {
                let png = png.clone();
                cx.defer(move |cx| app_windows::open_screenshot_editor(png, cx));
            },
            actions,
            theme,
        )
    }

    fn render_screenshot_card_actions(
        &self,
        item: &ScreenshotItem,
        cx: &mut Context<Self>,
    ) -> gpui::Div {
        let png = item.path.clone();
        let name = item.pretty_name.clone();
        let bundle = item.bundle.clone();
        div()
            .flex()
            .flex_row()
            .items_center()
            .justify_between()
            .px(px(8.))
            .pb(px(6.))
            .gap(px(4.))
            .child(self.library_action("lib-ss-copy", "icons/copy.svg", {
                let png = png.clone();
                move |_, _, cx| {
                    let copied = crate::platform::copy_image_to_clipboard(png.clone(), cx);
                    cx.spawn(async move |_| {
                        if let Err(error) = copied.await {
                            tracing::warn!("copying screenshot failed: {error}");
                            crate::platform::alert_dialog(
                                tr::main::library::copy_screenshot_failed(),
                                &error,
                            );
                        }
                    })
                    .detach();
                }
            }))
            .child(self.library_action(
                "lib-ss-save",
                "icons/download.svg",
                cx.listener(move |this, _, window, cx| {
                    this.save_screenshot(png.clone(), name.clone(), window, cx);
                }),
            ))
            .child(self.library_action("lib-ss-open", "icons/folder.svg", {
                let bundle = bundle.clone();
                move |_, _, cx| library::reveal_path(&bundle, cx)
            }))
            .child(self.library_action(
                "lib-ss-delete",
                "icons/trash.svg",
                cx.listener(move |this, _, window, cx| {
                    this.delete_library_screenshot(bundle.clone(), window, cx);
                }),
            ))
    }

    fn library_action(
        &self,
        id: impl Into<gpui::ElementId>,
        icon: &'static str,
        on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static,
    ) -> impl IntoElement {
        let theme = self.theme;
        div()
            .id(id)
            .flex_1()
            .flex()
            .items_center()
            .justify_center()
            .p(px(4.))
            .rounded(px(4.))
            .text_color(theme.gray_11)
            .cursor_pointer()
            .hover(|style| style.bg(theme.body_hover_fill(5)).text_color(theme.gray_12))
            .child(svg().path(icon).size(px(14.)).text_color(theme.gray_11))
            .on_click(on_click)
    }

    #[allow(clippy::too_many_arguments)]
    fn library_card(
        &self,
        id: impl Into<gpui::ElementId>,
        thumbnail: Option<std::sync::Arc<gpui::RenderImage>>,
        fallback: &'static str,
        label: String,
        subtitle: Option<String>,
        clip_count: u32,
        on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static,
        actions: impl IntoElement,
        theme: Theme,
    ) -> impl IntoElement {
        let thumb = match thumbnail {
            Some(image) => {
                use gpui::StyledImage as _;
                img(image)
                    .size_full()
                    .rounded_t(px(8.))
                    .object_fit(gpui::ObjectFit::Cover)
                    .into_any_element()
            }
            None => div()
                .rounded_t(px(8.))
                .flex()
                .size_full()
                .items_center()
                .justify_center()
                .bg(theme.body_fill(4))
                .child(svg().path(fallback).size(px(24.)).text_color(theme.gray_9))
                .into_any_element(),
        };

        div()
            .id(id)
            .flex()
            .flex_col()
            .flex_1()
            .min_w_0()
            .overflow_hidden()
            .rounded(px(8.))
            .bg(theme.body_fill(3))
            .hover(|style| style.bg(theme.body_hover_fill(4)))
            .child(
                div()
                    .id("library-card-open")
                    .cursor_pointer()
                    .child(
                        div()
                            .relative()
                            .w_full()
                            .h(px(76.))
                            .rounded_t(px(8.))
                            .overflow_hidden()
                            .bg(theme.body_fill(4))
                            .child(thumb)
                            .when(clip_count > 1, |this| {
                                this.child(
                                    div()
                                        .absolute()
                                        .left(px(4.))
                                        .top(px(4.))
                                        .rounded_full()
                                        .bg(black_alpha(0.55))
                                        .px(px(6.))
                                        .py(px(2.))
                                        .text_size(px(10.))
                                        .font_weight(FontWeight::MEDIUM)
                                        .text_color(gpui::white())
                                        .child(tr::main::library::clips(clip_count as usize)),
                                )
                            }),
                    )
                    .child(
                        div()
                            .flex()
                            .flex_col()
                            .w_full()
                            .min_w_0()
                            .px(px(8.))
                            .py(px(6.))
                            .text_size(px(11.))
                            .child(
                                div()
                                    .w_full()
                                    .truncate()
                                    .font_weight(FontWeight::MEDIUM)
                                    .text_color(theme.gray_12)
                                    .child(label),
                            )
                            .children(subtitle.map(|subtitle| {
                                div()
                                    .w_full()
                                    .truncate()
                                    .text_color(theme.gray_11)
                                    .child(subtitle)
                            })),
                    )
                    .on_click(on_click),
            )
            .child(actions)
    }

    fn delete_library_recording(
        &mut self,
        path: std::path::PathBuf,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        cx.spawn_in(window, async move |_, cx| {
            if !crate::platform::confirm_dialog(
                "Cap",
                tr::main::delete_recording_confirm(),
                tr::common::yes(),
                tr::common::no(),
                false,
            ) {
                return;
            }
            let Ok(task) = cx.update(|_, cx| {
                gpui_tokio::Tokio::spawn(cx, crate::upload::queue::delete_recording(path.clone()))
            }) else {
                return;
            };
            let deleted = task.await.unwrap_or_else(|error| Err(error.to_string()));
            if let Err(error) = deleted {
                tracing::error!(path = %path.display(), "deleting the recording failed: {error}");
                return;
            }
            cx.update(|_, cx| cx.defer(app_windows::refresh_library_after_delete))
                .ok();
        })
        .detach();
    }

    fn delete_library_screenshot(
        &mut self,
        bundle: std::path::PathBuf,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        cx.spawn_in(window, async move |this, cx| {
            if !crate::platform::confirm_dialog(
                "Cap",
                tr::main::delete_screenshot_confirm(),
                tr::common::yes(),
                tr::common::no(),
                false,
            ) {
                return;
            }
            let deleted = cx
                .background_executor()
                .spawn({
                    let bundle = bundle.clone();
                    async move { library::delete_screenshot(&bundle) }
                })
                .await;
            if let Err(error) = deleted {
                tracing::error!(path = %bundle.display(), "deleting the screenshot failed: {error}");
                return;
            }
            this.update_in(cx, |this, window, cx| {
                this.refresh_library(LibraryKind::Screenshots, window, cx);
            })
            .ok();
        })
        .detach();
    }

    fn save_screenshot(
        &mut self,
        src: std::path::PathBuf,
        name: String,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        cx.spawn_in(window, async move |this, cx| {
            let dest =
                crate::platform::save_file_panel_async(&format!("{name}.png"), &["png"], cx).await;
            if this.update_in(cx, |_, _, _| ()).is_err() {
                return;
            }
            let Some(dest) = dest else {
                return;
            };
            if let Err(error) = library::copy_file_to_path(&src, &dest) {
                tracing::error!("saving screenshot failed: {error}");
            }
        })
        .detach();
    }

    /// Case-insensitive substring match, the same test the Tauri panels filter
    /// on before they highlight the matched run.
    fn matches_search(&self, haystack: &str) -> bool {
        if self.search.is_empty() {
            return true;
        }
        haystack
            .to_lowercase()
            .contains(&self.search.to_lowercase())
    }

    /// The filter field's own events. Escape is the panel's, not the field's:
    /// the first one clears the filter, a second one leaves -- which is why
    /// `TextInputState` emits `Cancelled` instead of deciding.
    fn on_search_event(
        &mut self,
        input: Entity<ui::TextInputState>,
        event: &ui::TextInputEvent,
        cx: &mut Context<Self>,
    ) {
        match event {
            ui::TextInputEvent::Changed => {
                self.search = input.read(cx).text().to_string();
                cx.notify();
            }
            ui::TextInputEvent::Cancelled => {
                self.dismiss_main(cx);
            }
            _ => {}
        }
    }

    fn clear_search(&mut self, cx: &mut Context<Self>) {
        self.search.clear();
        self.search_input
            .update(cx, |input, cx| input.set_text("", cx));
        cx.notify();
    }

    fn render_device_list(&self, menu: DeviceMenu, cx: &mut Context<Self>) -> gpui::Div {
        if self.device_format_target.is_some() {
            return self.render_device_formats(menu, cx);
        }
        let theme = self.theme;
        let list = div().flex().flex_col().gap(px(4.)).w_full();

        if self.enumerating {
            return list.child(
                div()
                    .py(px(24.))
                    .w_full()
                    .text_size(px(14.))
                    .text_color(theme.gray_11)
                    .child(tr::common::loading()),
            );
        }

        let list = list.children(self.render_access_banner(menu, cx));

        // The "none" row is always offered and is never filtered out -- it is
        // how you turn the device off, not a search result.
        let mut rows = vec![
            self.render_device_list_row(
                SharedString::from(format!("{}-none", menu.id_name())),
                "icons/circle-x.svg",
                menu.none_label().to_string(),
                None,
                match menu {
                    DeviceMenu::Camera => self.camera.is_none(),
                    DeviceMenu::Microphone => self.microphone.is_none(),
                },
                None,
                cx.listener(move |this, _, _window, cx| {
                    if !this.device_changes_allowed(cx) {
                        return;
                    }
                    match menu {
                        DeviceMenu::Camera => {
                            this.set_camera_selection(None, cx);
                        }
                        DeviceMenu::Microphone => {
                            this.set_microphone_selection(None, cx);
                        }
                    }
                    this.close_panel(cx);
                }),
            )
            .into_any_element(),
        ];

        let mut matched = 0usize;
        match menu {
            DeviceMenu::Camera => {
                for camera in self
                    .devices
                    .cameras
                    .iter()
                    .filter(|camera| self.matches_search(&camera.label))
                {
                    matched += 1;
                    let selected = self
                        .camera
                        .as_ref()
                        .is_some_and(|selected| selected.device_id == camera.device_id);
                    let chosen = camera.clone();

                    rows.push(
                        div()
                            .flex()
                            .flex_row()
                            .gap(px(4.))
                            .w_full()
                            .child(
                                self.render_device_list_row(
                                    SharedString::from(format!("camera-{}", camera.device_id)),
                                    menu.icon(),
                                    camera.label.clone(),
                                    if selected {
                                        Feeds::global(cx)
                                            .read(cx)
                                            .applied_device_settings()
                                            .camera
                                            .map(|settings| DeviceFormat::Camera(settings).label())
                                    } else {
                                        camera.best_format.map(|format| {
                                            tr::main::device::format::available(format.describe())
                                        })
                                    },
                                    selected,
                                    None,
                                    cx.listener(move |this, _, _window, cx| {
                                        if !this.device_changes_allowed(cx) {
                                            return;
                                        }
                                        this.ensure_media_access(OSPermission::Camera, cx);
                                        this.set_camera_selection(Some(chosen.clone()), cx);
                                        this.close_panel(cx);
                                    }),
                                )
                                .flex_1()
                                .min_w_0(),
                            )
                            .child(self.render_device_format_button(
                                DeviceFormatTarget::Camera(camera.clone()),
                                cx,
                            ))
                            .into_any_element(),
                    );
                }
            }
            DeviceMenu::Microphone => {
                for mic in self
                    .devices
                    .microphones
                    .iter()
                    .filter(|mic| self.matches_search(&mic.name))
                {
                    matched += 1;
                    let selected = self
                        .microphone
                        .as_ref()
                        .is_some_and(|selected| selected.name == mic.name);
                    let chosen = mic.clone();

                    rows.push(
                        div()
                            .flex()
                            .flex_row()
                            .gap(px(4.))
                            .w_full()
                            .child(
                                self.render_device_list_row(
                                    SharedString::from(format!("mic-{}", mic.name)),
                                    menu.icon(),
                                    mic.name.clone(),
                                    if selected {
                                        Feeds::global(cx)
                                            .read(cx)
                                            .applied_device_settings()
                                            .microphone
                                            .map(|settings| {
                                                DeviceFormat::Microphone(settings).label()
                                            })
                                    } else {
                                        mic.describe().map(tr::main::device::format::available)
                                    },
                                    selected,
                                    selected.then(|| self.device_microphone_level.clone()),
                                    cx.listener(move |this, _, _window, cx| {
                                        if !this.device_changes_allowed(cx) {
                                            return;
                                        }
                                        this.ensure_media_access(OSPermission::Microphone, cx);
                                        this.set_microphone_selection(Some(chosen.clone()), cx);
                                        this.close_panel(cx);
                                    }),
                                )
                                .flex_1()
                                .min_w_0(),
                            )
                            .child(self.render_device_format_button(
                                DeviceFormatTarget::Microphone(mic.name.clone()),
                                cx,
                            ))
                            .into_any_element(),
                    );
                }
            }
        }

        if matched == 0 {
            rows.push(self.render_empty_state(menu.empty_message(!self.search.is_empty())));
        }

        list.children(rows)
    }

    fn open_device_formats(&mut self, target: DeviceFormatTarget, cx: &mut Context<Self>) {
        if !self.device_changes_allowed(cx) {
            return;
        }
        self.device_format_generation += 1;
        let generation = self.device_format_generation;
        self.device_format_value = Some(match &target {
            DeviceFormatTarget::Camera(camera) => DeviceFormat::Camera(
                crate::store::RecordingDeviceSettings::for_camera(
                    &camera.device_id,
                    camera.model_id.as_ref(),
                )
                .unwrap_or_default(),
            ),
            DeviceFormatTarget::Microphone(name) => DeviceFormat::Microphone(
                crate::store::RecordingDeviceSettings::for_microphone(name).unwrap_or_default(),
            ),
        });
        self.device_format_target = Some(target.clone());
        self.device_format_notice = None;
        let should_query = match &target {
            DeviceFormatTarget::Camera(camera) => camera.formats.is_empty(),
            DeviceFormatTarget::Microphone(_) => true,
        };
        self.device_formats = match &target {
            DeviceFormatTarget::Camera(camera) => Some(Ok(std::iter::once(DeviceFormat::Camera(
                Default::default(),
            ))
            .chain(
                camera
                    .formats
                    .iter()
                    .map(|format| DeviceFormat::Camera(format.settings())),
            )
            .collect())),
            _ => None,
        };
        if should_query {
            cx.spawn(async move |this, cx| {
                let (formats, camera_formats, notice) = cx
                    .background_executor()
                    .spawn(async move {
                        match target {
                            DeviceFormatTarget::Camera(camera) => {
                                let formats = devices::camera_formats(&camera.device_id);
                                let metadata = formats.as_ref().ok().cloned();
                                let (choices, notice) = camera_format_choices(formats);
                                (Ok(choices), metadata, notice)
                            }
                            DeviceFormatTarget::Microphone(name) => {
                                let formats = devices::microphone_formats(&name).map(|formats| {
                                    std::iter::once(DeviceFormat::Microphone(Default::default()))
                                        .chain(formats.into_iter().map(DeviceFormat::Microphone))
                                        .collect()
                                });
                                (formats, None, None)
                            }
                        }
                    })
                    .await;
                this.update(cx, |this, cx| {
                    if this.device_format_generation != generation
                        || this.device_format_target.is_none()
                    {
                        return;
                    }
                    if let Some(formats) = camera_formats
                        && let Some(DeviceFormatTarget::Camera(camera)) = &this.device_format_target
                        && !this.devices.update_camera_formats(camera, formats)
                    {
                        this.device_formats =
                            Some(Err(tr::main::device::format::camera_changed().into()));
                        cx.notify();
                        return;
                    }
                    this.device_format_notice = notice;
                    this.device_formats = Some(formats);
                    cx.notify();
                })
                .ok();
            })
            .detach();
        }
        cx.notify();
    }

    fn device_format_target_selected(&self, target: &DeviceFormatTarget) -> bool {
        match target {
            DeviceFormatTarget::Camera(camera) => self
                .camera
                .as_ref()
                .is_some_and(|selected| selected.device_id == camera.device_id),
            DeviceFormatTarget::Microphone(name) => self
                .microphone
                .as_ref()
                .is_some_and(|selected| selected.name == *name),
        }
    }

    fn choose_device_format(&mut self, format: DeviceFormat, cx: &mut Context<Self>) {
        if !self.device_changes_allowed(cx) {
            return;
        }
        let Some(target) = self.device_format_target.clone() else {
            return;
        };
        if !self
            .device_formats
            .as_ref()
            .and_then(|formats| formats.as_ref().ok())
            .is_some_and(|formats| formats.contains(&format))
        {
            return;
        }
        self.device_format_notice = None;
        if !self.device_format_target_selected(&target) {
            self.complete_device_format_save(&target, format, cx);
            return;
        }
        let feeds = Feeds::global(cx);
        let epoch = feeds.update(cx, |feeds, cx| match format {
            DeviceFormat::Camera(settings) => {
                feeds.set_camera_with_settings(feeds.camera.clone(), Some(settings), cx)
            }
            DeviceFormat::Microphone(settings) => {
                feeds.set_microphone_with_settings(feeds.microphone.clone(), Some(settings), cx)
            }
        });
        self.device_format_pending = Some(PendingDeviceFormat {
            target,
            format,
            epoch,
        });
        let result = match format {
            DeviceFormat::Camera(_) => feeds.read(cx).camera_configuration_result(epoch),
            DeviceFormat::Microphone(_) => feeds.read(cx).microphone_configuration_result(epoch),
        };
        self.finish_device_format_change(result, cx);
        cx.notify();
    }

    fn finish_device_format_change(
        &mut self,
        result: Option<Result<(), String>>,
        cx: &mut Context<Self>,
    ) {
        let still_owned = !self.device_restore_suspended
            && self.session.read(cx).phase == Phase::Idle
            && self
                .device_format_pending
                .as_ref()
                .is_some_and(|pending| self.device_format_target_selected(&pending.target));
        let Some(result) = complete_format_request(
            &mut self.device_format_pending,
            result,
            still_owned,
            save_device_format,
        ) else {
            return;
        };
        match result {
            Ok(format) => {
                self.device_format_value = Some(format);
                self.device_format_notice = None;
            }
            Err(error) => {
                self.device_format_notice = Some(error.clone());
                self.session.update(cx, |session, cx| {
                    session.error = Some(tr::main::device::format::apply_failed(&error));
                    cx.notify();
                });
            }
        }
        cx.notify();
    }

    fn complete_device_format_save(
        &mut self,
        target: &DeviceFormatTarget,
        format: DeviceFormat,
        cx: &mut Context<Self>,
    ) {
        if save_device_format(target, format) {
            self.device_format_value = Some(format);
            self.device_format_notice = None;
        } else {
            let error = tr::main::device::format::save_failed().to_string();
            self.device_format_notice = Some(error.clone());
            self.session.update(cx, |session, cx| {
                session.error = Some(error);
                cx.notify();
            });
        }
        cx.notify();
    }

    fn render_device_formats(&self, menu: DeviceMenu, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;
        let applied = self
            .device_format_target
            .as_ref()
            .filter(|target| self.device_format_target_selected(target))
            .and_then(|target| {
                let settings = Feeds::global(cx).read(cx).applied_device_settings();
                match target {
                    DeviceFormatTarget::Camera(_) => settings.camera.map(DeviceFormat::Camera),
                    DeviceFormatTarget::Microphone(_) => {
                        settings.microphone.map(DeviceFormat::Microphone)
                    }
                }
            });
        let list = div()
            .flex()
            .flex_col()
            .gap(px(4.))
            .w_full()
            .child(
                div()
                    .text_size(px(12.))
                    .text_color(theme.gray_11)
                    .pb(px(8.))
                    .child(tr::main::device::format::preferred()),
            )
            .when_some(applied, |list, format| {
                list.child(
                    div()
                        .text_size(px(12.))
                        .text_color(theme.gray_11)
                        .pb(px(8.))
                        .child(tr::main::device::format::current(format.label())),
                )
            });
        let list = match &self.device_formats {
            None => list.child(self.render_empty_state(tr::main::device::format::loading())),
            Some(Err(error)) => list.child(self.render_empty_state(error.clone())),
            Some(Ok(formats)) => {
                list.children(formats.iter().enumerate().map(|(index, format)| {
                    let format = *format;
                    self.render_device_list_row(
                        SharedString::from(format!("device-format-{index}")),
                        menu.icon(),
                        format.label(),
                        None,
                        self.device_format_value == Some(format),
                        None,
                        cx.listener(move |this, _, _, cx| this.choose_device_format(format, cx)),
                    )
                }))
            }
        };
        list.when(self.device_format_pending.is_some(), |list| {
            list.child(self.render_empty_state(tr::main::device::format::applying()))
        })
        .when_some(self.device_format_notice.as_ref(), |list, notice| {
            list.child(self.render_empty_state(notice.clone()))
        })
    }

    fn render_device_format_button(
        &self,
        target: DeviceFormatTarget,
        cx: &mut Context<Self>,
    ) -> gpui::Stateful<gpui::Div> {
        let theme = self.theme;
        let enabled = self.device_changes_allowed(cx);
        let name = match &target {
            DeviceFormatTarget::Camera(camera) => format!("camera-format-{}", camera.device_id),
            DeviceFormatTarget::Microphone(name) => format!("mic-format-{name}"),
        };
        div()
            .id(SharedString::from(name))
            .flex()
            .items_center()
            .justify_center()
            .w(px(32.))
            .flex_shrink_0()
            .rounded(px(10.))
            .when(enabled, |button| {
                button
                    .cursor_pointer()
                    .hover(move |style| style.bg(glass_hover(theme)))
                    .active(move |style| style.bg(glass_press(theme)))
            })
            .when(!enabled, |button| button.opacity(0.45))
            .child(
                svg()
                    .path("icons/settings-2.svg")
                    .size(px(15.))
                    .text_color(theme.gray_10),
            )
            .on_click(
                cx.listener(move |this, _, _, cx| this.open_device_formats(target.clone(), cx)),
            )
    }

    /// The width one target card gets, computed rather than flexed.
    ///
    /// Two columns inside `render_body`'s `px-[13px]` and the panel body's
    /// `px-2`, with an 8px gutter: `(W - 26 - 16 - 8) / 2`. A `flex_1` card
    /// carrying three lines of text re-measures its children every layout pass,
    /// which is fine for four cards and catastrophic for the forty a window
    /// picker routinely holds, so the grid states the width and the cards take
    /// it with `flex_none`.
    fn target_card_width(&self) -> f32 {
        (MAIN_WINDOW_WIDTH - 50.) / 2.
    }

    /// `TargetMenuGrid`: two columns of cards.
    ///
    /// Each card leads with a live 320x180 capture of the display or window
    /// (`target_thumbnails`), falling back to the target's icon on `gray-4` --
    /// which is exactly what the Tauri card does before its thumbnail arrives
    /// or when the `<img>` errors (`TargetCard.tsx:367-374`).
    fn render_target_grid(&self, target: TargetType, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;
        let grid = div().flex().flex_col().gap(px(8.)).w_full();

        if self.enumerating {
            return grid.child(
                div()
                    .py(px(24.))
                    .w_full()
                    .text_size(px(14.))
                    .text_color(theme.gray_11)
                    .child(tr::common::loading()),
            );
        }

        let mut cards: Vec<gpui::AnyElement> = Vec::new();

        match target {
            TargetType::Display => {
                for display in self
                    .devices
                    .displays
                    .iter()
                    .filter(|display| self.matches_search(&display.label))
                {
                    let selected = self
                        .selected_display
                        .as_ref()
                        .is_some_and(|current| current.id == display.id);
                    let chosen = display.clone();

                    cards.push(
                        self.render_target_card(
                            SharedString::from(format!("display-{}", display.id)),
                            self.thumbnails.display(&display.id),
                            "icons/screen.svg",
                            display.label.clone(),
                            None,
                            display.describe_refresh_rate(),
                            selected,
                            cx.listener(move |this, _, _window, cx| {
                                this.selected_display = Some(chosen.clone());
                                this.target = Some(TargetType::Display);
                                this.close_panel(cx);
                                this.sync_overlays(cx);
                            }),
                        )
                        .into_any_element(),
                    );
                }
            }
            TargetType::Window => {
                for window in self
                    .devices
                    .windows
                    .iter()
                    .filter(|w| self.matches_search(&w.label) || self.matches_search(&w.app))
                {
                    let selected = self
                        .selected_window
                        .as_ref()
                        .is_some_and(|current| current.id == window.id);
                    let chosen = window.clone();

                    cards.push(
                        self.render_target_card(
                            SharedString::from(format!("window-{}", window.id)),
                            self.thumbnails.window(&window.id),
                            "icons/window.svg",
                            window.label.clone(),
                            Some(window.app.clone()),
                            window.describe_metadata(),
                            selected,
                            cx.listener(move |this, _, _window, cx| {
                                this.selected_window = Some(chosen.clone());
                                this.target = Some(TargetType::Window);
                                this.close_panel(cx);
                                this.sync_overlays(cx);
                                this.focus_selected_window(cx);
                            }),
                        )
                        .into_any_element(),
                    );
                }
            }
            // Area and Camera Only have nothing to pick between.
            TargetType::Area | TargetType::CameraOnly => {}
        }

        if cards.is_empty() {
            return grid.child(
                self.render_empty_state(match (target, self.search.is_empty()) {
                    (TargetType::Display, true) => tr::main::target::no_displays(),
                    (TargetType::Display, false) => tr::main::target::no_matching_displays(),
                    (_, true) => tr::main::target::no_windows(),
                    (_, false) => tr::main::target::no_matching_windows(),
                }),
            );
        }

        // Two columns, laid out as rows of two. The cards carry their own
        // width (`target_card_width`), so a lone trailing card keeps half the
        // row without needing a spacer to hold the other half open.
        grid.children(
            cards
                .into_iter()
                .collect::<Vec<_>>()
                .chunks_mut(2)
                .map(|pair| {
                    let mut row = div().flex().flex_row().gap(px(8.)).w_full().items_stretch();
                    for card in pair.iter_mut() {
                        row = row.child(std::mem::replace(card, div().into_any_element()));
                    }
                    row
                })
                .collect::<Vec<_>>(),
        )
    }

    /// `TargetCard`: a 76px (`h-19`) thumbnail area over three 11px lines.
    #[allow(clippy::too_many_arguments)]
    fn render_target_card(
        &self,
        id: SharedString,
        thumb: target_thumbnails::TargetThumb,
        icon: &'static str,
        label: String,
        subtitle: Option<String>,
        metadata: Option<String>,
        selected: bool,
        on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static,
    ) -> gpui::Stateful<gpui::Div> {
        let theme = self.theme;

        div()
            .id(id)
            .when(self.keyboard_nav, ui::keyboard_nav::stop)
            .role(gpui::Role::Button)
            .aria_label(label.clone())
            .aria_selected(selected)
            .flex()
            .flex_col()
            .w(px(self.target_card_width()))
            .flex_none()
            .min_w_0()
            .overflow_hidden()
            .rounded(px(8.))
            .border_1()
            .border_color(if selected {
                Hsla::from(theme.blue_8)
            } else {
                gpui::transparent_black()
            })
            .bg(theme.body_fill(3))
            .child(target_thumbnails::render_thumbnail_slot(thumb, icon, theme))
            .child(
                div()
                    .flex()
                    .flex_col()
                    .w_full()
                    .min_w_0()
                    .px(px(8.))
                    .py(px(6.))
                    .text_size(px(11.))
                    .child(
                        div()
                            .w_full()
                            .truncate()
                            .font_weight(FontWeight::MEDIUM)
                            .text_color(theme.gray_12)
                            .child(label),
                    )
                    .children(subtitle.map(|subtitle| {
                        div()
                            .w_full()
                            .truncate()
                            .text_color(theme.gray_11)
                            .child(subtitle)
                    }))
                    .children(metadata.map(|metadata| {
                        div()
                            .w_full()
                            .truncate()
                            .text_color(theme.gray_10)
                            .child(metadata)
                    })),
            )
            .hover(|style| style.bg(theme.body_hover_fill(4)))
            .on_click(on_click)
    }

    /// `ModeInfoPanel`. Its copy is deliberately not the copy in `MODE_BUTTONS`
    /// -- the hover cards and this panel describe the modes differently, so
    /// both sets of strings are carried.
    fn render_mode_info(&self, cx: &mut Context<Self>) -> gpui::Div {
        let theme = self.theme;
        let effective_mode = self.effective_mode(cx);
        let locked = self.session.read(cx).editor_recording_target().is_some();

        div().flex().flex_col().gap(px(8.)).w_full().children(
            [Mode::Instant, Mode::Studio, Mode::Screenshot].map(|mode| {
                let selected = mode == effective_mode;

                div()
                    .id(SharedString::from(mode.panel_title()))
                    .when(locked && !selected, |this| this.opacity(0.5))
                    .flex()
                    .flex_row()
                    .items_center()
                    .gap(px(12.))
                    .p(px(12.))
                    .w_full()
                    .rounded(px(12.))
                    .border_2()
                    .border_color(if selected {
                        Hsla::from(theme.blue_9)
                    } else if theme.is_dark() {
                        theme.body_border(5)
                    } else {
                        theme.body_border(4)
                    })
                    .bg(if selected {
                        theme.tile_selected_bg()
                    } else if theme.is_dark() {
                        theme.body_fill(3)
                    } else {
                        theme.body_fill(2)
                    })
                    .child(
                        svg()
                            .path(mode.icon())
                            .size(px(20.))
                            .flex_shrink_0()
                            .text_color(if selected {
                                theme.blue_11
                            } else {
                                theme.gray_12
                            }),
                    )
                    .child(
                        div()
                            .flex()
                            .flex_col()
                            .gap(px(2.))
                            .flex_1()
                            .min_w_0()
                            .child(
                                div()
                                    .text_size(px(14.))
                                    // `text-sm font-semibold`
                                    // (`ModeInfoPanel.tsx:106`). `font-semibold`
                                    // renders 700: no 600 face is loaded over
                                    // there (`ui-solid/vite.js:31-33`).
                                    .font_weight(FontWeight::BOLD)
                                    .text_color(if selected {
                                        theme.blue_11
                                    } else {
                                        theme.gray_12
                                    })
                                    .child(mode.display_name()),
                            )
                            .child(
                                div()
                                    .text_size(px(12.))
                                    .text_color(theme.gray_11)
                                    .child(mode.panel_description()),
                            ),
                    )
                    .hover(|style| style.bg(theme.body_hover_fill(4)))
                    .on_click(cx.listener(move |this, _, _window, cx| {
                        this.set_mode(mode, cx);
                        this.close_panel(cx);
                    }))
            }),
        )
    }

    fn render_access_banner(
        &self,
        menu: DeviceMenu,
        cx: &mut Context<Self>,
    ) -> Option<gpui::AnyElement> {
        let permission = menu.permission();
        let access = MediaAccess::global(cx);
        let access = access.read(cx);
        if access.allowed(permission) {
            return None;
        }
        let theme = self.theme;
        let denied = access.status(permission) == crate::permissions::OSPermissionStatus::Denied;
        let requesting = access.requesting(permission);
        let noun = match menu {
            DeviceMenu::Camera => "camera",
            DeviceMenu::Microphone => "microphone",
        };
        let message = match (menu, denied, requesting) {
            (DeviceMenu::Camera, true, _) => tr::main::device::access::camera_denied(),
            (DeviceMenu::Microphone, true, _) => tr::main::device::access::microphone_denied(),
            (_, false, true) => tr::common::choose_allow_prompt(),
            (DeviceMenu::Camera, false, false) => tr::main::device::access::camera_needed(),
            (DeviceMenu::Microphone, false, false) => tr::main::device::access::microphone_needed(),
        };
        let label = if denied {
            tr::main::device::access::open_system_settings()
        } else if requesting {
            tr::main::device::pill::asking()
        } else {
            crate::media_access::action_label(access.status(permission))
        };
        Some(
            div()
                .flex()
                .flex_row()
                .items_center()
                .gap(px(8.))
                .w_full()
                .px(px(10.))
                .py(px(8.))
                .mb(px(4.))
                .rounded(px(10.))
                .bg(theme.amber_3)
                .border_1()
                .border_color(theme.amber_6)
                .child(
                    div()
                        .flex_1()
                        .min_w_0()
                        .text_size(px(12.))
                        .line_height(px(16.))
                        .text_color(theme.amber_11)
                        .child(message),
                )
                .child(
                    ui::Button::body(
                        &theme,
                        SharedString::from(format!("{noun}-access")),
                        ui::ButtonVariant::Primary,
                        ui::ButtonSize::Xs,
                    )
                    .label(label)
                    .disabled(requesting)
                    .on_click(cx.listener(move |this, _, _, cx| {
                        this.ensure_media_access(permission, cx);
                    })),
                )
                .into_any_element(),
        )
    }

    fn render_empty_state(&self, message: impl Into<SharedString>) -> gpui::AnyElement {
        div()
            .py(px(24.))
            .w_full()
            .text_size(px(14.))
            .text_color(self.theme.gray_11)
            .child(message.into())
            .into_any_element()
    }

    /// `CameraListItem` / `MicrophoneListItem`: `px-3 py-2.5`, `rounded-lg`,
    /// 14px label over an optional 11px detail line indented `pl-7`.
    ///
    /// Selection is `bg-blue-500` with white text -- note that is the custom
    /// `--blue-500`, not `blue-9` used by the pills; the two are different
    /// colours.
    // Mirrors the web list-item's prop list; a struct would rename, not reduce.
    #[allow(clippy::too_many_arguments)]
    fn render_device_list_row(
        &self,
        id: SharedString,
        icon: &'static str,
        label: String,
        detail: Option<String>,
        selected: bool,
        // `MicrophoneListItem`'s live level bar. Only the selected
        // microphone row gets one.
        audio_level: Option<Entity<ui::MicrophoneLevel>>,
        on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static,
    ) -> gpui::Stateful<gpui::Div> {
        let theme = self.theme;
        let accent = Hsla::from(theme.blue_500);

        div()
            .id(id)
            .when(self.keyboard_nav, ui::keyboard_nav::stop)
            .role(gpui::Role::Button)
            .aria_label(label.clone())
            .aria_selected(selected)
            .relative()
            .flex()
            .flex_row()
            .items_center()
            .gap(px(10.))
            .px(px(10.))
            .py(px(7.))
            .min_h(px(40.))
            .w_full()
            .rounded(px(10.))
            .cursor_pointer()
            .when(selected, |this| this.bg(glass_selected(theme)))
            .when(!selected, |this| {
                this.hover(move |style| style.bg(glass_hover(theme)))
                    .active(move |style| style.bg(glass_press(theme)))
            })
            .child(
                svg()
                    .path(icon)
                    .size(px(16.))
                    .flex_shrink_0()
                    .text_color(theme.gray_11),
            )
            .child(
                div()
                    .flex()
                    .flex_col()
                    .flex_1()
                    .min_w_0()
                    .gap(px(1.))
                    .child(
                        div()
                            .truncate()
                            .text_size(px(13.))
                            .text_color(theme.gray_12)
                            .child(label),
                    )
                    .children(detail.map(|detail| {
                        div()
                            .truncate()
                            .text_size(px(11.))
                            .text_color(theme.gray_10)
                            .child(detail)
                    }))
                    .when_some(audio_level.filter(|_| selected), |this, level| {
                        this.child(level)
                    }),
            )
            .when(selected, |this| {
                this.child(
                    svg()
                        .path("icons/check.svg")
                        .size(px(14.))
                        .flex_shrink_0()
                        .text_color(accent),
                )
            })
            .on_click(on_click)
    }

    fn render_logo_row(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let recording_clip = self.session.read(cx).editor_recording_target().is_some();
        div()
            .flex()
            .flex_row()
            .items_center()
            .justify_between()
            .mt(px(4.))
            .flex_shrink_0()
            .child(
                // `flex items-center space-x-1` around the logo and its badge.
                div()
                    .flex()
                    .flex_row()
                    .items_center()
                    .gap(px(4.))
                    .when(!recording_clip, |this| {
                        this.child(self.render_logo())
                            .child(self.render_plan_badge())
                    })
                    .when(recording_clip, |this| {
                        this.child(
                            div()
                                .flex()
                                .flex_col()
                                .gap(px(4.))
                                .child(div().text_size(px(13.)).child(tr::main::clip::record_new()))
                                .child(
                                    div()
                                        .text_size(px(10.))
                                        .text_color(self.theme.gray_11)
                                        .child(tr::main::clip::add_to_project()),
                                ),
                        )
                    }),
            )
            .child(
                div()
                    .flex()
                    .flex_col()
                    .items_center()
                    .gap(px(4.))
                    .child(self.render_mode_pill(cx))
                    .child(
                        div()
                            .text_size(px(9.))
                            .line_height(px(11.))
                            .text_color(self.theme.gray_11)
                            .child(self.effective_mode(cx).header_title()),
                    ),
            )
    }

    /// The plan badge: Personal is `text-[0.6rem] ml-2 rounded-lg border
    /// border-gray-5 px-1 py-0.5 bg-gray-3 hover:bg-gray-5`, a button; Pro and
    /// Commercial are a non-interactive span on `--blue-400`. Which one
    /// applies is [`PlanBadge::current`], the license query's resolution.
    fn render_plan_badge(&self) -> impl IntoElement {
        let theme = self.theme;

        let badge = div()
            .id("plan-badge")
            .flex()
            .items_center()
            .flex_shrink_0()
            .ml(px(8.))
            .px(px(4.))
            .py(px(2.))
            .rounded(px(8.))
            .text_size(px(9.6));

        match self.plan {
            PlanBadge::Personal => badge
                .border_1()
                .border_color(theme.body_border(5))
                .bg(theme.body_fill(3))
                .text_color(theme.gray_12)
                .child(tr::main::plan::personal())
                .hover(|style| style.bg(theme.body_hover_fill(5)))
                .on_click(|_, _, cx| {
                    cx.open_url(crate::auth::PRICING_URL);
                }),
            PlanBadge::Pro | PlanBadge::Commercial => badge
                .bg(theme.blue_9)
                .text_color(gpui::white())
                .child(if self.plan == PlanBadge::Commercial {
                    tr::main::plan::commercial()
                } else {
                    "Pro"
                }),
        }
    }

    /// This goes through `img()`, not `svg()`. The two take different paths in
    /// gpui: `svg()` keeps only the alpha and tints it with one colour, which
    /// would flatten the badge, the three blue rings and the wordmark into a
    /// single silhouette, whereas `img()` rasterises through resvg and keeps
    /// the colour. `img()` also renders at `SMOOTH_SVG_SCALE_FACTOR` (2x), so
    /// the 103px-wide source becomes a 206px raster -- more than the 168 device
    /// pixels an 84px lockup needs on a 2x display.
    ///
    /// The app ships two files rather than recolouring one, so this picks the
    /// same way it does.
    fn render_logo(&self) -> impl IntoElement {
        img(if self.theme.is_dark() {
            "icons/logo-full-dark.svg"
        } else {
            "icons/logo-full.svg"
        })
        .w(px(84.))
        .h(px(84. * 40. / 103.))
        .flex_shrink_0()
    }

    fn clear_mode_hover(&mut self) {
        self.mode_hover_task = None;
        self.mode_hover = ModeHoverState::default();
    }

    fn update_mode_hover(&mut self, mode: Mode, card: bool, hovered: bool, cx: &mut Context<Self>) {
        let previous = self.mode_hover.target();
        self.mode_hover.update(mode, card, hovered);
        let target = self.mode_hover.target();
        if target == previous {
            return;
        }
        self.mode_hover_task = None;
        if target == self.mode_hover.visible {
            return;
        }
        if target.is_some() {
            self.mode_hover.visible = None;
            cx.notify();
        }
        let delay = std::time::Duration::from_millis(if target.is_some() { 120 } else { 80 });
        self.mode_hover_task = Some(cx.spawn(async move |this, cx| {
            cx.background_executor().timer(delay).await;
            let _ = this.update(cx, |this, cx| {
                if this.mode_hover.target() == target {
                    this.mode_hover.visible = target;
                    cx.notify();
                }
            });
        }));
    }

    fn render_mode_hover(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> {
        let mode = self.mode_hover.visible?;
        if self.panel.is_some() || self.session.read(cx).phase != Phase::Idle {
            return None;
        }
        let index = match mode {
            Mode::Instant => 0,
            Mode::Studio => 1,
            Mode::Screenshot => 2,
        };
        let bounds = self.mode_hover_bounds[index].get()?;
        Some(
            gpui::anchored()
                .anchor(gpui::Anchor::TopRight)
                .position(gpui::point(bounds.right(), bounds.bottom() + px(12.)))
                .snap_to_window_with_margin(px(12.))
                .child(
                    div()
                        .id("mode-hover-card")
                        .occlude()
                        .on_hover(cx.listener(move |this, hovered, _, cx| {
                            this.update_mode_hover(mode, true, *hovered, cx);
                        }))
                        .on_click(cx.listener(|this, _, _, cx| {
                            this.clear_mode_hover();
                            cx.notify();
                        }))
                        .child(ModeHoverCard {
                            mode,
                            theme: self.theme,
                        }),
                ),
        )
    }

    /// `Mode.tsx`: `p-1.5 gap-2 rounded-full border border-gray-5 bg-gray-3`,
    /// 28px round buttons (`size-7`). Selected gets `bg-gray-7` plus a 2px
    /// `blue-500` ring offset 1px against `gray-1`.
    fn render_mode_pill(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let selected_mode = self.effective_mode(cx);
        let locked = self.session.read(cx).editor_recording_target().is_some();

        let button = |mode: Mode, id: &'static str, index: usize| {
            let selected = mode == selected_mode;
            let bounds = self.mode_hover_bounds[index].clone();
            div()
                .id(SharedString::from(id))
                .tab_index(0)
                .when(self.keyboard_nav, ui::keyboard_nav::ring)
                .role(gpui::Role::Button)
                .aria_label(mode.panel_title())
                .aria_selected(selected)
                .relative()
                .on_hover(cx.listener(move |this, hovered, _, cx| {
                    this.update_mode_hover(mode, false, *hovered, cx);
                }))
                .when(locked && !selected, |this| this.opacity(0.5))
                .flex()
                .items_center()
                .justify_center()
                .size(px(28.))
                .rounded_full()
                .bg(if selected {
                    Hsla::from(theme.gray_7)
                } else {
                    theme.body_fill(3)
                })
                .when(selected, |this| {
                    // `ring-2 ring-blue-500 ring-offset-1 ring-offset-gray-1`.
                    this.border_2()
                        .border_color(theme.blue_500)
                        .shadow(vec![gpui::BoxShadow {
                            color: theme.ring_offset(),
                            offset: gpui::point(px(0.), px(0.)),
                            blur_radius: px(0.),
                            spread_radius: px(1.),
                            inset: false,
                        }])
                })
                .child(
                    gpui::canvas(move |value, _, _| bounds.set(Some(value)), |_, _, _, _| {})
                        .absolute()
                        .size_full(),
                )
                .child(
                    svg()
                        .path(mode.icon())
                        .size(px(mode.icon_size()))
                        .text_color(theme.gray_12),
                )
                .hover(|style| style.bg(theme.body_hover_fill(7)))
                .on_click(cx.listener(move |this, _, _window, cx| {
                    if this.session.read(cx).editor_recording_target().is_some() {
                        return;
                    }
                    this.set_mode(mode, cx);
                    cx.defer(app_windows::refresh_target_overlays);
                }))
        };

        div()
            .relative()
            .flex()
            .flex_row()
            .items_center()
            .gap(px(8.))
            .p(px(6.))
            .rounded_full()
            .border_1()
            .border_color(theme.body_border(5))
            .bg(theme.body_fill(3))
            .child(button(Mode::Instant, "mode-instant", 0))
            .child(button(Mode::Studio, "mode-studio", 1))
            .child(button(Mode::Screenshot, "mode-screenshot", 2))
            // `absolute -left-1.5 -top-2 p-1 rounded-full bg-gray-5`, hanging
            // off the pill's top-left corner.
            .child(
                div()
                    .id("mode-info")
                    .tab_index(0)
                    .when(self.keyboard_nav, ui::keyboard_nav::ring)
                    .role(gpui::Role::Button)
                    .aria_label(tr::main::mode::info_tooltip())
                    .tooltip(move |_, cx| {
                        ui::Tooltip::new(&theme, tr::main::mode::info_tooltip()).view(cx)
                    })
                    .tooltip_show_delay(ui::TOOLTIP_SHOW_DELAY)
                    .absolute()
                    .left(px(-6.))
                    .top(px(-8.))
                    .flex()
                    .items_center()
                    .justify_center()
                    .p(px(4.))
                    .rounded_full()
                    .bg(theme.body_fill(5))
                    .child(
                        svg()
                            .path("icons/info.svg")
                            .size(px(10.))
                            .text_color(theme.gray_12),
                    )
                    .hover(|style| style.opacity(0.5))
                    .on_click(cx.listener(|this, _, window, cx| {
                        // What shipping new-main actually does: it passes
                        // `onInfoClick` into `Mode.tsx`, so the dot opens the
                        // in-window info panel -- `showWindow("ModeSelect")` is
                        // `Mode.tsx`'s *fallback*, and nothing in the shipping
                        // frontend reaches it. The standalone window exists
                        // here for parity (`CAP_GPUI_AUTO_MODE_SELECT` opens
                        // it), but the dot mirrors the observed behavior.
                        this.open_panel(Panel::ModeInfo, window, cx);
                    })),
            )
    }

    fn render_targets(&self, cx: &mut Context<Self>) -> impl IntoElement {
        div()
            .flex()
            .flex_col()
            .gap(px(8.))
            .w_full()
            .flex_shrink_0()
            .child(
                div()
                    .px(px(4.))
                    .text_size(px(10.))
                    .line_height(px(14.))
                    .text_color(self.theme.gray_11)
                    .child(tr::main::target::heading()),
            )
            .child(
                div()
                    .flex()
                    .flex_row()
                    .gap(px(8.))
                    .items_stretch()
                    .w_full()
                    .child(self.render_split_target(TargetType::Display, cx))
                    .child(self.render_split_target(TargetType::Window, cx)),
            )
            .child(
                div()
                    .flex()
                    .flex_row()
                    .gap(px(8.))
                    .items_stretch()
                    .w_full()
                    .child(self.render_target_tile(TargetType::Area, cx))
                    .child(self.render_target_tile(TargetType::CameraOnly, cx)),
            )
    }

    /// Display and Window are split controls: the tile plus a 28px
    /// (`w-7`) chevron button, sharing one rounded border.
    fn render_split_target(&self, target: TargetType, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let selected = self.target == Some(target);
        let dropdown_id = SharedString::from(format!("{}-dropdown", target.label()));

        div()
            .flex()
            .flex_1()
            .overflow_hidden()
            .rounded(px(10.))
            .map(|this| glass_button(this, theme, selected))
            .child(self.target_button_inner(target, true, cx))
            .child(
                div()
                    .id(dropdown_id.clone())
                    .when(self.keyboard_nav, ui::keyboard_nav::stop)
                    .role(gpui::Role::Button)
                    .aria_label(match target {
                        TargetType::Display => "Choose a display",
                        _ => "Choose a window",
                    })
                    .flex()
                    .w(px(28.))
                    .rounded_r(px(9.))
                    .flex_shrink_0()
                    .items_center()
                    .justify_center()
                    .child(
                        div()
                            .absolute()
                            .left_0()
                            .top(px(10.))
                            .bottom(px(10.))
                            .w(px(1.))
                            .bg(if selected {
                                Theme::with_alpha(theme.blue_9, 0.25)
                            } else {
                                glass_tint(theme, 0.08, 0.1)
                            }),
                    )
                    .relative()
                    .child(
                        svg()
                            .path("icons/chevron-down.svg")
                            .size(px(14.))
                            .text_color(if selected {
                                theme.blue_10
                            } else {
                                theme.gray_10
                            }),
                    )
                    .hover(move |style| style.bg(capture_hover_fill(theme, selected)))
                    .active(move |style| style.bg(capture_press_fill(theme, selected)))
                    .tooltip(move |_, cx| {
                        ui::Tooltip::new(
                            &theme,
                            match target {
                                TargetType::Display => tr::main::target::choose_display(),
                                TargetType::Window => tr::main::target::choose_window(),
                                _ => unreachable!(),
                            },
                        )
                        .view(cx)
                    })
                    .tooltip_show_delay(ui::TOOLTIP_SHOW_DELAY)
                    .on_click(cx.listener(move |this, _, window, cx| {
                        this.open_panel(Panel::Target(target), window, cx);
                    })),
            )
    }

    /// Area and Camera Only are plain tiles with their own border.
    fn render_target_tile(&self, target: TargetType, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = self.theme;
        let selected = self.target == Some(target);

        div()
            .flex()
            .flex_1()
            .overflow_hidden()
            .rounded(px(10.))
            .map(|this| glass_button(this, theme, selected))
            .child(self.target_button_inner(target, false, cx))
    }

    fn target_button_inner(
        &self,
        target: TargetType,
        split: bool,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let theme = self.theme;
        let selected = self.target == Some(target);
        let icon_color = if selected {
            theme.blue_10
        } else {
            theme.gray_10
        };
        let label_color = if selected {
            theme.blue_11
        } else {
            theme.gray_12
        };

        let icon = svg()
            .path(target.icon())
            .size(px(20.))
            .flex_shrink_0()
            .text_color(icon_color);

        let base = div()
            .id(SharedString::from(target.label()))
            .when(self.keyboard_nav, ui::keyboard_nav::stop)
            .role(gpui::Role::Button)
            .aria_label(target.label())
            .aria_selected(selected)
            .flex()
            .flex_1()
            .py(px(8.))
            .when(split, |this| this.rounded_l(px(9.)))
            .when(!split, |this| this.rounded(px(9.)))
            .hover(move |style| style.bg(capture_hover_fill(theme, selected)))
            .active(move |style| style.bg(capture_press_fill(theme, selected)))
            .on_click(cx.listener(move |this, _, _window, cx| {
                // `toggleTargetMode`: clicking the armed tile again is a
                // cancel, which takes the overlays down with it.
                this.target = if this.target == Some(target) {
                    None
                } else {
                    Some(target)
                };
                this.sync_overlays(cx);
                cx.notify();
            }));

        base.flex_col()
            .items_center()
            .justify_end()
            .gap(px(4.))
            // `pl-5` on the split controls when compact, to keep the icon
            // optically centred against the chevron on the right.
            .when(split, |this| this.pl(px(20.)))
            .child(icon)
            .child(
                div()
                    .text_size(px(12.))
                    .line_height(px(16.))
                    .text_color(label_color)
                    .child(target.display_label()),
            )
    }

    /// `BaseControls`: camera, microphone, system audio.
    fn render_base_controls(&self, cx: &mut Context<Self>) -> impl IntoElement {
        div()
            .flex()
            .flex_col()
            .gap(px(8.))
            .w_full()
            .child(
                div()
                    .px(px(4.))
                    .text_size(px(10.))
                    .line_height(px(14.))
                    .text_color(self.theme.gray_11)
                    .child(tr::main::device::heading()),
            )
            .child(
                div().flex().flex_col().child(
                    self.render_device_row(
                        "camera-row",
                        "icons/camera.svg",
                        self.camera
                            .as_ref()
                            .map(|camera| camera_label(&camera.label))
                            .unwrap_or_else(|| tr::main::device::no_camera().into()),
                        if self.camera.is_some()
                            && let Some(pill) = self.access_pill(OSPermission::Camera, cx)
                        {
                            pill
                        } else if self.camera.is_some() {
                            if self.devices.cameras.iter().any(|camera| {
                                Some(&camera.device_id)
                                    == self.camera.as_ref().map(|selected| &selected.device_id)
                            }) {
                                PillState::On
                            } else {
                                PillState::Disconnected
                            }
                        } else {
                            PillState::Off
                        },
                        Some(DeviceMenu::Camera),
                        cx,
                    )
                    .on_click(cx.listener(|this, _, window, cx| {
                        // Through `open_panel`, so the filter field takes focus
                        // the way it already does for the display and window
                        // panels -- before the field was real there was nothing
                        // to focus into.
                        if !this.enumerating && this.device_changes_allowed(cx) {
                            this.open_panel(Panel::Device(DeviceMenu::Camera), window, cx);
                        }
                    })),
                ),
            )
            .child(
                div().flex().flex_col().child(
                    self.render_device_row(
                        "microphone-row",
                        "icons/microphone.svg",
                        self.microphone
                            .as_ref()
                            .map(|mic| mic.name.clone())
                            .unwrap_or_else(|| tr::main::device::no_microphone().into()),
                        if self.microphone.is_some()
                            && let Some(pill) = self.access_pill(OSPermission::Microphone, cx)
                        {
                            pill
                        } else if self.microphone.is_some() {
                            if self.devices.microphones.iter().any(|microphone| {
                                Some(&microphone.name)
                                    == self.microphone.as_ref().map(|selected| &selected.name)
                            }) {
                                PillState::On
                            } else {
                                PillState::Disconnected
                            }
                        } else {
                            PillState::Off
                        },
                        Some(DeviceMenu::Microphone),
                        cx,
                    )
                    .on_click(cx.listener(|this, _, window, cx| {
                        // Through `open_panel`, so the filter field takes focus
                        // the way it already does for the display and window
                        // panels -- before the field was real there was nothing
                        // to focus into.
                        if !this.enumerating && this.device_changes_allowed(cx) {
                            this.open_panel(Panel::Device(DeviceMenu::Microphone), window, cx);
                        }
                    })),
                ),
            )
            .child(
                div().flex().flex_col().child(
                    self.render_device_row(
                        "system-audio-row",
                        "icons/screen.svg",
                        if self.system_audio {
                            tr::main::device::system_audio_on().into()
                        } else {
                            tr::main::device::system_audio_off().into()
                        },
                        if self.system_audio {
                            PillState::On
                        } else {
                            PillState::Off
                        },
                        None,
                        cx,
                    )
                    // System audio has no device to choose, so the row is a
                    // plain toggle rather than a picker.
                    .on_click(cx.listener(|this, _, _window, cx| {
                        if this.device_changes_allowed(cx) {
                            this.set_system_audio(!this.system_audio, cx);
                        }
                    })),
                ),
            )
    }

    /// `DEVICE_ROW_CLASS`: 42px tall, `rounded-lg`, `border-gray-6`, `bg-gray-2`,
    /// `pl-3 pr-1.5 gap-2.5`.
    fn render_device_row(
        &self,
        id: &'static str,
        icon: &'static str,
        label: String,
        pill: PillState,
        menu: Option<DeviceMenu>,
        cx: &mut Context<Self>,
    ) -> gpui::Stateful<gpui::Div> {
        let theme = self.theme;

        div()
            .id(SharedString::from(id))
            .when(self.keyboard_nav, ui::keyboard_nav::stop)
            .role(gpui::Role::Button)
            .aria_label(label.clone())
            .relative()
            .flex()
            .flex_row()
            .items_center()
            .gap(px(10.))
            .pl(px(12.))
            .pr(px(6.))
            .w_full()
            .h(px(DEVICE_ROW_HEIGHT))
            .rounded(px(10.))
            .map(|this| glass_button(this, theme, false))
            .cursor_default()
            .overflow_hidden()
            .child(
                div()
                    .id(SharedString::from(format!("{id}-hover")))
                    .absolute()
                    .top_0()
                    .left_0()
                    .size_full()
                    .rounded(px(9.))
                    .group_hover(id, move |style| style.bg(capture_hover_fill(theme, false)))
                    .group_active(id, move |style| style.bg(capture_press_fill(theme, false))),
            )
            .when(menu == Some(DeviceMenu::Microphone), |this| {
                this.child(
                    self.microphone_level.clone().cached(
                        gpui::StyleRefinement::default()
                            .absolute()
                            .top_0()
                            .left_0()
                            .size_full(),
                    ),
                )
            })
            .child(
                svg()
                    .path(icon)
                    .size(px(16.))
                    .flex_shrink_0()
                    .text_color(theme.gray_11),
            )
            .child(
                div()
                    .flex_1()
                    .min_w_0()
                    .text_size(px(14.))
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(theme.gray_12)
                    .truncate()
                    .child(label),
            )
            .child(
                div()
                    .id(SharedString::from(format!("{id}-toggle")))
                    .child(pill.render(theme))
                    .on_click(cx.listener(move |this, _, _window, cx| {
                        let Some(menu) = menu else {
                            return;
                        };
                        if this.enumerating || !this.device_changes_allowed(cx) {
                            cx.stop_propagation();
                            return;
                        }
                        let permission = menu.permission();
                        let selected = match menu {
                            DeviceMenu::Camera => this.camera.is_some(),
                            DeviceMenu::Microphone => this.microphone.is_some(),
                        };
                        if selected && !MediaAccess::global(cx).read(cx).allowed(permission) {
                            cx.stop_propagation();
                            this.ensure_media_access(permission, cx);
                            return;
                        }
                        match menu {
                            DeviceMenu::Camera if this.camera.is_some() => {
                                cx.stop_propagation();
                                this.set_camera_selection(None, cx);
                            }
                            DeviceMenu::Microphone if this.microphone.is_some() => {
                                cx.stop_propagation();
                                this.set_microphone_selection(None, cx);
                            }
                            _ => {}
                        }
                    })),
            )
            .group(id)
    }
}

/// `black/N` -- Tailwind's slash-alpha over the two absolute colours, which
/// come from neither the Radix palette nor the material tokens.
fn black_alpha(alpha: f32) -> Hsla {
    let mut color = gpui::black();
    color.a = alpha;
    color
}

/// `CAP_GPUI_AUTO_OVERLAY=display|window|area|camera`, the harness's stand-in
/// for clicking a target tile.
pub fn auto_overlay_kind() -> Option<TargetType> {
    match std::env::var("CAP_GPUI_AUTO_OVERLAY").ok()?.as_str() {
        "display" => Some(TargetType::Display),
        "window" => Some(TargetType::Window),
        "area" => Some(TargetType::Area),
        "camera" => Some(TargetType::CameraOnly),
        _ => None,
    }
}

pub(crate) fn auto_window_title() -> Option<String> {
    std::env::var("CAP_GPUI_AUTO_WINDOW")
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}

fn window_matches(window: &crate::devices::WindowOption, title: &str) -> bool {
    window.label == title
}

/// `CAP_GPUI_AUTO_AREA=x,y,width,height`, the harness's stand-in for drawing a
/// crop with the mouse.
fn auto_area_rect() -> Option<crate::target_overlay::AreaRect> {
    crate::target_overlay::AreaRect::parse(&std::env::var("CAP_GPUI_AUTO_AREA").ok()?)
}

/// `InfoPill` + `TargetSelectInfoPill`: 24px tall, min 40px wide, `px-2.5`,
/// `rounded-full`, 11px medium text.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PillState {
    On,
    Off,
    Disconnected,
    /// The device is picked but macOS has not given Cap access: the pill is
    /// the button that asks (or opens System Settings).
    Access(&'static str),
}

impl PillState {
    fn render(self, theme: Theme) -> impl IntoElement {
        let (bg, fg, text) = match self {
            Self::On => (
                Hsla::from(theme.blue_9),
                gpui::white(),
                tr::main::device::pill::on(),
            ),
            Self::Off => (
                theme.body_fill(5),
                Hsla::from(theme.gray_11),
                tr::main::device::pill::off(),
            ),
            Self::Disconnected => (
                theme.body_fill(5),
                Hsla::from(theme.gray_11),
                tr::main::device::pill::not_connected(),
            ),
            Self::Access(label) => (Hsla::from(theme.amber_3), Hsla::from(theme.amber_11), label),
        };

        div()
            .flex()
            .items_center()
            .justify_center()
            .h(px(24.))
            .min_w(px(40.))
            .px(px(10.))
            .flex_shrink_0()
            .rounded_full()
            .bg(bg)
            .text_size(px(11.))
            .font_weight(FontWeight::MEDIUM)
            .text_color(fg)
            .child(text)
    }
}

/// The recovery toast's `3 segments · ~2m 5s` line, one message per shape so
/// each language can order and pluralize it.
fn recovery_segments_label(count: usize, duration: u64) -> String {
    let duration = if duration == 0 {
        return tr::main::recovery::segments(count);
    } else if duration < 60 {
        tr::main::recovery::duration_seconds(duration)
    } else if duration.is_multiple_of(60) {
        tr::main::recovery::duration_minutes(duration / 60)
    } else {
        tr::main::recovery::duration_minutes_seconds(duration / 60, duration % 60)
    };
    tr::main::recovery::segments_with_duration(count, duration)
}

const ERROR_TEXT_MAX_HEIGHT: f32 = 132.;

fn recording_error_text(error: String) -> impl IntoElement + Styled {
    div()
        .id("recording-error-text")
        .w_full()
        .min_w_0()
        .min_h_0()
        .max_h(px(ERROR_TEXT_MAX_HEIGHT))
        .text_size(px(12.))
        .line_height(px(16.))
        .whitespace_normal()
        .overflow_scroll()
        .child(error)
}

#[cfg(any(target_os = "linux", test))]
fn clean_capture_resume_available(phase: Phase, active: bool, paused_acknowledged: bool) -> bool {
    active && phase == (Phase::Recording { paused: true }) && paused_acknowledged
}

fn clear_shown_idle_error(phase: Phase, current: &mut Option<String>, shown: &str) -> bool {
    if phase != Phase::Idle || current.as_deref() != Some(shown) {
        return false;
    }
    *current = None;
    true
}

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

    fn camera(device: &str, model: Option<&str>) -> CameraOption {
        CameraOption {
            device_id: device.to_string(),
            model_id: model.map(|model| cap_camera::ModelID::try_from(model.to_string()).unwrap()),
            label: format!("Camera {device}"),
            best_format: None,
            formats: Vec::new(),
        }
    }

    #[test]
    fn restores_model_identity_after_device_id_changes() {
        let candidates = [
            camera("other", Some("other:model")),
            camera("new-id", Some("same:model")),
        ];
        let model = cap_camera::ModelID::try_from("same:model".to_string()).unwrap();
        let restored = remembered_camera(&recording::DeviceOrModelID::ModelID(model), &candidates);
        assert_eq!(restored, candidates[1]);
        assert_eq!(
            remembered_camera(
                &recording::DeviceOrModelID::DeviceID("other".into()),
                &candidates
            ),
            candidates[0]
        );
    }

    #[test]
    fn missing_camera_and_microphone_never_select_another_device() {
        let cameras = [camera("unrequested", None)];
        let missing = recording::DeviceOrModelID::DeviceID("remembered".into());
        let restored = remembered_camera(&missing, &cameras);
        assert_eq!(restored.device_id, "remembered");
        assert_ne!(restored, cameras[0]);
        assert_eq!(restored.label, "");
        assert_eq!(
            camera_label(&restored.label),
            tr::main::device::fallback_camera()
        );
        assert_eq!(camera_label(&cameras[0].label), cameras[0].label);

        let microphones = [MicrophoneOption {
            name: "Unrequested microphone".into(),
            sample_rate: Some(48000),
            channels: Some(2),
        }];
        let restored = remembered_microphone("Remembered microphone", &microphones);
        assert_eq!(restored.name, "Remembered microphone");
        assert_eq!(restored.sample_rate, None);
        assert_eq!(
            remembered_microphone("Unrequested microphone", &microphones),
            microphones[0]
        );
    }

    #[test]
    fn closing_before_enumeration_defers_saved_inputs_until_reopen_once() {
        let saved = crate::store::RecordingInputSettings {
            camera_id: Some(recording::DeviceOrModelID::DeviceID(
                "remembered-camera".into(),
            )),
            microphone_name: Some("Remembered microphone".into()),
        };
        let mut pending = saved.clone();
        assert!(take_pending_recording_inputs(&mut pending, true, false).is_none());
        assert!(take_pending_recording_inputs(&mut pending, false, true).is_none());
        assert_eq!(pending, saved);
        let reopened = take_pending_recording_inputs(&mut pending, false, false).unwrap();
        assert_eq!(reopened, saved);
        assert!(take_pending_recording_inputs(&mut pending, false, false).is_none());
    }

    #[test]
    fn explicit_off_during_suspended_restore_is_not_reversed_on_reopen() {
        let mut pending = crate::store::RecordingInputSettings {
            camera_id: Some(recording::DeviceOrModelID::DeviceID("old-camera".into())),
            microphone_name: Some("Remembered microphone".into()),
        };
        assert!(take_pending_recording_inputs(&mut pending, false, true).is_none());
        pending.camera_id = None;
        assert!(take_pending_recording_inputs(&mut pending, true, false).is_none());
        let reopened = take_pending_recording_inputs(&mut pending, false, false).unwrap();
        assert!(reopened.camera_id.is_none());
        assert_eq!(
            reopened.microphone_name.as_deref(),
            Some("Remembered microphone")
        );
        assert!(take_pending_recording_inputs(&mut pending, false, false).is_none());
    }
}

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

    fn pending() -> Option<PendingDeviceFormat> {
        Some(PendingDeviceFormat {
            target: DeviceFormatTarget::Microphone("Desk".into()),
            format: DeviceFormat::Microphone(
                cap_recording::feeds::microphone::MicrophoneDeviceSettings {
                    sample_rate: Some(48_000),
                    channels: Some(1),
                },
            ),
            epoch: 7,
        })
    }

    #[test]
    fn format_preference_is_saved_once_after_readiness_not_while_pending() {
        let mut request = pending();
        let mut writes = Vec::new();
        assert!(
            complete_format_request(&mut request, None, true, |_, _| panic!(
                "pending input cannot save"
            ))
            .is_none()
        );
        assert!(request.is_some());
        let result = complete_format_request(&mut request, Some(Ok(())), true, |target, format| {
            writes.push((target.clone(), format));
            true
        })
        .unwrap()
        .unwrap();
        assert!(request.is_none());
        assert_eq!(
            writes,
            vec![(DeviceFormatTarget::Microphone("Desk".into()), result)]
        );
        assert!(
            complete_format_request(&mut request, Some(Ok(())), true, |_, _| panic!(
                "duplicate acknowledgement cannot save"
            ))
            .is_none()
        );
    }

    #[test]
    fn failed_or_unowned_format_acknowledgements_do_not_save() {
        for (result, owned) in [
            (Err("input disconnected".into()), true),
            (Ok(()), false),
            (Err("selection changed".into()), false),
        ] {
            let mut request = pending();
            let completion = complete_format_request(&mut request, Some(result), owned, |_, _| {
                panic!("failed or stale input cannot save")
            });
            assert!(request.is_none());
            if owned {
                assert!(completion.unwrap().is_err());
            } else {
                assert!(completion.is_none());
            }
        }
    }

    #[test]
    fn format_save_error_does_not_report_success() {
        let mut request = pending();
        let error = complete_format_request(&mut request, Some(Ok(())), true, |_, _| false)
            .unwrap()
            .unwrap_err();
        assert!(error.contains("Could not save"));
        assert!(request.is_none());
    }
}

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

    #[test]
    fn start_dispatch_rejects_owned_inputs_before_any_side_effect() {
        for phase in [
            Phase::Idle,
            Phase::Starting,
            Phase::Recording { paused: false },
            Phase::Recording { paused: true },
            Phase::Stopping,
        ] {
            for owned in [false, true] {
                for preparing in [false, true] {
                    let mut inputs = ("original camera", "original microphone");
                    let result = RecordingStartPermit::prepare(phase, owned, preparing).map(|_| {
                        inputs = ("requested camera", "requested microphone");
                    });
                    let allowed = phase == Phase::Idle && !owned && !preparing;
                    assert_eq!(result.is_ok(), allowed);
                    assert_eq!(
                        inputs,
                        if allowed {
                            ("requested camera", "requested microphone")
                        } else {
                            ("original camera", "original microphone")
                        }
                    );
                }
            }
        }
    }

    #[test]
    fn rejected_start_does_not_revoke_the_request_that_owns_preparation() {
        let current = RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap();
        assert!(RecordingStartPermit::prepare(Phase::Idle, false, true).is_err());
        assert!(current.allows(Phase::Idle, false));
    }

    #[test]
    fn cancelled_permit_blocks_preview_storage_prompt_and_final_handoff_callbacks() {
        for stop_before in 0..5 {
            let permit = RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap();
            let callbacks: [_; 5] = std::array::from_fn(|_| permit.clone());
            let mut started = false;
            for (step, callback) in callbacks.iter().enumerate() {
                if step == stop_before {
                    permit.cancel();
                }
                if !callback.allows(Phase::Idle, false) {
                    continue;
                }
                if step == callbacks.len() - 1 {
                    started = true;
                }
            }
            assert!(!started);
        }
    }

    #[test]
    fn observing_transition_or_clean_ownership_permanently_revokes_a_request() {
        for (phase, owned) in [
            (Phase::Starting, false),
            (Phase::Recording { paused: false }, false),
            (Phase::Stopping, false),
            (Phase::Idle, true),
        ] {
            let permit = RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap();
            assert!(!permit.allows(phase, owned));
            assert!(!permit.allows(Phase::Idle, false));
        }
    }

    #[test]
    fn stale_completion_cannot_revoke_a_later_start_permit() {
        let previous = RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap();
        let completion = previous.clone();
        assert!(completion.same(&previous));
        previous.cancel();
        let current = RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap();
        assert!(!completion.same(&current));
        completion.cancel();
        assert!(current.allows(Phase::Idle, false));
    }

    #[test]
    fn cancel_current_rejects_queued_callbacks_after_rearm() {
        let first = RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap();
        let callbacks: [_; 3] = std::array::from_fn(|_| first.clone());
        let mut retained = Some(first);
        assert!(RecordingStartPermit::cancel_current(&mut retained));
        assert!(retained.is_none());
        retained = Some(RecordingStartPermit::prepare(Phase::Idle, false, false).unwrap());

        for callback in callbacks {
            assert!(!callback.allows(Phase::Idle, false));
            assert!(!retained.as_ref().unwrap().same(&callback));
            callback.cancel();
            assert!(retained.as_ref().unwrap().allows(Phase::Idle, false));
        }
        assert!(RecordingStartPermit::cancel_current(&mut retained));
        assert!(!RecordingStartPermit::cancel_current(&mut retained));
    }

    #[test]
    fn editor_recording_mode_does_not_change_the_normal_preference() {
        for preferred in [Mode::Instant, Mode::Studio, Mode::Screenshot] {
            assert_eq!(effective_recording_mode(preferred, true), Mode::Studio);
            assert_eq!(effective_recording_mode(preferred, false), preferred);
        }
    }
}

#[cfg(test)]
mod mode_hover_tests {
    use super::{Mode, ModeHoverState};

    #[test]
    fn moving_between_trigger_and_card_keeps_the_card_open_in_either_event_order() {
        for enter_first in [false, true] {
            let mut hover = ModeHoverState::default();
            hover.update(Mode::Studio, false, true);
            hover.visible = Some(Mode::Studio);
            if enter_first {
                hover.update(Mode::Studio, true, true);
                hover.update(Mode::Studio, false, false);
            } else {
                hover.update(Mode::Studio, false, false);
                hover.update(Mode::Studio, true, true);
            }
            assert_eq!(hover.target(), Some(Mode::Studio));
            hover.update(Mode::Studio, true, false);
            assert_eq!(hover.target(), None);
        }
    }

    #[test]
    fn late_leave_from_previous_trigger_does_not_dismiss_the_new_mode() {
        let mut hover = ModeHoverState::default();
        hover.update(Mode::Instant, false, true);
        hover.update(Mode::Studio, false, true);
        hover.update(Mode::Instant, false, false);
        assert_eq!(hover.target(), Some(Mode::Studio));
        hover.update(Mode::Studio, false, false);
        assert_eq!(hover.target(), None);
    }

    #[test]
    fn switching_from_a_card_to_another_mode_does_not_restore_the_removed_card() {
        let mut hover = ModeHoverState::default();
        hover.update(Mode::Instant, true, true);
        hover.visible = Some(Mode::Instant);
        hover.update(Mode::Studio, false, true);
        assert_eq!(hover.target(), Some(Mode::Studio));
        hover.update(Mode::Studio, false, false);
        assert_eq!(hover.target(), None);
        hover.update(Mode::Instant, true, false);
        assert_eq!(hover.target(), None);
    }
}

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

    #[test]
    fn clean_capture_resume_requires_the_acknowledged_active_paused_session() {
        for phase in [
            Phase::Idle,
            Phase::Starting,
            Phase::Recording { paused: false },
            Phase::Recording { paused: true },
            Phase::Stopping,
        ] {
            for active in [false, true] {
                for acknowledged in [false, true] {
                    assert_eq!(
                        clean_capture_resume_available(phase, active, acknowledged),
                        active && acknowledged && phase == (Phase::Recording { paused: true }),
                    );
                }
            }
        }
    }

    #[test]
    fn long_error_keeps_bounded_scrollable_text() {
        let mut element = recording_error_text("Requested microphone unavailable. ".repeat(200));
        let style = element.style();
        assert_eq!(
            style.max_size.height,
            Some(px(ERROR_TEXT_MAX_HEIGHT).into())
        );
        assert_eq!(style.min_size.height, Some(px(0.).into()));
        assert_eq!(style.overflow.x, Some(gpui::Overflow::Scroll));
        assert_eq!(style.overflow.y, Some(gpui::Overflow::Scroll));
    }

    #[test]
    fn unbroken_error_keeps_both_scroll_axes() {
        let mut element = recording_error_text("x".repeat(8192));
        let style = element.style();
        assert_eq!(
            style.max_size.height,
            Some(px(ERROR_TEXT_MAX_HEIGHT).into())
        );
        assert_eq!(style.overflow.x, Some(gpui::Overflow::Scroll));
        assert_eq!(style.overflow.y, Some(gpui::Overflow::Scroll));
    }

    #[test]
    fn dismiss_clears_only_matching_idle_error() {
        let shown = "Requested microphone unavailable. ".repeat(200);
        let mut current = Some(shown.clone());
        assert!(clear_shown_idle_error(Phase::Idle, &mut current, &shown));
        assert!(current.is_none());
        assert!(!clear_shown_idle_error(Phase::Idle, &mut current, &shown));
    }

    #[test]
    fn stale_dismiss_retains_new_error() {
        let mut current = Some("A different failure".to_owned());
        assert!(!clear_shown_idle_error(
            Phase::Idle,
            &mut current,
            "Old failure"
        ));
        assert_eq!(current.as_deref(), Some("A different failure"));
    }

    #[test]
    fn dismiss_never_clears_active_or_unconfirmed_cleanup_error() {
        for phase in [
            Phase::Starting,
            Phase::Recording { paused: false },
            Phase::Recording { paused: true },
            Phase::Stopping,
        ] {
            let mut current = Some("Capture cleanup is unconfirmed".to_owned());
            assert!(!clear_shown_idle_error(
                phase,
                &mut current,
                "Capture cleanup is unconfirmed",
            ));
            assert_eq!(current.as_deref(), Some("Capture cleanup is unconfirmed"));
        }
    }
}

#[cfg(test)]
mod recovery_label_tests {
    use super::recovery_segments_label;

    fn replaced(segment_count: usize, duration: u64) -> String {
        let duration_label = if duration == 0 {
            String::new()
        } else if duration < 60 {
            format!(" · ~{duration}s")
        } else if duration.is_multiple_of(60) {
            format!(" · ~{}m", duration / 60)
        } else {
            format!(" · ~{}m {}s", duration / 60, duration % 60)
        };
        format!(
            "{} segment{}{}",
            segment_count,
            if segment_count == 1 { "" } else { "s" },
            duration_label
        )
    }

    #[test]
    fn english_matches_the_assembled_label() {
        for count in [0, 1, 2, 7] {
            for duration in [0, 1, 45, 59, 60, 61, 125, 180, 3_599, 3_600] {
                assert_eq!(
                    recovery_segments_label(count, duration),
                    replaced(count, duration),
                    "{count} segments, {duration}s"
                );
            }
        }
    }
}