12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // KMCustomButton.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/12/18.
- //
- import Cocoa
- @objcMembers class KMCustomButton: NSButton {
-
- @objc var mouseMoveCallback: ((_ mouseEntered: Bool)->Void)?
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- if self.isHighlighted {
- let radius = self.layer?.cornerRadius ?? 0
- let context = NSGraphicsContext.current
- context?.saveGraphicsState()
- NSBezierPath(roundedRect: dirtyRect, xRadius: radius, yRadius: radius).setClip()
- NSColor(deviceRed: 0, green: 0, blue: 0, alpha: 0.15).set()
- dirtyRect.fill(using: .sourceOver)
- context?.restoreGraphicsState()
- }
- }
-
- override func viewDidMoveToWindow() {
- super.viewDidMoveToWindow()
-
- if self.window != nil {
- self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
- }
- }
-
- override func setFrameSize(_ newSize: NSSize) {
- super.setFrameSize(newSize)
-
- self.addTrackingRect(NSMakeRect(0, 0, newSize.width, newSize.height), owner: self, userData: nil, assumeInside: false)
- }
-
- override func mouseEntered(with event: NSEvent) {
- guard let callback = self.mouseMoveCallback else {
- return
- }
- callback(true)
- }
-
- override func mouseExited(with event: NSEvent) {
- guard let callback = self.mouseMoveCallback else {
- return
- }
- callback(false)
- }
- }
|