StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | //! A UI sample demonstrating how the SelectableArea element can be used. |
| 2 | |
| 3 | use strato_ui::fonts::FamilyId; |
| 4 | use strato_ui::SingletonEntity as _; |
| 5 | use strato_ui::{ |
| 6 | elements::{ |
| 7 | Border, ChildView, ConstrainedBox, Container, Flex, ParentElement, Rect, SelectableArea, |
| 8 | SelectionHandle, Stack, Text, |
| 9 | }, |
| 10 | AppContext, Element, Entity, TypedActionView, View, ViewContext, ViewHandle, |
| 11 | }; |
| 12 | |
| 13 | use strato_ui::color::ColorU; |
| 14 | |
| 15 | pub struct RootView { |
| 16 | sub_view: ViewHandle<SelectableExampleView>, |
| 17 | } |
| 18 | |
| 19 | impl RootView { |
| 20 | pub fn new(ctx: &mut ViewContext<Self>) -> Self { |
| 21 | let sub_view = ctx.add_view(|ctx| { |
| 22 | let font_family = strato_ui::fonts::Cache::handle(ctx).update(ctx, |cache, _| { |
| 23 | cache.load_system_font("Menlo").expect("Should load Menlo") |
| 24 | }); |
| 25 | let view = SelectableExampleView { |
| 26 | font_family, |
| 27 | selectable_area_state_handle_1: Default::default(), |
| 28 | selectable_area_state_handle_2: Default::default(), |
| 29 | }; |
| 30 | ctx.focus_self(); |
| 31 | view |
| 32 | }); |
| 33 | Self { sub_view } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Entity for RootView { |
| 38 | type Event = (); |
| 39 | } |
| 40 | |
| 41 | impl View for RootView { |
| 42 | fn ui_name() -> &'static str { |
| 43 | "RootView" |
| 44 | } |
| 45 | |
| 46 | fn render(&self, _ctx: &AppContext) -> Box<dyn Element> { |
| 47 | ChildView::new(&self.sub_view).finish() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub struct SelectableExampleView { |
| 52 | font_family: FamilyId, |
| 53 | selectable_area_state_handle_1: SelectionHandle, |
| 54 | selectable_area_state_handle_2: SelectionHandle, |
| 55 | } |
| 56 | |
| 57 | impl Entity for SelectableExampleView { |
| 58 | type Event = (); |
| 59 | } |
| 60 | |
| 61 | impl View for SelectableExampleView { |
| 62 | fn ui_name() -> &'static str { |
| 63 | "SelectableExampleView" |
| 64 | } |
| 65 | |
| 66 | fn render(&self, _: &AppContext) -> Box<dyn Element> { |
| 67 | Stack::new() |
| 68 | .with_child(Rect::new().with_background_color(ColorU::black()).finish()) |
| 69 | .with_child( |
| 70 | Flex::column() |
| 71 | .with_child( |
| 72 | SelectableArea::new( |
| 73 | self.selectable_area_state_handle_1.clone(), |
| 74 | |selection_args, _, _| { |
| 75 | println!("SELECTED TEXT - {:?}", selection_args.selection); |
| 76 | }, |
| 77 | Container::new( |
| 78 | ConstrainedBox::new( |
| 79 | Flex::row() |
| 80 | .with_child( |
| 81 | Container::new( |
| 82 | Flex::column() |
| 83 | .with_children([ |
| 84 | Container::new( |
| 85 | Text::new( |
| 86 | "HELLO WORLD 1", |
| 87 | self.font_family, |
| 88 | 16., |
| 89 | ) |
| 90 | .finish(), |
| 91 | ) |
| 92 | .with_vertical_margin(10.) |
| 93 | .finish(), |
| 94 | ConstrainedBox::new( |
| 95 | Text::new( |
| 96 | "HELLO WORLD 2", |
| 97 | self.font_family, |
| 98 | 16., |
| 99 | ) |
| 100 | .finish(), |
| 101 | ) |
| 102 | .with_width(400.) |
| 103 | .with_height(400.) |
| 104 | .finish(), |
| 105 | Container::new( |
| 106 | Text::new_inline( |
| 107 | "HELLO WORLD 3", |
| 108 | self.font_family, |
| 109 | 16., |
| 110 | ) |
| 111 | .finish(), |
| 112 | ) |
| 113 | .with_vertical_margin(10.) |
| 114 | .finish(), |
| 115 | ]) |
| 116 | .finish(), |
| 117 | ) |
| 118 | .with_horizontal_margin(10.) |
| 119 | .with_uniform_padding(10.) |
| 120 | .with_border( |
| 121 | Border::all(1.0).with_border_fill(ColorU::new( |
| 122 | 255, 194, 255, 255, |
| 123 | )), |
| 124 | ) |
| 125 | .finish(), |
| 126 | ) |
| 127 | .with_child( |
| 128 | Container::new( |
| 129 | Flex::column() |
| 130 | .with_children([ |
| 131 | Container::new( |
| 132 | Text::new_inline( |
| 133 | "HELLO WORLD 4", |
| 134 | self.font_family, |
| 135 | 16., |
| 136 | ) |
| 137 | .finish(), |
| 138 | ) |
| 139 | .with_vertical_margin(10.) |
| 140 | .finish(), |
| 141 | ConstrainedBox::new( |
| 142 | Text::new( |
| 143 | "HELLO WORLD 5", |
| 144 | self.font_family, |
| 145 | 16., |
| 146 | ) |
| 147 | .finish(), |
| 148 | ) |
| 149 | .finish(), |
| 150 | Container::new( |
| 151 | Text::new_inline( |
| 152 | "HELLO WORLD 6", |
| 153 | self.font_family, |
| 154 | 16., |
| 155 | ) |
| 156 | .finish(), |
| 157 | ) |
| 158 | .with_vertical_margin(10.) |
| 159 | .finish(), |
| 160 | ]) |
| 161 | .finish(), |
| 162 | ) |
| 163 | .with_horizontal_margin(10.) |
| 164 | .with_uniform_padding(10.) |
| 165 | .with_border( |
| 166 | Border::all(1.0).with_border_fill(ColorU::new( |
| 167 | 255, 194, 255, 255, |
| 168 | )), |
| 169 | ) |
| 170 | .finish(), |
| 171 | ) |
| 172 | .finish(), |
| 173 | ) |
| 174 | .finish(), |
| 175 | ) |
| 176 | .with_uniform_padding(100.) |
| 177 | .finish(), |
| 178 | ) |
| 179 | .finish(), |
| 180 | ) |
| 181 | .with_child( |
| 182 | SelectableArea::new( |
| 183 | self.selectable_area_state_handle_2.clone(), |
| 184 | |selection_args, _, _| { |
| 185 | println!("SELECTED TEXT - {:?}", selection_args.selection); |
| 186 | }, |
| 187 | Container::new( |
| 188 | ConstrainedBox::new( |
| 189 | Flex::row() |
| 190 | .with_child( |
| 191 | Container::new( |
| 192 | Flex::column() |
| 193 | .with_children([ |
| 194 | Container::new( |
| 195 | Text::new_inline( |
| 196 | "HELLO WORLD 11", |
| 197 | self.font_family, |
| 198 | 16., |
| 199 | ) |
| 200 | .finish(), |
| 201 | ) |
| 202 | .with_vertical_margin(10.) |
| 203 | .finish(), |
| 204 | ConstrainedBox::new( |
| 205 | Text::new( |
| 206 | "HELLO WORLD 22", |
| 207 | self.font_family, |
| 208 | 16., |
| 209 | ) |
| 210 | .finish(), |
| 211 | ) |
| 212 | .finish(), |
| 213 | Container::new( |
| 214 | Text::new_inline( |
| 215 | "HELLO WORLD 33", |
| 216 | self.font_family, |
| 217 | 16., |
| 218 | ) |
| 219 | .finish(), |
| 220 | ) |
| 221 | .with_vertical_margin(10.) |
| 222 | .finish(), |
| 223 | ]) |
| 224 | .finish(), |
| 225 | ) |
| 226 | .with_horizontal_margin(10.) |
| 227 | .with_uniform_padding(10.) |
| 228 | .with_border( |
| 229 | Border::all(1.0).with_border_fill(ColorU::new( |
| 230 | 255, 194, 255, 255, |
| 231 | )), |
| 232 | ) |
| 233 | .finish(), |
| 234 | ) |
| 235 | .with_child( |
| 236 | Container::new( |
| 237 | Flex::column() |
| 238 | .with_children([ |
| 239 | Container::new( |
| 240 | Text::new_inline( |
| 241 | "HELLO WORLD 44", |
| 242 | self.font_family, |
| 243 | 16., |
| 244 | ) |
| 245 | .finish(), |
| 246 | ) |
| 247 | .with_vertical_margin(10.) |
| 248 | .finish(), |
| 249 | ConstrainedBox::new( |
| 250 | Text::new( |
| 251 | "HELLO 👀👀👀 WORLD 55", |
| 252 | self.font_family, |
| 253 | 16., |
| 254 | ) |
| 255 | .finish(), |
| 256 | ) |
| 257 | .with_width(400.) |
| 258 | .with_height(400.) |
| 259 | .finish(), |
| 260 | Container::new( |
| 261 | Text::new_inline( |
| 262 | "HELLO WORLD 66 👀", |
| 263 | self.font_family, |
| 264 | 16., |
| 265 | ) |
| 266 | .finish(), |
| 267 | ) |
| 268 | .with_vertical_margin(10.) |
| 269 | .finish(), |
| 270 | ]) |
| 271 | .finish(), |
| 272 | ) |
| 273 | .with_horizontal_margin(10.) |
| 274 | .with_uniform_padding(10.) |
| 275 | .with_border( |
| 276 | Border::all(1.0).with_border_fill(ColorU::new( |
| 277 | 255, 194, 255, 255, |
| 278 | )), |
| 279 | ) |
| 280 | .finish(), |
| 281 | ) |
| 282 | .finish(), |
| 283 | ) |
| 284 | .finish(), |
| 285 | ) |
| 286 | .with_uniform_margin(100.) |
| 287 | .finish(), |
| 288 | ) |
| 289 | .finish(), |
| 290 | ) |
| 291 | .finish(), |
| 292 | ) |
| 293 | .finish() |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | impl TypedActionView for RootView { |
| 298 | type Action = (); |
| 299 | } |
| 300 |