KMWatermarkPositionView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // KMWatermarkPositionView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/18.
  6. //
  7. import Cocoa
  8. enum KMWatermarkPosition: Int {
  9. case leftTop = 0
  10. case top = 1
  11. case rightTop = 2
  12. case leftMiddle = 3
  13. case middle = 4
  14. case rightMiddle = 5
  15. case leftBottom = 6
  16. case bottom = 7
  17. case rightBottom = 8
  18. }
  19. typealias KMWatermarkPositionItemClick = (_ position: KMWatermarkPosition)->()
  20. class KMWatermarkPositionView: NSView {
  21. var hLine1 = NSView()
  22. var hLine2 = NSView()
  23. var vLine1 = NSView()
  24. var vLine2 = NSView()
  25. var buttons: Array<NSButton> = []
  26. var position: KMWatermarkPosition = .middle
  27. var itemClick: KMWatermarkPositionItemClick!
  28. override init(frame frameRect: NSRect) {
  29. super.init(frame: frameRect)
  30. initSubViews()
  31. }
  32. required init?(coder: NSCoder) {
  33. super.init(coder: coder)
  34. initSubViews()
  35. }
  36. func initSubViews() {
  37. wantsLayer = true
  38. layer?.borderWidth = 1
  39. layer?.borderColor = NSColor.black.cgColor
  40. layer?.cornerRadius = 2
  41. addSubview(hLine1)
  42. addSubview(hLine2)
  43. addSubview(vLine1)
  44. addSubview(vLine2)
  45. for line in [hLine1, hLine2, vLine1, vLine2] {
  46. line.wantsLayer = true
  47. line.layer?.backgroundColor = NSColor.black.cgColor
  48. }
  49. for button in buttons {
  50. button.removeFromSuperview()
  51. }
  52. buttons.removeAll()
  53. for i in 0 ... 8 {
  54. let button = NSButton()
  55. button.isBordered = false
  56. button.wantsLayer = true
  57. button.title = ""
  58. button.tag = i
  59. addSubview(button)
  60. buttons.append(button)
  61. button.target = self
  62. button.action = #selector(buttonAction)
  63. }
  64. buttonAction(sender: buttons[4])
  65. }
  66. override func layout() {
  67. super.layout()
  68. let width: CGFloat = NSWidth(self.bounds)
  69. let height: CGFloat = NSHeight(self.bounds)
  70. let itemSize: CGFloat = height / 3
  71. hLine1.frame = NSMakeRect(0, 2*itemSize, width, 1)
  72. hLine2.frame = NSMakeRect(0, itemSize, width, 1)
  73. vLine1.frame = NSMakeRect(itemSize, 0, 1, height)
  74. vLine2.frame = NSMakeRect(2*itemSize, 0, 1, height)
  75. var i: Int = 0
  76. for button in buttons {
  77. let x: Int = i % 3
  78. let y: Int = i / 3
  79. button.frame = NSMakeRect(CGFloat(x) * itemSize, CGFloat(y) * itemSize, itemSize, itemSize)
  80. i += 1
  81. }
  82. }
  83. @objc func buttonAction(sender: NSButton) {
  84. dealButtonSelectedState(selectButton: sender)
  85. if (sender.tag == 0) {
  86. self.position = .leftBottom
  87. } else if (sender.tag == 1) {
  88. self.position = .bottom
  89. } else if (sender.tag == 2) {
  90. self.position = .rightBottom
  91. } else if (sender.tag == 3) {
  92. self.position = .leftMiddle
  93. } else if (sender.tag == 4) {
  94. self.position = .middle
  95. } else if (sender.tag == 5) {
  96. self.position = .rightMiddle
  97. } else if (sender.tag == 6) {
  98. self.position = .leftTop
  99. } else if (sender.tag == 7) {
  100. self.position = .top
  101. } else if (sender.tag == 8) {
  102. self.position = .rightTop
  103. }
  104. guard let callback = itemClick else {
  105. return
  106. }
  107. callback(self.position)
  108. }
  109. func dealButtonSelectedState(selectButton: NSButton) {
  110. for button in buttons {
  111. if (button.isEqual(to: selectButton)) {
  112. button.layer?.backgroundColor = NSColor.black.cgColor
  113. } else {
  114. button.layer?.backgroundColor = NSColor.clear.cgColor
  115. }
  116. }
  117. }
  118. func selectPosition(positon: KMWatermarkPosition) {
  119. self.position = positon
  120. if (positon == .leftTop) {
  121. dealButtonSelectedState(selectButton: buttons[0])
  122. } else if (positon == .top) {
  123. dealButtonSelectedState(selectButton: buttons[1])
  124. } else if (positon == .rightTop) {
  125. dealButtonSelectedState(selectButton: buttons[2])
  126. } else if (positon == .leftMiddle) {
  127. dealButtonSelectedState(selectButton: buttons[3])
  128. } else if (positon == .middle) {
  129. dealButtonSelectedState(selectButton: buttons[4])
  130. } else if (positon == .rightMiddle) {
  131. dealButtonSelectedState(selectButton: buttons[5])
  132. } else if (positon == .leftBottom) {
  133. dealButtonSelectedState(selectButton: buttons[6])
  134. } else if (positon == .bottom) {
  135. dealButtonSelectedState(selectButton: buttons[7])
  136. } else if (positon == .rightBottom) {
  137. dealButtonSelectedState(selectButton: buttons[8])
  138. }
  139. }
  140. }