{
            {
                let window = window.clone();
                let state = Rc::clone(&state);
                let child_proc = Rc::clone(&child_proc);
                let widgets = widgets.clone();
                let focus_signal_path = Rc::clone(&focus_signal_path);
                let input_state_path = Rc::clone(&input_state_path);
                let tests = Rc::clone(&tests);
                let server_entry = server_entry.clone();
                let server_addr_fallback = Rc::clone(&server_addr);
                let last_focus_marker =
                    Rc::new(RefCell::new(path_marker(focus_signal_path.as_path())));
                let power_request_in_flight = Rc::clone(&power_request_in_flight);
                let relay_request_in_flight = Rc::clone(&relay_request_in_flight);
                let preview = preview.clone();
                let power_tx = power_tx.clone();
                let caps_tx = caps_tx.clone();
                let caps_request_in_flight = Rc::clone(&caps_request_in_flight);
                let diagnostics_network = Rc::clone(&diagnostics_network);
                let diagnostics_process = Rc::clone(&diagnostics_process);
                let next_diagnostics_probe = Rc::clone(&next_diagnostics_probe);
                let next_diagnostics_sample = Rc::clone(&next_diagnostics_sample);
                let preview_session_active = Rc::clone(&preview_session_active);
                let log_tx = log_tx.clone();
                let camera_preview_path = uplink_camera_preview_path();
                let mic_level_path = uplink_mic_level_path();
                let uplink_telemetry_path = uplink_telemetry_path();
                glib::timeout_add_local(Duration::from_millis(180), move || {
                    let child_running = reap_exited_child(&child_proc);
                    if let Some(preview) = preview.as_ref() {
                        let desired_preview_active = {
                            let state_snapshot = state.borrow();
                            session_preview_active(&state_snapshot, child_running)
                        };
                        if preview_session_active.get() != desired_preview_active {
                            preview.set_session_active(desired_preview_active);
                            preview_session_active.set(desired_preview_active);
                        }
                    }
                    if !child_running && state.borrow().remote_active {
                        let power_mode = {
                            let mut state = state.borrow_mut();
                            let _ = state.stop_remote();
                            state.capture_power.mode.clone()
                        };
                        dock_all_displays_to_preview(&state, &child_proc, &popouts, &widgets);
                        window.present();
                        if let Some(preview) = preview.as_ref() {
                            preview.set_session_active(false);
                        }
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        if power_mode != "auto" {
                            widgets.status_label.set_text(
                                "Relay disconnected. Returning capture to automatic mode so it can fall back after the disconnect grace.",
                            );
                            request_capture_power_command(
                                power_tx.clone(),
                                server_addr.clone(),
                                CapturePowerCommand::Auto,
                            );
                        } else {
                            widgets
                                .status_label
                                .set_text(disconnected_capture_note(&power_mode));
                        }
                        request_capture_power_refresh(
                            power_tx.clone(),
                            server_addr.clone(),
                            Duration::from_millis(250),
                        );
                        request_capture_power_refresh(
                            power_tx.clone(),
                            server_addr,
                            Duration::from_secs(31),
                        );
                    }

                    if child_running
                        && let Some(routing) = read_input_routing_state(input_state_path.as_path())
                        && routing != state.borrow().routing
                    {
                        state.borrow_mut().set_routing(routing);
                        refresh_launcher_ui(&widgets, &state.borrow(), child_running);
                        if matches!(routing, InputRouting::Remote) {
                            present_popout_windows(&popouts);
                        } else {
                            window.present();
                        }
                    }

                    let next_focus_marker = path_marker(focus_signal_path.as_path());
                    let mut last_focus = last_focus_marker.borrow_mut();
                    if next_focus_marker > *last_focus {
                        *last_focus = next_focus_marker;
                        state.borrow_mut().set_routing(InputRouting::Local);
                        refresh_launcher_ui(&widgets, &state.borrow(), child_running);
                        widgets
                            .status_label
                            .set_text("Local control restored and the launcher is focused.");
                        window.present();
                    }

                    while let Ok(message) = relay_rx.try_recv() {
                        relay_request_in_flight.set(false);
                        match message {
                            RelayMessage::Spawned(Ok(mut child)) => {
                                attach_child_log_streams(&mut child, log_tx.clone());
                                *child_proc.borrow_mut() = Some(child);
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    let _ = state.start_remote();
                                }
                                let server_addr =
                                    selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                                if let Some(preview) = preview.as_ref() {
                                    preview.set_server_addr(server_addr.clone());
                                    preview.set_session_active(session_preview_active(
                                        &state.borrow(),
                                        child_proc.borrow().is_some(),
                                    ));
                                }
                                let routing = routing_name(state.borrow().routing);
                                let power_mode = state.borrow().capture_power.mode.clone();
                                let message = match power_mode.as_str() {
                                    "forced-off" => format!(
                                        "Relay connected with inputs routed to {routing}, but capture is forced off. Return capture to Auto or Force On when you want remote video."
                                    ),
                                    "forced-on" => format!(
                                        "Relay connected with inputs routed to {routing}. Capture is being held awake and the eye previews are coming online."
                                    ),
                                    _ => format!(
                                        "Relay connected with inputs routed to {routing}. The eye previews will come up with the live session."
                                    ),
                                };
                                widgets.status_label.set_text(&message);
                                if matches!(state.borrow().routing, InputRouting::Remote) {
                                    present_popout_windows(&popouts);
                                }
                                request_capture_power_refresh(
                                    power_tx.clone(),
                                    server_addr.clone(),
                                    Duration::from_millis(250),
                                );
                                request_capture_power_refresh(
                                    power_tx.clone(),
                                    server_addr,
                                    Duration::from_millis(1250),
                                );
                            }
                            RelayMessage::Spawned(Err(err)) => {
                                state.borrow_mut().set_server_available(false);
                                if let Some(preview) = preview.as_ref() {
                                    preview.set_session_active(false);
                                }
                                widgets
                                    .status_label
                                    .set_text(&format!("Relay start failed: {err}"));
                            }
                        }
                    }

                    while let Ok(line) = log_rx.try_recv() {
                        let level = *widgets.session_log_level.borrow();
                        if append_session_log_for_level(&widgets.session_log_buffer, &line, level)
                        {
                            let mut end = widgets.session_log_buffer.end_iter();
                            widgets
                                .session_log_view
                                .scroll_to_iter(&mut end, 0.0, false, 0.0, 1.0);
                        }
                    }

                    while let Ok(message) = power_rx.try_recv() {
                        power_request_in_flight.set(false);
                        match message {
                            PowerMessage::Refresh(Ok(power)) => {
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    state.set_capture_power(power);
                                }
                                if let Some(preview) = preview.as_ref() {
                                    let preview_active = {
                                        let state = state.borrow();
                                        session_preview_active(
                                            &state,
                                            child_proc.borrow().is_some(),
                                        )
                                    };
                                    preview.set_session_active(preview_active);
                                }
                            }
                            PowerMessage::Refresh(Err(err)) => {
                                let relay_live = child_proc.borrow().is_some()
                                    || state.borrow().remote_active;
                                {
                                    let mut state = state.borrow_mut();
                                    if relay_live {
                                        state.set_server_available(true);
                                        if !state.capture_power.available {
                                            state.set_capture_power(unavailable_capture_power(err));
                                        }
                                    } else {
                                        state.set_server_available(false);
                                        state.set_capture_power(unavailable_capture_power(err));
                                    }
                                }
                                if let Some(preview) = preview.as_ref() {
                                    let preview_active = {
                                        let state = state.borrow();
                                        session_preview_active(
                                            &state,
                                            child_proc.borrow().is_some(),
                                        )
                                    };
                                    preview.set_session_active(preview_active);
                                }
                            }
                            PowerMessage::Command(Ok(power)) => {
                                let mode = power.mode.clone();
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_server_available(true);
                                    state.set_capture_power(power);
                                }
                                if let Some(preview) = preview.as_ref() {
                                    let preview_active = {
                                        let state = state.borrow();
                                        session_preview_active(
                                            &state,
                                            child_proc.borrow().is_some(),
                                        )
                                    };
                                    preview.set_session_active(preview_active);
                                }
                                widgets.status_label.set_text(match mode.as_str() {
                                    "forced-on" => "Capture feeds forced on. Remote eyes stay awake even if previews or the relay stop.",
                                    "forced-off" => "Capture feeds forced off. Remote eye previews and session video stay dark until you switch back.",
                                    _ => "Capture feeds returned to automatic mode. Live previews and relay demand will wake them as needed.",
                                });
                            }
                            PowerMessage::Command(Err(err)) => {
                                let mut state = state.borrow_mut();
                                state.set_server_available(false);
                                state.set_capture_power(unavailable_capture_power(err.clone()));
                                widgets
                                    .status_label
                                    .set_text(&format!("Capture power update failed: {err}"));
                            }
                        }
                    }

                    while let Ok(message) = caps_rx.try_recv() {
                        caps_request_in_flight.set(false);
                        match message {
                            CapsMessage::Refresh(probe_result) => {
                                diagnostics_network.borrow_mut().record(&probe_result);
                                let caps = probe_result.caps;
                                let rebind_preview = eye_caps_changed(&state.borrow(), &caps);
                                {
                                    let mut state = state.borrow_mut();
                                    if probe_result.reachable {
                                        state.set_server_available(true);
                                    } else if child_proc.borrow().is_none() {
                                        state.set_server_available(false);
                                    }
                                    state.set_server_version(caps.server_version.clone());
                                }
                                if let (Some(width), Some(height)) =
                                    (caps.eye_width, caps.eye_height)
                                {
                                    let fps = caps
                                        .eye_fps
                                        .unwrap_or(crate::launcher::state::PreviewSourceSize::default().fps);
                                {
                                    let mut state = state.borrow_mut();
                                    state.set_preview_source_profile(width, height, fps);
                                }
                                    if rebind_preview && let Some(preview) = preview.as_ref() {
                                        sync_preview_profiles(preview, &widgets, &popouts, &state.borrow());
                                    }
                                    refresh_eye_feed_controls(&widgets, &state.borrow());
                                } else {
                                    refresh_eye_feed_controls(&widgets, &state.borrow());
                                }
                            }
                        }
                    }

                    while let Ok(message) = clipboard_rx.try_recv() {
                        match message {
                            ClipboardMessage::Finished(Ok(detail)) => {
                                widgets.status_label.set_text(&format!("✨ {detail}"));
                            }
                            ClipboardMessage::Finished(Err(err)) => {
                                widgets
                                    .status_label
                                    .set_text(&format!("Clipboard send failed: {err}"));
                            }
                        }
                    }

                    let now = Instant::now();
                    let child_running = child_proc.borrow().is_some();

                    if now >= next_power_probe.get()
                        && !power_request_in_flight.get()
                        && (child_running
                            || state.borrow().capture_power.enabled
                            || state.borrow().remote_active)
                    {
                        power_request_in_flight.set(true);
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        request_capture_power_refresh(power_tx.clone(), server_addr, Duration::ZERO);
                        next_power_probe.set(now + Duration::from_secs(2));
                    }

                    if now >= next_diagnostics_probe.get() && !caps_request_in_flight.get() {
                        caps_request_in_flight.set(true);
                        let server_addr =
                            selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                        request_handshake_caps(caps_tx.clone(), server_addr, Duration::ZERO);
                        next_diagnostics_probe.set(now + Duration::from_secs(2));
                    }

                    if now >= next_diagnostics_sample.get() {
                        let network = diagnostics_network.borrow_mut().snapshot();
                        let uplink =
                            crate::uplink_telemetry::load_uplink_telemetry(&uplink_telemetry_path);
                        let client_process_cpu_pct = diagnostics_process
                            .borrow_mut()
                            .sample_percent()
                            .unwrap_or(0.0);
                        record_diagnostics_sample(
                            &widgets,
                            &state.borrow(),
                            preview.as_ref().map(|preview| preview.as_ref()),
                            uplink.as_ref(),
                            network,
                            client_process_cpu_pct,
                        );
                        next_diagnostics_sample.set(now + Duration::from_secs(1));
                    }

                    let (camera_probe_active, camera_label, mic_probe_active) = {
                        let state = state.borrow();
                        (
                            state.channels.camera && state.devices.camera.is_some(),
                            state.devices.camera.clone(),
                            state.channels.microphone && state.devices.microphone.is_some(),
                        )
                    };
                    if let Err(err) = tests.borrow_mut().sync_relay_uplink_probe(
                        child_running,
                        camera_probe_active,
                        camera_label.as_deref(),
                        &camera_preview_path,
                        mic_probe_active,
                        &mic_level_path,
                    ) {
                        widgets
                            .status_label
                            .set_text(&format!("Local uplink monitor could not start: {err}"));
                    }

                    refresh_launcher_ui(&widgets, &state.borrow(), child_running);
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                    glib::ControlFlow::Continue
                });
            }
}
