| 1 | use crate::{archive_present as present, archive_theme as theme}; |
| 2 | use eframe::egui::{self, Align, Layout}; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | |
| 5 | #[derive(Clone, Copy, PartialEq)] |
| 6 | pub enum Purpose { |
| 7 | Open, |
| 8 | Destination, |
| 9 | Retry, |
| 10 | Files, |
| 11 | Folder, |
| 12 | ZipFolder, |
| 13 | } |
| 14 | |
| 15 | pub enum Choice { |
| 16 | Cancel, |
| 17 | Paths(Vec<PathBuf>), |
| 18 | } |
| 19 | |
| 20 | pub struct Picker { |
| 21 | pub purpose: Purpose, |
| 22 | current: PathBuf, |
| 23 | entries: Vec<present::BrowserEntry>, |
| 24 | selected: Vec<PathBuf>, |
| 25 | error: bool, |
| 26 | } |
| 27 | |
| 28 | impl Picker { |
| 29 | pub fn new(purpose: Purpose, start: &Path) -> Self { |
| 30 | let current = if start.is_dir() { |
| 31 | start.to_path_buf() |
| 32 | } else { |
| 33 | start |
| 34 | .parent() |
| 35 | .filter(|p| p.is_dir()) |
| 36 | .map(Path::to_path_buf) |
| 37 | .unwrap_or_else(present::home_directory) |
| 38 | }; |
| 39 | let mut picker = Self { |
| 40 | purpose, |
| 41 | current, |
| 42 | entries: Vec::new(), |
| 43 | selected: Vec::new(), |
| 44 | error: false, |
| 45 | }; |
| 46 | picker.reload(); |
| 47 | picker |
| 48 | } |
| 49 | |
| 50 | fn reload(&mut self) { |
| 51 | self.selected.clear(); |
| 52 | match present::directory_entries(&self.current) { |
| 53 | Ok(entries) => { |
| 54 | self.entries = entries; |
| 55 | self.error = false; |
| 56 | } |
| 57 | Err(_) => { |
| 58 | self.entries.clear(); |
| 59 | self.error = true; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn directory_mode(&self) -> bool { |
| 65 | matches!( |
| 66 | self.purpose, |
| 67 | Purpose::Destination | Purpose::Retry | Purpose::Folder | Purpose::ZipFolder |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | pub fn show(&mut self, ctx: &egui::Context) -> Option<Choice> { |
| 72 | let mut choice = None; |
| 73 | let mut navigate = None; |
| 74 | let width = (ctx.content_rect().width() - 64.0).min(720.0); |
| 75 | let height = (ctx.content_rect().height() - 280.0).clamp(160.0, 340.0); |
| 76 | let title = match self.purpose { |
| 77 | Purpose::Open => "파일 열기", |
| 78 | Purpose::Destination | Purpose::Retry => "풀 위치 선택", |
| 79 | Purpose::Files => "묶을 파일 선택", |
| 80 | Purpose::Folder => "묶을 폴더 선택", |
| 81 | Purpose::ZipFolder => "ZIP 저장 폴더 선택", |
| 82 | }; |
| 83 | let title = if self.purpose == Purpose::Open { |
| 84 | "파일 열기" |
| 85 | } else { |
| 86 | title |
| 87 | }; |
| 88 | let modal = egui::Modal::new(egui::Id::unique("archive-picker")).show(ctx, |ui| { |
| 89 | ui.set_width(width); |
| 90 | ui.heading(title); |
| 91 | egui::ScrollArea::horizontal() |
| 92 | .id_salt("breadcrumbs") |
| 93 | .show(ui, |ui| { |
| 94 | ui.horizontal(|ui| { |
| 95 | let mut ancestors = self.current.ancestors().collect::<Vec<_>>(); |
| 96 | ancestors.reverse(); |
| 97 | for path in ancestors { |
| 98 | let label = path |
| 99 | .file_name() |
| 100 | .unwrap_or(path.as_os_str()) |
| 101 | .to_string_lossy(); |
| 102 | if ui |
| 103 | .button(label.as_ref()) |
| 104 | .on_hover_text(path.display().to_string()) |
| 105 | .clicked() |
| 106 | { |
| 107 | navigate = Some(path.to_path_buf()); |
| 108 | } |
| 109 | } |
| 110 | }); |
| 111 | }); |
| 112 | ui.separator(); |
| 113 | ui.horizontal_top(|ui| { |
| 114 | ui.allocate_ui_with_layout( |
| 115 | egui::vec2(128.0, height), |
| 116 | Layout::top_down(Align::Min), |
| 117 | |ui| { |
| 118 | ui.set_min_size(egui::vec2(128.0, height)); |
| 119 | for (label, path) in present::shortcuts() { |
| 120 | if theme::action(ui, &label, false, true).clicked() { |
| 121 | navigate = Some(path); |
| 122 | } |
| 123 | } |
| 124 | if let Some(parent) = self.current.parent() { |
| 125 | if theme::action(ui, "상위 폴더", false, true).clicked() { |
| 126 | navigate = Some(parent.to_path_buf()); |
| 127 | } |
| 128 | } |
| 129 | }, |
| 130 | ); |
| 131 | ui.add_space(theme::SPACE); |
| 132 | ui.allocate_ui_with_layout( |
| 133 | egui::vec2((width - 160.0).max(240.0), height), |
| 134 | Layout::top_down(Align::Min), |
| 135 | |ui| { |
| 136 | ui.set_min_size(egui::vec2((width - 160.0).max(240.0), height)); |
| 137 | if self.error { |
| 138 | ui.colored_label( |
| 139 | theme::ERROR, |
| 140 | "이 폴더를 열 수 없습니다. 접근 권한을 확인하세요", |
| 141 | ); |
| 142 | } else if self.entries.is_empty() { |
| 143 | ui.label("이 폴더는 비어 있습니다"); |
| 144 | } |
| 145 | egui::ScrollArea::vertical() |
| 146 | .id_salt("picker-entries") |
| 147 | .max_height(height) |
| 148 | .show_rows(ui, theme::ROW, self.entries.len(), |ui, rows| { |
| 149 | for index in rows { |
| 150 | let entry = &self.entries[index]; |
| 151 | let selected = self.selected.contains(&entry.path); |
| 152 | let response = ui |
| 153 | .horizontal(|ui| { |
| 154 | theme::icon( |
| 155 | ui, |
| 156 | if entry.directory { |
| 157 | theme::Icon::Folder |
| 158 | } else { |
| 159 | theme::Icon::File |
| 160 | }, |
| 161 | 20.0, |
| 162 | ); |
| 163 | let name = if entry.link { |
| 164 | format!("{} (링크)", entry.name) |
| 165 | } else { |
| 166 | entry.name.clone() |
| 167 | }; |
| 168 | ui.add_sized( |
| 169 | [ui.available_width(), theme::ROW], |
| 170 | egui::Button::new("") |
| 171 | .left_text(present::middle_text(&name, 38)) |
| 172 | .selected(selected), |
| 173 | ) |
| 174 | .on_hover_text(entry.path.display().to_string()) |
| 175 | }) |
| 176 | .inner; |
| 177 | theme::focus(&response); |
| 178 | if response.clicked() { |
| 179 | if self.purpose == Purpose::Files |
| 180 | && ui.input(|i| i.modifiers.command || i.modifiers.ctrl) |
| 181 | { |
| 182 | if selected { |
| 183 | self.selected.retain(|p| p != &entry.path); |
| 184 | } else if !entry.directory { |
| 185 | self.selected.push(entry.path.clone()); |
| 186 | } |
| 187 | } else { |
| 188 | self.selected = vec![entry.path.clone()]; |
| 189 | } |
| 190 | } |
| 191 | let keyboard_activation = response.has_focus() |
| 192 | && ui.input(|i| { |
| 193 | i.key_pressed(egui::Key::Enter) |
| 194 | || i.key_pressed(egui::Key::Space) |
| 195 | }); |
| 196 | if response.double_clicked() || keyboard_activation { |
| 197 | if entry.directory && !entry.link { |
| 198 | navigate = Some(entry.path.clone()); |
| 199 | } else if !self.directory_mode() { |
| 200 | choice = Some(Choice::Paths(vec![entry.path.clone()])); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | }); |
| 205 | }, |
| 206 | ); |
| 207 | }); |
| 208 | ui.separator(); |
| 209 | ui.horizontal(|ui| { |
| 210 | if self.purpose == Purpose::Files { |
| 211 | ui.label("Ctrl을 누르면 여러 파일을 고를 수 있습니다"); |
| 212 | } |
| 213 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 214 | let selected_directory = self.selected.first().filter(|path| { |
| 215 | self.entries |
| 216 | .iter() |
| 217 | .any(|e| &e.path == *path && (e.directory || e.link)) |
| 218 | }); |
| 219 | let paths = if self.directory_mode() { |
| 220 | vec![ |
| 221 | selected_directory |
| 222 | .cloned() |
| 223 | .unwrap_or_else(|| self.current.clone()), |
| 224 | ] |
| 225 | } else { |
| 226 | self.selected |
| 227 | .iter() |
| 228 | .filter(|path| { |
| 229 | self.entries |
| 230 | .iter() |
| 231 | .any(|e| &e.path == *path && !e.directory) |
| 232 | }) |
| 233 | .cloned() |
| 234 | .collect() |
| 235 | }; |
| 236 | let label = if self.directory_mode() { |
| 237 | "이 폴더 선택" |
| 238 | } else if self.purpose == Purpose::Files { |
| 239 | "추가" |
| 240 | } else { |
| 241 | "열기" |
| 242 | }; |
| 243 | let enabled = !paths.is_empty() && !self.error; |
| 244 | let (cancel, confirm) = theme::action_pair(ui, "취소", Some((label, enabled))); |
| 245 | if navigate.is_none() && confirm { |
| 246 | choice = Some(Choice::Paths(paths)); |
| 247 | } |
| 248 | if cancel { |
| 249 | choice = Some(Choice::Cancel); |
| 250 | } |
| 251 | }); |
| 252 | }); |
| 253 | }); |
| 254 | if modal.should_close() { |
| 255 | choice = Some(Choice::Cancel); |
| 256 | } |
| 257 | if let Some(path) = navigate { |
| 258 | self.current = path; |
| 259 | self.reload(); |
| 260 | } |
| 261 | choice |
| 262 | } |
| 263 | } |