team-ljyzipelo

공개
439줄15 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) => {
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::CreateZip {
231 output_file,
232 aes256,
233 ..
234 } => {
235 self.create_name =
236 Some(output_file.file_name().unwrap().to_string_lossy().into());
237 if aes256 {
238 self.event(Event::PasswordRequired {
239 challenge_id: 17,
240 attempt: 1,
241 encrypt: true,
242 });
243 } else {
244 self.finish(false);
245 }
246 }
247 }
248 }
249 Command::Password { .. } => self.finish(false),
250 Command::Cancel => {
251 self.event(Event::CancelAck {
252 request_sequence: request.sequence,
253 status: CancelStatus::Requested,
254 });
255 if self.mode != "cancelling" {
256 self.terminal(Terminal {
257 outcome: Outcome::Cancelled,
258 error: None,
259 publication: Publication::None,
260 created_count: 0,
261 retained_partial_count: 0,
262 durability: Durability::NotApplicable,
263 cleanup: Cleanup::Complete,
264 });
265 }
266 }
267 }
268 Ok(())
269 }
270 fn poll(&mut self) -> Option<HostEvent> {
271 self.queue.pop_front()
272 }
273}
274struct Capture {
275 app: ArchiveApp,
276 mode: String,
277 factory: CaptureFactory,
278 triggered: bool,
279}
280impl eframe::App for Capture {
281 fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) {
282 if self.mode == "hover" {
283 root.ctx().input_mut(|i| {
284 i.raw.hovered_files = vec![egui::HoveredFile {
285 path: Some(PathBuf::from("여행.zip")),
286 ..Default::default()
287 }]
288 });
289 }
290 if !self.triggered && self.app.model.list_id.is_some() {
291 self.triggered = true;
292 if self.mode.starts_with("long-") {
293 let name = format!("{}.txt", "W".repeat(220));
294 let folder = "deep-folder/".repeat(30);
295 let base = std::env::current_dir()
296 .unwrap()
297 .join("tests/ui-redesign/runs/fixtures");
298 self.app.model.archive = Some(base.join(&folder).join(format!("{name}.zip")));
299 if self.mode == "long-list" || self.mode == "long-folder" {
300 self.app.model.entries = (0..5000)
301 .map(|id| Entry {
302 id,
303 name: format!(
304 "{}{id:04}-{name}",
305 if self.mode == "long-folder" {
306 folder.as_str()
307 } else {
308 ""
309 }
310 ),
311 kind: Kind::File,
312 bytes: Some(31),
313 packed: None,
314 encrypted: true,
315 })
316 .collect();
317 if self.mode == "long-folder" {
318 self.app.browse_folder(folder.trim_end_matches('/'));
319 }
320 }
321 if self.mode == "long-create" {
322 let source = base.join(&name);
323 std::fs::write(&source, b"capture source").unwrap();
324 self.app.add_sources(vec![source]);
325 self.app.show_create(false);
326 }
327 if self.mode == "long-result" {
328 let destination = base.join(&folder);
329 std::fs::create_dir_all(&destination).unwrap();
330 self.app.choose_destination(destination);
331 self.app.begin_extract();
332 }
333 }
334 if self.mode == "toolbar-folder" {
335 self.app.browse_folder("Install");
336 }
337 if self.mode == "toolbar-create" {
338 self.app.show_create(false);
339 }
340 if [
341 "password",
342 "password-retry",
343 "password-create",
344 "progress",
345 "progress-unknown",
346 "cancelling",
347 "success",
348 "error",
349 "cancelled",
350 ]
351 .contains(&self.mode.as_str())
352 {
353 let archive = self.app.model.archive.clone().unwrap();
354 let start = if self.mode == "password-create" {
355 Start::CreateZip {
356 sources: vec![(archive.clone(), "자료".into())],
357 output_file: archive.with_file_name("보호한 자료.zip"),
358 method: Method::Deflate,
359 aes256: true,
360 }
361 } else {
362 Start::Extract {
363 archive,
364 destination: std::env::current_dir().unwrap(),
365 selection: Selection::All,
366 list_id: self.app.model.list_id.clone().unwrap(),
367 gzip_output_name: None,
368 }
369 };
370 // 이전 화면과 같은 공개 상태 API로 암호 및 진행 상태를 재현함.
371 if self.mode == "password-create" {
372 self.app.model.begin(start, &mut self.factory);
373 } else {
374 self.app.begin_extract();
375 }
376 }
377 }
378 self.app.show(root);
379 root.ctx()
380 .request_repaint_after(std::time::Duration::from_millis(30));
381 }
382}
383fn main() -> eframe::Result {
384 let mode = std::env::args().nth(1).unwrap_or_else(|| "empty".into());
385 let base = std::env::current_dir()
386 .unwrap()
387 .join("tests/ui-redesign/runs/fixtures");
388 std::fs::create_dir_all(&base).unwrap();
389 std::fs::write(base.join("읽어주세요.txt"), "화면 검증 입력").unwrap();
390 std::fs::create_dir_all(base.join("사진")).unwrap();
391 let fallback = system_font::load()
392 .ok()
393 .map(|font| (font.bytes, font.face_index));
394 let title = format!("Zipelo UI capture {mode}");
395 renderer::run_native(
396 &title,
397 eframe::NativeOptions {
398 viewport: egui::ViewportBuilder::default()
399 .with_inner_size([1060.0, 720.0])
400 .with_min_inner_size([760.0, 540.0]),
401 ..Default::default()
402 },
403 || {
404 let mode = mode.clone();
405 let base = base.clone();
406 let fallback = fallback.clone();
407 Box::new(move |cc| {
408 cc.egui_ctx
409 .set_fonts(zipelo_ui_memory_app::archive_theme::fonts(fallback.clone()));
410 cc.egui_ctx.set_visuals(egui::Visuals::light());
411 cc.egui_ctx.style_mut_of(egui::Theme::Light, |style| {
412 style
413 .text_styles
414 .insert(TextStyle::Body, FontId::proportional(18.0));
415 style
416 .text_styles
417 .insert(TextStyle::Button, FontId::proportional(18.0));
418 style
419 .text_styles
420 .insert(TextStyle::Heading, FontId::proportional(26.0));
421 });
422 let factory = CaptureFactory { mode: mode.clone() };
423 let startup = (!["empty", "hover", "create", "create-filled"]
424 .contains(&mode.as_str()))
425 .then(|| base.join("여행.zip"));
426 let mut app = ArchiveApp::new(Box::new(factory.clone()), startup, true);
427 if mode == "create-filled" {
428 app.add_sources(vec![base.join("읽어주세요.txt"), base.join("사진")]);
429 }
430 Ok(Box::new(Capture {
431 app,
432 mode,
433 factory,
434 triggered: false,
435 }))
436 })
437 },
438 )
439}