use super::WindowKey;
use crate::files::{DialogBackend, DialogLaunch, DialogReply, DialogResult, SystemDialogs};
use std::sync::Arc;
use winit::window::Window;
struct Running {
launcher: WindowKey,
parent: WindowKey,
reply: DialogReply,
}
pub struct DialogHost {
backend: Box<dyn DialogBackend>,
running: Vec<Running>,
}
impl DialogHost {
pub fn new(backend: impl DialogBackend + 'static) -> Self {
Self {
backend: Box::new(backend),
running: Vec::new(),
}
}
pub fn system() -> Self {
Self::new(SystemDialogs)
}
pub fn launch(
&mut self,
launcher: &WindowKey,
launch: DialogLaunch,
parent: &WindowKey,
window: Option<&Arc<Window>>,
) {
self.running.retain(|r| !r.reply.is_answered());
self.running.push(Running {
launcher: launcher.clone(),
parent: parent.clone(),
reply: launch.reply.clone(),
});
self.backend.launch(&launch.dialog, window, launch.reply);
}
pub fn window_closed(&mut self, closed: &[WindowKey]) {
self.running.retain(|running| {
if closed.contains(&running.parent) {
running.reply.send(DialogResult::Cancelled);
}
!running.reply.is_answered()
&& !closed.contains(&running.parent)
&& !closed.contains(&running.launcher)
});
}
pub fn open(&mut self) -> usize {
self.running.retain(|r| !r.reply.is_answered());
self.running.len()
}
}