use egui::Color32;
use egui::RichText;
use crate::ChipEdit;
use crate::UnownedChipEdit;
pub struct ChipEditBuilder {
chip_edit: ChipEdit,
texts: Vec<String>,
}
impl ChipEditBuilder {
pub fn new(separator: &str) -> Result<Self, String> {
if separator.is_empty() {
Err("separator cannot be empty".to_owned())
} else {
let ret = Self {
chip_edit: ChipEdit {
texts: vec![],
unowned: crate::UnownedChipEdit::new(separator)?,
},
texts: vec![],
};
Ok(ret)
}
}
pub fn texts(mut self, texts: impl IntoIterator<Item = impl ToString>) -> Self {
self.texts = texts.into_iter().map(|s| s.to_string()).collect();
self
}
pub fn chip_colors(mut self, bg_color: Color32, text_color: Color32) -> Self {
self.chip_edit.unowned.chip_bg = Some(bg_color);
self.chip_edit.unowned.chip_fg = Some(text_color);
self
}
pub fn widget_colors(mut self, bg_color: Color32, fg_color: Color32) -> Self {
self.chip_edit.unowned.widget_bg = Some(bg_color);
self.chip_edit.unowned.widget_fg = Some(fg_color);
self
}
pub fn frame(mut self, frame: bool) -> Self {
self.chip_edit.unowned.frame = frame;
self
}
pub fn chip_size(mut self, chip_size: Option<[f32; 2]>) -> Self {
self.chip_edit.unowned.chip_size = chip_size;
self
}
pub fn chip_icon(mut self, icon: Option<RichText>) -> Result<Self, String> {
if matches!(&icon, Some(t) if t.text().chars().count() != 1) {
Err(format!(
"icon text needs to be single char but found {}",
icon.unwrap().text().len()
))
} else {
self.chip_edit.unowned.icon = icon;
Ok(self)
}
}
pub fn build(self) -> ChipEdit {
let Self {
mut chip_edit,
texts,
} = self;
chip_edit.set_text(texts);
chip_edit.rebuild();
chip_edit
}
pub fn build_unowned(self) -> UnownedChipEdit {
let Self {
mut chip_edit,
texts,
} = self;
chip_edit.set_text(texts);
chip_edit.rebuild();
chip_edit.unowned
}
}