use super::{DialogResult, FileDialog};
use crate::{files::Notify, Id};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
};
pub(crate) struct DialogInbox {
results: Mutex<Vec<(Id, DialogResult)>>,
notify: Notify,
}
impl DialogInbox {
pub(crate) fn new(notify: Notify) -> Arc<Self> {
Arc::new(Self {
results: Mutex::new(Vec::new()),
notify,
})
}
pub(crate) fn drain(&self) -> Vec<(Id, DialogResult)> {
std::mem::take(&mut *self.results.lock().expect("dialog inbox"))
}
}
#[derive(Clone)]
pub struct DialogReply {
id: Id,
inbox: Arc<DialogInbox>,
sent: Arc<AtomicBool>,
}
impl DialogReply {
pub(crate) fn new(id: Id, inbox: Arc<DialogInbox>) -> Self {
Self {
id,
inbox,
sent: Arc::default(),
}
}
pub fn send(&self, result: DialogResult) -> bool {
if self.sent.swap(true, Ordering::AcqRel) {
return false;
}
self.inbox
.results
.lock()
.expect("dialog inbox")
.push((self.id, result));
self.inbox.notify.signal();
true
}
pub fn is_answered(&self) -> bool {
self.sent.load(Ordering::Acquire)
}
}
pub struct DialogLaunch {
pub dialog: FileDialog,
pub reply: DialogReply,
}
pub trait DialogBackend {
fn launch(
&mut self,
dialog: &FileDialog,
parent: Option<&Arc<winit::window::Window>>,
reply: DialogReply,
);
}