KMCropPropertyController.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // KMCropPropertyController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/20.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMCropPropertyController: NSViewController {
  10. @IBOutlet var contendView: NSView!
  11. //White Margin
  12. @IBOutlet var whiteMarginBGView: NSView!
  13. @IBOutlet var whiteMarginLabel: NSTextField!
  14. @IBOutlet var separateLabel: NSTextField!
  15. @IBOutlet var separateTip: ComponentToolTipsHelp!
  16. @IBOutlet var autoLabel: NSTextField!
  17. @IBOutlet var autoTipsView: ComponentToolTipsHelp!
  18. //PageSize
  19. @IBOutlet var pageSizeBGView: NSView!
  20. @IBOutlet var pageSizeLabel: NSTextField!
  21. @IBOutlet var sizeSyncButton: ComponentButton!
  22. @IBOutlet var sizeWidthInput: ComponentInputNumber!
  23. @IBOutlet var sizeHeightInput: ComponentInputNumber!
  24. //PageMargin
  25. @IBOutlet var pageMarginBGView: NSView!
  26. @IBOutlet var pageMarginLabel: NSTextField!
  27. @IBOutlet var pageLeftInput: ComponentInputNumber!
  28. @IBOutlet var pageTopInput: ComponentInputNumber!
  29. @IBOutlet var pageRightInput: ComponentInputNumber!
  30. @IBOutlet var pageBottomInput: ComponentInputNumber!
  31. //PageRange
  32. @IBOutlet var pageRangeBGView: NSView!
  33. @IBOutlet var pageRangeLabel: NSTextField!
  34. @IBOutlet var pangeRangeSelectView: KMPageRangeSelectView!
  35. @IBOutlet var cropButton: ComponentButton!
  36. private var syncChangeBounds: Bool = true //同步修改宽高
  37. var pdfView: CPDFListView?
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. // Do view setup here.
  41. setupProperty()
  42. reloadData()
  43. }
  44. func setupProperty() {
  45. //White Margin
  46. whiteMarginLabel.stringValue = KMLocalizedString("Crop White Margins")
  47. whiteMarginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  48. whiteMarginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  49. separateLabel.stringValue = KMLocalizedString("Separate")
  50. separateLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
  51. separateLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  52. autoLabel.stringValue = KMLocalizedString("Auto")
  53. autoLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
  54. autoLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  55. separateTip.toolTip = KMLocalizedString("separateTip tooltips")
  56. autoTipsView.toolTip = KMLocalizedString("autoTipsView tooltips")
  57. //PageSize
  58. whiteMarginLabel.stringValue = KMLocalizedString("Crop Page Size")
  59. whiteMarginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  60. whiteMarginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  61. sizeSyncButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "sync_Change_unlock"), keepPressState: true)
  62. sizeSyncButton.properties.propertyInfo.leftIcon_press = NSImage(named: "sync_Change_lock")
  63. sizeSyncButton.setTarget(self, action: #selector(sizeSyncButtonClicked(_:)))
  64. sizeSyncButton.reloadData()
  65. sizeWidthInput.properties = ComponentInputNumberProperty(alignment: .center,
  66. size: .s,
  67. state: .normal,
  68. showPrefix: true,
  69. leftIcon: NSImage(named: "w_icon"),
  70. minSize: 0,
  71. maxSize: 1000,
  72. text:"100")
  73. sizeWidthInput.delegate = self
  74. sizeHeightInput.properties = ComponentInputNumberProperty(alignment: .center,
  75. size: .s,
  76. state: .normal,
  77. showPrefix: true,
  78. leftIcon: NSImage(named: "h_icon"),
  79. minSize: 0,
  80. maxSize: 1000,
  81. text:"100")
  82. sizeHeightInput.delegate = self
  83. //PageMargin
  84. whiteMarginLabel.stringValue = KMLocalizedString("Crop Page Margins")
  85. whiteMarginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  86. whiteMarginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  87. pageLeftInput.properties = ComponentInputNumberProperty(size: .s,
  88. state: .normal,
  89. minSize: 0,
  90. maxSize: 1000,
  91. text:"0")
  92. pageTopInput.properties = ComponentInputNumberProperty(size: .s,
  93. state: .normal,
  94. minSize: 0,
  95. maxSize: 1000,
  96. text:"0")
  97. pageRightInput.properties = ComponentInputNumberProperty(size: .s,
  98. state: .normal,
  99. minSize: 0,
  100. maxSize: 1000,
  101. text:"0")
  102. pageBottomInput.properties = ComponentInputNumberProperty(size: .s,
  103. state: .normal,
  104. minSize: 0,
  105. maxSize: 1000,
  106. text:"0")
  107. pageLeftInput.delegate = self
  108. pageTopInput.delegate = self
  109. pageRightInput.delegate = self
  110. pageBottomInput.delegate = self
  111. //PageRange
  112. pageRangeLabel.stringValue = KMLocalizedString("Page Range")
  113. pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  114. pageRangeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  115. cropButton.properties = ComponentButtonProperty(type: .primary, size: .m, buttonText: KMLocalizedString("Apply"), keepPressState: false)
  116. }
  117. func reloadData() {
  118. guard let pdfView = self.pdfView else {
  119. return
  120. }
  121. guard let page = pdfView.currentSelectionPage() else {
  122. return
  123. }
  124. let pageBounds = page.bounds
  125. var selectionRect = pdfView.selectionRect
  126. //White Margin
  127. //PageSize
  128. sizeWidthInput.properties.maxSize = Int(pageBounds.size.width)
  129. sizeWidthInput.properties.text = String(format: "%.0f", selectionRect.size.width)
  130. sizeWidthInput.reloadData()
  131. sizeHeightInput.properties.maxSize = Int(pageBounds.size.height)
  132. sizeHeightInput.properties.text = String(format: "%.0f", selectionRect.size.height)
  133. sizeHeightInput.reloadData()
  134. //PageMargin
  135. pageLeftInput.properties.maxSize = Int(pageBounds.size.width)
  136. pageLeftInput.properties.text = String(format: "%.0f", selectionRect.origin.x)
  137. pageLeftInput.reloadData()
  138. pageRightInput.properties.maxSize = Int(pageBounds.size.width)
  139. pageRightInput.properties.text = String(format: "%.0f", pageBounds.size.width - CGRectGetMaxX(selectionRect))
  140. pageRightInput.reloadData()
  141. pageTopInput.properties.maxSize = Int(pageBounds.size.height)
  142. pageTopInput.properties.text = String(format: "%.0f", pageBounds.size.height - CGRectGetMaxY(selectionRect))
  143. pageTopInput.reloadData()
  144. pageBottomInput.properties.maxSize = Int(pageBounds.size.height)
  145. pageBottomInput.properties.text = String(format: "%.0f", selectionRect.origin.y)
  146. pageBottomInput.reloadData()
  147. //PageRange
  148. print(selectionRect)
  149. }
  150. //MARK: - Action
  151. @objc func sizeSyncButtonClicked(_ sender: ComponentButton) {
  152. syncChangeBounds = !syncChangeBounds
  153. if syncChangeBounds {
  154. sizeSyncButton.properties.state = .pressed
  155. } else {
  156. sizeSyncButton.properties.state = .normal
  157. }
  158. sizeSyncButton.reloadData()
  159. }
  160. }
  161. //MARK: - ComponentInputNumberDelegate
  162. extension KMCropPropertyController: ComponentInputNumberDelegate {
  163. }
  164. //MARK: - ComponentSelectDelegate
  165. extension KMCropPropertyController: ComponentSelectDelegate {
  166. }