summaryrefslogtreecommitdiffstats
path: root/core/src/layout.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2023-09-10 01:14:39 +0200
committerLibravatar GitHub <noreply@github.com>2023-09-10 01:14:39 +0200
commit1af5ff41abdef243588199ef7988666655924a02 (patch)
tree4dfd518dd93b0393e77355867062c60d75e7f358 /core/src/layout.rs
parenta3489e4af960388e9f73988b88df361022a654a4 (diff)
parent1cc5bf59d7c4f47ae47d9a4e22ebaab3ea4975c1 (diff)
downloadiced-1af5ff41abdef243588199ef7988666655924a02.tar.gz
iced-1af5ff41abdef243588199ef7988666655924a02.tar.bz2
iced-1af5ff41abdef243588199ef7988666655924a02.zip
Merge pull request #2058 from iced-rs/explicit-text-caching
Explicit text caching
Diffstat (limited to 'core/src/layout.rs')
-rw-r--r--core/src/layout.rs35
1 files changed, 34 insertions, 1 deletions
diff --git a/core/src/layout.rs b/core/src/layout.rs
index 04954fb9..caf315b6 100644
--- a/core/src/layout.rs
+++ b/core/src/layout.rs
@@ -7,7 +7,7 @@ pub mod flex;
pub use limits::Limits;
pub use node::Node;
-use crate::{Point, Rectangle, Vector};
+use crate::{Point, Rectangle, Size, Vector};
/// The bounds of a [`Node`] and its children, using absolute coordinates.
#[derive(Debug, Clone, Copy)]
@@ -63,3 +63,36 @@ impl<'a> Layout<'a> {
})
}
}
+
+/// Produces a [`Node`] with two children nodes one right next to each other.
+pub fn next_to_each_other(
+ limits: &Limits,
+ spacing: f32,
+ left: impl FnOnce(&Limits) -> Node,
+ right: impl FnOnce(&Limits) -> Node,
+) -> Node {
+ let mut left_node = left(limits);
+ let left_size = left_node.size();
+
+ let right_limits = limits.shrink(Size::new(left_size.width + spacing, 0.0));
+
+ let mut right_node = right(&right_limits);
+ let right_size = right_node.size();
+
+ let (left_y, right_y) = if left_size.height > right_size.height {
+ (0.0, (left_size.height - right_size.height) / 2.0)
+ } else {
+ ((right_size.height - left_size.height) / 2.0, 0.0)
+ };
+
+ left_node.move_to(Point::new(0.0, left_y));
+ right_node.move_to(Point::new(left_size.width + spacing, right_y));
+
+ Node::with_children(
+ Size::new(
+ left_size.width + spacing + right_size.width,
+ left_size.height.max(right_size.height),
+ ),
+ vec![left_node, right_node],
+ )
+}