// // KMDottedLineView.swift // PDF Master // // Created by wanjun on 2023/5/19. // import Cocoa @objc enum KMDrawBorderState : Int { case None = 0 case Hover case Select } class KMDottedLineView: NSView { var state: KMDrawBorderState = .None var area: NSTrackingArea? var canHover : Bool! = true // 是否允许悬浮 var canClick : Bool! = true // 是否允许点击 deinit { if (self.area != nil) { self.removeTrackingArea(self.area!) self.area = nil } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) if ((self.state == .Hover) || (self.state == .Select)) { let path = NSBezierPath(roundedRect: self.bounds, xRadius: 8.0, yRadius: 8.0) path.lineWidth = 1.0 NSColor.km_init(hex: "#68ACF8").setStroke() path.stroke() } else { let path = NSBezierPath(roundedRect: self.bounds, xRadius: 8.0, yRadius: 8.0) path.setLineDash([3, 3, 3], count: 3, phase: 0) path.lineWidth = 1.0 NSColor.km_init(hex: "#CED0D4").setStroke() path.stroke() } } override func updateTrackingAreas() { super.updateTrackingAreas() if let _area = self.area, _area.rect.isEmpty == false { if (_area.rect.equalTo(self.bounds)) { return } } if (self.area != nil) { self.removeTrackingArea(self.area!) self.area = nil } self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self) self.addTrackingArea(self.area!) } override func mouseEntered(with event: NSEvent) { if canHover { self.state = .Hover self.needsDisplay = true } } override func mouseExited(with event: NSEvent) { if canHover { self.state = .None self.needsDisplay = true } } }