From f9728b88db5fa23764a8129b376d431c5b5500ef Mon Sep 17 00:00:00 2001 From: Ivan <68190772+IVAN-MK7@users.noreply.github.com> Date: Mon, 19 Dec 2022 10:55:39 +0100 Subject: [PATCH] Combobox .wrap(true) width usage (#2472) * Combobox .wrap(true) width fix .wrap(true) does note use all the available width this fix does not change the original .wrap(false) behaviours * Code comment convention Co-authored-by: IVANMK-7 <68190772+IVANMK-7@users.noreply.github.com> --- crates/egui/src/containers/combo_box.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/containers/combo_box.rs b/crates/egui/src/containers/combo_box.rs index 38e9f8af..9942e899 100644 --- a/crates/egui/src/containers/combo_box.rs +++ b/crates/egui/src/containers/combo_box.rs @@ -265,21 +265,35 @@ fn combo_box_dyn<'c, R>( 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 = if wrap_enabled { - ui.available_width() - ui.spacing().item_spacing.x * 2.0 + // Currently selected value's text will be wrapped if needed, so occupy the available width. + ui.available_width() } else { + // Occupy at least the minimum width assigned to Slider and ComboBox. ui.spacing().slider_width - 2.0 * margin.x }; let icon_size = Vec2::splat(ui.spacing().icon_width); let wrap_width = if wrap_enabled { + // Use the available width, currently selected value's text will be wrapped if exceeds this value. ui.available_width() - ui.spacing().item_spacing.x - icon_size.x } else { + // Use all the width necessary to display the currently selected value's text. f32::INFINITY }; 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; + // The width necessary to contain the whole widget with the currently selected value's text. + let width = if wrap_enabled { + full_minimum_width + } else { + // Occupy at least the minimum width needed to contain the widget with the currently selected value's text. + galley.size().x + ui.spacing().item_spacing.x + icon_size.x + }; + + // Case : wrap_enabled : occupy all the available width. + // Case : !wrap_enabled : occupy at least the minimum width assigned to Slider and ComboBox, + // increase if the currently selected value needs additional horizontal space to fully display its text (up to wrap_width (f32::INFINITY)). let width = width.at_least(full_minimum_width); let height = galley.size().y.max(icon_size.y);