Skip to content

Commit

Permalink
ComboBox: add builder method for height (#3001)
Browse files Browse the repository at this point in the history
Matches the already existing `ComboBox::width()`.

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
hinto-janai and emilk authored Jan 15, 2024
1 parent 5d2e192 commit c3bc4e2
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions crates/egui/src/containers/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand All @@ -47,6 +51,7 @@ impl ComboBox {
label: Some(label.into()),
selected_text: Default::default(),
width: None,
height: None,
icon: None,
wrap_enabled: false,
}
Expand All @@ -60,6 +65,7 @@ impl ComboBox {
label: Some(label),
selected_text: Default::default(),
width: None,
height: None,
icon: None,
wrap_enabled: false,
}
Expand All @@ -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 {
Expand Down Expand Up @@ -158,6 +176,7 @@ impl ComboBox {
label,
selected_text,
width,
height,
icon,
wrap_enabled,
} = self;
Expand All @@ -172,7 +191,7 @@ impl ComboBox {
menu_contents,
icon,
wrap_enabled,
width,
(width, height),
);
if let Some(label) = label {
ir.response
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -335,14 +354,17 @@ 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,
&button_response,
above_or_below,
|ui| {
ScrollArea::vertical()
.max_height(ui.spacing().combo_height)
.max_height(height)
.show(ui, menu_contents)
.inner
},
Expand Down

0 comments on commit c3bc4e2

Please sign in to comment.