team-ljyzipelo

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