Commit 19c7682f authored by Florian Blasius's avatar Florian Blasius 🤘

[update] widget macro

parent 59d70d55
use orbtk::*;
widget!(MainView ());
widget!(MainView);
impl Widget for MainView {
fn create() -> Self {
......
use orbtk::*;
widget!(MainView ());
widget!(MainView);
impl Widget for MainView {
fn create() -> Self {
......
use orbtk::*;
widget!(MainView ());
widget!(MainView);
impl Widget for MainView {
fn create() -> Self {
......
......@@ -30,7 +30,7 @@ fn create_header(text: &str, grid: usize, column: usize) -> Template {
.into()
}
widget!(MainView ());
widget!(MainView);
impl Widget for MainView {
fn create() -> Self {
......
......@@ -19,7 +19,7 @@ mod sender {
}
}
widget!(SenderView ());
widget!(SenderView);
impl Widget for SenderView {
fn create() -> Self {
......@@ -58,7 +58,7 @@ mod receiver {
}
}
widget!(ReceiverView ());
widget!(ReceiverView);
impl Widget for ReceiverView {
fn create() -> Self {
......
......@@ -30,7 +30,7 @@ fn create_header(text: &str, grid: usize, column: usize) -> Template {
.into()
}
widget!(MainView ());
widget!(MainView);
impl Widget for MainView {
fn create() -> Self {
......
// #[macro_export]
// macro_rules!wip_widget {
// ( $(#[$widget_doc:meta])* $type:ident ) => (
// wip_widget!($type { properties {}, handlers {} });
// );
// ( $(#[$widget_doc:meta])* $type:ident { properties{ $( $prop_name:ident: $prop_type:ty ),* } } ) => (
// wip_widget!($type { properties { $( $prop_name: $prop_type )* }, handlers {} } );
// );
// ( $(#[$widget_doc:meta])* $type:ident { properties { $( $prop_name:ident: $prop_type:ty ),* }, handlers { $( $hand_name:ident: hand_type:ty ),* } } ) => (
// use crate::{
// widget::{ TemplateBase, Template },
// properties::{
// HorizontalAlignmentProperty,
// VerticalAlignmentProperty,
// EnabledProperty,
// VisibilityProperty,
// MarginProperty,
// },
// theme::SelectorProperty,
// };
// $(#[$widget_doc])*
// pub struct $type {
// template: Template
// }
// impl $type {
// /// Creates a new widget.
// pub fn new() -> Self {
// $type {
// template: Template::new()
// }
// }
// $(
// fn $prop_name<V: Into<$prop_type>>(self, $prop_name: V) -> Self {
// self.$shared_method($prop_name.into())
// }
// fn $prop_name_shared(self, $prop_name: impl Into<Property>) -> Self {
// self.template(|template| template.property($prop_name.into())
// }
// )*
// }
// impl From<Template> for $type {
// fn from(template: Template) -> Self {
// $type {
// template
// }
// }
// }
// impl Into<Template> for $type {
// fn into(self) -> Template {
// self.template
// }
// }
// // impl TemplateBase for $type {}
// // impl HorizontalAlignmentProperty for $type {}
// // impl VerticalAlignmentProperty for $type {}
// // impl SelectorProperty for $type {}
// // impl EnabledProperty for $type {}
// // impl VisibilityProperty for $type {}
// // impl MarginProperty for $type {}
// // $(
// // impl $derive for $type {}
// // )*
// )
// }
/// Used to define a widget, with properties and event handlers.
#[macro_export]
macro_rules! widget {
( $(#[$widget_doc:meta])* $type:ident ) => (
widget!( $type () );
);
( $(#[$widget_doc:meta])* $type:ident ( $( $derive:ident ),* ] ) => (
use crate::{
widget::TemplateBase,
......@@ -144,14 +66,55 @@ macro_rules! widget {
)
}
#[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)) => {
use dces::prelude::{Entity, EntityComponentManager};
use crate::widget::{get_property, Property, Template};
$(#[$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()))
}
$(#[$name_doc])*
fn $name<V: Into<$type>>(self, $name: V) -> Self {
self.$prop_name($name.into())
}
$(#[$prop_name_doc])*
fn $prop_name(self, $name: impl Into<Property>) -> Self {
self.template(|template| template.property($name.into())
}
}
impl From<$type> for Property {
fn from(prop: $type) -> Self {
Property::new(prop)
}
}
impl $type {
/// Returns the value of a property.
pub fn get(entity: Entity, ecm: &EntityComponentManager) -> $type {
get_property::<$type>(entity, ecm)
}
}
};
}
/// Used to define a property.
#[macro_export]
macro_rules! property {
($type:ident, $property:ident, $method:ident, $shared_method:ident) => {
($(#[$widget_doc:meta])* $type:ident, $property:ident, $name:ident, $prop_name:ident) => {
use dces::prelude::{Entity, EntityComponentManager};
use crate::widget::{get_property, Property, Template};
$(#[$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 {
......@@ -159,13 +122,13 @@ macro_rules! property {
}
/// Inserts a property.
fn $method<V: Into<$type>>(self, $method: V) -> Self {
self.$shared_method($method.into())
fn $name<V: Into<$type>>(self, $name: V) -> Self {
self.$prop_name($name.into())
}
/// Inserts a shared property.
fn $shared_method(self, $method: impl Into<Property>) -> Self {
self.template(|template| template.property($method.into())
fn $prop_name(self, $name: impl Into<Property>) -> Self {
self.template(|template| template.property($name.into())
}
}
......
......@@ -4,11 +4,14 @@ use crate::structs::{Brush, Color};
#[derive(Clone)]
pub struct Background(pub Brush);
property!(
Background,
BackgroundProperty,
background,
shared_background
wip_property!(
/// Used to set the background of a widget.
BackgroundProperty: Background(
/// Sets the background.
background,
/// Sets the background property.
background_prop
)
);
impl From<Background> for Color {
......
......@@ -80,7 +80,7 @@ impl Widget for Button {
),
)
.shared_padding(padding.share())
.shared_background(background.share())
.background_prop(background.share())
.shared_border_radius(border_radius.share())
.shared_border_thickness(border_thickness.share())
.shared_border_brush(border_brush.share()),
......@@ -93,7 +93,7 @@ impl Widget for Button {
.shared_icon_size(icon_size)
.shared_icon_font(icon_font)
.shared_foreground(foreground)
.shared_background(background)
.background_prop(background)
.shared_border_radius(border_radius)
.shared_border_thickness(border_thickness)
.shared_border_brush(border_brush)
......
......@@ -28,17 +28,3 @@ impl Widget for TextBlock {
.debug_name("TextBlock")
}
}
// pub use abc::*;
// mod abc {
// use crate::properties::Text;
// wip_widget!(
// TestBox {
// properties {
// text: Text
// }
// }
// );
// }
......@@ -263,7 +263,7 @@ impl Widget for TextBox {
.attach(selector.clone())
.attach(focused.share())
.shared_padding(padding.share())
.shared_background(background.share())
.background_prop(background.share())
.shared_border_radius(border_radius.share())
.shared_border_thickness(border_thickness.share())
.shared_border_brush(border_brush.share()),
......@@ -277,7 +277,7 @@ impl Widget for TextBox {
.attach(offset)
.shared_focused(focused)
.shared_padding(padding)
.shared_background(background)
.background_prop(background)
.shared_border_radius(border_radius)
.shared_border_thickness(border_thickness)
.shared_border_brush(border_brush)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment