KMPreferenceSegementControl.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // KMPreferenceSegementControl.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/2/3.
  6. //
  7. import Cocoa
  8. typealias KMPreferenceSegementItemClick = (_ index: Int) -> ()
  9. class KMPreferenceSegementItem: NSView {
  10. var imageView = NSImageView()
  11. var label = NSTextField(labelWithString: "")
  12. var itemID: Int = 0
  13. var itemClick: KMPreferenceSegementItemClick?
  14. var selectedImage: NSImage?
  15. var unSelectedImage: NSImage?
  16. var selectedTextColor: NSColor?
  17. var unSelectedTextColor: NSColor?
  18. private var mySelected: Bool = false
  19. var isSelected: Bool {
  20. get {
  21. return self.mySelected
  22. }
  23. set {
  24. self.mySelected = newValue
  25. if (newValue) {
  26. self.layer?.backgroundColor = NSColor(white: 0, alpha: 0.05).cgColor
  27. self.imageView.image = self.selectedImage
  28. self.label.textColor = self.selectedTextColor
  29. } else {
  30. self.layer?.backgroundColor = NSColor.clear.cgColor
  31. self.imageView.image = self.unSelectedImage
  32. self.label.textColor = self.unSelectedTextColor
  33. }
  34. }
  35. }
  36. override init(frame frameRect: NSRect) {
  37. super.init(frame: frameRect)
  38. initSubViews()
  39. }
  40. required init?(coder: NSCoder) {
  41. super.init(coder: coder)
  42. initSubViews()
  43. }
  44. override var isFlipped: Bool {
  45. return true
  46. }
  47. public func initSubViews() {
  48. self.addSubview(self.imageView)
  49. self.addSubview(self.label)
  50. self.label.alignment = .center
  51. self.wantsLayer = true
  52. self.layer?.cornerRadius = 7
  53. /// 新增点击手势
  54. let gesture = NSClickGestureRecognizer(target: self, action: #selector(gestureAction))
  55. self.addGestureRecognizer(gesture)
  56. }
  57. override func layout() {
  58. super.layout()
  59. let width: CGFloat = NSWidth(self.bounds)
  60. let size: CGFloat = 17
  61. self.imageView.frame = NSMakeRect((width-size)*0.5, 6, size, size)
  62. self.label.frame = NSMakeRect(0, self.imageView.frame.maxY+5, width, 14)
  63. }
  64. @objc private func gestureAction() {
  65. guard let callback = self.itemClick else {
  66. return
  67. }
  68. callback(self.itemID)
  69. }
  70. }
  71. class KMPreferenceSegementControl: NSControl {
  72. var items: Array<KMPreferenceSegementItem> = []
  73. var selectedIndex: Int = 0
  74. var selectedItem: KMPreferenceSegementItem?
  75. override init(frame frameRect: NSRect) {
  76. super.init(frame: frameRect)
  77. initSubViews()
  78. }
  79. required init?(coder: NSCoder) {
  80. super.init(coder: coder)
  81. initSubViews()
  82. }
  83. override var isFlipped: Bool {
  84. return true
  85. }
  86. func initSubViews() {
  87. for item in self.items {
  88. item.removeFromSuperview()
  89. }
  90. self.items.removeAll()
  91. for i in 0 ... 2 {
  92. let item = KMPreferenceSegementItem()
  93. item.itemID = i + 1
  94. self.addSubview(item)
  95. self.items.append(item)
  96. item.itemClick = { [weak self] index in
  97. self?.selectItem(index)
  98. let _ = self?.target?.perform(self?.action, with: self)
  99. }
  100. }
  101. }
  102. override func layout() {
  103. super.layout()
  104. let width: CGFloat = NSWidth(self.bounds)
  105. let itemSize: NSSize = NSSize(width: 75, height: 45)
  106. let leftMargin: CGFloat = (width-CGFloat(self.items.count)*itemSize.width)*0.5
  107. let topMargin: CGFloat = 8
  108. var preItem: KMPreferenceSegementItem?
  109. for item in self.items {
  110. if (preItem != nil) {
  111. item.frame = NSMakeRect((preItem?.frame.maxX)!, topMargin, itemSize.width, itemSize.height)
  112. } else {
  113. item.frame = NSMakeRect(leftMargin, topMargin, itemSize.width, itemSize.height)
  114. }
  115. preItem = item
  116. }
  117. }
  118. public func selectItem(_ index: Int) {
  119. self.selectedIndex = index
  120. for item in items {
  121. if (item.itemID == index) {
  122. item.isSelected = true
  123. self.selectedItem = item
  124. } else {
  125. item.isSelected = false
  126. }
  127. }
  128. }
  129. }