KMWatermarkAdjectiveOutsideView.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // KMWatermarkAdjectiveOutsideView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/16.
  6. //
  7. import Cocoa
  8. class KMWatermarkAdjectiveOutsideView: KMWatermarkAdjectiveBaseView {
  9. var titleLabel: NSTextField!
  10. var rotateButton: NSButton!
  11. var ratateComboBox: NSView!
  12. var alphaButton: NSButton!
  13. var alphaComboBox: NSComboBox!
  14. var pageAboveComboBox: NSComboBox!
  15. var rComboBox: NSComboBox!
  16. var pageTargetScale: NSButton!
  17. var pageTargetScaleComboBox: NSComboBox!
  18. override var isFlipped: Bool {
  19. return true
  20. }
  21. override init(frame frameRect: NSRect) {
  22. super.init(frame: frameRect)
  23. self.titleLabel = NSTextField(labelWithString: "")
  24. self.rotateButton = NSButton()
  25. self.ratateComboBox = NSView()
  26. self.alphaButton = NSButton()
  27. self.alphaComboBox = NSComboBox()
  28. self.pageAboveComboBox = NSComboBox()
  29. self.pageTargetScale = NSButton(checkboxWithTitle: NSLocalizedString("Scale relative to target page", comment: ""), target: self, action: #selector(pageTargetScaleAction))
  30. self.pageTargetScaleComboBox = NSComboBox()
  31. self.addSubview(self.titleLabel)
  32. self.addSubview(self.rotateButton)
  33. self.addSubview(self.ratateComboBox)
  34. self.addSubview(self.alphaButton)
  35. self.addSubview(self.alphaComboBox)
  36. self.addSubview(self.pageAboveComboBox)
  37. self.rComboBox = NSComboBox()
  38. self.addSubview(self.rComboBox)
  39. self.addSubview(self.pageTargetScale)
  40. self.addSubview(self.pageTargetScaleComboBox)
  41. //
  42. titleLabel.stringValue = NSLocalizedString("Appearance", comment: "")
  43. rotateButton.isBordered = false
  44. rotateButton.title = NSLocalizedString("", comment: "")
  45. rComboBox.isEditable = false
  46. rComboBox.delegate = self
  47. rComboBox.removeAllItems()
  48. for i in 0 ... 360 {
  49. rComboBox.addItem(withObjectValue: i)
  50. }
  51. rComboBox.selectItem(withObjectValue: 0)
  52. alphaButton.isBordered = false
  53. alphaButton.title = NSLocalizedString("", comment: "")
  54. alphaComboBox.isEditable = false
  55. alphaComboBox.delegate = self
  56. alphaComboBox.removeAllItems()
  57. for i in 0 ... 100 {
  58. alphaComboBox.addItem(withObjectValue: "\(i)%")
  59. }
  60. alphaComboBox.selectItem(withObjectValue: "100%")
  61. pageAboveComboBox.isEditable = false
  62. pageAboveComboBox.delegate = self
  63. pageAboveComboBox.removeAllItems()
  64. pageAboveComboBox.addItems(withObjectValues: [NSLocalizedString("At the top of the page", comment: ""),NSLocalizedString("At the top of the page", comment: "")])
  65. pageAboveComboBox.selectItem(at: 0)
  66. self.pageTargetScaleComboBox.delegate = self
  67. self.pageTargetScaleComboBox.removeAllItems()
  68. for i in 0 ... 100 {
  69. pageTargetScaleComboBox.addItem(withObjectValue: "\(i)%")
  70. }
  71. self.pageTargetScaleComboBox.selectItem(withObjectValue: "100%")
  72. self.pageTargetScale.state = .on
  73. }
  74. required init?(coder: NSCoder) {
  75. super.init(coder: coder)
  76. }
  77. override func layout() {
  78. super.layout()
  79. let width: CGFloat = NSWidth(self.bounds)
  80. let heigth: CGFloat = NSHeight(self.bounds)
  81. let leftMargin: CGFloat = 16
  82. self.titleLabel.frame = NSMakeRect(leftMargin, 0, width-2*leftMargin, 16)
  83. let rotateY = self.titleLabel.frame.maxY+10
  84. self.rotateButton.frame = NSMakeRect(leftMargin, rotateY, 22, 22)
  85. self.rComboBox.frame = NSMakeRect(self.rotateButton.frame.maxX+10, rotateY, 80, 22)
  86. self.alphaButton.frame = NSMakeRect(self.rComboBox.frame.maxX+10, rotateY, 22, 22)
  87. self.alphaComboBox.frame = NSMakeRect(self.alphaButton.frame.maxX+10, rotateY, 80, 22)
  88. self.pageAboveComboBox.frame = NSMakeRect(leftMargin, self.rotateButton.frame.maxY+10, width-1*leftMargin, 22)
  89. self.pageTargetScale.frame = NSMakeRect(leftMargin, self.pageAboveComboBox.frame.maxY+10, width-2*leftMargin, 16)
  90. self.pageTargetScaleComboBox.frame = NSMakeRect(leftMargin, self.pageTargetScale.frame.maxY+10, 100, 22)
  91. }
  92. override func setModel(model: KMWatermarkModel) {
  93. super.setModel(model: model)
  94. rComboBox.stringValue = "\(abs(Int(-model.rotation)))"
  95. alphaComboBox.stringValue = "\(Int(model.opacity*100))%"
  96. if (model.isFront) {
  97. pageAboveComboBox.selectItem(at: 0)
  98. } else {
  99. pageAboveComboBox.selectItem(at: 1)
  100. }
  101. }
  102. @objc func pageTargetScaleAction(sender: NSButton) {
  103. self.pageTargetScaleComboBox.isEnabled = sender.state == .on
  104. if (sender.state == .on) {
  105. guard let callback = itemClick else {
  106. return
  107. }
  108. callback(4, pageTargetScaleComboBox.indexOfSelectedItem)
  109. } else {
  110. guard let callback = itemClick else {
  111. return
  112. }
  113. callback(4, 50)
  114. }
  115. }
  116. }
  117. extension KMWatermarkAdjectiveOutsideView: NSComboBoxDelegate {
  118. func comboBoxSelectionDidChange(_ notification: Notification) {
  119. if (rComboBox.isEqual(to: notification.object)) { /// 旋转
  120. if (rComboBox.indexOfSelectedItem < 0) {
  121. return
  122. }
  123. guard let callback = itemClick else {
  124. return
  125. }
  126. callback(1, rComboBox.indexOfSelectedItem)
  127. } else if (alphaComboBox.isEqual(to: notification.object)) { /// 透明度
  128. if (alphaComboBox.indexOfSelectedItem < 0) {
  129. return
  130. }
  131. guard let callback = itemClick else {
  132. return
  133. }
  134. callback(2, alphaComboBox.indexOfSelectedItem)
  135. } else if (pageAboveComboBox.isEqual(to: notification.object)) {
  136. if (pageAboveComboBox.indexOfSelectedItem < 0) {
  137. return
  138. }
  139. guard let callback = itemClick else {
  140. return
  141. }
  142. callback(3, pageAboveComboBox.indexOfSelectedItem)
  143. } else if (pageTargetScaleComboBox.isEqual(to: notification.object)) {
  144. if (pageTargetScaleComboBox.indexOfSelectedItem < 0) {
  145. return
  146. }
  147. guard let callback = itemClick else {
  148. return
  149. }
  150. callback(4, pageTargetScaleComboBox.indexOfSelectedItem)
  151. }
  152. }
  153. }