| 1 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 2 | #[path = "../../../tests/rust-app-ui/scenarios.rs"] |
| 3 | pub mod scenarios; |
| 4 | |
| 5 | use crate::{ |
| 6 | archive_browser as browser, |
| 7 | archive_dialog::{ |
| 8 | DIALOG_UNAVAILABLE, DialogAnswer, DialogRequest, Dialogs, NativeDialogs, PendingDialog, |
| 9 | Purpose, |
| 10 | }, |
| 11 | archive_present as present, |
| 12 | archive_theme::{self as theme, Icon}, |
| 13 | archive_ui::*, |
| 14 | window_frame, |
| 15 | }; |
| 16 | use eframe::egui::{self, Align, Layout, RichText}; |
| 17 | use std::{ |
| 18 | collections::BTreeSet, |
| 19 | path::{Path, PathBuf}, |
| 20 | }; |
| 21 | |
| 22 | const WORK_WIDTH: f32 = 480.0; |
| 23 | const CREATE_WIDTH: f32 = 560.0; |
| 24 | /// 처음 열 때의 높이. 그다음부터는 내용 높이에 맞춤. |
| 25 | const CREATE_MIN_HEIGHT: f32 = 300.0; |
| 26 | const CREATE_LIST_HEIGHT: f32 = 288.0; |
| 27 | |
| 28 | /// ZIP 만들기 창 목록의 한 행. 디스크에서 읽은 파일이나 폴더임. |
| 29 | #[derive(Clone)] |
| 30 | struct SourceRow { |
| 31 | name: String, |
| 32 | directory: bool, |
| 33 | bytes: Option<u64>, |
| 34 | } |
| 35 | |
| 36 | fn source_row(path: &Path) -> SourceRow { |
| 37 | let metadata = std::fs::symlink_metadata(path).ok(); |
| 38 | SourceRow { |
| 39 | name: path |
| 40 | .file_name() |
| 41 | .map(|name| name.to_string_lossy().into_owned()) |
| 42 | .unwrap_or_default(), |
| 43 | // 심볼릭 링크는 따라가지 않으므로 폴더로 보이지 않음. |
| 44 | directory: metadata.as_ref().is_some_and(|m| m.is_dir()), |
| 45 | bytes: metadata.filter(|m| m.is_file()).map(|m| m.len()), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// 폴더 안 항목을 메인 목록과 같은 순서(폴더 먼저, 이름순)로 읽음. 읽지 못하면 None. |
| 50 | fn read_source_dir(dir: &Path) -> Option<std::rc::Rc<Vec<SourceRow>>> { |
| 51 | let mut rows = std::fs::read_dir(dir) |
| 52 | .ok()? |
| 53 | .filter_map(Result::ok) |
| 54 | .map(|entry| source_row(&entry.path())) |
| 55 | .collect::<Vec<_>>(); |
| 56 | rows.sort_by(|a, b| { |
| 57 | b.directory |
| 58 | .cmp(&a.directory) |
| 59 | .then_with(|| a.name.cmp(&b.name)) |
| 60 | }); |
| 61 | Some(std::rc::Rc::new(rows)) |
| 62 | } |
| 63 | |
| 64 | /// 덮어쓴 뒤 버려 메모리에 암호가 남지 않게 함. |
| 65 | fn wipe(value: &mut String) { |
| 66 | let mut bytes = std::mem::take(value).into_bytes(); |
| 67 | bytes.fill(0); |
| 68 | } |
| 69 | const WORK_MIN_HEIGHT: f32 = 96.0; |
| 70 | const WORK_MAX_HEIGHT: f32 = 520.0; |
| 71 | const WORK_RESULT_MAX_HEIGHT: f32 = 220.0; |
| 72 | |
| 73 | /// 작업 진행·결과를 보여주는 자식 창의 위치 상태. |
| 74 | /// 부모 창 기준 상대 위치를 기억해 두고, 부모 창이 움직이면 같은 상대 위치로 따라감. |
| 75 | struct WorkWindow { |
| 76 | open: bool, |
| 77 | initial: Option<egui::Pos2>, |
| 78 | offset: Option<egui::Vec2>, |
| 79 | parent: Option<egui::Pos2>, |
| 80 | expected: Option<egui::Pos2>, |
| 81 | settle_until: f64, |
| 82 | height: f32, |
| 83 | } |
| 84 | |
| 85 | impl Default for WorkWindow { |
| 86 | fn default() -> Self { |
| 87 | Self { |
| 88 | open: false, |
| 89 | initial: None, |
| 90 | offset: None, |
| 91 | parent: None, |
| 92 | expected: None, |
| 93 | settle_until: 0.0, |
| 94 | height: 168.0, |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl WorkWindow { |
| 100 | /// 부모 창 가운데에 열고, 사용자가 옮긴 상대 위치를 유지한 채 부모 창을 따라가게 함. |
| 101 | /// 앱을 쓰는 동안에만 부모 위에 두고, 처음 위치를 담은 ViewportBuilder를 돌려줌. |
| 102 | /// Wayland처럼 창 위치를 알 수 없으면 위치는 창 관리자에 맡김. |
| 103 | fn builder( |
| 104 | &mut self, |
| 105 | ctx: &egui::Context, |
| 106 | id: egui::ViewportId, |
| 107 | size: egui::Vec2, |
| 108 | title: &str, |
| 109 | decorations: bool, |
| 110 | native_dialog_open: bool, |
| 111 | ) -> egui::ViewportBuilder { |
| 112 | let (parent, parent_focused, now) = ctx.input(|i| { |
| 113 | ( |
| 114 | i.viewport().outer_rect, |
| 115 | i.viewport().focused.unwrap_or(false), |
| 116 | i.time, |
| 117 | ) |
| 118 | }); |
| 119 | let (child, child_focused) = ctx.input(|i| { |
| 120 | i.raw.viewports.get(&id).map_or((None, false), |v| { |
| 121 | (v.outer_rect, v.focused.unwrap_or(false)) |
| 122 | }) |
| 123 | }); |
| 124 | if !self.open { |
| 125 | // 처음 열 때 부모 창 가운데에 둠. 닫혔다 다시 열 때는 기억한 위치에 둠. |
| 126 | self.open = true; |
| 127 | self.initial = parent.map(|p| match self.offset { |
| 128 | Some(offset) => (p.min + offset).round(), |
| 129 | None => (p.center() - size / 2.0).round(), |
| 130 | }); |
| 131 | self.offset = self.initial.zip(parent).map(|(c, p)| c - p.min); |
| 132 | self.parent = parent.map(|p| p.min); |
| 133 | self.expected = self.initial; |
| 134 | self.settle_until = now + 0.5; |
| 135 | } |
| 136 | if let (Some(parent), Some(child)) = (parent, child) { |
| 137 | let parent_moved = self.parent.is_some_and(|p| (p - parent.min).length() > 0.5); |
| 138 | let settled = self |
| 139 | .expected |
| 140 | .is_some_and(|e| (child.min - e).length() <= 2.0); |
| 141 | if settled || now >= self.settle_until { |
| 142 | self.settle_until = 0.0; |
| 143 | } |
| 144 | if parent_moved { |
| 145 | if let Some(offset) = self.offset { |
| 146 | let target = (parent.min + offset).round(); |
| 147 | ctx.send_viewport_cmd_to(id, egui::ViewportCommand::OuterPosition(target)); |
| 148 | self.expected = Some(target); |
| 149 | self.settle_until = now + 0.5; |
| 150 | } |
| 151 | } else if self.settle_until == 0.0 && !settled { |
| 152 | // 사용자가 자식 창을 옮긴 위치를 부모 기준으로 기억함. |
| 153 | self.offset = Some(child.min - parent.min); |
| 154 | self.expected = Some(child.min); |
| 155 | } |
| 156 | self.parent = Some(parent.min); |
| 157 | } |
| 158 | let mut builder = egui::ViewportBuilder::default() |
| 159 | .with_title(title) |
| 160 | .with_icon(crate::app_icon::load()) |
| 161 | .with_decorations(decorations) |
| 162 | .with_inner_size(size) |
| 163 | .with_resizable(false) |
| 164 | .with_minimize_button(false) |
| 165 | .with_maximize_button(false) |
| 166 | .with_taskbar(false) |
| 167 | .with_window_level( |
| 168 | if !native_dialog_open && (parent_focused || child_focused) { |
| 169 | egui::WindowLevel::AlwaysOnTop |
| 170 | } else { |
| 171 | egui::WindowLevel::Normal |
| 172 | }, |
| 173 | ); |
| 174 | if let Some(position) = self.initial { |
| 175 | builder = builder.with_position(position); |
| 176 | } |
| 177 | builder |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | struct Marquee { |
| 182 | // 스크롤 중에도 시작점을 목록 좌표로 유지함. |
| 183 | origin: egui::Pos2, |
| 184 | base: BTreeSet<u32>, |
| 185 | moved: bool, |
| 186 | } |
| 187 | |
| 188 | /// 파일 목록 한 행의 높이. 행 안의 크기 칸(theme::ROW)이 위아래 2px 여백으로 들어감. |
| 189 | const LIST_ROW: f32 = 36.0; |
| 190 | |
| 191 | pub struct ArchiveApp { |
| 192 | pub model: Model, |
| 193 | factory: Box<dyn Factory>, |
| 194 | destination: PathBuf, |
| 195 | automatic_destination: bool, |
| 196 | owned_destination: Option<PathBuf>, |
| 197 | gzip_name: String, |
| 198 | sources: Vec<PathBuf>, |
| 199 | zip_folder: PathBuf, |
| 200 | zip_name: String, |
| 201 | password: String, |
| 202 | password_confirm: String, |
| 203 | password_visible: bool, |
| 204 | password_challenge: Option<u64>, |
| 205 | conflict_challenge: Option<(u64, u64)>, |
| 206 | conflict_apply: bool, |
| 207 | aes256: bool, |
| 208 | create: bool, |
| 209 | selected_only: bool, |
| 210 | method: Method, |
| 211 | dialogs: Box<dyn Dialogs>, |
| 212 | picker: Option<(Purpose, std::sync::mpsc::Receiver<DialogAnswer>)>, |
| 213 | preview_open: bool, |
| 214 | preview_name: String, |
| 215 | preview_text: String, |
| 216 | preview_texture: Option<egui::TextureHandle>, |
| 217 | preview_return_focus: Option<egui::Id>, |
| 218 | preview_initial_focus: bool, |
| 219 | work: WorkWindow, |
| 220 | create_win: WorkWindow, |
| 221 | /// ZIP 만들기 창에서 보고 있는 폴더 (고른 폴더 기준 상대 경로, 비어 있으면 최상위). |
| 222 | create_folder: String, |
| 223 | create_listing: Option<(PathBuf, Option<std::rc::Rc<Vec<SourceRow>>>)>, |
| 224 | /// 마지막으로 누른 폴더 행 (이름, 프레임 번호, 시각). 두 번 누름을 직접 판단할 때 씀. |
| 225 | create_last_click: Option<(String, u64, f64)>, |
| 226 | /// ZIP 만들기 목록에서 한 번 눌러 선택한 항목 이름. 보고 있는 폴더가 바뀌면 지움. |
| 227 | create_selected: Option<String>, |
| 228 | zip_password: String, |
| 229 | zip_password_confirm: String, |
| 230 | zip_password_visible: bool, |
| 231 | /// 암호를 지웠으면 입력창의 실행 취소 기록도 다음 그리기 전에 지움. |
| 232 | forget_password_history: Vec<&'static str>, |
| 233 | /// 작업을 시작한 뒤 작업자가 암호를 요청하면 넘길 암호. 넘기거나 작업이 끝나면 지움. |
| 234 | pending_zip_password: Option<String>, |
| 235 | custom_frame: bool, |
| 236 | release_after_move: bool, |
| 237 | folder: String, |
| 238 | search: String, |
| 239 | list_anchor: Option<usize>, |
| 240 | list_cursor: Option<usize>, |
| 241 | list_offset: f32, |
| 242 | list_focused: bool, |
| 243 | marquee: Option<Marquee>, |
| 244 | last_request: Option<Start>, |
| 245 | targets: Option<BTreeSet<String>>, |
| 246 | directories: BTreeSet<String>, |
| 247 | dismissed: bool, |
| 248 | details: bool, |
| 249 | checked_destination: bool, |
| 250 | ui_error: Option<String>, |
| 251 | initialized: bool, |
| 252 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 253 | scenario_marquee: Option<egui::Rect>, |
| 254 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 255 | scenario_sources: Option<std::rc::Rc<Vec<SourceRow>>>, |
| 256 | } |
| 257 | |
| 258 | impl ArchiveApp { |
| 259 | pub fn new(factory: Box<dyn Factory>, startup: Option<PathBuf>, _fixture: bool) -> Self { |
| 260 | let mut app = Self { |
| 261 | model: Model::default(), |
| 262 | factory, |
| 263 | destination: PathBuf::new(), |
| 264 | automatic_destination: true, |
| 265 | owned_destination: None, |
| 266 | gzip_name: String::new(), |
| 267 | sources: Vec::new(), |
| 268 | zip_folder: present::home_directory(), |
| 269 | zip_name: String::new(), |
| 270 | password: String::new(), |
| 271 | password_confirm: String::new(), |
| 272 | password_visible: false, |
| 273 | password_challenge: None, |
| 274 | conflict_challenge: None, |
| 275 | conflict_apply: false, |
| 276 | aes256: false, |
| 277 | create: false, |
| 278 | selected_only: false, |
| 279 | method: Method::Deflate, |
| 280 | dialogs: Box::new(NativeDialogs), |
| 281 | picker: None, |
| 282 | preview_open: false, |
| 283 | preview_name: String::new(), |
| 284 | preview_text: String::new(), |
| 285 | preview_texture: None, |
| 286 | preview_return_focus: None, |
| 287 | preview_initial_focus: false, |
| 288 | work: WorkWindow::default(), |
| 289 | create_win: WorkWindow::default(), |
| 290 | create_folder: String::new(), |
| 291 | create_listing: None, |
| 292 | create_last_click: None, |
| 293 | create_selected: None, |
| 294 | zip_password: String::new(), |
| 295 | zip_password_confirm: String::new(), |
| 296 | zip_password_visible: false, |
| 297 | forget_password_history: Vec::new(), |
| 298 | pending_zip_password: None, |
| 299 | custom_frame: false, |
| 300 | release_after_move: false, |
| 301 | folder: String::new(), |
| 302 | search: String::new(), |
| 303 | list_anchor: None, |
| 304 | list_cursor: None, |
| 305 | list_offset: 0.0, |
| 306 | list_focused: false, |
| 307 | marquee: None, |
| 308 | last_request: None, |
| 309 | targets: None, |
| 310 | directories: BTreeSet::new(), |
| 311 | dismissed: false, |
| 312 | details: false, |
| 313 | checked_destination: false, |
| 314 | ui_error: None, |
| 315 | initialized: false, |
| 316 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 317 | scenario_marquee: None, |
| 318 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 319 | scenario_sources: None, |
| 320 | }; |
| 321 | if let Some(path) = startup { |
| 322 | app.open_path(path); |
| 323 | } |
| 324 | app |
| 325 | } |
| 326 | |
| 327 | pub fn set_dialogs(&mut self, dialogs: Box<dyn Dialogs>) { |
| 328 | self.dialogs = dialogs; |
| 329 | } |
| 330 | |
| 331 | /// 운영체제가 창 테두리를 그려 주지 않을 때(GNOME Wayland) 제목 표시줄을 직접 그림. |
| 332 | pub fn set_custom_frame(&mut self, custom: bool) { |
| 333 | self.custom_frame = custom; |
| 334 | } |
| 335 | |
| 336 | pub fn browse_folder(&mut self, folder: &str) { |
| 337 | self.folder = folder.trim_matches('/').to_owned(); |
| 338 | self.search.clear(); |
| 339 | self.clear_list_selection(); |
| 340 | } |
| 341 | |
| 342 | pub fn show_create(&mut self, encrypted: bool) { |
| 343 | if !self.model.busy() { |
| 344 | self.create = true; |
| 345 | self.aes256 = encrypted; |
| 346 | self.ui_error = None; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | fn clear_password(&mut self) { |
| 351 | for value in [&mut self.password, &mut self.password_confirm] { |
| 352 | let mut bytes = std::mem::take(value).into_bytes(); |
| 353 | bytes.fill(0); |
| 354 | } |
| 355 | self.password_visible = false; |
| 356 | // 이 함수는 암호 요청이 없는 동안 매 프레임 불리므로, 암호 창을 쓴 뒤에만 기록을 지움. |
| 357 | if self.password_challenge.is_some() { |
| 358 | self.forget_password_history |
| 359 | .extend(["archive-password-input", "archive-password-confirm"]); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /// 지운 암호가 입력창의 실행 취소(Ctrl+Z)로 되살아나지 않게 egui가 보관하는 |
| 364 | /// 입력창 상태(실행 취소 기록 포함)를 지움. |
| 365 | fn forget_password_history(&mut self, ctx: &egui::Context) { |
| 366 | if self.forget_password_history.is_empty() { |
| 367 | return; |
| 368 | } |
| 369 | let names = std::mem::take(&mut self.forget_password_history); |
| 370 | ctx.data_mut(|data| { |
| 371 | for name in names { |
| 372 | data.remove::<egui::text_edit::TextEditState>(egui::Id::unique(name)); |
| 373 | } |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | fn clear_zip_password(&mut self) { |
| 378 | wipe(&mut self.zip_password); |
| 379 | wipe(&mut self.zip_password_confirm); |
| 380 | if let Some(mut pending) = self.pending_zip_password.take() { |
| 381 | wipe(&mut pending); |
| 382 | } |
| 383 | self.zip_password_visible = false; |
| 384 | self.forget_password_history |
| 385 | .extend(["zip-password-input", "zip-password-confirm"]); |
| 386 | } |
| 387 | |
| 388 | /// 암호로 보호하지 않거나, 두 암호가 같고 1~1024바이트이면 만들 수 있음. |
| 389 | fn zip_password_ready(&self) -> bool { |
| 390 | !self.aes256 |
| 391 | || (!self.zip_password.is_empty() |
| 392 | && self.zip_password.len() <= 1024 |
| 393 | && self.zip_password == self.zip_password_confirm) |
| 394 | } |
| 395 | |
| 396 | fn close_create(&mut self) { |
| 397 | self.create = false; |
| 398 | self.ui_error = None; |
| 399 | self.clear_zip_password(); |
| 400 | self.create_win = WorkWindow::default(); |
| 401 | } |
| 402 | |
| 403 | /// 압축하기: 묶을 폴더를 운영체제 폴더 선택 창에서 고른 뒤 ZIP 만들기 창을 엶. |
| 404 | fn choose_zip_folder(&mut self) { |
| 405 | if self.model.busy() || self.picker.is_some() { |
| 406 | return; |
| 407 | } |
| 408 | // 이미 열린 ZIP 만들기 창의 목록과 암호 설정은 새 폴더를 실제로 고른 뒤에만 바꿈. |
| 409 | // 선택을 취소하면 그대로 남음(apply_picker의 Purpose::Folder). |
| 410 | self.open_picker(Purpose::Folder); |
| 411 | } |
| 412 | |
| 413 | /// 폴더 하나를 골랐으면 그 폴더 안을 목록으로 보여 줌. 여러 항목이면 그 항목들을 보여 줌. |
| 414 | fn create_root(&self) -> Option<PathBuf> { |
| 415 | (self.sources.len() == 1 && self.sources[0].is_dir()).then(|| self.sources[0].clone()) |
| 416 | } |
| 417 | |
| 418 | fn create_rows(&mut self) -> Option<std::rc::Rc<Vec<SourceRow>>> { |
| 419 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 420 | if let Some(rows) = &self.scenario_sources { |
| 421 | return Some(rows.clone()); |
| 422 | } |
| 423 | let Some(root) = self.create_root() else { |
| 424 | return Some(std::rc::Rc::new( |
| 425 | self.sources.iter().map(|path| source_row(path)).collect(), |
| 426 | )); |
| 427 | }; |
| 428 | let dir = if self.create_folder.is_empty() { |
| 429 | root |
| 430 | } else { |
| 431 | root.join(&self.create_folder) |
| 432 | }; |
| 433 | // 프레임마다 디스크를 읽지 않도록 보고 있는 폴더가 바뀔 때만 다시 읽음. |
| 434 | if self |
| 435 | .create_listing |
| 436 | .as_ref() |
| 437 | .is_none_or(|(path, _)| path != &dir) |
| 438 | { |
| 439 | let rows = read_source_dir(&dir); |
| 440 | self.create_listing = Some((dir, rows)); |
| 441 | } |
| 442 | self.create_listing |
| 443 | .as_ref() |
| 444 | .and_then(|(_, rows)| rows.clone()) |
| 445 | } |
| 446 | |
| 447 | pub fn open_path(&mut self, archive: PathBuf) { |
| 448 | if self.model.busy() { |
| 449 | return; |
| 450 | } |
| 451 | self.clear_password(); |
| 452 | self.destination = present::default_destination(&archive); |
| 453 | self.automatic_destination = true; |
| 454 | self.gzip_name = present::gzip_output_name(&archive); |
| 455 | self.selected_only = false; |
| 456 | self.folder.clear(); |
| 457 | self.search.clear(); |
| 458 | self.clear_list_selection(); |
| 459 | self.targets = None; |
| 460 | self.directories.clear(); |
| 461 | self.start(Start::List { archive }); |
| 462 | } |
| 463 | |
| 464 | fn start(&mut self, request: Start) -> bool { |
| 465 | // 파일 선택 창이 응답을 기다리는 동안에는 어느 창에서도 새 작업을 시작하지 않음. |
| 466 | if self.picker.is_some() { |
| 467 | return false; |
| 468 | } |
| 469 | self.last_request = Some(request.clone()); |
| 470 | self.dismissed = false; |
| 471 | self.details = false; |
| 472 | self.checked_destination = false; |
| 473 | self.ui_error = None; |
| 474 | self.model.begin(request, self.factory.as_mut()) |
| 475 | } |
| 476 | |
| 477 | pub fn choose_destination(&mut self, path: PathBuf) { |
| 478 | self.destination = path; |
| 479 | self.automatic_destination = false; |
| 480 | self.ui_error = None; |
| 481 | } |
| 482 | |
| 483 | pub fn add_sources(&mut self, paths: Vec<PathBuf>) { |
| 484 | if self.model.busy() { |
| 485 | return; |
| 486 | } |
| 487 | let mut validated = Vec::new(); |
| 488 | for path in paths { |
| 489 | let Ok(metadata) = std::fs::symlink_metadata(&path) else { |
| 490 | self.ui_error = Some("파일이나 폴더를 찾지 못했습니다. 다시 선택하세요".into()); |
| 491 | return; |
| 492 | }; |
| 493 | if metadata.file_type().is_symlink() || !(metadata.is_file() || metadata.is_dir()) { |
| 494 | self.ui_error = Some("일반 파일과 폴더만 추가할 수 있습니다".into()); |
| 495 | return; |
| 496 | } |
| 497 | let Ok(path) = path.canonicalize() else { |
| 498 | return; |
| 499 | }; |
| 500 | if path.to_str().is_none() |
| 501 | || !path |
| 502 | .file_name() |
| 503 | .and_then(|name| name.to_str()) |
| 504 | .is_some_and(valid_leaf) |
| 505 | { |
| 506 | self.ui_error = Some("파일이나 폴더의 이름을 확인하세요".into()); |
| 507 | return; |
| 508 | } |
| 509 | validated.push(crate::archive_path::worker_path(&path)); |
| 510 | } |
| 511 | let paths = validated; |
| 512 | if self.sources.is_empty() { |
| 513 | if let Some(first) = paths.first() { |
| 514 | self.zip_folder = first.parent().unwrap_or(Path::new("/")).to_path_buf(); |
| 515 | let name = if first.is_dir() { |
| 516 | first.file_name() |
| 517 | } else { |
| 518 | first.file_stem() |
| 519 | }; |
| 520 | self.zip_name = format!("{}.zip", name.unwrap_or_default().to_string_lossy()); |
| 521 | } |
| 522 | } |
| 523 | for path in paths { |
| 524 | if !self.sources.contains(&path) { |
| 525 | self.sources.push(path); |
| 526 | } |
| 527 | } |
| 528 | self.create_folder.clear(); |
| 529 | self.create_listing = None; |
| 530 | self.create_selected = None; |
| 531 | self.create = true; |
| 532 | } |
| 533 | |
| 534 | pub fn begin_extract(&mut self) -> bool { |
| 535 | self.selected_only = !self.model.selected.is_empty(); |
| 536 | let selection = if self.selected_only { |
| 537 | Selection::EntryIds( |
| 538 | browser::expand_selection(&self.model.entries, &self.model.selected) |
| 539 | .into_iter() |
| 540 | .collect(), |
| 541 | ) |
| 542 | } else { |
| 543 | Selection::All |
| 544 | }; |
| 545 | self.extract_selection(selection) |
| 546 | } |
| 547 | |
| 548 | fn extract_selection(&mut self, selection: Selection) -> bool { |
| 549 | if !self.model.can_extract() |
| 550 | || matches!(&selection, Selection::EntryIds(ids) if ids.is_empty()) |
| 551 | { |
| 552 | return false; |
| 553 | } |
| 554 | self.selected_only = matches!(&selection, Selection::EntryIds(_)); |
| 555 | match present::prepare_destination(&self.destination, self.automatic_destination) { |
| 556 | Ok(true) => self.owned_destination = Some(self.destination.clone()), |
| 557 | Ok(false) => self.owned_destination = None, |
| 558 | Err(_) => { |
| 559 | self.ui_error = Some(present::FOLDER_ERROR.into()); |
| 560 | return false; |
| 561 | } |
| 562 | } |
| 563 | let gzip = self |
| 564 | .model |
| 565 | .list_id |
| 566 | .as_ref() |
| 567 | .is_some_and(|id| id.format == Format::Gzip); |
| 568 | self.targets = present::target_files( |
| 569 | &self.model.entries, |
| 570 | &selection, |
| 571 | gzip.then_some(self.gzip_name.as_str()), |
| 572 | ); |
| 573 | self.directories = if gzip { |
| 574 | BTreeSet::new() |
| 575 | } else { |
| 576 | present::target_directories(&self.model.entries, &selection) |
| 577 | }; |
| 578 | let request = Start::Extract { |
| 579 | archive: self.model.archive.clone().unwrap(), |
| 580 | destination: self.destination.clone(), |
| 581 | selection, |
| 582 | list_id: self.model.list_id.clone().unwrap(), |
| 583 | gzip_output_name: gzip.then(|| self.gzip_name.clone()), |
| 584 | }; |
| 585 | if self.start(request) { |
| 586 | true |
| 587 | } else { |
| 588 | if let Some(path) = self.owned_destination.take() { |
| 589 | present::remove_owned_empty(&path); |
| 590 | } |
| 591 | false |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | fn begin_zip(&mut self) { |
| 596 | if self.sources.is_empty() { |
| 597 | return; |
| 598 | } |
| 599 | if !self.zip_name.to_ascii_lowercase().ends_with(".zip") { |
| 600 | self.zip_name.push_str(".zip"); |
| 601 | } |
| 602 | if !valid_leaf(&self.zip_name) { |
| 603 | self.ui_error = Some("ZIP 파일 이름을 확인하세요".into()); |
| 604 | return; |
| 605 | } |
| 606 | let output_file = self.zip_folder.join(&self.zip_name); |
| 607 | if std::fs::symlink_metadata(&output_file).is_ok() { |
| 608 | self.ui_error = Some("같은 이름의 파일이 이미 있습니다. 다른 이름을 입력하세요".into()); |
| 609 | return; |
| 610 | } |
| 611 | let mut sources = Vec::new(); |
| 612 | for path in &self.sources { |
| 613 | let Some(name) = path.file_name().and_then(|name| name.to_str()) else { |
| 614 | self.ui_error = Some( |
| 615 | "이름을 읽을 수 없는 파일이 있습니다. 파일 이름을 바꾼 뒤 다시 추가하세요" |
| 616 | .into(), |
| 617 | ); |
| 618 | return; |
| 619 | }; |
| 620 | sources.push((path.clone(), name.to_owned())); |
| 621 | } |
| 622 | if !self.zip_password_ready() { |
| 623 | self.ui_error = Some("두 암호가 같은지 확인하세요".into()); |
| 624 | return; |
| 625 | } |
| 626 | self.targets = Some([self.zip_name.clone()].into_iter().collect()); |
| 627 | self.directories.clear(); |
| 628 | // 암호는 ZIP 만들기 창에서 받아 두었다가 작업자가 요청하면 넘김. |
| 629 | let password = self.aes256.then(|| std::mem::take(&mut self.zip_password)); |
| 630 | if self.start(Start::CreateZip { |
| 631 | sources, |
| 632 | output_file, |
| 633 | method: self.method, |
| 634 | aes256: self.aes256, |
| 635 | }) { |
| 636 | self.create = false; |
| 637 | self.create_win = WorkWindow::default(); |
| 638 | self.clear_zip_password(); |
| 639 | self.pending_zip_password = password; |
| 640 | } else if let Some(password) = password { |
| 641 | self.zip_password = password; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | fn open_picker(&mut self, purpose: Purpose) { |
| 646 | if self.model.busy() || self.picker.is_some() { |
| 647 | return; |
| 648 | } |
| 649 | let gzip = self |
| 650 | .model |
| 651 | .list_id |
| 652 | .as_ref() |
| 653 | .is_some_and(|id| id.format == Format::Gzip); |
| 654 | let purpose = match (purpose, gzip) { |
| 655 | (Purpose::Destination, true) => Purpose::SaveGzip, |
| 656 | (Purpose::Retry, true) => Purpose::RetryGzip, |
| 657 | _ => purpose, |
| 658 | }; |
| 659 | let directory = match purpose { |
| 660 | Purpose::SaveZip => self.zip_folder.clone(), |
| 661 | Purpose::Destination | Purpose::SaveGzip => self |
| 662 | .model |
| 663 | .archive |
| 664 | .as_ref() |
| 665 | .and_then(|path| path.parent()) |
| 666 | .map(Path::to_path_buf) |
| 667 | .unwrap_or_else(present::home_directory), |
| 668 | Purpose::Retry | Purpose::RetryGzip => self.destination.clone(), |
| 669 | _ => self |
| 670 | .sources |
| 671 | .last() |
| 672 | .and_then(|path| path.parent()) |
| 673 | .map(Path::to_path_buf) |
| 674 | .unwrap_or_else(present::home_directory), |
| 675 | }; |
| 676 | let request = DialogRequest { |
| 677 | purpose, |
| 678 | directory, |
| 679 | file_name: if matches!(purpose, Purpose::SaveGzip | Purpose::RetryGzip) { |
| 680 | self.gzip_name.clone() |
| 681 | } else { |
| 682 | self.zip_name.clone() |
| 683 | }, |
| 684 | }; |
| 685 | match self.dialogs.start(request) { |
| 686 | PendingDialog::Ready(answer) => self.apply_picker(purpose, answer), |
| 687 | PendingDialog::Waiting(receiver) => self.picker = Some((purpose, receiver)), |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | fn poll_picker(&mut self) { |
| 692 | let Some((purpose, receiver)) = &self.picker else { |
| 693 | return; |
| 694 | }; |
| 695 | let purpose = *purpose; |
| 696 | let answer = match receiver.try_recv() { |
| 697 | Ok(answer) => answer, |
| 698 | Err(std::sync::mpsc::TryRecvError::Empty) => return, |
| 699 | Err(std::sync::mpsc::TryRecvError::Disconnected) => Err(DIALOG_UNAVAILABLE.into()), |
| 700 | }; |
| 701 | self.picker = None; |
| 702 | self.apply_picker(purpose, answer); |
| 703 | } |
| 704 | |
| 705 | fn apply_picker(&mut self, purpose: Purpose, answer: DialogAnswer) { |
| 706 | if answer.is_ok() && self.ui_error.as_deref() == Some(DIALOG_UNAVAILABLE) { |
| 707 | self.ui_error = None; |
| 708 | } |
| 709 | match answer { |
| 710 | Ok(Some(paths)) if !paths.is_empty() => { |
| 711 | let path = paths[0].clone(); |
| 712 | match purpose { |
| 713 | Purpose::Open => { |
| 714 | if std::fs::symlink_metadata(&path) |
| 715 | .is_ok_and(|m| m.is_file() && !m.file_type().is_symlink()) |
| 716 | { |
| 717 | self.open_path(path); |
| 718 | } else { |
| 719 | self.ui_error = Some("일반 압축파일을 선택하세요".into()); |
| 720 | } |
| 721 | } |
| 722 | Purpose::Files => self.add_sources(paths), |
| 723 | Purpose::Folder => { |
| 724 | // 새 폴더로 처음부터 시작함. 고른 폴더가 쓸 수 없으면 기존 내용을 되돌림. |
| 725 | let previous = std::mem::take(&mut self.sources); |
| 726 | self.add_sources(paths); |
| 727 | if self.sources.is_empty() { |
| 728 | self.sources = previous; |
| 729 | } else { |
| 730 | self.aes256 = false; |
| 731 | self.clear_zip_password(); |
| 732 | self.ui_error = None; |
| 733 | } |
| 734 | } |
| 735 | Purpose::Destination | Purpose::Retry => { |
| 736 | self.choose_destination(path); |
| 737 | self.selected_only = !self.model.selected.is_empty(); |
| 738 | if purpose == Purpose::Retry { |
| 739 | self.retry(); |
| 740 | } else { |
| 741 | self.begin_extract(); |
| 742 | } |
| 743 | } |
| 744 | Purpose::SaveGzip | Purpose::RetryGzip => { |
| 745 | let Some(name) = path.file_name().and_then(|name| name.to_str()) else { |
| 746 | self.ui_error = Some("저장할 파일 이름을 확인하세요".into()); |
| 747 | return; |
| 748 | }; |
| 749 | if !present::valid_output_leaf(name) { |
| 750 | self.ui_error = Some("저장할 파일 이름을 확인하세요".into()); |
| 751 | return; |
| 752 | } |
| 753 | let name = name.to_owned(); |
| 754 | self.choose_destination( |
| 755 | path.parent().unwrap_or(Path::new("/")).to_path_buf(), |
| 756 | ); |
| 757 | self.gzip_name = name.clone(); |
| 758 | self.selected_only = !self.model.selected.is_empty(); |
| 759 | if purpose == Purpose::RetryGzip { |
| 760 | if let Some(Start::Extract { |
| 761 | gzip_output_name, .. |
| 762 | }) = &mut self.last_request |
| 763 | { |
| 764 | *gzip_output_name = Some(name); |
| 765 | } |
| 766 | self.retry(); |
| 767 | } else { |
| 768 | self.begin_extract(); |
| 769 | } |
| 770 | } |
| 771 | // 저장 위치를 고르는 사이 ZIP 만들기 창을 닫았으면 아무 작업도 시작하지 않음. |
| 772 | Purpose::SaveZip if !self.create => {} |
| 773 | Purpose::SaveZip => { |
| 774 | if let Some(name) = path.file_name().and_then(|name| name.to_str()) { |
| 775 | self.zip_name = name.to_owned(); |
| 776 | self.zip_folder = path.parent().unwrap_or(Path::new("/")).to_path_buf(); |
| 777 | self.begin_zip(); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | Ok(_) => {} |
| 783 | Err(error) => self.ui_error = Some(error), |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | fn retry(&mut self) { |
| 788 | if let Some(request) = self.last_request.clone() { |
| 789 | match request { |
| 790 | Start::List { archive } => self.open_path(archive), |
| 791 | Start::Preview { .. } => {} |
| 792 | Start::Extract { |
| 793 | selection, |
| 794 | gzip_output_name, |
| 795 | .. |
| 796 | } => { |
| 797 | if let Some(name) = gzip_output_name { |
| 798 | self.gzip_name = name; |
| 799 | } |
| 800 | self.extract_selection(selection); |
| 801 | } |
| 802 | Start::CreateZip { aes256, .. } => { |
| 803 | self.create = true; |
| 804 | if aes256 { |
| 805 | // 암호는 보관하지 않으므로 ZIP 만들기 창에서 다시 받음. |
| 806 | self.aes256 = true; |
| 807 | } else { |
| 808 | self.begin_zip(); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | fn on_drop(&mut self, paths: Vec<PathBuf>) { |
| 816 | if self.preview_open || self.model.busy() || paths.is_empty() { |
| 817 | return; |
| 818 | } |
| 819 | if paths.len() == 1 && !self.create && !paths[0].is_dir() { |
| 820 | if std::fs::symlink_metadata(&paths[0]) |
| 821 | .is_ok_and(|m| m.is_file() && !m.file_type().is_symlink()) |
| 822 | { |
| 823 | self.open_path(paths[0].clone()); |
| 824 | } else { |
| 825 | self.ui_error = Some("일반 압축파일을 선택하세요".into()); |
| 826 | } |
| 827 | } else { |
| 828 | self.add_sources(paths); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | fn poll(&mut self) { |
| 833 | let busy = self.model.busy(); |
| 834 | let close_before = self.model.close; |
| 835 | self.model.poll(); |
| 836 | if close_before == Close::Confirm && self.model.close == Close::Review { |
| 837 | // 종료 확인 창에 답하기 전에 작업이 끝났음. 사용자가 닫기를 승인하지 않았으므로 |
| 838 | // 바로 닫지 않고 계속 물음(close_dialog가 작업이 끝난 문구로 바꿔 보여 줌). |
| 839 | self.model.close = Close::Confirm; |
| 840 | } |
| 841 | if busy && !self.model.busy() { |
| 842 | if let Some(path) = self.owned_destination.take() { |
| 843 | if !self.model.confirmed |
| 844 | || self |
| 845 | .model |
| 846 | .terminal |
| 847 | .as_ref() |
| 848 | .is_none_or(|t| t.outcome != Outcome::Completed) |
| 849 | { |
| 850 | present::remove_owned_empty(&path); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | pub fn show(&mut self, root: &mut egui::Ui) { |
| 857 | let ctx = root.ctx().clone(); |
| 858 | self.forget_password_history(&ctx); |
| 859 | if !self.initialized { |
| 860 | theme::apply(&ctx); |
| 861 | self.initialized = true; |
| 862 | } |
| 863 | self.poll(); |
| 864 | let preview_was_open = self.preview_open; |
| 865 | self.poll_picker(); |
| 866 | if self.model.challenge.is_some_and(|(_, _, encrypt)| encrypt) |
| 867 | && let Some(password) = self.pending_zip_password.take() |
| 868 | { |
| 869 | // ZIP 만들기 창에서 받은 암호를 작업자의 암호 요청에 바로 넘김. |
| 870 | self.model.password(password); |
| 871 | } |
| 872 | if !self.model.busy() |
| 873 | && let Some(mut pending) = self.pending_zip_password.take() |
| 874 | { |
| 875 | wipe(&mut pending); |
| 876 | } |
| 877 | let challenge = self.model.challenge.map(|(id, _, _)| id); |
| 878 | let new_challenge = challenge != self.password_challenge; |
| 879 | if new_challenge || challenge.is_none() { |
| 880 | self.clear_password(); |
| 881 | } |
| 882 | self.password_challenge = challenge; |
| 883 | let dropped = ctx.input(|i| { |
| 884 | i.raw |
| 885 | .dropped_files |
| 886 | .iter() |
| 887 | .map(|file| file.path().to_path_buf()) |
| 888 | .collect() |
| 889 | }); |
| 890 | if self.picker.is_none() { |
| 891 | self.on_drop(dropped); |
| 892 | } |
| 893 | if ctx.input(|i| i.viewport().close_requested()) { |
| 894 | self.model.request_close(); |
| 895 | // 실행 중인 작업이 없으면 결과 확인 안내 없이 바로 닫음. |
| 896 | self.model.acknowledge_close(); |
| 897 | if self.model.close != Close::Allowed { |
| 898 | ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); |
| 899 | } |
| 900 | } |
| 901 | if self.model.close == Close::Review { |
| 902 | // 작업을 취소하고 닫기로 한 뒤 정리가 끝난 경우도 안내 없이 닫음. |
| 903 | self.model.acknowledge_close(); |
| 904 | } |
| 905 | if self.model.close == Close::Allowed { |
| 906 | ctx.send_viewport_cmd(egui::ViewportCommand::Close); |
| 907 | } |
| 908 | let title = self |
| 909 | .model |
| 910 | .archive |
| 911 | .as_ref() |
| 912 | .map(|path| { |
| 913 | let full = if path.is_absolute() { |
| 914 | path.clone() |
| 915 | } else { |
| 916 | std::env::current_dir().unwrap_or_default().join(path) |
| 917 | }; |
| 918 | format!("{} — Zipelo", full.display()) |
| 919 | }) |
| 920 | .unwrap_or_else(|| "Zipelo".into()); |
| 921 | let short_title = if let Some(path) = title.strip_suffix(" — Zipelo") { |
| 922 | format!( |
| 923 | "{} — Zipelo", |
| 924 | theme::window_title(root, path, ctx.content_rect().width() - 300.0) |
| 925 | ) |
| 926 | } else { |
| 927 | title.clone() |
| 928 | }; |
| 929 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(short_title)); |
| 930 | if self.custom_frame |
| 931 | && window_frame::title_bar( |
| 932 | root, |
| 933 | "main", |
| 934 | &title, |
| 935 | window_frame::Buttons { |
| 936 | minimize: true, |
| 937 | maximize: true, |
| 938 | }, |
| 939 | ) |
| 940 | { |
| 941 | self.release_after_move = true; |
| 942 | } |
| 943 | egui::Panel::top("archive-actions") |
| 944 | .frame( |
| 945 | theme::panel(theme::PANEL) |
| 946 | .corner_radius(0) |
| 947 | .inner_margin(egui::Margin { |
| 948 | left: 12, |
| 949 | right: 12, |
| 950 | // 버튼 안 내용은 보이는 모양 기준으로 가운데에 두므로 위아래를 같게 둠. |
| 951 | top: 4, |
| 952 | bottom: 4, |
| 953 | }), |
| 954 | ) |
| 955 | .show(root, |ui| { |
| 956 | ui.horizontal(|ui| { |
| 957 | ui.spacing_mut().item_spacing.x = 2.0; |
| 958 | let idle = !self.model.busy() |
| 959 | && self.model.close == Close::Open |
| 960 | && self.picker.is_none(); |
| 961 | if theme::tool(ui, "열기", theme::OPEN, idle, false) |
| 962 | .on_hover_text(&title) |
| 963 | .clicked() |
| 964 | { |
| 965 | self.open_picker(Purpose::Open); |
| 966 | } |
| 967 | if theme::tool( |
| 968 | ui, |
| 969 | "미리보기", |
| 970 | theme::PREVIEW, |
| 971 | idle && self.can_preview() && !self.preview_open, |
| 972 | false, |
| 973 | ) |
| 974 | .clicked() |
| 975 | { |
| 976 | self.begin_preview(ui.ctx()); |
| 977 | } |
| 978 | if theme::tool(ui, "압축하기", theme::CREATE, idle, false).clicked() { |
| 979 | self.choose_zip_folder(); |
| 980 | } |
| 981 | if theme::tool( |
| 982 | ui, |
| 983 | "압축해제", |
| 984 | theme::EXTRACT, |
| 985 | self.model.can_extract(), |
| 986 | true, |
| 987 | ) |
| 988 | .clicked() |
| 989 | { |
| 990 | self.open_picker(Purpose::Destination); |
| 991 | } |
| 992 | }); |
| 993 | }); |
| 994 | egui::Panel::bottom("archive-status") |
| 995 | .exact_size(theme::STATUS_HEIGHT) |
| 996 | .resizable(false) |
| 997 | .frame( |
| 998 | egui::Frame::new() |
| 999 | .fill(theme::STRIPE) |
| 1000 | .inner_margin(egui::Margin::symmetric(16, 0)), |
| 1001 | ) |
| 1002 | .show(root, |ui| { |
| 1003 | let rows = browser::rows(&self.model.entries, &self.folder, &self.search); |
| 1004 | let selected = rows |
| 1005 | .iter() |
| 1006 | .filter(|row| !row.ids.is_empty() && row.ids.is_subset(&self.model.selected)) |
| 1007 | .count(); |
| 1008 | let rect = ui.max_rect(); |
| 1009 | let half = (rect.width() - theme::SPACE) / 2.0; |
| 1010 | theme::status_text( |
| 1011 | ui, |
| 1012 | rect, |
| 1013 | &format!("{}개 항목", rows.len()), |
| 1014 | half, |
| 1015 | Align::Min, |
| 1016 | ); |
| 1017 | theme::status_text(ui, rect, &format!("선택 {selected}개"), half, Align::Max); |
| 1018 | }); |
| 1019 | if ctx.embed_viewports() && self.work_visible() && !self.limit_result_visible() { |
| 1020 | // 별도 창을 만들 수 없을 때만 쓰는 대체 배치. 목록을 가리지 않도록 하단에 둠. |
| 1021 | egui::Panel::bottom("archive-work") |
| 1022 | .frame(theme::panel(theme::PANEL).corner_radius(0)) |
| 1023 | .show(root, |ui| self.work_area(ui)); |
| 1024 | } |
| 1025 | egui::CentralPanel::default() |
| 1026 | .frame( |
| 1027 | theme::panel(theme::PANEL) |
| 1028 | .corner_radius(0) |
| 1029 | .inner_margin(egui::Margin::ZERO), |
| 1030 | ) |
| 1031 | .show(root, |ui| { |
| 1032 | if self.model.archive.is_some() { |
| 1033 | self.archive_list(ui); |
| 1034 | } else { |
| 1035 | self.empty(ui); |
| 1036 | } |
| 1037 | }); |
| 1038 | self.forget_password_history(&ctx); |
| 1039 | self.preview_dialog(&ctx); |
| 1040 | self.work_window(&ctx); |
| 1041 | self.limit_result_dialog(&ctx); |
| 1042 | self.create_window(&ctx); |
| 1043 | if self.model.challenge.is_some() { |
| 1044 | self.password_dialog(&ctx, new_challenge); |
| 1045 | } |
| 1046 | self.resource_dialog(&ctx); |
| 1047 | if self.model.conflict.is_some() && self.model.close == Close::Open { |
| 1048 | self.conflict_dialog(&ctx); |
| 1049 | } |
| 1050 | self.close_dialog(&ctx); |
| 1051 | self.picker_notice(&ctx); |
| 1052 | if !preview_was_open |
| 1053 | && !self.preview_open |
| 1054 | && !self.model.busy() |
| 1055 | && let Some(id) = self.preview_return_focus.take() |
| 1056 | { |
| 1057 | ctx.memory_mut(|m| m.request_focus(id)); |
| 1058 | } |
| 1059 | if self.custom_frame { |
| 1060 | window_frame::edges(&ctx, true); |
| 1061 | } |
| 1062 | if self.model.busy() { |
| 1063 | ctx.request_repaint_after(std::time::Duration::from_millis(30)); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // 선택 창이 열려 있는 동안 다른 조작을 막고, 응답을 받도록 화면을 계속 갱신함. |
| 1068 | fn picker_notice(&mut self, ctx: &egui::Context) { |
| 1069 | if self.picker.is_none() { |
| 1070 | return; |
| 1071 | } |
| 1072 | egui::Modal::new(egui::Id::unique("archive-picker")).show(ctx, |ui| { |
| 1073 | ui.set_width(360.0); |
| 1074 | ui.label("파일 선택 창에서 선택을 마쳐 주세요"); |
| 1075 | }); |
| 1076 | ctx.request_repaint_after(std::time::Duration::from_millis(100)); |
| 1077 | } |
| 1078 | |
| 1079 | fn empty(&mut self, ui: &mut egui::Ui) { |
| 1080 | let hovered = ui.ctx().input(|i| !i.raw.hovered_files.is_empty()); |
| 1081 | let width = (ui.available_width() * 0.60) |
| 1082 | .max(480.0) |
| 1083 | .min(ui.available_width()); |
| 1084 | let y = ((ui.available_height() - theme::DROP_HEIGHT - theme::SPACE * 8.0) / 2.0).max(0.0); |
| 1085 | ui.add_space(y); |
| 1086 | ui.vertical_centered(|ui| { |
| 1087 | let drop_frame = egui::Frame::new() |
| 1088 | .fill(if hovered { theme::DROP } else { theme::PANEL }) |
| 1089 | .corner_radius(theme::DROP_RADIUS) |
| 1090 | .inner_margin(egui::Margin::same(theme::DROP_PAD)) |
| 1091 | .show(ui, |ui| { |
| 1092 | ui.set_width(width - 2.0 * theme::DROP_PAD as f32); |
| 1093 | ui.set_min_height(theme::DROP_HEIGHT - 2.0 * theme::DROP_PAD as f32); |
| 1094 | ui.vertical_centered(|ui| { |
| 1095 | ui.add_space(theme::PAD as f32); |
| 1096 | theme::icon(ui, Icon::Archive, 48.0); |
| 1097 | ui.add_space(theme::PAD as f32); |
| 1098 | ui.label( |
| 1099 | RichText::new(if hovered { |
| 1100 | "놓으면 엽니다" |
| 1101 | } else { |
| 1102 | "압축파일을 여기에 끌어 놓으세요" |
| 1103 | }) |
| 1104 | .size(theme::FONT_LARGE) |
| 1105 | .strong(), |
| 1106 | ); |
| 1107 | ui.label("또는"); |
| 1108 | if theme::action(ui, "파일 열기", true, !self.model.busy()).clicked() { |
| 1109 | self.open_picker(Purpose::Open); |
| 1110 | } |
| 1111 | }); |
| 1112 | }); |
| 1113 | let rect = drop_frame.response.rect; |
| 1114 | if hovered { |
| 1115 | ui.painter().rect_stroke( |
| 1116 | rect, |
| 1117 | 12, |
| 1118 | egui::Stroke::new(2.0, theme::BLUE), |
| 1119 | egui::StrokeKind::Inside, |
| 1120 | ); |
| 1121 | } else { |
| 1122 | let stroke = egui::Stroke::new(2.0, theme::DASH); |
| 1123 | for (a, b) in [ |
| 1124 | (rect.left_top(), rect.right_top()), |
| 1125 | (rect.right_top(), rect.right_bottom()), |
| 1126 | (rect.right_bottom(), rect.left_bottom()), |
| 1127 | (rect.left_bottom(), rect.left_top()), |
| 1128 | ] { |
| 1129 | let direction = (b - a).normalized(); |
| 1130 | let mut distance = theme::DROP_RADIUS as f32; |
| 1131 | while distance < a.distance(b) - theme::DROP_RADIUS as f32 { |
| 1132 | ui.painter().line_segment( |
| 1133 | [ |
| 1134 | a + direction * distance, |
| 1135 | a + direction |
| 1136 | * (distance + theme::SPACE) |
| 1137 | .min(a.distance(b) - theme::DROP_RADIUS as f32), |
| 1138 | ], |
| 1139 | stroke, |
| 1140 | ); |
| 1141 | distance += theme::PAD as f32; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | ui.add_space(theme::PAD as f32); |
| 1146 | ui.colored_label( |
| 1147 | theme::MUTED, |
| 1148 | "ZIP, 7z, TAR, GZ, TAR.GZ, TAR.BZ2, TAR.XZ 파일을 열 수 있습니다", |
| 1149 | ); |
| 1150 | }); |
| 1151 | } |
| 1152 | |
| 1153 | fn archive_list(&mut self, ui: &mut egui::Ui) { |
| 1154 | // 경로 줄은 입력란 높이만 차지하고 창 너비 전체를 회색으로 채움. |
| 1155 | let path_row = egui::Frame::new() |
| 1156 | .fill(theme::STRIPE) |
| 1157 | .inner_margin(egui::Margin { |
| 1158 | left: 8, |
| 1159 | right: 12, |
| 1160 | top: 0, |
| 1161 | bottom: 0, |
| 1162 | }) |
| 1163 | .show(ui, |ui| { |
| 1164 | ui.set_min_width(ui.available_width()); |
| 1165 | ui.horizontal(|ui| { |
| 1166 | ui.set_height(theme::FIELD); |
| 1167 | // 간격은 아래에서 직접 넣어 위치가 흔들리지 않게 함. |
| 1168 | ui.spacing_mut().item_spacing.x = 0.0; |
| 1169 | let up = theme::flat_icon_button( |
| 1170 | ui, |
| 1171 | "위로", |
| 1172 | theme::UP, |
| 1173 | !self.folder.is_empty() && !self.model.busy(), |
| 1174 | egui::vec2(theme::FIELD, theme::FIELD), |
| 1175 | ); |
| 1176 | if up.clicked() { |
| 1177 | let parent = self |
| 1178 | .folder |
| 1179 | .rsplit_once('/') |
| 1180 | .map_or("", |(parent, _)| parent) |
| 1181 | .to_owned(); |
| 1182 | self.browse_folder(&parent); |
| 1183 | } |
| 1184 | ui.add_space(6.0); |
| 1185 | let path = format!("/{}", self.folder); |
| 1186 | let search_width = 240.0; |
| 1187 | let gap = 8.0; |
| 1188 | let path_width = (ui.available_width() - search_width - gap).max(0.0); |
| 1189 | // 두 입력란은 같은 높이의 칸을 받아 같은 세로 위치에 그림. |
| 1190 | let (path_rect, _) = ui.allocate_exact_size( |
| 1191 | egui::vec2(path_width, theme::FIELD), |
| 1192 | egui::Sense::hover(), |
| 1193 | ); |
| 1194 | theme::field_frame(ui, path_rect, false); |
| 1195 | // 부모 배치에 영향을 주지 않는 하위 영역에 경로 글자를 그림. |
| 1196 | let mut text_ui = ui.new_child( |
| 1197 | egui::UiBuilder::new() |
| 1198 | .max_rect(path_rect.shrink2(egui::vec2(10.0, 0.0))) |
| 1199 | .layout(Layout::left_to_right(Align::Center)), |
| 1200 | ); |
| 1201 | theme::bounded_text(&mut text_ui, &path, (path_width - 20.0).max(0.0)); |
| 1202 | ui.add_space(gap); |
| 1203 | let (search_rect, _) = ui.allocate_exact_size( |
| 1204 | egui::vec2(search_width, theme::FIELD), |
| 1205 | egui::Sense::hover(), |
| 1206 | ); |
| 1207 | let search_id = egui::Id::unique("archive-search"); |
| 1208 | let focused = ui.memory(|m| m.has_focus(search_id)); |
| 1209 | theme::field_frame(ui, search_rect, focused); |
| 1210 | let icon = egui::Rect::from_center_size( |
| 1211 | egui::pos2(search_rect.left() + 20.0, search_rect.center().y), |
| 1212 | egui::vec2(16.0, 16.0), |
| 1213 | ); |
| 1214 | theme::svg_icon(ui.painter(), icon, theme::SEARCH, theme::MUTED); |
| 1215 | let search = ui.put( |
| 1216 | egui::Rect::from_min_max( |
| 1217 | egui::pos2(icon.right() + 8.0, search_rect.top() + 1.0), |
| 1218 | egui::pos2(search_rect.right() - 8.0, search_rect.bottom() - 1.0), |
| 1219 | ), |
| 1220 | egui::TextEdit::singleline(&mut self.search) |
| 1221 | .id(search_id) |
| 1222 | .frame(egui::Frame::NONE) |
| 1223 | .margin(egui::Margin::ZERO) |
| 1224 | .align(egui::Align2::LEFT_CENTER) |
| 1225 | .hint_text("파일 검색"), |
| 1226 | ); |
| 1227 | if search.changed() { |
| 1228 | self.clear_list_selection(); |
| 1229 | } |
| 1230 | }); |
| 1231 | }) |
| 1232 | .response |
| 1233 | .rect; |
| 1234 | // 경로 줄 아래 한 줄이 목록과의 경계이자 입력란의 아래 테두리 역할을 함. |
| 1235 | ui.painter().hline( |
| 1236 | path_row.x_range(), |
| 1237 | path_row.bottom() - 0.5, |
| 1238 | egui::Stroke::new(1.0, theme::BORDER), |
| 1239 | ); |
| 1240 | let rows = browser::rows(&self.model.entries, &self.folder, &self.search); |
| 1241 | |
| 1242 | // 열 제목 줄 없이 바로 목록을 보여 줌. 이름과 크기는 내용으로 구분됨. |
| 1243 | let name_width = (ui.available_width() - 16.0 - 28.0 - 96.0 - 40.0).max(120.0); |
| 1244 | let active = !self.preview_open |
| 1245 | && !self.model.busy() |
| 1246 | && !self.create |
| 1247 | && self.model.challenge.is_none() |
| 1248 | && self.model.close == Close::Open; |
| 1249 | let list_id = egui::Id::unique("archive-list"); |
| 1250 | let background = ui.interact( |
| 1251 | ui.available_rect_before_wrap(), |
| 1252 | list_id, |
| 1253 | if active { |
| 1254 | egui::Sense::click_and_drag() |
| 1255 | } else { |
| 1256 | egui::Sense::hover() |
| 1257 | }, |
| 1258 | ); |
| 1259 | background |
| 1260 | .widget_info(|| egui::WidgetInfo::labeled(egui::Role::ListBox, active, "파일 목록")); |
| 1261 | if background.has_focus() { |
| 1262 | ui.ctx().memory_mut(|memory| { |
| 1263 | memory.set_focus_lock_filter( |
| 1264 | list_id, |
| 1265 | egui::EventFilter { |
| 1266 | vertical_arrows: true, |
| 1267 | escape: true, |
| 1268 | ..Default::default() |
| 1269 | }, |
| 1270 | ) |
| 1271 | }); |
| 1272 | } |
| 1273 | let mut focused = None; |
| 1274 | let mut open = None; |
| 1275 | let mut next_offset = None; |
| 1276 | let output = egui::ScrollArea::vertical() |
| 1277 | .id_salt("archive-rows") |
| 1278 | .auto_shrink([false, false]) |
| 1279 | .vertical_scroll_offset(self.list_offset) |
| 1280 | .show_viewport(ui, |ui, viewport| { |
| 1281 | let origin = ui.max_rect().min; |
| 1282 | let clip = ui.clip_rect(); |
| 1283 | // 행을 누르는 영역은 오른쪽 24px을 비워 둠. 목록이 길어도 그 빈 곳에서 |
| 1284 | // 끌어서 선택을 시작할 수 있음. 행 배경(선택, 마우스 올림)은 너비를 꽉 채움. |
| 1285 | let full_width = ui.available_width(); |
| 1286 | let width = (full_width - 24.0).max(0.0); |
| 1287 | ui.set_min_height((rows.len() as f32 * LIST_ROW).max(viewport.height())); |
| 1288 | let point = ui.input(|i| i.pointer.interact_pos()); |
| 1289 | if active && let Some(point) = point { |
| 1290 | let press = ui.input(|i| i.pointer.press_origin()).unwrap_or(point); |
| 1291 | let relative = press - origin; |
| 1292 | let in_row = relative.x >= 0.0 |
| 1293 | && relative.x <= width |
| 1294 | && relative.y >= 0.0 |
| 1295 | && relative.y < rows.len() as f32 * LIST_ROW; |
| 1296 | if clip.contains(press) && !in_row && ui.input(|i| i.pointer.primary_pressed()) |
| 1297 | { |
| 1298 | background.request_focus(); |
| 1299 | let ctrl = ui.input(|i| i.modifiers.ctrl || i.modifiers.command); |
| 1300 | self.marquee = Some(Marquee { |
| 1301 | origin: relative.to_pos2(), |
| 1302 | base: if ctrl { |
| 1303 | self.model.selected.clone() |
| 1304 | } else { |
| 1305 | BTreeSet::new() |
| 1306 | }, |
| 1307 | moved: false, |
| 1308 | }); |
| 1309 | if !ctrl { |
| 1310 | self.model.selected.clear(); |
| 1311 | self.list_anchor = None; |
| 1312 | self.list_cursor = None; |
| 1313 | } |
| 1314 | } |
| 1315 | if let Some(drag) = &mut self.marquee { |
| 1316 | let end = (point.clamp(clip.min, clip.max) - origin).to_pos2(); |
| 1317 | drag.moved |= drag.origin.distance(end) > 3.0; |
| 1318 | if drag.moved { |
| 1319 | let rect = egui::Rect::from_two_pos(drag.origin, end); |
| 1320 | self.model.selected = drag.base.clone(); |
| 1321 | if rect.left() < width && rect.right() > 0.0 { |
| 1322 | let first = (rect.top().max(0.0) / LIST_ROW).floor() as usize; |
| 1323 | let last = ((rect.bottom().max(0.0) / LIST_ROW).ceil() as usize) |
| 1324 | .min(rows.len()); |
| 1325 | for row in rows.iter().take(last).skip(first) { |
| 1326 | self.model.selected.extend(&row.ids); |
| 1327 | } |
| 1328 | if first < last { |
| 1329 | self.list_anchor = Some(first); |
| 1330 | self.list_cursor = Some(last - 1); |
| 1331 | } |
| 1332 | } |
| 1333 | let speed = if point.y > clip.bottom() - 24.0 { |
| 1334 | ((point.y - clip.bottom() + 24.0) / 24.0).clamp(0.0, 3.0) |
| 1335 | } else if point.y < clip.top() + 24.0 { |
| 1336 | -((clip.top() + 24.0 - point.y) / 24.0).clamp(0.0, 3.0) |
| 1337 | } else { |
| 1338 | 0.0 |
| 1339 | }; |
| 1340 | if ui.input(|i| i.pointer.primary_down()) && speed != 0.0 { |
| 1341 | let dt = ui.input(|i| i.stable_dt).clamp(0.001, 0.05); |
| 1342 | next_offset = Some((viewport.min.y + speed * 500.0 * dt).clamp( |
| 1343 | 0.0, |
| 1344 | (rows.len() as f32 * LIST_ROW - viewport.height()).max(0.0), |
| 1345 | )); |
| 1346 | ui.ctx().request_repaint(); |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | let first = (viewport.min.y / LIST_ROW).floor() as usize; |
| 1352 | let last = ((viewport.max.y / LIST_ROW).ceil() as usize + 1).min(rows.len()); |
| 1353 | for index in first.min(rows.len())..last { |
| 1354 | let row = &rows[index]; |
| 1355 | let rect = egui::Rect::from_min_size( |
| 1356 | origin + egui::vec2(0.0, index as f32 * LIST_ROW), |
| 1357 | egui::vec2(width, LIST_ROW), |
| 1358 | ); |
| 1359 | let selected = !row.ids.is_empty() && row.ids.is_subset(&self.model.selected); |
| 1360 | let response = ui |
| 1361 | .interact( |
| 1362 | rect, |
| 1363 | egui::Id::unique(("archive-row", &row.path)), |
| 1364 | if active { |
| 1365 | egui::Sense::click() |
| 1366 | } else { |
| 1367 | egui::Sense::hover() |
| 1368 | }, |
| 1369 | ) |
| 1370 | .on_hover_text(&row.name); |
| 1371 | theme::pointer(&response); |
| 1372 | response.widget_info(|| { |
| 1373 | egui::WidgetInfo::selected( |
| 1374 | egui::Role::ListBoxOption, |
| 1375 | active, |
| 1376 | selected, |
| 1377 | &row.name, |
| 1378 | ) |
| 1379 | }); |
| 1380 | if response.has_focus() { |
| 1381 | focused = Some(index); |
| 1382 | ui.ctx().memory_mut(|memory| { |
| 1383 | memory.set_focus_lock_filter( |
| 1384 | response.id, |
| 1385 | egui::EventFilter { |
| 1386 | vertical_arrows: true, |
| 1387 | escape: true, |
| 1388 | ..Default::default() |
| 1389 | }, |
| 1390 | ) |
| 1391 | }); |
| 1392 | } |
| 1393 | theme::row_background( |
| 1394 | ui, |
| 1395 | egui::Rect::from_min_size(rect.min, egui::vec2(full_width, LIST_ROW)), |
| 1396 | selected, |
| 1397 | response.hovered(), |
| 1398 | ); |
| 1399 | if response.clicked() { |
| 1400 | background.request_focus(); |
| 1401 | let modifiers = ui.input(|i| i.modifiers); |
| 1402 | self.select_row( |
| 1403 | &rows, |
| 1404 | index, |
| 1405 | modifiers.ctrl || modifiers.command, |
| 1406 | modifiers.shift, |
| 1407 | ); |
| 1408 | } |
| 1409 | if row.directory && response.double_clicked() { |
| 1410 | open = Some(row.path.clone()); |
| 1411 | } |
| 1412 | // 암호가 걸린 항목은 이름 앞에 *를 붙이고, 툴팁은 실제 이름을 보여 줌. |
| 1413 | let name = if row.encrypted { |
| 1414 | format!("*{}", row.name) |
| 1415 | } else { |
| 1416 | row.name.clone() |
| 1417 | }; |
| 1418 | Self::entry_cells( |
| 1419 | ui, |
| 1420 | rect, |
| 1421 | row.directory, |
| 1422 | &name, |
| 1423 | &row.name, |
| 1424 | if row.directory { |
| 1425 | "0 B".into() |
| 1426 | } else { |
| 1427 | row.bytes |
| 1428 | .map(present::bytes_text) |
| 1429 | .unwrap_or_else(|| "—".into()) |
| 1430 | }, |
| 1431 | name_width, |
| 1432 | ); |
| 1433 | if active |
| 1434 | && self.list_cursor == Some(index) |
| 1435 | && (response.has_focus() || background.has_focus()) |
| 1436 | { |
| 1437 | ui.painter().rect_stroke( |
| 1438 | egui::Rect::from_min_size(rect.min, egui::vec2(full_width, LIST_ROW)) |
| 1439 | .shrink(1.0), |
| 1440 | 0, |
| 1441 | egui::Stroke::new(1.0, theme::BLUE), |
| 1442 | egui::StrokeKind::Inside, |
| 1443 | ); |
| 1444 | } |
| 1445 | } |
| 1446 | if rows.is_empty() { |
| 1447 | ui.scope_builder(egui::UiBuilder::new().max_rect(clip), |ui| { |
| 1448 | ui.centered_and_justified(|ui| { |
| 1449 | ui.label(if self.search.is_empty() { |
| 1450 | "이 폴더는 비어 있습니다" |
| 1451 | } else { |
| 1452 | "일치하는 항목이 없습니다" |
| 1453 | }) |
| 1454 | }); |
| 1455 | }); |
| 1456 | } |
| 1457 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| 1458 | if let Some(rect) = self.scenario_marquee { |
| 1459 | ui.painter().with_clip_rect(clip).rect( |
| 1460 | rect.translate(origin.to_vec2()), |
| 1461 | 0, |
| 1462 | theme::BLUE.gamma_multiply(0.16), |
| 1463 | egui::Stroke::new(1.0, theme::BLUE), |
| 1464 | egui::StrokeKind::Inside, |
| 1465 | ); |
| 1466 | } |
| 1467 | if let Some(drag) = &self.marquee |
| 1468 | && drag.moved |
| 1469 | && let Some(point) = point |
| 1470 | { |
| 1471 | let rect = egui::Rect::from_two_pos( |
| 1472 | origin + drag.origin.to_vec2(), |
| 1473 | point.clamp(clip.min, clip.max), |
| 1474 | ); |
| 1475 | ui.painter().with_clip_rect(clip).rect( |
| 1476 | rect, |
| 1477 | 0, |
| 1478 | theme::BLUE.gamma_multiply(0.16), |
| 1479 | egui::Stroke::new(1.0, theme::BLUE), |
| 1480 | egui::StrokeKind::Inside, |
| 1481 | ); |
| 1482 | } |
| 1483 | }); |
| 1484 | self.list_offset = next_offset.unwrap_or(output.state.clamped_offset().y); |
| 1485 | if ui.input(|i| i.pointer.primary_released() || !i.pointer.primary_down()) { |
| 1486 | self.marquee = None; |
| 1487 | } |
| 1488 | let escape_from_list = self.list_focused |
| 1489 | && ui.ctx().memory(|memory| memory.focused()).is_none() |
| 1490 | && ui.input(|i| i.key_pressed(egui::Key::Escape)); |
| 1491 | self.list_focused = |
| 1492 | active && (background.has_focus() || focused.is_some() || escape_from_list); |
| 1493 | if self.list_focused { |
| 1494 | if let Some(index) = focused { |
| 1495 | self.list_cursor = Some(index); |
| 1496 | } |
| 1497 | let modifiers = ui.input(|i| i.modifiers); |
| 1498 | let ctrl = modifiers.ctrl || modifiers.command; |
| 1499 | if ui.input(|i| i.key_pressed(egui::Key::Escape)) { |
| 1500 | self.model.selected.clear(); |
| 1501 | self.marquee = None; |
| 1502 | background.request_focus(); |
| 1503 | } |
| 1504 | if ctrl && ui.input(|i| i.key_pressed(egui::Key::A)) { |
| 1505 | self.model.selected = rows |
| 1506 | .iter() |
| 1507 | .flat_map(|row| row.ids.iter().copied()) |
| 1508 | .collect(); |
| 1509 | self.list_cursor = (!rows.is_empty()).then_some(0); |
| 1510 | self.list_anchor = self.list_cursor; |
| 1511 | } |
| 1512 | if !rows.is_empty() { |
| 1513 | let direction = if ui.input(|i| i.key_pressed(egui::Key::ArrowDown)) { |
| 1514 | Some(1isize) |
| 1515 | } else if ui.input(|i| i.key_pressed(egui::Key::ArrowUp)) { |
| 1516 | Some(-1) |
| 1517 | } else { |
| 1518 | None |
| 1519 | }; |
| 1520 | if let Some(direction) = direction { |
| 1521 | let index = self.list_cursor.map_or(0, |index| { |
| 1522 | index.saturating_add_signed(direction).min(rows.len() - 1) |
| 1523 | }); |
| 1524 | if ctrl && !modifiers.shift { |
| 1525 | self.list_cursor = Some(index); |
| 1526 | } else { |
| 1527 | self.select_row(&rows, index, ctrl, modifiers.shift); |
| 1528 | } |
| 1529 | background.request_focus(); |
| 1530 | let top = index as f32 * LIST_ROW; |
| 1531 | if top < self.list_offset { |
| 1532 | self.list_offset = top; |
| 1533 | } else if top + LIST_ROW > self.list_offset + output.inner_rect.height() { |
| 1534 | self.list_offset = top + LIST_ROW - output.inner_rect.height(); |
| 1535 | } |
| 1536 | ui.ctx().request_repaint(); |
| 1537 | } |
| 1538 | if ui.input(|i| i.key_pressed(egui::Key::Enter)) { |
| 1539 | let index = self |
| 1540 | .list_cursor |
| 1541 | .or(focused) |
| 1542 | .unwrap_or(0) |
| 1543 | .min(rows.len() - 1); |
| 1544 | if rows[index].directory { |
| 1545 | open = Some(rows[index].path.clone()); |
| 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | if ui.input(|i| i.key_pressed(egui::Key::Backspace)) && !self.folder.is_empty() { |
| 1550 | open = Some( |
| 1551 | self.folder |
| 1552 | .rsplit_once('/') |
| 1553 | .map_or("", |(parent, _)| parent) |
| 1554 | .to_owned(), |
| 1555 | ); |
| 1556 | } |
| 1557 | } |
| 1558 | if let Some(folder) = open { |
| 1559 | self.browse_folder(&folder); |
| 1560 | background.request_focus(); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | fn clear_list_selection(&mut self) { |
| 1565 | self.model.selected.clear(); |
| 1566 | self.list_anchor = None; |
| 1567 | self.list_cursor = None; |
| 1568 | self.list_offset = 0.0; |
| 1569 | self.marquee = None; |
| 1570 | } |
| 1571 | |
| 1572 | fn select_row(&mut self, rows: &[browser::Row], index: usize, ctrl: bool, shift: bool) { |
| 1573 | if shift { |
| 1574 | let anchor = self |
| 1575 | .list_anchor |
| 1576 | .unwrap_or(self.list_cursor.unwrap_or(index)) |
| 1577 | .min(rows.len() - 1); |
| 1578 | if !ctrl { |
| 1579 | self.model.selected.clear(); |
| 1580 | } |
| 1581 | for row in &rows[anchor.min(index)..=anchor.max(index)] { |
| 1582 | self.model.selected.extend(&row.ids); |
| 1583 | } |
| 1584 | self.list_anchor = Some(anchor); |
| 1585 | } else { |
| 1586 | if !ctrl { |
| 1587 | self.model.selected.clear(); |
| 1588 | } |
| 1589 | if ctrl && rows[index].ids.is_subset(&self.model.selected) { |
| 1590 | // 폴더 이동 시 선택을 지우므로 현재 행의 ID만 제거함. |
| 1591 | self.model |
| 1592 | .selected |
| 1593 | .retain(|id| !rows[index].ids.contains(id)); |
| 1594 | } else { |
| 1595 | self.model.selected.extend(&rows[index].ids); |
| 1596 | } |
| 1597 | self.list_anchor = Some(index); |
| 1598 | } |
| 1599 | self.list_cursor = Some(index); |
| 1600 | } |
| 1601 | |
| 1602 | fn result_visible(&self) -> bool { |
| 1603 | !self.dismissed |
| 1604 | && !self |
| 1605 | .last_request |
| 1606 | .as_ref() |
| 1607 | .is_some_and(|r| matches!(r, Start::Preview { .. })) |
| 1608 | && !self.model.busy() |
| 1609 | && self.last_request.as_ref().is_some_and(|request| { |
| 1610 | !matches!(request, Start::List { .. }) |
| 1611 | || self.model.error.is_some() |
| 1612 | || self |
| 1613 | .model |
| 1614 | .terminal |
| 1615 | .as_ref() |
| 1616 | .is_some_and(|t| t.outcome != Outcome::Completed) |
| 1617 | }) |
| 1618 | } |
| 1619 | |
| 1620 | fn can_preview(&self) -> bool { |
| 1621 | self.model.can_preview() |
| 1622 | && browser::rows(&self.model.entries, &self.folder, &self.search) |
| 1623 | .iter() |
| 1624 | .any(|row| !row.directory && row.ids == self.model.selected) |
| 1625 | } |
| 1626 | |
| 1627 | fn begin_preview(&mut self, ctx: &egui::Context) { |
| 1628 | if !self.can_preview() || self.picker.is_some() { |
| 1629 | return; |
| 1630 | } |
| 1631 | self.preview_texture = None; |
| 1632 | self.preview_text.clear(); |
| 1633 | self.preview_name = self |
| 1634 | .model |
| 1635 | .entries |
| 1636 | .iter() |
| 1637 | .find(|e| self.model.selected.contains(&e.id)) |
| 1638 | .unwrap() |
| 1639 | .name |
| 1640 | .clone(); |
| 1641 | self.preview_return_focus = Some( |
| 1642 | ctx.memory(|m| m.focused()) |
| 1643 | .unwrap_or_else(|| egui::Id::unique("archive-list")), |
| 1644 | ); |
| 1645 | self.preview_initial_focus = true; |
| 1646 | let request = Start::Preview { |
| 1647 | archive: self.model.archive.clone().unwrap(), |
| 1648 | entry_id: *self.model.selected.first().unwrap(), |
| 1649 | list_id: self.model.list_id.clone().unwrap(), |
| 1650 | }; |
| 1651 | self.preview_open = true; |
| 1652 | self.start(request); |
| 1653 | } |
| 1654 | |
| 1655 | fn close_preview(&mut self) { |
| 1656 | self.preview_open = false; |
| 1657 | self.model.dismiss_preview(); |
| 1658 | self.preview_texture = None; |
| 1659 | self.preview_text.clear(); |
| 1660 | self.dismissed = true; |
| 1661 | } |
| 1662 | |
| 1663 | fn preview_dialog(&mut self, ctx: &egui::Context) { |
| 1664 | if !self.preview_open |
| 1665 | || self.model.challenge.is_some() |
| 1666 | || self.model.resource.is_some() |
| 1667 | || self.model.close != Close::Open |
| 1668 | { |
| 1669 | return; |
| 1670 | } |
| 1671 | if let Some(preview) = self.model.preview.as_mut() |
| 1672 | && (!preview.data.is_empty() || preview.info.total_bytes == 0) |
| 1673 | { |
| 1674 | match preview.info.format { |
| 1675 | PreviewFormat::Utf8Text => { |
| 1676 | self.preview_text = |
| 1677 | String::from_utf8(std::mem::take(&mut preview.data)).unwrap_or_default(); |
| 1678 | } |
| 1679 | PreviewFormat::Rgba8 => { |
| 1680 | let width = preview.info.width as usize; |
| 1681 | let height = preview.info.height as usize; |
| 1682 | let limit = ctx.input(|i| i.max_texture_side).max(1); |
| 1683 | let step = width.div_ceil(limit).max(height.div_ceil(limit)).max(1); |
| 1684 | let size = [width.div_ceil(step), height.div_ceil(step)]; |
| 1685 | let mut colors = Vec::with_capacity(size[0] * size[1]); |
| 1686 | for y in (0..height).step_by(step) { |
| 1687 | for x in (0..width).step_by(step) { |
| 1688 | let index = (y * width + x) * 4; |
| 1689 | colors.push(egui::Color32::from_rgba_unmultiplied( |
| 1690 | preview.data[index], |
| 1691 | preview.data[index + 1], |
| 1692 | preview.data[index + 2], |
| 1693 | preview.data[index + 3], |
| 1694 | )); |
| 1695 | } |
| 1696 | } |
| 1697 | let pixels = egui::ColorImage::new(size, colors); |
| 1698 | preview.data = Vec::new(); |
| 1699 | self.preview_texture = Some(ctx.load_texture( |
| 1700 | "archive-preview", |
| 1701 | pixels, |
| 1702 | egui::TextureOptions::LINEAR, |
| 1703 | )); |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | let mut close = false; |
| 1708 | let modal = egui::Modal::new(egui::Id::unique("archive-preview")).show(ctx, |ui| { |
| 1709 | ui.set_width((ctx.content_rect().width() - 64.0).clamp(240.0, 760.0)); |
| 1710 | ui.horizontal(|ui| { |
| 1711 | ui.heading("미리보기"); |
| 1712 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 1713 | let button = theme::flat_icon_button( |
| 1714 | ui, |
| 1715 | "미리보기 닫기", |
| 1716 | theme::CLOSE, |
| 1717 | true, |
| 1718 | egui::vec2(36.0, 36.0), |
| 1719 | ); |
| 1720 | if self.preview_initial_focus { |
| 1721 | button.request_focus(); |
| 1722 | self.preview_initial_focus = false; |
| 1723 | } |
| 1724 | close |= button.clicked(); |
| 1725 | }); |
| 1726 | }); |
| 1727 | ui.add(egui::Label::new(&self.preview_name).truncate()) |
| 1728 | .on_hover_text(&self.preview_name); |
| 1729 | let max_height = (ctx.content_rect().height() - 220.0).clamp(100.0, 560.0); |
| 1730 | if self.model.busy() { |
| 1731 | ui.label("미리보기를 불러오는 중입니다"); |
| 1732 | if ui.button("취소").clicked() { |
| 1733 | close = true; |
| 1734 | } |
| 1735 | } else if let Some(error) = self.model.error.as_ref() { |
| 1736 | Self::error_notice(ui, error); |
| 1737 | } else if let Some(texture) = self.preview_texture.as_ref() { |
| 1738 | ui.add( |
| 1739 | egui::Image::new(texture) |
| 1740 | .max_size(egui::vec2(ui.available_width(), max_height)) |
| 1741 | .shrink_to_fit(), |
| 1742 | ); |
| 1743 | } else if self.model.preview.is_some() { |
| 1744 | egui::ScrollArea::both() |
| 1745 | .id_salt("preview-text") |
| 1746 | .max_height(max_height) |
| 1747 | .show(ui, |ui| { |
| 1748 | let mut text = self.preview_text.as_str(); |
| 1749 | ui.add( |
| 1750 | egui::TextEdit::multiline(&mut text) |
| 1751 | .desired_width(ui.available_width()) |
| 1752 | .desired_rows(12) |
| 1753 | .frame(egui::Frame::NONE), |
| 1754 | ); |
| 1755 | }); |
| 1756 | if self.preview_text.is_empty() { |
| 1757 | ui.label("빈 텍스트 파일입니다"); |
| 1758 | } |
| 1759 | } else { |
| 1760 | let code = self.model.terminal.as_ref().and_then(|t| t.error); |
| 1761 | ui.label(match code { |
| 1762 | Some(ErrorCode::Limit) => "미리보기 크기 또는 처리 한도를 초과했습니다.", |
| 1763 | Some(ErrorCode::Integrity) => "파일이 손상됐거나 UTF-8 텍스트가 아닙니다.", |
| 1764 | Some(ErrorCode::UnsupportedFormat | ErrorCode::UnsupportedFeature) => { |
| 1765 | "이 파일 형식은 미리보기를 지원하지 않습니다." |
| 1766 | } |
| 1767 | Some(ErrorCode::SourceChanged) => "압축파일이 변경됐습니다. 다시 열어 주세요.", |
| 1768 | Some(ErrorCode::WrongPassword) => "암호가 올바르지 않습니다.", |
| 1769 | _ => "미리보기를 불러오지 못했습니다.", |
| 1770 | }); |
| 1771 | ui.label("닫은 뒤 파일을 선택하고 [압축해제]를 사용할 수 있습니다."); |
| 1772 | } |
| 1773 | }); |
| 1774 | if close || modal.should_close() { |
| 1775 | self.close_preview(); |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | fn limit_result_visible(&self) -> bool { |
| 1780 | self.result_visible() |
| 1781 | && self |
| 1782 | .model |
| 1783 | .terminal |
| 1784 | .as_ref() |
| 1785 | .is_some_and(|terminal| terminal.error == Some(ErrorCode::Limit)) |
| 1786 | } |
| 1787 | |
| 1788 | fn limit_result_dialog(&mut self, ctx: &egui::Context) { |
| 1789 | if !self.limit_result_visible() || self.main_modal_open() { |
| 1790 | return; |
| 1791 | } |
| 1792 | egui::Modal::new(egui::Id::new("archive-limit-result")).show(ctx, |ui| { |
| 1793 | ui.set_width((ctx.content_rect().width() - 64.0).min(WORK_WIDTH)); |
| 1794 | ui.heading("작업 결과"); |
| 1795 | self.work_area(ui); |
| 1796 | }); |
| 1797 | } |
| 1798 | |
| 1799 | fn error_notice(ui: &mut egui::Ui, error: &str) { |
| 1800 | let response = theme::panel(theme::ERROR_BG) |
| 1801 | .show(ui, |ui| { |
| 1802 | ui.horizontal_wrapped(|ui| { |
| 1803 | theme::icon(ui, Icon::Error, 24.0); |
| 1804 | ui.colored_label(theme::ERROR, error); |
| 1805 | }); |
| 1806 | }) |
| 1807 | .response; |
| 1808 | response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Alert, true, error)); |
| 1809 | } |
| 1810 | |
| 1811 | fn work_visible(&self) -> bool { |
| 1812 | !self |
| 1813 | .last_request |
| 1814 | .as_ref() |
| 1815 | .is_some_and(|r| matches!(r, Start::Preview { .. })) |
| 1816 | && (self.model.busy() |
| 1817 | || self.result_visible() |
| 1818 | || (self.ui_error.is_some() && !self.create)) |
| 1819 | } |
| 1820 | |
| 1821 | /// 메인 창 가운데에 모달(암호 입력, 종료 확인, 파일 선택 안내)이나 같은 자리에 뜨는 |
| 1822 | /// ZIP 만들기 창이 떠 있는지. 닫기가 정해진 뒤(Allowed)에도 결과 창을 다시 띄우지 않음. |
| 1823 | /// 모달이 늘어도 진행·결과 창 숨김 규칙은 이 한 곳만 보면 되도록 모음. |
| 1824 | fn main_modal_open(&self) -> bool { |
| 1825 | (self.create && !self.model.busy()) |
| 1826 | || self.picker.is_some() |
| 1827 | || self.model.challenge.is_some() |
| 1828 | || self.model.resource.is_some() |
| 1829 | || self.model.conflict.is_some() |
| 1830 | || self.model.close != Close::Open |
| 1831 | } |
| 1832 | |
| 1833 | fn work_window(&mut self, ctx: &egui::Context) { |
| 1834 | if !self.work_visible() { |
| 1835 | self.work = WorkWindow::default(); |
| 1836 | return; |
| 1837 | } |
| 1838 | if self.limit_result_visible() { |
| 1839 | self.work.open = false; |
| 1840 | return; |
| 1841 | } |
| 1842 | if ctx.embed_viewports() { |
| 1843 | // 별도 창을 만들 수 없는 환경(시험 등)은 show()에서 하단에 그림. |
| 1844 | return; |
| 1845 | } |
| 1846 | if self.main_modal_open() { |
| 1847 | // 메인 창 가운데에 뜨는 모달을 항상 위에 뜨는 진행·결과 창이 가리지 않게 |
| 1848 | // 그동안 창을 닫음. 부모 기준 위치는 기억해 두고 다시 엶. |
| 1849 | self.work.open = false; |
| 1850 | return; |
| 1851 | } |
| 1852 | let id = egui::ViewportId::from_hash_of("archive-work"); |
| 1853 | let title = if self.model.busy() { |
| 1854 | present::activity_text( |
| 1855 | &self.model, |
| 1856 | matches!(self.last_request, Some(Start::CreateZip { .. })), |
| 1857 | ) |
| 1858 | .to_owned() |
| 1859 | } else { |
| 1860 | "작업 결과".to_owned() |
| 1861 | }; |
| 1862 | let window_title = format!("{title} — Zipelo"); |
| 1863 | let custom_frame = self.custom_frame; |
| 1864 | let size = egui::vec2(WORK_WIDTH, self.work.height); |
| 1865 | let builder = self.work.builder( |
| 1866 | ctx, |
| 1867 | id, |
| 1868 | size, |
| 1869 | &window_title, |
| 1870 | !custom_frame, |
| 1871 | self.picker.is_some(), |
| 1872 | ); |
| 1873 | let height = ctx.show_viewport_immediate(id, builder, |ui, class| { |
| 1874 | if ui.ctx().input(|i| i.viewport().close_requested()) { |
| 1875 | if self.model.busy() { |
| 1876 | ui.ctx() |
| 1877 | .send_viewport_cmd(egui::ViewportCommand::CancelClose); |
| 1878 | } else { |
| 1879 | self.dismissed = true; |
| 1880 | self.ui_error = None; |
| 1881 | self.model.input_error = None; |
| 1882 | } |
| 1883 | } |
| 1884 | if class == egui::ViewportClass::EmbeddedWindow { |
| 1885 | ui.set_width(WORK_WIDTH); |
| 1886 | self.work_area(ui); |
| 1887 | return None; |
| 1888 | } |
| 1889 | let mut chrome = 0.0; |
| 1890 | if custom_frame { |
| 1891 | window_frame::title_bar( |
| 1892 | ui, |
| 1893 | "work", |
| 1894 | &window_title, |
| 1895 | window_frame::Buttons { |
| 1896 | minimize: false, |
| 1897 | maximize: false, |
| 1898 | }, |
| 1899 | ); |
| 1900 | window_frame::edges(ui.ctx(), false); |
| 1901 | chrome = window_frame::TITLE_HEIGHT; |
| 1902 | } |
| 1903 | let margin = theme::PAD as f32; |
| 1904 | egui::CentralPanel::default() |
| 1905 | .frame(theme::panel(theme::PANEL).corner_radius(0)) |
| 1906 | .show(ui, |ui| { |
| 1907 | let content = ui.vertical(|ui| self.work_area(ui)).response.rect; |
| 1908 | Some(content.height() + 2.0 * margin + chrome) |
| 1909 | }) |
| 1910 | .inner |
| 1911 | }); |
| 1912 | if let Some(height) = height { |
| 1913 | let height = height.clamp(WORK_MIN_HEIGHT, WORK_MAX_HEIGHT).ceil(); |
| 1914 | if (height - self.work.height).abs() > 1.0 { |
| 1915 | self.work.height = height; |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | fn work_area(&mut self, ui: &mut egui::Ui) { |
| 1921 | // 선택 창 안내 모달은 메인 창만 막으므로, 별도 진행 창의 버튼은 여기서 막음. |
| 1922 | let enabled = self.picker.is_none(); |
| 1923 | ui.add_enabled_ui(enabled, |ui| self.work_contents(ui)); |
| 1924 | } |
| 1925 | |
| 1926 | fn work_contents(&mut self, ui: &mut egui::Ui) { |
| 1927 | if self.model.busy() { |
| 1928 | self.progress(ui); |
| 1929 | } else if self.result_visible() { |
| 1930 | self.result(ui); |
| 1931 | } |
| 1932 | if !self.create |
| 1933 | && let Some(error) = &self.ui_error |
| 1934 | { |
| 1935 | Self::error_notice(ui, error); |
| 1936 | if error == present::FOLDER_ERROR |
| 1937 | && theme::action(ui, "폴더 바꾸기", false, true).clicked() |
| 1938 | { |
| 1939 | self.open_picker(Purpose::Destination); |
| 1940 | } |
| 1941 | } |
| 1942 | if let Some(error) = &self.model.input_error { |
| 1943 | ui.colored_label( |
| 1944 | theme::ERROR, |
| 1945 | if error.starts_with("암호는") { |
| 1946 | error.as_str() |
| 1947 | } else { |
| 1948 | if matches!(self.last_request, Some(Start::CreateZip { .. })) { |
| 1949 | "저장 위치와 추가한 항목을 확인하세요" |
| 1950 | } else { |
| 1951 | "풀 위치와 선택한 항목을 확인하세요" |
| 1952 | } |
| 1953 | }, |
| 1954 | ); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | fn progress(&mut self, ui: &mut egui::Ui) { |
| 1959 | if self.model.resource.is_some() { |
| 1960 | ui.horizontal(|ui| { |
| 1961 | ui.label("사용자 허용을 기다리고 있습니다"); |
| 1962 | self.cancel_action(ui, false); |
| 1963 | }); |
| 1964 | return; |
| 1965 | } |
| 1966 | let creating = matches!(self.last_request, Some(Start::CreateZip { .. })); |
| 1967 | let cancelling = self.model.cancellation_requested() || self.model.cancel.is_some(); |
| 1968 | let title = present::activity_text(&self.model, creating); |
| 1969 | ui.strong(title); |
| 1970 | if let Some((stage, done, total, count)) = self.model.progress { |
| 1971 | // 원본을 읽는 단계에서는 아직 처리한 항목이 없으므로 개수를 표시하지 않음. |
| 1972 | let processed = if stage == Stage::Read { |
| 1973 | String::new() |
| 1974 | } else { |
| 1975 | format!("{count}개 처리함, ") |
| 1976 | }; |
| 1977 | if let Some(total) = total.filter(|n| *n > 0) { |
| 1978 | ui.scope(|ui| { |
| 1979 | ui.visuals_mut().extreme_bg_color = theme::BORDER; |
| 1980 | ui.add( |
| 1981 | egui::ProgressBar::new((done as f64 / total as f64).clamp(0.0, 1.0) as f32) |
| 1982 | .fill(theme::BLUE) |
| 1983 | .desired_height(theme::SPACE * 2.0), |
| 1984 | ); |
| 1985 | }); |
| 1986 | ui.horizontal(|ui| { |
| 1987 | ui.label(format!( |
| 1988 | "{:.0}% / {processed}{} / {}", |
| 1989 | (done as f64 / total as f64).clamp(0.0, 1.0) * 100.0, |
| 1990 | present::bytes_text(done), |
| 1991 | present::bytes_text(total) |
| 1992 | )); |
| 1993 | self.cancel_action(ui, cancelling); |
| 1994 | }); |
| 1995 | } else { |
| 1996 | theme::indeterminate(ui); |
| 1997 | ui.horizontal(|ui| { |
| 1998 | ui.label(format!("{processed}{}", present::bytes_text(done))); |
| 1999 | self.cancel_action(ui, cancelling); |
| 2000 | }); |
| 2001 | } |
| 2002 | } else { |
| 2003 | theme::indeterminate(ui); |
| 2004 | ui.horizontal(|ui| { |
| 2005 | ui.label("작업을 진행하고 있습니다"); |
| 2006 | self.cancel_action(ui, cancelling); |
| 2007 | }); |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | fn cancel_action(&mut self, ui: &mut egui::Ui, cancelling: bool) { |
| 2012 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 2013 | if theme::action(ui, "취소", false, !cancelling).clicked() { |
| 2014 | self.clear_password(); |
| 2015 | self.model.cancel(); |
| 2016 | } |
| 2017 | }); |
| 2018 | } |
| 2019 | |
| 2020 | fn result(&mut self, ui: &mut egui::Ui) { |
| 2021 | let t = self.model.terminal.clone(); |
| 2022 | let listing = matches!(self.last_request, Some(Start::List { .. })); |
| 2023 | let creating = matches!(self.last_request, Some(Start::CreateZip { .. })); |
| 2024 | let resolved_targets = present::resolved_targets( |
| 2025 | self.targets.as_ref(), |
| 2026 | &self.model.entries, |
| 2027 | matches!( |
| 2028 | self.last_request, |
| 2029 | Some(Start::Extract { |
| 2030 | gzip_output_name: Some(_), |
| 2031 | .. |
| 2032 | }) |
| 2033 | ) |
| 2034 | .then_some(self.gzip_name.as_str()), |
| 2035 | &self.model.resolutions, |
| 2036 | ); |
| 2037 | let skipped = self |
| 2038 | .model |
| 2039 | .resolutions |
| 2040 | .iter() |
| 2041 | .filter(|r| r.choice == ConflictChoice::Skip) |
| 2042 | .count(); |
| 2043 | let created_names = self |
| 2044 | .model |
| 2045 | .outputs |
| 2046 | .iter() |
| 2047 | .filter(|(_, disposition)| *disposition == Disposition::Created) |
| 2048 | .map(|(name, _)| name.replace('\\', "/")) |
| 2049 | .collect::<BTreeSet<_>>(); |
| 2050 | let warnings = if listing { |
| 2051 | Vec::new() |
| 2052 | } else { |
| 2053 | present::publication_warnings( |
| 2054 | t.as_ref(), |
| 2055 | self.model.confirmed, |
| 2056 | resolved_targets.as_ref(), |
| 2057 | &self.directories, |
| 2058 | &self.model.outputs, |
| 2059 | ) |
| 2060 | }; |
| 2061 | let success = self.model.confirmed |
| 2062 | && self.model.error.is_none() |
| 2063 | && t.as_ref().is_some_and(|t| t.outcome == Outcome::Completed) |
| 2064 | && warnings.is_empty(); |
| 2065 | let durability_warning = t |
| 2066 | .as_ref() |
| 2067 | .filter(|_| self.model.confirmed && !listing) |
| 2068 | .and_then(|t| present::durability_warning(t.durability)); |
| 2069 | let durability_in_details = success |
| 2070 | && t.as_ref() |
| 2071 | .is_some_and(|t| t.durability == Durability::Unconfirmed); |
| 2072 | let cancelled = |
| 2073 | self.model.confirmed && t.as_ref().is_some_and(|t| t.outcome == Outcome::Cancelled); |
| 2074 | let title = if success { |
| 2075 | if creating { |
| 2076 | "ZIP 파일을 만들었습니다" |
| 2077 | } else if skipped > 0 && t.as_ref().is_some_and(|t| t.created_count == 0) { |
| 2078 | "모든 파일을 건너뛰었습니다" |
| 2079 | } else { |
| 2080 | "압축을 풀었습니다" |
| 2081 | } |
| 2082 | } else if cancelled { |
| 2083 | "작업을 취소했습니다" |
| 2084 | } else if listing { |
| 2085 | "압축파일을 열지 못했습니다" |
| 2086 | } else if creating { |
| 2087 | "ZIP 파일을 만들지 못했습니다" |
| 2088 | } else { |
| 2089 | "압축을 풀지 못했습니다" |
| 2090 | }; |
| 2091 | let fill = if success { |
| 2092 | theme::SUCCESS_BG |
| 2093 | } else if cancelled { |
| 2094 | theme::STRIPE |
| 2095 | } else { |
| 2096 | theme::ERROR_BG |
| 2097 | }; |
| 2098 | let mut choose = false; |
| 2099 | let mut retry = false; |
| 2100 | let mut expand = false; |
| 2101 | let mut reopen = false; |
| 2102 | let mut folder = false; |
| 2103 | theme::panel(fill).show(ui, |ui| { |
| 2104 | ui.horizontal(|ui| { |
| 2105 | theme::icon( |
| 2106 | ui, |
| 2107 | if success { |
| 2108 | Icon::Success |
| 2109 | } else if cancelled { |
| 2110 | Icon::Warning |
| 2111 | } else { |
| 2112 | Icon::Error |
| 2113 | }, |
| 2114 | 24.0, |
| 2115 | ); |
| 2116 | ui.strong(title); |
| 2117 | }); |
| 2118 | let max_height = WORK_RESULT_MAX_HEIGHT; |
| 2119 | egui::ScrollArea::vertical() |
| 2120 | .id_salt("result-message") |
| 2121 | .max_height(max_height) |
| 2122 | .show(ui, |ui| { |
| 2123 | if success && t.as_ref().is_some_and(|t| t.created_count > 0) { |
| 2124 | let path = match self.last_request.as_ref() { |
| 2125 | Some(Start::CreateZip { output_file, .. }) => { |
| 2126 | output_file.display().to_string() |
| 2127 | } |
| 2128 | _ => format!("{} 폴더에 저장했습니다.", self.destination.display()), |
| 2129 | }; |
| 2130 | theme::bounded_text(ui, &path, ui.available_width()); |
| 2131 | } else if let Some(code) = t.as_ref().and_then(|t| t.error) { |
| 2132 | ui.label(if creating && code == ErrorCode::SourceChanged { |
| 2133 | "묶을 파일이 바뀌었습니다. 파일을 확인한 뒤 다시 만드세요".to_owned() |
| 2134 | } else { |
| 2135 | present::operation_text(present::error_text(code), creating) |
| 2136 | }); |
| 2137 | } else if let Some(error) = &self.model.error { |
| 2138 | ui.label(present::operation_text( |
| 2139 | present::client_error_text(error), |
| 2140 | creating, |
| 2141 | )); |
| 2142 | } |
| 2143 | if skipped > 0 { |
| 2144 | ui.label(format!("건너뛴 파일 {skipped}개")); |
| 2145 | } |
| 2146 | for resolution in self.model.resolutions.iter().filter(|r| { |
| 2147 | r.choice == ConflictChoice::Rename |
| 2148 | && created_names.contains(&r.relative_name.replace('\\', "/")) |
| 2149 | }) { |
| 2150 | theme::bounded_text( |
| 2151 | ui, |
| 2152 | &format!("이름 바꿔 저장: {}", resolution.relative_name), |
| 2153 | ui.available_width(), |
| 2154 | ); |
| 2155 | } |
| 2156 | for warning in warnings |
| 2157 | .iter() |
| 2158 | .copied() |
| 2159 | .chain(durability_warning.filter(|_| !durability_in_details)) |
| 2160 | { |
| 2161 | ui.horizontal_wrapped(|ui| { |
| 2162 | theme::icon(ui, Icon::Warning, 20.0); |
| 2163 | ui.colored_label( |
| 2164 | theme::WARNING, |
| 2165 | present::operation_text(warning, creating), |
| 2166 | ); |
| 2167 | }); |
| 2168 | } |
| 2169 | if !success |
| 2170 | && !listing |
| 2171 | && present::definitely_unsaved( |
| 2172 | t.as_ref(), |
| 2173 | self.model.confirmed, |
| 2174 | &self.model.outputs, |
| 2175 | ) |
| 2176 | { |
| 2177 | ui.label(if creating { |
| 2178 | "ZIP 파일을 만들지 않았습니다" |
| 2179 | } else { |
| 2180 | "아무 파일도 풀지 않았습니다" |
| 2181 | }); |
| 2182 | } |
| 2183 | if t.as_ref().is_some_and(|t| { |
| 2184 | t.error == Some(ErrorCode::Limit) |
| 2185 | && (t.created_count > 0 |
| 2186 | || t.retained_partial_count > 0 |
| 2187 | || t.publication != Publication::None) |
| 2188 | }) { |
| 2189 | ui.label(format!( |
| 2190 | "{}개 파일이 저장되었거나 일부 남아 있습니다. 저장 위치를 확인하세요.", |
| 2191 | t.as_ref().unwrap().created_count |
| 2192 | )); |
| 2193 | theme::bounded_text( |
| 2194 | ui, |
| 2195 | &self.destination.display().to_string(), |
| 2196 | ui.available_width(), |
| 2197 | ); |
| 2198 | } |
| 2199 | if self.details { |
| 2200 | theme::panel(theme::STRIPE).show(ui, |ui| { |
| 2201 | if durability_in_details { |
| 2202 | if let Some(warning) = durability_warning { |
| 2203 | ui.label(present::operation_text(warning, creating)); |
| 2204 | } |
| 2205 | } |
| 2206 | if let Some(t) = &t { |
| 2207 | for line in terminal_lines(t) { |
| 2208 | ui.label(line); |
| 2209 | } |
| 2210 | } |
| 2211 | if let Some(error) = &self.model.error { |
| 2212 | ui.label(error); |
| 2213 | } |
| 2214 | for (name, state) in &self.model.outputs { |
| 2215 | theme::bounded_text( |
| 2216 | ui, |
| 2217 | &format!("{name}: {}", disposition_label(*state)), |
| 2218 | ui.available_width(), |
| 2219 | ); |
| 2220 | } |
| 2221 | }); |
| 2222 | } |
| 2223 | }); |
| 2224 | let conflict = t |
| 2225 | .as_ref() |
| 2226 | .is_some_and(|t| t.error == Some(ErrorCode::Conflict)) |
| 2227 | && !creating; |
| 2228 | let uncertain = warnings.contains(&present::UNCERTAIN); |
| 2229 | if conflict && uncertain { |
| 2230 | let response = ui.checkbox( |
| 2231 | &mut self.checked_destination, |
| 2232 | "기존 풀 위치 폴더를 확인했습니다", |
| 2233 | ); |
| 2234 | theme::pointer(&response); |
| 2235 | theme::focus(&response); |
| 2236 | } |
| 2237 | ui.horizontal(|ui| { |
| 2238 | if theme::action( |
| 2239 | ui, |
| 2240 | if self.details { |
| 2241 | "자세히 닫기" |
| 2242 | } else { |
| 2243 | "자세히 보기" |
| 2244 | }, |
| 2245 | false, |
| 2246 | true, |
| 2247 | ) |
| 2248 | .clicked() |
| 2249 | { |
| 2250 | self.details = !self.details; |
| 2251 | } |
| 2252 | let action = if success { |
| 2253 | Some(("폴더 열기", true)) |
| 2254 | } else if conflict { |
| 2255 | Some(( |
| 2256 | if self |
| 2257 | .model |
| 2258 | .list_id |
| 2259 | .as_ref() |
| 2260 | .is_some_and(|id| id.format == Format::Gzip) |
| 2261 | { |
| 2262 | "다른 이름으로 저장" |
| 2263 | } else { |
| 2264 | "다른 폴더에 풀기" |
| 2265 | }, |
| 2266 | !uncertain || self.checked_destination, |
| 2267 | )) |
| 2268 | } else if t |
| 2269 | .as_ref() |
| 2270 | .is_some_and(|t| t.error == Some(ErrorCode::SourceChanged)) |
| 2271 | { |
| 2272 | Some(( |
| 2273 | if creating { |
| 2274 | "다시 만들기" |
| 2275 | } else { |
| 2276 | "압축파일 다시 열기" |
| 2277 | }, |
| 2278 | true, |
| 2279 | )) |
| 2280 | } else if self.model.can_retry_expanded() { |
| 2281 | None |
| 2282 | } else if t |
| 2283 | .as_ref() |
| 2284 | .is_some_and(|t| t.error == Some(ErrorCode::Limit)) |
| 2285 | { |
| 2286 | None |
| 2287 | } else if !cancelled && (!uncertain || listing) { |
| 2288 | Some(("다시 시도", true)) |
| 2289 | } else { |
| 2290 | None |
| 2291 | }; |
| 2292 | let (confirm, primary) = theme::action_pair(ui, "확인", action); |
| 2293 | if confirm { |
| 2294 | self.dismissed = true; |
| 2295 | self.ui_error = None; |
| 2296 | self.model.input_error = None; |
| 2297 | } |
| 2298 | if primary { |
| 2299 | match action.map(|(label, _)| label) { |
| 2300 | Some("폴더 열기") => folder = true, |
| 2301 | Some("다른 폴더에 풀기" | "다른 이름으로 저장") => { |
| 2302 | choose = true |
| 2303 | } |
| 2304 | Some("압축파일 다시 열기") => reopen = true, |
| 2305 | Some("다시 시도" | "다시 만들기") => retry = true, |
| 2306 | _ => {} |
| 2307 | } |
| 2308 | } |
| 2309 | }); |
| 2310 | if self.model.can_retry_expanded() { |
| 2311 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 2312 | expand = theme::action(ui, "한도를 늘려 다시 시도", true, true).clicked(); |
| 2313 | }); |
| 2314 | } |
| 2315 | }); |
| 2316 | if choose { |
| 2317 | self.open_picker(Purpose::Retry); |
| 2318 | } |
| 2319 | if retry { |
| 2320 | self.retry(); |
| 2321 | } |
| 2322 | if expand { |
| 2323 | let ready = if matches!(self.last_request, Some(Start::Extract { .. })) { |
| 2324 | match present::prepare_destination(&self.destination, self.automatic_destination) { |
| 2325 | Ok(created) => { |
| 2326 | self.owned_destination = created.then(|| self.destination.clone()); |
| 2327 | true |
| 2328 | } |
| 2329 | Err(_) => { |
| 2330 | self.ui_error = Some(present::FOLDER_ERROR.into()); |
| 2331 | false |
| 2332 | } |
| 2333 | } |
| 2334 | } else { |
| 2335 | true |
| 2336 | }; |
| 2337 | if ready && self.model.retry_expanded(self.factory.as_mut()) { |
| 2338 | self.dismissed = false; |
| 2339 | self.details = false; |
| 2340 | self.ui_error = None; |
| 2341 | } |
| 2342 | } |
| 2343 | if reopen { |
| 2344 | if let Some(path) = self.model.archive.clone() { |
| 2345 | self.open_path(path); |
| 2346 | } |
| 2347 | } |
| 2348 | if folder { |
| 2349 | let path = match self.last_request.as_ref() { |
| 2350 | Some(Start::CreateZip { output_file, .. }) => { |
| 2351 | output_file.parent().unwrap_or(Path::new("/")) |
| 2352 | } |
| 2353 | _ => self.destination.as_path(), |
| 2354 | }; |
| 2355 | match present::folder_command(path).spawn() { |
| 2356 | Ok(mut child) => { |
| 2357 | std::thread::spawn(move || { |
| 2358 | let _ = child.wait(); |
| 2359 | }); |
| 2360 | } |
| 2361 | Err(_) => { |
| 2362 | self.ui_error = Some( |
| 2363 | "폴더를 열지 못했습니다. 파일 탐색기에서 저장 위치를 확인하세요".into(), |
| 2364 | ) |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | /// 파일 목록 한 행의 아이콘, 이름, 크기 칸. 메인 목록과 ZIP 만들기 창이 같이 씀. |
| 2371 | fn entry_cells( |
| 2372 | ui: &mut egui::Ui, |
| 2373 | rect: egui::Rect, |
| 2374 | directory: bool, |
| 2375 | name: &str, |
| 2376 | hover: &str, |
| 2377 | size: String, |
| 2378 | name_width: f32, |
| 2379 | ) { |
| 2380 | ui.scope_builder( |
| 2381 | egui::UiBuilder::new() |
| 2382 | .max_rect(rect.shrink2(egui::vec2(0.0, 2.0))) |
| 2383 | .layout(Layout::left_to_right(Align::Center)), |
| 2384 | |ui| { |
| 2385 | ui.style_mut().interaction.selectable_labels = false; |
| 2386 | ui.add_space(16.0); |
| 2387 | theme::icon(ui, if directory { Icon::Folder } else { Icon::File }, 20.0); |
| 2388 | theme::bounded_text_hover(ui, name, hover, name_width); |
| 2389 | theme::column_label(ui, size, 96.0, Align::Max); |
| 2390 | }, |
| 2391 | ); |
| 2392 | } |
| 2393 | |
| 2394 | /// ZIP 만들기를 진행 창처럼 부모 창을 따라가는 별도 창으로 띄움. |
| 2395 | /// 별도 창을 만들 수 없는 환경(시험 등)에서는 메인 창 안의 모달로 띄움. |
| 2396 | fn create_window(&mut self, ctx: &egui::Context) { |
| 2397 | if !self.create || self.model.busy() { |
| 2398 | self.create_win = WorkWindow::default(); |
| 2399 | return; |
| 2400 | } |
| 2401 | if ctx.embed_viewports() { |
| 2402 | let mut close = false; |
| 2403 | let modal = egui::Modal::new(egui::Id::unique("create-zip")).show(ctx, |ui| { |
| 2404 | ui.set_width((ctx.content_rect().width() - 64.0).min(CREATE_WIDTH)); |
| 2405 | ui.horizontal(|ui| { |
| 2406 | ui.heading("ZIP 만들기"); |
| 2407 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 2408 | let button = theme::icon_button(ui, "닫기", theme::CLOSE, true); |
| 2409 | close = button.clicked(); |
| 2410 | }); |
| 2411 | }); |
| 2412 | close |= self.create_area(ui); |
| 2413 | ui.add_space(theme::SPACE); |
| 2414 | close |= self.create_actions(ui); |
| 2415 | }); |
| 2416 | if close || modal.should_close() { |
| 2417 | self.close_create(); |
| 2418 | } |
| 2419 | return; |
| 2420 | } |
| 2421 | let id = egui::ViewportId::from_hash_of("archive-create"); |
| 2422 | let title = "ZIP 만들기 — Zipelo"; |
| 2423 | let custom_frame = self.custom_frame; |
| 2424 | if !self.create_win.open { |
| 2425 | self.create_win.height = self.create_win.height.max(CREATE_MIN_HEIGHT); |
| 2426 | } |
| 2427 | let size = egui::vec2(CREATE_WIDTH, self.create_win.height); |
| 2428 | let builder = |
| 2429 | self.create_win |
| 2430 | .builder(ctx, id, size, title, !custom_frame, self.picker.is_some()); |
| 2431 | let mut close = false; |
| 2432 | let height = ctx.show_viewport_immediate(id, builder, |ui, class| { |
| 2433 | if ui.ctx().input(|i| i.viewport().close_requested()) { |
| 2434 | close = true; |
| 2435 | } |
| 2436 | if class == egui::ViewportClass::EmbeddedWindow { |
| 2437 | ui.set_width(CREATE_WIDTH); |
| 2438 | close |= self.create_area(ui); |
| 2439 | close |= self.create_actions(ui); |
| 2440 | return None; |
| 2441 | } |
| 2442 | let mut chrome = 0.0; |
| 2443 | if custom_frame { |
| 2444 | window_frame::title_bar( |
| 2445 | ui, |
| 2446 | "create", |
| 2447 | title, |
| 2448 | window_frame::Buttons { |
| 2449 | minimize: false, |
| 2450 | maximize: false, |
| 2451 | }, |
| 2452 | ); |
| 2453 | window_frame::edges(ui.ctx(), false); |
| 2454 | chrome = window_frame::TITLE_HEIGHT; |
| 2455 | } |
| 2456 | // 버튼은 창 오른쪽 아래에 여백을 두고 고정함. 위 내용이 바뀌어도 자리가 같음. |
| 2457 | let actions = egui::Panel::bottom(egui::Id::unique("create-actions")) |
| 2458 | .resizable(false) |
| 2459 | .show_separator_line(false) |
| 2460 | .frame( |
| 2461 | egui::Frame::new() |
| 2462 | .fill(theme::PANEL) |
| 2463 | .inner_margin(egui::Margin { |
| 2464 | left: theme::PAD, |
| 2465 | right: theme::PAD, |
| 2466 | top: 0, |
| 2467 | bottom: theme::PAD, |
| 2468 | }), |
| 2469 | ) |
| 2470 | .show(ui, |ui| close |= self.create_actions(ui)) |
| 2471 | .response |
| 2472 | .rect |
| 2473 | .height(); |
| 2474 | let margin = theme::PAD as f32; |
| 2475 | egui::CentralPanel::default() |
| 2476 | .frame(theme::panel(theme::PANEL).corner_radius(0)) |
| 2477 | .show(ui, |ui| { |
| 2478 | let content = ui |
| 2479 | .vertical(|ui| close |= self.create_area(ui)) |
| 2480 | .response |
| 2481 | .rect; |
| 2482 | Some(content.height() + 2.0 * margin + chrome + actions) |
| 2483 | }) |
| 2484 | .inner |
| 2485 | }); |
| 2486 | if let Some(height) = height { |
| 2487 | let height = height.clamp(CREATE_MIN_HEIGHT, 900.0).ceil(); |
| 2488 | if (height - self.create_win.height).abs() > 1.0 { |
| 2489 | self.create_win.height = height; |
| 2490 | } |
| 2491 | } |
| 2492 | if close { |
| 2493 | self.close_create(); |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | /// ZIP 만들기 내용. 닫기를 눌렀으면 true를 돌려줌. |
| 2498 | fn create_area(&mut self, ui: &mut egui::Ui) -> bool { |
| 2499 | // 저장 위치 선택 창이 응답을 기다리는 동안에는 버튼을 막음. |
| 2500 | let enabled = self.picker.is_none(); |
| 2501 | ui.add_enabled_ui(enabled, |ui| self.create_contents(ui)) |
| 2502 | .inner |
| 2503 | } |
| 2504 | |
| 2505 | fn create_contents(&mut self, ui: &mut egui::Ui) -> bool { |
| 2506 | let rows = self.create_rows(); |
| 2507 | let root = self.create_root(); |
| 2508 | |
| 2509 | // 경로 줄: 메인 화면처럼 위로 버튼과 현재 폴더 경로. |
| 2510 | ui.horizontal(|ui| { |
| 2511 | ui.set_height(theme::FIELD); |
| 2512 | ui.spacing_mut().item_spacing.x = 0.0; |
| 2513 | let up = theme::flat_icon_button( |
| 2514 | ui, |
| 2515 | "위로", |
| 2516 | theme::UP, |
| 2517 | !self.create_folder.is_empty(), |
| 2518 | egui::vec2(theme::FIELD, theme::FIELD), |
| 2519 | ); |
| 2520 | if up.clicked() { |
| 2521 | self.create_folder = self |
| 2522 | .create_folder |
| 2523 | .rsplit_once('/') |
| 2524 | .map_or("", |(parent, _)| parent) |
| 2525 | .to_owned(); |
| 2526 | self.create_selected = None; |
| 2527 | } |
| 2528 | ui.add_space(6.0); |
| 2529 | let path = match &root { |
| 2530 | Some(root) => { |
| 2531 | let name = root.file_name().unwrap_or_default().to_string_lossy(); |
| 2532 | if self.create_folder.is_empty() { |
| 2533 | format!("/{name}") |
| 2534 | } else { |
| 2535 | format!("/{name}/{}", self.create_folder) |
| 2536 | } |
| 2537 | } |
| 2538 | None => "/".to_owned(), |
| 2539 | }; |
| 2540 | let (rect, _) = ui.allocate_exact_size( |
| 2541 | egui::vec2(ui.available_width(), theme::FIELD), |
| 2542 | egui::Sense::hover(), |
| 2543 | ); |
| 2544 | theme::boxed_field(ui, rect); |
| 2545 | theme::field_text(ui, rect, &path); |
| 2546 | }); |
| 2547 | ui.add_space(theme::SPACE); |
| 2548 | |
| 2549 | // 파일 목록: 메인 화면 목록과 같은 행 모양. 폴더를 두 번 누르면 들어감. |
| 2550 | let mut enter = None; |
| 2551 | egui::Frame::new() |
| 2552 | .stroke(egui::Stroke::new(1.0, theme::BORDER)) |
| 2553 | .show(ui, |ui| { |
| 2554 | ui.set_min_size(egui::vec2(ui.available_width(), CREATE_LIST_HEIGHT)); |
| 2555 | ui.set_max_height(CREATE_LIST_HEIGHT); |
| 2556 | let message = match &rows { |
| 2557 | None => Some("폴더를 읽지 못했습니다"), |
| 2558 | Some(rows) if rows.is_empty() => Some("이 폴더는 비어 있습니다"), |
| 2559 | Some(_) => None, |
| 2560 | }; |
| 2561 | if let Some(message) = message { |
| 2562 | ui.centered_and_justified(|ui| ui.label(message)); |
| 2563 | return; |
| 2564 | } |
| 2565 | let rows = rows.unwrap(); |
| 2566 | let name_width = (ui.available_width() - 16.0 - 28.0 - 96.0 - 24.0).max(80.0); |
| 2567 | egui::ScrollArea::vertical() |
| 2568 | .id_salt("create-rows") |
| 2569 | .auto_shrink([false, false]) |
| 2570 | .max_height(CREATE_LIST_HEIGHT) |
| 2571 | .show_rows(ui, LIST_ROW, rows.len(), |ui, range| { |
| 2572 | for row in &rows[range] { |
| 2573 | let (rect, response) = ui.allocate_exact_size( |
| 2574 | egui::vec2(ui.available_width(), LIST_ROW), |
| 2575 | egui::Sense::click(), |
| 2576 | ); |
| 2577 | let response = response.on_hover_text(&row.name); |
| 2578 | theme::pointer(&response); |
| 2579 | response.widget_info(|| { |
| 2580 | egui::WidgetInfo::labeled( |
| 2581 | egui::Role::ListBoxOption, |
| 2582 | true, |
| 2583 | &row.name, |
| 2584 | ) |
| 2585 | }); |
| 2586 | if response.clicked() { |
| 2587 | self.create_selected = Some(row.name.clone()); |
| 2588 | } |
| 2589 | theme::row_background( |
| 2590 | ui, |
| 2591 | rect, |
| 2592 | self.create_selected.as_deref() == Some(row.name.as_str()), |
| 2593 | response.hovered(), |
| 2594 | ); |
| 2595 | if row.directory && root.is_some() && response.clicked() { |
| 2596 | // egui가 한 프레임을 다시 그리면 두 번 누름 정보가 사라질 수 있어 |
| 2597 | // 같은 폴더를 다른 프레임에서 0.4초 안에 다시 누르면 두 번 누름으로 봄. |
| 2598 | let frame = ui.ctx().cumulative_frame_nr(); |
| 2599 | let time = ui.input(|i| i.time); |
| 2600 | match &self.create_last_click { |
| 2601 | Some((name, last_frame, last_time)) |
| 2602 | if name == &row.name |
| 2603 | && *last_frame != frame |
| 2604 | && time - last_time < 0.4 => |
| 2605 | { |
| 2606 | enter = Some(row.name.clone()); |
| 2607 | self.create_last_click = None; |
| 2608 | } |
| 2609 | Some((name, last_frame, _)) |
| 2610 | if name == &row.name && *last_frame == frame => {} |
| 2611 | _ => { |
| 2612 | self.create_last_click = |
| 2613 | Some((row.name.clone(), frame, time)); |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | Self::entry_cells( |
| 2618 | ui, |
| 2619 | rect, |
| 2620 | row.directory, |
| 2621 | &row.name, |
| 2622 | &row.name, |
| 2623 | row.bytes.map(present::bytes_text).unwrap_or_default(), |
| 2624 | name_width, |
| 2625 | ); |
| 2626 | } |
| 2627 | }); |
| 2628 | }); |
| 2629 | if let Some(name) = enter { |
| 2630 | self.create_folder = if self.create_folder.is_empty() { |
| 2631 | name |
| 2632 | } else { |
| 2633 | format!("{}/{name}", self.create_folder) |
| 2634 | }; |
| 2635 | self.create_selected = None; |
| 2636 | } |
| 2637 | ui.add_space(theme::SPACE); |
| 2638 | |
| 2639 | if let Some(error) = &self.ui_error { |
| 2640 | Self::error_notice(ui, error); |
| 2641 | } |
| 2642 | |
| 2643 | // 암호: 체크하면 바로 아래에 암호와 암호 확인 칸을 보여 줌. |
| 2644 | let check = ui.checkbox(&mut self.aes256, "암호로 보호하기"); |
| 2645 | theme::pointer(&check); |
| 2646 | theme::focus(&check); |
| 2647 | if self.aes256 { |
| 2648 | let label_width = 72.0; |
| 2649 | ui.horizontal(|ui| { |
| 2650 | ui.add_sized([label_width, theme::FIELD], egui::Label::new("암호")); |
| 2651 | let toggle = if self.zip_password_visible { |
| 2652 | "숨기기" |
| 2653 | } else { |
| 2654 | "보기" |
| 2655 | }; |
| 2656 | let toggle_width = 72.0; |
| 2657 | let edit = ui.add_sized( |
| 2658 | [ |
| 2659 | (ui.available_width() - toggle_width - theme::SPACE).max(80.0), |
| 2660 | theme::FIELD, |
| 2661 | ], |
| 2662 | egui::TextEdit::singleline(&mut self.zip_password) |
| 2663 | .password(!self.zip_password_visible) |
| 2664 | .id(egui::Id::unique("zip-password-input")) |
| 2665 | .char_limit(1024) |
| 2666 | .align(egui::Align2::LEFT_CENTER), |
| 2667 | ); |
| 2668 | theme::focus(&edit); |
| 2669 | if theme::action(ui, toggle, false, true).clicked() { |
| 2670 | self.zip_password_visible = !self.zip_password_visible; |
| 2671 | } |
| 2672 | }); |
| 2673 | ui.horizontal(|ui| { |
| 2674 | ui.add_sized([label_width, theme::FIELD], egui::Label::new("암호 확인")); |
| 2675 | let edit = ui.add_sized( |
| 2676 | [ |
| 2677 | (ui.available_width() - 72.0 - theme::SPACE).max(80.0), |
| 2678 | theme::FIELD, |
| 2679 | ], |
| 2680 | egui::TextEdit::singleline(&mut self.zip_password_confirm) |
| 2681 | .password(!self.zip_password_visible) |
| 2682 | .id(egui::Id::unique("zip-password-confirm")) |
| 2683 | .char_limit(1024) |
| 2684 | .align(egui::Align2::LEFT_CENTER), |
| 2685 | ); |
| 2686 | theme::focus(&edit); |
| 2687 | }); |
| 2688 | if self.zip_password.len() > 1024 { |
| 2689 | ui.colored_label(theme::ERROR, "암호는 UTF-8 1~1024바이트로 입력하세요"); |
| 2690 | } else if !self.zip_password_confirm.is_empty() |
| 2691 | && self.zip_password != self.zip_password_confirm |
| 2692 | { |
| 2693 | ui.colored_label(theme::ERROR, "두 암호가 같지 않습니다"); |
| 2694 | } |
| 2695 | ui.label(RichText::new("암호를 잊으면 파일을 열 수 없습니다.").color(theme::MUTED)); |
| 2696 | } else if check.changed() |
| 2697 | || !self.zip_password.is_empty() |
| 2698 | || !self.zip_password_confirm.is_empty() |
| 2699 | { |
| 2700 | // 체크를 해제하면 입력한 암호와 그 실행 취소 기록을 남겨 두지 않음. |
| 2701 | // 입력했다가 모두 지운 경우도 기록에는 남아 있으므로 해제한 순간 함께 지움. |
| 2702 | self.clear_zip_password(); |
| 2703 | } |
| 2704 | false |
| 2705 | } |
| 2706 | |
| 2707 | /// 취소와 압축하기 버튼. 취소를 눌렀으면 true. |
| 2708 | fn create_actions(&mut self, ui: &mut egui::Ui) -> bool { |
| 2709 | let ready = !self.sources.is_empty() && self.zip_password_ready(); |
| 2710 | let enabled = self.picker.is_none(); |
| 2711 | let (cancel, compress) = ui |
| 2712 | .add_enabled_ui(enabled, |ui| { |
| 2713 | theme::action_pair(ui, "취소", Some(("압축하기", ready))) |
| 2714 | }) |
| 2715 | .inner; |
| 2716 | if compress { |
| 2717 | self.open_picker(Purpose::SaveZip); |
| 2718 | } |
| 2719 | cancel |
| 2720 | } |
| 2721 | |
| 2722 | fn resource_dialog(&mut self, ctx: &egui::Context) { |
| 2723 | let Some(question) = self.model.resource.clone() else { |
| 2724 | return; |
| 2725 | }; |
| 2726 | let Some(job) = self.model.job_id() else { |
| 2727 | return; |
| 2728 | }; |
| 2729 | let mut allow = false; |
| 2730 | let mut cancel = false; |
| 2731 | let modal = egui::Modal::new(egui::Id::unique(("archive-resource", job, question.id))) |
| 2732 | .show(ctx, |ui| { |
| 2733 | ui.set_width((ctx.content_rect().width() - 64.0).min(460.0)); |
| 2734 | ui.heading(if question.scope.ratio { |
| 2735 | "압축률이 매우 높습니다" |
| 2736 | } else { |
| 2737 | "큰 작업을 허용할까요?" |
| 2738 | }); |
| 2739 | ui.label(match question.action { |
| 2740 | ActionKind::List => "압축파일 목록 열기", |
| 2741 | ActionKind::Extract => "압축 해제", |
| 2742 | ActionKind::CreateZip => "ZIP 만들기", |
| 2743 | ActionKind::Preview => "파일 미리보기", |
| 2744 | }); |
| 2745 | if question.scope.ratio { |
| 2746 | ui.label("작은 압축파일도 많은 저장 공간을 사용할 수 있습니다."); |
| 2747 | } |
| 2748 | if question.scope.output { |
| 2749 | ui.label(format!( |
| 2750 | "총량 한도: {}에서 {}로", |
| 2751 | present::bytes_text(question.soft_limits.output), |
| 2752 | present::bytes_text(question.ceiling.output) |
| 2753 | )); |
| 2754 | } |
| 2755 | if question.scope.entries { |
| 2756 | ui.label(format!( |
| 2757 | "항목 한도: {}개에서 {}개로", |
| 2758 | question.soft_limits.entries, question.ceiling.entries |
| 2759 | )); |
| 2760 | } |
| 2761 | let mut u = question.usage; |
| 2762 | let estimated = self.model.estimated_usage(); |
| 2763 | u.estimated_bytes = u.estimated_bytes.or(estimated.estimated_bytes); |
| 2764 | u.estimated_entries = u.estimated_entries.or(estimated.estimated_entries); |
| 2765 | let bytes = |value: Option<u64>| { |
| 2766 | value |
| 2767 | .map(present::bytes_text) |
| 2768 | .unwrap_or_else(|| "미확인".into()) |
| 2769 | }; |
| 2770 | let entries = |value: Option<u32>| { |
| 2771 | value |
| 2772 | .map(|n| format!("{n}개")) |
| 2773 | .unwrap_or_else(|| "미확인".into()) |
| 2774 | }; |
| 2775 | ui.label(format!( |
| 2776 | "예상 총량: {} / 실제 총량: {}", |
| 2777 | bytes(u.estimated_bytes), |
| 2778 | bytes(u.actual_bytes) |
| 2779 | )); |
| 2780 | ui.label(format!( |
| 2781 | "예상 항목: {} / 실제 항목: {}", |
| 2782 | entries(u.estimated_entries), |
| 2783 | entries(u.actual_entries) |
| 2784 | )); |
| 2785 | if question.scope.ratio { |
| 2786 | ui.label(format!( |
| 2787 | "압축 크기: {} / 풀린 크기: {}", |
| 2788 | bytes(u.compressed_bytes), |
| 2789 | bytes(u.ratio_output_bytes) |
| 2790 | )); |
| 2791 | } |
| 2792 | ui.label(format!( |
| 2793 | "확인한 저장 볼륨의 여유 공간: {}", |
| 2794 | bytes(u.available_bytes) |
| 2795 | )); |
| 2796 | ui.label(format!( |
| 2797 | "이 작업의 상한: {} / {}개", |
| 2798 | present::bytes_text(question.ceiling.output), |
| 2799 | question.ceiling.entries |
| 2800 | )); |
| 2801 | ui.label("이 작업에만 적용합니다. 공간 부족이나 자원 상한에 도달하면 중단합니다."); |
| 2802 | ui.horizontal(|ui| { |
| 2803 | cancel = theme::action(ui, "취소", false, true).clicked(); |
| 2804 | allow = theme::action(ui, "이 작업 허용", true, true).clicked(); |
| 2805 | }); |
| 2806 | }); |
| 2807 | if cancel || modal.should_close() { |
| 2808 | self.clear_password(); |
| 2809 | self.model.cancel(); |
| 2810 | } else if allow { |
| 2811 | self.model.resource_reply(job, question.id, true); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | fn conflict_dialog(&mut self, ctx: &egui::Context) { |
| 2816 | let conflict = self.model.conflict.clone().unwrap(); |
| 2817 | let question = (self.model.job_id().unwrap_or(0), conflict.challenge_id); |
| 2818 | if self.conflict_challenge != Some(question) { |
| 2819 | self.conflict_challenge = Some(question); |
| 2820 | self.conflict_apply = false; |
| 2821 | } |
| 2822 | let mut choice = None; |
| 2823 | let mut cancel = false; |
| 2824 | let modal = egui::Modal::new(egui::Id::unique("archive-conflict")).show(ctx, |ui| { |
| 2825 | ui.set_width((ctx.content_rect().width() - 64.0).min(460.0)); |
| 2826 | ui.heading("같은 이름의 파일이 있습니다"); |
| 2827 | theme::bounded_text(ui, &conflict.relative_name, ui.available_width()); |
| 2828 | ui.add_space(theme::SPACE); |
| 2829 | egui::Grid::new("conflict-sizes") |
| 2830 | .spacing([24.0, 8.0]) |
| 2831 | .show(ui, |ui| { |
| 2832 | ui.label("기존 파일"); |
| 2833 | ui.label(present::bytes_text(conflict.existing_size)); |
| 2834 | ui.end_row(); |
| 2835 | ui.label("압축파일 안의 파일"); |
| 2836 | ui.label(present::bytes_text(conflict.incoming_size)); |
| 2837 | ui.end_row(); |
| 2838 | }); |
| 2839 | ui.add_space(theme::SPACE); |
| 2840 | ui.checkbox(&mut self.conflict_apply, "나머지 파일에도 같게 적용"); |
| 2841 | ui.add_space(theme::SPACE); |
| 2842 | ui.horizontal_wrapped(|ui| { |
| 2843 | for (label, value) in [ |
| 2844 | ("덮어쓰기", ConflictChoice::Overwrite), |
| 2845 | ("건너뛰기", ConflictChoice::Skip), |
| 2846 | ("이름 바꿔 저장", ConflictChoice::Rename), |
| 2847 | ] { |
| 2848 | if theme::action(ui, label, false, self.model.error.is_none()).clicked() { |
| 2849 | choice = Some(value); |
| 2850 | } |
| 2851 | } |
| 2852 | }); |
| 2853 | ui.label("이름 바꿔 저장하면 같은 폴더에 번호를 붙입니다."); |
| 2854 | if let Some(error) = &self.model.error { |
| 2855 | Self::error_notice(ui, present::client_error_text(error)); |
| 2856 | } |
| 2857 | cancel = theme::action(ui, "작업 취소", false, true).clicked(); |
| 2858 | }); |
| 2859 | if modal.should_close() || cancel { |
| 2860 | self.model.cancel(); |
| 2861 | } else if let Some(choice) = choice { |
| 2862 | self.model.resolve_conflict(choice, self.conflict_apply); |
| 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | fn password_dialog(&mut self, ctx: &egui::Context, new_challenge: bool) { |
| 2867 | let (_, attempt, encrypt) = self.model.challenge.unwrap(); |
| 2868 | let mut submit = false; |
| 2869 | let mut cancel = false; |
| 2870 | let password_enter = ctx.memory(|memory| memory.focused()).is_some_and(|id| { |
| 2871 | id == egui::Id::unique("archive-password-input") |
| 2872 | || id == egui::Id::unique("archive-password-confirm") |
| 2873 | }) && ctx.input(|input| input.key_pressed(egui::Key::Enter)); |
| 2874 | let modal = egui::Modal::new(egui::Id::unique("archive-password")).show(ctx, |ui| { |
| 2875 | ui.set_width((ctx.content_rect().width() - 64.0).min(420.0)); |
| 2876 | ui.heading(if encrypt { |
| 2877 | "ZIP 암호 설정" |
| 2878 | } else { |
| 2879 | "암호가 필요합니다" |
| 2880 | }); |
| 2881 | if encrypt { |
| 2882 | ui.label("암호를 잊으면 파일을 열 수 없습니다."); |
| 2883 | } else if let Some(path) = &self.model.archive { |
| 2884 | ui.label(format!( |
| 2885 | "{}은 암호로 보호되어 있습니다.", |
| 2886 | present::middle_text( |
| 2887 | &path.file_name().unwrap_or_default().to_string_lossy(), |
| 2888 | 24 |
| 2889 | ) |
| 2890 | )); |
| 2891 | } |
| 2892 | ui.horizontal(|ui| { |
| 2893 | ui.label("암호"); |
| 2894 | let edit = ui.add( |
| 2895 | egui::TextEdit::singleline(&mut self.password) |
| 2896 | .password(!self.password_visible) |
| 2897 | .id(egui::Id::unique("archive-password-input")) |
| 2898 | .char_limit(1024) |
| 2899 | .desired_width(232.0), |
| 2900 | ); |
| 2901 | theme::focus(&edit); |
| 2902 | if new_challenge { |
| 2903 | edit.request_focus(); |
| 2904 | } |
| 2905 | if theme::action( |
| 2906 | ui, |
| 2907 | if self.password_visible { |
| 2908 | "숨기기" |
| 2909 | } else { |
| 2910 | "보기" |
| 2911 | }, |
| 2912 | false, |
| 2913 | true, |
| 2914 | ) |
| 2915 | .clicked() |
| 2916 | { |
| 2917 | self.password_visible = !self.password_visible; |
| 2918 | } |
| 2919 | }); |
| 2920 | if encrypt { |
| 2921 | ui.horizontal(|ui| { |
| 2922 | ui.label("암호 확인"); |
| 2923 | let edit = ui.add( |
| 2924 | egui::TextEdit::singleline(&mut self.password_confirm) |
| 2925 | .password(!self.password_visible) |
| 2926 | .id(egui::Id::unique("archive-password-confirm")) |
| 2927 | .char_limit(1024) |
| 2928 | .desired_width(232.0), |
| 2929 | ); |
| 2930 | theme::focus(&edit); |
| 2931 | }); |
| 2932 | if self.password != self.password_confirm { |
| 2933 | ui.colored_label(theme::ERROR, "두 암호가 같지 않습니다"); |
| 2934 | } |
| 2935 | } else if attempt > 1 { |
| 2936 | ui.colored_label(theme::ERROR, "암호가 맞지 않습니다. 다시 입력하세요"); |
| 2937 | } |
| 2938 | if self.password.len() > 1024 { |
| 2939 | ui.colored_label(theme::ERROR, "암호는 UTF-8 1~1024바이트로 입력하세요"); |
| 2940 | } |
| 2941 | let valid = !self.password.is_empty() |
| 2942 | && self.password.len() <= 1024 |
| 2943 | && (!encrypt || self.password == self.password_confirm); |
| 2944 | let actions = theme::action_pair(ui, "취소", Some(("확인", valid))); |
| 2945 | cancel = actions.0; |
| 2946 | submit = actions.1 || (valid && password_enter); |
| 2947 | }); |
| 2948 | if modal.should_close() || cancel { |
| 2949 | self.clear_password(); |
| 2950 | self.model.cancel(); |
| 2951 | } else if submit { |
| 2952 | let password = std::mem::take(&mut self.password); |
| 2953 | self.clear_password(); |
| 2954 | self.model.password(password); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | fn close_dialog(&mut self, ctx: &egui::Context) { |
| 2959 | // Review(결과 확인 후 닫기)는 show()에서 바로 확인 처리하므로 그리지 않음. |
| 2960 | if matches!( |
| 2961 | self.model.close, |
| 2962 | Close::Open | Close::Allowed | Close::Review |
| 2963 | ) { |
| 2964 | return; |
| 2965 | } |
| 2966 | egui::Modal::new(egui::Id::unique("archive-close")).show(ctx, |ui| { |
| 2967 | ui.set_width(420.0); |
| 2968 | match self.model.close { |
| 2969 | Close::Confirm => { |
| 2970 | // 답하기 전에 작업이 끝났으면 취소할 작업이 없으므로 닫을지만 물음. |
| 2971 | let busy = self.model.busy(); |
| 2972 | let (heading, text, keep, close) = if busy { |
| 2973 | ( |
| 2974 | "작업 중 닫기", |
| 2975 | "작업을 취소하고 닫을까요?", |
| 2976 | "계속 작업", |
| 2977 | "취소하고 닫기", |
| 2978 | ) |
| 2979 | } else { |
| 2980 | ( |
| 2981 | "닫기", |
| 2982 | "작업이 끝났습니다. 창을 닫을까요?", |
| 2983 | "열어 두기", |
| 2984 | "닫기", |
| 2985 | ) |
| 2986 | }; |
| 2987 | ui.heading(heading); |
| 2988 | ui.label(text); |
| 2989 | ui.horizontal(|ui| { |
| 2990 | if theme::action(ui, keep, false, true).clicked() { |
| 2991 | self.model.keep_open(); |
| 2992 | } |
| 2993 | if theme::action(ui, close, true, true).clicked() { |
| 2994 | self.clear_password(); |
| 2995 | self.model.confirm_close(); |
| 2996 | } |
| 2997 | }); |
| 2998 | } |
| 2999 | Close::Waiting => { |
| 3000 | ui.heading("종료 확인 중"); |
| 3001 | ui.label("마무리하는 중입니다. 잠시만 기다려 주세요"); |
| 3002 | if theme::action(ui, "닫기 취소", false, true).clicked() { |
| 3003 | self.model.keep_open(); |
| 3004 | } |
| 3005 | } |
| 3006 | _ => {} |
| 3007 | } |
| 3008 | }); |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | impl eframe::App for ArchiveApp { |
| 3013 | fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) { |
| 3014 | self.show(root); |
| 3015 | } |
| 3016 | |
| 3017 | fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { |
| 3018 | // 운영체제가 창 이동을 맡으면 버튼 놓기가 오지 않으므로 대신 넣어 눌림 상태를 풀어 줌. |
| 3019 | if std::mem::take(&mut self.release_after_move) { |
| 3020 | let pos = ctx.input(|i| i.pointer.latest_pos()).unwrap_or_default(); |
| 3021 | raw_input.events.insert( |
| 3022 | 0, |
| 3023 | egui::Event::PointerButton { |
| 3024 | pos, |
| 3025 | button: egui::PointerButton::Primary, |
| 3026 | pressed: false, |
| 3027 | modifiers: ctx.input(|i| i.modifiers), |
| 3028 | }, |
| 3029 | ); |
| 3030 | } |
| 3031 | } |
| 3032 | } |
| 3033 | impl Drop for ArchiveApp { |
| 3034 | fn drop(&mut self) { |
| 3035 | self.clear_password(); |
| 3036 | } |
| 3037 | } |