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