KMEditPDFPopToolBarWindow.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. if key == .fontAdd || key == .fontReduce {
  155. if let item = self._fetchCurrentItem(itemKey: key) {
  156. if item.isEnabled != model.isEnabled {
  157. needUpdateData = true
  158. }
  159. }
  160. }
  161. }
  162. if needUpdateData {
  163. contentViewC?.datas = datas
  164. }
  165. }
  166. }
  167. } else {
  168. if self.style.contains(.image) { // image
  169. if self.isMultiple {
  170. width = 396-20
  171. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop]
  172. var datas: [KMEditPDFToolbarModel] = []
  173. for key in contentViewC?.itemKeys ?? [] {
  174. let model = KMEditPDFToolbarModel()
  175. model.itemKey = key
  176. if key == .crop {
  177. model.isEnabled = !self.isMultiple
  178. } else if key == .replace {
  179. model.isEnabled = !self.isMultiple
  180. }
  181. datas.append(model)
  182. }
  183. contentViewC?.datas = datas
  184. } else {
  185. width = 304-16
  186. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export]
  187. var datas: [KMEditPDFToolbarModel] = []
  188. for key in contentViewC?.itemKeys ?? [] {
  189. let model = KMEditPDFToolbarModel()
  190. model.itemKey = key
  191. if key == .crop {
  192. model.isEnabled = !self.isMultiple
  193. }
  194. datas.append(model)
  195. }
  196. contentViewC?.datas = datas
  197. }
  198. } else { // none
  199. }
  200. }
  201. let winFrame = positioningView.window?.frame ?? .zero
  202. var position = positioningView.convert(positioningRect.origin, to: nil)
  203. position.x += winFrame.origin.x
  204. position.y += winFrame.origin.y
  205. position.y += positioningRect.size.height
  206. position.y += 26
  207. position.x += (positioningRect.size.width*0.5-width*0.5)
  208. // var x = max(0, position.x)
  209. var x = max(winFrame.origin.x, position.x)
  210. let offsetX = x + width - NSMaxX(winFrame)
  211. if offsetX > 0 { // 超出右编辑
  212. x -= offsetX
  213. }
  214. var y = max(0, position.y)
  215. let screenFrame = NSScreen.main?.frame ?? .zero
  216. if y + 44 + 40 >= screenFrame.size.height {
  217. y = screenFrame.size.height - 44 - 40
  218. }
  219. let frame = NSMakeRect(x, y, width, 44)
  220. self.setFrame(frame, display: true)
  221. self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44)
  222. self.orderFront(nil)
  223. // self.makeKeyAndOrderFront(nil)
  224. }
  225. override var isMainWindow: Bool {
  226. return true
  227. }
  228. override var isKeyWindow: Bool {
  229. return true
  230. }
  231. func updateFontColor(fontColor: NSColor?) {
  232. if self.isVisible {
  233. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  234. contentViewC?.fontColor = fontColor
  235. for model in contentViewC?.datas ?? [] {
  236. if model.itemKey == .color {
  237. model.isEnabled = fontColor != nil
  238. contentViewC?.toolbarView?.reloadData()
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. func updateTextAlign(align: NSTextAlignment) {
  245. if self.isVisible {
  246. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  247. for model in contentViewC?.datas ?? [] {
  248. if model.itemKey == .textAlignment {
  249. if model.textAlign == align {
  250. break;
  251. }
  252. model.textAlign = align
  253. contentViewC?.toolbarView?.reloadData()
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. func updateFontSizeButtons(enable: Bool) {
  260. if self.isVisible {
  261. let contentViewC = self.contentViewController as? KMEditPDFPopToolBarController
  262. for model in contentViewC?.datas ?? [] {
  263. if model.itemKey == .fontAdd || model.itemKey == .fontReduce {
  264. model.isEnabled = enable
  265. }
  266. }
  267. contentViewC?.toolbarView?.reloadData()
  268. }
  269. }
  270. // MARK: - Private Methods
  271. private func _fetchCurrentItem(itemKey: KMEditPDFToolbarItemKey) -> KMEditPDFToolbarModel? {
  272. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  273. return nil
  274. }
  275. for model in contentC.datas {
  276. if model.itemKey == itemKey {
  277. return model
  278. }
  279. }
  280. return nil
  281. }
  282. private func _fontSizeItemIsEnabled() -> Bool {
  283. if self.model.editAreasFontSizeIsEqual() {
  284. if let fontSize = self.model.fontSizes.first, fontSize == -1 {
  285. return false
  286. } else {
  287. return true
  288. }
  289. } else {
  290. return false
  291. }
  292. }
  293. private func _fetchTextAlign() -> NSTextAlignment? {
  294. if self.model.editAreasTextAlignmentIsEqual() {
  295. return self.model.textAlignments.first
  296. }
  297. return nil
  298. }
  299. private func _fetchCurrentBoldIsSelected() -> Bool {
  300. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  301. return false
  302. }
  303. for model in contentC.datas {
  304. if model.itemKey == .fontBold {
  305. return model.isSelected
  306. }
  307. }
  308. return false
  309. }
  310. private func _fetchCurrentItalicIsSelected() -> Bool {
  311. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  312. return false
  313. }
  314. for model in contentC.datas {
  315. if model.itemKey == .fontItalic {
  316. return model.isSelected
  317. }
  318. }
  319. return false
  320. }
  321. private func _fetchCurrentTextAlign() -> NSTextAlignment? {
  322. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  323. return nil
  324. }
  325. for model in contentC.datas {
  326. if model.itemKey == .textAlignment {
  327. return model.textAlign
  328. }
  329. }
  330. return nil
  331. }
  332. private func _fetchCurrentFontName() -> String? {
  333. guard let contentC = self.contentViewController as? KMEditPDFPopToolBarController else {
  334. return nil
  335. }
  336. for model in contentC.datas {
  337. if model.itemKey == .fontStyle {
  338. return model.fontName
  339. }
  340. }
  341. return nil
  342. }
  343. }
  344. extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol {
  345. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  346. self.appearance = .init(named: appearance)
  347. self.contentViewController?.interfaceThemeDidChanged(appearance)
  348. }
  349. }