team-ljyzipelo

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