KMTextfieldButton.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // KMTextfieldButton.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/2/15.
  6. //
  7. import Cocoa
  8. typealias MouseDownAction = (_ button: NSButton, _ itemString: String) -> ()
  9. class KMTextfieldButton: KMBaseXibView {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var imageView: NSImageView!
  12. @IBOutlet weak var button: NSButton!
  13. var createFilePopover: NSPopover?
  14. var data: [String] = []
  15. var mouseDownAction: MouseDownAction?
  16. var popWidth: CGFloat?
  17. var stringValue: String = "" {
  18. didSet {
  19. self.titleLabel.stringValue = stringValue
  20. }
  21. }
  22. var imageName: String = "" {
  23. didSet {
  24. self.imageView.image = NSImage(named: imageName)
  25. }
  26. }
  27. var isEnabled: Bool = true {
  28. didSet {
  29. self.titleLabel.isEnabled = isEnabled
  30. self.titleLabel.alphaValue = isEnabled ? 1 : 0.5
  31. self.imageView.isEnabled = isEnabled
  32. self.imageView.alphaValue = isEnabled ? 1 : 0.5
  33. self.button.isEnabled = isEnabled
  34. self.button.alphaValue = isEnabled ? 1 : 0.5
  35. }
  36. }
  37. var font: NSFont? {
  38. didSet {
  39. self.titleLabel.font = font
  40. }
  41. }
  42. var textColor: NSColor? {
  43. didSet {
  44. self.titleLabel.textColor = textColor
  45. }
  46. }
  47. override func draw(_ dirtyRect: NSRect) {
  48. super.draw(dirtyRect)
  49. // Drawing code here.
  50. }
  51. override func setup() {
  52. super.setup()
  53. }
  54. override func reloadData() {
  55. super.reloadData()
  56. }
  57. @IBAction func buttonAction(_ sender: NSButton) {
  58. if self.data.count != 0 {
  59. var popViewDataArr: [String] = []
  60. for string in self.data {
  61. popViewDataArr.append(NSLocalizedString(string, comment: ""))
  62. }
  63. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr)
  64. let createFilePopover: NSPopover = NSPopover.init()
  65. createFilePopover.contentViewController = vc
  66. createFilePopover.animates = true
  67. createFilePopover.behavior = .semitransient
  68. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  69. 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)
  70. vc.customBoxWidthLayoutConstraint.constant = self.popWidth ?? sender.frame.width
  71. vc.downCallback = { [weak self](downEntered: Bool, count: String) -> Void in
  72. if downEntered {
  73. if self != nil {
  74. if self!.mouseDownAction != nil {
  75. self!.mouseDownAction!(sender, count)
  76. }
  77. }
  78. createFilePopover.close()
  79. }
  80. }
  81. } else {
  82. if self.mouseDownAction != nil {
  83. self.mouseDownAction!(sender, "")
  84. }
  85. }
  86. }
  87. }