abletk/src/context.rs

57 lines
1.6 KiB
Rust
Executable File

use std::collections::vec_deque::VecDeque;
use std::future::Future;
use tokio::runtime::Runtime;
use winit::event_loop::ControlFlow;
use crate::window::WindowBuilder;
/// Contains functionality that is shared across all of the application.
pub struct Context {
control_flow: ControlFlow,
window_queue: VecDeque<WindowBuilder>,
rt: Option<Runtime>,
}
impl Context {
/// Schedules the creation of a new window for the next iteration of the
/// event loop.
pub fn add_window(&mut self, builder: WindowBuilder) {
self.window_queue.push_back(builder);
}
/// Schedules the creation of new windows for the next iteration of the
/// event loop.
pub fn add_windows<const N: usize>(&mut self, builders: [WindowBuilder; N]) {
self.window_queue.extend(builders);
}
/// Block on a future using the internal tokio runtime instance.
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
self.rt.as_ref().unwrap().block_on(future)
}
/// Schedules shutdown for the next iteration of the event loop.
pub fn quit(&mut self) {
self.control_flow = ControlFlow::Exit;
}
pub(crate) fn new() -> Self {
Self {
control_flow: ControlFlow::Wait,
window_queue: VecDeque::new(),
rt: None,
}
}
pub(crate) fn control_flow(&self) -> ControlFlow {
self.control_flow
}
pub(crate) fn pop_window_builder(&mut self) -> Option<WindowBuilder> {
self.window_queue.pop_back()
}
pub(crate) fn set_rt(&mut self, rt: Runtime) {
self.rt = Some(rt)
}
}