KMPDFEditPageRangeWindowController.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // KMPDFEditPageRangeWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/10.
  6. //
  7. import Cocoa
  8. class KMPDFEditPageRangeWindowController: NSWindowController {
  9. weak var pdfDocument: CPDFDocument?
  10. var pageSelectCallBack: (([Int]) -> Void)?
  11. @IBOutlet var pageRangeLabel: NSTextField!
  12. @IBOutlet var pageRangeTextField: NSTextField!
  13. @IBOutlet var totalPageCountLabel: NSTextField!
  14. @IBOutlet var detailLabel: NSTextField!
  15. @IBOutlet var cancelButton: NSButton!
  16. @IBOutlet var okButton: NSButton!
  17. private var fileAttri_: KMFileAttribute?
  18. override var windowNibName: NSNib.Name? {
  19. return "KMPDFEditPageRangeWindowController"
  20. }
  21. deinit {
  22. KMPrint("KMPDFEditPageRangeWindowController deinit.")
  23. }
  24. override func windowDidLoad() {
  25. super.windowDidLoad()
  26. self.pageRangeLabel.stringValue = KMLocalizedString("Page Range")
  27. self.pageRangeTextField.placeholderString = KMLocalizedString("e.g. 1,3-5,10")
  28. // self.totalPageCountLabel.integerValue = self.pdfDocument.pageCount;
  29. self.totalPageCountLabel.stringValue = String(format: KMLocalizedString(", Total %ld Pages"), self.pdfDocument?.pageCount ?? 0)
  30. self.detailLabel.stringValue = ""
  31. self.cancelButton.title = KMLocalizedString("Cancel")
  32. self.okButton.title = KMLocalizedString("OK")
  33. self.window?.title = KMLocalizedString("Page Range")
  34. self.fileAttri_ = KMFileAttribute()
  35. self.fileAttri_?.filePath = self.pdfDocument?.documentURL.path ?? ""
  36. if let cnt = self.pdfDocument?.pageCount {
  37. self.fileAttri_?.pageCnt = Int(cnt)
  38. }
  39. self.fileAttri_?.bAllPage = false
  40. self.fileAttri_?.pagesType = .custom
  41. }
  42. @IBAction func buttonClicked_Cancel(_ sender: AnyObject?) {
  43. if let win = self.window {
  44. self.window?.sheetParent?.endSheet(win)
  45. }
  46. }
  47. @IBAction func buttonClicked_Done(_ sender: AnyObject?) {
  48. self.fileAttri_?.pagesString = self.pageRangeTextField.stringValue
  49. if self.fileAttri_ == nil || self.fileAttri_!.fetchSelectPages().isEmpty {
  50. let alert = NSAlert()
  51. alert.alertStyle = .critical
  52. alert.messageText = String(format: "%@ %@", self.pdfDocument?.documentURL.lastPathComponent.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  53. // alert.runModal()
  54. alert.beginSheetModal(for: self.window!) { res in
  55. self.window?.makeFirstResponder(self.pageRangeTextField)
  56. }
  57. return
  58. } else {
  59. if (self.pageSelectCallBack != nil) {
  60. self.pageSelectCallBack!(self.fileAttri_?.fetchSelectPages() ?? [])
  61. }
  62. }
  63. }
  64. }