StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use std::any::Any; |
| 2 | |
| 3 | use pathfinder_color::ColorU; |
| 4 | use pathfinder_geometry::{ |
| 5 | rect::RectF, |
| 6 | vector::{vec2f, Vector2F}, |
| 7 | }; |
| 8 | use strato_ui::elements::{AcceptedByDropTarget, DropTarget, DropTargetData}; |
| 9 | use strato_ui::{ |
| 10 | elements::{ |
| 11 | Align, ConstrainedBox, Container, DragAxis, Draggable, DraggableState, ParentElement, Rect, |
| 12 | Stack, |
| 13 | }, |
| 14 | AppContext, Element, Entity, TypedActionView, View, |
| 15 | }; |
| 16 | |
| 17 | #[derive(Default)] |
| 18 | pub struct RootView { |
| 19 | basic_draggable_state: DraggableState, |
| 20 | horizontal_draggable_state: DraggableState, |
| 21 | vertical_draggable_state: DraggableState, |
| 22 | clamped_draggable_state: DraggableState, |
| 23 | } |
| 24 | |
| 25 | // Implement the entity trait. |
| 26 | impl Entity for RootView { |
| 27 | type Event = (); |
| 28 | } |
| 29 | |
| 30 | // Implement the view trait so RootView could be considered as a view. |
| 31 | impl View for RootView { |
| 32 | fn ui_name() -> &'static str { |
| 33 | "RootView" |
| 34 | } |
| 35 | |
| 36 | // Let's render a simple black rect background. |
| 37 | fn render(&self, _: &AppContext) -> Box<dyn Element> { |
| 38 | Stack::new() |
| 39 | .with_child(Rect::new().with_background_color(ColorU::black()).finish()) |
| 40 | .with_child( |
| 41 | Align::new( |
| 42 | Draggable::new( |
| 43 | self.basic_draggable_state.clone(), |
| 44 | ConstrainedBox::new( |
| 45 | Rect::new() |
| 46 | .with_background_color(ColorU::new(255, 0, 0, 255)) |
| 47 | .finish(), |
| 48 | ) |
| 49 | .with_width(50.) |
| 50 | .with_height(50.) |
| 51 | .finish(), |
| 52 | ) |
| 53 | .on_drag_start(|_, _, _| eprintln!("Regular Drag Start!")) |
| 54 | .on_drop(|_, _, _, drop_data| eprintln!("Regular Drop! Data: {drop_data:?}")) |
| 55 | .with_accepted_by_drop_target_fn(|_, _| AcceptedByDropTarget::Yes) |
| 56 | .with_drag_bounds_callback(|_, window_size| { |
| 57 | Some(RectF::new(Vector2F::zero(), window_size)) |
| 58 | }) |
| 59 | .finish(), |
| 60 | ) |
| 61 | .finish(), |
| 62 | ) |
| 63 | .with_child( |
| 64 | Align::new( |
| 65 | Container::new({ |
| 66 | let draggable = Draggable::new( |
| 67 | self.horizontal_draggable_state.clone(), |
| 68 | ConstrainedBox::new( |
| 69 | Rect::new() |
| 70 | .with_background_color(ColorU::new(255, 0, 255, 255)) |
| 71 | .finish(), |
| 72 | ) |
| 73 | .with_width(100.) |
| 74 | .with_height(50.) |
| 75 | .finish(), |
| 76 | ) |
| 77 | .with_drag_axis(DragAxis::HorizontalOnly) |
| 78 | .on_drag_start(|_, _, _| eprintln!("Horizontal Drag Start!")) |
| 79 | .on_drop(|_, _, _, drop_data| { |
| 80 | eprintln!("Horizontal Drop! Drop data: {drop_data:?}") |
| 81 | }) |
| 82 | .with_accepted_by_drop_target_fn(|_, _| AcceptedByDropTarget::Yes) |
| 83 | .finish(); |
| 84 | DropTarget::new(draggable, DropTargetColor::Magenta).finish() |
| 85 | }) |
| 86 | .with_margin_top(20.) |
| 87 | .finish(), |
| 88 | ) |
| 89 | .top_center() |
| 90 | .finish(), |
| 91 | ) |
| 92 | .with_child( |
| 93 | Align::new( |
| 94 | Container::new( |
| 95 | Draggable::new( |
| 96 | self.vertical_draggable_state.clone(), |
| 97 | ConstrainedBox::new( |
| 98 | Rect::new() |
| 99 | .with_background_color(ColorU::new(0, 255, 255, 255)) |
| 100 | .finish(), |
| 101 | ) |
| 102 | .with_width(50.) |
| 103 | .with_height(100.) |
| 104 | .finish(), |
| 105 | ) |
| 106 | .with_drag_axis(DragAxis::VerticalOnly) |
| 107 | .on_drag_start(|_, _, _| eprintln!("Vertical Drag Start!")) |
| 108 | .on_drop(|_, _, _, _| eprintln!("Vertical Drop!")) |
| 109 | .with_accepted_by_drop_target_fn(|_, _| AcceptedByDropTarget::Yes) |
| 110 | .finish(), |
| 111 | ) |
| 112 | .with_margin_right(20.) |
| 113 | .finish(), |
| 114 | ) |
| 115 | .right() |
| 116 | .finish(), |
| 117 | ) |
| 118 | .with_child( |
| 119 | Align::new( |
| 120 | Container::new({ |
| 121 | let draggable = Draggable::new( |
| 122 | self.clamped_draggable_state.clone(), |
| 123 | ConstrainedBox::new( |
| 124 | Rect::new() |
| 125 | .with_background_color(ColorU::new(255, 255, 0, 255)) |
| 126 | .finish(), |
| 127 | ) |
| 128 | .with_width(50.) |
| 129 | .with_height(50.) |
| 130 | .finish(), |
| 131 | ) |
| 132 | .with_drag_bounds(RectF::new(vec2f(20., 30.), vec2f(200., 400.))) |
| 133 | .on_drag_start(|_, _, _| eprintln!("Clamped Drag Start!")) |
| 134 | .on_drop(|_, _, _, drop_target_data| { |
| 135 | eprintln!("Clamped Drop! Data: {drop_target_data:?}") |
| 136 | }) |
| 137 | .with_accepted_by_drop_target_fn(|_, _| AcceptedByDropTarget::Yes) |
| 138 | .finish(); |
| 139 | |
| 140 | DropTarget::new(draggable, DropTargetColor::Yellow).finish() |
| 141 | }) |
| 142 | .with_margin_left(50.) |
| 143 | .with_margin_top(50.) |
| 144 | .finish(), |
| 145 | ) |
| 146 | .top_left() |
| 147 | .finish(), |
| 148 | ) |
| 149 | .with_child( |
| 150 | Align::new( |
| 151 | DropTarget::new( |
| 152 | ConstrainedBox::new( |
| 153 | Rect::new() |
| 154 | .with_background_color(ColorU::new(0, 0, 255, 255)) |
| 155 | .finish(), |
| 156 | ) |
| 157 | .with_width(100.) |
| 158 | .with_height(100.) |
| 159 | .finish(), |
| 160 | DropTargetColor::Blue, |
| 161 | ) |
| 162 | .finish(), |
| 163 | ) |
| 164 | .bottom_center() |
| 165 | .finish(), |
| 166 | ) |
| 167 | .finish() |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #[derive(Debug)] |
| 172 | enum DropTargetColor { |
| 173 | Yellow, |
| 174 | Blue, |
| 175 | Magenta, |
| 176 | } |
| 177 | |
| 178 | impl DropTargetData for DropTargetColor { |
| 179 | fn as_any(&self) -> &dyn Any { |
| 180 | self |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | impl TypedActionView for RootView { |
| 185 | type Action = (); |
| 186 | } |
| 187 |