Commit 835a8b5c authored by Florian Blasius's avatar Florian Blasius 🤘

[#172] fix crash by integer parameter.

parent 7ae29902
This diff is collapsed.
......@@ -9,4 +9,5 @@ keywords = ["ecs", "tree"]
edition = "2018"
[dependencies]
dces = { git = "https://gitlab.redox-os.org/redox-os/dces-rust.git" }
\ No newline at end of file
dces = { git = "https://gitlab.redox-os.org/redox-os/dces-rust.git" }
# dces = { path = "../../../dces-rust" }
\ No newline at end of file
......@@ -54,20 +54,23 @@ impl Tree {
}
/// Configure the tree iterator with a start node.
pub fn start_node(&self, start_node: Entity) -> &Self {
self.iterator_start_node.set(start_node);
pub fn start_node(&self, start_node: impl Into<Entity>) -> &Self {
self.iterator_start_node.set(start_node.into());
self
}
/// Registers a new entity `entity` as node.
pub fn register_node(&mut self, entity: Entity) {
pub fn register_node(&mut self, entity: impl Into<Entity>) {
let entity = entity.into();
self.children.insert(entity, vec![]);
self.parent.insert(entity, None);
}
/// Appends a `child` entity to the given `parent` entity.
/// Raised `NotFound` error if the parent is not part of the tree.
pub fn append_child(&mut self, parent: Entity, child: Entity) -> Result<Entity, NotFound> {
pub fn append_child(&mut self, parent: impl Into<Entity>, child: impl Into<Entity>) -> Result<Entity, NotFound> {
let parent = parent.into();
let child = child.into();
if let Some(p) = self.children.get_mut(&parent) {
p.push(child);
} else {
......@@ -97,11 +100,12 @@ impl Tree {
}
impl EntityContainer for Tree {
fn register_entity(&mut self, entity: Entity) {
self.register_node(entity);
fn register_entity(&mut self, entity: impl Into<Entity>) {
self.register_node(entity.into());
}
fn remove_entity(&mut self, entity: Entity) {
fn remove_entity(&mut self, entity: impl Into<Entity>) {
let entity = entity.into();
self.children.remove(&entity);
self.parent.remove(&entity);
}
......@@ -189,13 +193,13 @@ mod tests {
assert_eq!(tree.children.len(), 1);
assert_eq!(tree.parent.len(), 1);
assert!(tree.children.get(&0).is_some());
assert!(tree.children.get(&Entity(0)).is_some());
}
#[test]
fn test_append_child() {
let parent: u32 = 0;
let child: u32 = 1;
let parent = Entity(0);
let child = Entity(1);
let mut tree = Tree::new();
tree.register_node(parent);
......@@ -236,7 +240,7 @@ mod tests {
assert_eq!(tree.children.len(), 1);
assert_eq!(tree.parent.len(), 1);
assert!(tree.children.get(&0).is_some());
assert!(tree.children.get(&Entity(0)).is_some());
}
#[test]
......@@ -246,12 +250,12 @@ mod tests {
tree.register_entity(0);
assert_eq!(tree.children.len(), 1);
assert_eq!(tree.parent.len(), 1);
assert!(tree.children.get(&0).is_some());
assert!(tree.children.get(&Entity(0)).is_some());
tree.remove_entity(0);
assert_eq!(tree.children.len(), 0);
assert_eq!(tree.parent.len(), 0);
assert!(tree.children.get(&0).is_none());
assert!(tree.children.get(&Entity(0)).is_none());
}
#[test]
......@@ -276,12 +280,12 @@ mod tests {
let mut iterator = tree.into_iter();
assert_eq!(0, iterator.next().unwrap());
assert_eq!(1, iterator.next().unwrap());
assert_eq!(3, iterator.next().unwrap());
assert_eq!(4, iterator.next().unwrap());
assert_eq!(2, iterator.next().unwrap());
assert_eq!(5, iterator.next().unwrap());
assert_eq!(6, iterator.next().unwrap());
assert_eq!(Entity(0), iterator.next().unwrap());
assert_eq!(Entity(1), iterator.next().unwrap());
assert_eq!(Entity(3), iterator.next().unwrap());
assert_eq!(Entity(4), iterator.next().unwrap());
assert_eq!(Entity(2), iterator.next().unwrap());
assert_eq!(Entity(5), iterator.next().unwrap());
assert_eq!(Entity(6), iterator.next().unwrap());
}
}
......@@ -11,7 +11,6 @@ pub use self::margin::*;
pub use self::offset::*;
pub use self::orientation::*;
pub use self::padding::*;
pub use self::point::*;
pub use self::position::*;
pub use self::row::*;
pub use self::row_span::*;
......@@ -30,7 +29,6 @@ mod margin;
mod offset;
mod orientation;
mod padding;
mod point;
mod position;
mod row;
mod row_span;
......
......@@ -10,3 +10,9 @@ impl From<f64> for Offset {
Offset((t, t))
}
}
impl From<(i32, i32)> for Offset {
fn from(s: (i32, i32)) -> Offset {
Offset::from((s.0 as f64, s.1 as f64))
}
}
// use std::ops::{Add, Sub};
// /// This struct represents a non visual point.
// #[derive(Copy, Clone, Debug, Default)]
// pub struct Point {
// pub x: f64,
// pub y: f64,
// }
// impl Point {
// /// Creates a new point.
// pub fn new(x: f64, y: f64) -> Self {
// Point { x: x, y: y }
// }
// }
// impl Add for Point {
// type Output = Point;
// /// Adds the given `other` point to self point.
// fn add(self, other: Point) -> Self::Output {
// Point {
// x: self.x + other.x,
// y: self.y + other.y,
// }
// }
// }
// impl Sub for Point {
// type Output = Point;
// /// Subs the given `other` point from self point.
// fn sub(self, other: Point) -> Self::Output {
// Point {
// x: self.x - other.x,
// y: self.y - other.y,
// }
// }
// }
......@@ -9,4 +9,9 @@ impl From<(f64, f64)> for Pos {
fn from(t: (f64, f64)) -> Self {
Pos::from(Point::new(t.0, t.1))
}
}
impl From<(i32, i32)> for Pos {
fn from(s: (i32, i32)) -> Pos {
Pos::from((s.0 as f64, s.1 as f64))
}
}
\ No newline at end of file
......@@ -4,3 +4,9 @@ property!(
/// `IconSize` describes the degree to which the corners of a Border are rounded.
BorderRadius(f64)
);
impl From<i32> for BorderRadius {
fn from(s: i32) -> BorderRadius {
BorderRadius(s as f64)
}
}
\ No newline at end of file
......@@ -4,3 +4,9 @@ property!(
/// `FontSize` describes the font size of a text element.
FontSize(f64)
);
impl From<i32> for FontSize {
fn from(s: i32) -> FontSize {
FontSize(s as f64)
}
}
\ No newline at end of file
......@@ -4,3 +4,9 @@ property!(
/// `IconSize` describes the icon size of a text element.
IconSize(f64)
);
impl From<i32> for IconSize {
fn from(s: i32) -> IconSize {
IconSize(s as f64)
}
}
\ No newline at end of file
......@@ -4,3 +4,9 @@ property!(
/// `Opacity` describes the opacity of a widget.
Opacity(f64)
);
impl From<i32> for Opacity {
fn from(s: i32) -> Opacity {
Opacity(s as f64)
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ pub struct InitSystem {
impl InitSystem {
// init css ids.
fn init_id(&self, node: Entity, ecm: &mut EntityComponentManager) {
fn init_id(&self, node: Entity, ecm: &mut EntityComponentManager, root: Entity) {
// Add css id to global id map.
let id = if let Ok(selector) = ecm.borrow_component::<Selector>(node) {
if let Some(id) = &selector.0.id {
......@@ -25,7 +25,7 @@ impl InitSystem {
};
if let Some((entity, id)) = id {
if let Ok(global) = ecm.borrow_mut_component::<Global>(0) {
if let Ok(global) = ecm.borrow_mut_component::<Global>(root) {
global.id_map.insert(id, entity);
}
}
......@@ -57,8 +57,9 @@ impl System<Tree> for InitSystem {
let window_shell = &mut self.backend.borrow_mut();
// init css ids
let root = tree.root;
for node in tree.into_iter() {
self.init_id(node, ecm);
self.init_id(node, ecm, root);
let mut context = Context::new(
node,
......@@ -81,7 +82,7 @@ fn print_tree(entity: Entity, depth: usize, tree: &Tree, ecm: &mut EntityCompone
let name = Name::get(entity, ecm);
let selector = Selector::get(entity, ecm);
println!("{}{} (entity: {}{})", "| ".repeat(depth), name, entity, selector);
println!("{}{} (entity: {}{})", "| ".repeat(depth), name, entity.0, selector);
for child in tree.children.get(&entity).unwrap() {
print_tree(*child, depth + 1, tree, ecm);
......
......@@ -88,7 +88,7 @@ impl<'a> Context<'a> {
/// Sends a message to the widget with the given id over the message channel.
pub fn send_message(&mut self, target_widget: &str, message: impl Into<MessageBox>) {
let mut entity = None;
if let Ok(global) = self.ecm.borrow_component::<Global>(0) {
if let Ok(global) = self.ecm.borrow_component::<Global>(0.into()) {
if let Some(en) = global.id_map.get(target_widget) {
entity = Some(*en);
}
......
......@@ -32,7 +32,7 @@ impl<'a> WidgetContainer<'a> {
panic!(
"Entity {} does not contain property type {:?}",
self.current_node,
self.current_node.0,
TypeId::of::<P>()
);
}
......@@ -52,7 +52,7 @@ impl<'a> WidgetContainer<'a> {
panic!(
"Entity {} does not contain property type {:?}",
self.current_node,
self.current_node.0,
TypeId::of::<P>()
);
}
......@@ -85,7 +85,7 @@ impl<'a> WidgetContainer<'a> {
panic!(
"Entity {} does not contain property type {:?}",
self.current_node,
self.current_node.0,
TypeId::of::<P>()
);
}
......@@ -117,7 +117,7 @@ impl<'a> WidgetContainer<'a> {
panic!(
"Entity {} does not contain property type {:?}",
self.current_node,
self.current_node.0,
TypeId::of::<P>()
);
}
......
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