-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
use crate::{ | ||
composed_area::ComposedArea, platform::Platform, test_platform::TestPlatform, tree::Node, | ||
world::World, | ||
}; | ||
|
||
struct Context<P: Platform> { | ||
tree: Node<ComposedArea>, | ||
world: World, | ||
platform: P, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
composed_area::{ComposedAreaKind, ComposedHtml}, | ||
geo::{Pos, Size}, | ||
geo::{Pos, Rect, Size}, | ||
id::Id, | ||
}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let ctx = Context { | ||
tree: Node::new(ComposedArea { | ||
id: Id::from("root"), | ||
size: Size::new(100.0, 100.0), | ||
abs_pos: Pos::new(0.0, 0.0), | ||
kind: ComposedAreaKind::Canvas, | ||
}) | ||
.nest( | ||
Node::new(ComposedArea { | ||
id: Id::from("child"), | ||
size: Size::new(50.0, 50.0), | ||
abs_pos: Pos::new(10.0, 10.0), | ||
kind: ComposedAreaKind::Canvas, | ||
}) | ||
.nest(Node::new(ComposedArea { | ||
id: Id::from("grandchild"), | ||
size: Size::new(25.0, 25.0), | ||
abs_pos: Pos::new(5.0, 5.0), | ||
kind: ComposedAreaKind::Html(ComposedHtml { | ||
content: "<div>hello</div>".into(), | ||
}), | ||
})), | ||
), | ||
let mut ctx = Context { | ||
platform: TestPlatform { | ||
mouse_pos: Pos::new(0.0, 0.0), | ||
}, | ||
world: World::new(Rect::from((0.0, 0.0, 100.0, 100.0))), | ||
}; | ||
|
||
ctx.tree | ||
.find(|node| node.read(|data| data.value.id == Id::from("grandchild"))) | ||
.unwrap(); | ||
ctx.world.add( | ||
Id::root(), | ||
ComposedArea { | ||
id: Id::from("child"), | ||
size: Size::new(50.0, 50.0), | ||
abs_pos: Pos::new(10.0, 10.0), | ||
kind: ComposedAreaKind::Canvas, | ||
}, | ||
); | ||
|
||
ctx.world.add( | ||
Id::from("child"), | ||
ComposedArea { | ||
id: Id::from("grandchild"), | ||
size: Size::new(25.0, 25.0), | ||
abs_pos: Pos::new(5.0, 5.0), | ||
kind: ComposedAreaKind::Html(ComposedHtml { | ||
content: "<div>hello</div>".into(), | ||
}), | ||
}, | ||
); | ||
|
||
ctx.world.get(Id::from("grandchild")).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::{ | ||
composed_area::{ComposedArea, ComposedAreaKind}, | ||
geo::Rect, | ||
id::Id, | ||
tree::Node, | ||
}; | ||
|
||
pub(crate) struct World(Node<ComposedArea>); | ||
|
||
impl World { | ||
pub(crate) fn new(viewport: Rect) -> Self { | ||
World(Node::new(ComposedArea { | ||
id: Id::root(), | ||
size: viewport.size, | ||
abs_pos: viewport.pos, | ||
kind: ComposedAreaKind::Canvas, | ||
})) | ||
} | ||
|
||
/// Getter for the root node of this world. | ||
pub(crate) fn root(&self) -> Node<ComposedArea> { | ||
self.0.clone() | ||
} | ||
|
||
/// Fetch a node from the tree by it's Id. Returns None if no node with the given Id is found. | ||
pub(crate) fn get(&self, id: Id) -> Option<Node<ComposedArea>> { | ||
self.0.find(|node| node.read(|data| data.value.id == id)) | ||
} | ||
|
||
/// Add a new node to the tree under a specific Id. | ||
/// If the parent Id is not found, this function will panic. | ||
pub(crate) fn add(&mut self, parent_id: Id, area: ComposedArea) { | ||
let parent = self | ||
.0 | ||
.find(|node| node.read(|data| data.value.id == parent_id)) | ||
.expect("parent not found"); | ||
|
||
parent.nest(Node::new(area)); | ||
} | ||
} |