summaryrefslogtreecommitdiffstats
path: root/src/node.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-09-14 19:16:06 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-09-14 19:16:06 +0200
commita97401aed2a173260a4abfdb65a77975ce6c0f01 (patch)
treeca85ba2e078ddfeee8e74abd4eaae7c25b031cb2 /src/node.rs
parent8b8f7563ad33dafeadf6238e377748cdec17d67a (diff)
downloadiced-a97401aed2a173260a4abfdb65a77975ce6c0f01.tar.gz
iced-a97401aed2a173260a4abfdb65a77975ce6c0f01.tar.bz2
iced-a97401aed2a173260a4abfdb65a77975ce6c0f01.zip
Rethink workspace structure
Diffstat (limited to 'src/node.rs')
-rw-r--r--src/node.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/node.rs b/src/node.rs
new file mode 100644
index 00000000..1db10d7f
--- /dev/null
+++ b/src/node.rs
@@ -0,0 +1,60 @@
+use stretch::node;
+
+use crate::{Number, Size, Style};
+
+/// The visual requirements of a [`Widget`] and its children.
+///
+/// When there have been changes and the [`Layout`] needs to be recomputed, the
+/// runtime obtains a [`Node`] by calling [`Widget::node`].
+///
+/// [`Style`]: struct.Style.html
+/// [`Widget`]: widget/trait.Widget.html
+/// [`Node`]: struct.Node.html
+/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node
+/// [`Layout`]: struct.Layout.html
+#[derive(Debug)]
+pub struct Node(pub(crate) node::Node);
+
+impl Node {
+ /// Creates a new [`Node`] with the given [`Style`].
+ ///
+ /// [`Node`]: struct.Node.html
+ /// [`Style`]: struct.Style.html
+ pub fn new(style: Style) -> Node {
+ Self::with_children(style, Vec::new())
+ }
+
+ /// Creates a new [`Node`] with the given [`Style`] and a measure function.
+ ///
+ /// This type of node cannot have any children.
+ ///
+ /// You should use this when your [`Widget`] can adapt its contents to the
+ /// size of its container. The measure function will receive the container
+ /// size as a parameter and must compute the size of the [`Node`] inside
+ /// the given bounds (if the `Number` for a dimension is `Undefined` it
+ /// means that it has no boundary).
+ ///
+ /// [`Node`]: struct.Node.html
+ /// [`Style`]: struct.Style.html
+ /// [`Widget`]: widget/trait.Widget.html
+ pub fn with_measure<F>(style: Style, measure: F) -> Node
+ where
+ F: 'static + Fn(Size<Number>) -> Size<f32>,
+ {
+ Node(node::Node::new_leaf(
+ style.0,
+ Box::new(move |size| Ok(measure(size))),
+ ))
+ }
+
+ /// Creates a new [`Node`] with the given [`Style`] and children.
+ ///
+ /// [`Node`]: struct.Node.html
+ /// [`Style`]: struct.Style.html
+ pub fn with_children(style: Style, children: Vec<Node>) -> Node {
+ Node(node::Node::new(
+ style.0,
+ children.iter().map(|c| &c.0).collect(),
+ ))
+ }
+}