1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- //
- // 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(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(hex: "#CED0D4").setStroke()
- path.stroke()
- }
- }
-
- override func updateTrackingAreas() {
- super.updateTrackingAreas()
- 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
- }
- }
- }
|