KMRedactTopToolBar.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // KMRedactTopToolBar.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/16.
  6. //
  7. import Cocoa
  8. typealias KMRedactTopToolBarItemViewItemClick = (_ index: Int) -> ()
  9. private class KMRedactTopToolBarItemView: NSView {
  10. var imageView = NSImageView()
  11. var titleLabel = NSTextField(labelWithString: "")
  12. var bottomLine = NSView()
  13. var button = NSButton()
  14. var itemID: Int = 0
  15. private var myIsSelected: Bool = false
  16. var isSelected: Bool {
  17. get {
  18. return myIsSelected
  19. }
  20. set {
  21. myIsSelected = newValue
  22. if (myIsSelected) {
  23. self.bottomLine.isHidden = false
  24. } else {
  25. self.bottomLine.isHidden = true
  26. }
  27. }
  28. }
  29. var itemClick: KMRedactTopToolBarItemViewItemClick!
  30. override init(frame frameRect: NSRect) {
  31. super.init(frame: frameRect)
  32. initSubViews()
  33. }
  34. required init?(coder: NSCoder) {
  35. super.init(coder: coder)
  36. initSubViews()
  37. }
  38. func initSubViews() {
  39. self.addSubview(self.imageView)
  40. self.addSubview(self.titleLabel)
  41. self.addSubview(self.bottomLine)
  42. self.bottomLine.wantsLayer = true
  43. self.bottomLine.layer?.backgroundColor = NSColor.black.cgColor
  44. self.addSubview(self.button)
  45. self.button.isBordered = false
  46. self.button.title = ""
  47. self.button.target = self
  48. self.button.action = #selector(buttonAction)
  49. }
  50. override func layout() {
  51. super.layout()
  52. let width: CGFloat = NSWidth(self.bounds)
  53. let heigth: CGFloat = NSHeight(self.bounds)
  54. let imageSize: NSSize = NSSize(width: 14, height: 15)
  55. let imageY: CGFloat = (heigth-imageSize.height)*0.5
  56. self.imageView.frame = NSMakeRect(0, imageY, imageSize.width, imageSize.height)
  57. let titleH: CGFloat = 20
  58. let titleY: CGFloat = (heigth-titleH)*0.5
  59. let titleX: CGFloat = self.imageView.frame.maxX+8
  60. self.titleLabel.frame = NSMakeRect(titleX, titleY, width-titleX, titleH)
  61. self.bottomLine.frame = NSMakeRect(0, 0, width, 1)
  62. self.button.frame = self.bounds
  63. }
  64. @objc func buttonAction() {
  65. guard let callback = self.itemClick else {
  66. return
  67. }
  68. callback(self.itemID)
  69. }
  70. }
  71. typealias KMRedactTopToolBarItemClick = (_ index: Int) -> ()
  72. class KMRedactTopToolBar: NSView {
  73. private var textAndImageItemView = KMRedactTopToolBarItemView()
  74. private var pageItemView = KMRedactTopToolBarItemView()
  75. private var applyButton = NSButton()
  76. private var clearButton = NSButton()
  77. private var closeButton = NSButton()
  78. var itemClick: KMRedactTopToolBarItemClick!
  79. override init(frame frameRect: NSRect) {
  80. super.init(frame: frameRect)
  81. initSubViews()
  82. }
  83. required init?(coder: NSCoder) {
  84. super.init(coder: coder)
  85. initSubViews()
  86. }
  87. public func initSubViews() {
  88. self.addSubview(self.textAndImageItemView)
  89. self.addSubview(self.pageItemView)
  90. self.addSubview(self.applyButton)
  91. self.addSubview(self.clearButton)
  92. self.addSubview(self.closeButton)
  93. self.textAndImageItemView.titleLabel.stringValue = NSLocalizedString("Redact Text & Image", comment: "")
  94. self.textAndImageItemView.itemClick = { [weak self] index in
  95. self!.selectItem(0)
  96. guard let callback = self!.itemClick else {
  97. return
  98. }
  99. callback(4)
  100. }
  101. self.pageItemView.titleLabel.stringValue = NSLocalizedString("Redact Page", comment: "")
  102. self.pageItemView.itemClick = { [weak self] index in
  103. self!.selectItem(1)
  104. guard let callback = self!.itemClick else {
  105. return
  106. }
  107. callback(5)
  108. }
  109. self.applyButton.isBordered = false
  110. self.applyButton.title = NSLocalizedString("Redact", comment: "")
  111. self.applyButton.wantsLayer = true
  112. self.applyButton.layer?.backgroundColor = NSColor.black.cgColor
  113. self.applyButton.attributedTitle = NSAttributedString(string: self.applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  114. self.applyButton.layer?.cornerRadius = 4
  115. self.applyButton.target = self
  116. self.applyButton.action = #selector(applyButtonAction)
  117. self.clearButton.isBordered = false
  118. self.clearButton.title = NSLocalizedString("Erase", comment: "")
  119. self.clearButton.wantsLayer = true
  120. self.clearButton.layer?.backgroundColor = NSColor.black.cgColor
  121. self.clearButton.attributedTitle = NSAttributedString(string: self.clearButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
  122. self.clearButton.layer?.cornerRadius = 4
  123. self.clearButton.target = self
  124. self.clearButton.action = #selector(clearButtonAction)
  125. self.closeButton.isBordered = false
  126. self.closeButton.title = NSLocalizedString("Exit", comment: "")
  127. self.closeButton.wantsLayer = true
  128. self.closeButton.layer?.cornerRadius = 4
  129. self.closeButton.layer?.borderWidth = 1
  130. self.closeButton.layer?.borderColor = NSColor.black.cgColor
  131. self.closeButton.target = self
  132. self.closeButton.action = #selector(closeButtonAction)
  133. }
  134. override func layout() {
  135. super.layout()
  136. let width: CGFloat = NSWidth(self.bounds)
  137. let height: CGFloat = NSHeight(self.bounds)
  138. let itemX: CGFloat = (width-280)*0.5
  139. self.textAndImageItemView.frame = NSMakeRect(itemX, 0, 140, height)
  140. self.pageItemView.frame = NSMakeRect(self.textAndImageItemView.frame.maxX, 0, 140, height)
  141. let buttonH: CGFloat = 24
  142. let buttonY: CGFloat = (height-buttonH)*0.5
  143. let closeW: CGFloat = 44
  144. let closeX: CGFloat = width-12-closeW
  145. let closeY: CGFloat = (height-buttonH)*0.5
  146. self.closeButton.frame = NSMakeRect(closeX, closeY, closeW, buttonH)
  147. let clearW: CGFloat = 68
  148. let clearX: CGFloat = self.closeButton.frame.minX-8-clearW
  149. self.clearButton.frame = NSMakeRect(clearX, buttonY, clearW, buttonH)
  150. let applyW: CGFloat = 68
  151. let applyX: CGFloat = self.clearButton.frame.minX-8-applyW
  152. self.applyButton.frame = NSMakeRect(applyX, buttonY, applyW, buttonH)
  153. }
  154. @objc private func applyButtonAction() {
  155. guard let callback = self.itemClick else {
  156. return
  157. }
  158. callback(3)
  159. }
  160. @objc private func clearButtonAction() {
  161. guard let callback = self.itemClick else {
  162. return
  163. }
  164. callback(2)
  165. }
  166. @objc private func closeButtonAction() {
  167. guard let callback = self.itemClick else {
  168. return
  169. }
  170. callback(1)
  171. }
  172. public func selectItem(_ index: Int) {
  173. var count: Int = 0
  174. for item in [self.textAndImageItemView,
  175. self.pageItemView] {
  176. if (index == count) {
  177. item.isSelected = true
  178. } else {
  179. item.isSelected = false
  180. }
  181. count += 1
  182. }
  183. }
  184. }