KMSegmentedControl.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // KMSegmentedControl.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/11/16.
  6. //
  7. import Cocoa
  8. class KMSegmentedControl: NSControl {
  9. private var _segmentCount: UInt8 = 0
  10. var segmentCount: UInt8 {
  11. get {
  12. return self._segmentCount
  13. }
  14. set {
  15. self._segmentCount = newValue
  16. self.setSegmentCount(self.segmentCount, with: 55)
  17. }
  18. }
  19. private var _selectedSegment: UInt8 = 0
  20. var selectedSegment: UInt8 {
  21. get {
  22. return self._selectedSegment
  23. }
  24. set {
  25. self._selectedSegment = newValue
  26. if (self.block != nil) {
  27. self.block!(self.selectedSegment)
  28. }
  29. for i in 0 ..< self.segments.count {
  30. let button = self.segments[i]
  31. if (self.isBackgroundHighlighted) {
  32. if (i == self.selectedSegment) {
  33. button.wantsLayer = true
  34. button.layer?.cornerRadius = 6.0
  35. if (KMAppearance.isDarkMode()) {
  36. button.layer?.backgroundColor = NSColor(red: 0.133, green: 0.478, blue: 1, alpha: 0.3).cgColor
  37. } else {
  38. button.layer?.backgroundColor = NSColor(red: 0.286, green: 0.510, blue: 0.902, alpha: 0.2).cgColor
  39. }
  40. if (i == 0) {
  41. button.image = NSImage(named: KMImageNameUXIconBtnSidebarListPre)
  42. } else if (i == 1) {
  43. button.image = NSImage(named: KMImageNameUXIconBtnSidebarPagePre)
  44. }
  45. } else {
  46. button.wantsLayer = true;
  47. button.layer?.backgroundColor = .clear
  48. if (i == 0) {
  49. button.image = NSImage(named: KMImageNameUXIconBtnSidebarListNor)
  50. } else if (i == 1) {
  51. button.image = NSImage(named: KMImageNameUXIconBtnSidebarPageNor)
  52. }
  53. }
  54. } else {
  55. if (i == self.selectedSegment) {
  56. if (i == 0) {
  57. button.image = NSImage(named: KMImageNameUXIconSidetabbarThumbnailSel)
  58. } else if (i == 1) {
  59. button.image = NSImage(named: KMImageNameUXIconSidetabbarOutlineSel)
  60. } else if (i == 2) {
  61. button.image = NSImage(named: KMImageNameUXIconSidetabbarAnnotationSel)
  62. } else if (i == 3) {
  63. button.image = NSImage(named: KMImageNameUXIconSidetabbarSnapshotSel)
  64. } else if (i == 4) {
  65. button.image = NSImage(named: KMImageNameUXIconSidetabbarSearchSel)
  66. }
  67. button.wantsLayer = true
  68. if (KMAppearance.isDarkMode()) {
  69. button.layer?.backgroundColor = NSColor(red: 0.133, green: 0.478, blue: 1.000, alpha: 1).cgColor
  70. } else {
  71. button.layer?.backgroundColor = NSColor(red: 0.286, green: 0.510, blue: 0.902, alpha: 1).cgColor
  72. }
  73. } else {
  74. if (i == 0) {
  75. button.image = NSImage(named: KMImageNameUXIconSidetabbarThumbnailNor)
  76. } else if (i == 1) {
  77. button.image = NSImage(named: KMImageNameUXIconSidetabbarOutlineNor)
  78. } else if (i == 2) {
  79. button.image = NSImage(named: KMImageNameUXIconSidetabbarAnnotationNor)
  80. } else if (i == 3) {
  81. button.image = NSImage(named: KMImageNameUXIconSidetabbarSnapshotNor)
  82. } else if (i == 4) {
  83. button.image = NSImage(named: KMImageNameUXIconSidetabbarSearchNor)
  84. }
  85. button.wantsLayer = true
  86. if (KMAppearance.isDarkMode()) {
  87. button.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  88. } else {
  89. button.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. var isBackgroundHighlighted = false
  97. var block: ((_ segmentCount: UInt8) -> Void)?
  98. var buttonActionHandle: ((_ segmentCount: UInt8) -> Void)?
  99. private var _segments: [NSButton] = []
  100. var segments: [NSButton] {
  101. get {
  102. return self._segments
  103. }
  104. }
  105. private var _contentView: NSView?
  106. var contentView: NSView? {
  107. get {
  108. return self._contentView
  109. }
  110. }
  111. override func updateLayer() {
  112. super.updateLayer()
  113. if #available(macOS 10.14, *) {
  114. self.selectedSegment = self._selectedSegment
  115. }
  116. }
  117. // MARK: - Public Methods
  118. func setSegmentCount(_ segmentCount: uint8, with width: Float) {
  119. self._segmentCount = segmentCount
  120. let height = self.frame.size.height
  121. if (self.contentView?.superview != nil) {
  122. self.contentView?.removeFromSuperview()
  123. }
  124. self._contentView = NSView()
  125. let x = (self.frame.size.width - (Float(self.segmentCount) * width).cgFloat) / 2.0
  126. let y = (self.frame.size.height-height)/2.0
  127. self._contentView?.frame = NSMakeRect(x, y, (Float(self.segmentCount)*width).cgFloat, height)
  128. self._contentView?.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  129. self.addSubview(self.contentView!)
  130. self._segments.removeAll()
  131. for i in 0 ..< self.segmentCount {
  132. let button = NSButton()
  133. button.bezelStyle = .regularSquare
  134. button.isBordered = false
  135. button.imageScaling = .scaleProportionallyDown
  136. button.imagePosition = .imageOnly
  137. button.frame = NSMakeRect((Float(i)*width).cgFloat, 0, width.cgFloat, height)
  138. button.tag = Int(i)
  139. button.target = self
  140. button.action = #selector(_buttonAction)
  141. self.contentView?.addSubview(button)
  142. self._segments.append(button)
  143. }
  144. }
  145. func setImage(_ image: NSImage, for segment: UInt8) {
  146. if (segment >= self.segments.count) {
  147. return
  148. }
  149. let button = self.segments[Int(segment)]
  150. button.image = image
  151. button.alternateImage = image
  152. }
  153. func setToolTip(_ toolTip: String, for segment: uint8) {
  154. if (segment >= self.segments.count) {
  155. return
  156. }
  157. let button = self.segments[Int(segment)]
  158. button.toolTip = toolTip
  159. }
  160. }
  161. // MARK: - Private Methods
  162. extension KMSegmentedControl {
  163. @objc private func _buttonAction(_ sender: NSButton) {
  164. self.selectedSegment = UInt8(sender.tag)
  165. if (self.buttonActionHandle != nil) {
  166. self.buttonActionHandle!(self.selectedSegment)
  167. }
  168. }
  169. /*
  170. - (NSImage *)image:(NSImage *)image withColor:(NSColor *)color {
  171. NSImage *newImage = [image copy];
  172. [newImage lockFocus];
  173. [color set];
  174. NSRectFillUsingOperation(NSMakeRect(0, 0, image.size.width, image.size.height), NSCompositingOperationSourceAtop);
  175. [newImage unlockFocus];
  176. return [newImage autorelease];
  177. }
  178. */
  179. }