KMWatermarkAdjectiveTopBarView.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // KMWatermarkAdjectiveTopBarView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/15.
  6. //
  7. import Cocoa
  8. typealias KMWatermarkAdjectiveTopBarViewItemClick = (_ secton: Int,_ item: Int)->()
  9. typealias KMWatermarkAdjectiveTopBarViewApplyClick = ()->()
  10. typealias KMWatermarkAdjectiveTopBarViewCancelClick = ()->()
  11. class KMWatermarkAdjectiveTopBarView: NSView {
  12. private lazy var itemContainsView: NSView = {
  13. let itemContainsView = NSView()
  14. return itemContainsView
  15. }()
  16. private lazy var applyButton: NSButton = {
  17. let button = NSButton()
  18. button.isBordered = false
  19. button.title = NSLocalizedString("Apply", comment: "")
  20. button.wantsLayer = true
  21. button.layer?.cornerRadius = 4
  22. button.layer?.backgroundColor = NSColor.black.cgColor
  23. button.attributedTitle = NSAttributedString(string: button.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  24. button.target = self
  25. button.action = #selector(applyButonClick)
  26. return button
  27. }()
  28. private lazy var cancelButton: NSButton = {
  29. let button = NSButton()
  30. button.isBordered = false
  31. button.title = NSLocalizedString("Exit", comment: "")
  32. button.wantsLayer = true
  33. button.layer?.cornerRadius = 4
  34. button.layer?.borderWidth = 1
  35. button.layer?.borderColor = NSColor.black.cgColor
  36. button.target = self
  37. button.action = #selector(cancelButtonClick)
  38. return button
  39. }()
  40. var itemClick: KMWatermarkAdjectiveTopBarViewItemClick!
  41. var applyClick: KMWatermarkAdjectiveTopBarViewApplyClick!
  42. var cancelClick: KMWatermarkAdjectiveTopBarViewCancelClick!
  43. private var itemViews: Array<NSView> = []
  44. override init(frame frameRect: NSRect) {
  45. super.init(frame: frameRect)
  46. self.addSubview(self.itemContainsView)
  47. self.addSubview(self.applyButton)
  48. self.addSubview(self.cancelButton)
  49. }
  50. required init?(coder: NSCoder) {
  51. super.init(coder: coder)
  52. }
  53. override func layout() {
  54. super.layout()
  55. let width: CGFloat = NSWidth(self.bounds)
  56. let height: CGFloat = NSHeight(self.bounds)
  57. let itemContainsViewX: CGFloat = 120
  58. self.itemContainsView.frame = NSMakeRect(itemContainsViewX, 0, width-itemContainsViewX*2, height)
  59. let itemLineH: CGFloat = 10
  60. let itemH: CGFloat = 32
  61. let itemContainsH = NSHeight(self.itemContainsView.frame)
  62. let itemConainsW = 84
  63. let itemX: CGFloat = NSWidth(self.itemContainsView.frame)*0.5-100
  64. var preItemView: NSView!
  65. for itemView in self.itemViews {
  66. if (itemView.isKind(of: KMWatermarkAdjectiveTopBarItemLineView.self)) {
  67. itemView.frame = NSMakeRect(preItemView.frame.maxX+10, (itemContainsH-itemLineH)*0.5, 1, itemLineH)
  68. } else {
  69. if (itemView.isEqual(to: self.itemViews.first)) {
  70. itemView.frame = NSMakeRect(itemX, (itemContainsH-itemH)*0.5, 84, itemH)
  71. } else {
  72. if (preItemView.isKind(of: KMWatermarkAdjectiveTopBarItemLineView.self)) {
  73. itemView.frame = NSMakeRect(preItemView.frame.maxX+10, (itemContainsH-itemH)*0.5, 60, itemH)
  74. } else {
  75. itemView.frame = NSMakeRect(preItemView.frame.maxX+10, (itemContainsH-itemH)*0.5, 60, itemH)
  76. }
  77. }
  78. }
  79. preItemView = itemView
  80. }
  81. let buttonSize: CGSize = CGSize(width: 44, height: 24)
  82. let buttonY = (height-buttonSize.height) * 0.5
  83. let cancelButtonX = width-12-buttonSize.width
  84. self.cancelButton.frame = NSMakeRect(cancelButtonX, buttonY, buttonSize.width, buttonSize.height)
  85. let applyButtonX = self.cancelButton.frame.minX - 10 - buttonSize.width
  86. self.applyButton.frame = NSMakeRect(applyButtonX, buttonY, buttonSize.width, buttonSize.height)
  87. }
  88. func initItemData(itemArrays: Array<Array<KMWatermarkAdjectiveTopBarItemModel>>) {
  89. for subview in self.itemContainsView.subviews {
  90. subview.removeFromSuperview()
  91. }
  92. self.itemViews.removeAll()
  93. var count: Int = 0
  94. var index: Int = 0
  95. for items in itemArrays {
  96. for model in items {
  97. let view = KMWatermarkAdjectiveTopBarItemView()
  98. self.itemContainsView.addSubview(view)
  99. self.itemViews.append(view)
  100. view.button.title = model.itemTitle
  101. view.button.tag = index
  102. view.button.target = self
  103. view.button.action = #selector(itemButtonClick)
  104. index += 1
  105. }
  106. if (count != itemArrays.count-1) {
  107. let line = KMWatermarkAdjectiveTopBarItemLineView()
  108. self.itemContainsView.addSubview(line)
  109. self.itemViews.append(line)
  110. line.wantsLayer = true
  111. line.layer?.backgroundColor = NSColor.lightGray.cgColor
  112. }
  113. count += 1
  114. }
  115. self.layoutSubtreeIfNeeded()
  116. }
  117. @objc func applyButonClick() {
  118. guard let callback = applyClick else {
  119. return
  120. }
  121. callback()
  122. }
  123. @objc func cancelButtonClick() {
  124. guard let callback = cancelClick else {
  125. return
  126. }
  127. callback()
  128. }
  129. @objc func itemButtonClick(sender: NSButton) {
  130. guard let callback = itemClick else {
  131. return
  132. }
  133. let index = sender.tag
  134. callback(0, index)
  135. }
  136. func selectTopItem(index: Int) {
  137. var count: Int = 0
  138. for item in self.itemViews {
  139. if (!(item.isKind(of: KMWatermarkAdjectiveTopBarItemView.self))) {
  140. continue
  141. }
  142. let itemView: KMWatermarkAdjectiveTopBarItemView = item as! KMWatermarkAdjectiveTopBarItemView
  143. if (index == count) {
  144. itemView.isSelected(selected: true)
  145. } else {
  146. itemView.isSelected(selected: false)
  147. }
  148. count += 1
  149. }
  150. }
  151. func unselectTopItem(index: Int) {
  152. var count: Int = 0
  153. for item in self.itemViews {
  154. if (!(item.isKind(of: KMWatermarkAdjectiveTopBarItemView.self))) {
  155. continue
  156. }
  157. let itemView: KMWatermarkAdjectiveTopBarItemView = item as! KMWatermarkAdjectiveTopBarItemView
  158. if (index == count) {
  159. itemView.isSelected(selected: false)
  160. } else {
  161. // itemView.isSelected(selected: false)
  162. }
  163. count += 1
  164. }
  165. }
  166. func isCanApply(can: Bool) {
  167. self.applyButton.isEnabled = can
  168. if (can) {
  169. self.applyButton.layer?.backgroundColor = NSColor.black.cgColor
  170. self.applyButton.attributedTitle = NSAttributedString(string: self.applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  171. } else {
  172. self.applyButton.layer?.backgroundColor = NSColor.lightGray.cgColor
  173. self.applyButton.attributedTitle = NSAttributedString(string: self.applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  174. }
  175. }
  176. }