KMPopMenuButton.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // KMPopMenuButton.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. class KMPopMenuButton: NSButton {
  9. override init(frame frameRect: NSRect) {
  10. super.init(frame: frameRect)
  11. self.setupButton()
  12. }
  13. required init?(coder: NSCoder) {
  14. super.init(coder: coder)
  15. self.setupButton()
  16. }
  17. private func setupButton() {
  18. self.addTrackingArea()
  19. self.wantsLayer = true
  20. self.layer?.backgroundColor = NSColor.clear.cgColor
  21. self.font = NSFont.systemFont(ofSize: 14)
  22. }
  23. // override class var cellClass: AnyClass? {
  24. // return KMPopMenuButtonCell.self
  25. // }
  26. //
  27. func addTrackingArea() {
  28. let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self, userInfo: nil)
  29. self.addTrackingArea(trackingArea)
  30. }
  31. override func mouseEntered(with event: NSEvent) {
  32. super.mouseEntered(with: event)
  33. if self.isEnabled {
  34. if #available(macOS 10.14, *) {
  35. self.layer?.backgroundColor = NSColor.controlAccentColor.cgColor
  36. } else {
  37. self.layer?.backgroundColor = NSColor.blue.cgColor
  38. }
  39. self.setTitleColorWithColor(NSColor.white, font: nil)
  40. }
  41. }
  42. override func mouseExited(with event: NSEvent) {
  43. super.mouseExited(with: event)
  44. if self.isEnabled {
  45. self.layer?.backgroundColor = NSColor.clear.cgColor
  46. self.setTitleColorWithColor(NSColor.labelColor, font: nil)
  47. }
  48. }
  49. override var intrinsicContentSize: NSSize {
  50. var size = super.intrinsicContentSize
  51. size.width += 45
  52. return size
  53. }
  54. override var state: NSControl.StateValue {
  55. didSet {
  56. if state == .on {
  57. if #available(macOS 10.14, *) {
  58. self.layer?.backgroundColor = NSColor.controlAccentColor.cgColor
  59. } else {
  60. self.layer?.backgroundColor = NSColor.blue.cgColor
  61. }
  62. self.setTitleColorWithColor(NSColor.white, font: nil)
  63. } else {
  64. self.layer?.backgroundColor = NSColor.clear.cgColor
  65. self.setTitleColorWithColor(NSColor.labelColor, font: nil)
  66. }
  67. }
  68. }
  69. // override func setTitle(_ title: String) {
  70. // super.title = title
  71. // self.setTitleColorWithColor(NSColor.labelColor, font: nil)
  72. // }
  73. func setTitleColorWithColor(_ color: NSColor?, font: NSFont?) {
  74. // 设置按钮文本颜色和字体
  75. if let color = color {
  76. let titleAttributes: [NSAttributedString.Key: Any] = [
  77. .foregroundColor: color
  78. ]
  79. self.attributedTitle = NSAttributedString(string: self.title, attributes: titleAttributes)
  80. }
  81. if let font = font {
  82. self.font = font
  83. }
  84. }
  85. }