use crate::{builder::validator::multi_line_labelinfo::MultiLineLabelInfo, prelude::*}; use std::sync::{ atomic::{AtomicU32, Ordering::SeqCst}, Arc, Mutex, }; use anyhow::Result; use utilities::prelude::*; pub struct MultiLineLabelBuilder { text_alignment: TextAlignment, text_ratio: f32, text_color: Color, text: Option, line_count: u32, background: Option, } impl MultiLineLabelBuilder { pub fn set_text(mut self, text: impl Into) -> Self { self.text = Some(text.into()); self } pub fn set_ratio(mut self, ratio: f32) -> Self { self.text_ratio = ratio; self } pub fn set_text_color(mut self, text_color: impl Into) -> Self { self.text_color = text_color.into(); self } pub fn set_text_alignment(mut self, text_alignment: TextAlignment) -> Self { self.text_alignment = text_alignment; self } pub fn set_line_count(mut self, line_count: u32) -> Self { self.line_count = line_count; self } pub fn set_background(mut self, background: impl Into) -> Self { self.background = Some(background.into()); self } pub fn build(self, gui_handler: Arc) -> Result> { let base_grid = Grid::new(gui_handler.clone(), 1, self.line_count as usize, false)?; base_grid.set_margin(0); base_grid.set_padding(0); if let Some(background) = self.background { base_grid.set_background(background)?; } for i in 0..self.line_count { let label = Label::builder() .set_ratio(self.text_ratio) .set_text_color(self.text_color) .set_text_alignment(self.text_alignment) .build(gui_handler.clone())?; base_grid.attach(label, 0, i as usize, 1, 1)?; } Ok(Arc::new(MultiLineLabel { grid: base_grid, characters_per_line: AtomicU32::new(0), text: Mutex::new(self.text.unwrap_or_default()), splits: Mutex::new(Vec::new()), })) } } pub struct MultiLineLabel { grid: Arc, characters_per_line: AtomicU32, text: Mutex, splits: Mutex>, } impl MultiLineLabel { pub fn builder() -> MultiLineLabelBuilder { MultiLineLabelBuilder { text_alignment: TextAlignment::Center, text_ratio: 0.7, text_color: Color::Black, text: None, line_count: 1, background: None, } } pub fn set_text(&self, text: impl ToString) -> Result<()> { *self.text.lock().unwrap() = text.to_string(); self.update_text() } fn update_text(&self) -> Result<()> { let text = self.text.lock().unwrap(); let splits = text.split(' '); let character_count = self.characters_per_line.load(SeqCst) as usize; let mut current_line = String::new(); let mut lines = Vec::new(); for split in splits { let current_len = current_line.len(); let next_len = split.len() + 1; if (current_len + next_len) <= character_count { if current_line.is_empty() { current_line = split.to_string(); } else { current_line += &format!(" {}", split); } } else { lines.push(current_line); current_line = split.to_string(); } } if !current_line.is_empty() { lines.push(current_line); } *self.splits.lock().unwrap() = lines.clone(); self.iter_label(|label, y| { if y < lines.len() { label.set_text(&lines[y])?; } else { label.set_text("")?; } Ok(()) }) } pub fn text(&self) -> String { self.text.lock().unwrap().clone() } pub fn set_text_color(&self, text_color: Color) -> Result<()> { self.iter_label(|label, _| label.set_text_color(text_color)) } pub fn set_alignment(&self, alignment: TextAlignment) -> Result<()> { self.iter_label(|label, _| label.set_alignment(alignment)) } pub fn set_text_ratio(&self, ratio: f32) -> Result<()> { self.iter_label(|label, _| label.set_text_ratio(ratio)) } pub fn set_background(&self, background: impl Into) -> Result<()> { self.grid.set_background(background) } pub fn try_from( multi_line_label_info: &MultiLineLabelInfo, gui_handler: &Arc, ) -> Result> { let text = multi_line_label_info.text.read().unwrap().clone(); let color = multi_line_label_info.text_color; let mut multi_line_label_builder = MultiLineLabel::builder() .set_text_color(color) .set_text_alignment(multi_line_label_info.text_alignment) .set_line_count(multi_line_label_info.line_count.get()?); if let Some(background_type) = &multi_line_label_info.background_type { multi_line_label_builder = multi_line_label_builder.set_background(background_type.clone()); } if let Some(ratio) = multi_line_label_info.text_ratio { multi_line_label_builder = multi_line_label_builder.set_ratio(ratio); } if !text.is_empty() { multi_line_label_builder = multi_line_label_builder.set_text(text); } multi_line_label_builder.build(gui_handler.clone()) } fn iter_label(&self, f: F) -> Result<()> where F: Fn(&Label, usize) -> Result<()>, { for y in 0..self.grid.dimensions().1 { let child = self.grid.child_at(0, y)?.unwrap(); let gui_element = child.downcast().unwrap(); let label = gui_element.label().unwrap(); f(label, y)?; } Ok(()) } } impl GuiElementTraits for MultiLineLabel { fn gridable(&self) -> Option<&dyn Gridable> { Some(self) } fn visibility(&self) -> Option<&dyn Visibility> { Some(self) } fn downcast<'a>(&'a self) -> Option> { Some(GuiElement::MultiLineLabel(self)) } } impl Visibility for MultiLineLabel { fn visible(&self) -> bool { self.grid.visible() } fn set_visibility(&self, visibility: bool) -> Result<()> { self.grid.set_visibility(visibility) } } impl Gridable for MultiLineLabel { fn set_frame( &self, x: i32, y: i32, w: u32, h: u32, vert_align: VerticalAlign, hori_align: HorizontalAlign, ) -> Result<()> { self.grid.set_frame(x, y, w, h, vert_align, hori_align)?; if let Some(child) = self.grid.child_at(0, 0)? { let gui_element = child.downcast().unwrap(); let label = gui_element.label().unwrap(); let left = label.framable.left(); let right = label.framable.right(); let top = label.framable.top(); let bottom = label.framable.bottom(); let width = right - left; let height = bottom - top; let text_ratio = label.text_ratio(); let text_size = height as f32 * text_ratio; let max_letter_count = (width as f32 / (text_size / 2.0)).floor(); self.characters_per_line .store(max_letter_count as u32, SeqCst); self.update_text()?; } Ok(()) } fn selectable(&self) -> Option<&Arc> { None } fn type_name(&self) -> &str { "MultiLineLabel" } fn set_layer(&self, layer: i32) -> Result<()> { self.grid.set_layer(layer) } }