| 1 | use super::*; |
| 2 | use std::ffi::OsString; |
| 3 | |
| 4 | #[path = "fake.rs"] |
| 5 | mod fake; |
| 6 | use fake::FakeFactory; |
| 7 | |
| 8 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 9 | pub struct Scenario { |
| 10 | pub name: &'static str, |
| 11 | pub description: &'static str, |
| 12 | } |
| 13 | |
| 14 | macro_rules! catalog { |
| 15 | ($($name:literal => $description:literal),+ $(,)?) => { |
| 16 | pub const SCENARIOS: &[Scenario] = &[$(Scenario { |
| 17 | name: $name, |
| 18 | description: $description, |
| 19 | }),+]; |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | catalog! { |
| 24 | "empty" => "빈 첫 화면", |
| 25 | "archive" => "폴더와 파일이 있는 압축파일", |
| 26 | "tar-bz2" => "TAR.BZ2 목록과 자동 해제 폴더", |
| 27 | "tar-xz" => "TAR.XZ 목록과 자동 해제 폴더", |
| 28 | "folder" => "사진 하위 폴더", |
| 29 | "many-items" => "500개 항목과 세로 스크롤", |
| 30 | "selection" => "여러 파일과 폴더 선택", |
| 31 | "marquee" => "반투명 사각형으로 끌어서 선택 중", |
| 32 | "search-empty" => "검색 결과 없음", |
| 33 | "password" => "압축 해제 암호 입력", |
| 34 | "password-retry" => "암호가 틀린 뒤 다시 입력", |
| 35 | "extract-progress" => "압축 해제 진행률 62%", |
| 36 | "extract-unknown" => "전체 크기를 모르는 압축 해제", |
| 37 | "extract-complete" => "압축 해제 완료", |
| 38 | "extract-cancelled" => "압축 해제 취소 완료", |
| 39 | "cancelling" => "취소 요청 후 작업 종료 대기", |
| 40 | "conflict" => "같은 이름 파일 충돌", |
| 41 | "unsupported" => "지원하지 않는 압축 형식", |
| 42 | "damaged" => "손상된 압축파일", |
| 43 | "create" => "암호 없는 ZIP 만들기 창", |
| 44 | "create-encrypted" => "암호 설정이 켜진 ZIP 만들기 창", |
| 45 | "create-progress" => "ZIP 만들기 진행률 62%", |
| 46 | "create-complete" => "ZIP 만들기 완료", |
| 47 | "folder-empty" => "항목이 없는 하위 폴더", |
| 48 | "gzip-conflict" => "단일 GZ 파일 이름 충돌", |
| 49 | "extract-partial" => "일부 파일만 풀린 결과", |
| 50 | "extract-uncertain" => "저장 여부를 확인할 수 없는 결과", |
| 51 | "extract-unsynced" => "완료 뒤 저장 확인 경고의 상세 정보", |
| 52 | "source-changed" => "목록을 연 뒤 원본이 바뀐 오류", |
| 53 | "dialog-error" => "운영체제 파일 선택 창 실행 실패", |
| 54 | "create-error" => "ZIP 만들기 중 디스크 공간 부족", |
| 55 | "password-create" => "암호 ZIP 생성의 작업자 암호 요청", |
| 56 | "close-confirm" => "진행 중 창 닫기 확인", |
| 57 | } |
| 58 | |
| 59 | pub fn listing() -> String { |
| 60 | SCENARIOS |
| 61 | .iter() |
| 62 | .map(|s| format!("{}\t{}\n", s.name, s.description)) |
| 63 | .collect() |
| 64 | } |
| 65 | |
| 66 | pub fn parse(name: &str) -> Result<Scenario, String> { |
| 67 | SCENARIOS |
| 68 | .iter() |
| 69 | .copied() |
| 70 | .find(|s| s.name == name) |
| 71 | .ok_or_else(|| { |
| 72 | format!( |
| 73 | "알 수 없는 UI 상황: {name}\n사용 가능한 상황:\n{}", |
| 74 | listing() |
| 75 | ) |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | #[derive(Debug, PartialEq)] |
| 80 | pub enum Launch { |
| 81 | Normal(Vec<OsString>), |
| 82 | Scenario(Scenario), |
| 83 | List, |
| 84 | } |
| 85 | |
| 86 | pub fn launch(args: Vec<OsString>) -> Result<Launch, String> { |
| 87 | let requested = args |
| 88 | .iter() |
| 89 | .any(|a| a == "--ui-scenario" || a == "--list-ui-scenarios"); |
| 90 | if !requested { |
| 91 | return Ok(Launch::Normal(args)); |
| 92 | } |
| 93 | if args.len() == 1 && args[0] == "--list-ui-scenarios" { |
| 94 | return Ok(Launch::List); |
| 95 | } |
| 96 | if args.len() == 2 && args[0] == "--ui-scenario" { |
| 97 | return args[1] |
| 98 | .to_str() |
| 99 | .ok_or_else(|| format!("상황 이름은 UTF-8이어야 합니다\n{}", listing())) |
| 100 | .and_then(parse) |
| 101 | .map(Launch::Scenario); |
| 102 | } |
| 103 | Err(format!( |
| 104 | "사용법: --ui-scenario <이름> 또는 --list-ui-scenarios\n{}", |
| 105 | listing() |
| 106 | )) |
| 107 | } |
| 108 | |
| 109 | fn sample_root() -> PathBuf { |
| 110 | PathBuf::from(if cfg!(target_os = "windows") { |
| 111 | "C:\\Samples" |
| 112 | } else { |
| 113 | "/Samples" |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | fn entries(many: bool) -> Vec<Entry> { |
| 118 | let mut entries = vec![ |
| 119 | ("사진", Kind::Directory), |
| 120 | ("읽어주세요.txt", Kind::File), |
| 121 | ("여행 일정.pdf", Kind::File), |
| 122 | ("사진/바다.jpg", Kind::File), |
| 123 | ("사진/풍경.jpg", Kind::File), |
| 124 | ] |
| 125 | .into_iter() |
| 126 | .enumerate() |
| 127 | .map(|(id, (name, kind))| Entry { |
| 128 | id: id as u32, |
| 129 | name: name.into(), |
| 130 | kind, |
| 131 | bytes: Some(if kind == Kind::Directory { |
| 132 | 0 |
| 133 | } else { |
| 134 | 1_287_952 |
| 135 | }), |
| 136 | packed: Some(900_000), |
| 137 | encrypted: id == 2, |
| 138 | }) |
| 139 | .collect::<Vec<_>>(); |
| 140 | if many { |
| 141 | entries.extend((5..505).map(|id| Entry { |
| 142 | id, |
| 143 | name: format!("자료-{id:03}.txt"), |
| 144 | kind: Kind::File, |
| 145 | bytes: Some(4096), |
| 146 | packed: Some(1800), |
| 147 | encrypted: false, |
| 148 | })); |
| 149 | } |
| 150 | entries |
| 151 | } |
| 152 | |
| 153 | impl Scenario { |
| 154 | /// 실제 파일과 작업자를 사용하지 않고 기존 화면 상태를 준비함. |
| 155 | pub fn build(self) -> ArchiveApp { |
| 156 | let factory = FakeFactory::manual(); |
| 157 | let state = factory.state.clone(); |
| 158 | let mut app = ArchiveApp::new(Box::new(factory), None, true); |
| 159 | let base = sample_root(); |
| 160 | app.destination = base.join("풀린 자료"); |
| 161 | app.automatic_destination = false; |
| 162 | app.zip_folder = base.clone(); |
| 163 | app.zip_name = "여행 자료.zip".into(); |
| 164 | if self.name == "empty" { |
| 165 | return app; |
| 166 | } |
| 167 | let archive = base.join(match self.name { |
| 168 | "gzip-conflict" => "여행 자료.gz", |
| 169 | "tar-bz2" => "여행 자료.TAR.BZ2", |
| 170 | "tar-xz" => "여행 자료.TaR.Xz", |
| 171 | _ => "여행 자료.zip", |
| 172 | }); |
| 173 | app.model.archive = Some(archive.clone()); |
| 174 | app.model.entries = entries(self.name == "many-items"); |
| 175 | app.model.list_id = Some(ListId { |
| 176 | catalog_version: 1, |
| 177 | format: Format::Zip, |
| 178 | source_object: SourceObject::Unix { |
| 179 | device: 1, |
| 180 | inode: 1, |
| 181 | }, |
| 182 | archive_bytes: 2_700_000, |
| 183 | archive_sha256: [7; 32], |
| 184 | }); |
| 185 | app.model.confirmed = true; |
| 186 | match self.name { |
| 187 | "tar-bz2" | "tar-xz" => { |
| 188 | app.model.list_id.as_mut().unwrap().format = Format::Tar; |
| 189 | for entry in &mut app.model.entries { |
| 190 | entry.encrypted = false; |
| 191 | } |
| 192 | app.destination = present::default_destination(&archive); |
| 193 | app.automatic_destination = true; |
| 194 | return app; |
| 195 | } |
| 196 | "archive" | "many-items" => return app, |
| 197 | "folder-empty" => { |
| 198 | app.browse_folder("빈 폴더"); |
| 199 | return app; |
| 200 | } |
| 201 | "dialog-error" => { |
| 202 | app.ui_error = Some(DIALOG_UNAVAILABLE.into()); |
| 203 | return app; |
| 204 | } |
| 205 | "folder" => { |
| 206 | app.browse_folder("사진"); |
| 207 | return app; |
| 208 | } |
| 209 | "search-empty" => { |
| 210 | app.search = "없는 파일".into(); |
| 211 | return app; |
| 212 | } |
| 213 | "selection" | "marquee" => { |
| 214 | app.model |
| 215 | .selected |
| 216 | .extend([0, 3, 4, if self.name == "marquee" { 2 } else { 1 }]); |
| 217 | if self.name == "marquee" { |
| 218 | app.scenario_marquee = Some(egui::Rect::from_min_size( |
| 219 | egui::pos2(22.0, 8.0), |
| 220 | egui::vec2(360.0, 62.0), |
| 221 | )); |
| 222 | } |
| 223 | return app; |
| 224 | } |
| 225 | "create" | "create-encrypted" => { |
| 226 | app.sources = vec![base.join("읽어주세요.txt"), base.join("사진")]; |
| 227 | app.scenario_sources = Some(std::rc::Rc::new(vec![ |
| 228 | SourceRow { |
| 229 | name: "사진".into(), |
| 230 | directory: true, |
| 231 | bytes: Some(0), |
| 232 | }, |
| 233 | SourceRow { |
| 234 | name: "읽어주세요.txt".into(), |
| 235 | directory: false, |
| 236 | bytes: Some(4096), |
| 237 | }, |
| 238 | ])); |
| 239 | app.show_create(self.name == "create-encrypted"); |
| 240 | return app; |
| 241 | } |
| 242 | _ => {} |
| 243 | } |
| 244 | let creating = self.name.starts_with("create-") || self.name == "password-create"; |
| 245 | let listing_error = matches!(self.name, "unsupported" | "damaged"); |
| 246 | if self.name == "gzip-conflict" { |
| 247 | app.model.list_id.as_mut().unwrap().format = Format::Gzip; |
| 248 | app.model.entries = vec![Entry { |
| 249 | id: 0, |
| 250 | name: "여행 자료".into(), |
| 251 | kind: Kind::File, |
| 252 | bytes: Some(1_287_952), |
| 253 | packed: Some(900_000), |
| 254 | encrypted: false, |
| 255 | }]; |
| 256 | app.gzip_name = "여행 자료".into(); |
| 257 | } |
| 258 | let request = if creating { |
| 259 | Start::CreateZip { |
| 260 | sources: vec![(base.join("사진"), "사진".into())], |
| 261 | output_file: base.join("여행 자료.zip"), |
| 262 | method: Method::Deflate, |
| 263 | aes256: self.name == "password-create", |
| 264 | } |
| 265 | } else if listing_error { |
| 266 | Start::List { archive } |
| 267 | } else { |
| 268 | Start::Extract { |
| 269 | archive, |
| 270 | destination: app.destination.clone(), |
| 271 | selection: Selection::All, |
| 272 | list_id: app.model.list_id.clone().unwrap(), |
| 273 | gzip_output_name: (self.name == "gzip-conflict").then(|| app.gzip_name.clone()), |
| 274 | } |
| 275 | }; |
| 276 | app.targets = Some(if creating { |
| 277 | BTreeSet::from(["여행 자료.zip".into()]) |
| 278 | } else if self.name == "gzip-conflict" { |
| 279 | BTreeSet::from(["여행 자료".into()]) |
| 280 | } else { |
| 281 | BTreeSet::from([ |
| 282 | "읽어주세요.txt".into(), |
| 283 | "여행 일정.pdf".into(), |
| 284 | "사진/바다.jpg".into(), |
| 285 | "사진/풍경.jpg".into(), |
| 286 | ]) |
| 287 | }); |
| 288 | assert!(app.start(request)); |
| 289 | { |
| 290 | let mut state = state.borrow_mut(); |
| 291 | state.frame(Event::Accepted { encryption: true }); |
| 292 | match self.name { |
| 293 | "password" | "password-retry" | "password-create" => { |
| 294 | state.frame(Event::PasswordRequired { |
| 295 | challenge_id: 17, |
| 296 | attempt: if self.name == "password-retry" { 2 } else { 1 }, |
| 297 | encrypt: self.name == "password-create", |
| 298 | }) |
| 299 | } |
| 300 | "extract-complete" | "create-complete" | "extract-unsynced" => { |
| 301 | let names = app.targets.as_ref().unwrap(); |
| 302 | for name in names { |
| 303 | state.frame(Event::Output { |
| 304 | entry_id: None, |
| 305 | relative_name: name.clone(), |
| 306 | disposition: Disposition::Created, |
| 307 | }); |
| 308 | } |
| 309 | state.frame(Event::Terminal(Terminal { |
| 310 | outcome: Outcome::Completed, |
| 311 | error: None, |
| 312 | publication: Publication::Complete, |
| 313 | created_count: names.len() as u64, |
| 314 | retained_partial_count: 0, |
| 315 | durability: if self.name == "extract-unsynced" { |
| 316 | Durability::Unconfirmed |
| 317 | } else { |
| 318 | Durability::Synced |
| 319 | }, |
| 320 | cleanup: Cleanup::Complete, |
| 321 | })); |
| 322 | state.queue.extend([ |
| 323 | HostEvent::StdoutEof, |
| 324 | HostEvent::StderrEof, |
| 325 | HostEvent::Exited { success: true }, |
| 326 | ]); |
| 327 | } |
| 328 | "extract-cancelled" => state.finish(Outcome::Cancelled, Publication::None), |
| 329 | "extract-partial" | "extract-uncertain" => { |
| 330 | let partial = self.name == "extract-partial"; |
| 331 | if partial { |
| 332 | state.frame(Event::Output { |
| 333 | entry_id: Some(1), |
| 334 | relative_name: "읽어주세요.txt".into(), |
| 335 | disposition: Disposition::Created, |
| 336 | }); |
| 337 | } |
| 338 | state.frame(Event::Terminal(Terminal { |
| 339 | outcome: Outcome::Failed, |
| 340 | error: Some(ErrorCode::Io), |
| 341 | publication: if partial { |
| 342 | Publication::Partial |
| 343 | } else { |
| 344 | Publication::Uncertain |
| 345 | }, |
| 346 | created_count: u64::from(partial), |
| 347 | retained_partial_count: 0, |
| 348 | durability: Durability::Synced, |
| 349 | cleanup: Cleanup::Complete, |
| 350 | })); |
| 351 | state.queue.extend([ |
| 352 | HostEvent::StdoutEof, |
| 353 | HostEvent::StderrEof, |
| 354 | HostEvent::Exited { success: true }, |
| 355 | ]); |
| 356 | } |
| 357 | "conflict" | "gzip-conflict" | "unsupported" | "damaged" | "source-changed" |
| 358 | | "create-error" => { |
| 359 | state.frame(Event::Terminal(Terminal { |
| 360 | outcome: Outcome::Failed, |
| 361 | error: Some(match self.name { |
| 362 | "conflict" | "gzip-conflict" => ErrorCode::Conflict, |
| 363 | "source-changed" => ErrorCode::SourceChanged, |
| 364 | "create-error" => ErrorCode::DiskFull, |
| 365 | "unsupported" => ErrorCode::UnsupportedFormat, |
| 366 | _ => ErrorCode::Integrity, |
| 367 | }), |
| 368 | publication: Publication::None, |
| 369 | created_count: 0, |
| 370 | retained_partial_count: 0, |
| 371 | durability: Durability::NotApplicable, |
| 372 | cleanup: Cleanup::Complete, |
| 373 | })); |
| 374 | state.queue.extend([ |
| 375 | HostEvent::StdoutEof, |
| 376 | HostEvent::StderrEof, |
| 377 | HostEvent::Exited { success: true }, |
| 378 | ]); |
| 379 | } |
| 380 | _ => state.frame(Event::Progress { |
| 381 | stage: if creating { |
| 382 | Stage::Write |
| 383 | } else { |
| 384 | Stage::Decode |
| 385 | }, |
| 386 | processed: 38 << 20, |
| 387 | total: (self.name != "extract-unknown").then_some(61 << 20), |
| 388 | completed_entries: 12, |
| 389 | }), |
| 390 | } |
| 391 | } |
| 392 | for _ in 0..64 { |
| 393 | app.model.poll(); |
| 394 | } |
| 395 | if self.name == "extract-unsynced" { |
| 396 | app.details = true; |
| 397 | } |
| 398 | if self.name == "cancelling" { |
| 399 | app.model.cancel(); |
| 400 | } |
| 401 | if self.name == "close-confirm" { |
| 402 | app.model.request_close(); |
| 403 | } |
| 404 | app |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /// 화면을 고정하고 실제 파일 조작을 막는 미리보기임. |
| 409 | pub struct Preview(pub ArchiveApp); |
| 410 | impl Preview { |
| 411 | pub fn new(app: ArchiveApp, ctx: &egui::Context) -> Self { |
| 412 | ctx.on_begin_pass( |
| 413 | "ui-scenario-readonly", |
| 414 | std::sync::Arc::new(|ctx| { |
| 415 | ctx.input_mut(|i| { |
| 416 | i.events.clear(); |
| 417 | i.pointer = Default::default(); |
| 418 | i.keys_down.clear(); |
| 419 | i.modifiers = Default::default(); |
| 420 | i.raw.dropped_files.clear(); |
| 421 | i.raw.hovered_files.clear(); |
| 422 | }); |
| 423 | }), |
| 424 | ); |
| 425 | Self(app) |
| 426 | } |
| 427 | } |
| 428 | impl eframe::App for Preview { |
| 429 | fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) { |
| 430 | if root.ctx().input(|i| i.viewport().close_requested()) { |
| 431 | root.ctx().send_viewport_cmd(egui::ViewportCommand::Close); |
| 432 | return; |
| 433 | } |
| 434 | self.0.show(root); |
| 435 | } |
| 436 | } |