use std::fmt::Debug;
use relm4::gtk::prelude::{ComboBoxExt, ComboBoxExtManual};
use relm4::{gtk, ComponentSender};
use relm4::{Component, ComponentParts};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleComboBox<E: ToString> {
pub variants: Vec<E>,
pub active_index: Option<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SimpleComboBoxMsg<E: ToString> {
UpdateData(SimpleComboBox<E>),
SetActiveIdx(usize),
#[doc(hidden)]
UpdateIndex(usize),
}
impl<E> Component for SimpleComboBox<E>
where
E: ToString + 'static + Debug,
{
type CommandOutput = ();
type Input = SimpleComboBoxMsg<E>;
type Output = usize;
type Init = Self;
type Root = gtk::ComboBoxText;
type Widgets = gtk::ComboBoxText;
fn init_root() -> Self::Root {
gtk::ComboBoxText::default()
}
fn init(
model: Self::Init,
widgets: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
model.render(&widgets);
widgets.connect_changed(move |combo_box| {
if let Some(active_idx) = combo_box.active() {
sender.input(Self::Input::UpdateIndex(active_idx as usize));
}
});
ComponentParts { model, widgets }
}
fn update_with_view(
&mut self,
widgets: &mut Self::Widgets,
input: Self::Input,
sender: ComponentSender<Self>,
_root: &Self::Root,
) {
match input {
SimpleComboBoxMsg::UpdateIndex(idx) => {
sender.output(idx).ok();
self.active_index = Some(idx);
}
SimpleComboBoxMsg::SetActiveIdx(idx) => {
if idx < self.variants.len() {
self.active_index = Some(idx);
widgets.set_active(u32::try_from(idx).ok());
}
}
SimpleComboBoxMsg::UpdateData(data) => {
*self = data;
self.render(widgets);
}
}
}
}
impl<E> SimpleComboBox<E>
where
E: ToString,
{
fn render(&self, combo_box: >k::ComboBoxText) {
combo_box.remove_all();
for (idx, e) in self.variants.iter().enumerate() {
combo_box.insert_text(idx as i32, &e.to_string());
}
combo_box.set_active(self.active_index.and_then(|val| u32::try_from(val).ok()));
}
#[must_use]
pub fn get_active_elem(&self) -> Option<&E> {
self.active_index.map(|idx| &self.variants[idx])
}
}