KMCropCoverController.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // KMCropCoverController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/20.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public enum CropPointPosition: Int {
  10. case none = 0
  11. case left_Top
  12. case left_Center
  13. case left_Bottom
  14. case top_Center
  15. case right_Top
  16. case right_Center
  17. case right_Bottom
  18. case bottom_Center
  19. case center
  20. }
  21. class ResizableView: NSView {
  22. }
  23. class KMCropCoverController: NSViewController {
  24. var emptyView: ResizableView!
  25. var stopGesture: Bool = false
  26. var initialFrame: NSRect = NSRect.zero
  27. var startPoint: NSPoint = .zero
  28. var pointType: CropPointPosition = .none
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do view setup here.
  32. setup()
  33. }
  34. func setup() {
  35. view.wantsLayer = true
  36. // view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/mask").cgColor
  37. view.layer?.backgroundColor = NSColor.red.cgColor
  38. // 创建空白 View
  39. emptyView = ResizableView(frame: NSRect(x: 100, y: 100, width: 200, height: 200))
  40. emptyView.wantsLayer = true
  41. emptyView.layer?.backgroundColor = NSColor.white.cgColor
  42. view.addSubview(emptyView)
  43. let panGesture = NSPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
  44. view.addGestureRecognizer(panGesture)
  45. }
  46. @objc func handlePan(_ gesture: NSPanGestureRecognizer) {
  47. let point = gesture.location(in: self.view)
  48. let emptyPoint = self.view.convert(point, to: emptyView)
  49. if gesture.state == .began {
  50. startPoint = point
  51. initialFrame = emptyView.frame
  52. if CGRectContainsPoint(emptyView.frame, point) == false {
  53. stopGesture = true
  54. } else {
  55. if CGRectContainsPoint(CGRectMake(0, 0, 12, 12), emptyPoint) {
  56. pointType = .left_Bottom
  57. } else if CGRectContainsPoint(CGRectMake(0, CGRectGetHeight(emptyView.frame)/2 - 6, 12, 12), emptyPoint) {
  58. pointType = .left_Center
  59. } else if CGRectContainsPoint(CGRectMake(0, CGRectGetHeight(emptyView.frame) - 12, 12, 12), emptyPoint) {
  60. pointType = .left_Top
  61. } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame)/2 - 6, CGRectGetHeight(emptyView.frame) - 12, 12, 12), emptyPoint) {
  62. pointType = .top_Center
  63. } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame) - 12, CGRectGetHeight(emptyView.frame) - 12, 12, 12), emptyPoint) {
  64. pointType = .right_Top
  65. } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame) - 12, CGRectGetHeight(emptyView.frame)/2 - 6, 12, 12), emptyPoint) {
  66. pointType = .right_Center
  67. } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame) - 12, 0, 12, 12), emptyPoint) {
  68. pointType = .right_Bottom
  69. } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame)/2 - 6, 0, 12, 12), emptyPoint) {
  70. pointType = .bottom_Center
  71. } else if CGRectContainsPoint(CGRectInset(emptyView.bounds, 12, 12), emptyPoint) {
  72. pointType = .center
  73. }
  74. }
  75. } else if gesture.state == .ended {
  76. stopGesture = false
  77. pointType = .none
  78. }
  79. if stopGesture == true ||
  80. pointType == .none {
  81. return
  82. }
  83. print(point.x-startPoint.x, point.y-startPoint.y)
  84. if gesture.state == .began {
  85. } else if gesture.state == .changed {
  86. let offsetX = point.x-startPoint.x
  87. let offsetY = point.y-startPoint.y
  88. var rect = initialFrame
  89. if pointType == .left_Bottom {
  90. if rect.origin.x + offsetX < 0 {
  91. rect.origin.x = 0
  92. rect.size.width = initialFrame.origin.x + rect.size.width
  93. } else {
  94. rect.origin.x += offsetX
  95. rect.size.width -= offsetX
  96. }
  97. if rect.origin.y + offsetY < 0 {
  98. rect.origin.y = 0
  99. rect.size.height = initialFrame.origin.y + rect.size.height
  100. } else {
  101. rect.origin.y += offsetY
  102. rect.size.height -= offsetY
  103. }
  104. } else if pointType == .left_Center {
  105. if rect.origin.x + offsetX < 0 {
  106. rect.origin.x = 0
  107. rect.size.width = initialFrame.origin.x + rect.size.width
  108. } else {
  109. rect.origin.x += offsetX
  110. rect.size.width -= offsetX
  111. }
  112. } else if pointType == .left_Top {
  113. if rect.origin.x + offsetX < 0 {
  114. rect.origin.x = 0
  115. rect.size.width = initialFrame.origin.x + rect.size.width
  116. } else {
  117. rect.origin.x += offsetX
  118. rect.size.width -= offsetX
  119. }
  120. rect.size.height += offsetY
  121. } else if pointType == .top_Center {
  122. rect.size.height += offsetY
  123. } else if pointType == .right_Top {
  124. rect.size.width += offsetX
  125. rect.size.height += offsetY
  126. } else if pointType == .right_Center {
  127. rect.size.width += offsetX
  128. } else if pointType == .right_Bottom {
  129. rect.size.width += offsetX
  130. if rect.origin.y + offsetY < 0 {
  131. rect.origin.y = 0
  132. rect.size.height = initialFrame.origin.y + rect.size.height
  133. } else {
  134. rect.origin.y += offsetY
  135. rect.size.height -= offsetY
  136. }
  137. } else if pointType == .bottom_Center {
  138. if rect.origin.y + offsetY < 0 {
  139. rect.origin.y = 0
  140. rect.size.height = initialFrame.origin.y + rect.size.height
  141. } else {
  142. rect.origin.y += offsetY
  143. rect.size.height -= offsetY
  144. }
  145. } else if pointType == .center {
  146. rect.origin.x += offsetX
  147. rect.origin.y += offsetY
  148. if CGRectGetMaxX(rect) > CGRectGetWidth(self.view.bounds) {
  149. rect.origin.x = CGRectGetWidth(self.view.bounds) - rect.size.width
  150. }
  151. if CGRectGetMaxY(rect) > CGRectGetHeight(self.view.bounds) {
  152. rect.origin.y = CGRectGetHeight(self.view.bounds) - rect.size.height
  153. }
  154. if rect.origin.x < 0 {
  155. rect.origin.x = 0
  156. }
  157. if rect.origin.y < 0 {
  158. rect.origin.y = 0
  159. }
  160. }
  161. if pointType == .center {
  162. } else {
  163. // if rect.origin.x < 0 {
  164. // rect.origin.x = 0
  165. // }
  166. // if rect.origin.y < 0 {
  167. // rect.origin.y = 0
  168. // }
  169. if CGRectGetMaxX(rect) > CGRectGetWidth(self.view.bounds) {
  170. rect.size.width = CGRectGetWidth(self.view.bounds) - rect.origin.x
  171. }
  172. if CGRectGetMaxY(rect) > CGRectGetHeight(self.view.bounds) {
  173. rect.size.height = CGRectGetHeight(self.view.bounds) - rect.origin.y
  174. }
  175. rect.size.height = max(0, rect.size.height)
  176. rect.size.width = max(0, rect.size.width)
  177. }
  178. print(rect)
  179. emptyView.frame = rect
  180. }
  181. }
  182. }