KMEditPDFPopToolBarWindow.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. var width: CGFloat = 392
  39. if self.style.contains(.text) {
  40. if self.style.contains(.image) { // text + image
  41. contentViewC?.itemKeys = [.alignmentLeft, .alignmentCenterX, .alignmentRight, .alignmentjustifiedX, .alignmentTop, .alignmentCenterY, .alignmentBottom, .alignmentjustifiedY]
  42. var datas: [KMEditPDFToolbarModel] = []
  43. for key in contentViewC?.itemKeys ?? [] {
  44. let model = KMEditPDFToolbarModel()
  45. model.itemKey = key
  46. datas.append(model)
  47. }
  48. contentViewC?.datas = datas
  49. width = 320
  50. } else { // text
  51. if self.isMultiple {
  52. width = 478
  53. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment, .separator, .alignmentLeft, .alignmentTop]
  54. var datas: [KMEditPDFToolbarModel] = []
  55. for key in contentViewC?.itemKeys ?? [] {
  56. let model = KMEditPDFToolbarModel()
  57. model.itemKey = key
  58. if key == .color {
  59. model.isEnabled = self.model.editAreasFontColorIsEqual()
  60. } else if key == .fontStyle {
  61. model.isEnabled = self.model.editAreasFontNameIsEqual()
  62. } else if key == .fontAdd {
  63. model.isEnabled = self.model.editAreasFontSizeIsEqual()
  64. } else if key == .fontReduce {
  65. model.isEnabled = self.model.editAreasFontSizeIsEqual()
  66. } else if key == .fontBold {
  67. model.isEnabled = self.model.editAreasFontBoldIsEqual()
  68. } else if key == .fontItalic {
  69. model.isEnabled = self.model.editAreasFontItalicIsEqual()
  70. } else if key == .textAlignment {
  71. model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  72. }
  73. datas.append(model)
  74. }
  75. contentViewC?.datas = datas
  76. } else {
  77. width = 392
  78. contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment]
  79. var datas: [KMEditPDFToolbarModel] = []
  80. for key in contentViewC?.itemKeys ?? [] {
  81. let model = KMEditPDFToolbarModel()
  82. model.itemKey = key
  83. if key == .color {
  84. model.isEnabled = self.model.editAreasFontColorIsEqual()
  85. } else if key == .fontStyle {
  86. model.isEnabled = self.model.editAreasFontNameIsEqual()
  87. } else if key == .fontAdd {
  88. model.isEnabled = self.model.editAreasFontSizeIsEqual()
  89. } else if key == .fontReduce {
  90. model.isEnabled = self.model.editAreasFontSizeIsEqual()
  91. } else if key == .fontBold {
  92. model.isEnabled = self.model.editAreasFontBoldIsEqual()
  93. } else if key == .fontItalic {
  94. model.isEnabled = self.model.editAreasFontItalicIsEqual()
  95. } else if key == .textAlignment {
  96. model.isEnabled = self.model.editAreasTextAlignmentIsEqual()
  97. }
  98. datas.append(model)
  99. }
  100. contentViewC?.datas = datas
  101. }
  102. }
  103. } else {
  104. if self.style.contains(.image) { // image
  105. if self.isMultiple {
  106. width = 396
  107. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop]
  108. var datas: [KMEditPDFToolbarModel] = []
  109. for key in contentViewC?.itemKeys ?? [] {
  110. let model = KMEditPDFToolbarModel()
  111. model.itemKey = key
  112. if key == .crop {
  113. model.isEnabled = !self.isMultiple
  114. }
  115. datas.append(model)
  116. }
  117. contentViewC?.datas = datas
  118. } else {
  119. width = 304
  120. contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export]
  121. var datas: [KMEditPDFToolbarModel] = []
  122. for key in contentViewC?.itemKeys ?? [] {
  123. let model = KMEditPDFToolbarModel()
  124. model.itemKey = key
  125. if key == .crop {
  126. model.isEnabled = !self.isMultiple
  127. }
  128. datas.append(model)
  129. }
  130. contentViewC?.datas = datas
  131. }
  132. } else { // none
  133. }
  134. }
  135. contentViewC?.fontColor = self.model.fontColors.last ?? .black
  136. let winFrame = positioningView.window?.frame ?? .zero
  137. var position = positioningView.convert(positioningRect.origin, to: nil)
  138. // var position = NSEvent.mouseLocation
  139. position.x += winFrame.origin.x
  140. position.y += winFrame.origin.y
  141. position.y += positioningRect.size.height
  142. position.y += 26
  143. // position.x -= 60
  144. // self.setFrameOrigin(position)
  145. let frame = NSMakeRect(position.x, position.y, width, 44)
  146. self.setFrame(frame, display: true)
  147. self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44)
  148. self.orderFront(nil)
  149. // self.makeKeyAndOrderFront(nil)
  150. }
  151. override var isMainWindow: Bool {
  152. return true
  153. }
  154. override var isKeyWindow: Bool {
  155. return true
  156. }
  157. }
  158. extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol {
  159. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  160. self.appearance = .init(named: appearance)
  161. self.contentViewController?.interfaceThemeDidChanged(appearance)
  162. }
  163. }