KMHeaderFooterContentInfoView.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // KMHeaderFooterContentInfoView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/28.
  6. //
  7. import Cocoa
  8. typealias KMHeaderFooterContentInfoView_selectAreaViewItemClick = (_ index: Int) -> ()
  9. private class KMHeaderFooterContentInfoView_selectAreaView: NSView {
  10. private var hLine = NSView()
  11. private var vLine = NSView()
  12. private var vLine2 = NSView()
  13. private var buttons: Array<NSButton> = []
  14. var itemClick: KMHeaderFooterContentInfoView_selectAreaViewItemClick!
  15. override init(frame frameRect: NSRect) {
  16. super.init(frame: frameRect)
  17. self.initSubViews()
  18. }
  19. required init?(coder: NSCoder) {
  20. super.init(coder: coder)
  21. self.initSubViews()
  22. }
  23. override var isFlipped: Bool {
  24. return true
  25. }
  26. private func initSubViews() {
  27. self.addSubview(self.hLine)
  28. self.addSubview(self.vLine)
  29. self.addSubview(self.vLine2)
  30. self.wantsLayer = true
  31. self.layer?.borderWidth = 1
  32. self.layer?.borderColor = NSColor.black.cgColor
  33. self.layer?.cornerRadius = 2
  34. for line in [self.hLine, self.vLine, self.vLine2] {
  35. line.wantsLayer = true
  36. line.layer?.backgroundColor = NSColor.black.cgColor
  37. }
  38. self.buttons.removeAll()
  39. for i in 0 ... 1 {
  40. for j in 0 ... 2 {
  41. let button = NSButton()
  42. button.isBordered = false
  43. button.title = ""
  44. self.addSubview(button)
  45. self.buttons.append(button)
  46. button.tag = i * 3 + j + 1
  47. button.target = self
  48. button.action = #selector(buttonAction)
  49. }
  50. }
  51. }
  52. override func layout() {
  53. super.layout()
  54. let width: CGFloat = NSWidth(self.bounds)
  55. let height: CGFloat = NSHeight(self.bounds)
  56. let itemSize: NSSize = NSMakeSize(width/3, height*0.5)
  57. self.hLine.frame = NSMakeRect(0, itemSize.height, width, 1)
  58. self.vLine.frame = NSMakeRect(itemSize.width, 0, 1, height)
  59. self.vLine2.frame = NSMakeRect(itemSize.width*2, 0, 1, height)
  60. for i in 0 ... 1 {
  61. for j in 0 ... 2 {
  62. let button = self.buttons[i*3+j]
  63. button.frame = NSMakeRect(CGFloat(j)*itemSize.width, CGFloat(i)*itemSize.height, itemSize.width, itemSize.height)
  64. }
  65. }
  66. }
  67. func selectIndex(index: Int) {
  68. for button in self.buttons {
  69. button.wantsLayer = true
  70. if (button.tag == index) {
  71. button.layer?.backgroundColor = NSColor.black.cgColor
  72. } else {
  73. button.layer?.backgroundColor = NSColor.clear.cgColor
  74. }
  75. }
  76. }
  77. @objc private func buttonAction(sender: NSButton) {
  78. guard let callback = self.itemClick else {
  79. return
  80. }
  81. self.selectIndex(index: sender.tag)
  82. callback(sender.tag)
  83. }
  84. }
  85. typealias KMHeaderFooterContentInfoViewAddAction = (_ index: Int, _ sender: NSButton) -> ()
  86. class KMHeaderFooterContentInfoView: KMHeaderFooterAdjectiveInfoBaseView {
  87. var addButton = NSButton()
  88. private var selectAreaView = KMHeaderFooterContentInfoView_selectAreaView()
  89. var textViewBox = NSView()
  90. var scrollView = NSScrollView()
  91. var textView = NSTextView()
  92. var addAction: KMHeaderFooterContentInfoViewAddAction!
  93. private var selectedIndex: Int = 1
  94. var indexForSelectedItem: Int {
  95. get {
  96. return self.selectedIndex
  97. }
  98. }
  99. override func initSubviews() {
  100. super.initSubviews()
  101. self.addSubview(self.addButton)
  102. self.addSubview(self.selectAreaView)
  103. self.addSubview(self.textViewBox)
  104. self.addSubview(self.scrollView)
  105. self.scrollView.documentView = self.textView
  106. self.titleLabel.stringValue = NSLocalizedString("Layout & Content", comment: "")
  107. self.addButton.isBordered = false
  108. self.addButton.title = "+"
  109. self.addButton.target = self
  110. self.addButton.action = #selector(addButtonAction)
  111. self.textViewBox.wantsLayer = true
  112. self.textViewBox.layer?.borderWidth = 1
  113. self.textViewBox.layer?.borderColor = NSColor.black.cgColor
  114. self.textViewBox.layer?.cornerRadius = 4
  115. self.scrollView.borderType = .noBorder
  116. self.scrollView.wantsLayer = true
  117. self.scrollView.layer?.borderWidth = 1
  118. self.scrollView.layer?.borderColor = NSColor.black.cgColor
  119. self.scrollView.layer?.cornerRadius = 2
  120. self.scrollView.layer?.masksToBounds = true
  121. self.textView.textContainerInset = NSMakeSize(6, 8)
  122. self.textView.isRichText = false
  123. self.textView.drawsBackground = false
  124. self.scrollView.drawsBackground = false
  125. self.textView.delegate = self
  126. self.selectAreaView.itemClick = {
  127. (index: Int) in
  128. self.selectedIndex = index
  129. if (index == 1) {
  130. self.textView.string = self.model.topLeftString
  131. } else if (index == 2) {
  132. self.textView.string = self.model.topCenterString
  133. } else if (index == 3) {
  134. self.textView.string = self.model.topRightString
  135. } else if (index == 4) {
  136. self.textView.string = self.model.bottomLeftString
  137. } else if (index == 5) {
  138. self.textView.string = self.model.bottomCenterString
  139. } else if (index == 6) {
  140. self.textView.string = self.model.bottomRightString
  141. }
  142. }
  143. }
  144. override func layout() {
  145. super.layout()
  146. let leftMargin = self.contentInset.left
  147. let width: CGFloat = NSWidth(self.bounds)
  148. let contentWidth: CGFloat = width-leftMargin-self.contentInset.right
  149. let buttonSize: CGFloat = 22
  150. let buttonX: CGFloat = width-self.contentInset.right-buttonSize
  151. self.addButton.frame = NSMakeRect(buttonX, 0, buttonSize, buttonSize)
  152. self.selectAreaView.frame = NSMakeRect(leftMargin, self.titleLabel.frame.maxY+10, contentWidth, 40)
  153. self.textViewBox.frame = NSMakeRect(leftMargin, self.selectAreaView.frame.maxY+10, contentWidth, 80)
  154. self.scrollView.frame = NSMakeRect(leftMargin+1, self.selectAreaView.frame.maxY+11, contentWidth-2, 78)
  155. self.textView.frame = NSMakeRect(1, 1, contentWidth-2, 78)
  156. }
  157. override var model: KMHeaderFooterAdjectiveModel {
  158. get {
  159. return super.model
  160. }
  161. set {
  162. super.model = newValue
  163. self.selectAreaView.selectIndex(index: 1)
  164. self.selectedIndex = 1
  165. self.textView.string = model.topLeftString
  166. }
  167. }
  168. @objc private func addButtonAction() {
  169. guard let callback = self.addAction else {
  170. return
  171. }
  172. callback(self.selectedIndex, self.addButton)
  173. }
  174. }
  175. extension KMHeaderFooterContentInfoView: NSTextViewDelegate {
  176. func textDidChange(_ notification: Notification) {
  177. guard let callback = self.itemClick else {
  178. return
  179. }
  180. callback(self.selectedIndex, self.textView.string)
  181. }
  182. }