KMEditPDFPopToolBarWindow.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. var needUpdateData = false
  101. width = 392
  102. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment]
  103. let keyCnt = contentViewC?.itemKeys.count ?? 0
  104. let dataCnt = contentViewC?.datas.count ?? 0
  105. if dataCnt == 0 || dataCnt != keyCnt {
  106. needUpdateData = true
  107. }
  108. var datas: [KMEditPDFToolbarModel] = []
  109. for key in contentViewC?.itemKeys ?? [] {
  110. let model = KMEditPDFToolbarModel()
  111. model.itemKey = key
  112. if key == .color {
  113. model.isEnabled = self.model.editAreasFontColorIsEqual()
  114. } else if key == .fontStyle {
  115. if self.model.editAreasFontNameIsEqual() {
  116. model.isEnabled = true
  117. model.fontName = self.model.fontNames.first
  118. if model.fontName != self._fetchCurrentFontName() {
  119. needUpdateData = true
  120. }
  121. } else {
  122. model.isEnabled = false
  123. // model.fontName = "Helvetica"
  124. model.fontName = "--"
  125. }
  126. } else if key == .fontAdd {
  127. model.isEnabled = self._fontSizeItemIsEnabled()
  128. } else if key == .fontReduce {
  129. model.isEnabled = self._fontSizeItemIsEnabled()
  130. } else if key == .fontBold {
  131. model.isEnabled = self.model.editAreasFontBoldIsEqual()
  132. model.isSelected = self.model.fontBolds.first ?? false
  133. model.isChanged = model.isSelected != self._fetchCurrentBoldIsSelected()
  134. if model.isSelected != self._fetchCurrentBoldIsSelected() {
  135. needUpdateData = true
  136. }
  137. } else if key == .fontItalic {
  138. model.isEnabled = self.model.editAreasFontItalicIsEqual()
  139. model.isSelected = self.model.fontItalics.first ?? false
  140. if model.isSelected != self._fetchCurrentItalicIsSelected() {
  141. needUpdateData = true
  142. }
  143. } else if key == .textAlignment {
  144. // model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  145. if let data = self._fetchTextAlign() {
  146. model.textAlign = data
  147. }
  148. // model.isSelected = true
  149. if model.textAlign != self._fetchCurrentTextAlign() {
  150. needUpdateData = true
  151. }
  152. }
  153. datas.append(model)
  154. }
  155. if needUpdateData {
  156. contentViewC?.datas = datas
  157. }
  158. }
  159. }
  160. } else {
  161. if self.style.contains(.image) { // image
  162. if self.isMultiple {
  163. width = 396-20
  164. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop]
  165. var datas: [KMEditPDFToolbarModel] = []
  166. for key in contentViewC?.itemKeys ?? [] {
  167. let model = KMEditPDFToolbarModel()
  168. model.itemKey = key
  169. if key == .crop {
  170. model.isEnabled = !self.isMultiple
  171. } else if key == .replace {
  172. model.isEnabled = !self.isMultiple
  173. }
  174. datas.append(model)
  175. }
  176. contentViewC?.datas = datas
  177. } else {
  178. width = 304-16
  179. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export]
  180. var datas: [KMEditPDFToolbarModel] = []
  181. for key in contentViewC?.itemKeys ?? [] {
  182. let model = KMEditPDFToolbarModel()
  183. model.itemKey = key
  184. if key == .crop {
  185. model.isEnabled = !self.isMultiple
  186. }
  187. datas.append(model)
  188. }
  189. contentViewC?.datas = datas
  190. }
  191. } else { // none
  192. }
  193. }
  194. let winFrame = positioningView.window?.frame ?? .zero
  195. var position = positioningView.convert(positioningRect.origin, to: nil)
  196. position.x += winFrame.origin.x
  197. position.y += winFrame.origin.y
  198. position.y += positioningRect.size.height
  199. position.y += 26
  200. position.x += (positioningRect.size.width*0.5-width*0.5)
  201. // var x = max(0, position.x)
  202. var x = max(winFrame.origin.x, position.x)
  203. let offsetX = x + width - NSMaxX(winFrame)
  204. if offsetX > 0 { // 超出右编辑
  205. x -= offsetX
  206. }
  207. var y = max(0, position.y)
  208. let screenFrame = NSScreen.main?.frame ?? .zero
  209. if y + 44 + 40 >= screenFrame.size.height {
  210. y = screenFrame.size.height - 44 - 40
  211. }
  212. let frame = NSMakeRect(x, y, width, 44)
  213. self.setFrame(frame, display: true)
  214. self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44)
  215. self.orderFront(nil)
  216. // self.makeKeyAndOrderFront(nil)
  217. }
  218. override var isMainWindow: Bool {
  219. return true
  220. }
  221. override var isKeyWindow: Bool {
  222. return true
  223. }
  224. func updateFontColor(fontColor: NSColor?) {
  225. if self.isVisible {
  226. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  227. contentViewC?.fontColor = fontColor
  228. for model in contentViewC?.datas ?? [] {
  229. if model.itemKey == .color {
  230. model.isEnabled = fontColor != nil
  231. contentViewC?.toolbarView?.reloadData()
  232. break;
  233. }
  234. }
  235. }
  236. }
  237. func updateTextAlign(align: NSTextAlignment) {
  238. if self.isVisible {
  239. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  240. for model in contentViewC?.datas ?? [] {
  241. if model.itemKey == .textAlignment {
  242. if model.textAlign == align {
  243. break;
  244. }
  245. model.textAlign = align
  246. contentViewC?.toolbarView?.reloadData()
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. func updateFontSizeButtons(enable: Bool) {
  253. if self.isVisible {
  254. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  255. for model in contentViewC?.datas ?? [] {
  256. if model.itemKey == .fontAdd || model.itemKey == .fontReduce {
  257. model.isEnabled = enable
  258. }
  259. }
  260. contentViewC?.toolbarView?.reloadData()
  261. }
  262. }
  263. // MARK: - Private Methods
  264. private func _fontSizeItemIsEnabled() -> Bool {
  265. if self.model.editAreasFontSizeIsEqual() {
  266. if let fontSize = self.model.fontSizes.first, fontSize == -1 {
  267. return false
  268. } else {
  269. return true
  270. }
  271. } else {
  272. return false
  273. }
  274. }
  275. private func _fetchTextAlign() -> NSTextAlignment? {
  276. if self.model.editAreasTextAlignmentIsEqual() {
  277. return self.model.textAlignments.first
  278. }
  279. return nil
  280. }
  281. private func _fetchCurrentBoldIsSelected() -> Bool {
  282. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  283. return false
  284. }
  285. for model in contentC.datas {
  286. if model.itemKey == .fontBold {
  287. return model.isSelected
  288. }
  289. }
  290. return false
  291. }
  292. private func _fetchCurrentItalicIsSelected() -> Bool {
  293. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  294. return false
  295. }
  296. for model in contentC.datas {
  297. if model.itemKey == .fontItalic {
  298. return model.isSelected
  299. }
  300. }
  301. return false
  302. }
  303. private func _fetchCurrentTextAlign() -> NSTextAlignment? {
  304. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  305. return nil
  306. }
  307. for model in contentC.datas {
  308. if model.itemKey == .textAlignment {
  309. return model.textAlign
  310. }
  311. }
  312. return nil
  313. }
  314. private func _fetchCurrentFontName() -> String? {
  315. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  316. return nil
  317. }
  318. for model in contentC.datas {
  319. if model.itemKey == .fontStyle {
  320. return model.fontName
  321. }
  322. }
  323. return nil
  324. }
  325. }
  326. extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol {
  327. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  328. self.appearance = .init(named: appearance)
  329. self.contentViewController?.interfaceThemeDidChanged(appearance)
  330. }
  331. }