team-ljyzipelo

공개
379줄15 KB
1use crate::archive_ui::*;
2use eframe::egui;
3use std::path::PathBuf;
4
5pub struct ArchiveApp {
6 pub model: Model,
7 factory: Box<dyn Factory>,
8 open_text: String,
9 destination: String,
10 gzip_name: String,
11 sources: String,
12 zip_output: String,
13 password: String,
14 create: bool,
15 open: bool,
16 selected_only: bool,
17 method: Method,
18 fixture: bool,
19}
20impl ArchiveApp {
21 pub fn new(factory: Box<dyn Factory>, startup: Option<PathBuf>, fixture: bool) -> Self {
22 let mut app = Self {
23 model: Model::default(),
24 factory,
25 open_text: String::new(),
26 destination: String::new(),
27 gzip_name: String::new(),
28 sources: String::new(),
29 zip_output: String::new(),
30 password: String::new(),
31 create: false,
32 open: false,
33 selected_only: false,
34 method: Method::Deflate,
35 fixture,
36 };
37 if let Some(path) = startup {
38 app.open_path(path);
39 }
40 app
41 }
42 pub fn open_path(&mut self, archive: PathBuf) {
43 self.password.clear();
44 self.model
45 .begin(Start::List { archive }, self.factory.as_mut());
46 }
47 fn on_drop(&mut self, paths: Vec<PathBuf>) {
48 if self.model.busy() || paths.is_empty() {
49 return;
50 }
51 if paths.len() == 1 && !self.create {
52 self.open_path(paths[0].clone());
53 } else if paths.iter().all(|p| p.to_str().is_some()) {
54 self.sources = paths
55 .iter()
56 .map(|p| p.to_str().unwrap())
57 .collect::<Vec<_>>()
58 .join("\n");
59 self.create = true;
60 } else {
61 self.model.error = Some("ZIP 생성 경로는 현재 UTF-8 입력만 지원합니다".into());
62 }
63 }
64}
65impl ArchiveApp {
66 pub fn show(&mut self, root: &mut egui::Ui) {
67 self.model.poll();
68 if self.model.challenge.is_none() {
69 self.password.clear();
70 }
71 let ctx = root.ctx().clone();
72 let dropped = ctx.input(|i| {
73 i.raw
74 .dropped_files
75 .iter()
76 .map(|f| f.path().to_path_buf())
77 .collect::<Vec<_>>()
78 });
79 self.on_drop(dropped);
80 if ctx.input(|i| i.viewport().close_requested()) {
81 self.model.request_close();
82 if self.model.close != Close::Allowed {
83 ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
84 }
85 }
86 if self.model.close == Close::Allowed {
87 ctx.send_viewport_cmd(egui::ViewportCommand::Close);
88 }
89 egui::Panel::top("archive-actions").show(root, |ui| {
90 ui.horizontal_wrapped(|ui| {
91 ui.heading("Zipelo");
92 ui.add_enabled_ui(
93 !self.model.busy() && self.model.close == Close::Open,
94 |ui| {
95 if ui.button("파일 열기").clicked() {
96 self.open = true;
97 }
98 if ui.button("ZIP 만들기").clicked() {
99 self.create = true;
100 }
101 },
102 );
103 if ui
104 .add_enabled(self.model.busy(), egui::Button::new("취소 요청"))
105 .clicked()
106 {
107 self.password.clear();
108 self.model.cancel();
109 }
110 });
111 if self.fixture {
112 ui.colored_label(
113 egui::Color32::from_rgb(143, 69, 0),
114 "화면 시험 모드 / 파일을 읽거나 저장하지 않습니다",
115 );
116 }
117 });
118 egui::Panel::bottom("archive-status").show(root, |ui| {
119 ui.label(self.model.status());
120 if let Some((stage, done, total, entries)) =
121 self.model.progress.filter(|_| self.model.busy())
122 {
123 ui.label(format!(
124 "{} / {done}바이트 / {entries}개 처리",
125 stage_label(stage)
126 ));
127 if let Some(total) = total.filter(|n| *n > 0) {
128 ui.add(
129 egui::ProgressBar::new((done as f64 / total as f64).clamp(0.0, 1.0) as f32)
130 .show_percentage(),
131 );
132 } else if self.model.busy() {
133 ui.spinner();
134 }
135 }
136 if !self.model.busy() {
137 if let Some(t) = &self.model.terminal {
138 for line in terminal_lines(t) {
139 ui.label(line);
140 }
141 }
142 }
143 });
144 egui::CentralPanel::default_margins().show(root, |ui| {
145 if let Some(path) = &self.model.archive {
146 ui.heading(
147 path.file_name()
148 .unwrap_or(path.as_os_str())
149 .to_string_lossy(),
150 );
151 ui.label(path.display().to_string());
152 } else {
153 ui.heading("압축파일을 여기로 끌어 놓으세요");
154 }
155 if self.model.list_id.is_some() {
156 ui.add_enabled_ui(self.model.can_extract(), |ui| {
157 ui.horizontal_wrapped(|ui| {
158 ui.label("출력 폴더");
159 ui.text_edit_singleline(&mut self.destination);
160 });
161 if self
162 .model
163 .list_id
164 .as_ref()
165 .is_some_and(|id| id.format == Format::Gzip)
166 {
167 ui.horizontal(|ui| {
168 ui.label("출력 파일명");
169 ui.text_edit_singleline(&mut self.gzip_name);
170 });
171 }
172 ui.horizontal_wrapped(|ui| {
173 ui.checkbox(&mut self.selected_only, "선택한 항목만 풀기");
174 if ui.button("풀기").clicked() {
175 self.model.extract(
176 PathBuf::from(&self.destination),
177 self.selected_only,
178 Some(self.gzip_name.clone()),
179 self.factory.as_mut(),
180 );
181 }
182 });
183 });
184 }
185 ui.separator();
186 egui::ScrollArea::both().show(ui, |ui| {
187 egui::Grid::new("archive-list")
188 .striped(true)
189 .min_col_width(90.0)
190 .show(ui, |ui| {
191 ui.label("선택");
192 ui.label("이름");
193 ui.label("크기");
194 ui.label("형식");
195 ui.label("암호");
196 ui.end_row();
197 for entry in &self.model.entries {
198 let mut selected = self.model.selected.contains(&entry.id);
199 if ui
200 .add_enabled(
201 !self.model.busy()
202 && matches!(entry.kind, Kind::File | Kind::Directory),
203 egui::Checkbox::without_text(&mut selected),
204 )
205 .on_hover_text(format!("{} 선택", entry.name))
206 .changed()
207 {
208 if selected {
209 self.model.selected.insert(entry.id);
210 } else {
211 self.model.selected.remove(&entry.id);
212 }
213 }
214 ui.label(&entry.name);
215 ui.label(
216 entry
217 .bytes
218 .map_or("알 수 없음".into(), |n| format!("{n} B")),
219 );
220 ui.label(match entry.kind {
221 Kind::File => "파일",
222 Kind::Directory => "폴더",
223 Kind::Link => "링크",
224 Kind::Other => "기타",
225 });
226 ui.label(if entry.encrypted { "있음" } else { "없음" });
227 ui.end_row();
228 }
229 });
230 if self.model.entries.is_empty() && self.model.list_id.is_some() {
231 ui.label("압축파일에 항목이 없습니다");
232 }
233 for (name, state) in &self.model.outputs {
234 ui.label(format!("{name}: {}", disposition_label(*state)));
235 }
236 });
237 });
238 if self.open {
239 egui::Window::new("파일 열기")
240 .collapsible(false)
241 .show(&ctx, |ui| {
242 ui.label("압축파일 절대 경로");
243 ui.text_edit_singleline(&mut self.open_text);
244 if ui.button("열기").clicked() {
245 self.open_path(self.open_text.clone().into());
246 self.open = false;
247 }
248 if ui.button("닫기").clicked() {
249 self.open = false;
250 }
251 });
252 }
253 if self.create {
254 egui::Window::new("ZIP 만들기")
255 .collapsible(false)
256 .show(&ctx, |ui| {
257 ui.add_enabled_ui(!self.model.busy(), |ui| {
258 ui.label("원본 파일 절대 경로 / 한 줄에 하나");
259 ui.add(egui::TextEdit::multiline(&mut self.sources).desired_rows(4));
260 ui.label("저장할 ZIP 절대 경로");
261 ui.text_edit_singleline(&mut self.zip_output);
262 ui.horizontal(|ui| {
263 ui.selectable_value(&mut self.method, Method::Deflate, "압축");
264 ui.selectable_value(&mut self.method, Method::Store, "묶기");
265 });
266 ui.label("암호 ZIP 생성은 작업자 지원 후 활성화됩니다");
267 if ui.button("ZIP 만들기").clicked() {
268 let sources = self
269 .sources
270 .lines()
271 .map(|s| {
272 let path = PathBuf::from(s);
273 let name = path
274 .file_name()
275 .and_then(|n| n.to_str())
276 .unwrap_or("")
277 .to_owned();
278 (path, name)
279 })
280 .collect();
281 if self.model.begin(
282 Start::CreateZip {
283 sources,
284 output_file: self.zip_output.clone().into(),
285 method: self.method,
286 aes256: false,
287 },
288 self.factory.as_mut(),
289 ) {
290 self.create = false;
291 }
292 }
293 });
294 if ui.button("닫기").clicked() {
295 self.create = false;
296 }
297 });
298 }
299 if let Some((_, attempt, encrypt)) = self.model.challenge {
300 egui::Window::new(if encrypt {
301 "ZIP 암호 설정"
302 } else {
303 "압축파일 암호"
304 })
305 .collapsible(false)
306 .show(&ctx, |ui| {
307 ui.label(format!("암호 입력 요청 {attempt}"));
308 let edit = ui.add(
309 egui::TextEdit::singleline(&mut self.password)
310 .password(true)
311 .char_limit(1024),
312 );
313 if ui.button("암호 전달").clicked()
314 || (edit.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)))
315 {
316 let text = std::mem::take(&mut self.password);
317 self.model.password(text);
318 }
319 if ui.button("작업 취소").clicked() {
320 self.password.clear();
321 self.model.cancel();
322 }
323 });
324 }
325 match self.model.close {
326 Close::Confirm => {
327 egui::Window::new("작업 중 닫기")
328 .collapsible(false)
329 .show(&ctx, |ui| {
330 ui.label("취소를 요청하고 작업자 종료를 기다릴까요?");
331 if ui.button("계속 열기").clicked() {
332 self.model.keep_open();
333 }
334 if ui.button("취소 후 기다리기").clicked() {
335 self.model.confirm_close();
336 }
337 });
338 }
339 Close::Waiting => {
340 egui::Window::new("종료 확인 중")
341 .collapsible(false)
342 .show(&ctx, |ui| {
343 ui.label("취소 결과와 프로세스 종료를 기다립니다");
344 if ui.button("닫기 철회").clicked() {
345 self.model.keep_open();
346 }
347 });
348 }
349 Close::Review => {
350 egui::Window::new("결과 확인 후 닫기")
351 .collapsible(false)
352 .show(&ctx, |ui| {
353 ui.label(self.model.status());
354 if let Some(t) = &self.model.terminal {
355 for line in terminal_lines(t) {
356 ui.label(line);
357 }
358 }
359 if ui.button("계속 열기").clicked() {
360 self.model.keep_open();
361 }
362 if ui.button("닫기").clicked() {
363 self.model.acknowledge_close();
364 }
365 });
366 }
367 _ => {}
368 }
369 if self.model.busy() {
370 ctx.request_repaint_after(std::time::Duration::from_millis(30));
371 }
372 }
373}
374
375impl eframe::App for ArchiveApp {
376 fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) {
377 self.show(root);
378 }
379}