KMNPDFInsertPDFWindowController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // KMNPDFInsertPDFWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/22.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNPDFInsertPDFWindowController: NSWindowController {
  10. private var parentWindow: NSWindow?
  11. private var handler: ((String?) -> Void)!
  12. @IBOutlet var titleLabel: NSTextField!
  13. @IBOutlet var fileLabel: NSTextField!
  14. @IBOutlet var fileNameInput: ComponentInputWithAddon!
  15. @IBOutlet var selectFileInput: ComponentInputAddon!
  16. @IBOutlet var pageRangeLabel: NSTextField!
  17. @IBOutlet var pageRangeSelect: ComponentSelect!
  18. @IBOutlet var positionLabel: NSTextField!
  19. @IBOutlet var firstRadioButton: ComponentRadio!
  20. @IBOutlet var lastRadioButton: ComponentRadio!
  21. @IBOutlet var pageRadioButton: ComponentRadio!
  22. @IBOutlet var pageNumInput: ComponentInputNumber!
  23. @IBOutlet var pageCountLabel: NSTextField!
  24. @IBOutlet var positionSelect: ComponentSelect!
  25. @IBOutlet var cancelButton: ComponentButton!
  26. @IBOutlet var insertButton: ComponentButton!
  27. @IBOutlet var cancelWidthButton:NSLayoutConstraint!
  28. @IBOutlet var insertWidthButton:NSLayoutConstraint!
  29. @IBOutlet var pageRadioWidthButton:NSLayoutConstraint!
  30. private var orgDocument:CPDFDocument?
  31. private var insertDocument:CPDFDocument?
  32. deinit {
  33. KMPrint("KMSearchReplaceWindowController deinit.")
  34. }
  35. override func windowDidLoad() {
  36. super.windowDidLoad()
  37. setUpProperty()
  38. }
  39. // convenience init(_ document: CPDFDocument?) {
  40. // self.init(windowNibName: "KMNPDFInsertPDFWindowController")
  41. // orgDocument = document
  42. // }
  43. //
  44. // convenience init(_ filePath: String,password:String?) {
  45. // self.init(windowNibName: "KMNPDFInsertPDFWindowController")
  46. // let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  47. // if password != nil {
  48. // document?.unlock(withPassword: password as String?)
  49. // }
  50. //
  51. // orgDocument = document
  52. // }
  53. required init?(coder: NSCoder) {
  54. fatalError("init(coder:) has not been implemented")
  55. }
  56. private func setUPPageSelctProperty() {
  57. pageRangeSelect.properties = ComponentSelectProperties(size: .s,
  58. state: .normal,
  59. isDisabled: false,
  60. isError: false,
  61. leftIcon: false,
  62. placeholder: nil,
  63. errorText: nil,
  64. creatable: false,
  65. text: KMLocalizedString("All Pages", comment: ""))
  66. var menuItemString:[String] = [KMLocalizedString("All Pages"),
  67. KMLocalizedString("Odd Pages Only"),
  68. KMLocalizedString("Even Pages Only"),
  69. KMLocalizedString("e.g. 1,3-5,10")]
  70. if insertDocument?.pageCount ?? 0 <= 1 {
  71. menuItemString.removeObject(KMLocalizedString("Even Pages Only"))
  72. }
  73. var menuItemArr: [ComponentMenuitemProperty] = []
  74. for language in menuItemString {
  75. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  76. itemSelected: false,
  77. isDisabled: false,
  78. keyEquivalent: nil,
  79. text: language)
  80. menuItemArr.append(itemProperty)
  81. }
  82. pageRangeSelect.updateMenuItemsArr(menuItemArr)
  83. pageRangeSelect.delegate = self
  84. }
  85. private func setUPPositionSelctProperty() {
  86. positionSelect.properties = ComponentSelectProperties(size: .s,
  87. state: .normal,
  88. isDisabled: false,
  89. isError: false,
  90. leftIcon: false,
  91. placeholder: nil,
  92. errorText: nil,
  93. creatable: false,
  94. text: KMLocalizedString("After", comment: ""))
  95. var menuItemArr: [ComponentMenuitemProperty] = []
  96. for language in [KMLocalizedString("After"),KMLocalizedString("Before")] {
  97. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  98. itemSelected: false,
  99. isDisabled: false,
  100. keyEquivalent: nil,
  101. text: language)
  102. menuItemArr.append(itemProperty)
  103. }
  104. positionSelect.updateMenuItemsArr(menuItemArr)
  105. positionSelect.delegate = self
  106. }
  107. private func setUpProperty() {
  108. titleLabel.stringValue = KMLocalizedString("Insert File")
  109. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  110. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  111. fileLabel.stringValue = KMLocalizedString("Form File")
  112. fileLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  113. fileLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  114. pageRangeLabel.stringValue = KMLocalizedString("Page Range:")
  115. pageRangeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  116. pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field-colorText-filled-nor")
  117. positionLabel.stringValue = KMLocalizedString("Insertion Position")
  118. positionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  119. positionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  120. firstRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  121. state: .normal,
  122. isDisabled: false,
  123. showhelp: false,
  124. text: KMLocalizedString("First"),
  125. checkboxType: .normal)
  126. lastRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  127. state: .normal,
  128. isDisabled: false,
  129. showhelp: false,
  130. text: KMLocalizedString("First"),
  131. checkboxType: .normal)
  132. pageRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  133. state: .normal,
  134. isDisabled: false,
  135. showhelp: false,
  136. text: KMLocalizedString("Page"),
  137. checkboxType: .normal)
  138. insertButton.properties = ComponentButtonProperty(type: .primary,
  139. size: .s,
  140. state: .normal,
  141. buttonText: KMLocalizedString("Apply"))
  142. insertButton.setTarget(self, action: #selector(insertButtonClicked(_ :)))
  143. insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth
  144. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  145. size: .s,
  146. state: .normal,
  147. buttonText: KMLocalizedString("Cancel"))
  148. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  149. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  150. firstRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  151. lastRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  152. pageRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  153. pageNumInput.properties = ComponentInputNumberProperty(alignment: .left,
  154. size: .s,
  155. state: .normal,
  156. isError: false,
  157. showErrorInfo: false,
  158. isDisabled: true,
  159. showPrefix: false,
  160. showSuffix: false,
  161. minSize: 5,
  162. maxSize: 99,
  163. text: SettingsManager.sharedInstance.autoSaveMinutes)
  164. pageNumInput.inputNumberDelegate = self
  165. let inputWithAddonProperty = ComponentInputWithAddonProperty(size: .s,
  166. isDisabled:false,
  167. addonType: .text)
  168. fileNameInput.properties = inputWithAddonProperty
  169. selectFileInput.properties = ComponentInputAddonProperty(size: .s,
  170. state: .normal,
  171. addOnBefore: false,
  172. onlyRead: false,
  173. addonType: .imageWithColor,
  174. iconImage:nil,text: KMLocalizedString("Browse"))
  175. selectFileInput.setTarget(self, action: #selector(chooseURLAction(_ :)))
  176. pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
  177. }
  178. func own_beginSheetModal(for window: NSWindow?, completionHandler handler: ((String?) -> Void)?) {
  179. if window != nil {
  180. parentWindow = window
  181. window!.beginSheet(self.window!) { ModalResponse in
  182. self.handler?(nil)
  183. }
  184. }
  185. self.handler = handler
  186. }
  187. //MARK: - Action
  188. @objc func cancelButtonClicked(_ sender: NSView) {
  189. parentWindow?.endSheet(self.window!)
  190. }
  191. @objc func insertButtonClicked(_ sender: NSView) {
  192. parentWindow?.endSheet(self.window!)
  193. }
  194. @objc func insertPositionAction(_ sender: NSView) {
  195. if sender == lastRadioButton {
  196. } else if sender == firstRadioButton {
  197. } else if sender == pageRadioButton {
  198. }
  199. }
  200. @objc func chooseURLAction(_ sender: NSView) {
  201. }
  202. }
  203. extension KMNPDFInsertPDFWindowController: ComponentInputNumberDelegate {
  204. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  205. }
  206. }
  207. extension KMNPDFInsertPDFWindowController: ComponentSelectDelegate {
  208. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  209. }
  210. }