use super::types::Animation;
use crate::render::Cell;
use crate::style::Color;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};
pub struct TransitionGroup {
items: Vec<String>,
enter_animation: Option<Animation>,
leave_animation: Option<Animation>,
move_animation: Option<Animation>,
stagger_delay: u64,
props: WidgetProps,
}
impl TransitionGroup {
pub fn new(items: impl IntoIterator<Item = impl Into<String>>) -> Self {
let items: Vec<String> = items.into_iter().map(|s| s.into()).collect();
Self {
items,
enter_animation: None,
leave_animation: None,
move_animation: None,
stagger_delay: 0,
props: WidgetProps::default(),
}
}
pub fn enter(mut self, animation: Animation) -> Self {
self.enter_animation = Some(animation);
self
}
pub fn leave(mut self, animation: Animation) -> Self {
self.leave_animation = Some(animation);
self
}
pub fn move_animation(mut self, animation: Animation) -> Self {
self.move_animation = Some(animation);
self
}
pub fn stagger(mut self, delay_ms: u64) -> Self {
self.stagger_delay = delay_ms;
self
}
pub fn push(&mut self, item: impl Into<String>) {
self.items.push(item.into());
}
pub fn remove(&mut self, index: usize) -> Option<String> {
if index < self.items.len() {
Some(self.items.remove(index))
} else {
None
}
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn items(&self) -> &[String] {
&self.items
}
}
impl Default for TransitionGroup {
fn default() -> Self {
Self::new(Vec::<String>::new())
}
}
impl View for TransitionGroup {
crate::impl_view_meta!("TransitionGroup");
fn render(&self, ctx: &mut RenderContext) {
let area = ctx.area;
let default_bg = Color::BLACK;
let default_fg = Color::WHITE;
for (y, item) in (0_u16..).zip(self.items.iter()) {
if y >= area.height {
break;
}
let mut x: u16 = 0;
for ch in item.chars() {
let cw = crate::utils::char_width(ch) as u16;
if x + cw > area.width {
break;
}
let mut cell = Cell::new(ch);
cell.fg = Some(default_fg);
cell.bg = Some(default_bg);
ctx.set(x, y, cell);
x += cw;
}
}
}
}
impl_styled_view!(TransitionGroup);
impl_props_builders!(TransitionGroup);