KMCropPropertyController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. @objc protocol KMCropPropertyControllerDelegate: AnyObject {
  10. @objc optional func kmCropControllerDidCrop(_ controller: KMCropPropertyController, _ cropRect: CGRect, _ view: KMPageRangeSelectView)
  11. @objc optional func kmCropControllerDidCropSeparate(_ controller: KMCropPropertyController, _ view: KMPageRangeSelectView)
  12. @objc optional func kmCropControllerDidCropAuto(_ controller: KMCropPropertyController, _ view: KMPageRangeSelectView)
  13. }
  14. class KMCropPropertyController: NSViewController {
  15. @IBOutlet var contendView: NSView!
  16. //White Margin
  17. @IBOutlet var whiteMarginBGView: NSView!
  18. @IBOutlet var whiteMarginLabel: NSTextField!
  19. @IBOutlet var separateLabel: NSTextField!
  20. @IBOutlet var separateTip: ComponentToolTipsHelp!
  21. @IBOutlet var separateSwitch: ComponentSwitch!
  22. @IBOutlet var autoLabel: NSTextField!
  23. @IBOutlet var autoTipsView: ComponentToolTipsHelp!
  24. @IBOutlet var autoSwitch: ComponentSwitch!
  25. //PageSize
  26. @IBOutlet var pageSizeBGView: NSView!
  27. @IBOutlet var pageSizeLabel: NSTextField!
  28. @IBOutlet var sizeSyncButton: ComponentButton!
  29. @IBOutlet var sizeWidthInput: ComponentInputNumber!
  30. @IBOutlet var sizeHeightInput: ComponentInputNumber!
  31. //PageMargin
  32. @IBOutlet var pageMarginBGView: NSView!
  33. @IBOutlet var pageMarginLabel: NSTextField!
  34. @IBOutlet var pageLeftInput: ComponentInputNumber!
  35. @IBOutlet var pageTopInput: ComponentInputNumber!
  36. @IBOutlet var pageRightInput: ComponentInputNumber!
  37. @IBOutlet var pageBottomInput: ComponentInputNumber!
  38. //PageRange
  39. @IBOutlet var pageRangeBGView: NSView!
  40. @IBOutlet var pageRangeLabel: NSTextField!
  41. @IBOutlet var pangeRangeSelectView: KMPageRangeSelectView!
  42. @IBOutlet var cropButton: ComponentButton!
  43. private var _cropSeparateOn: Bool = false
  44. private var _cropAutoOn: Bool = false
  45. private var _syncChangeBounds: Bool = false //同步修改宽高
  46. weak open var delegate: KMCropPropertyControllerDelegate?
  47. public var pdfView: CPDFListView?
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. // Do view setup here.
  51. cropSeparateOn = UserDefaults.standard.bool(forKey: "CropSeparateOnKey")
  52. cropAutoOn = UserDefaults.standard.bool(forKey: "CropAutoOnKey")
  53. if UserDefaults.standard.object(forKey: "CropSyncChangeBoundsKey") == nil {
  54. UserDefaults.standard.setValue(true, forKey: "CropSyncChangeBoundsKey")
  55. }
  56. syncChangeBounds = UserDefaults.standard.bool(forKey: "CropSyncChangeBoundsKey")
  57. setupProperty()
  58. reloadData()
  59. }
  60. //MARK: - Setter and Getter
  61. public var cropSeparateOn : Bool {
  62. get {
  63. return _cropSeparateOn
  64. }
  65. set {
  66. _cropSeparateOn = newValue
  67. UserDefaults.standard.setValue(_cropSeparateOn, forKey: "CropSeparateOnKey")
  68. }
  69. }
  70. public var cropAutoOn : Bool {
  71. get {
  72. return _cropAutoOn
  73. }
  74. set {
  75. _cropAutoOn = newValue
  76. UserDefaults.standard.setValue(_cropAutoOn, forKey: "CropAutoOnKey")
  77. }
  78. }
  79. public var syncChangeBounds : Bool {
  80. get {
  81. return _syncChangeBounds
  82. }
  83. set {
  84. _syncChangeBounds = newValue
  85. UserDefaults.standard.setValue(_syncChangeBounds, forKey: "CropSyncChangeBoundsKey")
  86. }
  87. }
  88. func setupProperty() {
  89. //White Margin
  90. whiteMarginLabel.stringValue = KMLocalizedString("Crop White Margins")
  91. whiteMarginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  92. whiteMarginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  93. separateLabel.stringValue = KMLocalizedString("Separate")
  94. separateLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
  95. separateLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  96. separateSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
  97. separateSwitch.setTarget(self, action: #selector(switchAction(_:)))
  98. autoLabel.stringValue = KMLocalizedString("Auto")
  99. autoLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-form/colorText-label")
  100. autoLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-regular")
  101. separateTip.toolTip = KMLocalizedString("separateTip tooltips")
  102. autoTipsView.toolTip = KMLocalizedString("autoTipsView tooltips")
  103. autoSwitch.properties = ComponentSwitchProperty(size: .s, open: false, isDisabled: false)
  104. autoSwitch.setTarget(self, action: #selector(switchAction(_:)))
  105. //PageSize
  106. whiteMarginLabel.stringValue = KMLocalizedString("Crop Page Size")
  107. whiteMarginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  108. whiteMarginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  109. sizeSyncButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "sync_Change_unlock"), keepPressState: true)
  110. sizeSyncButton.properties.propertyInfo.leftIcon_press = NSImage(named: "sync_Change_lock")
  111. sizeSyncButton.setTarget(self, action: #selector(sizeSyncButtonClicked(_:)))
  112. sizeSyncButton.reloadData()
  113. sizeWidthInput.properties = ComponentInputNumberProperty(alignment: .center,
  114. size: .s,
  115. state: .normal,
  116. showPrefix: true,
  117. leftIcon: NSImage(named: "w_icon"),
  118. minSize: 0,
  119. maxSize: 1000,
  120. text:"100")
  121. sizeWidthInput.delegate = self
  122. sizeHeightInput.properties = ComponentInputNumberProperty(alignment: .center,
  123. size: .s,
  124. state: .normal,
  125. showPrefix: true,
  126. leftIcon: NSImage(named: "h_icon"),
  127. minSize: 0,
  128. maxSize: 1000,
  129. text:"100")
  130. sizeHeightInput.delegate = self
  131. //PageMargin
  132. whiteMarginLabel.stringValue = KMLocalizedString("Crop Page Margins")
  133. whiteMarginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  134. whiteMarginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  135. pageLeftInput.properties = ComponentInputNumberProperty(size: .s,
  136. state: .normal,
  137. minSize: 0,
  138. maxSize: 1000,
  139. text:"0")
  140. pageTopInput.properties = ComponentInputNumberProperty(size: .s,
  141. state: .normal,
  142. minSize: 0,
  143. maxSize: 1000,
  144. text:"0")
  145. pageRightInput.properties = ComponentInputNumberProperty(size: .s,
  146. state: .normal,
  147. minSize: 0,
  148. maxSize: 1000,
  149. text:"0")
  150. pageBottomInput.properties = ComponentInputNumberProperty(size: .s,
  151. state: .normal,
  152. minSize: 0,
  153. maxSize: 1000,
  154. text:"0")
  155. pageLeftInput.delegate = self
  156. pageTopInput.delegate = self
  157. pageRightInput.delegate = self
  158. pageBottomInput.delegate = self
  159. //PageRange
  160. pageRangeLabel.stringValue = KMLocalizedString("Page Range")
  161. pageRangeLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  162. pageRangeLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  163. cropButton.properties = ComponentButtonProperty(type: .primary, size: .m, buttonText: KMLocalizedString("Apply"), keepPressState: false)
  164. cropButton.setTarget(self, action: #selector(cropButtonClicked(_:)))
  165. }
  166. func reloadData() {
  167. guard let pdfView = self.pdfView else {
  168. return
  169. }
  170. guard let page = pdfView.currentSelectionPage() else {
  171. return
  172. }
  173. let pageBounds = page.bounds
  174. let selectionRect = pdfView.selectionRect
  175. //White Margin
  176. separateSwitch.properties.open = cropSeparateOn
  177. separateSwitch.reloadData()
  178. autoSwitch.properties.open = cropAutoOn
  179. autoSwitch.reloadData()
  180. //PageSize
  181. sizeWidthInput.properties.maxSize = Int(pageBounds.size.width)
  182. sizeWidthInput.properties.text = String(format: "%.0f", selectionRect.size.width)
  183. sizeWidthInput.reloadData()
  184. sizeHeightInput.properties.maxSize = Int(pageBounds.size.height)
  185. sizeHeightInput.properties.text = String(format: "%.0f", selectionRect.size.height)
  186. sizeHeightInput.reloadData()
  187. if syncChangeBounds {
  188. sizeSyncButton.properties.state = .pressed
  189. } else {
  190. sizeSyncButton.properties.state = .normal
  191. }
  192. sizeSyncButton.reloadData()
  193. //PageMargin
  194. pageLeftInput.properties.maxSize = Int(pageBounds.size.width)
  195. pageLeftInput.properties.text = String(format: "%.0f", selectionRect.origin.x)
  196. pageLeftInput.reloadData()
  197. pageRightInput.properties.maxSize = Int(pageBounds.size.width)
  198. pageRightInput.properties.text = String(format: "%.0f", pageBounds.size.width - CGRectGetMaxX(selectionRect))
  199. pageRightInput.reloadData()
  200. pageTopInput.properties.maxSize = Int(pageBounds.size.height)
  201. pageTopInput.properties.text = String(format: "%.0f", pageBounds.size.height - CGRectGetMaxY(selectionRect))
  202. pageTopInput.reloadData()
  203. pageBottomInput.properties.maxSize = Int(pageBounds.size.height)
  204. pageBottomInput.properties.text = String(format: "%.0f", selectionRect.origin.y)
  205. pageBottomInput.reloadData()
  206. //PageRange
  207. if CGRectEqualToRect(pageBounds, selectionRect) {
  208. cropButton.properties.isDisabled = true
  209. } else {
  210. cropButton.properties.isDisabled = false
  211. }
  212. cropButton.reloadData()
  213. }
  214. //MARK: - Action
  215. @objc func sizeSyncButtonClicked(_ sender: ComponentButton) {
  216. syncChangeBounds = !syncChangeBounds
  217. reloadData()
  218. }
  219. @objc func cropButtonClicked(_ sender: ComponentButton) {
  220. if cropSeparateOn {
  221. delegate?.kmCropControllerDidCropSeparate?(self, pangeRangeSelectView)
  222. } else if cropAutoOn {
  223. delegate?.kmCropControllerDidCropAuto?(self, pangeRangeSelectView)
  224. } else {
  225. delegate?.kmCropControllerDidCrop?(self, pdfView?.selectionRect ?? CGRectZero, pangeRangeSelectView)
  226. }
  227. }
  228. @objc func switchAction(_ sender: ComponentSwitch) {
  229. guard let pdfView = self.pdfView else {
  230. return
  231. }
  232. guard let page = pdfView.document.page(at: 0) else {
  233. return
  234. }
  235. if sender == separateSwitch {
  236. cropSeparateOn = sender.properties.open
  237. cropAutoOn = false
  238. let rect = KMCropTools.getPageForegroundBox(page)
  239. pdfView.selectionRect = rect
  240. pdfView.setNeedsDisplayForVisiblePages()
  241. } else if sender == autoSwitch {
  242. cropAutoOn = sender.properties.open
  243. cropSeparateOn = false
  244. let rect = KMCropTools.getPageForegroundBox(page)
  245. pdfView.selectionRect = rect
  246. pdfView.setNeedsDisplayForVisiblePages()
  247. }
  248. reloadData()
  249. }
  250. }
  251. //MARK: - ComponentInputNumberDelegate
  252. extension KMCropPropertyController: ComponentInputNumberDelegate {
  253. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  254. guard let pdfView = self.pdfView else {
  255. return
  256. }
  257. guard let page = pdfView.currentSelectionPage() else {
  258. return
  259. }
  260. let pageBounds = page.bounds
  261. var value: CGFloat = 0
  262. if let text = inputNumber?.properties.text {
  263. value = text.stringToCGFloat()
  264. }
  265. var rect = pdfView.selectionRect
  266. if inputNumber == sizeWidthInput {
  267. if syncChangeBounds {
  268. var scale: CGFloat = 0
  269. if rect.size.width != 0 {
  270. scale = value / rect.size.width
  271. }
  272. rect.size.height *= scale
  273. }
  274. rect.size.width = value
  275. } else if inputNumber == sizeHeightInput {
  276. if syncChangeBounds {
  277. var scale: CGFloat = 0
  278. if rect.size.height != 0 {
  279. scale = value / rect.size.height
  280. }
  281. rect.size.width *= scale
  282. }
  283. rect.size.height = value
  284. } else if inputNumber == pageLeftInput {
  285. rect.origin.x = value
  286. } else if inputNumber == pageRightInput {
  287. rect.size.width = pageBounds.size.width - rect.origin.x - value
  288. } else if inputNumber == pageTopInput {
  289. rect.size.height = pageBounds.size.height - rect.origin.y - value
  290. } else if inputNumber == pageBottomInput {
  291. rect.origin.y = value
  292. }
  293. pdfView.selectionRect = rect
  294. pdfView.setNeedsDisplayForVisiblePages()
  295. cropAutoOn = false
  296. cropSeparateOn = false
  297. reloadData()
  298. }
  299. }