StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use crate::event::DispatchedEvent; |
| 2 | |
| 3 | use super::{ |
| 4 | AfterLayoutContext, AppContext, Element, EventContext, LayoutContext, PaintContext, Point, |
| 5 | SizeConstraint, |
| 6 | }; |
| 7 | use pathfinder_geometry::vector::Vector2F; |
| 8 | |
| 9 | #[derive(Default)] |
| 10 | pub struct Empty { |
| 11 | size: Option<Vector2F>, |
| 12 | origin: Option<Point>, |
| 13 | } |
| 14 | |
| 15 | impl Empty { |
| 16 | pub fn new() -> Self { |
| 17 | Self { |
| 18 | size: None, |
| 19 | origin: None, |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl Element for Empty { |
| 25 | fn layout( |
| 26 | &mut self, |
| 27 | constraint: SizeConstraint, |
| 28 | _: &mut LayoutContext, |
| 29 | _: &AppContext, |
| 30 | ) -> Vector2F { |
| 31 | // Set the size of the element to be the max constraint. If the max constraint is unbounded |
| 32 | // use the min constraint to avoid rendering an unbounded-sized element. |
| 33 | let max_constraint = constraint.max; |
| 34 | |
| 35 | let x = if max_constraint.x().is_infinite() { |
| 36 | constraint.min.x() |
| 37 | } else { |
| 38 | max_constraint.x() |
| 39 | }; |
| 40 | |
| 41 | let y = if max_constraint.y().is_infinite() { |
| 42 | constraint.min.y() |
| 43 | } else { |
| 44 | max_constraint.y() |
| 45 | }; |
| 46 | |
| 47 | let size = Vector2F::new(x, y); |
| 48 | |
| 49 | self.size = Some(size); |
| 50 | size |
| 51 | } |
| 52 | |
| 53 | fn after_layout(&mut self, _: &mut AfterLayoutContext, _: &AppContext) {} |
| 54 | |
| 55 | fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, _: &AppContext) { |
| 56 | self.origin = Some(Point::from_vec2f(origin, ctx.scene.z_index())); |
| 57 | } |
| 58 | |
| 59 | fn dispatch_event( |
| 60 | &mut self, |
| 61 | _: &DispatchedEvent, |
| 62 | _: &mut EventContext, |
| 63 | _: &AppContext, |
| 64 | ) -> bool { |
| 65 | false |
| 66 | } |
| 67 | |
| 68 | fn size(&self) -> Option<Vector2F> { |
| 69 | self.size |
| 70 | } |
| 71 | |
| 72 | fn origin(&self) -> Option<Point> { |
| 73 | self.origin |
| 74 | } |
| 75 | } |
| 76 |