#[cfg(not(coverage))]
impl PreviewFeed {
    fn spawn(
        server_addr: Arc<Mutex<String>>,
        monitor_id: u32,
        profile: PreviewProfile,
        log_sink: Arc<Mutex<Option<std::sync::mpsc::Sender<String>>>>,
    ) -> Result<Self> {
        let shared = Arc::new(Mutex::new(SharedPreviewState::new()));
        let session_active = Arc::new(AtomicBool::new(false));
        let active_bindings = Arc::new(AtomicUsize::new(0));
        let running = Arc::new(AtomicBool::new(true));
        let shared_state = Arc::clone(&shared);
        let session_active_flag = Arc::clone(&session_active);
        let active_bindings_flag = Arc::clone(&active_bindings);
        let running_flag = Arc::clone(&running);
        let worker = std::thread::spawn(move || {
            if let Err(err) = run_preview_feed(
                server_addr,
                monitor_id,
                profile,
                session_active_flag,
                active_bindings_flag,
                running_flag,
                Arc::clone(&shared_state),
                Arc::clone(&log_sink),
            ) {
                set_shared_status(
                    &shared_state,
                    &log_sink,
                    monitor_id,
                    "Preview pipeline setup failed. See session log.",
                    true,
                );
                log_preview_issue(
                    &shared_state,
                    &log_sink,
                    monitor_id,
                    &format!("Preview feed startup failed: {err:#}"),
                );
                warn!(monitor_id, ?err, "launcher preview feed exited");
            }
        });
        Ok(Self {
            shared,
            session_active,
            active_bindings,
            running,
            worker: Arc::new(Mutex::new(Some(worker))),
            profile,
            disabled: false,
        })
    }
    fn spawn_disabled(profile: PreviewProfile) -> Self {
        let shared = Arc::new(Mutex::new(SharedPreviewState::new()));
        if let Ok(mut slot) = shared.lock() {
            slot.set_status("Feed disabled.", true);
        }
        Self {
            shared,
            session_active: Arc::new(AtomicBool::new(false)),
            active_bindings: Arc::new(AtomicUsize::new(0)),
            running: Arc::new(AtomicBool::new(false)),
            worker: Arc::new(Mutex::new(None)),
            profile,
            disabled: true,
        }
    }
    fn profile(&self) -> PreviewProfile {
        self.profile
    }

    fn is_disabled(&self) -> bool {
        self.disabled
    }

    fn is_active(&self) -> bool {
        self.session_active.load(Ordering::Relaxed)
    }

    fn set_active(&self, active: bool) {
        self.session_active.store(active, Ordering::Relaxed);
        if !active && !self.disabled {
            self.replace_status(PREVIEW_IDLE_STATUS, true);
        }
    }

    fn shutdown(&self) {
        self.running.store(false, Ordering::Relaxed);
        self.replace_status(
            if self.disabled {
                "Feed disabled."
            } else {
                PREVIEW_IDLE_STATUS
            },
            true,
        );
        if let Ok(mut worker) = self.worker.lock()
            && let Some(handle) = worker.take()
        {
            let _ = handle.join();
        }
    }

    fn replace_status(&self, status: impl Into<String>, clear_picture: bool) {
        if let Ok(mut shared) = self.shared.lock() {
            shared.set_status(status, clear_picture);
        }
    }

    fn install_on_picture(
        &self,
        picture: &gtk::Picture,
        status_label: &gtk::Label,
    ) -> PreviewBinding {
        let picture = picture.clone();
        let status_label = status_label.clone();
        let shared = Arc::clone(&self.shared);
        let enabled = Arc::new(AtomicBool::new(true));
        let alive = Arc::new(AtomicBool::new(true));
        let active_bindings = Arc::clone(&self.active_bindings);
        let enabled_flag = Arc::clone(&enabled);
        let alive_flag = Arc::clone(&alive);
        active_bindings.fetch_add(1, Ordering::AcqRel);
        let mut last_generation = 0_u64;
        glib::timeout_add_local(Duration::from_millis(120), move || {
            if !alive_flag.load(Ordering::Relaxed) {
                return glib::ControlFlow::Break;
            }
            if !enabled_flag.load(Ordering::Relaxed) {
                return glib::ControlFlow::Continue;
            }

            let (frame, status, generation, clear_picture) = match shared.lock() {
                Ok(mut slot) => {
                    let frame = slot.latest.take();
                    let status = slot.status.clone();
                    let generation = slot.generation;
                    let clear_picture = slot.clear_picture;
                    slot.clear_picture = false;
                    (frame, status, generation, clear_picture)
                }
                Err(_) => return glib::ControlFlow::Continue,
            };

            if clear_picture {
                picture.set_paintable(Option::<&gdk::Paintable>::None);
            }
            if let Some(frame) = frame {
                let bytes = glib::Bytes::from_owned(frame.rgba);
                let texture = gdk::MemoryTexture::new(
                    frame.width,
                    frame.height,
                    gdk::MemoryFormat::R8g8b8a8,
                    &bytes,
                    frame.stride,
                );
                picture.set_paintable(Some(&texture));
            }
            if generation != last_generation {
                status_label.set_text(&status);
                status_label.set_tooltip_text(Some(&status));
                last_generation = generation;
            }
            glib::ControlFlow::Continue
        });
        PreviewBinding {
            enabled,
            alive,
            active_bindings,
        }
    }

    fn snapshot_metrics(&self) -> PreviewMetricsSnapshot {
        self.shared
            .lock()
            .map(|mut shared| shared.telemetry.snapshot())
            .unwrap_or_default()
    }

    fn start_recording_tap(&self) -> Option<PreviewRecordingTap> {
        if self.disabled {
            return None;
        }
        let capacity = (self.profile.requested_fps.max(1) as usize).saturating_mul(2);
        let (tx, rx) = std::sync::mpsc::sync_channel(capacity.max(4));
        if let Ok(mut shared) = self.shared.lock() {
            shared.recorders.push(tx);
            Some(PreviewRecordingTap::new(
                rx,
                Arc::clone(&self.active_bindings),
            ))
        } else {
            None
        }
    }
}

#[cfg(not(coverage))]
struct PreviewFrame {
    width: i32,
    height: i32,
    stride: usize,
    rgba: Vec<u8>,
}

#[cfg(not(coverage))]
#[derive(Clone)]
pub(crate) struct PreviewFrameSnapshot {
    pub width: i32,
    pub height: i32,
    pub stride: usize,
    pub rgba: Vec<u8>,
}

#[cfg(not(coverage))]
impl PreviewFrame {
    fn snapshot(&self) -> PreviewFrameSnapshot {
        PreviewFrameSnapshot {
            width: self.width,
            height: self.height,
            stride: self.stride,
            rgba: self.rgba.clone(),
        }
    }
}
