{
            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let tests = Rc::clone(&tests);
                let microphone_combo = microphone_combo.clone();
                let microphone_combo_read = microphone_combo.clone();
                microphone_combo.connect_changed(move |_| {
                    state
                        .borrow_mut()
                        .select_microphone(selected_combo_value(&microphone_combo_read));
                    let relay_live = child_proc.borrow().is_some();
                    if relay_live {
                        apply_live_media_device_change(
                            &state.borrow(),
                            &widgets,
                            &child_proc,
                            "Microphone",
                        );
                    } else if tests.borrow_mut().is_running(DeviceTestKind::Microphone) {
                        widgets.status_label.set_text(
                            "Microphone selection changed. Restart Monitor Mic to audition the new input.",
                        );
                    }
                    refresh_launcher_ui(&widgets, &state.borrow(), relay_live);
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let tests = Rc::clone(&tests);
                let speaker_combo = speaker_combo.clone();
                let speaker_combo_read = speaker_combo.clone();
                speaker_combo.connect_changed(move |_| {
                    state
                        .borrow_mut()
                        .select_speaker(selected_combo_value(&speaker_combo_read));
                    let speaker_running = tests.borrow_mut().is_running(DeviceTestKind::Speaker);
                    let microphone_running =
                        tests.borrow_mut().is_running(DeviceTestKind::Microphone);
                    let relay_live = child_proc.borrow().is_some();
                    if relay_live {
                        apply_live_media_device_change(
                            &state.borrow(),
                            &widgets,
                            &child_proc,
                            "Speaker",
                        );
                    } else if speaker_running || microphone_running {
                        widgets.status_label.set_text(
                            "Speaker selection changed. Restart the local audio tests to hear the new output.",
                        );
                    }
                    refresh_launcher_ui(&widgets, &state.borrow(), relay_live);
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let audio_gain_scale = widgets.audio_gain_scale.clone();
                audio_gain_scale.connect_value_changed(move |scale| {
                    if !apply_audio_gain_change(scale, &state, &widgets, &child_proc) {
                        let scale = scale.clone();
                        let state = Rc::clone(&state);
                        let widgets = widgets.clone();
                        let child_proc = Rc::clone(&child_proc);
                        glib::idle_add_local_once(move || {
                            let _ = apply_audio_gain_change(&scale, &state, &widgets, &child_proc);
                        });
                    }
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let mic_gain_scale = widgets.mic_gain_scale.clone();
                mic_gain_scale.connect_value_changed(move |scale| {
                    if !apply_mic_gain_change(scale, &state, &widgets, &child_proc) {
                        let scale = scale.clone();
                        let state = Rc::clone(&state);
                        let widgets = widgets.clone();
                        let child_proc = Rc::clone(&child_proc);
                        glib::idle_add_local_once(move || {
                            let _ = apply_mic_gain_change(&scale, &state, &widgets, &child_proc);
                        });
                    }
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let tests = Rc::clone(&tests);
                let toggle = widgets.camera_channel_toggle.clone();
                toggle.connect_toggled(move |toggle| {
                    let enabled = toggle.is_active();
                    if let Ok(mut state) = state.try_borrow_mut() {
                        state.set_camera_channel_enabled(enabled);
                    }
                    if !enabled {
                        tests.borrow_mut().stop_camera_preview();
                    }
                    if let Ok(state_snapshot) = state.try_borrow().map(|state| state.clone()) {
                        apply_media_control_change(
                            &state_snapshot,
                            &widgets,
                            &child_proc,
                            "Camera",
                            enabled,
                        );
                        refresh_launcher_ui(
                            &widgets,
                            &state_snapshot,
                            child_proc.borrow().is_some(),
                        );
                    }
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let toggle = widgets.noise_suppression_toggle.clone();
                toggle.connect_toggled(move |toggle| {
                    let enabled = toggle.is_active();
                    if let Ok(mut state) = state.try_borrow_mut() {
                        state.set_mic_noise_suppression(enabled);
                    }
                    let label = if enabled {
                        "Noise cancellation enabled"
                    } else {
                        "Noise cancellation disabled"
                    };
                    if let Ok(state_snapshot) = state.try_borrow().map(|state| state.clone()) {
                        if child_proc.borrow().is_some() {
                            apply_live_media_device_change(
                                &state_snapshot,
                                &widgets,
                                &child_proc,
                                "Microphone noise suppression",
                            );
                        } else {
                            widgets.status_label.set_text(&format!(
                                "{label} for the next relay launch."
                            ));
                        }
                        refresh_launcher_ui(
                            &widgets,
                            &state_snapshot,
                            child_proc.borrow().is_some(),
                        );
                    }
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let tests = Rc::clone(&tests);
                let toggle = widgets.microphone_channel_toggle.clone();
                toggle.connect_toggled(move |toggle| {
                    let enabled = toggle.is_active();
                    if let Ok(mut state) = state.try_borrow_mut() {
                        state.set_microphone_channel_enabled(enabled);
                    }
                    if !enabled {
                        let mut tests = tests.borrow_mut();
                        tests.stop_microphone_monitor();
                        tests.stop_microphone_replay();
                    }
                    if let Ok(state_snapshot) = state.try_borrow().map(|state| state.clone()) {
                        apply_media_control_change(
                            &state_snapshot,
                            &widgets,
                            &child_proc,
                            "Mic",
                            enabled,
                        );
                        refresh_launcher_ui(
                            &widgets,
                            &state_snapshot,
                            child_proc.borrow().is_some(),
                        );
                    }
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                });
            }

            {
                let state = Rc::clone(&state);
                let widgets = widgets.clone();
                let child_proc = Rc::clone(&child_proc);
                let tests = Rc::clone(&tests);
                let toggle = widgets.audio_channel_toggle.clone();
                toggle.connect_toggled(move |toggle| {
                    let enabled = toggle.is_active();
                    if let Ok(mut state) = state.try_borrow_mut() {
                        state.set_audio_channel_enabled(enabled);
                    }
                    if !enabled {
                        let mut tests = tests.borrow_mut();
                        tests.stop_speaker_test();
                        tests.stop_microphone_replay();
                    }
                    if let Ok(state_snapshot) = state.try_borrow().map(|state| state.clone()) {
                        apply_media_control_change(
                            &state_snapshot,
                            &widgets,
                            &child_proc,
                            "Speaker",
                            enabled,
                        );
                        refresh_launcher_ui(
                            &widgets,
                            &state_snapshot,
                            child_proc.borrow().is_some(),
                        );
                    }
                    refresh_test_buttons(&widgets, &mut tests.borrow_mut());
                });
            }
}
