{
            {
                let state = Rc::clone(&state);
                let child_proc = Rc::clone(&child_proc);
                let tests = Rc::clone(&tests);
                let widgets = widgets.clone();
                let server_entry = server_entry.clone();
                let camera_combo = camera_combo.clone();
                let camera_quality_combo = camera_quality_combo.clone();
                let microphone_combo = microphone_combo.clone();
                let speaker_combo = speaker_combo.clone();
                let input_control_path = Rc::clone(&input_control_path);
                let input_state_path = Rc::clone(&input_state_path);
                let input_toggle_control_path = Rc::clone(&input_toggle_control_path);
                let server_addr_fallback = Rc::clone(&server_addr);
                let preview = preview.clone();
                let power_tx = power_tx.clone();
                let relay_tx = relay_tx.clone();
                let relay_request_in_flight = Rc::clone(&relay_request_in_flight);
                let disconnect_cooldown_until = Rc::clone(&disconnect_cooldown_until);
                let popouts = Rc::clone(&popouts);
                let window = window.clone();
                let start_button = widgets.start_button.clone();
                let widgets_handle = widgets.clone();
                start_button.connect_clicked(move |_| {
                    let server_addr =
                        selected_server_addr(&server_entry, server_addr_fallback.as_ref());
                    if relay_request_in_flight.get() {
                        return;
                    }
                    if child_proc.borrow().is_some() {
                        if let Some(remaining) = disconnect_cooldown_remaining(
                            Instant::now(),
                            disconnect_cooldown_until.get(),
                        ) {
                            widgets_handle
                                .status_label
                                .set_text(&disconnect_cooldown_status(remaining));
                            return;
                        }
                        stop_child_process(&child_proc);
                        disconnect_cooldown_until.set(None);
                        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_handle,
                        );
                        window.present();
                        if let Some(preview) = preview.as_ref() {
                            preview.set_server_addr(server_addr.clone());
                            preview.set_session_active(false);
                        }
                        if power_mode != "auto" {
                            widgets_handle.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_handle
                                .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),
                        );
                        refresh_launcher_ui(&widgets_handle, &state.borrow(), false);
                        return;
                    }
                    {
                        let mut state = state.borrow_mut();
                        state.select_camera(selected_combo_value(&camera_combo));
                        state.select_camera_quality(selected_camera_quality(&camera_quality_combo));
                        state.select_microphone(selected_combo_value(&microphone_combo));
                        state.select_speaker(selected_combo_value(&speaker_combo));
                        state.select_keyboard(selected_combo_value(&keyboard_combo));
                        state.select_mouse(selected_combo_value(&mouse_combo));
                    }
                    let _ = std::fs::remove_file(input_control_path.as_path());
                    let _ = std::fs::remove_file(input_state_path.as_path());
                    let _ = std::fs::remove_file(input_toggle_control_path.as_path());
                    let launch_state = state.borrow().clone();
                    tests.borrow_mut().stop_local_capture_for_relay();
                    refresh_test_buttons(&widgets_handle, &mut tests.borrow_mut());
                    let input_toggle_key = launch_state.swap_key.clone();
                    let input_control_path = input_control_path.as_ref().clone();
                    let input_state_path = input_state_path.as_ref().clone();
                    let input_toggle_control_path = input_toggle_control_path.as_ref().clone();
                    relay_request_in_flight.set(true);
                    widgets_handle.status_label.set_text(&format!(
                        "Connecting relay with {} as the swap key...",
                        toggle_key_label(&input_toggle_key)
                    ));
                    refresh_launcher_ui(
                        &widgets_handle,
                        &state.borrow(),
                        child_proc.borrow().is_some(),
                    );
                    let relay_tx = relay_tx.clone();
                    std::thread::spawn(move || {
                        let result = spawn_client_process(
                            &server_addr,
                            &launch_state,
                            &input_toggle_key,
                            input_control_path.as_path(),
                            input_state_path.as_path(),
                            input_toggle_control_path.as_path(),
                        )
                        .map_err(|err| err.to_string());
                        let _ = relay_tx.send(RelayMessage::Spawned(result));
                    });
                });
            }

            {
                let state = Rc::clone(&state);
                let child_proc = Rc::clone(&child_proc);
                let widgets = widgets.clone();
                let input_control_path = Rc::clone(&input_control_path);
                let popouts = Rc::clone(&popouts);
                let window = window.clone();
                let input_toggle_button = widgets.input_toggle_button.clone();
                let widgets_handle = widgets.clone();
                input_toggle_button.connect_clicked(move |_| {
                    let next = next_input_routing(state.borrow().routing);
                    let child_running = child_proc.borrow().is_some();
                    if child_running {
                        if let Err(err) =
                            write_input_routing_request(input_control_path.as_path(), next)
                        {
                            widgets_handle
                                .status_label
                                .set_text(&format!("Could not update live input target: {err}"));
                            refresh_launcher_ui(&widgets_handle, &state.borrow(), true);
                            return;
                        }
                        widgets_handle.status_label.set_text(&format!(
                            "Input routing switched toward {}.",
                            routing_name(next)
                        ));
                    } else {
                        widgets_handle.status_label.set_text(&format!(
                            "Relay will start with {} input ownership.",
                            routing_name(next)
                        ));
                    }
                    state.borrow_mut().set_routing(next);
                    refresh_launcher_ui(&widgets_handle, &state.borrow(), child_running);
                    if matches!(next, InputRouting::Remote) {
                        present_popout_windows(&popouts);
                    } else {
                        window.present();
                    }
                });
            }

            {
                let state = Rc::clone(&state);
                let child_proc = Rc::clone(&child_proc);
                let widgets = widgets.clone();
                let swap_key_button = widgets.swap_key_button.clone();
                swap_key_button.connect_clicked(move |_| {
                    let token = state.borrow_mut().begin_swap_key_binding();
                    widgets
                        .status_label
                        .set_text("Press a single key within 3 seconds to make it the swap shortcut.");
                    refresh_launcher_ui(&widgets, &state.borrow(), child_proc.borrow().is_some());
                    let state = Rc::clone(&state);
                    let child_proc = Rc::clone(&child_proc);
                    let widgets = widgets.clone();
                    glib::timeout_add_local_once(Duration::from_secs(3), move || {
                        if state.borrow_mut().cancel_swap_key_binding(token) {
                            widgets.status_label.set_text(
                                "Swap-key capture timed out. The previous shortcut is still in place.",
                            );
                            refresh_launcher_ui(
                                &widgets,
                                &state.borrow(),
                                child_proc.borrow().is_some(),
                            );
                        }
                    });
                });
            }
}
