Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/freya-core/src/elements/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use torin::{
Alignment,
Direction,
Length,
Order,
Position,
VisibleSize,
},
Expand Down Expand Up @@ -490,6 +491,24 @@ where
self.get_layout().layout.content = content;
self
}

/// Set the order children are stacked in along the direction axis. See [`Order`].
fn order(mut self, order: impl Into<Order>) -> Self {
self.get_layout().layout.order = order.into();
self
}

/// Stack children in their natural order along the direction axis. Shorthand for [`order`](Self::order) set to [`Order::Forward`].
fn forward_order(mut self) -> Self {
self.get_layout().layout.order = Order::forward();
self
}

/// Stack children in reverse order along the direction axis. Shorthand for [`order`](Self::order) set to [`Order::Backward`].
fn backward_order(mut self) -> Self {
self.get_layout().layout.order = Order::backward();
self
}
/// Center children on both axes. Shorthand for [`main_align`](Self::main_align) and [`cross_align`](Self::cross_align) set to [`Alignment::Center`].
fn center(mut self) -> Self {
self.get_layout().layout.main_alignment = Alignment::Center;
Expand Down
1 change: 1 addition & 0 deletions crates/freya-devtools-app/src/components/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn attribute_element(name: &str, attribute: AttributeType<'_>) -> Element {
.into(),
AttributeType::Text(text) => Property::new(name, text).into(),
AttributeType::Direction(direction) => Property::new(name, direction.pretty()).into(),
AttributeType::Order(order) => Property::new(name, order.pretty()).into(),
AttributeType::Position(position) => Property::new(name, position.pretty()).into(),
AttributeType::Content(content) => Property::new(name, content.pretty()).into(),
AttributeType::Alignment(alignment) => Property::new(name, alignment.pretty()).into(),
Expand Down
3 changes: 3 additions & 0 deletions crates/freya-devtools/src/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use torin::{
direction::Direction,
gaps::Gaps,
geometry::Length,
order::Order,
prelude::{
Area,
AreaOf,
Expand Down Expand Up @@ -83,6 +84,7 @@ impl NodeState {
"direction",
AttributeType::Direction(&self.layout.direction),
),
("order", AttributeType::Order(&self.layout.order)),
("padding", AttributeType::Measures(self.layout.padding)),
("margin", AttributeType::Measures(self.layout.margin)),
("position", AttributeType::Position(&self.layout.position)),
Expand Down Expand Up @@ -190,6 +192,7 @@ pub enum AttributeType<'a> {
Measures(Gaps),
CornerRadius(CornerRadius),
Direction(&'a Direction),
Order(&'a Order),
Position(&'a Position),
Content(&'a Content),
Alignment(&'a Alignment),
Expand Down
1 change: 1 addition & 0 deletions crates/freya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub mod prelude {
CursorPoint,
Size2D,
},
order::Order,
position::Position,
size::Size,
visible_size::VisibleSize,
Expand Down
5 changes: 4 additions & 1 deletion crates/torin/src/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ where
// Parent Node is dirty.
parent_is_dirty: bool,
) {
let children = self.tree_adapter.children_of(parent_node_id);
let mut children = self.tree_adapter.children_of(parent_node_id);
if parent_node.order.is_backward() {
children.reverse();
}

let initial_area = *inner_area;

Expand Down
21 changes: 21 additions & 0 deletions crates/torin/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
direction::Direction,
gaps::Gaps,
geometry::Length,
order::Order,
prelude::{
Content,
Position,
Expand Down Expand Up @@ -51,6 +52,9 @@ pub struct Node {
/// Direction in which it's inner Nodes will be stacked
pub direction: Direction,

/// Order in which it's inner Nodes will be stacked along the direction axis
pub order: Order,

/// Position config
pub position: Position,

Expand Down Expand Up @@ -104,6 +108,7 @@ impl Node {
&& self.padding == other.padding
&& self.margin == other.margin
&& self.direction == other.direction
&& self.order == other.order
&& self.position == other.position
&& self.content == other.content
&& self.has_layout_references == other.has_layout_references
Expand All @@ -126,6 +131,22 @@ impl Node {
}
}

/// Construct a new Node given a size, a direction and an order
pub fn from_size_and_direction_and_order(
width: Size,
height: Size,
direction: Direction,
order: Order,
) -> Self {
Self {
width,
height,
direction,
order,
..Default::default()
}
}

/// Construct a new Node given some sizes
pub fn from_sizes(
width: Size,
Expand Down
2 changes: 2 additions & 0 deletions crates/torin/src/values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod alignment;
pub mod content;
pub mod direction;
pub mod gaps;
pub mod order;
pub mod position;
pub mod size;
pub mod visible_size;
Expand All @@ -12,6 +13,7 @@ pub mod prelude {
content::*,
direction::*,
gaps::*,
order::*,
position::*,
size::*,
visible_size::*,
Expand Down
33 changes: 33 additions & 0 deletions crates/torin/src/values/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Eq, Clone, Debug, Default, Copy)]
pub enum Order {
/// Lay children out in their natural order. This is the default.
#[default]
Forward,
/// Lay children out in reverse order along the direction axis.
Backward,
}

impl Order {
/// Use a [`Forward`](Order::Forward) order.
pub fn forward() -> Order {
Order::Forward
}

/// Use a [`Backward`](Order::Backward) order.
pub fn backward() -> Order {
Order::Backward
}

/// Whether children are laid out in reverse order.
pub fn is_backward(&self) -> bool {
matches!(self, Self::Backward)
}

pub fn pretty(&self) -> String {
match self {
Self::Forward => "forward".to_string(),
Self::Backward => "backward".to_string(),
}
}
}
Loading
Loading