ui/src/builder/validator/textfieldinfo.rs

109 lines
3.5 KiB
Rust
Raw Normal View History

2025-02-27 09:43:07 +00:00
use crate::gui_handler::gui::displayable::DisplayableFillType;
2023-01-16 09:53:52 +00:00
use crate::prelude::*;
use anyhow::Result;
2023-01-16 11:58:59 +00:00
use utilities::prelude::*;
2023-01-16 09:53:52 +00:00
use super::gridinfo::GridInfo;
use super::mandatory::Mandatory;
use std::convert::TryFrom;
use std::str::from_utf8;
use std::sync::{Arc, RwLock, Weak};
2024-04-23 13:31:52 +00:00
use super::validator::{
cow_to_fill_type, cow_to_fill_type_info, cow_to_str, cow_to_text_alignment, str_into,
};
2023-01-16 09:53:52 +00:00
pub struct TextFieldInfo {
pub id: String,
pub x_slot: Mandatory<usize>,
pub y_slot: Mandatory<usize>,
pub x_dim: u32,
pub y_dim: u32,
pub text_ratio: Option<f32>,
pub text: RwLock<String>,
pub text_color: Color,
pub text_alignment: TextAlignment,
pub background_type: Option<FillTypeInfo>,
2024-04-23 13:31:52 +00:00
background_fill_type: DisplayableFillType,
2023-01-16 09:53:52 +00:00
pub parent: Weak<GridInfo>,
}
impl TextFieldInfo {
pub fn new<'a>(
attributes: quick_xml::events::attributes::Attributes<'a>,
grid: &Arc<GridInfo>,
) -> Result<TextFieldInfo> {
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,
2024-04-23 13:31:52 +00:00
background_fill_type: DisplayableFillType::Expand,
2023-01-16 09:53:52 +00:00
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" => {
2024-04-23 13:31:52 +00:00
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)?;
2023-01-16 09:53:52 +00:00
}
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())?
)))
}
}
}
2024-04-23 13:31:52 +00:00
if let Some(background_type) = &mut text_field_info.background_type {
match background_type {
2024-04-24 12:37:02 +00:00
FillTypeInfo::Image(_) => {}
FillTypeInfo::Color(_, fill_type) => {
2024-04-23 13:31:52 +00:00
*fill_type = text_field_info.background_fill_type;
}
FillTypeInfo::Element(_, fill_type) => {
*fill_type = text_field_info.background_fill_type;
}
}
}
2023-01-16 09:53:52 +00:00
Ok(text_field_info)
}
}