KMNPDFInsertPDFWindowController.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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: KMNBaseWindowController {
  10. @IBOutlet var titleLabel: NSTextField!
  11. @IBOutlet var fileLabel: NSTextField!
  12. @IBOutlet var fileNameInput: ComponentInput!
  13. @IBOutlet var selectFileInput: ComponentInputAddon!
  14. @IBOutlet var pageRangeLabel: NSTextField!
  15. @IBOutlet var pageRangeSelect: ComponentSelect!
  16. @IBOutlet var positionLabel: NSTextField!
  17. @IBOutlet var firstRadioButton: ComponentRadio!
  18. @IBOutlet var lastRadioButton: ComponentRadio!
  19. @IBOutlet var pageRadioButton: ComponentRadio!
  20. @IBOutlet var pageNumInput: ComponentInputNumber!
  21. @IBOutlet var pageCountLabel: NSTextField!
  22. @IBOutlet var positionSelect: ComponentSelect!
  23. @IBOutlet var cancelButton: ComponentButton!
  24. @IBOutlet var insertButton: ComponentButton!
  25. @IBOutlet var cancelWidthButton:NSLayoutConstraint!
  26. @IBOutlet var insertWidthButton:NSLayoutConstraint!
  27. @IBOutlet var pageRadioWidthButton:NSLayoutConstraint!
  28. private var orgDocument:CPDFDocument?
  29. private var insertFilePath:String?
  30. private var insertPDFPassword:String?
  31. private var insertDocument:CPDFDocument?
  32. private var selectionIndexPaths:Set<IndexPath>?
  33. var pdfCallback: ((KMNFileAttribute, Int) -> Void)?
  34. var fileCallback: ((String, Int) -> Void)?
  35. convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>?) {
  36. self.init(windowNibName: "KMNPDFInsertPDFWindowController")
  37. orgDocument = document
  38. self.selectionIndexPaths = selectionIndexPaths
  39. }
  40. convenience init(_ document: CPDFDocument?,filePath: String,password:String?,selectionIndexPaths: Set<IndexPath>?) {
  41. self.init(windowNibName: "KMNPDFInsertPDFWindowController")
  42. orgDocument = document
  43. insertPDFPassword = password
  44. insertFilePath = filePath
  45. self.selectionIndexPaths = selectionIndexPaths
  46. }
  47. override func windowDidLoad() {
  48. super.windowDidLoad()
  49. if insertFilePath?.isEmpty == false && insertFilePath?.pathExtension == "pdf" {
  50. insertDocument = CPDFDocument(url: NSURL.fileURL(withPath: insertFilePath ?? ""))
  51. if let data = insertDocument?.isLocked, data {
  52. insertDocument?.unlock(withPassword: insertPDFPassword)
  53. }
  54. }
  55. }
  56. override func beginSheetFinish() {
  57. super.beginSheetFinish()
  58. pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
  59. pageRangeSelect.selectItemAtIndex(0)
  60. positionSelect.selectItemAtIndex(0)
  61. if(selectionIndexPaths?.count ?? 0 > 0) {
  62. pageRadioButton.properties.checkboxType = .selected
  63. let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
  64. pageNumInput.properties.text = String((maxmumIndexPath?.item ?? 0)+1)
  65. pageRadioButton.reloadData()
  66. pageNumInput.reloadData()
  67. } else {
  68. firstRadioButton.properties.checkboxType = .selected
  69. pageRadioButton.properties.checkboxType = .normal
  70. positionSelect.properties.isDisabled = true
  71. pageNumInput.properties.isDisabled = true
  72. firstRadioButton.reloadData()
  73. pageRadioButton.reloadData()
  74. pageNumInput.reloadData()
  75. positionSelect.reloadData()
  76. }
  77. self.updateInsertPDF()
  78. }
  79. private func setUpPageSelctProperty() {
  80. var menuItemString:[String] = [KMLocalizedString("All Pages"),
  81. KMLocalizedString("Odd Pages Only"),
  82. KMLocalizedString("Even Pages Only"),
  83. KMLocalizedString("e.g. 1,3-5,10")]
  84. if insertDocument?.pageCount ?? 0 <= 1 {
  85. menuItemString.removeObject(KMLocalizedString("Even Pages Only"))
  86. }
  87. var menuItemArr: [ComponentMenuitemProperty] = []
  88. for language in menuItemString {
  89. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  90. itemSelected: false,
  91. isDisabled: false,
  92. keyEquivalent: nil,
  93. text: language)
  94. menuItemArr.append(itemProperty)
  95. }
  96. pageRangeSelect.updateMenuItemsArr(menuItemArr)
  97. }
  98. private func setUpPositionSelctProperty() {
  99. var menuItemArr: [ComponentMenuitemProperty] = []
  100. for language in [KMLocalizedString("After"),KMLocalizedString("Before")] {
  101. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  102. itemSelected: false,
  103. isDisabled: false,
  104. keyEquivalent: nil,
  105. text: language)
  106. menuItemArr.append(itemProperty)
  107. }
  108. positionSelect.updateMenuItemsArr(menuItemArr)
  109. }
  110. override func updateUIThemeColor() {
  111. super.updateUIThemeColor()
  112. window?.contentView?.wantsLayer = true
  113. window?.contentView?.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/popUp").cgColor
  114. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  115. fileLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  116. pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  117. positionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  118. firstRadioButton.reloadData()
  119. lastRadioButton.reloadData()
  120. pageRadioButton.reloadData()
  121. insertButton.reloadData()
  122. cancelButton.reloadData()
  123. selectFileInput.reloadData()
  124. pageRangeSelect.reloadData()
  125. positionSelect.reloadData()
  126. }
  127. override func updateUILanguage() {
  128. super.updateUILanguage()
  129. titleLabel.stringValue = KMLocalizedString("Insert File")
  130. fileLabel.stringValue = KMLocalizedString("From PDF")
  131. pageRangeLabel.stringValue = KMLocalizedString("Page Range:")
  132. positionLabel.stringValue = KMLocalizedString("Where to insert?")
  133. firstRadioButton.properties.text = KMLocalizedString("First")
  134. lastRadioButton.properties.text = KMLocalizedString("Last")
  135. pageRadioButton.properties.text = KMLocalizedString("Page")
  136. firstRadioButton.reloadData()
  137. lastRadioButton.reloadData()
  138. pageRadioButton.reloadData()
  139. insertButton.properties.buttonText = KMLocalizedString("Insert")
  140. cancelButton.properties.buttonText = KMLocalizedString("Cancel")
  141. insertButton.reloadData()
  142. cancelButton.reloadData()
  143. selectFileInput.properties.text = KMLocalizedString("Browse")
  144. selectFileInput.reloadData()
  145. setUpPageSelctProperty()
  146. setUpPositionSelctProperty()
  147. let pageRangeSelectIndex = pageRangeSelect.indexOfSelect()
  148. if pageRangeSelectIndex == 0 {
  149. pageRangeSelect.properties.text = KMLocalizedString("All Pages")
  150. } else if(pageRangeSelectIndex == 1) {
  151. pageRangeSelect.properties.text = KMLocalizedString("Odd Pages Only")
  152. } else {
  153. if(orgDocument?.pageCount ?? 0 > 1 && pageRangeSelectIndex == 2) {
  154. pageRangeSelect.properties.text = KMLocalizedString("Even Pages Only")
  155. }
  156. }
  157. let positionSelectIndex = positionSelect.indexOfSelect()
  158. if positionSelectIndex == 0 {
  159. positionSelect.properties.text = KMLocalizedString("After")
  160. } else {
  161. positionSelect.properties.text = KMLocalizedString("Before")
  162. }
  163. pageRangeSelect.reloadData()
  164. positionSelect.reloadData()
  165. pageRadioWidthButton.constant = pageRadioButton.properties.propertyInfo.viewWidth
  166. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  167. insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth
  168. }
  169. override func initContentView() {
  170. super.initContentView()
  171. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  172. fileLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  173. pageRangeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  174. positionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
  175. firstRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  176. state: .normal,
  177. isDisabled: false,
  178. showhelp: false,
  179. text: KMLocalizedString("First"),
  180. checkboxType: .normal)
  181. lastRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  182. state: .normal,
  183. isDisabled: false,
  184. showhelp: false,
  185. text: KMLocalizedString("Last"),
  186. checkboxType: .normal)
  187. pageRadioButton.properties = ComponentCheckBoxProperty(size: .s,
  188. state: .normal,
  189. isDisabled: false,
  190. showhelp: false,
  191. text: KMLocalizedString("Page"),
  192. checkboxType: .normal)
  193. insertButton.properties = ComponentButtonProperty(type: .primary,
  194. size: .s,
  195. state: .normal,
  196. isDisable: true,
  197. buttonText: KMLocalizedString("Insert"))
  198. insertButton.setTarget(self, action: #selector(insertButtonClicked(_ :)))
  199. insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth
  200. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary,
  201. size: .s,
  202. state: .normal,
  203. buttonText: KMLocalizedString("Cancel"))
  204. cancelButton.setTarget(self, action: #selector(cancelButtonClicked(_ :)))
  205. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  206. firstRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  207. lastRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  208. pageRadioButton.setTarget(self, action: #selector(insertPositionAction(_:)))
  209. pageNumInput.properties = ComponentInputNumberProperty(alignment: .left,
  210. size: .s,
  211. state: .normal,
  212. isError: false,
  213. showErrorInfo: false,
  214. isDisabled: false,
  215. showPrefix: false,
  216. showSuffix: false,
  217. minSize: 1,
  218. maxSize: Int(orgDocument?.pageCount ?? 1),
  219. text: "")
  220. pageNumInput.delegate = self
  221. let inputWithAddonProperty = ComponentInputProperty(size: .s,
  222. state: .normal,
  223. isError: false,
  224. showPrefix: false,
  225. showSuffix: false,
  226. showClear: false,
  227. isDisabled: false,
  228. placeholder: KMLocalizedString("Please enter..."),
  229. text: "",
  230. creatable: false)
  231. fileNameInput.properties = inputWithAddonProperty
  232. fileNameInput.properties.propertyInfo.cornerRadius_topRight = 0
  233. fileNameInput.properties.propertyInfo.cornerRadius_bottomRight = 0
  234. fileNameInput.reloadData()
  235. selectFileInput.properties = ComponentInputAddonProperty(size: .s,
  236. state: .normal,
  237. addOnBefore: false,
  238. onlyRead: false,
  239. addonType: .textWithColor,
  240. text: KMLocalizedString("Browse"))
  241. selectFileInput.setTarget(self, action: #selector(chooseURLAction(_ :)))
  242. pageRangeSelect.properties = ComponentSelectProperties(size: .s,
  243. state: .normal,
  244. isDisabled: true,
  245. isError: false,
  246. leftIcon: false,
  247. placeholder: nil,
  248. errorText: nil,
  249. creatable: false,
  250. text: KMLocalizedString("All Pages", comment: ""))
  251. positionSelect.properties = ComponentSelectProperties(size: .s,
  252. state: .normal,
  253. isDisabled: false,
  254. isError: false,
  255. leftIcon: false,
  256. placeholder: nil,
  257. errorText: nil,
  258. creatable: false,
  259. text: KMLocalizedString("After", comment: ""))
  260. setUpPositionSelctProperty()
  261. setUpPageSelctProperty()
  262. positionSelect.delegate = self
  263. pageRangeSelect.delegate = self
  264. pageRadioWidthButton.constant = pageRadioButton.properties.propertyInfo.viewWidth
  265. cancelWidthButton.constant = cancelButton.properties.propertyInfo.viewWidth
  266. insertWidthButton.constant = insertButton.properties.propertyInfo.viewWidth
  267. }
  268. private func updateInsertPDF() {
  269. fileNameInput.properties.text = insertFilePath ?? ""
  270. fileNameInput.reloadData()
  271. window?.makeFirstResponder(self)
  272. if(insertDocument != nil) {
  273. pageRangeSelect.properties.isDisabled = false
  274. pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  275. setUpPageSelctProperty()
  276. pageRangeSelect.selectItemAtIndex(0)
  277. pageRangeSelect.reloadData()
  278. } else {
  279. pageRangeSelect.properties.isDisabled = true
  280. pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-dis")
  281. pageRangeSelect.selectItemAtIndex(0)
  282. pageRangeSelect.reloadData()
  283. }
  284. if(insertFilePath?.isEmpty == false) {
  285. if insertButton.properties.isDisabled == true {
  286. insertButton.properties.isDisabled = false
  287. insertButton.reloadData()
  288. }
  289. }
  290. }
  291. //MARK: - Action
  292. @objc func cancelButtonClicked(_ sender: NSView) {
  293. own_closeEndSheet()
  294. }
  295. @objc func insertButtonClicked(_ sender: NSView) {
  296. var insetPageDex:Int = 0
  297. if firstRadioButton.properties.checkboxType == .selected {
  298. insetPageDex = 0
  299. } else if lastRadioButton.properties.checkboxType == .selected {
  300. insetPageDex = Int(orgDocument?.pageCount ?? 0)
  301. } else if pageRadioButton.properties.checkboxType == .selected {
  302. insetPageDex = Int(pageNumInput.properties.text ?? "0") ?? 0
  303. let positionSelectIndex = positionSelect.indexOfSelect()
  304. if (1 == positionSelectIndex) {
  305. insetPageDex -= 1
  306. }
  307. }
  308. if(insertDocument != nil) {
  309. let pageRangeSelectIndex = pageRangeSelect.indexOfSelect()
  310. let fileAttribute = KMNFileAttribute()
  311. fileAttribute.pdfDocument = insertDocument
  312. fileAttribute.password = insertDocument?.password ?? ""
  313. fileAttribute.filePath = insertDocument?.documentURL.path ?? ""
  314. if pageRangeSelectIndex == 0 {
  315. fileAttribute.bAllPage = true
  316. fileAttribute.pagesType = .AllPages
  317. } else if pageRangeSelectIndex == 1 {
  318. fileAttribute.bAllPage = false
  319. fileAttribute.pagesType = .OnlyOdd
  320. } else {
  321. if(insertDocument?.pageCount ?? 0 <= 1) {
  322. fileAttribute.bAllPage = false
  323. fileAttribute.pagesType = .PagesString
  324. fileAttribute.pagesString = pageRangeSelect.properties.text ?? ""
  325. } else {
  326. fileAttribute.bAllPage = false
  327. if(pageRangeSelectIndex == 2) {
  328. fileAttribute.pagesType = .OnlyEven
  329. } else {
  330. fileAttribute.bAllPage = false
  331. fileAttribute.pagesType = .PagesString
  332. fileAttribute.pagesString = pageRangeSelect.properties.text ?? ""
  333. }
  334. }
  335. }
  336. if (fileAttribute.fetchSelectPages().isEmpty) {
  337. let alert = NSAlert()
  338. alert.alertStyle = .critical
  339. alert.messageText = String(format: "%@ %@", fileAttribute.filePath.lastPathComponent, KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  340. alert.runModal()
  341. return
  342. }
  343. pdfCallback?(fileAttribute,insetPageDex)
  344. } else {
  345. fileCallback?(insertFilePath ?? "",insetPageDex)
  346. }
  347. own_closeEndSheet()
  348. }
  349. @objc func insertPositionAction(_ sender: NSView) {
  350. var oldSelectRadioBtn:ComponentRadio? = nil
  351. if sender == lastRadioButton {
  352. if(firstRadioButton.properties.checkboxType == .selected) {
  353. oldSelectRadioBtn = firstRadioButton
  354. } else if (pageRadioButton.properties.checkboxType == .selected){
  355. oldSelectRadioBtn = pageRadioButton
  356. pageNumInput.properties.isDisabled = true
  357. positionSelect.properties.isDisabled = true
  358. pageNumInput.reloadData()
  359. positionSelect.reloadData()
  360. }
  361. } else if sender == firstRadioButton {
  362. if(lastRadioButton.properties.checkboxType == .selected) {
  363. oldSelectRadioBtn = lastRadioButton
  364. } else if (pageRadioButton.properties.checkboxType == .selected){
  365. oldSelectRadioBtn = pageRadioButton
  366. pageNumInput.properties.isDisabled = true
  367. positionSelect.properties.isDisabled = true
  368. pageNumInput.reloadData()
  369. positionSelect.reloadData()
  370. }
  371. } else if sender == pageRadioButton {
  372. if(lastRadioButton.properties.checkboxType == .selected) {
  373. oldSelectRadioBtn = lastRadioButton
  374. } else if (firstRadioButton.properties.checkboxType == .selected){
  375. oldSelectRadioBtn = firstRadioButton
  376. }
  377. pageNumInput.properties.isDisabled = false
  378. positionSelect.properties.isDisabled = false
  379. pageNumInput.reloadData()
  380. positionSelect.reloadData()
  381. }
  382. if(oldSelectRadioBtn != nil) {
  383. oldSelectRadioBtn?.properties.checkboxType = .normal
  384. oldSelectRadioBtn?.reloadData()
  385. }
  386. }
  387. @objc func chooseURLAction(_ sender: NSView) {
  388. let openPanel = NSOpenPanel()
  389. let supportFiles = KMNConvertTool.pdfExtensions + KMConvertPDFManager.supportFileType()
  390. openPanel.allowedFileTypes = supportFiles
  391. openPanel.allowsMultipleSelection = false
  392. openPanel.beginSheetModal(for: self.window!) { [self] result in
  393. if (result == .OK) {
  394. let fileURL = openPanel.url
  395. if(fileURL?.pathExtension == "pdf") {
  396. let pdfDoc = CPDFDocument(url: fileURL)
  397. if let data = pdfDoc?.isLocked, data {
  398. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  399. self.window?.windowController?.showCheckPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner, completion: { [weak self] success, resultPassword in
  400. if success && resultPassword.isEmpty == false {
  401. self?.insertDocument = CPDFDocument(url: fileURL)
  402. if let data = self?.insertDocument?.isLocked, data {
  403. self?.insertDocument?.unlock(withPassword: resultPassword)
  404. }
  405. self?.insertFilePath = fileURL?.path
  406. self?.updateInsertPDF()
  407. }
  408. })
  409. }
  410. } else {
  411. self.insertDocument = CPDFDocument(url: fileURL)
  412. self.insertFilePath = fileURL?.path
  413. self.updateInsertPDF()
  414. }
  415. } else {
  416. self.insertDocument = nil
  417. self.insertFilePath = fileURL?.path
  418. self.updateInsertPDF()
  419. }
  420. }
  421. }
  422. }
  423. }
  424. //MARK: - ComponentInputNumberDelegate
  425. extension KMNPDFInsertPDFWindowController: ComponentInputNumberDelegate {
  426. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  427. if(inputNumber == pageNumInput) {
  428. }
  429. }
  430. }
  431. //MARK: - ComponentSelectDelegate
  432. extension KMNPDFInsertPDFWindowController: ComponentSelectDelegate {
  433. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  434. if(view == pageRangeSelect) {
  435. let pageRangeSelectIndex = pageRangeSelect.indexOfSelect()
  436. var isEditIndex:Int = 3
  437. if(insertDocument?.pageCount ?? 0 <= 1) {
  438. isEditIndex = 2
  439. }
  440. if (pageRangeSelectIndex == isEditIndex) {
  441. pageRangeSelect.properties.text = ""
  442. pageRangeSelect.properties.placeholder = KMLocalizedString("e.g. 1,3-5,10")
  443. pageRangeSelect.properties.creatable = true
  444. } else {
  445. pageRangeSelect.properties.placeholder = ""
  446. pageRangeSelect.properties.creatable = false
  447. }
  448. pageRangeSelect.reloadData()
  449. }
  450. }
  451. }