// // KMTextfieldButton.swift // PDF Master // // Created by lizhe on 2023/2/15. // import Cocoa typealias MouseDownAction = (_ button: NSButton, _ itemString: String) -> () class KMTextfieldButton: KMBaseXibView { @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var imageView: NSImageView! @IBOutlet weak var button: NSButton! var createFilePopover: NSPopover? var data: [String] = [] var mouseDownAction: MouseDownAction? var popWidth: CGFloat? var stringValue: String = "" { didSet { self.titleLabel.stringValue = stringValue } } var imageName: String = "" { didSet { self.imageView.image = NSImage(named: imageName) } } var isEnabled: Bool = true { didSet { self.titleLabel.isEnabled = isEnabled self.titleLabel.alphaValue = isEnabled ? 1 : 0.5 self.imageView.isEnabled = isEnabled self.imageView.alphaValue = isEnabled ? 1 : 0.5 self.button.isEnabled = isEnabled self.button.alphaValue = isEnabled ? 1 : 0.5 } } var font: NSFont? { didSet { self.titleLabel.font = font } } var textColor: NSColor? { didSet { self.titleLabel.textColor = textColor } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func setup() { super.setup() } override func reloadData() { super.reloadData() } @IBAction func buttonAction(_ sender: NSButton) { if self.data.count != 0 { var popViewDataArr: [String] = [] for string in self.data { popViewDataArr.append(NSLocalizedString(string, comment: "")) } let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr) let createFilePopover: NSPopover = NSPopover.init() createFilePopover.contentViewController = vc createFilePopover.animates = true createFilePopover.behavior = .semitransient createFilePopover.setValue(true, forKey: "shouldHideAnchor") createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: -10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY) vc.customBoxWidthLayoutConstraint.constant = self.popWidth ?? sender.frame.width vc.downCallback = { [weak self](downEntered: Bool, count: String) -> Void in if downEntered { if self != nil { if self!.mouseDownAction != nil { self!.mouseDownAction!(sender, count) } } createFilePopover.close() } } } else { if self.mouseDownAction != nil { self.mouseDownAction!(sender, "") } } } }