| 1 | use std::path::PathBuf; |
| 2 | |
| 3 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 4 | pub enum Purpose { |
| 5 | Open, |
| 6 | Files, |
| 7 | Folder, |
| 8 | Destination, |
| 9 | Retry, |
| 10 | SaveZip, |
| 11 | } |
| 12 | |
| 13 | #[derive(Clone, Debug)] |
| 14 | pub struct DialogRequest { |
| 15 | pub purpose: Purpose, |
| 16 | pub directory: PathBuf, |
| 17 | pub file_name: String, |
| 18 | } |
| 19 | |
| 20 | pub trait Dialogs { |
| 21 | fn choose(&mut self, request: DialogRequest) -> Result<Option<Vec<PathBuf>>, String>; |
| 22 | } |
| 23 | |
| 24 | #[derive(Default)] |
| 25 | pub struct NativeDialogs; |
| 26 | |
| 27 | impl Dialogs for NativeDialogs { |
| 28 | fn choose(&mut self, request: DialogRequest) -> Result<Option<Vec<PathBuf>>, String> { |
| 29 | let dialog = rfd::FileDialog::new().set_directory(request.directory); |
| 30 | let answer = |
| 31 | std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match request.purpose { |
| 32 | Purpose::Open => dialog |
| 33 | .set_title("압축파일 열기") |
| 34 | .add_filter("압축파일", &["zip", "7z", "tar", "gz", "tgz"]) |
| 35 | .pick_file() |
| 36 | .map(|path| vec![path]), |
| 37 | Purpose::Files => dialog.set_title("파일 추가").pick_files(), |
| 38 | Purpose::Folder => dialog |
| 39 | .set_title("폴더 추가") |
| 40 | .pick_folder() |
| 41 | .map(|path| vec![path]), |
| 42 | Purpose::Destination | Purpose::Retry => dialog |
| 43 | .set_title("압축을 풀 폴더 선택") |
| 44 | .pick_folder() |
| 45 | .map(|path| vec![path]), |
| 46 | Purpose::SaveZip => dialog |
| 47 | .set_title("ZIP 파일 저장") |
| 48 | .set_file_name(request.file_name) |
| 49 | .add_filter("ZIP 압축파일", &["zip"]) |
| 50 | .save_file() |
| 51 | .map(|path| vec![path]), |
| 52 | })); |
| 53 | answer.map_err(|_| "파일 선택 창을 열지 못했습니다. 다시 시도하세요".to_owned()) |
| 54 | } |
| 55 | } |