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-gradient-text/root_view.rs
1use strato_ui::color::ColorU;
2use strato_ui::elements::shimmering_text::{
3 ShimmerConfig, ShimmeringTextElement, ShimmeringTextStateHandle,
4};
5use strato_ui::elements::{Align, ConstrainedBox, ParentElement, Rect, Stack};
6use strato_ui::fonts::FamilyId;
7use strato_ui::SingletonEntity as _;
8use strato_ui::{AppContext, Element, Entity, TypedActionView, View, ViewContext};
9 
10pub struct RootView {
11 text: String,
12 font_family: FamilyId,
13 font_size: f32,
14 start: ColorU,
15 end: ColorU,
16 config: ShimmerConfig,
17 
18 // Persist the animation/layout state across renders.
19 shimmering_text_handle: ShimmeringTextStateHandle,
20}
21 
22impl RootView {
23 pub fn new(ctx: &mut ViewContext<Self>) -> Self {
24 let font_family = strato_ui::fonts::Cache::handle(ctx).update(ctx, |cache, _| {
25 cache
26 .load_system_font("Times")
27 .or_else(|_| cache.load_system_font("Arial"))
28 .expect("Should load a system font")
29 });
30 
31 // Treat start/end as the dim → bright endpoints.
32 let start = ColorU::new(160, 160, 160, 255);
33 let end = ColorU::new(255, 255, 255, 255);
34 
35 Self {
36 text: "Warp shimmer: 👩‍💻with ligatures — fi fl 🇺🇸".to_string(),
37 font_family,
38 font_size: 28.0,
39 start,
40 end,
41 config: ShimmerConfig::default(),
42 shimmering_text_handle: ShimmeringTextStateHandle::new(),
43 }
44 }
45}
46 
47impl Entity for RootView {
48 type Event = ();
49}
50 
51impl View for RootView {
52 fn ui_name() -> &'static str {
53 "AnimatedGradientText"
54 }
55 
56 fn render(&self, _: &AppContext) -> Box<dyn Element> {
57 let shimmering = ShimmeringTextElement::new(
58 self.text.clone(),
59 self.font_family,
60 self.font_size,
61 self.start,
62 self.end,
63 self.config,
64 self.shimmering_text_handle.clone(),
65 )
66 .finish();
67 
68 Stack::new()
69 .with_child(Rect::new().with_background_color(ColorU::black()).finish())
70 .with_child(
71 Align::new(
72 ConstrainedBox::new(shimmering)
73 .with_max_width(900.)
74 .finish(),
75 )
76 .finish(),
77 )
78 .finish()
79 }
80}
81 
82impl TypedActionView for RootView {
83 type Action = ();
84}
85