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