// // KMCropTipView.swift // PDF Master // // Created by tangchao on 2023/1/4. // import Cocoa typealias KMCropTipViewEnterAction = ()->() class KMCropTipView: NSView { private var contentView = NSView() private var label = NSTextField(labelWithString: "") private var localMonitor: Any! var enterAction: KMCropTipViewEnterAction! deinit { NSEvent.removeMonitor(self.localMonitor as Any) self.localMonitor = nil } override init(frame frameRect: NSRect) { super.init(frame: frameRect) initSubviews() } required init?(coder: NSCoder) { super.init(coder: coder) initSubviews() } func initSubviews() { self.addSubview(self.contentView) self.contentView.addSubview(self.label) self.contentView.wantsLayer = true self.contentView.layer?.backgroundColor = NSColor(red: 17.0/255.0, green: 138.0/255.0, blue: 1.0, alpha: 1.0).cgColor self.label.textColor = NSColor.white self.label.font = NSFont.systemFont(ofSize: 14) self.label.alignment = .center self.localMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyUp, handler: { [weak self] (event: NSEvent) -> NSEvent in if (event.keyCode != 36) { return event } guard let callback = self!.enterAction else { return event } callback() return event }) } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let height: CGFloat = NSHeight(self.bounds) let contentWidth: CGFloat = 186 let contentHeight: CGFloat = 32 self.contentView.frame = NSMakeRect((width-contentWidth)*0.5, (height-contentHeight)*0.5, contentWidth, contentHeight) self.label.frame = NSMakeRect(0, 5, contentWidth, 22) } func setString(string: String) { self.label.stringValue = string } }