| 1 | use eframe::egui::{self, FontData, FontDefinitions, FontFamily, FontId, TextStyle}; |
| 2 | use std::{collections::VecDeque, path::PathBuf}; |
| 3 | use zipelo_ui_memory_app::{archive_screen::ArchiveApp, archive_ui::*}; |
| 4 | #[path = "../../experiments/rust-ui-memory-app/src/renderer.rs"] |
| 5 | mod renderer; |
| 6 | #[path = "../../experiments/rust-ui-memory-app/src/system_font.rs"] |
| 7 | mod system_font; |
| 8 | |
| 9 | #[derive(Clone)] |
| 10 | struct CaptureFactory { |
| 11 | mode: String, |
| 12 | } |
| 13 | struct CaptureConnection { |
| 14 | job: u64, |
| 15 | sequence: u64, |
| 16 | mode: String, |
| 17 | queue: VecDeque<HostEvent>, |
| 18 | create_name: Option<String>, |
| 19 | } |
| 20 | impl 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 | } |
| 87 | fn 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 | } |
| 115 | impl Factory for CaptureFactory { |
| 116 | fn connect(&mut self, job: u64) -> Result<Box<dyn Connection>, String> { |
| 117 | Ok(Box::new(CaptureConnection { |
| 118 | job, |
| 119 | sequence: 0, |
| 120 | mode: self.mode.clone(), |
| 121 | queue: VecDeque::new(), |
| 122 | create_name: None, |
| 123 | })) |
| 124 | } |
| 125 | } |
| 126 | impl Connection for CaptureConnection { |
| 127 | fn send(&mut self, request: Request) -> Result<(), String> { |
| 128 | match request.command { |
| 129 | Command::Start(start) => { |
| 130 | self.event(Event::Accepted { encryption: true }); |
| 131 | match start { |
| 132 | Start::List { .. } => { |
| 133 | for entry in entries() { |
| 134 | self.event(Event::Entry(entry)); |
| 135 | } |
| 136 | self.event(Event::ListEnd { |
| 137 | entry_count: 3, |
| 138 | list_id: ListId { |
| 139 | catalog_version: 1, |
| 140 | format: Format::Zip, |
| 141 | source_object: SourceObject::Unix { |
| 142 | device: 1, |
| 143 | inode: 1, |
| 144 | }, |
| 145 | archive_bytes: 902000, |
| 146 | archive_sha256: [7; 32], |
| 147 | }, |
| 148 | }); |
| 149 | self.finish(true); |
| 150 | } |
| 151 | Start::Extract { .. } => { |
| 152 | if self.mode.starts_with("password") { |
| 153 | self.event(Event::PasswordRequired { |
| 154 | challenge_id: 17, |
| 155 | attempt: if self.mode == "password-retry" { 2 } else { 1 }, |
| 156 | encrypt: false, |
| 157 | }); |
| 158 | } else if self.mode.starts_with("progress") || self.mode == "cancelling" { |
| 159 | self.event(Event::Progress { |
| 160 | stage: Stage::Decode, |
| 161 | processed: 38 << 20, |
| 162 | total: (self.mode != "progress-unknown").then_some(61 << 20), |
| 163 | completed_entries: 12, |
| 164 | }); |
| 165 | } else { |
| 166 | self.finish(false); |
| 167 | } |
| 168 | } |
| 169 | Start::CreateZip { |
| 170 | output_file, |
| 171 | aes256, |
| 172 | .. |
| 173 | } => { |
| 174 | self.create_name = |
| 175 | Some(output_file.file_name().unwrap().to_string_lossy().into()); |
| 176 | if aes256 { |
| 177 | self.event(Event::PasswordRequired { |
| 178 | challenge_id: 17, |
| 179 | attempt: 1, |
| 180 | encrypt: true, |
| 181 | }); |
| 182 | } else { |
| 183 | self.finish(false); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | Command::Password { .. } => self.finish(false), |
| 189 | Command::Cancel => { |
| 190 | self.event(Event::CancelAck { |
| 191 | request_sequence: request.sequence, |
| 192 | status: CancelStatus::Requested, |
| 193 | }); |
| 194 | if self.mode != "cancelling" { |
| 195 | self.terminal(Terminal { |
| 196 | outcome: Outcome::Cancelled, |
| 197 | error: None, |
| 198 | publication: Publication::None, |
| 199 | created_count: 0, |
| 200 | retained_partial_count: 0, |
| 201 | durability: Durability::NotApplicable, |
| 202 | cleanup: Cleanup::Complete, |
| 203 | }); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | Ok(()) |
| 208 | } |
| 209 | fn poll(&mut self) -> Option<HostEvent> { |
| 210 | self.queue.pop_front() |
| 211 | } |
| 212 | } |
| 213 | struct Capture { |
| 214 | app: ArchiveApp, |
| 215 | mode: String, |
| 216 | factory: CaptureFactory, |
| 217 | triggered: bool, |
| 218 | } |
| 219 | impl eframe::App for Capture { |
| 220 | fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) { |
| 221 | if self.mode == "hover" { |
| 222 | root.ctx().input_mut(|i| { |
| 223 | i.raw.hovered_files = vec![egui::HoveredFile { |
| 224 | path: Some(PathBuf::from("여행.zip")), |
| 225 | ..Default::default() |
| 226 | }] |
| 227 | }); |
| 228 | } |
| 229 | if !self.triggered && self.app.model.list_id.is_some() { |
| 230 | self.triggered = true; |
| 231 | if [ |
| 232 | "password", |
| 233 | "password-retry", |
| 234 | "password-create", |
| 235 | "progress", |
| 236 | "progress-unknown", |
| 237 | "cancelling", |
| 238 | "success", |
| 239 | "error", |
| 240 | "cancelled", |
| 241 | ] |
| 242 | .contains(&self.mode.as_str()) |
| 243 | { |
| 244 | let archive = self.app.model.archive.clone().unwrap(); |
| 245 | let start = if self.mode == "password-create" { |
| 246 | Start::CreateZip { |
| 247 | sources: vec![(archive.clone(), "자료".into())], |
| 248 | output_file: archive.with_file_name("보호한 자료.zip"), |
| 249 | method: Method::Deflate, |
| 250 | aes256: true, |
| 251 | } |
| 252 | } else { |
| 253 | Start::Extract { |
| 254 | archive, |
| 255 | destination: std::env::current_dir().unwrap(), |
| 256 | selection: Selection::All, |
| 257 | list_id: self.app.model.list_id.clone().unwrap(), |
| 258 | gzip_output_name: None, |
| 259 | } |
| 260 | }; |
| 261 | // 이전 화면과 같은 공개 상태 API로 암호 및 진행 상태를 재현함. |
| 262 | if self.mode == "password-create" { |
| 263 | self.app.model.begin(start, &mut self.factory); |
| 264 | } else { |
| 265 | self.app.begin_extract(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | self.app.show(root); |
| 270 | root.ctx() |
| 271 | .request_repaint_after(std::time::Duration::from_millis(30)); |
| 272 | } |
| 273 | } |
| 274 | fn main() -> eframe::Result { |
| 275 | let mode = std::env::args().nth(1).unwrap_or_else(|| "empty".into()); |
| 276 | let base = std::env::current_dir() |
| 277 | .unwrap() |
| 278 | .join("tests/ui-redesign/runs/fixtures"); |
| 279 | std::fs::create_dir_all(&base).unwrap(); |
| 280 | std::fs::write(base.join("읽어주세요.txt"), "화면 검증 입력").unwrap(); |
| 281 | std::fs::create_dir_all(base.join("사진")).unwrap(); |
| 282 | let font = system_font::load().map_err(|e| eframe::Error::AppCreation(Box::new(e)))?; |
| 283 | let title = format!("Zipelo UI capture {mode}"); |
| 284 | renderer::run_native( |
| 285 | &title, |
| 286 | eframe::NativeOptions { |
| 287 | viewport: egui::ViewportBuilder::default() |
| 288 | .with_inner_size([1060.0, 720.0]) |
| 289 | .with_min_inner_size([760.0, 540.0]), |
| 290 | ..Default::default() |
| 291 | }, |
| 292 | || { |
| 293 | let mode = mode.clone(); |
| 294 | let base = base.clone(); |
| 295 | let bytes = font.bytes.clone(); |
| 296 | let face_index = font.face_index; |
| 297 | Box::new(move |cc| { |
| 298 | let mut fonts = FontDefinitions::empty(); |
| 299 | let mut data = FontData::from_owned(bytes.clone()); |
| 300 | data.index = face_index; |
| 301 | fonts.font_data.insert("noto-kr".into(), data.into()); |
| 302 | fonts |
| 303 | .families |
| 304 | .insert(FontFamily::Proportional, vec!["noto-kr".into()]); |
| 305 | cc.egui_ctx.set_fonts(fonts); |
| 306 | cc.egui_ctx.set_visuals(egui::Visuals::light()); |
| 307 | cc.egui_ctx.style_mut_of(egui::Theme::Light, |style| { |
| 308 | style |
| 309 | .text_styles |
| 310 | .insert(TextStyle::Body, FontId::proportional(18.0)); |
| 311 | style |
| 312 | .text_styles |
| 313 | .insert(TextStyle::Button, FontId::proportional(18.0)); |
| 314 | style |
| 315 | .text_styles |
| 316 | .insert(TextStyle::Heading, FontId::proportional(26.0)); |
| 317 | }); |
| 318 | let factory = CaptureFactory { mode: mode.clone() }; |
| 319 | let startup = (!["empty", "hover", "create", "create-filled"] |
| 320 | .contains(&mode.as_str())) |
| 321 | .then(|| base.join("여행.zip")); |
| 322 | let mut app = ArchiveApp::new(Box::new(factory.clone()), startup, true); |
| 323 | if mode == "create-filled" { |
| 324 | app.add_sources(vec![base.join("읽어주세요.txt"), base.join("사진")]); |
| 325 | } |
| 326 | Ok(Box::new(Capture { |
| 327 | app, |
| 328 | mode, |
| 329 | factory, |
| 330 | triggered: false, |
| 331 | })) |
| 332 | }) |
| 333 | }, |
| 334 | ) |
| 335 | } |