From cfb10a04f5141fa10bbecce5dc9046207c3c13f7 Mon Sep 17 00:00:00 2001 From: Rinde van Lon Date: Wed, 11 Jun 2025 11:01:34 +0100 Subject: [PATCH] Improve `ComboBox` doc example (#7116) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improves the `ComboBox` example with some code that shows how to handle changes in the `ComboBox`’s selection. The approach is based on the advice given in https://github.com/emilk/egui/discussions/923 . I hope this saves future me (and hopefully others) a web search for how to do this. * [x] I have followed the instructions in the PR template --- crates/egui/src/containers/combo_box.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/containers/combo_box.rs b/crates/egui/src/containers/combo_box.rs index ab89c435..3ae8344b 100644 --- a/crates/egui/src/containers/combo_box.rs +++ b/crates/egui/src/containers/combo_box.rs @@ -16,9 +16,10 @@ pub type IconPainter = Box; /// /// ``` /// # egui::__run_test_ui(|ui| { -/// # #[derive(Debug, PartialEq)] +/// # #[derive(Debug, PartialEq, Copy, Clone)] /// # enum Enum { First, Second, Third } /// # let mut selected = Enum::First; +/// let before = selected; /// egui::ComboBox::from_label("Select one!") /// .selected_text(format!("{:?}", selected)) /// .show_ui(ui, |ui| { @@ -27,6 +28,10 @@ pub type IconPainter = Box; /// ui.selectable_value(&mut selected, Enum::Third, "Third"); /// } /// ); +/// +/// if selected != before { +/// // Handle selection change +/// } /// # }); /// ``` #[must_use = "You should call .show*"]