// // KMPostionIndicateView.swift // PDF Reader Pro // // Created by lizhe on 2023/11/22. // import Cocoa enum KMPositionIndicateViewStyle: Int { case topLeft = 0 case topCenter = 1 case topRight = 2 case middleLeft = 3 case middleCenter = 4 case middleRight = 5 case bottomLeft = 6 case bottomCenter = 7 case bottomRight = 8 } class KMPostionIndicateView: NSView { var styleChangedCallBack: (() -> Void)? var isTile: Bool = false var style: KMPositionIndicateViewStyle = .middleCenter override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) guard let context = NSGraphicsContext.current?.cgContext else { return } for i in 0..<3 { for j in 0..<3 { context.saveGState() context.translateBy(x: CGFloat(28 * j), y: CGFloat(Int(bounds.height) - 28 * i - 24)) context.addRect(CGRect(x: 0, y: 0, width: 24, height: 24)) if style == KMPositionIndicateViewStyle(rawValue: j + 3 * i) { context.setFillColor(KMAppearance.Interactive.a0Color().cgColor) } else { context.setFillColor(KMAppearance.KM_DBDBDB_Color().cgColor) } context.fillPath() context.restoreGState() } } } override func mouseDown(with event: NSEvent) { let eventLocation = event.locationInWindow let localPoint = convert(eventLocation, from: nil) if isTile { return } let newStyle = styleForPoint(localPoint) if style != newStyle, let styleChangedCallBack = styleChangedCallBack { style = newStyle styleChangedCallBack() setNeedsDisplay(bounds) } } func styleForPoint(_ point: NSPoint) -> KMPositionIndicateViewStyle { for i in 0..<3 { for j in 0..<3 { let rect = NSRect(x: CGFloat(28 * j), y: CGFloat(Int(bounds.height) - 28 * i - 24), width: 24, height: 24) if rect.contains(point) { return KMPositionIndicateViewStyle(rawValue: j + 3 * i) ?? .middleCenter } } } return .middleCenter } }