KMEditPDFPopToolBarWindow.swift 11 KB

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