team-ljyzipelo

공개
383줄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 self.model
179 .list_id
180 .as_ref()
181 .filter(|id| id.format == Format::Gzip)
182 .map(|_| self.gzip_name.clone()),
183 self.factory.as_mut(),
184 );
185 }
186 });
187 });
188 }
189 ui.separator();
190 egui::ScrollArea::both().show(ui, |ui| {
191 egui::Grid::new("archive-list")
192 .striped(true)
193 .min_col_width(90.0)
194 .show(ui, |ui| {
195 ui.label("선택");
196 ui.label("이름");
197 ui.label("크기");
198 ui.label("형식");
199 ui.label("암호");
200 ui.end_row();
201 for entry in &self.model.entries {
202 let mut selected = self.model.selected.contains(&entry.id);
203 if ui
204 .add_enabled(
205 !self.model.busy()
206 && matches!(entry.kind, Kind::File | Kind::Directory),
207 egui::Checkbox::without_text(&mut selected),
208 )
209 .on_hover_text(format!("{} 선택", entry.name))
210 .changed()
211 {
212 if selected {
213 self.model.selected.insert(entry.id);
214 } else {
215 self.model.selected.remove(&entry.id);
216 }
217 }
218 ui.label(&entry.name);
219 ui.label(
220 entry
221 .bytes
222 .map_or("알 수 없음".into(), |n| format!("{n} B")),
223 );
224 ui.label(match entry.kind {
225 Kind::File => "파일",
226 Kind::Directory => "폴더",
227 Kind::Link => "링크",
228 Kind::Other => "기타",
229 });
230 ui.label(if entry.encrypted { "있음" } else { "없음" });
231 ui.end_row();
232 }
233 });
234 if self.model.entries.is_empty() && self.model.list_id.is_some() {
235 ui.label("압축파일에 항목이 없습니다");
236 }
237 for (name, state) in &self.model.outputs {
238 ui.label(format!("{name}: {}", disposition_label(*state)));
239 }
240 });
241 });
242 if self.open {
243 egui::Window::new("파일 열기")
244 .collapsible(false)
245 .show(&ctx, |ui| {
246 ui.label("압축파일 절대 경로");
247 ui.text_edit_singleline(&mut self.open_text);
248 if ui.button("열기").clicked() {
249 self.open_path(self.open_text.clone().into());
250 self.open = false;
251 }
252 if ui.button("닫기").clicked() {
253 self.open = false;
254 }
255 });
256 }
257 if self.create {
258 egui::Window::new("ZIP 만들기")
259 .collapsible(false)
260 .show(&ctx, |ui| {
261 ui.add_enabled_ui(!self.model.busy(), |ui| {
262 ui.label("원본 파일 절대 경로 / 한 줄에 하나");
263 ui.add(egui::TextEdit::multiline(&mut self.sources).desired_rows(4));
264 ui.label("저장할 ZIP 절대 경로");
265 ui.text_edit_singleline(&mut self.zip_output);
266 ui.horizontal(|ui| {
267 ui.selectable_value(&mut self.method, Method::Deflate, "압축");
268 ui.selectable_value(&mut self.method, Method::Store, "묶기");
269 });
270 ui.label("암호 ZIP 생성은 작업자 지원 후 활성화됩니다");
271 if ui.button("ZIP 만들기").clicked() {
272 let sources = self
273 .sources
274 .lines()
275 .map(|s| {
276 let path = PathBuf::from(s);
277 let name = path
278 .file_name()
279 .and_then(|n| n.to_str())
280 .unwrap_or("")
281 .to_owned();
282 (path, name)
283 })
284 .collect();
285 if self.model.begin(
286 Start::CreateZip {
287 sources,
288 output_file: self.zip_output.clone().into(),
289 method: self.method,
290 aes256: false,
291 },
292 self.factory.as_mut(),
293 ) {
294 self.create = false;
295 }
296 }
297 });
298 if ui.button("닫기").clicked() {
299 self.create = false;
300 }
301 });
302 }
303 if let Some((_, attempt, encrypt)) = self.model.challenge {
304 egui::Window::new(if encrypt {
305 "ZIP 암호 설정"
306 } else {
307 "압축파일 암호"
308 })
309 .collapsible(false)
310 .show(&ctx, |ui| {
311 ui.label(format!("암호 입력 요청 {attempt}"));
312 let edit = ui.add(
313 egui::TextEdit::singleline(&mut self.password)
314 .password(true)
315 .char_limit(1024),
316 );
317 if ui.button("암호 전달").clicked()
318 || (edit.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)))
319 {
320 let text = std::mem::take(&mut self.password);
321 self.model.password(text);
322 }
323 if ui.button("작업 취소").clicked() {
324 self.password.clear();
325 self.model.cancel();
326 }
327 });
328 }
329 match self.model.close {
330 Close::Confirm => {
331 egui::Window::new("작업 중 닫기")
332 .collapsible(false)
333 .show(&ctx, |ui| {
334 ui.label("취소를 요청하고 작업자 종료를 기다릴까요?");
335 if ui.button("계속 열기").clicked() {
336 self.model.keep_open();
337 }
338 if ui.button("취소 후 기다리기").clicked() {
339 self.model.confirm_close();
340 }
341 });
342 }
343 Close::Waiting => {
344 egui::Window::new("종료 확인 중")
345 .collapsible(false)
346 .show(&ctx, |ui| {
347 ui.label("취소 결과와 프로세스 종료를 기다립니다");
348 if ui.button("닫기 철회").clicked() {
349 self.model.keep_open();
350 }
351 });
352 }
353 Close::Review => {
354 egui::Window::new("결과 확인 후 닫기")
355 .collapsible(false)
356 .show(&ctx, |ui| {
357 ui.label(self.model.status());
358 if let Some(t) = &self.model.terminal {
359 for line in terminal_lines(t) {
360 ui.label(line);
361 }
362 }
363 if ui.button("계속 열기").clicked() {
364 self.model.keep_open();
365 }
366 if ui.button("닫기").clicked() {
367 self.model.acknowledge_close();
368 }
369 });
370 }
371 _ => {}
372 }
373 if self.model.busy() {
374 ctx.request_repaint_after(std::time::Duration::from_millis(30));
375 }
376 }
377}
378
379impl eframe::App for ArchiveApp {
380 fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) {
381 self.show(root);
382 }
383}