functora-egui 0.4.2

shadcn/ui-inspired widget library for egui - buttons, inputs, selects, dialogs, and 70+ more components with light/dark theming
Documentation
//! shadcn-styled Select builder struct.

/// A dropdown select widget styled after shadcn/ui.
///
/// `T` must be cloneable, displayable, and comparable. The widget takes a mutable
/// reference to the currently selected value and a slice of options.
#[must_use]
pub struct Select<'a, T: Clone + std::fmt::Display + PartialEq + 'static> {
    pub(crate) selected: &'a mut Option<T>,
    pub(crate) options: &'a [T],
    pub(crate) placeholder: Option<String>,
    pub(crate) width: Option<f32>,
    pub(crate) selected_text_override: Option<String>,
}

impl<'a, T: Clone + std::fmt::Display + PartialEq + 'static> Select<'a, T> {
    pub fn new(selected: &'a mut Option<T>, options: &'a [T]) -> Self {
        Self {
            selected,
            options,
            placeholder: None,
            width: None,
            selected_text_override: None,
        }
    }

    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = Some(placeholder.into());
        self
    }

    pub fn width(mut self, width: f32) -> Self {
        self.width = Some(width);
        self
    }

    /// Override the display text shown in the trigger (instead of `T::to_string()`).
    pub fn selected_text(mut self, text: impl Into<String>) -> Self {
        self.selected_text_override = Some(text.into());
        self
    }

    pub fn show(self, ui: &mut egui::Ui) -> egui::Response {
        ui.add(self)
    }
}

/// Non-Option variant: takes `&mut T` directly (always has a value selected).
#[must_use]
pub struct SelectValue<'a, T: Clone + std::fmt::Display + PartialEq + 'static> {
    pub(crate) selected: &'a mut T,
    pub(crate) options: &'a [T],
    pub(crate) width: Option<f32>,
    pub(crate) selected_text_override: Option<String>,
}

impl<'a, T: Clone + std::fmt::Display + PartialEq + 'static> SelectValue<'a, T> {
    pub fn new(selected: &'a mut T, options: &'a [T]) -> Self {
        Self {
            selected,
            options,
            width: None,
            selected_text_override: None,
        }
    }

    pub fn width(mut self, width: f32) -> Self {
        self.width = Some(width);
        self
    }

    /// Override the display text shown in the trigger.
    pub fn selected_text(mut self, text: impl Into<String>) -> Self {
        self.selected_text_override = Some(text.into());
        self
    }

    pub fn show(self, ui: &mut egui::Ui) -> egui::Response {
        ui.add(self)
    }
}

/// A dropdown select bound to enum values with explicit labels instead of
/// `Display`: each entry pairs a value with its (possibly localized) label.
#[must_use]
pub struct SelectLabeled<'a, T: Clone + PartialEq + 'static> {
    pub(crate) selected: &'a mut Option<T>,
    pub(crate) entries: &'a [(T, String)],
    pub(crate) placeholder: Option<String>,
    pub(crate) width: Option<f32>,
}

impl<'a, T: Clone + PartialEq + 'static> SelectLabeled<'a, T> {
    pub fn new(selected: &'a mut Option<T>, entries: &'a [(T, String)]) -> Self {
        Self {
            selected,
            entries,
            placeholder: None,
            width: None,
        }
    }

    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = Some(placeholder.into());
        self
    }

    pub fn width(mut self, width: f32) -> Self {
        self.width = Some(width);
        self
    }

    pub fn show(self, ui: &mut egui::Ui) -> egui::Response {
        ui.add(self)
    }
}

/// Non-Option labeled variant: takes `&mut T` directly with explicit labels.
#[must_use]
pub struct SelectValueLabeled<'a, T: Clone + PartialEq + 'static> {
    pub(crate) selected: &'a mut T,
    pub(crate) entries: &'a [(T, String)],
    pub(crate) width: Option<f32>,
}

impl<'a, T: Clone + PartialEq + 'static> SelectValueLabeled<'a, T> {
    pub fn new(selected: &'a mut T, entries: &'a [(T, String)]) -> Self {
        Self {
            selected,
            entries,
            width: None,
        }
    }

    pub fn width(mut self, width: f32) -> Self {
        self.width = Some(width);
        self
    }

    pub fn show(self, ui: &mut egui::Ui) -> egui::Response {
        ui.add(self)
    }
}