| 1 | use crate::{ |
| 2 | archive_browser as browser, |
| 3 | archive_dialog::{ |
| 4 | DIALOG_UNAVAILABLE, DialogAnswer, DialogRequest, Dialogs, NativeDialogs, PendingDialog, |
| 5 | Purpose, |
| 6 | }, |
| 7 | archive_present as present, |
| 8 | archive_theme::{self as theme, Icon}, |
| 9 | archive_ui::*, |
| 10 | window_frame, |
| 11 | }; |
| 12 | use eframe::egui::{self, Align, Layout, RichText}; |
| 13 | use std::{ |
| 14 | collections::BTreeSet, |
| 15 | path::{Path, PathBuf}, |
| 16 | }; |
| 17 | |
| 18 | const WORK_WIDTH: f32 = 480.0; |
| 19 | const WORK_MIN_HEIGHT: f32 = 96.0; |
| 20 | const WORK_MAX_HEIGHT: f32 = 520.0; |
| 21 | const WORK_RESULT_MAX_HEIGHT: f32 = 220.0; |
| 22 | |
| 23 | /// 작업 진행·결과를 보여주는 자식 창의 위치 상태. |
| 24 | /// 부모 창 기준 상대 위치를 기억해 두고, 부모 창이 움직이면 같은 상대 위치로 따라감. |
| 25 | struct WorkWindow { |
| 26 | open: bool, |
| 27 | initial: Option<egui::Pos2>, |
| 28 | offset: Option<egui::Vec2>, |
| 29 | parent: Option<egui::Pos2>, |
| 30 | expected: Option<egui::Pos2>, |
| 31 | settle_until: f64, |
| 32 | height: f32, |
| 33 | } |
| 34 | |
| 35 | impl Default for WorkWindow { |
| 36 | fn default() -> Self { |
| 37 | Self { |
| 38 | open: false, |
| 39 | initial: None, |
| 40 | offset: None, |
| 41 | parent: None, |
| 42 | expected: None, |
| 43 | settle_until: 0.0, |
| 44 | height: 168.0, |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | struct Marquee { |
| 50 | // 스크롤 중에도 시작점을 목록 좌표로 유지함. |
| 51 | origin: egui::Pos2, |
| 52 | base: BTreeSet<u32>, |
| 53 | moved: bool, |
| 54 | } |
| 55 | |
| 56 | const LIST_ROW: f32 = 44.0; |
| 57 | |
| 58 | pub struct ArchiveApp { |
| 59 | pub model: Model, |
| 60 | factory: Box<dyn Factory>, |
| 61 | destination: PathBuf, |
| 62 | automatic_destination: bool, |
| 63 | owned_destination: Option<PathBuf>, |
| 64 | gzip_name: String, |
| 65 | sources: Vec<PathBuf>, |
| 66 | zip_folder: PathBuf, |
| 67 | zip_name: String, |
| 68 | password: String, |
| 69 | password_confirm: String, |
| 70 | password_visible: bool, |
| 71 | password_challenge: Option<u64>, |
| 72 | aes256: bool, |
| 73 | create: bool, |
| 74 | selected_only: bool, |
| 75 | method: Method, |
| 76 | dialogs: Box<dyn Dialogs>, |
| 77 | picker: Option<(Purpose, std::sync::mpsc::Receiver<DialogAnswer>)>, |
| 78 | work: WorkWindow, |
| 79 | custom_frame: bool, |
| 80 | release_after_move: bool, |
| 81 | folder: String, |
| 82 | search: String, |
| 83 | list_anchor: Option<usize>, |
| 84 | list_cursor: Option<usize>, |
| 85 | list_offset: f32, |
| 86 | list_focused: bool, |
| 87 | marquee: Option<Marquee>, |
| 88 | last_request: Option<Start>, |
| 89 | targets: Option<BTreeSet<String>>, |
| 90 | directories: BTreeSet<String>, |
| 91 | dismissed: bool, |
| 92 | details: bool, |
| 93 | checked_destination: bool, |
| 94 | ui_error: Option<String>, |
| 95 | initialized: bool, |
| 96 | } |
| 97 | |
| 98 | impl ArchiveApp { |
| 99 | pub fn new(factory: Box<dyn Factory>, startup: Option<PathBuf>, _fixture: bool) -> Self { |
| 100 | let mut app = Self { |
| 101 | model: Model::default(), |
| 102 | factory, |
| 103 | destination: PathBuf::new(), |
| 104 | automatic_destination: true, |
| 105 | owned_destination: None, |
| 106 | gzip_name: String::new(), |
| 107 | sources: Vec::new(), |
| 108 | zip_folder: present::home_directory(), |
| 109 | zip_name: String::new(), |
| 110 | password: String::new(), |
| 111 | password_confirm: String::new(), |
| 112 | password_visible: false, |
| 113 | password_challenge: None, |
| 114 | aes256: false, |
| 115 | create: false, |
| 116 | selected_only: false, |
| 117 | method: Method::Deflate, |
| 118 | dialogs: Box::new(NativeDialogs), |
| 119 | picker: None, |
| 120 | work: WorkWindow::default(), |
| 121 | custom_frame: false, |
| 122 | release_after_move: false, |
| 123 | folder: String::new(), |
| 124 | search: String::new(), |
| 125 | list_anchor: None, |
| 126 | list_cursor: None, |
| 127 | list_offset: 0.0, |
| 128 | list_focused: false, |
| 129 | marquee: None, |
| 130 | last_request: None, |
| 131 | targets: None, |
| 132 | directories: BTreeSet::new(), |
| 133 | dismissed: false, |
| 134 | details: false, |
| 135 | checked_destination: false, |
| 136 | ui_error: None, |
| 137 | initialized: false, |
| 138 | }; |
| 139 | if let Some(path) = startup { |
| 140 | app.open_path(path); |
| 141 | } |
| 142 | app |
| 143 | } |
| 144 | |
| 145 | pub fn set_dialogs(&mut self, dialogs: Box<dyn Dialogs>) { |
| 146 | self.dialogs = dialogs; |
| 147 | } |
| 148 | |
| 149 | /// 운영체제가 창 테두리를 그려 주지 않을 때(GNOME Wayland) 제목 표시줄을 직접 그림. |
| 150 | pub fn set_custom_frame(&mut self, custom: bool) { |
| 151 | self.custom_frame = custom; |
| 152 | } |
| 153 | |
| 154 | pub fn browse_folder(&mut self, folder: &str) { |
| 155 | self.folder = folder.trim_matches('/').to_owned(); |
| 156 | self.search.clear(); |
| 157 | self.clear_list_selection(); |
| 158 | } |
| 159 | |
| 160 | pub fn show_create(&mut self, encrypted: bool) { |
| 161 | if !self.model.busy() { |
| 162 | self.create = true; |
| 163 | self.aes256 = encrypted; |
| 164 | self.ui_error = None; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fn clear_password(&mut self) { |
| 169 | for value in [&mut self.password, &mut self.password_confirm] { |
| 170 | let mut bytes = std::mem::take(value).into_bytes(); |
| 171 | bytes.fill(0); |
| 172 | } |
| 173 | self.password_visible = false; |
| 174 | } |
| 175 | |
| 176 | pub fn open_path(&mut self, archive: PathBuf) { |
| 177 | if self.model.busy() { |
| 178 | return; |
| 179 | } |
| 180 | self.clear_password(); |
| 181 | self.destination = present::default_destination(&archive); |
| 182 | self.automatic_destination = true; |
| 183 | self.gzip_name = present::gzip_output_name(&archive); |
| 184 | self.selected_only = false; |
| 185 | self.folder.clear(); |
| 186 | self.search.clear(); |
| 187 | self.clear_list_selection(); |
| 188 | self.targets = None; |
| 189 | self.directories.clear(); |
| 190 | self.start(Start::List { archive }); |
| 191 | } |
| 192 | |
| 193 | fn start(&mut self, request: Start) -> bool { |
| 194 | // 파일 선택 창이 응답을 기다리는 동안에는 어느 창에서도 새 작업을 시작하지 않음. |
| 195 | if self.picker.is_some() { |
| 196 | return false; |
| 197 | } |
| 198 | self.last_request = Some(request.clone()); |
| 199 | self.dismissed = false; |
| 200 | self.details = false; |
| 201 | self.checked_destination = false; |
| 202 | self.ui_error = None; |
| 203 | self.model.begin(request, self.factory.as_mut()) |
| 204 | } |
| 205 | |
| 206 | pub fn choose_destination(&mut self, path: PathBuf) { |
| 207 | self.destination = path; |
| 208 | self.automatic_destination = false; |
| 209 | self.ui_error = None; |
| 210 | } |
| 211 | |
| 212 | pub fn add_sources(&mut self, paths: Vec<PathBuf>) { |
| 213 | if self.model.busy() { |
| 214 | return; |
| 215 | } |
| 216 | let mut validated = Vec::new(); |
| 217 | for path in paths { |
| 218 | let Ok(metadata) = std::fs::symlink_metadata(&path) else { |
| 219 | self.ui_error = Some("파일이나 폴더를 찾지 못했습니다. 다시 선택하세요".into()); |
| 220 | return; |
| 221 | }; |
| 222 | if metadata.file_type().is_symlink() || !(metadata.is_file() || metadata.is_dir()) { |
| 223 | self.ui_error = Some("일반 파일과 폴더만 추가할 수 있습니다".into()); |
| 224 | return; |
| 225 | } |
| 226 | let Ok(path) = path.canonicalize() else { |
| 227 | return; |
| 228 | }; |
| 229 | if path.to_str().is_none() |
| 230 | || !path |
| 231 | .file_name() |
| 232 | .and_then(|name| name.to_str()) |
| 233 | .is_some_and(valid_leaf) |
| 234 | { |
| 235 | self.ui_error = Some("파일이나 폴더의 이름을 확인하세요".into()); |
| 236 | return; |
| 237 | } |
| 238 | validated.push(crate::archive_path::worker_path(&path)); |
| 239 | } |
| 240 | let paths = validated; |
| 241 | if self.sources.is_empty() { |
| 242 | if let Some(first) = paths.first() { |
| 243 | self.zip_folder = first.parent().unwrap_or(Path::new("/")).to_path_buf(); |
| 244 | let name = if first.is_dir() { |
| 245 | first.file_name() |
| 246 | } else { |
| 247 | first.file_stem() |
| 248 | }; |
| 249 | self.zip_name = format!("{}.zip", name.unwrap_or_default().to_string_lossy()); |
| 250 | } |
| 251 | } |
| 252 | for path in paths { |
| 253 | if !self.sources.contains(&path) { |
| 254 | self.sources.push(path); |
| 255 | } |
| 256 | } |
| 257 | self.create = true; |
| 258 | } |
| 259 | |
| 260 | pub fn begin_extract(&mut self) -> bool { |
| 261 | self.selected_only = !self.model.selected.is_empty(); |
| 262 | let selection = if self.selected_only { |
| 263 | Selection::EntryIds( |
| 264 | browser::expand_selection(&self.model.entries, &self.model.selected) |
| 265 | .into_iter() |
| 266 | .collect(), |
| 267 | ) |
| 268 | } else { |
| 269 | Selection::All |
| 270 | }; |
| 271 | self.extract_selection(selection) |
| 272 | } |
| 273 | |
| 274 | fn extract_selection(&mut self, selection: Selection) -> bool { |
| 275 | if !self.model.can_extract() |
| 276 | || matches!(&selection, Selection::EntryIds(ids) if ids.is_empty()) |
| 277 | { |
| 278 | return false; |
| 279 | } |
| 280 | self.selected_only = matches!(&selection, Selection::EntryIds(_)); |
| 281 | match present::prepare_destination(&self.destination, self.automatic_destination) { |
| 282 | Ok(true) => self.owned_destination = Some(self.destination.clone()), |
| 283 | Ok(false) => self.owned_destination = None, |
| 284 | Err(_) => { |
| 285 | self.ui_error = Some(present::FOLDER_ERROR.into()); |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | let gzip = self |
| 290 | .model |
| 291 | .list_id |
| 292 | .as_ref() |
| 293 | .is_some_and(|id| id.format == Format::Gzip); |
| 294 | self.targets = present::target_files( |
| 295 | &self.model.entries, |
| 296 | &selection, |
| 297 | gzip.then_some(self.gzip_name.as_str()), |
| 298 | ); |
| 299 | self.directories = if gzip { |
| 300 | BTreeSet::new() |
| 301 | } else { |
| 302 | present::target_directories(&self.model.entries, &selection) |
| 303 | }; |
| 304 | let request = Start::Extract { |
| 305 | archive: self.model.archive.clone().unwrap(), |
| 306 | destination: self.destination.clone(), |
| 307 | selection, |
| 308 | list_id: self.model.list_id.clone().unwrap(), |
| 309 | gzip_output_name: gzip.then(|| self.gzip_name.clone()), |
| 310 | }; |
| 311 | if self.start(request) { |
| 312 | true |
| 313 | } else { |
| 314 | if let Some(path) = self.owned_destination.take() { |
| 315 | present::remove_owned_empty(&path); |
| 316 | } |
| 317 | false |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | fn begin_zip(&mut self) { |
| 322 | if self.sources.is_empty() { |
| 323 | return; |
| 324 | } |
| 325 | if !self.zip_name.to_ascii_lowercase().ends_with(".zip") { |
| 326 | self.zip_name.push_str(".zip"); |
| 327 | } |
| 328 | if !valid_leaf(&self.zip_name) { |
| 329 | self.ui_error = Some("ZIP 파일 이름을 확인하세요".into()); |
| 330 | return; |
| 331 | } |
| 332 | let output_file = self.zip_folder.join(&self.zip_name); |
| 333 | if std::fs::symlink_metadata(&output_file).is_ok() { |
| 334 | self.ui_error = Some("같은 이름의 파일이 이미 있습니다. 다른 이름을 입력하세요".into()); |
| 335 | return; |
| 336 | } |
| 337 | let mut sources = Vec::new(); |
| 338 | for path in &self.sources { |
| 339 | let Some(name) = path.file_name().and_then(|name| name.to_str()) else { |
| 340 | self.ui_error = Some( |
| 341 | "이름을 읽을 수 없는 파일이 있습니다. 파일 이름을 바꾼 뒤 다시 추가하세요" |
| 342 | .into(), |
| 343 | ); |
| 344 | return; |
| 345 | }; |
| 346 | sources.push((path.clone(), name.to_owned())); |
| 347 | } |
| 348 | self.targets = Some([self.zip_name.clone()].into_iter().collect()); |
| 349 | self.directories.clear(); |
| 350 | if self.start(Start::CreateZip { |
| 351 | sources, |
| 352 | output_file, |
| 353 | method: self.method, |
| 354 | aes256: self.aes256, |
| 355 | }) { |
| 356 | self.create = false; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | fn open_picker(&mut self, purpose: Purpose) { |
| 361 | if self.model.busy() || self.picker.is_some() { |
| 362 | return; |
| 363 | } |
| 364 | let gzip = self |
| 365 | .model |
| 366 | .list_id |
| 367 | .as_ref() |
| 368 | .is_some_and(|id| id.format == Format::Gzip); |
| 369 | let purpose = match (purpose, gzip) { |
| 370 | (Purpose::Destination, true) => Purpose::SaveGzip, |
| 371 | (Purpose::Retry, true) => Purpose::RetryGzip, |
| 372 | _ => purpose, |
| 373 | }; |
| 374 | let directory = match purpose { |
| 375 | Purpose::SaveZip => self.zip_folder.clone(), |
| 376 | Purpose::Destination | Purpose::SaveGzip => self |
| 377 | .model |
| 378 | .archive |
| 379 | .as_ref() |
| 380 | .and_then(|path| path.parent()) |
| 381 | .map(Path::to_path_buf) |
| 382 | .unwrap_or_else(present::home_directory), |
| 383 | Purpose::Retry | Purpose::RetryGzip => self.destination.clone(), |
| 384 | _ => self |
| 385 | .sources |
| 386 | .last() |
| 387 | .and_then(|path| path.parent()) |
| 388 | .map(Path::to_path_buf) |
| 389 | .unwrap_or_else(present::home_directory), |
| 390 | }; |
| 391 | let request = DialogRequest { |
| 392 | purpose, |
| 393 | directory, |
| 394 | file_name: if matches!(purpose, Purpose::SaveGzip | Purpose::RetryGzip) { |
| 395 | self.gzip_name.clone() |
| 396 | } else { |
| 397 | self.zip_name.clone() |
| 398 | }, |
| 399 | }; |
| 400 | match self.dialogs.start(request) { |
| 401 | PendingDialog::Ready(answer) => self.apply_picker(purpose, answer), |
| 402 | PendingDialog::Waiting(receiver) => self.picker = Some((purpose, receiver)), |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | fn poll_picker(&mut self) { |
| 407 | let Some((purpose, receiver)) = &self.picker else { |
| 408 | return; |
| 409 | }; |
| 410 | let purpose = *purpose; |
| 411 | let answer = match receiver.try_recv() { |
| 412 | Ok(answer) => answer, |
| 413 | Err(std::sync::mpsc::TryRecvError::Empty) => return, |
| 414 | Err(std::sync::mpsc::TryRecvError::Disconnected) => Err(DIALOG_UNAVAILABLE.into()), |
| 415 | }; |
| 416 | self.picker = None; |
| 417 | self.apply_picker(purpose, answer); |
| 418 | } |
| 419 | |
| 420 | fn apply_picker(&mut self, purpose: Purpose, answer: DialogAnswer) { |
| 421 | if answer.is_ok() && self.ui_error.as_deref() == Some(DIALOG_UNAVAILABLE) { |
| 422 | self.ui_error = None; |
| 423 | } |
| 424 | match answer { |
| 425 | Ok(Some(paths)) if !paths.is_empty() => { |
| 426 | let path = paths[0].clone(); |
| 427 | match purpose { |
| 428 | Purpose::Open => { |
| 429 | if std::fs::symlink_metadata(&path) |
| 430 | .is_ok_and(|m| m.is_file() && !m.file_type().is_symlink()) |
| 431 | { |
| 432 | self.open_path(path); |
| 433 | } else { |
| 434 | self.ui_error = Some("일반 압축파일을 선택하세요".into()); |
| 435 | } |
| 436 | } |
| 437 | Purpose::Files | Purpose::Folder => self.add_sources(paths), |
| 438 | Purpose::Destination | Purpose::Retry => { |
| 439 | self.choose_destination(path); |
| 440 | self.selected_only = !self.model.selected.is_empty(); |
| 441 | if purpose == Purpose::Retry { |
| 442 | self.retry(); |
| 443 | } else { |
| 444 | self.begin_extract(); |
| 445 | } |
| 446 | } |
| 447 | Purpose::SaveGzip | Purpose::RetryGzip => { |
| 448 | let Some(name) = path.file_name().and_then(|name| name.to_str()) else { |
| 449 | self.ui_error = Some("저장할 파일 이름을 확인하세요".into()); |
| 450 | return; |
| 451 | }; |
| 452 | if !present::valid_output_leaf(name) { |
| 453 | self.ui_error = Some("저장할 파일 이름을 확인하세요".into()); |
| 454 | return; |
| 455 | } |
| 456 | let name = name.to_owned(); |
| 457 | self.choose_destination( |
| 458 | path.parent().unwrap_or(Path::new("/")).to_path_buf(), |
| 459 | ); |
| 460 | self.gzip_name = name.clone(); |
| 461 | self.selected_only = !self.model.selected.is_empty(); |
| 462 | if purpose == Purpose::RetryGzip { |
| 463 | if let Some(Start::Extract { |
| 464 | gzip_output_name, .. |
| 465 | }) = &mut self.last_request |
| 466 | { |
| 467 | *gzip_output_name = Some(name); |
| 468 | } |
| 469 | self.retry(); |
| 470 | } else { |
| 471 | self.begin_extract(); |
| 472 | } |
| 473 | } |
| 474 | Purpose::SaveZip => { |
| 475 | if let Some(name) = path.file_name().and_then(|name| name.to_str()) { |
| 476 | self.zip_name = name.to_owned(); |
| 477 | self.zip_folder = path.parent().unwrap_or(Path::new("/")).to_path_buf(); |
| 478 | self.begin_zip(); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | Ok(_) => {} |
| 484 | Err(error) => self.ui_error = Some(error), |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | fn retry(&mut self) { |
| 489 | if let Some(request) = self.last_request.clone() { |
| 490 | match request { |
| 491 | Start::List { archive } => self.open_path(archive), |
| 492 | Start::Extract { |
| 493 | selection, |
| 494 | gzip_output_name, |
| 495 | .. |
| 496 | } => { |
| 497 | if let Some(name) = gzip_output_name { |
| 498 | self.gzip_name = name; |
| 499 | } |
| 500 | self.extract_selection(selection); |
| 501 | } |
| 502 | Start::CreateZip { .. } => { |
| 503 | self.create = true; |
| 504 | self.begin_zip(); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | fn on_drop(&mut self, paths: Vec<PathBuf>) { |
| 511 | if self.model.busy() || paths.is_empty() { |
| 512 | return; |
| 513 | } |
| 514 | if paths.len() == 1 && !self.create && !paths[0].is_dir() { |
| 515 | if std::fs::symlink_metadata(&paths[0]) |
| 516 | .is_ok_and(|m| m.is_file() && !m.file_type().is_symlink()) |
| 517 | { |
| 518 | self.open_path(paths[0].clone()); |
| 519 | } else { |
| 520 | self.ui_error = Some("일반 압축파일을 선택하세요".into()); |
| 521 | } |
| 522 | } else { |
| 523 | self.add_sources(paths); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | fn poll(&mut self) { |
| 528 | let busy = self.model.busy(); |
| 529 | self.model.poll(); |
| 530 | if busy && !self.model.busy() { |
| 531 | if let Some(path) = self.owned_destination.take() { |
| 532 | if !self.model.confirmed |
| 533 | || self |
| 534 | .model |
| 535 | .terminal |
| 536 | .as_ref() |
| 537 | .is_none_or(|t| t.outcome != Outcome::Completed) |
| 538 | { |
| 539 | present::remove_owned_empty(&path); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | pub fn show(&mut self, root: &mut egui::Ui) { |
| 546 | let ctx = root.ctx().clone(); |
| 547 | if !self.initialized { |
| 548 | theme::apply(&ctx); |
| 549 | self.initialized = true; |
| 550 | } |
| 551 | self.poll(); |
| 552 | self.poll_picker(); |
| 553 | let challenge = self.model.challenge.map(|(id, _, _)| id); |
| 554 | let new_challenge = challenge != self.password_challenge; |
| 555 | if new_challenge || challenge.is_none() { |
| 556 | self.clear_password(); |
| 557 | } |
| 558 | self.password_challenge = challenge; |
| 559 | let dropped = ctx.input(|i| { |
| 560 | i.raw |
| 561 | .dropped_files |
| 562 | .iter() |
| 563 | .map(|file| file.path().to_path_buf()) |
| 564 | .collect() |
| 565 | }); |
| 566 | if self.picker.is_none() { |
| 567 | self.on_drop(dropped); |
| 568 | } |
| 569 | if ctx.input(|i| i.viewport().close_requested()) { |
| 570 | self.model.request_close(); |
| 571 | if self.model.close != Close::Allowed { |
| 572 | ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); |
| 573 | } |
| 574 | } |
| 575 | if self.model.close == Close::Allowed { |
| 576 | ctx.send_viewport_cmd(egui::ViewportCommand::Close); |
| 577 | } |
| 578 | let title = self |
| 579 | .model |
| 580 | .archive |
| 581 | .as_ref() |
| 582 | .map(|path| { |
| 583 | let full = if path.is_absolute() { |
| 584 | path.clone() |
| 585 | } else { |
| 586 | std::env::current_dir().unwrap_or_default().join(path) |
| 587 | }; |
| 588 | format!("{} — Zipelo", full.display()) |
| 589 | }) |
| 590 | .unwrap_or_else(|| "Zipelo".into()); |
| 591 | let short_title = if let Some(path) = title.strip_suffix(" — Zipelo") { |
| 592 | format!( |
| 593 | "{} — Zipelo", |
| 594 | theme::window_title(root, path, ctx.content_rect().width() - 300.0) |
| 595 | ) |
| 596 | } else { |
| 597 | title.clone() |
| 598 | }; |
| 599 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(short_title)); |
| 600 | if self.custom_frame |
| 601 | && window_frame::title_bar( |
| 602 | root, |
| 603 | "main", |
| 604 | &title, |
| 605 | window_frame::Buttons { |
| 606 | minimize: true, |
| 607 | maximize: true, |
| 608 | }, |
| 609 | ) |
| 610 | { |
| 611 | self.release_after_move = true; |
| 612 | } |
| 613 | egui::Panel::top("archive-actions") |
| 614 | .frame( |
| 615 | theme::panel(theme::PANEL) |
| 616 | .corner_radius(0) |
| 617 | .inner_margin(egui::Margin { |
| 618 | left: 12, |
| 619 | right: 12, |
| 620 | top: 6, |
| 621 | // 이름 글자 줄 높이 아래에 남는 빈 공간(약 5px)만큼 줄여 |
| 622 | // 아이콘 위와 글자 아래의 보이는 여백을 같게 맞춤. |
| 623 | bottom: 1, |
| 624 | }), |
| 625 | ) |
| 626 | .show(root, |ui| { |
| 627 | ui.horizontal(|ui| { |
| 628 | ui.spacing_mut().item_spacing.x = 2.0; |
| 629 | let idle = !self.model.busy() |
| 630 | && self.model.close == Close::Open |
| 631 | && self.picker.is_none(); |
| 632 | if theme::tool(ui, "열기", theme::OPEN, idle, false) |
| 633 | .on_hover_text(&title) |
| 634 | .clicked() |
| 635 | { |
| 636 | self.open_picker(Purpose::Open); |
| 637 | } |
| 638 | if theme::tool(ui, "압축하기", theme::CREATE, idle, false).clicked() { |
| 639 | self.show_create(false); |
| 640 | } |
| 641 | if theme::tool( |
| 642 | ui, |
| 643 | "압축해제", |
| 644 | theme::EXTRACT, |
| 645 | self.model.can_extract(), |
| 646 | true, |
| 647 | ) |
| 648 | .clicked() |
| 649 | { |
| 650 | self.open_picker(Purpose::Destination); |
| 651 | } |
| 652 | }); |
| 653 | }); |
| 654 | egui::Panel::bottom("archive-status") |
| 655 | .exact_size(theme::STATUS_HEIGHT) |
| 656 | .resizable(false) |
| 657 | .frame( |
| 658 | egui::Frame::new() |
| 659 | .fill(theme::STRIPE) |
| 660 | .inner_margin(egui::Margin::symmetric(16, 0)), |
| 661 | ) |
| 662 | .show(root, |ui| { |
| 663 | let rows = browser::rows(&self.model.entries, &self.folder, &self.search); |
| 664 | let selected = rows |
| 665 | .iter() |
| 666 | .filter(|row| !row.ids.is_empty() && row.ids.is_subset(&self.model.selected)) |
| 667 | .count(); |
| 668 | let rect = ui.max_rect(); |
| 669 | let half = (rect.width() - theme::SPACE) / 2.0; |
| 670 | theme::status_text( |
| 671 | ui, |
| 672 | rect, |
| 673 | &format!("{}개 항목", rows.len()), |
| 674 | half, |
| 675 | Align::Min, |
| 676 | ); |
| 677 | theme::status_text(ui, rect, &format!("선택 {selected}개"), half, Align::Max); |
| 678 | }); |
| 679 | if ctx.embed_viewports() && self.work_visible() { |
| 680 | // 별도 창을 만들 수 없을 때만 쓰는 대체 배치. 목록을 가리지 않도록 하단에 둠. |
| 681 | egui::Panel::bottom("archive-work") |
| 682 | .frame(theme::panel(theme::PANEL).corner_radius(0)) |
| 683 | .show(root, |ui| self.work_area(ui)); |
| 684 | } |
| 685 | egui::CentralPanel::default() |
| 686 | .frame( |
| 687 | theme::panel(theme::PANEL) |
| 688 | .corner_radius(0) |
| 689 | .inner_margin(egui::Margin::ZERO), |
| 690 | ) |
| 691 | .show(root, |ui| { |
| 692 | if self.model.archive.is_some() { |
| 693 | self.archive_list(ui); |
| 694 | } else { |
| 695 | self.empty(ui); |
| 696 | } |
| 697 | }); |
| 698 | self.work_window(&ctx); |
| 699 | if self.create && !self.model.busy() { |
| 700 | self.create_dialog(&ctx); |
| 701 | } |
| 702 | if self.model.challenge.is_some() { |
| 703 | self.password_dialog(&ctx, new_challenge); |
| 704 | } |
| 705 | self.close_dialog(&ctx); |
| 706 | self.picker_notice(&ctx); |
| 707 | if self.custom_frame { |
| 708 | window_frame::edges(&ctx, true); |
| 709 | } |
| 710 | if self.model.busy() { |
| 711 | ctx.request_repaint_after(std::time::Duration::from_millis(30)); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // 선택 창이 열려 있는 동안 다른 조작을 막고, 응답을 받도록 화면을 계속 갱신함. |
| 716 | fn picker_notice(&mut self, ctx: &egui::Context) { |
| 717 | if self.picker.is_none() { |
| 718 | return; |
| 719 | } |
| 720 | egui::Modal::new(egui::Id::unique("archive-picker")).show(ctx, |ui| { |
| 721 | ui.set_width(360.0); |
| 722 | ui.label("파일 선택 창에서 선택을 마쳐 주세요"); |
| 723 | }); |
| 724 | ctx.request_repaint_after(std::time::Duration::from_millis(100)); |
| 725 | } |
| 726 | |
| 727 | fn empty(&mut self, ui: &mut egui::Ui) { |
| 728 | let hovered = ui.ctx().input(|i| !i.raw.hovered_files.is_empty()); |
| 729 | let width = (ui.available_width() * 0.60) |
| 730 | .max(480.0) |
| 731 | .min(ui.available_width()); |
| 732 | let y = ((ui.available_height() - theme::DROP_HEIGHT - theme::SPACE * 8.0) / 2.0).max(0.0); |
| 733 | ui.add_space(y); |
| 734 | ui.vertical_centered(|ui| { |
| 735 | let drop_frame = egui::Frame::new() |
| 736 | .fill(if hovered { theme::DROP } else { theme::PANEL }) |
| 737 | .corner_radius(theme::DROP_RADIUS) |
| 738 | .inner_margin(egui::Margin::same(theme::DROP_PAD)) |
| 739 | .show(ui, |ui| { |
| 740 | ui.set_width(width - 2.0 * theme::DROP_PAD as f32); |
| 741 | ui.set_min_height(theme::DROP_HEIGHT - 2.0 * theme::DROP_PAD as f32); |
| 742 | ui.vertical_centered(|ui| { |
| 743 | ui.add_space(theme::PAD as f32); |
| 744 | theme::icon(ui, Icon::Archive, 48.0); |
| 745 | ui.add_space(theme::PAD as f32); |
| 746 | ui.label( |
| 747 | RichText::new(if hovered { |
| 748 | "놓으면 엽니다" |
| 749 | } else { |
| 750 | "압축파일을 여기에 끌어 놓으세요" |
| 751 | }) |
| 752 | .size(20.0) |
| 753 | .strong(), |
| 754 | ); |
| 755 | ui.label("또는"); |
| 756 | if theme::action(ui, "파일 열기", true, !self.model.busy()).clicked() { |
| 757 | self.open_picker(Purpose::Open); |
| 758 | } |
| 759 | }); |
| 760 | }); |
| 761 | let rect = drop_frame.response.rect; |
| 762 | if hovered { |
| 763 | ui.painter().rect_stroke( |
| 764 | rect, |
| 765 | 12, |
| 766 | egui::Stroke::new(2.0, theme::BLUE), |
| 767 | egui::StrokeKind::Inside, |
| 768 | ); |
| 769 | } else { |
| 770 | let stroke = egui::Stroke::new(2.0, theme::DASH); |
| 771 | for (a, b) in [ |
| 772 | (rect.left_top(), rect.right_top()), |
| 773 | (rect.right_top(), rect.right_bottom()), |
| 774 | (rect.right_bottom(), rect.left_bottom()), |
| 775 | (rect.left_bottom(), rect.left_top()), |
| 776 | ] { |
| 777 | let direction = (b - a).normalized(); |
| 778 | let mut distance = theme::DROP_RADIUS as f32; |
| 779 | while distance < a.distance(b) - theme::DROP_RADIUS as f32 { |
| 780 | ui.painter().line_segment( |
| 781 | [ |
| 782 | a + direction * distance, |
| 783 | a + direction |
| 784 | * (distance + theme::SPACE) |
| 785 | .min(a.distance(b) - theme::DROP_RADIUS as f32), |
| 786 | ], |
| 787 | stroke, |
| 788 | ); |
| 789 | distance += theme::PAD as f32; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | ui.add_space(theme::PAD as f32); |
| 794 | ui.colored_label( |
| 795 | theme::MUTED, |
| 796 | "ZIP, 7z, TAR, GZ, TAR.GZ 파일을 열 수 있습니다", |
| 797 | ); |
| 798 | }); |
| 799 | } |
| 800 | |
| 801 | fn archive_list(&mut self, ui: &mut egui::Ui) { |
| 802 | // 경로 줄은 입력란 높이만 차지하고 창 너비 전체를 회색으로 채움. |
| 803 | egui::Frame::new() |
| 804 | .fill(theme::STRIPE) |
| 805 | .inner_margin(egui::Margin { |
| 806 | left: 8, |
| 807 | right: 12, |
| 808 | top: 0, |
| 809 | bottom: 0, |
| 810 | }) |
| 811 | .show(ui, |ui| { |
| 812 | ui.set_min_width(ui.available_width()); |
| 813 | ui.horizontal(|ui| { |
| 814 | ui.set_height(theme::FIELD); |
| 815 | // 간격은 아래에서 직접 넣어 위치가 흔들리지 않게 함. |
| 816 | ui.spacing_mut().item_spacing.x = 0.0; |
| 817 | let up = theme::flat_icon_button( |
| 818 | ui, |
| 819 | "위로", |
| 820 | theme::UP, |
| 821 | !self.folder.is_empty() && !self.model.busy(), |
| 822 | egui::vec2(theme::FIELD, theme::FIELD), |
| 823 | ); |
| 824 | if up.clicked() { |
| 825 | let parent = self |
| 826 | .folder |
| 827 | .rsplit_once('/') |
| 828 | .map_or("", |(parent, _)| parent) |
| 829 | .to_owned(); |
| 830 | self.browse_folder(&parent); |
| 831 | } |
| 832 | ui.add_space(6.0); |
| 833 | let path = format!("/{}", self.folder); |
| 834 | let search_width = 240.0; |
| 835 | let gap = 8.0; |
| 836 | let path_width = (ui.available_width() - search_width - gap).max(0.0); |
| 837 | // 두 입력란은 같은 높이의 칸을 받아 같은 세로 위치에 그림. |
| 838 | let (path_rect, _) = ui.allocate_exact_size( |
| 839 | egui::vec2(path_width, theme::FIELD), |
| 840 | egui::Sense::hover(), |
| 841 | ); |
| 842 | theme::field_frame(ui, path_rect, false); |
| 843 | // 부모 배치에 영향을 주지 않는 하위 영역에 경로 글자를 그림. |
| 844 | let mut text_ui = ui.new_child( |
| 845 | egui::UiBuilder::new() |
| 846 | .max_rect(path_rect.shrink2(egui::vec2(10.0, 0.0))) |
| 847 | .layout(Layout::left_to_right(Align::Center)), |
| 848 | ); |
| 849 | theme::bounded_text(&mut text_ui, &path, (path_width - 20.0).max(0.0)); |
| 850 | ui.add_space(gap); |
| 851 | let (search_rect, _) = ui.allocate_exact_size( |
| 852 | egui::vec2(search_width, theme::FIELD), |
| 853 | egui::Sense::hover(), |
| 854 | ); |
| 855 | let search_id = egui::Id::unique("archive-search"); |
| 856 | let focused = ui.memory(|m| m.has_focus(search_id)); |
| 857 | theme::field_frame(ui, search_rect, focused); |
| 858 | let icon = egui::Rect::from_center_size( |
| 859 | egui::pos2(search_rect.left() + 20.0, search_rect.center().y), |
| 860 | egui::vec2(16.0, 16.0), |
| 861 | ); |
| 862 | theme::svg_icon(ui.painter(), icon, theme::SEARCH, theme::MUTED); |
| 863 | let search = ui.put( |
| 864 | egui::Rect::from_min_max( |
| 865 | egui::pos2(icon.right() + 8.0, search_rect.top() + 1.0), |
| 866 | egui::pos2(search_rect.right() - 8.0, search_rect.bottom() - 1.0), |
| 867 | ), |
| 868 | egui::TextEdit::singleline(&mut self.search) |
| 869 | .id(search_id) |
| 870 | .frame(egui::Frame::NONE) |
| 871 | .margin(egui::Margin::ZERO) |
| 872 | .align(egui::Align2::LEFT_CENTER) |
| 873 | .hint_text("파일 검색"), |
| 874 | ); |
| 875 | if search.changed() { |
| 876 | self.clear_list_selection(); |
| 877 | } |
| 878 | }); |
| 879 | }); |
| 880 | let rows = browser::rows(&self.model.entries, &self.folder, &self.search); |
| 881 | |
| 882 | let name_width = (ui.available_width() - 16.0 - 28.0 - 96.0 - 40.0).max(120.0); |
| 883 | ui.horizontal(|ui| { |
| 884 | ui.add_space(16.0 + 28.0); |
| 885 | theme::column_label(ui, RichText::new("이름").strong(), name_width, Align::Min); |
| 886 | theme::column_label(ui, "크기", 96.0, Align::Max); |
| 887 | }); |
| 888 | ui.separator(); |
| 889 | let active = !self.model.busy() |
| 890 | && !self.create |
| 891 | && self.model.challenge.is_none() |
| 892 | && self.model.close == Close::Open; |
| 893 | let list_id = egui::Id::unique("archive-list"); |
| 894 | let background = ui.interact( |
| 895 | ui.available_rect_before_wrap(), |
| 896 | list_id, |
| 897 | if active { |
| 898 | egui::Sense::click_and_drag() |
| 899 | } else { |
| 900 | egui::Sense::hover() |
| 901 | }, |
| 902 | ); |
| 903 | background |
| 904 | .widget_info(|| egui::WidgetInfo::labeled(egui::Role::ListBox, active, "파일 목록")); |
| 905 | if background.has_focus() { |
| 906 | ui.ctx().memory_mut(|memory| { |
| 907 | memory.set_focus_lock_filter( |
| 908 | list_id, |
| 909 | egui::EventFilter { |
| 910 | vertical_arrows: true, |
| 911 | escape: true, |
| 912 | ..Default::default() |
| 913 | }, |
| 914 | ) |
| 915 | }); |
| 916 | } |
| 917 | let mut focused = None; |
| 918 | let mut open = None; |
| 919 | let mut next_offset = None; |
| 920 | let output = egui::ScrollArea::vertical() |
| 921 | .id_salt("archive-rows") |
| 922 | .auto_shrink([false, false]) |
| 923 | .vertical_scroll_offset(self.list_offset) |
| 924 | .show_viewport(ui, |ui, viewport| { |
| 925 | let origin = ui.max_rect().min; |
| 926 | let clip = ui.clip_rect(); |
| 927 | let width = (ui.available_width() - 24.0).max(0.0); |
| 928 | ui.set_min_height((rows.len() as f32 * LIST_ROW).max(viewport.height())); |
| 929 | let point = ui.input(|i| i.pointer.interact_pos()); |
| 930 | if active && let Some(point) = point { |
| 931 | let press = ui.input(|i| i.pointer.press_origin()).unwrap_or(point); |
| 932 | let relative = press - origin; |
| 933 | let in_row = relative.x >= 0.0 |
| 934 | && relative.x <= width |
| 935 | && relative.y >= 0.0 |
| 936 | && relative.y < rows.len() as f32 * LIST_ROW; |
| 937 | if clip.contains(press) && !in_row && ui.input(|i| i.pointer.primary_pressed()) |
| 938 | { |
| 939 | background.request_focus(); |
| 940 | let ctrl = ui.input(|i| i.modifiers.ctrl || i.modifiers.command); |
| 941 | self.marquee = Some(Marquee { |
| 942 | origin: relative.to_pos2(), |
| 943 | base: if ctrl { |
| 944 | self.model.selected.clone() |
| 945 | } else { |
| 946 | BTreeSet::new() |
| 947 | }, |
| 948 | moved: false, |
| 949 | }); |
| 950 | if !ctrl { |
| 951 | self.model.selected.clear(); |
| 952 | self.list_anchor = None; |
| 953 | self.list_cursor = None; |
| 954 | } |
| 955 | } |
| 956 | if let Some(drag) = &mut self.marquee { |
| 957 | let end = (point.clamp(clip.min, clip.max) - origin).to_pos2(); |
| 958 | drag.moved |= drag.origin.distance(end) > 3.0; |
| 959 | if drag.moved { |
| 960 | let rect = egui::Rect::from_two_pos(drag.origin, end); |
| 961 | self.model.selected = drag.base.clone(); |
| 962 | if rect.left() < width && rect.right() > 0.0 { |
| 963 | let first = (rect.top().max(0.0) / LIST_ROW).floor() as usize; |
| 964 | let last = ((rect.bottom().max(0.0) / LIST_ROW).ceil() as usize) |
| 965 | .min(rows.len()); |
| 966 | for row in rows.iter().take(last).skip(first) { |
| 967 | self.model.selected.extend(&row.ids); |
| 968 | } |
| 969 | if first < last { |
| 970 | self.list_anchor = Some(first); |
| 971 | self.list_cursor = Some(last - 1); |
| 972 | } |
| 973 | } |
| 974 | let speed = if point.y > clip.bottom() - 24.0 { |
| 975 | ((point.y - clip.bottom() + 24.0) / 24.0).clamp(0.0, 3.0) |
| 976 | } else if point.y < clip.top() + 24.0 { |
| 977 | -((clip.top() + 24.0 - point.y) / 24.0).clamp(0.0, 3.0) |
| 978 | } else { |
| 979 | 0.0 |
| 980 | }; |
| 981 | if ui.input(|i| i.pointer.primary_down()) && speed != 0.0 { |
| 982 | let dt = ui.input(|i| i.stable_dt).clamp(0.001, 0.05); |
| 983 | next_offset = Some((viewport.min.y + speed * 500.0 * dt).clamp( |
| 984 | 0.0, |
| 985 | (rows.len() as f32 * LIST_ROW - viewport.height()).max(0.0), |
| 986 | )); |
| 987 | ui.ctx().request_repaint(); |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | let first = (viewport.min.y / LIST_ROW).floor() as usize; |
| 993 | let last = ((viewport.max.y / LIST_ROW).ceil() as usize + 1).min(rows.len()); |
| 994 | for index in first.min(rows.len())..last { |
| 995 | let row = &rows[index]; |
| 996 | let rect = egui::Rect::from_min_size( |
| 997 | origin + egui::vec2(0.0, index as f32 * LIST_ROW), |
| 998 | egui::vec2(width, LIST_ROW), |
| 999 | ); |
| 1000 | let selected = !row.ids.is_empty() && row.ids.is_subset(&self.model.selected); |
| 1001 | let response = ui |
| 1002 | .interact( |
| 1003 | rect, |
| 1004 | egui::Id::unique(("archive-row", &row.path)), |
| 1005 | if active { |
| 1006 | egui::Sense::click() |
| 1007 | } else { |
| 1008 | egui::Sense::hover() |
| 1009 | }, |
| 1010 | ) |
| 1011 | .on_hover_text(&row.name); |
| 1012 | response.widget_info(|| { |
| 1013 | egui::WidgetInfo::selected( |
| 1014 | egui::Role::ListBoxOption, |
| 1015 | active, |
| 1016 | selected, |
| 1017 | &row.name, |
| 1018 | ) |
| 1019 | }); |
| 1020 | if response.has_focus() { |
| 1021 | focused = Some(index); |
| 1022 | ui.ctx().memory_mut(|memory| { |
| 1023 | memory.set_focus_lock_filter( |
| 1024 | response.id, |
| 1025 | egui::EventFilter { |
| 1026 | vertical_arrows: true, |
| 1027 | escape: true, |
| 1028 | ..Default::default() |
| 1029 | }, |
| 1030 | ) |
| 1031 | }); |
| 1032 | } |
| 1033 | if selected { |
| 1034 | ui.painter().rect_filled(rect, 0, theme::SELECTED); |
| 1035 | } |
| 1036 | if response.clicked() { |
| 1037 | background.request_focus(); |
| 1038 | let modifiers = ui.input(|i| i.modifiers); |
| 1039 | self.select_row( |
| 1040 | &rows, |
| 1041 | index, |
| 1042 | modifiers.ctrl || modifiers.command, |
| 1043 | modifiers.shift, |
| 1044 | ); |
| 1045 | } |
| 1046 | if row.directory && response.double_clicked() { |
| 1047 | open = Some(row.path.clone()); |
| 1048 | } |
| 1049 | ui.scope_builder( |
| 1050 | egui::UiBuilder::new() |
| 1051 | .max_rect(rect.shrink2(egui::vec2(0.0, 4.0))) |
| 1052 | .layout(Layout::left_to_right(Align::Center)), |
| 1053 | |ui| { |
| 1054 | ui.style_mut().interaction.selectable_labels = false; |
| 1055 | ui.add_space(16.0); |
| 1056 | theme::icon( |
| 1057 | ui, |
| 1058 | if row.directory { |
| 1059 | Icon::Folder |
| 1060 | } else { |
| 1061 | Icon::File |
| 1062 | }, |
| 1063 | 20.0, |
| 1064 | ); |
| 1065 | // 암호가 걸린 항목은 이름 앞에 *를 붙여 표시함. |
| 1066 | let name = if row.encrypted { |
| 1067 | format!("*{}", row.name) |
| 1068 | } else { |
| 1069 | row.name.clone() |
| 1070 | }; |
| 1071 | // 툴팁은 표시용 *를 빼고 실제 이름을 보여 줌. |
| 1072 | theme::bounded_text_hover(ui, &name, &row.name, name_width); |
| 1073 | theme::column_label( |
| 1074 | ui, |
| 1075 | if row.directory { |
| 1076 | "0 B".into() |
| 1077 | } else { |
| 1078 | row.bytes |
| 1079 | .map(present::bytes_text) |
| 1080 | .unwrap_or_else(|| "—".into()) |
| 1081 | }, |
| 1082 | 96.0, |
| 1083 | Align::Max, |
| 1084 | ); |
| 1085 | }, |
| 1086 | ); |
| 1087 | if active |
| 1088 | && self.list_cursor == Some(index) |
| 1089 | && (response.has_focus() || background.has_focus()) |
| 1090 | { |
| 1091 | ui.painter().rect_stroke( |
| 1092 | rect.shrink(1.0), |
| 1093 | 0, |
| 1094 | egui::Stroke::new(1.0, theme::BLUE), |
| 1095 | egui::StrokeKind::Inside, |
| 1096 | ); |
| 1097 | } |
| 1098 | } |
| 1099 | if rows.is_empty() { |
| 1100 | ui.scope_builder(egui::UiBuilder::new().max_rect(clip), |ui| { |
| 1101 | ui.centered_and_justified(|ui| { |
| 1102 | ui.label(if self.search.is_empty() { |
| 1103 | "이 폴더는 비어 있습니다" |
| 1104 | } else { |
| 1105 | "일치하는 항목이 없습니다" |
| 1106 | }) |
| 1107 | }); |
| 1108 | }); |
| 1109 | } |
| 1110 | if let Some(drag) = &self.marquee |
| 1111 | && drag.moved |
| 1112 | && let Some(point) = point |
| 1113 | { |
| 1114 | let rect = egui::Rect::from_two_pos( |
| 1115 | origin + drag.origin.to_vec2(), |
| 1116 | point.clamp(clip.min, clip.max), |
| 1117 | ); |
| 1118 | ui.painter().with_clip_rect(clip).rect( |
| 1119 | rect, |
| 1120 | 0, |
| 1121 | theme::BLUE.gamma_multiply(0.16), |
| 1122 | egui::Stroke::new(1.0, theme::BLUE), |
| 1123 | egui::StrokeKind::Inside, |
| 1124 | ); |
| 1125 | } |
| 1126 | }); |
| 1127 | self.list_offset = next_offset.unwrap_or(output.state.clamped_offset().y); |
| 1128 | if ui.input(|i| i.pointer.primary_released() || !i.pointer.primary_down()) { |
| 1129 | self.marquee = None; |
| 1130 | } |
| 1131 | let escape_from_list = self.list_focused |
| 1132 | && ui.ctx().memory(|memory| memory.focused()).is_none() |
| 1133 | && ui.input(|i| i.key_pressed(egui::Key::Escape)); |
| 1134 | self.list_focused = |
| 1135 | active && (background.has_focus() || focused.is_some() || escape_from_list); |
| 1136 | if self.list_focused { |
| 1137 | if let Some(index) = focused { |
| 1138 | self.list_cursor = Some(index); |
| 1139 | } |
| 1140 | let modifiers = ui.input(|i| i.modifiers); |
| 1141 | let ctrl = modifiers.ctrl || modifiers.command; |
| 1142 | if ui.input(|i| i.key_pressed(egui::Key::Escape)) { |
| 1143 | self.model.selected.clear(); |
| 1144 | self.marquee = None; |
| 1145 | background.request_focus(); |
| 1146 | } |
| 1147 | if ctrl && ui.input(|i| i.key_pressed(egui::Key::A)) { |
| 1148 | self.model.selected = rows |
| 1149 | .iter() |
| 1150 | .flat_map(|row| row.ids.iter().copied()) |
| 1151 | .collect(); |
| 1152 | self.list_cursor = (!rows.is_empty()).then_some(0); |
| 1153 | self.list_anchor = self.list_cursor; |
| 1154 | } |
| 1155 | if !rows.is_empty() { |
| 1156 | let direction = if ui.input(|i| i.key_pressed(egui::Key::ArrowDown)) { |
| 1157 | Some(1isize) |
| 1158 | } else if ui.input(|i| i.key_pressed(egui::Key::ArrowUp)) { |
| 1159 | Some(-1) |
| 1160 | } else { |
| 1161 | None |
| 1162 | }; |
| 1163 | if let Some(direction) = direction { |
| 1164 | let index = self.list_cursor.map_or(0, |index| { |
| 1165 | index.saturating_add_signed(direction).min(rows.len() - 1) |
| 1166 | }); |
| 1167 | if ctrl && !modifiers.shift { |
| 1168 | self.list_cursor = Some(index); |
| 1169 | } else { |
| 1170 | self.select_row(&rows, index, ctrl, modifiers.shift); |
| 1171 | } |
| 1172 | background.request_focus(); |
| 1173 | let top = index as f32 * LIST_ROW; |
| 1174 | if top < self.list_offset { |
| 1175 | self.list_offset = top; |
| 1176 | } else if top + LIST_ROW > self.list_offset + output.inner_rect.height() { |
| 1177 | self.list_offset = top + LIST_ROW - output.inner_rect.height(); |
| 1178 | } |
| 1179 | ui.ctx().request_repaint(); |
| 1180 | } |
| 1181 | if ui.input(|i| i.key_pressed(egui::Key::Enter)) { |
| 1182 | let index = self |
| 1183 | .list_cursor |
| 1184 | .or(focused) |
| 1185 | .unwrap_or(0) |
| 1186 | .min(rows.len() - 1); |
| 1187 | if rows[index].directory { |
| 1188 | open = Some(rows[index].path.clone()); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | if ui.input(|i| i.key_pressed(egui::Key::Backspace)) && !self.folder.is_empty() { |
| 1193 | open = Some( |
| 1194 | self.folder |
| 1195 | .rsplit_once('/') |
| 1196 | .map_or("", |(parent, _)| parent) |
| 1197 | .to_owned(), |
| 1198 | ); |
| 1199 | } |
| 1200 | } |
| 1201 | if let Some(folder) = open { |
| 1202 | self.browse_folder(&folder); |
| 1203 | background.request_focus(); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | fn clear_list_selection(&mut self) { |
| 1208 | self.model.selected.clear(); |
| 1209 | self.list_anchor = None; |
| 1210 | self.list_cursor = None; |
| 1211 | self.list_offset = 0.0; |
| 1212 | self.marquee = None; |
| 1213 | } |
| 1214 | |
| 1215 | fn select_row(&mut self, rows: &[browser::Row], index: usize, ctrl: bool, shift: bool) { |
| 1216 | if shift { |
| 1217 | let anchor = self |
| 1218 | .list_anchor |
| 1219 | .unwrap_or(self.list_cursor.unwrap_or(index)) |
| 1220 | .min(rows.len() - 1); |
| 1221 | if !ctrl { |
| 1222 | self.model.selected.clear(); |
| 1223 | } |
| 1224 | for row in &rows[anchor.min(index)..=anchor.max(index)] { |
| 1225 | self.model.selected.extend(&row.ids); |
| 1226 | } |
| 1227 | self.list_anchor = Some(anchor); |
| 1228 | } else { |
| 1229 | if !ctrl { |
| 1230 | self.model.selected.clear(); |
| 1231 | } |
| 1232 | if ctrl && rows[index].ids.is_subset(&self.model.selected) { |
| 1233 | // 폴더 이동 시 선택을 지우므로 현재 행의 ID만 제거함. |
| 1234 | self.model |
| 1235 | .selected |
| 1236 | .retain(|id| !rows[index].ids.contains(id)); |
| 1237 | } else { |
| 1238 | self.model.selected.extend(&rows[index].ids); |
| 1239 | } |
| 1240 | self.list_anchor = Some(index); |
| 1241 | } |
| 1242 | self.list_cursor = Some(index); |
| 1243 | } |
| 1244 | |
| 1245 | fn result_visible(&self) -> bool { |
| 1246 | !self.dismissed |
| 1247 | && !self.model.busy() |
| 1248 | && self.last_request.as_ref().is_some_and(|request| { |
| 1249 | !matches!(request, Start::List { .. }) |
| 1250 | || self.model.error.is_some() |
| 1251 | || self |
| 1252 | .model |
| 1253 | .terminal |
| 1254 | .as_ref() |
| 1255 | .is_some_and(|t| t.outcome != Outcome::Completed) |
| 1256 | }) |
| 1257 | } |
| 1258 | |
| 1259 | fn error_notice(ui: &mut egui::Ui, error: &str) { |
| 1260 | let response = theme::panel(theme::ERROR_BG) |
| 1261 | .show(ui, |ui| { |
| 1262 | ui.horizontal_wrapped(|ui| { |
| 1263 | theme::icon(ui, Icon::Error, 24.0); |
| 1264 | ui.colored_label(theme::ERROR, error); |
| 1265 | }); |
| 1266 | }) |
| 1267 | .response; |
| 1268 | response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Alert, true, error)); |
| 1269 | } |
| 1270 | |
| 1271 | fn work_visible(&self) -> bool { |
| 1272 | self.model.busy() || self.result_visible() || (self.ui_error.is_some() && !self.create) |
| 1273 | } |
| 1274 | |
| 1275 | fn work_window(&mut self, ctx: &egui::Context) { |
| 1276 | if !self.work_visible() { |
| 1277 | self.work = WorkWindow::default(); |
| 1278 | return; |
| 1279 | } |
| 1280 | if ctx.embed_viewports() { |
| 1281 | // 별도 창을 만들 수 없는 환경(시험 등)은 show()에서 하단에 그림. |
| 1282 | return; |
| 1283 | } |
| 1284 | if self.model.challenge.is_some() { |
| 1285 | // 암호 입력 창은 메인 창 가운데에 뜨므로, 위에 떠 있는 진행 창이 입력란을 |
| 1286 | // 가리지 않게 그동안 진행 창을 닫음. 부모 기준 위치는 기억해 두고 다시 엶. |
| 1287 | self.work.open = false; |
| 1288 | return; |
| 1289 | } |
| 1290 | let id = egui::ViewportId::from_hash_of("archive-work"); |
| 1291 | let (parent, parent_focused, now) = ctx.input(|i| { |
| 1292 | ( |
| 1293 | i.viewport().outer_rect, |
| 1294 | i.viewport().focused.unwrap_or(false), |
| 1295 | i.time, |
| 1296 | ) |
| 1297 | }); |
| 1298 | let (child, child_focused) = ctx.input(|i| { |
| 1299 | i.raw.viewports.get(&id).map_or((None, false), |v| { |
| 1300 | (v.outer_rect, v.focused.unwrap_or(false)) |
| 1301 | }) |
| 1302 | }); |
| 1303 | let size = egui::vec2(WORK_WIDTH, self.work.height); |
| 1304 | if !self.work.open { |
| 1305 | // 처음 열 때 부모 창 가운데에 둠. 암호 입력 뒤 다시 열 때는 기억한 위치에 둠. |
| 1306 | // Wayland처럼 위치를 알 수 없으면 창 관리자에 맡김. |
| 1307 | self.work.open = true; |
| 1308 | self.work.initial = parent.map(|p| match self.work.offset { |
| 1309 | Some(offset) => (p.min + offset).round(), |
| 1310 | None => (p.center() - size / 2.0).round(), |
| 1311 | }); |
| 1312 | self.work.offset = self.work.initial.zip(parent).map(|(c, p)| c - p.min); |
| 1313 | self.work.parent = parent.map(|p| p.min); |
| 1314 | self.work.expected = self.work.initial; |
| 1315 | self.work.settle_until = now + 0.5; |
| 1316 | } |
| 1317 | if let (Some(parent), Some(child)) = (parent, child) { |
| 1318 | let parent_moved = self |
| 1319 | .work |
| 1320 | .parent |
| 1321 | .is_some_and(|p| (p - parent.min).length() > 0.5); |
| 1322 | let settled = self |
| 1323 | .work |
| 1324 | .expected |
| 1325 | .is_some_and(|e| (child.min - e).length() <= 2.0); |
| 1326 | if settled || now >= self.work.settle_until { |
| 1327 | self.work.settle_until = 0.0; |
| 1328 | } |
| 1329 | if parent_moved { |
| 1330 | if let Some(offset) = self.work.offset { |
| 1331 | let target = (parent.min + offset).round(); |
| 1332 | ctx.send_viewport_cmd_to(id, egui::ViewportCommand::OuterPosition(target)); |
| 1333 | self.work.expected = Some(target); |
| 1334 | self.work.settle_until = now + 0.5; |
| 1335 | } |
| 1336 | } else if self.work.settle_until == 0.0 && !settled { |
| 1337 | // 사용자가 자식 창을 옮긴 위치를 부모 기준으로 기억함. |
| 1338 | self.work.offset = Some(child.min - parent.min); |
| 1339 | self.work.expected = Some(child.min); |
| 1340 | } |
| 1341 | self.work.parent = Some(parent.min); |
| 1342 | } |
| 1343 | let title = if self.model.busy() { |
| 1344 | present::activity_text( |
| 1345 | &self.model, |
| 1346 | matches!(self.last_request, Some(Start::CreateZip { .. })), |
| 1347 | ) |
| 1348 | .to_owned() |
| 1349 | } else { |
| 1350 | "작업 결과".to_owned() |
| 1351 | }; |
| 1352 | let window_title = format!("{title} — Zipelo"); |
| 1353 | let custom_frame = self.custom_frame; |
| 1354 | let mut builder = egui::ViewportBuilder::default() |
| 1355 | .with_title(window_title.clone()) |
| 1356 | .with_decorations(!custom_frame) |
| 1357 | .with_inner_size(size) |
| 1358 | .with_resizable(false) |
| 1359 | .with_minimize_button(false) |
| 1360 | .with_maximize_button(false) |
| 1361 | .with_taskbar(false) |
| 1362 | // 앱을 쓰는 동안에만 부모 창 위에 유지함. |
| 1363 | .with_window_level(if parent_focused || child_focused { |
| 1364 | egui::WindowLevel::AlwaysOnTop |
| 1365 | } else { |
| 1366 | egui::WindowLevel::Normal |
| 1367 | }); |
| 1368 | if let Some(position) = self.work.initial { |
| 1369 | builder = builder.with_position(position); |
| 1370 | } |
| 1371 | let height = ctx.show_viewport_immediate(id, builder, |ui, class| { |
| 1372 | if ui.ctx().input(|i| i.viewport().close_requested()) { |
| 1373 | if self.model.busy() { |
| 1374 | ui.ctx() |
| 1375 | .send_viewport_cmd(egui::ViewportCommand::CancelClose); |
| 1376 | } else { |
| 1377 | self.dismissed = true; |
| 1378 | self.ui_error = None; |
| 1379 | self.model.input_error = None; |
| 1380 | } |
| 1381 | } |
| 1382 | if class == egui::ViewportClass::EmbeddedWindow { |
| 1383 | ui.set_width(WORK_WIDTH); |
| 1384 | self.work_area(ui); |
| 1385 | return None; |
| 1386 | } |
| 1387 | let mut chrome = 0.0; |
| 1388 | if custom_frame { |
| 1389 | window_frame::title_bar( |
| 1390 | ui, |
| 1391 | "work", |
| 1392 | &window_title, |
| 1393 | window_frame::Buttons { |
| 1394 | minimize: false, |
| 1395 | maximize: false, |
| 1396 | }, |
| 1397 | ); |
| 1398 | window_frame::edges(ui.ctx(), false); |
| 1399 | chrome = window_frame::TITLE_HEIGHT; |
| 1400 | } |
| 1401 | let margin = theme::PAD as f32; |
| 1402 | egui::CentralPanel::default() |
| 1403 | .frame(theme::panel(theme::PANEL).corner_radius(0)) |
| 1404 | .show(ui, |ui| { |
| 1405 | let content = ui.vertical(|ui| self.work_area(ui)).response.rect; |
| 1406 | Some(content.height() + 2.0 * margin + chrome) |
| 1407 | }) |
| 1408 | .inner |
| 1409 | }); |
| 1410 | if let Some(height) = height { |
| 1411 | let height = height.clamp(WORK_MIN_HEIGHT, WORK_MAX_HEIGHT).ceil(); |
| 1412 | if (height - self.work.height).abs() > 1.0 { |
| 1413 | self.work.height = height; |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | fn work_area(&mut self, ui: &mut egui::Ui) { |
| 1419 | // 선택 창 안내 모달은 메인 창만 막으므로, 별도 진행 창의 버튼은 여기서 막음. |
| 1420 | let enabled = self.picker.is_none(); |
| 1421 | ui.add_enabled_ui(enabled, |ui| self.work_contents(ui)); |
| 1422 | } |
| 1423 | |
| 1424 | fn work_contents(&mut self, ui: &mut egui::Ui) { |
| 1425 | if self.model.busy() { |
| 1426 | self.progress(ui); |
| 1427 | } else if self.result_visible() { |
| 1428 | self.result(ui); |
| 1429 | } |
| 1430 | if !self.create |
| 1431 | && let Some(error) = &self.ui_error |
| 1432 | { |
| 1433 | Self::error_notice(ui, error); |
| 1434 | if error == present::FOLDER_ERROR |
| 1435 | && theme::action(ui, "폴더 바꾸기", false, true).clicked() |
| 1436 | { |
| 1437 | self.open_picker(Purpose::Destination); |
| 1438 | } |
| 1439 | } |
| 1440 | if let Some(error) = &self.model.input_error { |
| 1441 | ui.colored_label( |
| 1442 | theme::ERROR, |
| 1443 | if error.starts_with("암호는") { |
| 1444 | error.as_str() |
| 1445 | } else { |
| 1446 | if matches!(self.last_request, Some(Start::CreateZip { .. })) { |
| 1447 | "저장 위치와 추가한 항목을 확인하세요" |
| 1448 | } else { |
| 1449 | "풀 위치와 선택한 항목을 확인하세요" |
| 1450 | } |
| 1451 | }, |
| 1452 | ); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | fn progress(&mut self, ui: &mut egui::Ui) { |
| 1457 | let creating = matches!(self.last_request, Some(Start::CreateZip { .. })); |
| 1458 | let cancelling = self.model.cancellation_requested() || self.model.cancel.is_some(); |
| 1459 | let title = present::activity_text(&self.model, creating); |
| 1460 | ui.strong(title); |
| 1461 | if let Some((stage, done, total, count)) = self.model.progress { |
| 1462 | // 원본을 읽는 단계에서는 아직 처리한 항목이 없으므로 개수를 표시하지 않음. |
| 1463 | let processed = if stage == Stage::Read { |
| 1464 | String::new() |
| 1465 | } else { |
| 1466 | format!("{count}개 처리함, ") |
| 1467 | }; |
| 1468 | if let Some(total) = total.filter(|n| *n > 0) { |
| 1469 | ui.scope(|ui| { |
| 1470 | ui.visuals_mut().extreme_bg_color = theme::BORDER; |
| 1471 | ui.add( |
| 1472 | egui::ProgressBar::new((done as f64 / total as f64).clamp(0.0, 1.0) as f32) |
| 1473 | .fill(theme::BLUE) |
| 1474 | .desired_height(theme::SPACE * 2.0), |
| 1475 | ); |
| 1476 | }); |
| 1477 | ui.horizontal(|ui| { |
| 1478 | ui.label(format!( |
| 1479 | "{:.0}% / {processed}{} / {}", |
| 1480 | (done as f64 / total as f64).clamp(0.0, 1.0) * 100.0, |
| 1481 | present::bytes_text(done), |
| 1482 | present::bytes_text(total) |
| 1483 | )); |
| 1484 | self.cancel_action(ui, cancelling); |
| 1485 | }); |
| 1486 | } else { |
| 1487 | theme::indeterminate(ui); |
| 1488 | ui.horizontal(|ui| { |
| 1489 | ui.label(format!("{processed}{}", present::bytes_text(done))); |
| 1490 | self.cancel_action(ui, cancelling); |
| 1491 | }); |
| 1492 | } |
| 1493 | } else { |
| 1494 | theme::indeterminate(ui); |
| 1495 | ui.horizontal(|ui| { |
| 1496 | ui.label("작업을 진행하고 있습니다"); |
| 1497 | self.cancel_action(ui, cancelling); |
| 1498 | }); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | fn cancel_action(&mut self, ui: &mut egui::Ui, cancelling: bool) { |
| 1503 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 1504 | if theme::action(ui, "취소", false, !cancelling).clicked() { |
| 1505 | self.clear_password(); |
| 1506 | self.model.cancel(); |
| 1507 | } |
| 1508 | }); |
| 1509 | } |
| 1510 | |
| 1511 | fn result(&mut self, ui: &mut egui::Ui) { |
| 1512 | let t = self.model.terminal.clone(); |
| 1513 | let listing = matches!(self.last_request, Some(Start::List { .. })); |
| 1514 | let creating = matches!(self.last_request, Some(Start::CreateZip { .. })); |
| 1515 | let warnings = if listing { |
| 1516 | Vec::new() |
| 1517 | } else { |
| 1518 | present::publication_warnings( |
| 1519 | t.as_ref(), |
| 1520 | self.model.confirmed, |
| 1521 | self.targets.as_ref(), |
| 1522 | &self.directories, |
| 1523 | &self.model.outputs, |
| 1524 | ) |
| 1525 | }; |
| 1526 | let success = self.model.confirmed |
| 1527 | && self.model.error.is_none() |
| 1528 | && t.as_ref().is_some_and(|t| t.outcome == Outcome::Completed) |
| 1529 | && warnings.is_empty(); |
| 1530 | let durability_warning = t |
| 1531 | .as_ref() |
| 1532 | .filter(|_| self.model.confirmed && !listing) |
| 1533 | .and_then(|t| present::durability_warning(t.durability)); |
| 1534 | let durability_in_details = success |
| 1535 | && t.as_ref() |
| 1536 | .is_some_and(|t| t.durability == Durability::Unconfirmed); |
| 1537 | let cancelled = |
| 1538 | self.model.confirmed && t.as_ref().is_some_and(|t| t.outcome == Outcome::Cancelled); |
| 1539 | let title = if success { |
| 1540 | if creating { |
| 1541 | "ZIP 파일을 만들었습니다" |
| 1542 | } else { |
| 1543 | "압축을 풀었습니다" |
| 1544 | } |
| 1545 | } else if cancelled { |
| 1546 | "작업을 취소했습니다" |
| 1547 | } else if listing { |
| 1548 | "압축파일을 열지 못했습니다" |
| 1549 | } else if creating { |
| 1550 | "ZIP 파일을 만들지 못했습니다" |
| 1551 | } else { |
| 1552 | "압축을 풀지 못했습니다" |
| 1553 | }; |
| 1554 | let fill = if success { |
| 1555 | theme::SUCCESS_BG |
| 1556 | } else if cancelled { |
| 1557 | theme::STRIPE |
| 1558 | } else { |
| 1559 | theme::ERROR_BG |
| 1560 | }; |
| 1561 | let mut choose = false; |
| 1562 | let mut retry = false; |
| 1563 | let mut reopen = false; |
| 1564 | let mut folder = false; |
| 1565 | theme::panel(fill).show(ui, |ui| { |
| 1566 | ui.horizontal(|ui| { |
| 1567 | theme::icon( |
| 1568 | ui, |
| 1569 | if success { |
| 1570 | Icon::Success |
| 1571 | } else if cancelled { |
| 1572 | Icon::Warning |
| 1573 | } else { |
| 1574 | Icon::Error |
| 1575 | }, |
| 1576 | 24.0, |
| 1577 | ); |
| 1578 | ui.strong(title); |
| 1579 | }); |
| 1580 | let max_height = WORK_RESULT_MAX_HEIGHT; |
| 1581 | egui::ScrollArea::vertical() |
| 1582 | .id_salt("result-message") |
| 1583 | .max_height(max_height) |
| 1584 | .show(ui, |ui| { |
| 1585 | if success { |
| 1586 | let path = match self.last_request.as_ref() { |
| 1587 | Some(Start::CreateZip { output_file, .. }) => { |
| 1588 | output_file.display().to_string() |
| 1589 | } |
| 1590 | _ => format!("{} 폴더에 저장했습니다.", self.destination.display()), |
| 1591 | }; |
| 1592 | theme::bounded_text(ui, &path, ui.available_width()); |
| 1593 | } else if let Some(code) = t.as_ref().and_then(|t| t.error) { |
| 1594 | ui.label(if creating && code == ErrorCode::SourceChanged { |
| 1595 | "묶을 파일이 바뀌었습니다. 파일을 확인한 뒤 다시 만드세요".to_owned() |
| 1596 | } else { |
| 1597 | present::operation_text(present::error_text(code), creating) |
| 1598 | }); |
| 1599 | } else if let Some(error) = &self.model.error { |
| 1600 | ui.label(present::operation_text( |
| 1601 | present::client_error_text(error), |
| 1602 | creating, |
| 1603 | )); |
| 1604 | } |
| 1605 | for warning in warnings |
| 1606 | .iter() |
| 1607 | .copied() |
| 1608 | .chain(durability_warning.filter(|_| !durability_in_details)) |
| 1609 | { |
| 1610 | ui.horizontal_wrapped(|ui| { |
| 1611 | theme::icon(ui, Icon::Warning, 20.0); |
| 1612 | ui.colored_label( |
| 1613 | theme::WARNING, |
| 1614 | present::operation_text(warning, creating), |
| 1615 | ); |
| 1616 | }); |
| 1617 | } |
| 1618 | if !success |
| 1619 | && !listing |
| 1620 | && present::definitely_unsaved( |
| 1621 | t.as_ref(), |
| 1622 | self.model.confirmed, |
| 1623 | &self.model.outputs, |
| 1624 | ) |
| 1625 | { |
| 1626 | ui.label(if creating { |
| 1627 | "ZIP 파일을 만들지 않았습니다" |
| 1628 | } else { |
| 1629 | "아무 파일도 풀지 않았습니다" |
| 1630 | }); |
| 1631 | } |
| 1632 | if self.details { |
| 1633 | theme::panel(theme::STRIPE).show(ui, |ui| { |
| 1634 | if durability_in_details { |
| 1635 | if let Some(warning) = durability_warning { |
| 1636 | ui.label(present::operation_text(warning, creating)); |
| 1637 | } |
| 1638 | } |
| 1639 | if let Some(t) = &t { |
| 1640 | for line in terminal_lines(t) { |
| 1641 | ui.label(line); |
| 1642 | } |
| 1643 | } |
| 1644 | if let Some(error) = &self.model.error { |
| 1645 | ui.label(error); |
| 1646 | } |
| 1647 | for (name, state) in &self.model.outputs { |
| 1648 | theme::bounded_text( |
| 1649 | ui, |
| 1650 | &format!("{name}: {}", disposition_label(*state)), |
| 1651 | ui.available_width(), |
| 1652 | ); |
| 1653 | } |
| 1654 | }); |
| 1655 | } |
| 1656 | }); |
| 1657 | let conflict = t |
| 1658 | .as_ref() |
| 1659 | .is_some_and(|t| t.error == Some(ErrorCode::Conflict)) |
| 1660 | && !creating; |
| 1661 | let uncertain = warnings.contains(&present::UNCERTAIN); |
| 1662 | if conflict && uncertain { |
| 1663 | let response = ui.checkbox( |
| 1664 | &mut self.checked_destination, |
| 1665 | "기존 풀 위치 폴더를 확인했습니다", |
| 1666 | ); |
| 1667 | theme::focus(&response); |
| 1668 | } |
| 1669 | ui.horizontal(|ui| { |
| 1670 | if theme::action( |
| 1671 | ui, |
| 1672 | if self.details { |
| 1673 | "자세히 닫기" |
| 1674 | } else { |
| 1675 | "자세히 보기" |
| 1676 | }, |
| 1677 | false, |
| 1678 | true, |
| 1679 | ) |
| 1680 | .clicked() |
| 1681 | { |
| 1682 | self.details = !self.details; |
| 1683 | } |
| 1684 | let action = if success { |
| 1685 | Some(("폴더 열기", true)) |
| 1686 | } else if conflict { |
| 1687 | Some(( |
| 1688 | if self |
| 1689 | .model |
| 1690 | .list_id |
| 1691 | .as_ref() |
| 1692 | .is_some_and(|id| id.format == Format::Gzip) |
| 1693 | { |
| 1694 | "다른 이름으로 저장" |
| 1695 | } else { |
| 1696 | "다른 폴더에 풀기" |
| 1697 | }, |
| 1698 | !uncertain || self.checked_destination, |
| 1699 | )) |
| 1700 | } else if t |
| 1701 | .as_ref() |
| 1702 | .is_some_and(|t| t.error == Some(ErrorCode::SourceChanged)) |
| 1703 | { |
| 1704 | Some(( |
| 1705 | if creating { |
| 1706 | "다시 만들기" |
| 1707 | } else { |
| 1708 | "압축파일 다시 열기" |
| 1709 | }, |
| 1710 | true, |
| 1711 | )) |
| 1712 | } else if !cancelled && (!uncertain || listing) { |
| 1713 | Some(("다시 시도", true)) |
| 1714 | } else { |
| 1715 | None |
| 1716 | }; |
| 1717 | let (confirm, primary) = theme::action_pair(ui, "확인", action); |
| 1718 | if confirm { |
| 1719 | self.dismissed = true; |
| 1720 | self.ui_error = None; |
| 1721 | self.model.input_error = None; |
| 1722 | } |
| 1723 | if primary { |
| 1724 | match action.map(|(label, _)| label) { |
| 1725 | Some("폴더 열기") => folder = true, |
| 1726 | Some("다른 폴더에 풀기" | "다른 이름으로 저장") => { |
| 1727 | choose = true |
| 1728 | } |
| 1729 | Some("압축파일 다시 열기") => reopen = true, |
| 1730 | Some("다시 시도" | "다시 만들기") => retry = true, |
| 1731 | _ => {} |
| 1732 | } |
| 1733 | } |
| 1734 | }); |
| 1735 | }); |
| 1736 | if choose { |
| 1737 | self.open_picker(Purpose::Retry); |
| 1738 | } |
| 1739 | if retry { |
| 1740 | self.retry(); |
| 1741 | } |
| 1742 | if reopen { |
| 1743 | if let Some(path) = self.model.archive.clone() { |
| 1744 | self.open_path(path); |
| 1745 | } |
| 1746 | } |
| 1747 | if folder { |
| 1748 | let path = match self.last_request.as_ref() { |
| 1749 | Some(Start::CreateZip { output_file, .. }) => { |
| 1750 | output_file.parent().unwrap_or(Path::new("/")) |
| 1751 | } |
| 1752 | _ => self.destination.as_path(), |
| 1753 | }; |
| 1754 | match present::folder_command(path).spawn() { |
| 1755 | Ok(mut child) => { |
| 1756 | std::thread::spawn(move || { |
| 1757 | let _ = child.wait(); |
| 1758 | }); |
| 1759 | } |
| 1760 | Err(_) => { |
| 1761 | self.ui_error = Some( |
| 1762 | "폴더를 열지 못했습니다. 파일 탐색기에서 저장 위치를 확인하세요".into(), |
| 1763 | ) |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | fn create_dialog(&mut self, ctx: &egui::Context) { |
| 1770 | let mut compress = false; |
| 1771 | let mut pick = None; |
| 1772 | let mut cancel = false; |
| 1773 | let modal = egui::Modal::new(egui::Id::unique("create-zip")).show(ctx, |ui| { |
| 1774 | ui.set_width((ctx.content_rect().width() - 64.0).min(600.0)); |
| 1775 | ui.horizontal(|ui| { |
| 1776 | ui.heading("ZIP 만들기"); |
| 1777 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 1778 | let close = theme::icon_button(ui, "닫기", theme::CLOSE, true); |
| 1779 | theme::focus(&close); |
| 1780 | cancel = close.clicked(); |
| 1781 | }); |
| 1782 | }); |
| 1783 | if let Some(error) = &self.ui_error { |
| 1784 | Self::error_notice(ui, error); |
| 1785 | } |
| 1786 | egui::ScrollArea::vertical() |
| 1787 | .id_salt("create-fields") |
| 1788 | .max_height( |
| 1789 | (ctx.content_rect().height() |
| 1790 | - 180.0 |
| 1791 | - if self.ui_error.is_some() { 96.0 } else { 0.0 }) |
| 1792 | .max(120.0), |
| 1793 | ) |
| 1794 | .show(ui, |ui| { |
| 1795 | ui.add_space(16.0); |
| 1796 | let frame = theme::panel(theme::STRIPE) |
| 1797 | .stroke(egui::Stroke::new(1.0, theme::DASH)) |
| 1798 | .inner_margin(egui::Margin::same(24)); |
| 1799 | let area = frame |
| 1800 | .show(ui, |ui| { |
| 1801 | ui.set_min_size(egui::vec2(ui.available_width(), 144.0)); |
| 1802 | ui.vertical_centered(|ui| { |
| 1803 | theme::icon(ui, Icon::File, 44.0); |
| 1804 | ui.add_space(16.0); |
| 1805 | ui.label( |
| 1806 | RichText::new("파일이나 폴더를 여기에 끌어 놓으세요") |
| 1807 | .size(18.0), |
| 1808 | ); |
| 1809 | }); |
| 1810 | }) |
| 1811 | .response; |
| 1812 | let drop = ui.interact( |
| 1813 | area.rect, |
| 1814 | egui::Id::unique("zip-source-drop"), |
| 1815 | egui::Sense::click(), |
| 1816 | ); |
| 1817 | theme::focus(&drop); |
| 1818 | drop.widget_info(|| { |
| 1819 | egui::WidgetInfo::labeled(egui::Role::Button, true, "파일 추가") |
| 1820 | }); |
| 1821 | if drop.clicked() { |
| 1822 | pick = Some(Purpose::Files); |
| 1823 | } |
| 1824 | egui::ScrollArea::vertical() |
| 1825 | .id_salt("zip-sources") |
| 1826 | .max_height(86.0) |
| 1827 | .show(ui, |ui| { |
| 1828 | let mut remove = None; |
| 1829 | for (index, path) in self.sources.iter().enumerate() { |
| 1830 | ui.horizontal(|ui| { |
| 1831 | theme::icon( |
| 1832 | ui, |
| 1833 | if path.is_dir() { |
| 1834 | Icon::Folder |
| 1835 | } else { |
| 1836 | Icon::File |
| 1837 | }, |
| 1838 | 20.0, |
| 1839 | ); |
| 1840 | let name = |
| 1841 | path.file_name().unwrap_or_default().to_string_lossy(); |
| 1842 | theme::bounded_text( |
| 1843 | ui, |
| 1844 | &name, |
| 1845 | (ui.available_width() - 72.0).max(0.0), |
| 1846 | ) |
| 1847 | .on_hover_text(path.display().to_string()); |
| 1848 | if ui.button("빼기").clicked() { |
| 1849 | remove = Some(index); |
| 1850 | } |
| 1851 | }); |
| 1852 | } |
| 1853 | if let Some(index) = remove { |
| 1854 | self.sources.remove(index); |
| 1855 | } |
| 1856 | }); |
| 1857 | ui.horizontal(|ui| { |
| 1858 | if theme::action(ui, "파일 추가", false, true).clicked() { |
| 1859 | pick = Some(Purpose::Files); |
| 1860 | } |
| 1861 | if theme::action(ui, "폴더 추가", false, true).clicked() { |
| 1862 | pick = Some(Purpose::Folder); |
| 1863 | } |
| 1864 | }); |
| 1865 | ui.add_space(16.0); |
| 1866 | ui.horizontal(|ui| { |
| 1867 | theme::icon(ui, Icon::Lock, 24.0); |
| 1868 | ui.strong("암호화"); |
| 1869 | }); |
| 1870 | let check = ui.checkbox(&mut self.aes256, "암호로 보호하기"); |
| 1871 | theme::focus(&check); |
| 1872 | ui.add_space(16.0); |
| 1873 | }); |
| 1874 | let (dismiss, confirm) = |
| 1875 | theme::action_pair(ui, "취소", Some(("압축하기", !self.sources.is_empty()))); |
| 1876 | cancel |= dismiss; |
| 1877 | compress = confirm; |
| 1878 | }); |
| 1879 | if cancel || modal.should_close() { |
| 1880 | self.create = false; |
| 1881 | self.ui_error = None; |
| 1882 | return; |
| 1883 | } |
| 1884 | if let Some(purpose) = pick { |
| 1885 | self.open_picker(purpose); |
| 1886 | } |
| 1887 | if compress { |
| 1888 | self.open_picker(Purpose::SaveZip); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | fn password_dialog(&mut self, ctx: &egui::Context, new_challenge: bool) { |
| 1893 | let (_, attempt, encrypt) = self.model.challenge.unwrap(); |
| 1894 | let mut submit = false; |
| 1895 | let mut cancel = false; |
| 1896 | let password_enter = ctx.memory(|memory| memory.focused()).is_some_and(|id| { |
| 1897 | id == egui::Id::unique("archive-password-input") |
| 1898 | || id == egui::Id::unique("archive-password-confirm") |
| 1899 | }) && ctx.input(|input| input.key_pressed(egui::Key::Enter)); |
| 1900 | let modal = egui::Modal::new(egui::Id::unique("archive-password")).show(ctx, |ui| { |
| 1901 | ui.set_width((ctx.content_rect().width() - 64.0).min(420.0)); |
| 1902 | ui.heading(if encrypt { |
| 1903 | "ZIP 암호 설정" |
| 1904 | } else { |
| 1905 | "암호가 필요합니다" |
| 1906 | }); |
| 1907 | if encrypt { |
| 1908 | ui.label("암호를 잊으면 파일을 열 수 없습니다."); |
| 1909 | } else if let Some(path) = &self.model.archive { |
| 1910 | ui.label(format!( |
| 1911 | "{}은 암호로 보호되어 있습니다.", |
| 1912 | present::middle_text( |
| 1913 | &path.file_name().unwrap_or_default().to_string_lossy(), |
| 1914 | 24 |
| 1915 | ) |
| 1916 | )); |
| 1917 | } |
| 1918 | ui.horizontal(|ui| { |
| 1919 | ui.label("암호"); |
| 1920 | let edit = ui.add( |
| 1921 | egui::TextEdit::singleline(&mut self.password) |
| 1922 | .password(!self.password_visible) |
| 1923 | .id(egui::Id::unique("archive-password-input")) |
| 1924 | .char_limit(1024) |
| 1925 | .desired_width(232.0), |
| 1926 | ); |
| 1927 | theme::focus(&edit); |
| 1928 | if new_challenge { |
| 1929 | edit.request_focus(); |
| 1930 | } |
| 1931 | if theme::action( |
| 1932 | ui, |
| 1933 | if self.password_visible { |
| 1934 | "숨기기" |
| 1935 | } else { |
| 1936 | "보기" |
| 1937 | }, |
| 1938 | false, |
| 1939 | true, |
| 1940 | ) |
| 1941 | .clicked() |
| 1942 | { |
| 1943 | self.password_visible = !self.password_visible; |
| 1944 | } |
| 1945 | }); |
| 1946 | if encrypt { |
| 1947 | ui.horizontal(|ui| { |
| 1948 | ui.label("암호 확인"); |
| 1949 | let edit = ui.add( |
| 1950 | egui::TextEdit::singleline(&mut self.password_confirm) |
| 1951 | .password(!self.password_visible) |
| 1952 | .id(egui::Id::unique("archive-password-confirm")) |
| 1953 | .char_limit(1024) |
| 1954 | .desired_width(232.0), |
| 1955 | ); |
| 1956 | theme::focus(&edit); |
| 1957 | }); |
| 1958 | if self.password != self.password_confirm { |
| 1959 | ui.colored_label(theme::ERROR, "두 암호가 같지 않습니다"); |
| 1960 | } |
| 1961 | } else if attempt > 1 { |
| 1962 | ui.colored_label(theme::ERROR, "암호가 맞지 않습니다. 다시 입력하세요"); |
| 1963 | } |
| 1964 | if self.password.len() > 1024 { |
| 1965 | ui.colored_label(theme::ERROR, "암호는 UTF-8 1~1024바이트로 입력하세요"); |
| 1966 | } |
| 1967 | let valid = !self.password.is_empty() |
| 1968 | && self.password.len() <= 1024 |
| 1969 | && (!encrypt || self.password == self.password_confirm); |
| 1970 | let actions = theme::action_pair(ui, "취소", Some(("확인", valid))); |
| 1971 | cancel = actions.0; |
| 1972 | submit = actions.1 || (valid && password_enter); |
| 1973 | }); |
| 1974 | if modal.should_close() || cancel { |
| 1975 | self.clear_password(); |
| 1976 | self.model.cancel(); |
| 1977 | } else if submit { |
| 1978 | let password = std::mem::take(&mut self.password); |
| 1979 | self.clear_password(); |
| 1980 | self.model.password(password); |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | fn close_dialog(&mut self, ctx: &egui::Context) { |
| 1985 | if self.model.close == Close::Open || self.model.close == Close::Allowed { |
| 1986 | return; |
| 1987 | } |
| 1988 | egui::Modal::new(egui::Id::unique("archive-close")).show(ctx, |ui| { |
| 1989 | ui.set_width(420.0); |
| 1990 | match self.model.close { |
| 1991 | Close::Confirm => { |
| 1992 | ui.heading("작업 중 닫기"); |
| 1993 | ui.label("작업을 취소하고 닫을까요?"); |
| 1994 | ui.horizontal(|ui| { |
| 1995 | if theme::action(ui, "계속 작업", false, true).clicked() { |
| 1996 | self.model.keep_open(); |
| 1997 | } |
| 1998 | if theme::action(ui, "취소하고 닫기", true, true).clicked() { |
| 1999 | self.clear_password(); |
| 2000 | self.model.confirm_close(); |
| 2001 | } |
| 2002 | }); |
| 2003 | } |
| 2004 | Close::Waiting => { |
| 2005 | ui.heading("종료 확인 중"); |
| 2006 | ui.label("마무리하는 중입니다. 잠시만 기다려 주세요"); |
| 2007 | if theme::action(ui, "닫기 취소", false, true).clicked() { |
| 2008 | self.model.keep_open(); |
| 2009 | } |
| 2010 | } |
| 2011 | Close::Review => { |
| 2012 | let creating = matches!(self.last_request, Some(Start::CreateZip { .. })); |
| 2013 | ui.heading("결과 확인 후 닫기"); |
| 2014 | let wrote = self |
| 2015 | .last_request |
| 2016 | .as_ref() |
| 2017 | .is_some_and(|request| !matches!(request, Start::List { .. })); |
| 2018 | ui.label(if !wrote { |
| 2019 | "프로그램을 닫을까요?".to_owned() |
| 2020 | } else if self.model.confirmed { |
| 2021 | "작업 결과를 확인한 뒤 닫으세요".to_owned() |
| 2022 | } else { |
| 2023 | present::operation_text(present::UNCERTAIN, creating) |
| 2024 | }); |
| 2025 | egui::ScrollArea::vertical() |
| 2026 | .max_height(160.0) |
| 2027 | .show(ui, |ui| { |
| 2028 | if let Some(code) = self.model.terminal.as_ref().and_then(|t| t.error) { |
| 2029 | ui.colored_label( |
| 2030 | theme::ERROR, |
| 2031 | present::operation_text(present::error_text(code), creating), |
| 2032 | ); |
| 2033 | } |
| 2034 | if wrote { |
| 2035 | for warning in present::warnings( |
| 2036 | self.model.terminal.as_ref(), |
| 2037 | self.model.confirmed, |
| 2038 | self.targets.as_ref(), |
| 2039 | &self.directories, |
| 2040 | &self.model.outputs, |
| 2041 | ) { |
| 2042 | ui.colored_label( |
| 2043 | theme::WARNING, |
| 2044 | present::operation_text(warning, creating), |
| 2045 | ); |
| 2046 | } |
| 2047 | } |
| 2048 | }); |
| 2049 | ui.horizontal(|ui| { |
| 2050 | if theme::action(ui, "계속 작업", false, true).clicked() { |
| 2051 | self.model.keep_open(); |
| 2052 | } |
| 2053 | if theme::action(ui, "닫기", true, true).clicked() { |
| 2054 | self.model.acknowledge_close(); |
| 2055 | } |
| 2056 | }); |
| 2057 | } |
| 2058 | _ => {} |
| 2059 | } |
| 2060 | }); |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | impl eframe::App for ArchiveApp { |
| 2065 | fn ui(&mut self, root: &mut egui::Ui, _: &mut eframe::Frame) { |
| 2066 | self.show(root); |
| 2067 | } |
| 2068 | |
| 2069 | fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { |
| 2070 | // 운영체제가 창 이동을 맡으면 버튼 놓기가 오지 않으므로 대신 넣어 눌림 상태를 풀어 줌. |
| 2071 | if std::mem::take(&mut self.release_after_move) { |
| 2072 | let pos = ctx.input(|i| i.pointer.latest_pos()).unwrap_or_default(); |
| 2073 | raw_input.events.insert( |
| 2074 | 0, |
| 2075 | egui::Event::PointerButton { |
| 2076 | pos, |
| 2077 | button: egui::PointerButton::Primary, |
| 2078 | pressed: false, |
| 2079 | modifiers: ctx.input(|i| i.modifiers), |
| 2080 | }, |
| 2081 | ); |
| 2082 | } |
| 2083 | } |
| 2084 | } |
| 2085 | impl Drop for ArchiveApp { |
| 2086 | fn drop(&mut self) { |
| 2087 | self.clear_password(); |
| 2088 | } |
| 2089 | } |