KMWatermarkAligementView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // KMWatermarkAligementView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/17.
  6. //
  7. import Cocoa
  8. typealias KMWatermarkAligementViewItemClick = (_ index: Int)->()
  9. class KMWatermarkAligementView: NSView {
  10. var leftButton = NSButton()
  11. var centerButton = NSButton()
  12. var rightButton = NSButton()
  13. var vLine = NSView()
  14. var vLine2 = NSView()
  15. var itemClick: KMWatermarkAligementViewItemClick!
  16. private var myIsEnabled: Bool = true
  17. var isEnabled: Bool {
  18. get {
  19. return myIsEnabled
  20. }
  21. set {
  22. myIsEnabled = newValue
  23. for button in [leftButton, centerButton, rightButton] {
  24. button.isEnabled = newValue
  25. }
  26. }
  27. }
  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(leftButton)
  42. addSubview(centerButton)
  43. addSubview(rightButton)
  44. addSubview(vLine)
  45. addSubview(vLine2)
  46. for button in [leftButton, centerButton, rightButton] {
  47. button.isBordered = false
  48. button.wantsLayer = true
  49. button.target = self
  50. button.action = #selector(buttonAction)
  51. }
  52. leftButton.title = ""
  53. centerButton.title = ""
  54. rightButton.title = ""
  55. for line in [vLine, vLine2] {
  56. line.wantsLayer = true
  57. line.layer?.backgroundColor = NSColor.black.cgColor
  58. }
  59. buttonAction(sender: leftButton)
  60. }
  61. override func layout() {
  62. super.layout()
  63. let width: CGFloat = NSWidth(self.bounds)
  64. let height: CGFloat = NSHeight(self.bounds)
  65. let itemWidth: CGFloat = width / 3
  66. leftButton.frame = NSMakeRect(0, 0, itemWidth, height)
  67. centerButton.frame = NSMakeRect(itemWidth, 0, itemWidth, height)
  68. rightButton.frame = NSMakeRect(2*itemWidth, 0, itemWidth, height)
  69. vLine.frame = NSMakeRect(itemWidth, 0, 1, height)
  70. vLine2.frame = NSMakeRect(2*itemWidth, 0, 1, height)
  71. }
  72. @objc func buttonAction(sender: NSButton) {
  73. dealButtonSelectedState(selectButton: sender)
  74. guard let callback = itemClick else {
  75. return
  76. }
  77. var index: Int = 0
  78. if (sender.isEqual(to: leftButton)) {
  79. index = 0
  80. } else if (sender.isEqual(to: centerButton)) {
  81. index = 1
  82. } else if (sender.isEqual(to: rightButton)) {
  83. index = 2
  84. }
  85. callback(index)
  86. }
  87. func dealButtonSelectedState(selectButton: NSButton) {
  88. for button in [leftButton, centerButton, rightButton] {
  89. if (button.isEqual(to: selectButton)) {
  90. button.wantsLayer = true
  91. button.layer?.cornerRadius = 4
  92. button.layer?.borderWidth = 1
  93. button.layer?.borderColor = NSColor.km_init(hex: "#CED0D4").cgColor
  94. button.layer?.backgroundColor = NSColor.km_init(hex: "#CED0D499").cgColor
  95. } else {
  96. button.wantsLayer = true
  97. button.layer?.backgroundColor = NSColor.clear.cgColor
  98. button.layer?.borderColor = NSColor.clear.cgColor
  99. }
  100. }
  101. if (selectButton.isEqual(to: self.leftButton)) {
  102. self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")
  103. self.leftButton.wantsLayer = true
  104. self.leftButton.layer?.backgroundColor = NSColor.km_init(hex: "#CED0D499").cgColor
  105. self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenter")
  106. self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRight")
  107. } else if (selectButton.isEqual(to: self.centerButton)) {
  108. self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")
  109. self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeft")
  110. self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRight")
  111. } else if (selectButton.isEqual(to: self.rightButton)) {
  112. self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRightSelect")
  113. self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenter")
  114. self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeft")
  115. }
  116. }
  117. func selectIndex(index: Int) {
  118. if (index == 0) {
  119. dealButtonSelectedState(selectButton: leftButton)
  120. } else if (index == 1) {
  121. dealButtonSelectedState(selectButton: centerButton)
  122. } else if (index == 2) {
  123. dealButtonSelectedState(selectButton: rightButton)
  124. }
  125. }
  126. }