use super::{Context, Id};
use crate::files::{DialogLaunch, DialogReply, DialogRequest, DialogResult, FileDialog};
use std::{collections::HashMap, hash::Hash, sync::Arc};
enum Entry {
Pending,
Ready(DialogResult),
}
pub(crate) struct Dialogs {
entries: HashMap<Id, Entry>,
launches: Vec<DialogLaunch>,
inbox: Arc<crate::files::DialogInbox>,
}
impl Dialogs {
pub(crate) fn new(notify: crate::files::Notify) -> Self {
Self {
entries: HashMap::new(),
launches: Vec::new(),
inbox: crate::files::DialogInbox::new(notify),
}
}
fn settle(&mut self) {
for (id, result) in self.inbox.drain() {
if let Some(entry) = self.entries.get_mut(&id) {
*entry = Entry::Ready(result);
}
}
}
}
impl Context {
pub fn open_dialog(&mut self, key: impl Hash, dialog: FileDialog) -> DialogRequest {
let id = Id::new(("file-dialog", key));
let request = DialogRequest { id };
if self.dialogs.entries.contains_key(&id) {
return request;
}
self.dialogs.entries.insert(id, Entry::Pending);
let reply = DialogReply::new(id, Arc::clone(&self.dialogs.inbox));
self.dialogs.launches.push(DialogLaunch { dialog, reply });
request
}
pub fn take_dialog_result(&mut self, request: &DialogRequest) -> Option<DialogResult> {
self.dialogs.settle();
match self.dialogs.entries.get(&request.id)? {
Entry::Pending => None,
Entry::Ready(_) => match self.dialogs.entries.remove(&request.id) {
Some(Entry::Ready(result)) => Some(result),
_ => None,
},
}
}
pub fn dialog_pending(&mut self, request: &DialogRequest) -> bool {
self.dialogs.settle();
self.dialogs.entries.contains_key(&request.id)
}
pub fn take_dialog_launches(&mut self) -> Vec<DialogLaunch> {
std::mem::take(&mut self.dialogs.launches)
}
pub(crate) fn dialog_answer(&mut self, request: &DialogRequest, result: DialogResult) {
if let Some(entry) = self.dialogs.entries.get_mut(&request.id) {
*entry = Entry::Ready(result);
self.request_repaint();
}
}
}