KMNExtractPDFWindowController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KMNExtractPDFWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/24.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNExtractPDFWindowController: KMNBaseWindowController {
  10. @IBOutlet var titleLabel: NSTextField!
  11. @IBOutlet var eachButton: ComponentCheckBox!
  12. @IBOutlet var deleteButton: ComponentCheckBox!
  13. @IBOutlet var cancelButton: ComponentButton!
  14. @IBOutlet var extractButton: ComponentButton!
  15. @IBOutlet var cancelWidthButton:NSLayoutConstraint!
  16. @IBOutlet var extractWidthButton:NSLayoutConstraint!
  17. private var orgDocument:CPDFDocument?
  18. private var selectionIndexPaths: Set<IndexPath> = []
  19. var callback: ((Bool, Bool)->Void)?
  20. private func setUpProperty() {
  21. titleLabel.stringValue = KMLocalizedString("Extract")
  22. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  23. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  24. eachButton.properties = ComponentCheckBoxProperty(size: .s,
  25. state: .normal,
  26. isDisabled: false,
  27. showhelp: false,
  28. text: KMLocalizedString("Each page in a separate file"),
  29. checkboxType: .normal)
  30. deleteButton.properties = ComponentCheckBoxProperty(size: .s,
  31. state: .normal,
  32. isDisabled: false,
  33. showhelp: false,
  34. text: KMLocalizedString("Delete pages after extraction"),
  35. checkboxType: .normal)
  36. extractButton.properties = ComponentButtonProperty(type: .primary,
  37. size: .s,
  38. state: .normal,
  39. isDisable: false,
  40. buttonText: KMLocalizedString("Extract"))
  41. extractButton.setTarget(self, action: #selector(extractButtonClicked(_ :)))
  42. extractWidthButton.constant = extractButton.properties.propertyInfo.viewWidth
  43. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  44. size: .s,
  45. state: .normal,
  46. buttonText: KMLocalizedString("Cancel"))
  47. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  48. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  49. eachButton.setTarget(self, action: #selector(eachPDFAction(_:)))
  50. deleteButton.setTarget(self, action: #selector(deletePDFAction(_:)))
  51. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  52. extractWidthButton.constant = extractButton.properties.propertyInfo.viewWidth
  53. }
  54. override func windowDidLoad() {
  55. super.windowDidLoad()
  56. setUpProperty()
  57. if(selectionIndexPaths.count == orgDocument?.pageCount ?? 0) {
  58. deleteButton.properties.isDisabled = true
  59. deleteButton.reloadData()
  60. }
  61. }
  62. convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>) {
  63. self.init(windowNibName: "KMNExtractPDFWindowController")
  64. orgDocument = document
  65. self.selectionIndexPaths = selectionIndexPaths // 修正此行
  66. }
  67. convenience init(_ filePath: String,password:String?) {
  68. self.init(windowNibName: "KMNExtractPDFWindowController")
  69. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  70. if password != nil {
  71. document?.unlock(withPassword: password as String?)
  72. }
  73. orgDocument = document
  74. selectionIndexPaths = [IndexPath(item: 0, section: 0)] // 修正此行
  75. }
  76. //MARK: - Action
  77. @objc func cancelButtonClicked(_ sender: NSView) {
  78. own_closeEndSheet()
  79. }
  80. @objc func extractButtonClicked(_ sender: NSView) {
  81. callback?(eachButton.properties.checkboxType == .selected,deleteButton.properties.checkboxType == .selected)
  82. }
  83. @objc func eachPDFAction(_ sender: NSView) {
  84. }
  85. @objc func deletePDFAction(_ sender: NSView) {
  86. }
  87. }