123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // 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, "")
- }
- }
- }
- }
|