use crate::{layout::Rect, render::Frame, style::Style};
use std::time::Duration;
pub struct LoadingAnimation {
message: String,
frames: Vec<String>,
current_frame: usize,
frame_duration: Duration,
style: Style,
}
impl LoadingAnimation {
pub fn new(message: &str) -> Self {
Self {
message: message.to_string(),
frames: vec!["|".to_string(), "/".to_string(), "-".to_string(), "\\".to_string()],
current_frame: 0,
frame_duration: Duration::from_millis(100),
style: Style::new(),
}
}
pub fn frames(mut self, frames: Vec<String>) -> Self {
self.frames = frames;
self
}
pub fn frame_duration(mut self, duration: Duration) -> Self {
self.frame_duration = duration;
self
}
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn tick(&mut self) {
self.current_frame = (self.current_frame + 1) % self.frames.len();
}
pub fn current_frame(&self) -> &str {
&self.frames[self.current_frame]
}
}
impl super::Component for LoadingAnimation {
fn render(&self, frame: &mut Frame, area: Rect) {
frame.render_loading_animation(&self.message, self.current_frame(), area, self.style.clone());
}
fn handle_event(&mut self, _event: &crate::event::Event) -> bool {
false
}
}