KMEditPDFPopToolBarWindow.swift 9.3 KB

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