Skip to main content

ComboBox

Struct ComboBox 

Source
pub struct ComboBox<'a> { /* private fields */ }
Available on crate feature egui only.
Expand description

A drop-down selection menu with a descriptive label.

let before = selected;
egui::ComboBox::from_label("Select one!")
    .selected_text(format!("{:?}", selected))
    .show_ui(ui, |ui| {
        ui.selectable_value(&mut selected, Enum::First, "First");
        ui.selectable_value(&mut selected, Enum::Second, "Second");
        ui.selectable_value(&mut selected, Enum::Third, "Third");
    }
);

if selected != before {
    // Handle selection change
}

Implementations§

Source§

impl<'a> ComboBox<'a>

Source

pub const CLASS: ClassName

Present on the button of every ComboBox.

Source

pub const CLASS_OPEN: ClassName

Present on the button of a ComboBox while its popup is open.

Source

pub fn new(id_salt: impl AsIdSalt, label: impl IntoAtoms<'a>) -> ComboBox<'a>

Create new ComboBox with id and label

Source

pub fn from_label(label: impl IntoAtoms<'a>) -> ComboBox<'a>

Label shown next to the combo box.

The contents of the label is used as the id salt (see Atoms::salt).

Source

pub fn from_id_salt(id_salt: impl AsIdSalt) -> ComboBox<'a>

Without label.

Source

pub fn width(self, width: f32) -> ComboBox<'a>

Set the outer width of the button and menu.

Default is Spacing::combo_width.

Source

pub fn height(self, height: f32) -> ComboBox<'a>

Set the maximum outer height of the menu.

Default is Spacing::combo_height.

Source

pub fn selected_text(self, selected_text: impl IntoAtoms<'a>) -> ComboBox<'a>

What we show as the currently selected value.

This can be any IntoAtoms, e.g. an image next to some text:

let ferris = egui::include_image!("../../assets/ferris.png");
egui::ComboBox::from_label("Crab")
    .selected_text((egui::Image::new(ferris.clone()).max_height(16.0), "Ferris"))
    .show_ui(ui, |ui| {
        let _ = ui.selectable_label(true, (egui::Image::new(ferris).max_height(16.0), "Ferris"));
    });
Source

pub fn icon( self, icon_fn: impl FnOnce(&Ui, Rect, &WidgetVisuals, bool) + 'static, ) -> ComboBox<'a>

Use the provided function to render a different ComboBox icon. Defaults to a triangle that expands when the cursor is hovering over the ComboBox.

For example:

pub fn filled_triangle(
    ui: &egui::Ui,
    rect: egui::Rect,
    visuals: &egui::style::WidgetVisuals,
    _is_open: bool,
) {
    let rect = egui::Rect::from_center_size(
        rect.center(),
        egui::vec2(rect.width() * 0.6, rect.height() * 0.4),
    );
    ui.painter().add(egui::Shape::convex_polygon(
        vec![rect.left_top(), rect.right_top(), rect.center_bottom()],
        visuals.fg_stroke.color,
        visuals.fg_stroke,
    ));
}

egui::ComboBox::from_id_salt("my-combobox")
    .selected_text(text)
    .icon(filled_triangle)
    .show_ui(ui, |_ui| {});
Source

pub fn wrap_mode(self, wrap_mode: TextWrapMode) -> ComboBox<'a>

Controls the wrap mode used for the selected text.

By default, Ui::wrap_mode will be used, which can be overridden with crate::Style::wrap_mode.

Note that any \n in the text will always produce a new line.

Source

pub fn wrap(self) -> ComboBox<'a>

Source

pub fn truncate(self) -> ComboBox<'a>

Source

pub fn close_behavior(self, close_behavior: PopupCloseBehavior) -> ComboBox<'a>

Controls the close behavior for the popup.

By default, PopupCloseBehavior::CloseOnClick will be used.

Source

pub fn popup_style(self, popup_style: StyleModifier) -> ComboBox<'a>

Set the style of the popup menu.

Could for example be used with crate::containers::menu::menu_style to get the frame-less menu button style.

Source

pub fn show_ui<R>( self, ui: &mut Ui, menu_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse<Option<R>>

Show the combo box, with the given ui code for the menu contents.

Returns InnerResponse { inner: None } if the combo box is closed.

Source

pub fn show_index<Text>( self, ui: &mut Ui, selected: &mut usize, len: usize, get: impl Fn(usize) -> Text, ) -> Response
where Text: IntoAtoms<'a>,

Show a list of items with the given selected index.

let alternatives = ["a", "b", "c", "d"];
let mut selected = 2;
egui::ComboBox::from_label("Select one!").show_index(
    ui,
    &mut selected,
    alternatives.len(),
    |i| alternatives[i]
);
Source

pub fn is_open(ctx: &Context, id: Id) -> bool

Check if the ComboBox with the given id has its popup menu currently opened.

Trait Implementations§

Source§

impl HasClasses for ComboBox<'_>

Source§

fn classes(&self) -> &Classes

Source§

fn classes_mut(&mut self) -> &mut Classes

Source§

fn has_class(&self, class: &str) -> bool

True if the class is present.
Source§

fn add_class(&mut self, class: impl Into<ClassName>) -> &mut Self
where Self: Sized,

Add the given class in-place.
Source§

fn set_class(&mut self, class: impl Into<ClassName>, present: bool) -> &mut Self
where Self: Sized,

Add the given class in-place if present, remove it otherwise. Read more
Source§

fn remove_class(&mut self, class: impl Into<ClassName>) -> &mut Self
where Self: Sized,

Remove the given class in-place.
Source§

fn with_class(self, class: impl Into<ClassName>) -> Self
where Self: Sized,

Add the given class by consuming self.
Source§

fn add_classes(&mut self, classes: Classes) -> &mut Self

Append all the given classes at the end, deduplicating them.
Source§

fn with_classes(self, classes: Classes) -> Self
where Self: Sized,

Append all the given classes at the end, deduplicating them. Consuming self.
Source§

fn add_class_if( &mut self, class: impl Into<ClassName>, condition: bool, ) -> &mut Self
where Self: Sized,

Add a class to the list if the condition is true. Read more
Source§

fn with_class_if(self, class: impl Into<ClassName>, condition: bool) -> Self
where Self: Sized,

Add the given class by consuming self if the condition is true. Read more
Source§

fn iter_classes(&self) -> Iter<'_, ClassName> ⓘ

Iterate over the classes, in the order they were added.
Source§

fn classes_as_slice(&self) -> &[ClassName]

Return the classes as a slice.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for ComboBox<'a>

§

impl<'a> !Send for ComboBox<'a>

§

impl<'a> !Sync for ComboBox<'a>

§

impl<'a> !UnwindSafe for ComboBox<'a>

§

impl<'a> Forget for ComboBox<'a>

§

impl<'a> Freeze for ComboBox<'a>
where NonZero<u64>: Freeze, *const u8 is !null: Freeze, PhantomData<Atom<'a>>: Freeze, *const dyn FnOnce(&Ui, Rect, &WidgetVisuals, bool) is !null: Freeze, PhantomData<dyn FnOnce(&Ui, Rect, &WidgetVisuals, bool)>: Freeze, *const ArcInner<dyn Fn(&mut Style) + Sync + Send> is !null: Freeze, PhantomData<ArcInner<dyn Fn(&mut Style) + Sync + Send>>: Freeze, &'static str: Freeze, *const ArcInner<str> is !null: Freeze, PhantomData<ArcInner<str>>: Freeze, *const ClassName is !null: Freeze, PhantomData<ClassName>: Freeze,

§

impl<'a> Move for ComboBox<'a>

§

impl<'a> Unpin for ComboBox<'a>
where NonZero<u64>: Unpin, *const u8 is !null: Unpin, Arc<RichText>: Unpin, Arc<LayoutJob>: Unpin, Arc<Galley>: Unpin, &'a str: Unpin, String: Unpin, &'static str: Unpin, &'static [u8]: Unpin, Arc<[u8]>: Unpin, Box<dyn FnOnce(&Ui, IntoSizedArgs) -> IntoSizedResult<'static> + 'a>: Unpin, Arc<dyn Fn(&Ui, AtomPaintArgs) + 'a>: Unpin, Box<WidgetAtom<'a>>: Unpin, Box<ContainerAtom<'a>>: Unpin, Box<dyn FnOnce(&Ui, Rect, &WidgetVisuals, bool)>: Unpin, Arc<dyn Fn(&mut Style) + Sync + Send>: Unpin, Arc<str>: Unpin, *const ClassName is !null: Unpin, ClassName: Unpin,

§

impl<'a> UnsafeUnpin for ComboBox<'a>
where NonZeroU64Inner: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<Atom<'a>>: UnsafeUnpin, *const dyn FnOnce(&Ui, Rect, &WidgetVisuals, bool) is !null: UnsafeUnpin, PhantomData<dyn FnOnce(&Ui, Rect, &WidgetVisuals, bool)>: UnsafeUnpin, *const ArcInner<dyn Fn(&mut Style) + Sync + Send> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Fn(&mut Style) + Sync + Send>>: UnsafeUnpin, &'static str: UnsafeUnpin, *const ArcInner<str> is !null: UnsafeUnpin, PhantomData<ArcInner<str>>: UnsafeUnpin, *const ClassName is !null: UnsafeUnpin, PhantomData<ClassName>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more