41 lines
858 B
Rust
41 lines
858 B
Rust
|
use context::prelude::*;
|
||
|
|
||
|
use std::{sync::Arc, thread};
|
||
|
|
||
|
use anyhow::Result;
|
||
|
|
||
|
pub struct LoadingScreen;
|
||
|
|
||
|
impl LoadingScreen {
|
||
|
pub fn load<T, R, L, G>(
|
||
|
context: &Arc<Context>,
|
||
|
gui: Option<Arc<G>>,
|
||
|
loader: L,
|
||
|
on_ready: R,
|
||
|
) -> Result<()>
|
||
|
where
|
||
|
R: Fn(T) -> Result<()> + Send + Sync + 'static,
|
||
|
T: Send + Sync + 'static,
|
||
|
|
||
|
L: FnOnce() -> T + Send + Sync + 'static,
|
||
|
G: TopLevelGui + TopGui + Send + Sync + 'static,
|
||
|
{
|
||
|
if let Some(gui) = &gui {
|
||
|
gui.enable()?;
|
||
|
}
|
||
|
|
||
|
let context = context.clone();
|
||
|
|
||
|
thread::spawn(move || {
|
||
|
let result = loader();
|
||
|
if let Some(gui) = &gui {
|
||
|
gui.disable().unwrap();
|
||
|
}
|
||
|
|
||
|
context.push_event(move || (on_ready)(result));
|
||
|
});
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|