From 58accf69c735247a59f7ec5b91a3299b9af02660 Mon Sep 17 00:00:00 2001 From: RobWalt Date: Wed, 9 Nov 2022 14:47:35 +0100 Subject: [PATCH 1/4] feat(combobox): implement text wrap for selected text --- crates/egui/src/containers/combo_box.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/containers/combo_box.rs b/crates/egui/src/containers/combo_box.rs index 49b9f8a1f9f..383346c0c33 100644 --- a/crates/egui/src/containers/combo_box.rs +++ b/crates/egui/src/containers/combo_box.rs @@ -28,6 +28,7 @@ pub struct ComboBox { selected_text: WidgetText, width: Option, icon: Option, + wrap: Option, } impl ComboBox { @@ -39,6 +40,7 @@ impl ComboBox { selected_text: Default::default(), width: None, icon: None, + wrap: None, } } @@ -51,6 +53,7 @@ impl ComboBox { selected_text: Default::default(), width: None, icon: None, + wrap: None, } } @@ -62,6 +65,7 @@ impl ComboBox { selected_text: Default::default(), width: None, icon: None, + wrap: None, } } @@ -112,6 +116,12 @@ impl ComboBox { self } + /// Set the wrap width for the selected text + pub fn wrap(mut self, wrap_width: f32) -> Self { + self.wrap.replace(wrap_width); + self + } + /// Show the combo box, with the given ui code for the menu contents. /// /// Returns `InnerResponse { inner: None }` if the combo box is closed. @@ -134,6 +144,7 @@ impl ComboBox { selected_text, width, icon, + wrap, } = self; let button_id = ui.make_persistent_id(id_source); @@ -142,7 +153,7 @@ impl ComboBox { if let Some(width) = width { ui.spacing_mut().slider_width = width; // yes, this is ugly. Will remove later. } - let mut ir = combo_box_dyn(ui, button_id, selected_text, menu_contents, icon); + let mut ir = combo_box_dyn(ui, button_id, selected_text, menu_contents, icon, wrap); if let Some(label) = label { ir.response .widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, label.text())); @@ -209,6 +220,7 @@ fn combo_box_dyn<'c, R>( selected_text: WidgetText, menu_contents: Box R + 'c>, icon: Option, + wrap: Option, ) -> InnerResponse> { let popup_id = button_id.with("popup"); @@ -218,7 +230,12 @@ fn combo_box_dyn<'c, R>( let full_minimum_width = ui.spacing().slider_width; let icon_size = Vec2::splat(ui.spacing().icon_width); - let galley = selected_text.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Button); + let galley = selected_text.into_galley( + ui, + wrap.map(|_| true), + wrap.unwrap_or(f32::INFINITY), + TextStyle::Button, + ); let width = galley.size().x + ui.spacing().item_spacing.x + icon_size.x; let width = width.at_least(full_minimum_width); From 4c179089937e3f0a62fa5e31a7300d34c321b447 Mon Sep 17 00:00:00 2001 From: RobWalt Date: Wed, 9 Nov 2022 15:02:06 +0100 Subject: [PATCH 2/4] chore(changelog): add line to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b3cf0b8daa..ec11cfb5def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG * Added `egui::gui_zoom` module with helpers for scaling the whole GUI of an app ([#2239](https://github.com/emilk/egui/pull/2239)). * You can now put one interactive widget on top of another, and only one will get interaction at a time ([#2244](https://github.com/emilk/egui/pull/2244)). * Add `ui.centered`. +* Added possibility to specify text wrap for the selected text of `egui::ComboBox` via a `.wrap(width)` method ([#2272](https://github.com/emilk/egui/pull/2244)) ### Changed 🔧 * Panels always have a separator line, but no stroke on other sides. Their spacing has also changed slightly ([#2261](https://github.com/emilk/egui/pull/2261)). From 30ea0f1871dea71296d6eff4186c0b95aa0a3e4b Mon Sep 17 00:00:00 2001 From: RobWalt Date: Thu, 10 Nov 2022 13:35:38 +0100 Subject: [PATCH 3/4] feat(combobox-text-wrap): make wrap boolean - specifying a wrap width didn't really make sense so now it's boolean - the selected text will now use the maximum available width while still respecting the spacing and icon coming after it --- crates/egui/src/containers/combo_box.rs | 42 +++++++++++++++---------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/crates/egui/src/containers/combo_box.rs b/crates/egui/src/containers/combo_box.rs index 383346c0c33..9444bc78c83 100644 --- a/crates/egui/src/containers/combo_box.rs +++ b/crates/egui/src/containers/combo_box.rs @@ -28,7 +28,7 @@ pub struct ComboBox { selected_text: WidgetText, width: Option, icon: Option, - wrap: Option, + wrap_enabled: bool, } impl ComboBox { @@ -40,7 +40,7 @@ impl ComboBox { selected_text: Default::default(), width: None, icon: None, - wrap: None, + wrap_enabled: false, } } @@ -53,7 +53,7 @@ impl ComboBox { selected_text: Default::default(), width: None, icon: None, - wrap: None, + wrap_enabled: false, } } @@ -65,7 +65,7 @@ impl ComboBox { selected_text: Default::default(), width: None, icon: None, - wrap: None, + wrap_enabled: false, } } @@ -116,9 +116,9 @@ impl ComboBox { self } - /// Set the wrap width for the selected text - pub fn wrap(mut self, wrap_width: f32) -> Self { - self.wrap.replace(wrap_width); + /// Controls whether text wrap is used for the selected text + pub fn wrap(mut self, wrap: bool) -> Self { + self.wrap_enabled = wrap; self } @@ -144,7 +144,7 @@ impl ComboBox { selected_text, width, icon, - wrap, + wrap_enabled, } = self; let button_id = ui.make_persistent_id(id_source); @@ -153,7 +153,14 @@ impl ComboBox { if let Some(width) = width { ui.spacing_mut().slider_width = width; // yes, this is ugly. Will remove later. } - let mut ir = combo_box_dyn(ui, button_id, selected_text, menu_contents, icon, wrap); + let mut ir = combo_box_dyn( + ui, + button_id, + selected_text, + menu_contents, + icon, + wrap_enabled, + ); if let Some(label) = label { ir.response .widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, label.text())); @@ -220,22 +227,23 @@ fn combo_box_dyn<'c, R>( selected_text: WidgetText, menu_contents: Box R + 'c>, icon: Option, - wrap: Option, + wrap_enabled: bool, ) -> InnerResponse> { let popup_id = button_id.with("popup"); let is_popup_open = ui.memory().is_popup_open(popup_id); let button_response = button_frame(ui, button_id, is_popup_open, Sense::click(), |ui| { // We don't want to change width when user selects something new - let full_minimum_width = ui.spacing().slider_width; + let full_minimum_width = wrap_enabled + .then(|| ui.available_width() - ui.spacing().item_spacing.x * 2.0) + .unwrap_or_else(|| ui.spacing().slider_width); let icon_size = Vec2::splat(ui.spacing().icon_width); + let wrap_width = wrap_enabled + .then(|| ui.available_width() - ui.spacing().item_spacing.x - icon_size.x) + .unwrap_or(f32::INFINITY); - let galley = selected_text.into_galley( - ui, - wrap.map(|_| true), - wrap.unwrap_or(f32::INFINITY), - TextStyle::Button, - ); + let galley = + selected_text.into_galley(ui, Some(wrap_enabled), wrap_width, TextStyle::Button); let width = galley.size().x + ui.spacing().item_spacing.x + icon_size.x; let width = width.at_least(full_minimum_width); From 8cd7969ece26a2275a766c84e9d24a9452aedde3 Mon Sep 17 00:00:00 2001 From: RobWalt Date: Thu, 10 Nov 2022 13:44:10 +0100 Subject: [PATCH 4/4] feat(combobox-text-wrap): update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec11cfb5def..43bae232496 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG * Added `egui::gui_zoom` module with helpers for scaling the whole GUI of an app ([#2239](https://github.com/emilk/egui/pull/2239)). * You can now put one interactive widget on top of another, and only one will get interaction at a time ([#2244](https://github.com/emilk/egui/pull/2244)). * Add `ui.centered`. -* Added possibility to specify text wrap for the selected text of `egui::ComboBox` via a `.wrap(width)` method ([#2272](https://github.com/emilk/egui/pull/2244)) +* Added possibility to enable text wrap for the selected text of `egui::ComboBox` ([#2272](https://github.com/emilk/egui/pull/2244)) ### Changed 🔧 * Panels always have a separator line, but no stroke on other sides. Their spacing has also changed slightly ([#2261](https://github.com/emilk/egui/pull/2261)).