Skip to content

Commit

Permalink
Create Direction enum
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 20, 2023
1 parent b6d4687 commit 0b99eb1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ impl Component for Counter {
}

fn view<'a>(&mut self, bump: &'a Bump) -> impl View<'a, Self::Message> {
LinearLayout::new((
LinearLayout::column((
format_in!(bump, "High five count: {}", self.count),
LinearLayout::new((
LinearLayout::row((
Text::new("Up high!").on_click(|_| Message::Increment),
Text::new("Down low!").on_click(|_| Message::Decrement),
)),
Expand Down
44 changes: 35 additions & 9 deletions src/element/linear_layout.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
use crate::{Element, Node, WindowMessage};
use crate::{Direction, Element, Node, WindowMessage};
use kurbo::Point;
use skia_safe::surfaces;
use taffy::geometry::Size;

pub struct LinearLayoutElement {
pub(crate) nodes: Vec<Node>,
pub(crate) points: Vec<(Point, Size<f64>)>,
pub(crate) direction: Direction,
}

impl Element for LinearLayoutElement {
fn layout(&mut self) -> taffy::prelude::Size<f64> {
let mut y = 0.;
let mut pos = Point::default();
let mut max = 0f64;
self.points.clear();
for node in &mut self.nodes {
let size = node.element.as_element_mut().layout();
let point = Point::new(0., y);
let point = match self.direction {
Direction::Row => {
let point = pos;
pos.x += size.width;
max = max.max(size.height);
point
}
Direction::Column => {
let point = pos;
pos.y += size.height;
max = max.max(size.width);
point
}
_ => todo!(),
};
self.points.push((point, size));
y += size.height;
}
Size {
width: 1000.,
height: y,
let mut width = pos.x;
let mut height = pos.y;
match self.direction {
Direction::Row => {
height = max;
}
Direction::RowReverse => todo!(),
Direction::Column => width = max,
Direction::ColumnReverse => todo!(),
}
Size { width, height }
}

fn handle(&mut self, msg: crate::WindowMessage, output: &mut Vec<Box<dyn std::any::Any>>) {
Expand All @@ -35,7 +57,7 @@ impl Element for LinearLayoutElement {
{
node.element.as_element_mut().handle(
WindowMessage::Click {
position: Point::new(point.x, position.y - point.y),
position: Point::new(position.x - point.x, position.y - point.y),
},
output,
);
Expand All @@ -54,7 +76,11 @@ impl Element for LinearLayoutElement {
.unwrap();
node.element.as_element_mut().render(surface.canvas());
let image = surface.image_snapshot();
canvas.draw_image(image, skia_safe::Point::new(0., point.y as _), None);
canvas.draw_image(
image,
skia_safe::Point::new(point.x as _, point.y as _),
None,
);
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use element::Element;
mod view;
use kurbo::Point;

pub use view::{LinearLayout, Text, View};
pub use view::{Direction, LinearLayout, Text, View};

mod view_group;
pub use view_group::ViewGroup;
Expand Down
22 changes: 20 additions & 2 deletions src/view/linear_layout.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
use crate::{element::LinearLayoutElement, View, ViewGroup};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Direction {
Row,
RowReverse,
Column,
ColumnReverse,
}

pub struct LinearLayout<V> {
view: V,
direction: Direction,
}

impl<V> LinearLayout<V> {
pub fn new(view: V) -> Self {
Self { view }
pub fn new(direction: Direction, view: V) -> Self {
Self { view, direction }
}

pub fn row(view: V) -> Self {
Self::new(Direction::Row, view)
}

pub fn column(view: V) -> Self {
Self::new(Direction::Column, view)
}
}

Expand All @@ -19,6 +36,7 @@ impl<'a, M, V: ViewGroup<'a, M>> View<'a, M> for LinearLayout<V> {
LinearLayoutElement {
nodes,
points: Vec::new(),
direction: self.direction,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/view/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{element::TextElement, Element};

mod linear_layout;
pub use linear_layout::LinearLayout;
pub use linear_layout::{Direction, LinearLayout};

mod text;
pub use text::Text;
Expand Down

0 comments on commit 0b99eb1

Please sign in to comment.