team-ljyzipelo

공개
458줄17 KB
1use eframe::egui::{self, FontId, TextStyle};
2use std::{collections::VecDeque, path::PathBuf};
3use zipelo_ui_memory_app::{archive_screen::ArchiveApp, archive_ui::*};
4#[path = "../../experiments/rust-ui-memory-app/src/renderer.rs"]
5mod renderer;
6#[path = "../../experiments/rust-ui-memory-app/src/system_font.rs"]
7mod system_font;
8
9#[derive(Clone)]
10struct CaptureFactory {
11 mode: String,
12}
13struct CaptureConnection {
14 job: u64,
15 sequence: u64,
16 mode: String,
17 queue: VecDeque<HostEvent>,
18 create_name: Option<String>,
19}
20impl CaptureConnection {
21 fn event(&mut self, event: Event) {
22 self.queue.push_back(HostEvent::Frame {
23 job_id: self.job,
24 sequence: self.sequence,
25 event,
26 });
27 self.sequence += 1;
28 }
29 fn terminal(&mut self, terminal: Terminal) {
30 self.event(Event::Terminal(terminal));
31 self.queue.extend([
32 HostEvent::StdoutEof,
33 HostEvent::StderrEof,
34 HostEvent::Exited { success: true },
35 ]);
36 }
37 fn finish(&mut self, listing: bool) {
38 let error = !listing && self.mode == "error";
39 let cancelled = !listing && self.mode == "cancelled";
40 let mut count = 0;
41 if !listing && !error && !cancelled {
42 let names = self
43 .create_name
44 .clone()
45 .map(|name| vec![name])
46 .unwrap_or_else(|| {
47 vec![
48 "읽어주세요.txt".into(),
49 "여행 일정.pdf".into(),
50 "사진".into(),
51 ]
52 });
53 for name in names {
54 self.event(Event::Output {
55 entry_id: None,
56 relative_name: name,
57 disposition: Disposition::Created,
58 });
59 count += 1;
60 }
61 }
62 self.terminal(Terminal {
63 outcome: if error {
64 Outcome::Failed
65 } else if cancelled {
66 Outcome::Cancelled
67 } else {
68 Outcome::Completed
69 },
70 error: error.then_some(ErrorCode::DiskFull),
71 publication: if listing || error || cancelled {
72 Publication::None
73 } else {
74 Publication::Complete
75 },
76 created_count: count,
77 retained_partial_count: 0,
78 durability: if listing {
79 Durability::NotApplicable
80 } else {
81 Durability::Synced
82 },
83 cleanup: Cleanup::Complete,
84 });
85 }
86}
87fn entries() -> Vec<Entry> {
88 vec![
89 Entry {
90 id: 0,
91 name: "읽어주세요.txt".into(),
92 kind: Kind::File,
93 bytes: Some(4096),
94 packed: Some(1800),
95 encrypted: false,
96 },
97 Entry {
98 id: 1,
99 name: "여행 일정.pdf".into(),
100 kind: Kind::File,
101 bytes: Some(1_287_952),
102 packed: Some(900_000),
103 encrypted: true,
104 },
105 Entry {
106 id: 2,
107 name: "사진".into(),
108 kind: Kind::Directory,
109 bytes: Some(0),
110 packed: None,
111 encrypted: false,
112 },
113 ]
114}
115fn toolbar_entries() -> Vec<Entry> {
116 let mut entries = vec![
117 Entry {
118 id: 0,
119 name: "Install".into(),
120 kind: Kind::Directory,
121 bytes: Some(0),
122 packed: None,
123 encrypted: false,
124 },
125 Entry {
126 id: 1,
127 name: "Install.exe".into(),
128 kind: Kind::File,
129 bytes: Some(234_906),
130 packed: None,
131 encrypted: false,
132 },
133 ];
134 for (index, name) in [
135 "cab1.cab",
136 "cab10.cab",
137 "cab11.cab",
138 "cab12.cab",
139 "cab2.cab",
140 "cab3.cab",
141 "cab4.cab",
142 "cab5.cab",
143 ]
144 .iter()
145 .enumerate()
146 {
147 entries.push(Entry {
148 id: index as u32 + 2,
149 name: format!("Install/{name}"),
150 kind: Kind::File,
151 bytes: Some(
152 [
153 99_510_272,
154 77_909_197,
155 102_236_160,
156 67_842_867,
157 103_389_594,
158 59_454_669,
159 78_014_054,
160 81_159_782,
161 ][index],
162 ),
163 packed: None,
164 encrypted: false,
165 });
166 }
167 entries
168}
169
170impl Factory for CaptureFactory {
171 fn connect(&mut self, job: u64) -> Result<Box<dyn Connection>, String> {
172 Ok(Box::new(CaptureConnection {
173 job,
174 sequence: 0,
175 mode: self.mode.clone(),
176 queue: VecDeque::new(),
177 create_name: None,
178 }))
179 }
180}
181impl Connection for CaptureConnection {
182 fn send(&mut self, request: Request) -> Result<(), String> {
183 match request.command {
184 Command::Start(start) | Command::StartExpanded(start) => {
185 self.event(Event::Accepted { encryption: true });
186 match start {
187 Start::List { .. } => {
188 let entries = if self.mode.starts_with("toolbar-") {
189 toolbar_entries()
190 } else {
191 entries()
192 };
193 let count = entries.len() as u32;
194 for entry in entries {
195 self.event(Event::Entry(entry));
196 }
197 self.event(Event::ListEnd {
198 entry_count: count,
199 list_id: ListId {
200 catalog_version: 1,
201 format: Format::Zip,
202 source_object: SourceObject::Unix {
203 device: 1,
204 inode: 1,
205 },
206 archive_bytes: 902000,
207 archive_sha256: [7; 32],
208 },
209 });
210 self.finish(true);
211 }
212 Start::Extract { .. } => {
213 if self.mode.starts_with("password") {
214 self.event(Event::PasswordRequired {
215 challenge_id: 17,
216 attempt: if self.mode == "password-retry" { 2 } else { 1 },
217 encrypt: false,
218 });
219 } else if self.mode.starts_with("progress") || self.mode == "cancelling" {
220 self.event(Event::Progress {
221 stage: Stage::Decode,
222 processed: 38 << 20,
223 total: (self.mode != "progress-unknown").then_some(61 << 20),
224 completed_entries: 12,
225 });
226 } else {
227 self.finish(false);
228 }
229 }
230 Start::Preview { .. } => self.terminal(Terminal {
231 outcome: Outcome::Failed,
232 error: Some(ErrorCode::UnsupportedFeature),
233 publication: Publication::None,
234 created_count: 0,
235 retained_partial_count: 0,
236 durability: Durability::NotApplicable,
237 cleanup: Cleanup::Complete,
238 }),
239 Start::RecoverZip { .. } => {
240 return Err("복구 상황은 --ui-scenario로 확인함".into());
241 }
242 Start::CreateSevenZip { output_file, .. } => {
243 self.create_name =
244 Some(output_file.file_name().unwrap().to_string_lossy().into());
245 self.finish(false);
246 }
247 Start::CreateZip {
248 output_file,
249 aes256,
250 ..
251 } => {
252 self.create_name =
253 Some(output_file.file_name().unwrap().to_string_lossy().into());
254 if aes256 {
255 self.event(Event::PasswordRequired {
256 challenge_id: 17,
257 attempt: 1,
258 encrypt: true,
259 });
260 } else {
261 self.finish(false);
262 }
263 }
264 }
265 }
266 Command::ConflictReply { .. } => return Err("충돌 선택은 상황 명령으로 확인함".into()),
267 Command::Password { .. } => self.finish(false),
268 Command::ResourceReply { .. } => {}
269 Command::Cancel => {
270 self.event(Event::CancelAck {
271 request_sequence: request.sequence,
272 status: CancelStatus::Requested,
273 });
274 if self.mode != "cancelling" {
275 self.terminal(Terminal {
276 outcome: Outcome::Cancelled,
277 error: None,
278 publication: Publication::None,
279 created_count: 0,
280 retained_partial_count: 0,
281 durability: Durability::NotApplicable,
282 cleanup: Cleanup::Complete,
283 });
284 }
285 }
286 }
287 Ok(())
288 }
289 fn poll(&mut self) -> Option<HostEvent> {
290 self.queue.pop_front()
291 }
292}
293struct Capture {
294 app: ArchiveApp,
295 mode: String,
296 factory: CaptureFactory,
297 triggered: bool,
298}
299impl eframe::App for Capture {
300 fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) {
301 if self.mode == "hover" {
302 root.ctx().input_mut(|i| {
303 i.raw.hovered_files = vec![egui::HoveredFile {
304 path: Some(PathBuf::from("여행.zip")),
305 ..Default::default()
306 }]
307 });
308 }
309 if !self.triggered && self.app.model.list_id.is_some() {
310 self.triggered = true;
311 if self.mode.starts_with("long-") {
312 let name = format!("{}.txt", "W".repeat(220));
313 let folder = "deep-folder/".repeat(30);
314 let base = std::env::current_dir()
315 .unwrap()
316 .join("tests/ui-redesign/runs/fixtures");
317 self.app.model.archive = Some(base.join(&folder).join(format!("{name}.zip")));
318 if self.mode == "long-list" || self.mode == "long-folder" {
319 self.app.model.entries = (0..5000)
320 .map(|id| Entry {
321 id,
322 name: format!(
323 "{}{id:04}-{name}",
324 if self.mode == "long-folder" {
325 folder.as_str()
326 } else {
327 ""
328 }
329 ),
330 kind: Kind::File,
331 bytes: Some(31),
332 packed: None,
333 encrypted: true,
334 })
335 .collect();
336 if self.mode == "long-folder" {
337 self.app.browse_folder(folder.trim_end_matches('/'));
338 }
339 }
340 if self.mode == "long-create" {
341 let source = base.join(&name);
342 std::fs::write(&source, b"capture source").unwrap();
343 self.app.add_sources(vec![source]);
344 self.app.show_create(false);
345 }
346 if self.mode == "long-result" {
347 let destination = base.join(&folder);
348 std::fs::create_dir_all(&destination).unwrap();
349 self.app.choose_destination(destination);
350 self.app.begin_extract();
351 }
352 }
353 if self.mode == "toolbar-folder" {
354 self.app.browse_folder("Install");
355 }
356 if self.mode == "toolbar-create" {
357 self.app.show_create(false);
358 }
359 if [
360 "password",
361 "password-retry",
362 "password-create",
363 "progress",
364 "progress-unknown",
365 "cancelling",
366 "success",
367 "error",
368 "cancelled",
369 ]
370 .contains(&self.mode.as_str())
371 {
372 let archive = self.app.model.archive.clone().unwrap();
373 let start = if self.mode == "password-create" {
374 Start::CreateZip {
375 sources: vec![(archive.clone(), "자료".into())],
376 output_file: archive.with_file_name("보호한 자료.zip"),
377 method: Method::Deflate,
378 aes256: true,
379 }
380 } else {
381 Start::Extract {
382 archive,
383 destination: std::env::current_dir().unwrap(),
384 selection: Selection::All,
385 list_id: self.app.model.list_id.clone().unwrap(),
386 gzip_output_name: None,
387 }
388 };
389 // 이전 화면과 같은 공개 상태 API로 암호 및 진행 상태를 재현함.
390 if self.mode == "password-create" {
391 self.app.model.begin(start, &mut self.factory);
392 } else {
393 self.app.begin_extract();
394 }
395 }
396 }
397 self.app.show(root);
398 root.ctx()
399 .request_repaint_after(std::time::Duration::from_millis(30));
400 }
401}
402fn main() -> eframe::Result {
403 let mode = std::env::args().nth(1).unwrap_or_else(|| "empty".into());
404 let base = std::env::current_dir()
405 .unwrap()
406 .join("tests/ui-redesign/runs/fixtures");
407 std::fs::create_dir_all(&base).unwrap();
408 std::fs::write(base.join("읽어주세요.txt"), "화면 검증 입력").unwrap();
409 std::fs::create_dir_all(base.join("사진")).unwrap();
410 let fallback = system_font::load()
411 .ok()
412 .map(|font| (font.bytes, font.face_index));
413 let title = format!("Zipelo UI capture {mode}");
414 renderer::run_native(
415 &title,
416 eframe::NativeOptions {
417 viewport: egui::ViewportBuilder::default()
418 .with_inner_size([1060.0, 720.0])
419 .with_min_inner_size([760.0, 540.0]),
420 ..Default::default()
421 },
422 || {
423 let mode = mode.clone();
424 let base = base.clone();
425 let fallback = fallback.clone();
426 Box::new(move |cc| {
427 cc.egui_ctx
428 .set_fonts(zipelo_ui_memory_app::archive_theme::fonts(fallback.clone()));
429 cc.egui_ctx.set_visuals(egui::Visuals::light());
430 cc.egui_ctx.style_mut_of(egui::Theme::Light, |style| {
431 style
432 .text_styles
433 .insert(TextStyle::Body, FontId::proportional(18.0));
434 style
435 .text_styles
436 .insert(TextStyle::Button, FontId::proportional(18.0));
437 style
438 .text_styles
439 .insert(TextStyle::Heading, FontId::proportional(26.0));
440 });
441 let factory = CaptureFactory { mode: mode.clone() };
442 let startup = (!["empty", "hover", "create", "create-filled"]
443 .contains(&mode.as_str()))
444 .then(|| base.join("여행.zip"));
445 let mut app = ArchiveApp::new(Box::new(factory.clone()), startup, true);
446 if mode == "create-filled" {
447 app.add_sources(vec![base.join("읽어주세요.txt"), base.join("사진")]);
448 }
449 Ok(Box::new(Capture {
450 app,
451 mode,
452 factory,
453 triggered: false,
454 }))
455 })
456 },
457 )
458}