Commit 249f3cc8 authored by Florian Blasius's avatar Florian Blasius 🤘

[wip] mega refactoring api.

parent 19c7682f
......@@ -107,7 +107,7 @@ impl Widget for MainView {
.child(create_header("Text", 2, 0))
.child(
TextBlock::create()
.shared_text(button_count_text.share())
.text_prop(button_count_text.share())
.margin((0.0, 8.0, 0.0, 0.0))
.attach(GridColumn(2))
.attach(GridRow(1)),
......
......@@ -108,7 +108,7 @@ impl Widget for MainView {
.child(
TextBlock::create()
.selector(Selector::new().class("body"))
.shared_text(button_count_text.share())
.text_prop(button_count_text.share())
.margin((0.0, 8.0, 0.0, 0.0))
.attach(GridColumn(2))
.attach(GridRow(1)),
......@@ -127,6 +127,14 @@ impl Widget for MainView {
}
fn main() {
let mut world = World::from_container(Tree::default());
let mut context = WipBuildContext::new(&mut world);
let _wip_button = WipButton::create(&mut context)
.background()
.value("green")
.foreground()
.value("blue");
let mut application = Application::default();
application
......
......@@ -66,9 +66,59 @@ macro_rules! widget {
)
}
// macro_rules! wip_property {
// ($property:ident, $value:ident) => {
// use std::rc::Rc;
// use std::cell::{ Cell, RefCell };
// // use dces::entity::Entity;
// use dces::prelude::ComponentBox;
// use crate::widget::{ WipProperty, WipPropertyBuilder };
// pub enum $property {
// Property($value),
// Builder{ value: Option<$value>, source_chain: Rc<RefCell<Vec<Rc<Cell<Option<Entity>>>>>> }
// }
// impl WipProperty for $property {
// type Value = $value;
// fn value(&self) -> Self::Value {
// match self {
// $property::Property(value) => return value.clone(),
// _ => return $value::default()
// }
// }
// fn set_value(&mut self, value: Self::Value) {
// *self = $property::Property(value);
// }
// }
// impl WipPropertyBuilder for $property {
// type Value = $value;
// fn build(self) -> (Option<ComponentBox>, Option<Rc<RefCell<Vec<Rc<Cell<Option<Entity>>>>>>>) {
// match self {
// $property::Builder { value, source_chain } => {
// if let Some(value) = value {
// return (Some(ComponentBox::new($property::Property(value))), Some(source_chain))
// } else {
// return (None, Some(source_chain));
// }
// },
// _ => (None, None)
// }
// }
// }
// };
// }
/// Used to define a property.
#[macro_export]
macro_rules! wip_property {
($(#[$widget_doc:meta])* $property:ident: $type:ident($(#[$name_doc:meta])* $name:ident, $(#[$prop_name_doc:meta])* $prop_name:ident)) => {
macro_rules! property {
($(#[$widget_doc:meta])* $type:ident, $property:ident, $name:ident, $prop_name:ident) => {
use dces::prelude::{Entity, EntityComponentManager};
use crate::widget::{get_property, Property, Template};
......@@ -80,12 +130,12 @@ macro_rules! wip_property {
Self::from(transform(self.into()))
}
$(#[$name_doc])*
/// Inserts a property.
fn $name<V: Into<$type>>(self, $name: V) -> Self {
self.$prop_name($name.into())
}
$(#[$prop_name_doc])*
/// Inserts a shared property.
fn $prop_name(self, $name: impl Into<Property>) -> Self {
self.template(|template| template.property($name.into())
}
......@@ -106,43 +156,111 @@ macro_rules! wip_property {
};
}
/// Used to define a property.
#[macro_export]
macro_rules! property {
($(#[$widget_doc:meta])* $type:ident, $property:ident, $name:ident, $prop_name:ident) => {
use dces::prelude::{Entity, EntityComponentManager};
macro_rules! wip_widget {
( $(#[$widget_doc:meta])* $widget:ident { $($(#[$prop_doc:meta])* $property:ident: $property_type:tt ),*} ) => {
$(#[$widget_doc])*
pub struct $type {
template: Template
}
use crate::widget::{get_property, Property, Template};
impl $type {
/// Creates a new widget.
pub fn new() -> Self {
$type {
template: Template::new()
}
}
$(#[$widget_doc])*
pub trait $property: Sized + From<Template> + Into<Template> {
/// Transforms the property into a template.
fn template<F: FnOnce(Template) -> Template>(self, transform: F) -> Self {
Self::from(transform(self.into()))
pub fn child(mut self, child: Template) -> Self {
self.template = template.child(child);
self
}
}
/// Inserts a property.
fn $name<V: Into<$type>>(self, $name: V) -> Self {
self.$prop_name($name.into())
}
$(
$(#[$prop_doc])*
pub fn $property(mut self, $property: impl Into<$Property>) -> Self {
self.template.property($property.into());
self
}
)*
};
}
/// Inserts a shared property.
fn $prop_name(self, $name: impl Into<Property>) -> Self {
self.template(|template| template.property($name.into())
}
/*
pub struct Button {
build_context: &mut BuildContext,
id: Entity,
}
pub struct BuildContext {
world: &mut World,
}
impl BuildContext {
fn property() -> Entity,
...
}
pub fn new(build_context: &mut BuildContext) -> Self {
let id = build_context.create_widget();
Button {
build_context,
id,
}
impl From<$type> for Property {
fn from(prop: $type) -> Self {
Property::new(prop)
}
}
Button::create(build_context).background().value("green").foreground().value("blue").attach(GridColumn(0)).attach_source::<GridRow>::(self.id).build()
pub struct Property {
widget: Button,
}
impl Property<P: Component> {
pub fn value(self, value: impl Into<P>) -> Button {
self.widget
}
pub fn source(id: Entity) -> Button {
}
}
fn background(self) -> Property {
Property {
widget: self,
}
}
impl $type {
/// Returns the value of a property.
pub fn get(entity: Entity, ecm: &EntityComponentManager) -> $type {
get_property::<$type>(entity, ecm)
fn attach<P: Component>(property: impl Into<C>) -> Self {
}
fn build(mut self) -> Template {
Template::new()
.child(Container::new(self.build_context).background().source(self.id))
}
*/
#[macro_export]
macro_rules! wip_property {
($property:ident($type:ty)) => {
pub struct $property(pub $type);
impl From<$property> for $type {
fn from(property: $property) -> $type {
property.0.into()
}
}
}
impl From<$type> for $property {
fn from(value: $type) -> $property {
$property(value)
}
}
};
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ pub struct Padding {
value: Thickness,
}
property!(Padding, PaddingProperty, padding, shared_padding);
property!(Padding, PaddingProperty, padding, padding_prop);
impl Spacer for Padding {
fn left(&self) -> f64 {
......
......@@ -4,16 +4,15 @@ use crate::structs::{Brush, Color};
#[derive(Clone)]
pub struct Background(pub Brush);
wip_property!(
/// Used to set the background of a widget.
BackgroundProperty: Background(
/// Sets the background.
background,
/// Sets the background property.
background_prop
)
property!(
Background,
BackgroundProperty,
background,
background_prop
);
// wip_property!(WipBackground, Brush);
impl From<Background> for Color {
fn from(b: Background) -> Color {
b.0.into()
......@@ -30,4 +29,10 @@ impl From<&str> for Background {
fn from(s: &str) -> Background {
Background(s.into())
}
}
\ No newline at end of file
}
// impl Into<Property> for Background {
// fn into(background: Background) -> Property {
// Property::new(background)
// }
// }
\ No newline at end of file
......@@ -8,7 +8,7 @@ property!(
BorderBrush,
BorderBrushProperty,
border_brush,
shared_border_brush
border_brush_prop
);
impl From<BorderBrush> for Color {
......
......@@ -6,7 +6,7 @@ property!(
BorderRadius,
BorderRadiusProperty,
border_radius,
shared_border_radius
border_radius_prop
);
impl From<f64> for BorderRadius {
......
......@@ -8,7 +8,7 @@ property!(
BorderThickness,
BorderThicknessProperty,
border_thickness,
shared_border_thickness
border_thickness_prop
);
impl Spacer for BorderThickness {
......
......@@ -6,7 +6,7 @@ use crate::styling::fonts;
#[derive(Clone)]
pub struct Font(pub OrbFont);
property!(Font, FontProperty, font, shared_font);
property!(Font, FontProperty, font, font_prop);
impl Default for Font {
fn default() -> Self {
......
......@@ -2,7 +2,7 @@
#[derive(Default, Clone, Copy)]
pub struct FontSize(pub f64);
property!(FontSize, FontSizeProperty, font_size, shared_font_size);
property!(FontSize, FontSizeProperty, font_size, font_size_prop);
impl From<f64> for FontSize {
fn from(t: f64) -> Self {
......
......@@ -8,7 +8,7 @@ property!(
Foreground,
ForegroundProperty,
foreground,
shared_foreground
foreground_prop
);
impl From<Foreground> for Color {
......
......@@ -4,7 +4,7 @@ use crate::structs::{Brush, Color};
#[derive(Clone)]
pub struct IconBrush(pub Brush);
property!(IconBrush, IconBrushProperty, icon_brush, shared_icon_brush);
property!(IconBrush, IconBrushProperty, icon_brush, icon_brush_prop);
impl From<IconBrush> for Color {
fn from(b: IconBrush) -> Color {
......
......@@ -6,7 +6,7 @@ use crate::styling::fonts;
#[derive(Clone)]
pub struct IconFont(pub OrbFont);
property!(IconFont, IconFontProperty, icon_font, shared_icon_font);
property!(IconFont, IconFontProperty, icon_font, icon_font_prop);
impl Default for IconFont {
fn default() -> Self {
......
......@@ -2,7 +2,7 @@
#[derive(Default, Clone, Copy)]
pub struct IconSize(pub f64);
property!(IconSize, IconSizeProperty, icon_size, shared_icon_size);
property!(IconSize, IconSizeProperty, icon_size, icon_size_prop);
impl From<f64> for IconSize {
fn from(t: f64) -> Self {
......
......@@ -2,7 +2,7 @@
#[derive(Default, Clone)]
pub struct FontIcon(pub String);
property!(FontIcon, FontIconProperty, font_icon, shared_font_icon);
property!(FontIcon, FontIconProperty, font_icon, font_icon_prop);
impl From<&str> for FontIcon {
fn from(s: &str) -> FontIcon {
......
......@@ -2,7 +2,7 @@
#[derive(Default, Clone)]
pub struct Text(pub String);
property!(Text, TextProperty, text, shared_text);
property!(Text, TextProperty, text, text_prop);
impl From<&str> for Text {
fn from(s: &str) -> Text {
......
......@@ -48,7 +48,7 @@ impl Widget for Button {
let border_thickness: Property = BorderThickness::from(0.0).into();
let border_brush: Property = BorderBrush::from("transparent").into();
let padding: Property = Padding::from((8.0, 0.0, 8.0, 0.0)).into();
let _opacity: Property = Opacity::from(1.0).into();
let opacity: Property = Opacity::from(1.0).into();
Button::new()
.height(32.0)
......@@ -66,37 +66,37 @@ impl Widget for Button {
.child(
FontIconBlock::create()
.margin((0.0, 0.0, 2.0, 0.0))
.shared_font_icon(icon.share())
.shared_icon_brush(icon_brush.share())
.shared_icon_size(icon_size.share())
.shared_icon_font(icon_font.share()),
.font_icon_prop(icon.share())
.icon_brush_prop(icon_brush.share())
.icon_size_prop(icon_size.share())
.icon_font_prop(icon_font.share()),
)
.child(
TextBlock::create()
.shared_foreground(foreground.share())
.shared_text(text.share())
.shared_font(font.share())
.shared_font_size(font_size.share()),
.foreground_prop(foreground.share())
.text_prop(text.share())
.font_prop(font.share())
.font_size_prop(font_size.share()),
),
)
.shared_padding(padding.share())
.padding_prop(padding.share())
.background_prop(background.share())
.shared_border_radius(border_radius.share())
.shared_border_thickness(border_thickness.share())
.shared_border_brush(border_brush.share()),
.border_radius_prop(border_radius.share())
.border_thickness_prop(border_thickness.share())
.border_brush_prop(border_brush.share()),
)
.shared_text(text)
.shared_font(font)
.shared_font_size(font_size)
.shared_font_icon(icon)
.shared_icon_brush(icon_brush)
.shared_icon_size(icon_size)
.shared_icon_font(icon_font)
.shared_foreground(foreground)
.text_prop(text)
.font_prop(font)
.font_size_prop(font_size)
.font_icon_prop(icon)
.icon_brush_prop(icon_brush)
.icon_size_prop(icon_size)
.icon_font_prop(icon_font)
.foreground_prop(foreground)
.background_prop(background)
.shared_border_radius(border_radius)
.shared_border_thickness(border_thickness)
.shared_border_brush(border_brush)
.shared_padding(padding)
.border_radius_prop(border_radius)
.border_thickness_prop(border_thickness)
.border_brush_prop(border_brush)
.padding_prop(opacity)
}
}
......@@ -41,7 +41,7 @@ impl Widget for CheckBox {
FontIconBlock::create()
.vertical_alignment("Center")
.horizontal_alignment("Center")
.shared_font_icon(icon.share())
.font_icon_prop(icon.share())
.shared_selector(selector.share()),
),
)
......@@ -49,12 +49,12 @@ impl Widget for CheckBox {
TextBlock::create()
.vertical_alignment("Center")
.margin((8.0, 0.0, 0.0, 0.0))
.shared_text(text.share())
.text_prop(text.share())
.shared_selector(selector.share()),
),
)
.shared_font_icon(icon)
.shared_text(text)
.font_icon_prop(icon)
.text_prop(text)
.shared_selector(selector)
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ use crate::theme::Selector;
pub use self::context::Context;
pub use self::message::{MessageBox, StringMessage};
pub use self::property::{get_property, PropertyResult, Property};
pub use self::property::{get_property, PropertyResult, Property, WipProperty, WipPropertyBuilder};
pub use self::state::State;
pub use self::template::{Template, TemplateBase};
pub use self::widget_container::WidgetContainer;
......
......@@ -15,6 +15,19 @@ pub enum PropertyResult {
PropertyNotFound,
}
pub trait WipProperty {
type Value;
fn value(&self) -> Self::Value;
fn set_value(&mut self, value: Self::Value);
}
pub trait WipPropertyBuilder {
type Value;
fn build(self) -> (Option<ComponentBox>, Option<Rc<RefCell<Vec<Rc<Cell<Option<Entity>>>>>>>);
}
/// The `Property` struct is used to define shared properties for widgets. A shared property could be shared between different widgets.
/// All references of a shared property will always share the same value. Only the origin shared property contains the concert property, all
/// other cloned shared properties only references to the origin.
......
......@@ -33,3 +33,200 @@ mod text_block;
mod text_box;
mod toggle_button;
mod water_mark_text_block;
use dces::prelude::*;
use crate::application::Tree;
pub struct WipBuildContext<'a> {
world: &'a mut World<Tree>,
}
impl<'a> WipBuildContext<'a> {
pub fn new(world: &'a mut World<Tree>) -> Self {
WipBuildContext {
world,
}
}
pub fn create_entity(&mut self) -> Entity {
self.world.create_entity().build()
}
pub fn register_property<P: Component>(&mut self, entity: Entity, property: P) {
self.world.entity_component_manager().register_component(entity, property);
}
pub fn register_shared_property<P:Component>(&mut self, target: Entity, source: Entity) {
self.world.entity_component_manager().register_shared_component::<P>(target, source);
}
}
pub struct WipTemplate {
id: Entity,
children: Vec<WipTemplate>,
// todo parent type
}
impl WipTemplate {
pub fn new(id: Entity) -> Self {
WipTemplate {
id,
children: vec![],
}
}
pub fn child(mut self, template: WipTemplate) -> Self {
self.children.push(template);
self
}
}
pub struct WipButton<'a> {
id: Entity,
context: &'a mut WipBuildContext<'a>,
}
wip_property!(WipBackground(String));
impl From<&str> for WipBackground {
fn from(s: &str) -> WipBackground {
WipBackground(s.into())
}
}
wip_property!(WipForeground(String));
impl From<&str> for WipForeground {
fn from(s: &str) -> WipForeground {
WipForeground(s.into())
}
}
pub trait WipProperty<'a, P> where P: Component {
type Widget;
fn value(self, value: impl Into<P>) -> Self::Widget;
fn source(self, id: Entity) -> Self::Widget;
}
mod inner {
use std::marker::PhantomData;
use super::{WipButton, WipProperty};
use dces::prelude::{Component, Entity};