use std::{collections::HashSet, fmt::Debug};
use dces::prelude::{Component, Entity, StringComponentStore};
pub use self::layout::*;
pub use self::widget::*;
use crate::{into_property_source, render, theming, utils};
mod layout;
mod widget;
pub fn get_property<T>(key: &str, entity: Entity, store: &StringComponentStore) -> T
where
T: Clone + Component,
{
store.get::<T>(key, entity).map(|r| r.clone()).unwrap()
}
pub fn get_property_or_value<T>(
key: &str,
entity: Entity,
store: &StringComponentStore,
value: T,
) -> T
where
T: Clone + Component,
{
if let Ok(property) = store.get::<T>(key, entity).map(|r| r.clone()) {
return property;
}
value
}
#[derive(PartialEq, Debug)]
pub enum PropertySource<P: Component + Debug> {
Source(Entity),
KeySource(String, Entity),
Value(P),
}
impl<P: Component + Debug> From<Entity> for PropertySource<P> {
fn from(entity: Entity) -> Self {
PropertySource::Source(entity)
}
}
pub trait IntoPropertySource<P: Component + Debug> {
fn into_source(self) -> PropertySource<P>;
}
pub struct AttachedProperty<P>
where
P: Component + Debug,
{
pub key: String,
pub property_source: PropertySource<P>,
}
impl<P> AttachedProperty<P>
where
P: Component + Debug,
{
pub fn new(key: impl Into<String>, property_source: impl IntoPropertySource<P>) -> Self {
AttachedProperty {
key: key.into(),
property_source: property_source.into_source(),
}
}
}
into_property_source!(bool);
into_property_source!(String: &str, utils::Value);
into_property_source!(usize);
into_property_source!(u32);
into_property_source!(f32: utils::Value);
into_property_source!(f64: i32, f32, utils::Value);
into_property_source!(i32);
into_property_source!(i64);
into_property_source!(utils::Alignment: &str);
into_property_source!(utils::Brush: &str, utils::Color, utils::Value);
into_property_source!(utils::Orientation: &str);
into_property_source!(utils::Point: f64, i32, (i32, i32), (f64, f64));
into_property_source!(utils::Size: f64, i32, (i32, i32), (f64, f64));
into_property_source!(
utils::Rectangle: (i32, i32, i32, i32),
(f64, f64, f64, f64),
(utils::Point, utils::Size)
);
into_property_source!(
utils::Thickness: i32,
f64,
(i32, i32),
(f64, f64),
(i32, i32, i32, i32),
(f64, f64, f64, f64),
utils::Value
);
into_property_source!(utils::String16: &str, String);
into_property_source!(utils::SelectionMode: &str);
into_property_source!(utils::Visibility: &str);
into_property_source!(Vec<String>);
into_property_source!(utils::Filter: &str, String, Vec<String>, Vec<&str>);
into_property_source!(theming::Selector: &str, String);
into_property_source!(theming::Theme);
into_property_source!(render::Image: &str, String, (u32, u32, Vec<u32>));
into_property_source!(Columns: ColumnsBuilder);
into_property_source!(Constraint: ConstraintBuilder);
into_property_source!(DefaultRenderPipeline);
into_property_source!(Rows: RowsBuilder);
into_property_source!(ScrollViewerMode: (&str, &str));
into_property_source!(SelectedEntities: HashSet<Entity>);
into_property_source!(SelectedIndices: HashSet<usize>);
into_property_source!(TextSelection: (usize, usize));