1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

ComboBox: add builder method for height (#3001)

Matches the already existing `ComboBox::width()`.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
hinto-janai
2024-01-15 04:07:42 -05:00
committed by GitHub
parent 5d2e192927
commit c3bc4e26b0

View File

@@ -2,6 +2,9 @@ use epaint::Shape;
use crate::{style::WidgetVisuals, *};
#[allow(unused_imports)] // Documentation
use crate::style::Spacing;
/// Indicate whether or not a popup will be shown above or below the box.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AboveOrBelow {
@@ -35,6 +38,7 @@ pub struct ComboBox {
label: Option<WidgetText>,
selected_text: WidgetText,
width: Option<f32>,
height: Option<f32>,
icon: Option<IconPainter>,
wrap_enabled: bool,
}
@@ -47,6 +51,7 @@ impl ComboBox {
label: Some(label.into()),
selected_text: Default::default(),
width: None,
height: None,
icon: None,
wrap_enabled: false,
}
@@ -60,6 +65,7 @@ impl ComboBox {
label: Some(label),
selected_text: Default::default(),
width: None,
height: None,
icon: None,
wrap_enabled: false,
}
@@ -72,18 +78,30 @@ impl ComboBox {
label: Default::default(),
selected_text: Default::default(),
width: None,
height: None,
icon: None,
wrap_enabled: false,
}
}
/// Set the outer width of the button and menu.
///
/// Default is [`Spacing::combo_width`].
#[inline]
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
/// Set the maximum outer height of the menu.
///
/// Default is [`Spacing::combo_height`].
#[inline]
pub fn height(mut self, height: f32) -> Self {
self.height = Some(height);
self
}
/// What we show as the currently selected value
#[inline]
pub fn selected_text(mut self, selected_text: impl Into<WidgetText>) -> Self {
@@ -158,6 +176,7 @@ impl ComboBox {
label,
selected_text,
width,
height,
icon,
wrap_enabled,
} = self;
@@ -172,7 +191,7 @@ impl ComboBox {
menu_contents,
icon,
wrap_enabled,
width,
(width, height),
);
if let Some(label) = label {
ir.response
@@ -241,7 +260,7 @@ fn combo_box_dyn<'c, R>(
menu_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
icon: Option<IconPainter>,
wrap_enabled: bool,
width: Option<f32>,
(width, height): (Option<f32>, Option<f32>),
) -> InnerResponse<Option<R>> {
let popup_id = button_id.with("popup");
@@ -335,6 +354,9 @@ fn combo_box_dyn<'c, R>(
if button_response.clicked() {
ui.memory_mut(|mem| mem.toggle_popup(popup_id));
}
let height = height.unwrap_or_else(|| ui.spacing().combo_height);
let inner = crate::popup::popup_above_or_below_widget(
ui,
popup_id,
@@ -342,7 +364,7 @@ fn combo_box_dyn<'c, R>(
above_or_below,
|ui| {
ScrollArea::vertical()
.max_height(ui.spacing().combo_height)
.max_height(height)
.show(ui, menu_contents)
.inner
},