KMSegmentedControl.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // KMSegmentedControl.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/16.
  6. //
  7. import Cocoa
  8. enum KMSegmenteDirection: Int {
  9. case vertical = 0
  10. case horizontal = 1
  11. }
  12. class KMSegmentedControl: NSControl {
  13. private var _segmentCount: UInt8 = 0
  14. var segmentCount: UInt8 {
  15. get {
  16. return self._segmentCount
  17. }
  18. set {
  19. self._segmentCount = newValue
  20. self.setSegmentCount(self.segmentCount, with: 55)
  21. }
  22. }
  23. private var _selectedSegment: UInt8 = 0
  24. var selectedSegment: UInt8 {
  25. get {
  26. return self._selectedSegment
  27. }
  28. set {
  29. self._selectedSegment = newValue
  30. if (self.block != nil) {
  31. self.block!(self.selectedSegment)
  32. }
  33. self.reloadData()
  34. }
  35. }
  36. var isBackgroundHighlighted = false
  37. var block: ((_ segmentCount: UInt8) -> Void)?
  38. private var _segments: [NSButton] = []
  39. var segments: [NSButton] {
  40. get {
  41. return self._segments
  42. }
  43. }
  44. private var _contentView: NSView?
  45. var contentView: NSView? {
  46. get {
  47. return self._contentView
  48. }
  49. }
  50. var direction: KMSegmenteDirection = .horizontal {
  51. didSet {
  52. self.needsLayout = true
  53. }
  54. }
  55. var itemSize: NSSize = NSMakeSize(44, 44)
  56. override func updateLayer() {
  57. super.updateLayer()
  58. if #available(macOS 10.14, *) {
  59. // self.selectedSegment = self._selectedSegment
  60. }
  61. self.reloadData()
  62. }
  63. // MARK: - Public Methods
  64. func setSegmentCount(_ segmentCount: uint8, with width: Float) {
  65. self._segmentCount = segmentCount
  66. self.itemSize = NSMakeSize(width.cgFloat, width.cgFloat)
  67. let height = self.frame.size.height
  68. if (self.contentView?.superview != nil) {
  69. self.contentView?.removeFromSuperview()
  70. }
  71. self._contentView = NSView()
  72. let x = (self.frame.size.width - (Float(self.segmentCount) * width).cgFloat) / 2.0
  73. let y = (self.frame.size.height-height)/2.0
  74. self._contentView?.frame = NSMakeRect(x, 0, (Float(self.segmentCount)*width).cgFloat, height)
  75. self._contentView?.autoresizingMask = [.minXMargin, .maxXMargin, .maxYMargin, .height]
  76. self.addSubview(self.contentView!)
  77. self._segments.removeAll()
  78. for i in 0 ..< self.segmentCount {
  79. let button = KMCoverButton()
  80. button.wantsLayer = true
  81. button.bezelStyle = .regularSquare
  82. button.isBordered = false
  83. button.imageScaling = .scaleProportionallyDown
  84. button.imagePosition = .imageOnly
  85. // button.layer?.cornerRadius = 6.0
  86. button.frame = NSMakeRect((Float(i)*width).cgFloat, 0, width.cgFloat, height)
  87. button.tag = Int(i)
  88. button.target = self
  89. button.action = #selector(_buttonAction)
  90. self.contentView?.addSubview(button)
  91. self._segments.append(button)
  92. button.coverAction = { [unowned self] cbtn, action in
  93. if self.selectedSegment == cbtn.tag {
  94. return
  95. }
  96. if action == .enter {
  97. if KMAppearance.isDarkMode() {
  98. cbtn.layer?.backgroundColor = NSColor(named: "282A2D")?.cgColor
  99. } else {
  100. cbtn.layer?.backgroundColor = NSColor(named: "DFE1E5")?.cgColor
  101. }
  102. } else if action == .exit {
  103. cbtn.layer?.backgroundColor = .clear
  104. }
  105. }
  106. }
  107. }
  108. override var isFlipped: Bool {
  109. return true
  110. }
  111. func setImage(_ image: NSImage, for segment: UInt8) {
  112. if (segment >= self.segments.count) {
  113. return
  114. }
  115. let button = self.segments[Int(segment)]
  116. button.image = image
  117. button.alternateImage = image
  118. }
  119. func setToolTip(_ toolTip: String, for segment: uint8) {
  120. if (segment >= self.segments.count) {
  121. return
  122. }
  123. let button = self.segments[Int(segment)]
  124. button.toolTip = toolTip
  125. }
  126. override func layout() {
  127. super.layout()
  128. let width = NSWidth(self.contentView?.bounds ?? .zero)
  129. let height = NSHeight(self.contentView?.bounds ?? .zero)
  130. let itemSize = self.itemSize
  131. var itemY: CGFloat = 0
  132. for i in 0 ..< self.segments.count {
  133. let button = self.segments[i]
  134. if self.direction == .horizontal {
  135. button.frame = NSMakeRect((CGFloat(i)*itemSize.width), 0, itemSize.width, itemSize.height)
  136. } else {
  137. itemY = height - CGFloat(i)*itemSize.height - itemSize.height
  138. button.frame = NSMakeRect(0, itemY, itemSize.width, itemSize.height)
  139. }
  140. }
  141. }
  142. func reloadData() {
  143. let block = {
  144. self._reloadData()
  145. }
  146. if Thread.isMainThread {
  147. block()
  148. } else {
  149. Task { @MainActor in
  150. block()
  151. }
  152. }
  153. }
  154. }
  155. // MARK: - Private Methods
  156. extension KMSegmentedControl {
  157. @objc private func _buttonAction(_ sender: NSButton) {
  158. self.selectedSegment = UInt8(sender.tag)
  159. }
  160. private func _reloadData() {
  161. for (i, button) in self.segments.enumerated() {
  162. if (self.isBackgroundHighlighted) {
  163. if (i == self.selectedSegment) {
  164. if (KMAppearance.isDarkMode()) {
  165. button.layer?.backgroundColor = NSColor(red: 0.133, green: 0.478, blue: 1, alpha: 0.3).cgColor
  166. } else {
  167. button.layer?.backgroundColor = NSColor(red: 0.286, green: 0.510, blue: 0.902, alpha: 0.2).cgColor
  168. }
  169. if (i == 0) {
  170. button.image = NSImage(named: KMImageNameUXIconBtnSidebarListPre)
  171. } else if (i == 1) {
  172. button.image = NSImage(named: KMImageNameUXIconBtnSidebarPagePre)
  173. }
  174. } else {
  175. button.layer?.backgroundColor = .clear
  176. if (i == 0) {
  177. button.image = NSImage(named: KMImageNameUXIconBtnSidebarListNor)
  178. } else if (i == 1) {
  179. button.image = NSImage(named: KMImageNameUXIconBtnSidebarPageNor)
  180. }
  181. }
  182. } else {
  183. if (i == self.selectedSegment) {
  184. if (i == 0) {
  185. button.image = NSImage(named: KMImageNameUXIconSidetabbarThumbnailSel)
  186. } else if (i == 1) {
  187. button.image = NSImage(named: KMImageNameUXIconSidetabbarOutlineSel)
  188. } else if (i == 2) {
  189. button.image = NSImage(named: KMImageNameUXIconSidetabbarAnnotationSel)
  190. } else if (i == 3) {
  191. button.image = NSImage(named: KMImageNameUXIconSidetabbarSnapshotSel)
  192. } else if (i == 4) {
  193. button.image = NSImage(named: KMImageNameUXIconSidetabbarSearchSel)
  194. }
  195. if (KMAppearance.isDarkMode()) {
  196. button.layer?.backgroundColor = NSColor(red: 0.133, green: 0.478, blue: 1.000, alpha: 1).cgColor
  197. } else {
  198. button.layer?.backgroundColor = NSColor(red: 0.286, green: 0.510, blue: 0.902, alpha: 1).cgColor
  199. }
  200. } else {
  201. if (i == 0) {
  202. button.image = NSImage(named: KMImageNameUXIconSidetabbarThumbnailNor)
  203. } else if (i == 1) {
  204. button.image = NSImage(named: KMImageNameUXIconSidetabbarOutlineNor)
  205. } else if (i == 2) {
  206. button.image = NSImage(named: KMImageNameUXIconSidetabbarAnnotationNor)
  207. } else if (i == 3) {
  208. button.image = NSImage(named: KMImageNameUXIconSidetabbarSnapshotNor)
  209. } else if (i == 4) {
  210. button.image = NSImage(named: KMImageNameUXIconSidetabbarSearchNor)
  211. }
  212. button.wantsLayer = true
  213. if (KMAppearance.isDarkMode()) {
  214. button.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  215. } else {
  216. button.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  217. }
  218. }
  219. }
  220. }
  221. }
  222. /*
  223. - (NSImage *)image:(NSImage *)image withColor:(NSColor *)color {
  224. NSImage *newImage = [image copy];
  225. [newImage lockFocus];
  226. [color set];
  227. NSRectFillUsingOperation(NSMakeRect(0, 0, image.size.width, image.size.height), NSCompositingOperationSourceAtop);
  228. [newImage unlockFocus];
  229. return [newImage autorelease];
  230. }
  231. */
  232. }