[][src]Function cursive_async_view::default_animation

pub fn default_animation(
    width: usize,
    _height: usize,
    frame_idx: usize
) -> AnimationFrame

The default loading animation for a AsyncView.

Creating your own loading function

As an example a very basic loading function would look like this:

use std::time::{Instant, Duration};
use cursive::Cursive;
use cursive::views::TextView;
use cursive::utils::markup::StyledString;
use cursive_async_view::{AsyncView, AsyncState, AnimationFrame};

fn my_loading_animation(
    _width: usize,
    _height: usize,
    frame_idx: usize,
) -> AnimationFrame {
    let content = if frame_idx < 30 {
        StyledString::plain("loading")
    } else {
        StyledString::plain("content")
    };

    AnimationFrame {
        content,
        next_frame_idx: (frame_idx + 1) % 60,
    }
}

let mut siv = Cursive::default();
let instant = Instant::now();
let async_view = AsyncView::new(&mut siv, move || {
    if instant.elapsed() > Duration::from_secs(5) {
        AsyncState::Available(
            TextView::new("Yay!\n\nThe content has loaded!")
        )
    } else {
        AsyncState::Pending
    }
}).with_animation_fn(my_loading_animation);

siv.add_layer(async_view);
// siv.run();

This animation function will first display loading for half a second and then display content for half a second.

The width and height parameters contain the maximum size the content may have (in characters). The initial frame_idx is 0.