use crate::gui_handler::gui::displayable::DisplayableFillType; use crate::prelude::*; use anyhow::Result; use utilities::prelude::*; use super::gridinfo::GridInfo; use super::mandatory::Mandatory; use std::convert::TryFrom; use std::str::from_utf8; use std::sync::{Arc, RwLock, Weak}; use super::validator::{ cow_to_fill_type, cow_to_fill_type_info, cow_to_str, cow_to_text_alignment, str_into, }; pub struct TextFieldInfo { pub id: String, pub x_slot: Mandatory, pub y_slot: Mandatory, pub x_dim: u32, pub y_dim: u32, pub text_ratio: Option, pub text: RwLock, pub text_color: Color, pub text_alignment: TextAlignment, pub background_type: Option, background_fill_type: DisplayableFillType, pub parent: Weak, } impl TextFieldInfo { pub fn new<'a>( attributes: quick_xml::events::attributes::Attributes<'a>, grid: &Arc, ) -> Result { let mut text_field_info = TextFieldInfo { id: String::new(), x_slot: Mandatory::default(), y_slot: Mandatory::default(), x_dim: 1, y_dim: 1, text_ratio: None, text: RwLock::new(String::new()), text_color: Color::default(), text_alignment: TextAlignment::default(), background_type: None, background_fill_type: DisplayableFillType::Expand, parent: Arc::downgrade(grid), }; for attribute_res in attributes { let attribute = attribute_res?; match attribute.key.into_inner() { b"id" => text_field_info.id = cow_to_str(attribute.value), b"x_slot" => text_field_info.x_slot.set(str_into(attribute.value)?), b"y_slot" => text_field_info.y_slot.set(str_into(attribute.value)?), b"x_size" => text_field_info.x_dim = str_into(attribute.value)?, b"y_size" => text_field_info.y_dim = str_into(attribute.value)?, b"text_color" => { let text = cow_to_str(attribute.value); text_field_info.text_color = Color::try_from(text.as_str())?; } b"text_ratio" => text_field_info.text_ratio = Some(str_into(attribute.value)?), b"background" => { text_field_info.background_type = Some(cow_to_fill_type_info(attribute.value)); } b"fill_type" => { text_field_info.background_fill_type = cow_to_fill_type(attribute.value)?; } b"text_alignment" => { text_field_info.text_alignment = cow_to_text_alignment(attribute.value)? } _ => { return Err(anyhow::Error::msg(format!( "Unsupported attribute in Text Field: {}", from_utf8(attribute.key.into_inner())? ))) } } } if let Some(background_type) = &mut text_field_info.background_type { match background_type { FillTypeInfo::Image(_) => {} FillTypeInfo::Color(_, fill_type) => { *fill_type = text_field_info.background_fill_type; } FillTypeInfo::Element(_, fill_type) => { *fill_type = text_field_info.background_fill_type; } } } Ok(text_field_info) } }