| 1 | use std::{cell::RefCell, collections::VecDeque, path::PathBuf, rc::Rc}; |
| 2 | use zipelo_ui_memory_app::archive_dialog::{DialogRequest, Dialogs, Purpose}; |
| 3 | |
| 4 | #[derive(Default)] |
| 5 | pub struct DialogState { |
| 6 | pub requests: Vec<DialogRequest>, |
| 7 | pub answers: VecDeque<Option<Vec<PathBuf>>>, |
| 8 | pub save_name: Option<String>, |
| 9 | pub cancel_save: bool, |
| 10 | pub error: Option<String>, |
| 11 | } |
| 12 | |
| 13 | #[derive(Clone, Default)] |
| 14 | pub struct TestDialogs(pub Rc<RefCell<DialogState>>); |
| 15 | |
| 16 | impl Dialogs for TestDialogs { |
| 17 | fn choose(&mut self, request: DialogRequest) -> Result<Option<Vec<PathBuf>>, String> { |
| 18 | let mut state = self.0.borrow_mut(); |
| 19 | state.requests.push(request.clone()); |
| 20 | if let Some(error) = &state.error { |
| 21 | return Err(error.clone()); |
| 22 | } |
| 23 | if let Some(answer) = state.answers.pop_front() { |
| 24 | return Ok(answer); |
| 25 | } |
| 26 | if matches!( |
| 27 | request.purpose, |
| 28 | Purpose::SaveZip | Purpose::SaveSevenZip | Purpose::SaveRecoveryZip |
| 29 | ) && !state.cancel_save |
| 30 | { |
| 31 | let name = state.save_name.take().unwrap_or(request.file_name); |
| 32 | return Ok(Some(vec![request.directory.join(name)])); |
| 33 | } |
| 34 | Ok(None) |
| 35 | } |
| 36 | } |