KMNExtractPDFWindowController.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. override func updateUIThemeColor() {
  21. super.updateUIThemeColor()
  22. window?.contentView?.wantsLayer = true
  23. window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup").cgColor
  24. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  25. deleteButton.reloadData()
  26. eachButton.reloadData()
  27. cancelButton.reloadData()
  28. extractButton.reloadData()
  29. }
  30. override func updateUILanguage() {
  31. super.updateUILanguage()
  32. titleLabel.stringValue = KMLocalizedString("Extract")
  33. eachButton.properties.text = KMLocalizedString("Each page in a separate file")
  34. deleteButton.properties.text = KMLocalizedString("Delete pages after extraction")
  35. deleteButton.reloadData()
  36. eachButton.reloadData()
  37. cancelButton.properties.buttonText = KMLocalizedString("Cancel")
  38. extractButton.properties.buttonText = KMLocalizedString("Extract")
  39. cancelButton.reloadData()
  40. extractButton.reloadData()
  41. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  42. extractWidthButton.constant = extractButton.properties.propertyInfo.viewWidth
  43. }
  44. override func initContentView() {
  45. super.initContentView()
  46. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  47. eachButton.properties = ComponentCheckBoxProperty(size: .s,
  48. state: .normal,
  49. isDisabled: false,
  50. showhelp: false,
  51. text: KMLocalizedString("Each page in a separate file"),
  52. checkboxType: .normal)
  53. deleteButton.properties = ComponentCheckBoxProperty(size: .s,
  54. state: .normal,
  55. isDisabled: false,
  56. showhelp: false,
  57. text: KMLocalizedString("Delete pages after extraction"),
  58. checkboxType: .normal)
  59. extractButton.properties = ComponentButtonProperty(type: .primary,
  60. size: .s,
  61. state: .normal,
  62. isDisable: false,
  63. buttonText: KMLocalizedString("Extract"),keepPressState: false)
  64. extractButton.setTarget(self, action: #selector(extractButtonClicked(_ :)))
  65. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  66. size: .s,
  67. state: .normal,
  68. buttonText: KMLocalizedString("Cancel"))
  69. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  70. eachButton.setTarget(self, action: #selector(eachPDFAction(_:)))
  71. deleteButton.setTarget(self, action: #selector(deletePDFAction(_:)))
  72. }
  73. override func windowDidLoad() {
  74. super.windowDidLoad()
  75. if(selectionIndexPaths.count == orgDocument?.pageCount ?? 0) {
  76. deleteButton.properties.isDisabled = true
  77. deleteButton.reloadData()
  78. }
  79. }
  80. convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>) {
  81. self.init(windowNibName: "KMNExtractPDFWindowController")
  82. orgDocument = document
  83. self.selectionIndexPaths = selectionIndexPaths // 修正此行
  84. }
  85. convenience init(_ filePath: String,password:String?) {
  86. self.init(windowNibName: "KMNExtractPDFWindowController")
  87. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  88. if password != nil {
  89. document?.unlock(withPassword: password as String?)
  90. }
  91. orgDocument = document
  92. selectionIndexPaths = [IndexPath(item: 0, section: 0)] // 修正此行
  93. }
  94. //MARK: - Action
  95. @objc func cancelButtonClicked(_ sender: NSView) {
  96. own_closeEndSheet()
  97. }
  98. @objc func extractButtonClicked(_ sender: NSView) {
  99. callback?(eachButton.properties.checkboxType == .selected,deleteButton.properties.checkboxType == .selected)
  100. }
  101. @objc func eachPDFAction(_ sender: NSView) {
  102. }
  103. @objc func deletePDFAction(_ sender: NSView) {
  104. }
  105. }