KMCropSettingWindowController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // KMCropSettingWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/30.
  6. //
  7. import Cocoa
  8. typealias KMCropSettingWindowControllerItemClick = (_ index: Int) -> ()
  9. class KMCropSettingWindowController: NSWindowController {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var pageRangeBox: NSBox!
  12. @IBOutlet weak var pageSizeBox: NSBox!
  13. @IBOutlet weak var applyButton: NSButton!
  14. @IBOutlet weak var cancelButton: NSButton!
  15. private var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
  16. private var pageSizeView = KMCropSettingPageSizeView()
  17. var itemClick: KMCropSettingWindowControllerItemClick!
  18. var pageCount: Int = 0
  19. var pageRangeIndex: Int = 0
  20. var pageRangePages: Array<Int> = []
  21. var pageSize: String = "None"
  22. override func windowDidLoad() {
  23. super.windowDidLoad()
  24. self.titleLabel.stringValue = "裁剪设置"
  25. let pageRangeView = self.pagePageView
  26. pageRangeView?.frame = self.pageRangeBox.contentView!.bounds
  27. pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  28. self.pageRangeBox.contentView?.addSubview(pageRangeView!)
  29. pageRangeView!.pageSizeComboBox.removeAllItems()
  30. pageRangeView!.pageSizeComboBox.addItems(withObjectValues: ["当前页面","全部页面", "奇数页面", "偶数页面", "自定义页面"])
  31. pageRangeView!.pageSizeComboBox.selectItem(at: 0)
  32. pageRangeView?.isTextDidEndCallback = true
  33. pageRangeView?.itemClick = { [unowned self] index, _ in
  34. self.pageRangeIndex = index
  35. }
  36. pageRangeView?.textDidEndEditingCallback = {
  37. [self] (textString: String) in
  38. let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self.pageCount)
  39. if (array.count <= 0) {
  40. let alert = NSAlert()
  41. alert.alertStyle = .warning
  42. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  43. alert.runModal()
  44. return
  45. }
  46. self.pageRangePages.removeAll()
  47. for i in array {
  48. self.pageRangePages.append(i)
  49. }
  50. }
  51. let pageSizeView = self.pageSizeView
  52. pageSizeView.frame = self.pageSizeBox.contentView!.bounds
  53. pageSizeView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  54. self.pageSizeBox.contentView?.addSubview(pageSizeView)
  55. pageSizeView.itemClick = {
  56. (index: Int, value: Any) in
  57. self.pageSize = value as! String
  58. }
  59. cancelButton.title = NSLocalizedString("取消", comment: "")
  60. cancelButton.isBordered = false
  61. cancelButton.wantsLayer = true
  62. cancelButton.layer?.borderWidth = 1
  63. cancelButton.layer?.borderColor = NSColor.black.cgColor
  64. cancelButton.layer?.cornerRadius = 4
  65. cancelButton.target = self
  66. cancelButton.action = #selector(cancelButtonAction)
  67. applyButton.title = NSLocalizedString("应用", comment: "")
  68. applyButton.isBordered = false
  69. applyButton.wantsLayer = true
  70. applyButton.layer?.cornerRadius = 4
  71. applyButton.target = self
  72. applyButton.action = #selector(applyButtonAction)
  73. applyButton.layer?.backgroundColor = NSColor.black.cgColor
  74. applyButton.attributedTitle = NSMutableAttributedString(string: applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  75. }
  76. @objc func cancelButtonAction() {
  77. guard let callback = self.itemClick else {
  78. return
  79. }
  80. callback(1)
  81. }
  82. @objc func applyButtonAction() {
  83. guard let callback = self.itemClick else {
  84. return
  85. }
  86. callback(2)
  87. }
  88. }