123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //
- // KMCropCoverController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/20.
- //
- import Cocoa
- import KMComponentLibrary
- @objc public enum CropPointPosition: Int {
- case none = 0
- case left_Top
- case left_Center
- case left_Bottom
- case top_Center
- case right_Top
- case right_Center
- case right_Bottom
- case bottom_Center
- case center
-
- }
- class ResizableView: NSView {
-
- }
- class KMCropCoverController: NSViewController {
-
- var emptyView: ResizableView!
-
- var stopGesture: Bool = false
- var initialFrame: NSRect = NSRect.zero
-
- var startPoint: NSPoint = .zero
- var pointType: CropPointPosition = .none
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- setup()
- }
-
- func setup() {
-
- view.wantsLayer = true
- // view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/mask").cgColor
- view.layer?.backgroundColor = NSColor.red.cgColor
-
- // 创建空白 View
- emptyView = ResizableView(frame: NSRect(x: 100, y: 100, width: 200, height: 200))
- emptyView.wantsLayer = true
- emptyView.layer?.backgroundColor = NSColor.white.cgColor
- view.addSubview(emptyView)
-
- let panGesture = NSPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
- view.addGestureRecognizer(panGesture)
-
- }
-
- @objc func handlePan(_ gesture: NSPanGestureRecognizer) {
- let point = gesture.location(in: self.view)
- let emptyPoint = self.view.convert(point, to: emptyView)
-
- if gesture.state == .began {
- startPoint = point
- initialFrame = emptyView.frame
-
- if CGRectContainsPoint(emptyView.frame, point) == false {
- stopGesture = true
- } else {
- if CGRectContainsPoint(CGRectMake(0, 0, 12, 12), emptyPoint) {
- pointType = .left_Bottom
- } else if CGRectContainsPoint(CGRectMake(0, CGRectGetHeight(emptyView.frame)/2 - 6, 12, 12), emptyPoint) {
- pointType = .left_Center
- } else if CGRectContainsPoint(CGRectMake(0, CGRectGetHeight(emptyView.frame) - 12, 12, 12), emptyPoint) {
- pointType = .left_Top
- } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame)/2 - 6, CGRectGetHeight(emptyView.frame) - 12, 12, 12), emptyPoint) {
- pointType = .top_Center
- } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame) - 12, CGRectGetHeight(emptyView.frame) - 12, 12, 12), emptyPoint) {
- pointType = .right_Top
- } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame) - 12, CGRectGetHeight(emptyView.frame)/2 - 6, 12, 12), emptyPoint) {
- pointType = .right_Center
- } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame) - 12, 0, 12, 12), emptyPoint) {
- pointType = .right_Bottom
- } else if CGRectContainsPoint(CGRectMake(CGRectGetWidth(emptyView.frame)/2 - 6, 0, 12, 12), emptyPoint) {
- pointType = .bottom_Center
- } else if CGRectContainsPoint(CGRectInset(emptyView.bounds, 12, 12), emptyPoint) {
- pointType = .center
- }
- }
- } else if gesture.state == .ended {
- stopGesture = false
- pointType = .none
- }
-
- if stopGesture == true ||
- pointType == .none {
- return
- }
-
- print(point.x-startPoint.x, point.y-startPoint.y)
-
- if gesture.state == .began {
-
- } else if gesture.state == .changed {
- let offsetX = point.x-startPoint.x
- let offsetY = point.y-startPoint.y
-
- var rect = initialFrame
-
- if pointType == .left_Bottom {
- if rect.origin.x + offsetX < 0 {
- rect.origin.x = 0
- rect.size.width = initialFrame.origin.x + rect.size.width
- } else {
- rect.origin.x += offsetX
- rect.size.width -= offsetX
- }
-
- if rect.origin.y + offsetY < 0 {
- rect.origin.y = 0
- rect.size.height = initialFrame.origin.y + rect.size.height
- } else {
- rect.origin.y += offsetY
- rect.size.height -= offsetY
- }
- } else if pointType == .left_Center {
- if rect.origin.x + offsetX < 0 {
- rect.origin.x = 0
- rect.size.width = initialFrame.origin.x + rect.size.width
- } else {
- rect.origin.x += offsetX
- rect.size.width -= offsetX
- }
- } else if pointType == .left_Top {
- if rect.origin.x + offsetX < 0 {
- rect.origin.x = 0
- rect.size.width = initialFrame.origin.x + rect.size.width
- } else {
- rect.origin.x += offsetX
- rect.size.width -= offsetX
- }
-
- rect.size.height += offsetY
- } else if pointType == .top_Center {
- rect.size.height += offsetY
- } else if pointType == .right_Top {
- rect.size.width += offsetX
- rect.size.height += offsetY
- } else if pointType == .right_Center {
- rect.size.width += offsetX
- } else if pointType == .right_Bottom {
- rect.size.width += offsetX
-
- if rect.origin.y + offsetY < 0 {
- rect.origin.y = 0
- rect.size.height = initialFrame.origin.y + rect.size.height
- } else {
- rect.origin.y += offsetY
- rect.size.height -= offsetY
- }
- } else if pointType == .bottom_Center {
- if rect.origin.y + offsetY < 0 {
- rect.origin.y = 0
- rect.size.height = initialFrame.origin.y + rect.size.height
- } else {
- rect.origin.y += offsetY
- rect.size.height -= offsetY
- }
- } else if pointType == .center {
- rect.origin.x += offsetX
- rect.origin.y += offsetY
-
- if CGRectGetMaxX(rect) > CGRectGetWidth(self.view.bounds) {
- rect.origin.x = CGRectGetWidth(self.view.bounds) - rect.size.width
- }
- if CGRectGetMaxY(rect) > CGRectGetHeight(self.view.bounds) {
- rect.origin.y = CGRectGetHeight(self.view.bounds) - rect.size.height
- }
- if rect.origin.x < 0 {
- rect.origin.x = 0
- }
- if rect.origin.y < 0 {
- rect.origin.y = 0
- }
- }
-
- if pointType == .center {
-
- } else {
- // if rect.origin.x < 0 {
- // rect.origin.x = 0
- // }
- // if rect.origin.y < 0 {
- // rect.origin.y = 0
- // }
- if CGRectGetMaxX(rect) > CGRectGetWidth(self.view.bounds) {
- rect.size.width = CGRectGetWidth(self.view.bounds) - rect.origin.x
- }
- if CGRectGetMaxY(rect) > CGRectGetHeight(self.view.bounds) {
- rect.size.height = CGRectGetHeight(self.view.bounds) - rect.origin.y
- }
- rect.size.height = max(0, rect.size.height)
- rect.size.width = max(0, rect.size.width)
- }
-
-
- print(rect)
-
- emptyView.frame = rect
-
- }
- }
- }
|