KMEditPDFPopToolBarWindow.swift 15 KB

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