KMImageTitleButton.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // KMImageTitleButton.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/1/18.
  6. //
  7. import Cocoa
  8. typealias KMImageTitleButtonAction = (_ view: KMImageTitleButton, _ button: NSButton) -> ()
  9. @objcMembers class KMImageTitleButton: BaseXibView {
  10. @IBOutlet weak var iconImage: NSImageView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var contentButton: NSButton!
  13. var action: KMImageTitleButtonAction?
  14. var title: String = "" {
  15. didSet {
  16. self.reloadData()
  17. }
  18. }
  19. var imageName: String = "" {
  20. didSet {
  21. selectImageName = imageName
  22. self.reloadData()
  23. }
  24. }
  25. var selectImageName: String = "" {
  26. didSet {
  27. self.reloadData()
  28. }
  29. }
  30. var isEnabled: Bool = true {
  31. didSet {
  32. self.iconImage.isEnabled = isEnabled
  33. self.iconImage.alphaValue = isEnabled ? 1 : 0.5
  34. self.titleLabel.isEnabled = isEnabled
  35. self.titleLabel.alphaValue = isEnabled ? 1 : 0.5
  36. self.contentButton.isEnabled = isEnabled
  37. self.contentButton.alphaValue = isEnabled ? 1 : 0.5
  38. }
  39. }
  40. var font: NSFont = NSFont.SFProTextRegularFont(14) {
  41. didSet {
  42. self.reloadData()
  43. }
  44. }
  45. var textColor: NSColor = NSColor.km_init(hex: "#252629") {
  46. didSet {
  47. self.reloadData()
  48. }
  49. }
  50. var state: NSControl.StateValue = .off {
  51. didSet {
  52. self.contentButton.state = state
  53. self.reloadData()
  54. }
  55. }
  56. override func draw(_ dirtyRect: NSRect) {
  57. super.draw(dirtyRect)
  58. // Drawing code here.
  59. }
  60. func setup() {
  61. }
  62. func reloadData() {
  63. self.titleLabel.stringValue = NSLocalizedString(self.title, comment: "")
  64. self.titleLabel.wantsLayer = true
  65. self.titleLabel.font = self.font
  66. self.titleLabel.textColor = self.textColor
  67. self.titleLabel.lineBreakMode = .byWordWrapping
  68. let paragraphStyle = NSMutableParagraphStyle()
  69. paragraphStyle.lineHeightMultiple = 1.32
  70. self.titleLabel.attributedStringValue = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : self.textColor])
  71. if self.state == .on {
  72. self.iconImage.image = NSImage(named: self.selectImageName)
  73. } else {
  74. self.iconImage.image = NSImage(named: self.imageName)
  75. }
  76. }
  77. @IBAction func contentButtonAction(_ sender: Any) {
  78. self.reloadData()
  79. if action != nil {
  80. self.action!(self, self.contentButton)
  81. }
  82. }
  83. }