use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;
use rnk::prelude::*;
struct AppState {
counter: i32,
messages: Vec<String>,
}
fn main() -> std::io::Result<()> {
let state = Arc::new(RwLock::new(AppState {
counter: 0,
messages: vec!["Starting...".to_string()],
}));
let state_clone = Arc::clone(&state);
thread::spawn(move || {
loop {
thread::sleep(Duration::from_millis(500));
{
let mut s = state_clone.write().unwrap();
s.counter += 1;
let count = s.counter;
s.messages
.push(format!("Update #{} from background thread", count));
if s.messages.len() > 5 {
s.messages.remove(0);
}
}
rnk::request_render();
}
});
let state_for_render = Arc::clone(&state);
render(move || app(&state_for_render)).run()
}
fn app(state: &Arc<RwLock<AppState>>) -> Element {
let s = state.read().unwrap();
let mut container = Box::new().flex_direction(FlexDirection::Column).padding(1);
container = container.child(
Text::new("Cross-Thread Render Demo")
.bold()
.color(Color::Cyan)
.into_element(),
);
container = container.child(Text::new("─".repeat(30)).dim().into_element());
container = container.child(
Box::new()
.child(
Text::new(format!("Counter: {}", s.counter))
.color(Color::Green)
.into_element(),
)
.into_element(),
);
container = container.child(Text::new("").into_element());
container = container.child(Text::new("Recent messages:").bold().into_element());
for msg in &s.messages {
container = container.child(Text::new(format!(" • {}", msg)).dim().into_element());
}
container = container.child(Text::new("").into_element());
container = container.child(Text::new("Press Ctrl+C to exit").dim().into_element());
container.into_element()
}