Commit d48d0e9b authored by Florian Blasius's avatar Florian Blasius 🤘

[api upate] finish TextBox update.

 wip fix property tests.
parent 92ee935e
......@@ -15,7 +15,7 @@ impl MainViewState {
impl State for MainViewState {
fn update(&self, context: &mut Context<'_>) {
if let Ok(button_count_text) = context.widget().borrow_mut::<Text>() {
if let Some(button_count_text) = context.widget().try_get_mut::<Text>() {
button_count_text.0 = format!("Button count: {}", self.counter.get());
}
}
......
......@@ -15,7 +15,7 @@ impl MainViewState {
impl State for MainViewState {
fn update(&self, context: &mut Context<'_>) {
if let Ok(button_count_text) = context.widget().borrow_mut::<Text>() {
if let Some(button_count_text) = context.widget().try_get_mut::<Text>() {
button_count_text.0 = format!("Button count: {}", self.counter.get());
}
}
......
......@@ -63,7 +63,7 @@ impl Layout for FixedSizeLayout {
let size = {
if widget.has::<Image>() {
if let Ok(image) = widget.borrow::<Image>() {
if let Some(image) = widget.try_get::<Image>() {
Some((image.width(), image.height()))
} else {
None
......@@ -74,7 +74,7 @@ impl Layout for FixedSizeLayout {
let font_size = widget.get::<FontSize>();
if text.0.is_empty() {
if let Ok(water_mark) = ecm.borrow_component::<WaterMark>(entity) {
if let Some(water_mark) = widget.try_get::<WaterMark>() {
if water_mark.0.is_empty() {
None
} else {
......
......@@ -111,36 +111,42 @@ impl Layout for TextSelectionLayout {
let vertical_alignment = VerticalAlignment::get(entity, ecm);
let margin = Margin::get(entity, ecm);
let widget = WidgetContainer::new(entity, ecm);
size.1 = vertical_alignment.align_measure(parent_size.1, size.1, margin.top(), margin.bottom());
if widget.has::<Text>() {
let text = widget.get::<Text>();
let font = widget.get::<Font>();
let font_size = widget.get::<FontSize>();
if let Ok(selection) = ecm.borrow_component::<TextSelection>(entity) {
if let Some(text_part) = text.0.get(0..selection.0.start_index) {
pos = FONT_MEASURE
.measure(text_part, &(font.0).0, font_size.0 as u32)
.0 as f64;
if text_part.ends_with(" ") {
pos +=
(FONT_MEASURE.measure("a", &(font.0).0, font_size.0 as u32).0 / 2) as f64;
{
let mut widget = WidgetContainer::new(entity, ecm);
size.1 = vertical_alignment.align_measure(parent_size.1, size.1, margin.top(), margin.bottom());
if widget.has::<Text>() {
let text = widget.get::<Text>();
let font = widget.get::<Font>();
let font_size = widget.get::<FontSize>();
if let Some(selection) = widget.try_get::<TextSelection>() {
if let Some(text_part) = text.0.get(0..selection.0.start_index) {
pos = FONT_MEASURE
.measure(text_part, &(font.0).0, font_size.0 as u32)
.0 as f64;
if text_part.ends_with(" ") {
pos +=
(FONT_MEASURE.measure("a", &(font.0).0, font_size.0 as u32).0 / 2) as f64;
}
}
}
}
}
if let Ok(off) = ecm.borrow_component::<Offset>(entity) {
pos += (off.0).0;
}
if let Some(off) = widget.try_get::<Offset>() {
pos += (off.0).0;
}
if let Some(margin) = widget.try_get_mut::<Margin>() {
margin.set_left(pos);
}
if let Ok(margin) = ecm.borrow_mut_component::<Margin>(entity) {
margin.set_left(pos);
if let Some(bounds) = widget.try_get_mut::<Bounds>() {
bounds.set_width(size.0);
bounds.set_height(size.1);
}
}
for child in &tree.children[&entity] {
......@@ -149,11 +155,6 @@ impl Layout for TextSelectionLayout {
}
}
if let Ok(bounds) = ecm.borrow_mut_component::<Bounds>(entity) {
bounds.set_width(size.0);
bounds.set_height(size.1);
}
self.desired_size.borrow_mut().set_dirty(false);
size
}
......
......@@ -53,7 +53,7 @@ macro_rules! property {
macro_rules! widget {
( $(#[$widget_doc:meta])* $widget:ident $(<$state:ident>)* $(: $( $handler:ident ),*)* $( { $($(#[$prop_doc:meta])* $property:ident: $property_type:tt ),* } )* ) => {
#[allow(dead_code)]
use std::{ any::TypeId, rc::Rc, collections::HashMap, cell::RefCell };
use std::{ any::TypeId, rc::Rc, collections::HashMap, cell::RefCell, fmt::Debug };
use dces::prelude::{Component, ComponentBox, SharedComponentBox };
......@@ -90,7 +90,7 @@ macro_rules! widget {
impl $widget {
/// Sets or shares an attached property.
pub fn attach<P: Component>(mut self, property: impl Into<PropertySource<P>>) -> Self {
pub fn attach<P: Component + PartialEq + Debug>(mut self, property: impl Into<PropertySource<P>>) -> Self {
match property.into() {
PropertySource::Value(value) => {
self.attached_properties.insert(TypeId::of::<P>(), ComponentBox::new(value));
......
......@@ -143,7 +143,7 @@ impl ColumnsBuilder {
/// Helper struct used inside of the columns Property.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ColumnsContainer(Vec<Column>);
pub struct ColumnsContainer(pub Vec<Column>);
property!(
/// `Columns` describes a list of grid columns.
......@@ -204,5 +204,3 @@ impl ColumnExtension for Columns {
}
}
// --- Conversions ---
......@@ -138,6 +138,12 @@ pub trait ConstraintExtension {
/// Sets height.
fn set_height(&mut self, height: f64);
/// Gets the size.
fn size(&self) -> (f64, f64);
/// Sets the size.
fn set_size(&mut self, width: f64, height: f64);
/// Gets min_width.
fn min_width(&self) -> f64;
......@@ -150,6 +156,12 @@ pub trait ConstraintExtension {
/// Sets min_height.
fn set_min_height(&mut self, min_height: f64);
/// Gets the min_size.
fn min_size(&self) -> (f64, f64);
/// Sets the min size.
fn set_min_size(&mut self, min_width: f64, min_height: f64);
/// Gets max_width.
fn max_width(&self) -> f64;
......@@ -162,6 +174,12 @@ pub trait ConstraintExtension {
/// Sets max_height.
fn set_max_height(&mut self, max_height: f64);
/// Gets the max_size.
fn max_size(&self) -> (f64, f64);
/// Sets the max size.
fn set_max_size(&mut self, max_width: f64, max_height: f64);
/// Adjust the given `size` to match the constraint.
fn perform(&self, size: (f64, f64)) -> (f64, f64);
}
......@@ -187,6 +205,15 @@ impl ConstraintExtension for BoxConstraint {
self.height = height;
}
fn size(&self) -> (f64, f64) {
(self.width, self.height)
}
fn set_size(&mut self, width: f64, height: f64) {
self.width = width;
self.height = height;
}
fn min_width(&self) -> f64 {
self.min_width
}
......@@ -203,6 +230,15 @@ impl ConstraintExtension for BoxConstraint {
self.min_height = min_height;
}
fn min_size(&self) -> (f64, f64) {
(self.min_width, self.min_height)
}
fn set_min_size(&mut self, min_width: f64, min_height: f64) {
self.min_width = min_width;
self.min_height = min_height;
}
fn max_width(&self) -> f64 {
self.max_width
}
......@@ -219,6 +255,15 @@ impl ConstraintExtension for BoxConstraint {
self.max_height = max_height;
}
fn max_size(&self) -> (f64, f64) {
(self.max_width, self.max_height)
}
fn set_max_size(&mut self, max_width: f64, max_height: f64) {
self.max_width = max_width;
self.max_height = max_height;
}
fn perform(&self, size: (f64, f64)) -> (f64, f64) {
let size = {
let width = if self.width > 0.0 { self.width } else { size.0 };
......@@ -259,6 +304,15 @@ impl ConstraintExtension for Constraint {
self.0.height = height;
}
fn size(&self) -> (f64, f64) {
(self.0.width, self.0.height)
}
fn set_size(&mut self, width: f64, height: f64) {
self.0.width = width;
self.0.height = height;
}
fn min_width(&self) -> f64 {
self.0.min_width
}
......@@ -275,6 +329,15 @@ impl ConstraintExtension for Constraint {
self.0.min_height = min_height;
}
fn min_size(&self) -> (f64, f64) {
(self.0.min_width, self.0.min_height)
}
fn set_min_size(&mut self, min_width: f64, min_height: f64) {
self.0.min_width = min_width;
self.0.min_height = min_height;
}
fn max_width(&self) -> f64 {
self.0.max_width
}
......@@ -291,6 +354,15 @@ impl ConstraintExtension for Constraint {
self.0.max_height = max_height;
}
fn max_size(&self) -> (f64, f64) {
(self.0.max_width, self.0.max_height)
}
fn set_max_size(&mut self, max_width: f64, max_height: f64) {
self.0.max_width = max_width;
self.0.max_height = max_height;
}
fn perform(&self, size: (f64, f64)) -> (f64, f64) {
self.0.perform(size)
}
......
// use super::*;
// use crate::structs::{Position, Size};
// #[test]
// fn test_contains() {
// let rect = Bounds::new(5.0, 8.0, 100.0, 80.0);
// let inner_point = Point::new(50.0, 50.0);
// let outer_point_one = Point::new(0.0, 0.0);
// let outer_point_two = Point::new(106.0, 80.0);
// assert!(rect.contains((inner_point.x, inner_point.y)));
// assert!(!rect.contains((outer_point_one.x, outer_point_one.y)));
// assert!(!rect.contains((outer_point_two.x, outer_point_two.y)));
// let neg_rect = Bounds::new(-5.0, -8.0, 100.0, 80.0);
// let neg_inner_point = Point::new(-3.0, -6.0);
// let neg_outer_point_one = Point::new(-10.0, -8.0);
// let neg_outer_point_two = Point::new(-5.0, 80.0);
// assert!(neg_rect.contains((neg_inner_point.x, neg_inner_point.y)));
// assert!(!neg_rect.contains((neg_outer_point_one.x, neg_outer_point_one.y)));
// assert!(!neg_rect.contains((neg_outer_point_two.x, neg_outer_point_two.y)));
// }
// #[test]
// fn test_contains_rect() {
// let rect = Bounds::new(5.0, 8.0, 100.0, 80.0);
// let inner_rect = Bounds::new(10.0, 12.0, 20.0, 20.0);
// let outer_rect_one = Bounds::new(0.0, 0.0, 2.0, 3.0);
// let outer_rect_two = Bounds::new(105.0, 89.0, 100.0, 50.0);
// assert!(rect.contains_rect(&inner_rect));
// assert!(!rect.contains_rect(&outer_rect_one));
// assert!(!rect.contains_rect(&outer_rect_two));
// let neg_rect = Bounds::new(-5.0, -8.0, 100.0, 80.0);
// let inner_neg_rect = Bounds::new(-2.0, -6.0, 20.0, 20.0);
// let outer_neg_rect_one = Bounds::new(-30.0, -20.0, 2.0, 3.0);
// let outer_neg_rect_two = Bounds::new(105.0, 89.0, 100.0, 50.0);
// assert!(neg_rect.contains_rect(&inner_neg_rect));
// assert!(!neg_rect.contains_rect(&outer_neg_rect_one));
// assert!(!neg_rect.contains_rect(&outer_neg_rect_two));
// }
// #[test]
// fn test_intersects() {
// let rect = Bounds::new(5.0, 8.0, 100.0, 80.0);
// let inner_rect = Bounds::new(2.0, 6.0, 100.0, 20.0);
// let outer_rect_one = Bounds::new(0.0, 0.0, 2.0, 3.0);
// let outer_rect_two = Bounds::new(105.0, 89.0, 100.0, 50.0);
// assert!(rect.intersects(&inner_rect));
// assert!(!rect.intersects(&outer_rect_one));
// assert!(!rect.intersects(&outer_rect_two));
// let neg_rect = Bounds::new(-5.0, -8.0, 100.0, 80.0);
// let inner_neg_rect = Bounds::new(-2.0, -6.0, 100.0, 120.0);
// let outer_neg_rect_one = Bounds::new(-30.0, -20.0, 2.0, 3.0);
// let outer_neg_rect_two = Bounds::new(105.0, 89.0, 100.0, 50.0);
// assert!(neg_rect.intersects(&inner_neg_rect));
// assert!(!neg_rect.intersects(&outer_neg_rect_one));
// assert!(!neg_rect.intersects(&outer_neg_rect_two));
// }
// #[test]
// fn test_into() {
// let bounds: Bounds = (10.0, 12.0, 13.0, 14.0).into();
// assert_eq!(bounds.x(), 10.0);
// assert_eq!(bounds.y(), 12.0);
// assert_eq!(bounds.width(), 13.0);
// assert_eq!(bounds.height(), 14.0);
// }
use super::*;
use crate::{structs::*, properties::PropertySource};
#[test]
fn test_contains() {
let rect = Bounds(Rect::new(5.0, 8.0, 100.0, 80.0));
let inner_point = Point::new(50.0, 50.0);
let outer_point_one = Point::new(0.0, 0.0);
let outer_point_two = Point::new(106.0, 80.0);
assert!(rect.contains((inner_point.x, inner_point.y)));
assert!(!rect.contains((outer_point_one.x, outer_point_one.y)));
assert!(!rect.contains((outer_point_two.x, outer_point_two.y)));
let neg_rect = Bounds(Rect::new(-5.0, -8.0, 100.0, 80.0));
let neg_inner_point = Point::new(-3.0, -6.0);
let neg_outer_point_one = Point::new(-10.0, -8.0);
let neg_outer_point_two = Point::new(-5.0, 80.0);
assert!(neg_rect.contains((neg_inner_point.x, neg_inner_point.y)));
assert!(!neg_rect.contains((neg_outer_point_one.x, neg_outer_point_one.y)));
assert!(!neg_rect.contains((neg_outer_point_two.x, neg_outer_point_two.y)));
}
#[test]
fn test_contains_rect() {
let rect = Bounds(Rect::new(5.0, 8.0, 100.0, 80.0));
let inner_rect = Bounds(Rect::new(10.0, 12.0, 20.0, 20.0));
let outer_rect_one = Bounds(Rect::new(0.0, 0.0, 2.0, 3.0));
let outer_rect_two = Bounds(Rect::new(105.0, 89.0, 100.0, 50.0));
assert!(rect.contains_rect(&inner_rect));
assert!(!rect.contains_rect(&outer_rect_one));
assert!(!rect.contains_rect(&outer_rect_two));
let neg_rect = Bounds(Rect::new(-5.0, -8.0, 100.0, 80.0));
let inner_neg_rect = Bounds(Rect::new(-2.0, -6.0, 20.0, 20.0));
let outer_neg_rect_one = Bounds(Rect::new(-30.0, -20.0, 2.0, 3.0));
let outer_neg_rect_two = Bounds(Rect::new(105.0, 89.0, 100.0, 50.0));
assert!(neg_rect.contains_rect(&inner_neg_rect));
assert!(!neg_rect.contains_rect(&outer_neg_rect_one));
assert!(!neg_rect.contains_rect(&outer_neg_rect_two));
}
#[test]
fn test_intersects() {
let rect = Bounds(Rect::new(5.0, 8.0, 100.0, 80.0));
let inner_rect = Bounds(Rect::new(2.0, 6.0, 100.0, 20.0));
let outer_rect_one = Bounds(Rect::new(0.0, 0.0, 2.0, 3.0));
let outer_rect_two = Bounds(Rect::new(105.0, 89.0, 100.0, 50.0));
assert!(rect.intersects(&inner_rect));
assert!(!rect.intersects(&outer_rect_one));
assert!(!rect.intersects(&outer_rect_two));
let neg_rect = Bounds(Rect::new(-5.0, -8.0, 100.0, 80.0));
let inner_neg_rect = Bounds(Rect::new(-2.0, -6.0, 100.0, 120.0));
let outer_neg_rect_one = Bounds(Rect::new(-30.0, -20.0, 2.0, 3.0));
let outer_neg_rect_two = Bounds(Rect::new(105.0, 89.0, 100.0, 50.0));
assert!(neg_rect.intersects(&inner_neg_rect));
assert!(!neg_rect.intersects(&outer_neg_rect_one));
assert!(!neg_rect.intersects(&outer_neg_rect_two));
}
#[test]
fn test_set_width() {
let mut bounds = Bounds::default();
bounds.set_width(5.0);
assert_eq!(5.0, bounds.width());
}
#[test]
fn test_set_height() {
let mut bounds = Bounds::default();
bounds.set_height(5.0);
assert_eq!(5.0, bounds.height());
}
#[test]
fn test_set_size() {
let mut bounds = Bounds::default();
bounds.set_size(6.0, 7.0);
assert_eq!(6.0, bounds.width());
assert_eq!(7.0, bounds.height());
}
#[test]
fn test_set_x() {
let mut bounds = Bounds::default();
bounds.set_x(5.0);
assert_eq!(5.0, bounds.x());
}
#[test]
fn test_set_y() {
let mut bounds = Bounds::default();
bounds.set_y(5.0);
assert_eq!(5.0, bounds.y());
}
#[test]
fn test_set_position() {
let mut bounds = Bounds::default();
bounds.set_position(6.0, 7.0);
assert_eq!(6.0, bounds.x());
assert_eq!(7.0, bounds.y());
}
#[test]
fn test_from() {
let bounds = Bounds::from((9.0, 10.0, 11.0, 12.0));
assert_eq!(9.0, bounds.x());
assert_eq!(10.0, bounds.y());
assert_eq!(11.0, bounds.width());
assert_eq!(12.0, bounds.height());
}
#[test]
fn test_into() {
let bounds: PropertySource<Bounds> = (17.0, 18.0, 19.0, 20.0).into();
assert_eq!(bounds, PropertySource::<Bounds>::Value(Bounds(Rect::new(17.0, 18.0, 19.0, 20.0))));
}
\ No newline at end of file
// use super::*;
use super::*;
use crate::properties::PropertySource;
// #[test]
// fn test_width() {
// let width = ColumnWidth::Width(64.0);
#[test]
fn test_width() {
let width = ColumnWidth::Width(64.0);
// let builder = ColumnBuilder::new();
// let column = builder.width(width).build();
let builder = ColumnBuilder::new();
let column = builder.width(width).build();
// assert_eq!(column.width, width);
// }
assert_eq!(column.width, width);
}
// #[test]
// fn test_min_width() {
// let min_width = 64.0;
#[test]
fn test_min_width() {
let min_width = 64.0;
// let builder = ColumnBuilder::new();
// let column = builder.min_width(min_width).build();
let builder = ColumnBuilder::new();
let column = builder.min_width(min_width).build();
// assert_eq!(column.min_width, min_width);
// }
assert_eq!(column.min_width, min_width);
}
// #[test]
// fn test_max_width() {
// let max_width = 64.0;
#[test]
fn test_max_width() {
let max_width = 64.0;
// let builder = ColumnBuilder::new();
// let column = builder.max_width(max_width).build();
let builder = ColumnBuilder::new();
let column = builder.max_width(max_width).build();
// assert_eq!(column.max_width, max_width);
// }
assert_eq!(column.max_width, max_width);
}
// #[test]
// fn test_set_current_width() {
// let out_one_width = 10.0;
// let out_two_width = 66.0;
// let in_width = 33.0;
// let min_width = 14.0;
// let max_width = 64.0;
#[test]
fn test_set_current_width() {
let out_one_width = 10.0;
let out_two_width = 66.0;
let in_width = 33.0;
let min_width = 14.0;
let max_width = 64.0;
// let builder = ColumnBuilder::new();
// let mut column = builder.min_width(min_width).max_width(max_width).build();
let builder = ColumnBuilder::new();
let mut column = builder.min_width(min_width).max_width(max_width).build();
// column.set_current_width(out_one_width);
// assert_eq!(column.current_width(), min_width);
column.set_current_width(out_one_width);
assert_eq!(column.current_width(), min_width);
// column.set_current_width(out_two_width);
// assert_eq!(column.current_width(), max_width);
column.set_current_width(out_two_width);
assert_eq!(column.current_width(), max_width);
// column.set_current_width(in_width);
// assert_eq!(column.current_width(), in_width);
// }
column.set_current_width(in_width);
assert_eq!(column.current_width(), in_width);
}
// #[test]
// fn test_column() {
// let builder = ColumnsBuilder::new();
// let columns = builder.build();
#[test]
fn test_column() {
let builder = ColumnsBuilder::new();
let columns = builder.build();
// assert_eq!(columns.len(), 0);
assert_eq!(columns.len(), 0);
// let builder = ColumnsBuilder::new();
// let columns = builder
// .column(Column::create().build())
// .column(Column::create().build())
// .build();
let builder = ColumnsBuilder::new();
let columns = builder
.column(Column::create().build())
.column(Column::create().build())
.build();
// assert_eq!(columns.len(), 2);
// }
assert_eq!(columns.len(), 2);
}
// #[test]
// fn test_column_width_into() {
// let column: Column = "Auto".into();
// assert_eq!(column.width(), ColumnWidth::Auto);
#[test]
fn test_column_width_into() {
let column: Column = "Auto".into();
assert_eq!(column.width(), ColumnWidth::Auto);
// let column: Column = "auto".into();
// assert_eq!(column.width(), ColumnWidth::Auto);
let column: Column = "auto".into();
assert_eq!(column.width(), ColumnWidth::Auto);
// let column: Column = "Stretch".into();
// assert_eq!(column.width(), ColumnWidth::Stretch);
let column: Column = "Stretch".into();
assert_eq!(column.width(), ColumnWidth::Stretch);
// let column: Column = "stretch".into();
// assert_eq!(column.width(), ColumnWidth::Stretch);
let column: Column = "stretch".into();
assert_eq!(column.width(), ColumnWidth::Stretch);
// let column: Column = "*".into();
// assert_eq!(column.width(), ColumnWidth::Stretch);
let column: Column = "*".into();
assert_eq!(column.width(), ColumnWidth::Stretch);
// let column: Column = "other".into();
// assert_eq!(column.width(), ColumnWidth::Stretch);
let column: Column = "other".into();
assert_eq!(column.width(), ColumnWidth::Stretch);
// let column: Column = 64.0.into();
// assert_eq!(column.width(), ColumnWidth::Width(64.0));
// }
let column: Column = 64.0.into();
assert_eq!(column.width(), ColumnWidth::Width(64.0));
}
// use super::*;
use super::*;
use crate::structs::*;
// #[test]
// fn test_builder_width() {
// let width = 12.0;
#[test]
fn test_builder_width() {
let width = 12.0;
// let constraint = Constraint::create().width(width).build();
let constraint = Constraint::create().width(width).build();
// assert_eq!(constraint.width(), width);
// }
assert_eq!(constraint.width(), width);
}
// #[test]
// fn test_builder_height() {
// let height = 12.0;
#[test]
fn test_builder_height() {
let height = 12.0;
// let constraint = Constraint::create().height(height).build();
let constraint = Constraint::create().height(height).build();
// assert_eq!(constraint.height(), height);
// }
assert_eq!(constraint.height(), height);
}
// #[test]
// fn test_builder_min_width() {
// let width = 12.0;
#[test]
fn test_builder_min_width() {
let width = 12.0;
// let constraint = Constraint::create().min_width(width).build();
let constraint = Constraint::create().min_width(width).build();
// assert_eq!(constraint.min_width(), width);
// }
assert_eq!(constraint.min_width(), width);
}
// #[test]
// fn test_builder_min_height() {
// let height = 12.0;
#[test]
fn test_builder_min_height() {
let height = 12.0;