KMEditPDFPopToolBarWindow.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. contentViewC?.toolbarView?.reloadData()
  41. var width: CGFloat = 392
  42. if self.style.contains(.text) {
  43. if self.style.contains(.image) { // text + image
  44. contentViewC?.itemKeys = [.alignmentLeft, .alignmentCenterX, .alignmentRight, .alignmentjustifiedX, .alignmentTop, .alignmentCenterY, .alignmentBottom, .alignmentjustifiedY]
  45. var datas: [KMEditPDFToolbarModel] = []
  46. for key in contentViewC?.itemKeys ?? [] {
  47. let model = KMEditPDFToolbarModel()
  48. model.itemKey = key
  49. if key == .alignmentjustifiedX || key == .alignmentjustifiedY {
  50. let areas = self.model.editingAreas ?? []
  51. model.isEnabled = areas.count > 2
  52. }
  53. datas.append(model)
  54. }
  55. contentViewC?.datas = datas
  56. width = 320-36
  57. } else { // text
  58. if self.isMultiple {
  59. width = 478
  60. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment, .separator, .alignmentLeft, .alignmentTop]
  61. var datas: [KMEditPDFToolbarModel] = []
  62. for key in contentViewC?.itemKeys ?? [] {
  63. let model = KMEditPDFToolbarModel()
  64. model.itemKey = key
  65. if key == .color {
  66. model.isEnabled = self.model.editAreasFontColorIsEqual()
  67. } else if key == .fontStyle {
  68. if self.model.editAreasFontNameIsEqual() {
  69. model.isEnabled = true
  70. model.fontName = self.model.fontNames.first
  71. } else {
  72. model.isEnabled = false
  73. model.fontName = nil
  74. }
  75. } else if key == .fontAdd {
  76. model.isEnabled = self._fontSizeItemIsEnabled()
  77. } else if key == .fontReduce {
  78. model.isEnabled = self._fontSizeItemIsEnabled()
  79. } else if key == .fontBold {
  80. model.isEnabled = self.model.editAreasFontBoldIsEqual()
  81. if self.model.editAreasFontBoldIsEqual() {
  82. model.isSelected = self.model.fontBolds.first ?? false
  83. }
  84. } else if key == .fontItalic {
  85. model.isEnabled = self.model.editAreasFontItalicIsEqual()
  86. if self.model.editAreasFontItalicIsEqual() {
  87. model.isSelected = self.model.fontItalics.first ?? false
  88. }
  89. } else if key == .textAlignment {
  90. // model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  91. if let data = self._fetchTextAlign() {
  92. model.textAlign = data
  93. }
  94. // model.isSelected = true
  95. }
  96. datas.append(model)
  97. }
  98. contentViewC?.datas = datas
  99. } else {
  100. width = 392
  101. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment]
  102. var datas: [KMEditPDFToolbarModel] = []
  103. for key in contentViewC?.itemKeys ?? [] {
  104. let model = KMEditPDFToolbarModel()
  105. model.itemKey = key
  106. if key == .color {
  107. model.isEnabled = self.model.editAreasFontColorIsEqual()
  108. } else if key == .fontStyle {
  109. if self.model.editAreasFontNameIsEqual() {
  110. model.isEnabled = true
  111. model.fontName = self.model.fontNames.first
  112. } else {
  113. model.isEnabled = false
  114. // model.fontName = "Helvetica"
  115. model.fontName = "--"
  116. }
  117. } else if key == .fontAdd {
  118. model.isEnabled = self._fontSizeItemIsEnabled()
  119. } else if key == .fontReduce {
  120. model.isEnabled = self._fontSizeItemIsEnabled()
  121. } else if key == .fontBold {
  122. model.isEnabled = self.model.editAreasFontBoldIsEqual()
  123. model.isSelected = self.model.fontBolds.first ?? false
  124. } else if key == .fontItalic {
  125. model.isEnabled = self.model.editAreasFontItalicIsEqual()
  126. model.isSelected = self.model.fontItalics.first ?? false
  127. } else if key == .textAlignment {
  128. // model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  129. if let data = self._fetchTextAlign() {
  130. model.textAlign = data
  131. }
  132. // model.isSelected = true
  133. }
  134. datas.append(model)
  135. }
  136. contentViewC?.datas = datas
  137. }
  138. }
  139. } else {
  140. if self.style.contains(.image) { // image
  141. if self.isMultiple {
  142. width = 396-20
  143. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop]
  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. } else if key == .replace {
  151. model.isEnabled = !self.isMultiple
  152. }
  153. datas.append(model)
  154. }
  155. contentViewC?.datas = datas
  156. } else {
  157. width = 304-16
  158. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export]
  159. var datas: [KMEditPDFToolbarModel] = []
  160. for key in contentViewC?.itemKeys ?? [] {
  161. let model = KMEditPDFToolbarModel()
  162. model.itemKey = key
  163. if key == .crop {
  164. model.isEnabled = !self.isMultiple
  165. }
  166. datas.append(model)
  167. }
  168. contentViewC?.datas = datas
  169. }
  170. } else { // none
  171. }
  172. }
  173. let winFrame = positioningView.window?.frame ?? .zero
  174. var position = positioningView.convert(positioningRect.origin, to: nil)
  175. position.x += winFrame.origin.x
  176. position.y += winFrame.origin.y
  177. position.y += positioningRect.size.height
  178. position.y += 26
  179. position.x += (positioningRect.size.width*0.5-width*0.5)
  180. // var x = max(0, position.x)
  181. var x = max(winFrame.origin.x, position.x)
  182. let offsetX = x + width - NSMaxX(winFrame)
  183. if offsetX > 0 { // 超出右编辑
  184. x -= offsetX
  185. }
  186. var y = max(0, position.y)
  187. let screenFrame = NSScreen.main?.frame ?? .zero
  188. if y + 44 + 40 >= screenFrame.size.height {
  189. y = screenFrame.size.height - 44 - 40
  190. }
  191. let frame = NSMakeRect(x, y, width, 44)
  192. self.setFrame(frame, display: true)
  193. self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44)
  194. self.orderFront(nil)
  195. // self.makeKeyAndOrderFront(nil)
  196. }
  197. override var isMainWindow: Bool {
  198. return true
  199. }
  200. override var isKeyWindow: Bool {
  201. return true
  202. }
  203. func updateFontColor(fontColor: NSColor?) {
  204. if self.isVisible {
  205. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  206. contentViewC?.fontColor = fontColor
  207. for model in contentViewC?.datas ?? [] {
  208. if model.itemKey == .color {
  209. model.isEnabled = fontColor != nil
  210. contentViewC?.toolbarView?.reloadData()
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. func updateTextAlign(align: NSTextAlignment) {
  217. if self.isVisible {
  218. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  219. for model in contentViewC?.datas ?? [] {
  220. if model.itemKey == .textAlignment {
  221. if model.textAlign == align {
  222. break;
  223. }
  224. model.textAlign = align
  225. contentViewC?.toolbarView?.reloadData()
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. func updateFontSizeButtons(enable: Bool) {
  232. if self.isVisible {
  233. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  234. for model in contentViewC?.datas ?? [] {
  235. if model.itemKey == .fontAdd || model.itemKey == .fontReduce {
  236. model.isEnabled = enable
  237. }
  238. }
  239. contentViewC?.toolbarView?.reloadData()
  240. }
  241. }
  242. // MARK: - Private Methods
  243. private func _fontSizeItemIsEnabled() -> Bool {
  244. if self.model.editAreasFontSizeIsEqual() {
  245. if let fontSize = self.model.fontSizes.first, fontSize == -1 {
  246. return false
  247. } else {
  248. return true
  249. }
  250. } else {
  251. return false
  252. }
  253. }
  254. private func _fetchTextAlign() -> NSTextAlignment? {
  255. if self.model.editAreasTextAlignmentIsEqual() {
  256. return self.model.textAlignments.first
  257. }
  258. return nil
  259. }
  260. }
  261. extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol {
  262. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  263. self.appearance = .init(named: appearance)
  264. self.contentViewController?.interfaceThemeDidChanged(appearance)
  265. }
  266. }