ui: add loading spinner that types text

This commit is contained in:
Michael Mallan 2024-10-07 15:05:53 +01:00
parent 3c46a7337c
commit 1d1e735ae9
No known key found for this signature in database
GPG Key ID: 5177CDCEDB0EABEB

View File

@ -155,3 +155,31 @@ where
Element::new(carousel)
}
}
/// Create a `Carousel` that types out the given `content` one character
/// at a time.
///
/// If `show_empty` is `true`, the text will begin with an empty string.
///
/// `interval` is how long to wait before the next character appears.
///
/// `text_builder` is used to build each `Text` element with the required
/// style etc.
pub fn typing_text_carousel<'a, Message, Theme>(
content: &'a str,
show_empty: bool,
interval: Duration,
text_builder: impl Fn(&'a str) -> iced::widget::Text<'a, Theme, Renderer>,
) -> Carousel<'a, Message, Theme>
where
Theme: 'a + iced::widget::text::StyleSheet,
{
let mut children = Vec::new();
if show_empty {
children.push(text_builder(""));
}
for end_char in 0..content.chars().count() {
children.push(text_builder(&content[0..=end_char]));
}
Carousel::new(interval, children)
}