Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/examples/animated_images/root_view.rs
1use instant::Instant;
2use strato_ui::{
3 assets::asset_cache::AssetSource,
4 elements::{
5 CacheOption, ConstrainedBox, CrossAxisAlignment, Flex, Image, ParentElement, Shrinkable,
6 Stack,
7 },
8 AppContext, Element, Entity, TypedActionView, View,
9};
10 
11pub struct RootView {
12 animation_start_time: Instant,
13}
14 
15impl RootView {
16 pub fn new() -> Self {
17 println!(
18 "WARN: This example is slow to start up due to the huge GIF. Compiling with --release \
19 helps."
20 );
21 RootView {
22 animation_start_time: Instant::now(),
23 }
24 }
25}
26 
27impl Default for RootView {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32 
33impl Entity for RootView {
34 type Event = ();
35}
36impl View for RootView {
37 fn ui_name() -> &'static str {
38 "RootView"
39 }
40 
41 fn render(&self, _: &AppContext) -> Box<dyn Element> {
42 Stack::new()
43 .with_child(
44 Shrinkable::new(
45 1.,
46 Image::new(
47 AssetSource::Bundled {
48 path: "rustyrain.gif",
49 },
50 CacheOption::Original,
51 )
52 .enable_animation_with_start_time(self.animation_start_time)
53 .cover()
54 .finish(),
55 )
56 .finish(),
57 )
58 .with_child(
59 Flex::row()
60 .with_cross_axis_alignment(CrossAxisAlignment::Center)
61 .with_child(
62 ConstrainedBox::new(
63 Image::new(
64 AssetSource::Bundled {
65 path: "numbers-1000ms.gif",
66 },
67 CacheOption::BySize,
68 )
69 .enable_animation_with_start_time(self.animation_start_time)
70 .finish(),
71 )
72 .with_height(350.)
73 .with_width(350.)
74 .finish(),
75 )
76 .with_child(
77 ConstrainedBox::new(
78 Image::new(
79 AssetSource::Bundled {
80 path: "numbers-750ms.gif",
81 },
82 CacheOption::BySize,
83 )
84 .enable_animation_with_start_time(self.animation_start_time)
85 .finish(),
86 )
87 .with_height(350.)
88 .with_width(350.)
89 .finish(),
90 )
91 .finish(),
92 )
93 .finish()
94 }
95}
96 
97impl TypedActionView for RootView {
98 type Action = ();
99}
100