// // KMBox.swift // PDF Master // // Created by wanjun on 2022/10/13. // import Cocoa typealias mouseMoveCallback = (_ mouseEntered: Bool, _ mouseBox: KMBox) -> Void typealias mouseDownCallback = (_ downEntered: Bool, _ mouseBox: KMBox, _ event: NSEvent) -> Void typealias doubleClickCallback = (_ downEntered: Bool, _ mouseBox: KMBox) -> Void typealias mouseRightDownCallback = (_ downEntered: Bool, _ mouseBox: KMBox, _ event: NSEvent) -> Void @objcMembers class KMBox: NSBox { var moveCallback: mouseMoveCallback? var downCallback: mouseDownCallback? var doubleCallback: doubleClickCallback? var rightDownCallback: mouseRightDownCallback? var hoverColor : NSColor? // 悬浮颜色 var clickColor : NSColor? // 点击颜色 var defaultColor : NSColor? // 默认颜色 var canHover : Bool! = true // 是否允许悬浮 var canClick : Bool! = true // 是否允许点击 var canRightClick : Bool! = true // 是否允许右击 var canAutoHidden : Bool = false //是否需要自动隐藏 var isDottedLine: Bool = false var lineDash: [CGFloat] = [3, 3, 3] var dottedLineRadius = 8.0 var dottedLineWidth = 1.0 var dottedLineColor: NSColor = NSColor.km_init(hex: "#68ACF8") var canDoubleClick : Bool = false //是否运行多次点击 var area: NSTrackingArea? deinit { if (self.area != nil) { self.removeTrackingArea(self.area!) self.area = nil } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) if isDottedLine { let path = NSBezierPath(roundedRect: self.bounds, xRadius: dottedLineRadius, yRadius: dottedLineRadius) path.setLineDash(lineDash, count: 3, phase: 0) path.lineWidth = dottedLineWidth dottedLineColor.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!) // self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false) } override func mouseEntered(with event: NSEvent) { if canHover { if hoverColor != nil { fillColor = hoverColor! } if let callback = moveCallback { callback(true, self) } } } override func mouseExited(with event: NSEvent) { if canHover { if defaultColor != nil { fillColor = defaultColor! } if let callback = moveCallback { callback(false, self) } } } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) if canClick { if clickColor != nil { fillColor = clickColor! } if self.canDoubleClick { if (event.clickCount == 1) { if let callback = downCallback { callback(true, self, event) } } else if (event.clickCount > 1) { if let callback = doubleCallback { callback(true, self) } } } else { if let callback = downCallback { callback(true, self, event) } } } } override func mouseUp(with event: NSEvent) { super.mouseUp(with: event) if canClick { if clickColor != nil { fillColor = defaultColor! } if let callback = downCallback { callback(false, self, event) } } } override func rightMouseDown(with event: NSEvent) { super.rightMouseDown(with: event) if canRightClick { if let callback = rightDownCallback { callback(true, self, event) } } } }