// // KMAngleIndicateView.swift // PDF Reader Pro // // Created by lizhe on 2023/11/22. // import Cocoa enum KMAngleIndicateViewStyle { case left45 case horizontal case right45 } class KMAngleIndicateView: NSView { var touchCallBack: (() -> Void)? private var gradient: NSGradient? var isSelcted: Bool = false { didSet { if isSelcted { layer?.borderWidth = 0.5 } else { layer?.borderWidth = 0 } needsDisplay = true } } var style: KMAngleIndicateViewStyle = .horizontal override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) if isSelcted { let path = NSBezierPath(roundedRect: bounds, xRadius: 1, yRadius: 1) gradient?.draw(in: path, relativeCenterPosition: NSPoint(x: 0, y: 1)) wantsLayer = true layer?.backgroundColor = NSColor.clear.cgColor layer?.cornerRadius = 0 layer?.borderWidth = 0 layer?.borderColor = NSColor.clear.cgColor shadow = NSShadow() layer?.shadowColor = NSColor.clear.cgColor layer?.shadowOffset = CGSize(width: 0, height: 0) layer?.shadowRadius = 0 } else { wantsLayer = true layer?.backgroundColor = KMAppearance.KM_FFFFFF_Color35().cgColor layer?.cornerRadius = 1 layer?.borderWidth = 0.5 layer?.borderColor = KMAppearance.KM_000000_Color15Chang35().cgColor shadow = NSShadow() layer?.shadowColor = KMAppearance.KM_000000_Color20().cgColor layer?.shadowOffset = CGSize(width: 0, height: -1) layer?.shadowRadius = 0.5 } if let context = NSGraphicsContext.current?.cgContext { // Middle color block context.saveGState() if style == .left45 { context.translateBy(x: bounds.width / 2, y: bounds.height / 2) context.rotate(by: CGFloat.pi / 4) context.addRect(CGRect(x: -4, y: -6, width: 8, height: 12)) context.setFillColor(KMAppearance.KM_757780_Color().cgColor) context.fillPath() } else if style == .horizontal { context.translateBy(x: bounds.width / 2, y: bounds.height / 2) context.addRect(CGRect(x: -4, y: -6, width: 8, height: 12)) context.setFillColor(KMAppearance.KM_757780_Color().cgColor) context.fillPath() } else if style == .right45 { context.translateBy(x: bounds.width / 2, y: bounds.height / 2) context.rotate(by: -CGFloat.pi / 4) context.addRect(CGRect(x: -4, y: -6, width: 8, height: 12)) context.setFillColor(KMAppearance.KM_757780_Color().cgColor) context.fillPath() } context.restoreGState() } } override func awakeFromNib() { super.awakeFromNib() gradient = NSGradient(starting: KMAppearance.KM_EDECED_Color(), ending: KMAppearance.KM_D2D1D2_Color()) } override func mouseUp(with event: NSEvent) { if !isSelcted, let callback = touchCallBack { isSelcted = true callback() } } }