KMEditPDFPopToolBarWindow.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if let data = self._fetchTextAlign() {
  88. model.textAlign = data
  89. }
  90. }
  91. datas.append(model)
  92. }
  93. contentViewC?.datas = datas
  94. } else {
  95. width = 392
  96. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment]
  97. var datas: [KMEditPDFToolbarModel] = []
  98. for key in contentViewC?.itemKeys ?? [] {
  99. let model = KMEditPDFToolbarModel()
  100. model.itemKey = key
  101. if key == .color {
  102. model.isEnabled = self.model.editAreasFontColorIsEqual()
  103. } else if key == .fontStyle {
  104. if self.model.editAreasFontNameIsEqual() {
  105. model.isEnabled = true
  106. model.fontName = self.model.fontNames.first
  107. } else {
  108. model.isEnabled = false
  109. model.fontName = "Helvetica"
  110. }
  111. } else if key == .fontAdd {
  112. model.isEnabled = self._fontSizeItemIsEnabled()
  113. } else if key == .fontReduce {
  114. model.isEnabled = self._fontSizeItemIsEnabled()
  115. } else if key == .fontBold {
  116. // model.isEnabled = self.model.editAreasFontBoldIsEqual()
  117. } else if key == .fontItalic {
  118. // model.isEnabled = self.model.editAreasFontItalicIsEqual()
  119. } else if key == .textAlignment {
  120. // model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  121. if let data = self._fetchTextAlign() {
  122. model.textAlign = data
  123. }
  124. }
  125. datas.append(model)
  126. }
  127. contentViewC?.datas = datas
  128. }
  129. }
  130. } else {
  131. if self.style.contains(.image) { // image
  132. if self.isMultiple {
  133. width = 396-20
  134. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop]
  135. var datas: [KMEditPDFToolbarModel] = []
  136. for key in contentViewC?.itemKeys ?? [] {
  137. let model = KMEditPDFToolbarModel()
  138. model.itemKey = key
  139. if key == .crop {
  140. model.isEnabled = !self.isMultiple
  141. } else if key == .replace {
  142. model.isEnabled = !self.isMultiple
  143. }
  144. datas.append(model)
  145. }
  146. contentViewC?.datas = datas
  147. } else {
  148. width = 304-16
  149. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export]
  150. var datas: [KMEditPDFToolbarModel] = []
  151. for key in contentViewC?.itemKeys ?? [] {
  152. let model = KMEditPDFToolbarModel()
  153. model.itemKey = key
  154. if key == .crop {
  155. model.isEnabled = !self.isMultiple
  156. }
  157. datas.append(model)
  158. }
  159. contentViewC?.datas = datas
  160. }
  161. } else { // none
  162. }
  163. }
  164. let winFrame = positioningView.window?.frame ?? .zero
  165. var position = positioningView.convert(positioningRect.origin, to: nil)
  166. position.x += winFrame.origin.x
  167. position.y += winFrame.origin.y
  168. position.y += positioningRect.size.height
  169. position.y += 26
  170. position.x += (positioningRect.size.width*0.5-width*0.5)
  171. // var x = max(0, position.x)
  172. var x = max(winFrame.origin.x, position.x)
  173. let offsetX = x + width - NSMaxX(winFrame)
  174. if offsetX > 0 { // 超出右编辑
  175. x -= offsetX
  176. }
  177. var y = max(0, position.y)
  178. let screenFrame = NSScreen.main?.frame ?? .zero
  179. if y + 44 + 40 >= screenFrame.size.height {
  180. y = screenFrame.size.height - 44 - 40
  181. }
  182. let frame = NSMakeRect(x, y, width, 44)
  183. self.setFrame(frame, display: true)
  184. self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44)
  185. self.orderFront(nil)
  186. // self.makeKeyAndOrderFront(nil)
  187. }
  188. override var isMainWindow: Bool {
  189. return true
  190. }
  191. override var isKeyWindow: Bool {
  192. return true
  193. }
  194. // MARK: - Private Methods
  195. private func _fontSizeItemIsEnabled() -> Bool {
  196. if self.model.editAreasFontSizeIsEqual() {
  197. if let fontSize = self.model.fontSizes.first, fontSize == -1 {
  198. return false
  199. } else {
  200. return true
  201. }
  202. } else {
  203. return false
  204. }
  205. }
  206. private func _fetchTextAlign() -> NSTextAlignment? {
  207. if self.model.editAreasTextAlignmentIsEqual() {
  208. return self.model.textAlignments.first
  209. }
  210. return nil
  211. }
  212. }
  213. extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol {
  214. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  215. self.appearance = .init(named: appearance)
  216. self.contentViewController?.interfaceThemeDidChanged(appearance)
  217. }
  218. }