KMEditPDFPopToolBarWindow.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // KMEditPDFPopToolBarWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/25.
  6. //
  7. import Cocoa
  8. @objcMembers class KMEditPDFPopToolBarWindow: NSWindow {
  9. static let shared = KMEditPDFPopToolBarWindow()
  10. var style: KMEditPDFToolbarStyle = .text
  11. var isMultiple: Bool = false
  12. let model = KMEditPDFModel()
  13. var itemClick: ((KMEditPDFToolbarItemKey, Any?)->Void)?
  14. convenience init() {
  15. let rect = NSRect(x: 0, y: 0, width: 400, height: 44)
  16. let styleMask: NSWindow.StyleMask = [.fullSizeContentView]
  17. self.init(contentRect: rect, styleMask: styleMask, backing: .buffered, defer: false)
  18. }
  19. override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
  20. super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
  21. let contentViewC = KMEditPDFPopToolBarController()
  22. self.contentViewController = contentViewC
  23. self.titlebarAppearsTransparent = true
  24. self.titleVisibility = .hidden
  25. // self.level = .popUpMenu
  26. // self.isMovableByWindowBackground = false
  27. self.isMovable = false
  28. self.contentView?.wantsLayer = true
  29. self.contentView?.layer?.cornerRadius = 4
  30. self.contentView?.layer?.masksToBounds = true
  31. self.backgroundColor = .clear
  32. contentViewC.itemClick = { [weak self] itemKey, obj in
  33. self?.itemClick?(itemKey, obj)
  34. }
  35. }
  36. func show(relativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge) {
  37. let contentViewC = (self.contentViewController as? KMEditPDFPopToolBarController)
  38. contentViewC?.fontColor = self.model.fontColors.last ?? .black
  39. contentViewC?.areaCount = self.model.editingAreas?.count ?? 0
  40. var width: CGFloat = 392
  41. if self.style.contains(.text) {
  42. if self.style.contains(.image) { // text + image
  43. contentViewC?.itemKeys = [.alignmentLeft, .alignmentCenterX, .alignmentRight, .alignmentjustifiedX, .alignmentTop, .alignmentCenterY, .alignmentBottom, .alignmentjustifiedY]
  44. var datas: [KMEditPDFToolbarModel] = []
  45. for key in contentViewC?.itemKeys ?? [] {
  46. let model = KMEditPDFToolbarModel()
  47. model.itemKey = key
  48. if key == .alignmentjustifiedX || key == .alignmentjustifiedY {
  49. let areas = self.model.editingAreas ?? []
  50. model.isEnabled = areas.count > 2
  51. }
  52. datas.append(model)
  53. }
  54. contentViewC?.datas = datas
  55. width = 320-36
  56. } else { // text
  57. if self.isMultiple {
  58. width = 478
  59. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment, .separator, .alignmentLeft, .alignmentTop]
  60. var datas: [KMEditPDFToolbarModel] = []
  61. for key in contentViewC?.itemKeys ?? [] {
  62. let model = KMEditPDFToolbarModel()
  63. model.itemKey = key
  64. if key == .color {
  65. model.isEnabled = self.model.editAreasFontColorIsEqual()
  66. } else if key == .fontStyle {
  67. if self.model.editAreasFontNameIsEqual() {
  68. model.isEnabled = true
  69. model.fontName = self.model.fontNames.first
  70. } else {
  71. model.isEnabled = false
  72. model.fontName = nil
  73. }
  74. } else if key == .fontAdd {
  75. model.isEnabled = self._fontSizeItemIsEnabled()
  76. } else if key == .fontReduce {
  77. model.isEnabled = self._fontSizeItemIsEnabled()
  78. } else if key == .fontBold {
  79. // model.isEnabled = self.model.editAreasFontBoldIsEqual()
  80. if self.model.editAreasFontBoldIsEqual() {
  81. model.isSelected = self.model.fontBolds.first ?? false
  82. }
  83. } else if key == .fontItalic {
  84. // model.isEnabled = self.model.editAreasFontItalicIsEqual()
  85. } else if key == .textAlignment {
  86. // model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  87. }
  88. datas.append(model)
  89. }
  90. contentViewC?.datas = datas
  91. } else {
  92. width = 392
  93. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment]
  94. var datas: [KMEditPDFToolbarModel] = []
  95. for key in contentViewC?.itemKeys ?? [] {
  96. let model = KMEditPDFToolbarModel()
  97. model.itemKey = key
  98. if key == .color {
  99. model.isEnabled = self.model.editAreasFontColorIsEqual()
  100. } else if key == .fontStyle {
  101. if self.model.editAreasFontNameIsEqual() {
  102. model.isEnabled = true
  103. model.fontName = self.model.fontNames.first
  104. } else {
  105. model.isEnabled = false
  106. model.fontName = nil
  107. }
  108. } else if key == .fontAdd {
  109. model.isEnabled = self._fontSizeItemIsEnabled()
  110. } else if key == .fontReduce {
  111. model.isEnabled = self._fontSizeItemIsEnabled()
  112. } else if key == .fontBold {
  113. // model.isEnabled = self.model.editAreasFontBoldIsEqual()
  114. } else if key == .fontItalic {
  115. // model.isEnabled = self.model.editAreasFontItalicIsEqual()
  116. } else if key == .textAlignment {
  117. // model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  118. }
  119. datas.append(model)
  120. }
  121. contentViewC?.datas = datas
  122. }
  123. }
  124. } else {
  125. if self.style.contains(.image) { // image
  126. if self.isMultiple {
  127. width = 396-20
  128. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop]
  129. var datas: [KMEditPDFToolbarModel] = []
  130. for key in contentViewC?.itemKeys ?? [] {
  131. let model = KMEditPDFToolbarModel()
  132. model.itemKey = key
  133. if key == .crop {
  134. model.isEnabled = !self.isMultiple
  135. } else if key == .replace {
  136. model.isEnabled = !self.isMultiple
  137. }
  138. datas.append(model)
  139. }
  140. contentViewC?.datas = datas
  141. } else {
  142. width = 304-16
  143. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export]
  144. var datas: [KMEditPDFToolbarModel] = []
  145. for key in contentViewC?.itemKeys ?? [] {
  146. let model = KMEditPDFToolbarModel()
  147. model.itemKey = key
  148. if key == .crop {
  149. model.isEnabled = !self.isMultiple
  150. }
  151. datas.append(model)
  152. }
  153. contentViewC?.datas = datas
  154. }
  155. } else { // none
  156. }
  157. }
  158. let winFrame = positioningView.window?.frame ?? .zero
  159. var position = positioningView.convert(positioningRect.origin, to: nil)
  160. position.x += winFrame.origin.x
  161. position.y += winFrame.origin.y
  162. position.y += positioningRect.size.height
  163. position.y += 26
  164. position.x += (positioningRect.size.width*0.5-width*0.5)
  165. var x = max(0, position.x)
  166. let offsetX = x + width - NSMaxX(winFrame)
  167. if offsetX > 0 { // 超出右编辑
  168. x -= offsetX
  169. }
  170. var y = max(0, position.y)
  171. let screenFrame = NSScreen.main?.frame ?? .zero
  172. if y + 44 + 40 >= screenFrame.size.height {
  173. y = screenFrame.size.height - 44 - 40
  174. }
  175. let frame = NSMakeRect(x, y, width, 44)
  176. self.setFrame(frame, display: true)
  177. self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44)
  178. self.orderFront(nil)
  179. // self.makeKeyAndOrderFront(nil)
  180. }
  181. override var isMainWindow: Bool {
  182. return true
  183. }
  184. override var isKeyWindow: Bool {
  185. return true
  186. }
  187. // MARK: - Private Methods
  188. private func _fontSizeItemIsEnabled() -> Bool {
  189. if self.model.editAreasFontSizeIsEqual() {
  190. if let fontSize = self.model.fontSizes.first, fontSize == -1 {
  191. return false
  192. } else {
  193. return true
  194. }
  195. } else {
  196. return false
  197. }
  198. }
  199. }
  200. extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol {
  201. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  202. self.appearance = .init(named: appearance)
  203. self.contentViewController?.interfaceThemeDidChanged(appearance)
  204. }
  205. }