KMToolbarCustomItemView.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // KMToolbarCustomItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/4.
  6. //
  7. import Cocoa
  8. class KMToolbarCustomItemView: KMToolbarItemView {
  9. private var menuViewC_: KMCustomButtonPopMenuViewController?
  10. private var popOver_: NSPopover?
  11. var popOver: NSPopover? {
  12. get {
  13. return self.popOver_
  14. }
  15. set {
  16. if self.popOver_ == nil || self.popOver_!.isEqual(to: newValue) == false {
  17. self.popOver_ = newValue
  18. if (newValue != nil) {
  19. self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  20. } else {
  21. self.updateSelectBackground()
  22. }
  23. }
  24. }
  25. }
  26. convenience init(itemIdentifier: String, postition imagePosition: NSControl.ImagePosition, withPopMenu popMenuViewController: KMCustomButtonPopMenuViewController?) {
  27. self.init(itemIdentifier: itemIdentifier)
  28. self.boxImagePosition = imagePosition
  29. self.menuViewC_ = popMenuViewController
  30. // if (popMenuViewController != nil) {
  31. NotificationCenter.default.addObserver(self, selector: #selector(_windowClosedPop), name: KMToolbarItemView.popOverClosedNotificationName, object: nil)
  32. // }
  33. }
  34. override func draw(_ dirtyRect: NSRect) {
  35. super.draw(dirtyRect)
  36. // Drawing code here.
  37. }
  38. override func mouseEntered(with event: NSEvent) {
  39. super.mouseEntered(with: event)
  40. guard let _window = self.window else {
  41. return
  42. }
  43. if (!_window.isKeyWindow) {
  44. return
  45. }
  46. if (self.itemIdentifier == KMToolbarDividerItemIdentifier || self.customizeView != nil || self.image == nil) {
  47. return
  48. }
  49. if self.unEnabled {
  50. return
  51. }
  52. if self.isPopToolTip {
  53. self._showPopTip(self)
  54. }else {
  55. if (self.needExpandAction) {
  56. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  57. self._showPop(self)
  58. }else if (self.isShowCustomToolTip) {
  59. self.perform(#selector(_showHUDHint), with: nil, afterDelay: 0.1)
  60. }
  61. }
  62. }
  63. override func mouseExited(with event: NSEvent) {
  64. super.mouseExited(with: event)
  65. if(self.needExpandAction && !self.isSelected) {
  66. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  67. }
  68. guard let _window = self.window else {
  69. return
  70. }
  71. if (!_window.isKeyWindow) {
  72. return
  73. }
  74. if(self.nameBtn.superview != nil && !self.isSelected) {
  75. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  76. }
  77. if self.isPopToolTip {
  78. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showHUDHint), object: nil)
  79. self._closePop()
  80. }else{
  81. if (self.isShowCustomToolTip && !self.needExpandAction) {
  82. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showHUDHint), object: nil)
  83. self._closePop()
  84. }
  85. }
  86. }
  87. // MARK: - Popover
  88. private func _showPop(_ sender: NSView) {
  89. if (self.popOver_ != nil) {
  90. return
  91. }
  92. let menuViewController = KMCustomButtonPopMenuViewController()
  93. menuViewController.delegate = self
  94. menuViewController.dataSources = self
  95. self.popOver = NSPopover()
  96. self.popOver?.delegate = self
  97. self.popOver?.contentViewController = menuViewController
  98. self.popOver?.animates = false
  99. self.popOver?.behavior = .semitransient
  100. self.popOver?.contentSize = menuViewController.view.frame.size
  101. var sourcesRect = sender.bounds
  102. sourcesRect = sender.convert(sourcesRect, to: nil)
  103. sourcesRect.origin.y -= 20
  104. sourcesRect.size.height += 20
  105. self.window?.popover = self.popOver
  106. self.window?.sourcesRect = sourcesRect
  107. self.popOver?.show(relativeTo: KMRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
  108. }
  109. func _showPopTip(_ sender: NSView) {
  110. if (self.popOver_ != nil) {
  111. return
  112. }
  113. let popViewController = KMToolbarItemPopViewController()
  114. self.popOver = NSPopover()
  115. self.popOver?.contentViewController = popViewController
  116. self.popOver?.animates = false
  117. self.popOver?.behavior = .semitransient
  118. self.popOver?.contentSize = popViewController.view.frame.size
  119. popViewController.updateWithHelpTip(helpTip: self.toolTip ?? "")
  120. self.popOver?.show(relativeTo: KMRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
  121. }
  122. private func _closePop() {
  123. self.popOver?.close()
  124. self.popOver = nil
  125. }
  126. @objc private func _showHUDHint() {
  127. // KMToolbarItemPopViewController *popViewController = [[[KMToolbarItemPopViewController alloc] init] autorelease];
  128. // self.popOver = [[[NSPopover alloc] init] autorelease];
  129. // self.popOver.contentViewController = popViewController;
  130. // self.popOver.animates = NO;
  131. // self.popOver.behavior = NSPopoverBehaviorSemitransient;
  132. // self.popOver.backgroundColor = [KMAppearance KMBluegrey01Color];
  133. //
  134. // self.popOver.contentSize = popViewController.view.frame.size;
  135. // [popViewController updateWithHelpTip:self.originalHelpTip];
  136. // [self.popOver showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMinY];
  137. }
  138. @objc private func _windowClosedPop(sender: Notification) {
  139. if let data = self.popOver_?.isEqual(to: sender.object), data {
  140. self.popOver = nil
  141. }
  142. }
  143. }
  144. extension KMToolbarCustomItemView: NSPopoverDelegate {
  145. func popoverDidClose(_ notification: Notification) {
  146. if let data = self.popOver?.isEqual(to: notification.object), data {
  147. self._closePop()
  148. }
  149. }
  150. }
  151. extension KMToolbarCustomItemView: KMCustomButtonPopMenuViewControllerDelegate, KMCustomButtonPopMenuViewControllerDataSources {
  152. func customViewButtonPopDidSelectIndex(_ index: Int) {
  153. self._closePop()
  154. if let items = self.menuFormRepresentation?.submenu?.items {
  155. let item = items[index]
  156. _ = item.target?.perform(item.action, with: item)
  157. }
  158. }
  159. func numberOfLine() -> Int {
  160. if let items = self.menuFormRepresentation?.submenu?.items {
  161. return items.count
  162. }
  163. return 0
  164. }
  165. func stringForLine(at index: Int) -> String? {
  166. if let items = self.menuFormRepresentation?.submenu?.items {
  167. return items[index].title
  168. }
  169. return nil
  170. }
  171. func needInsertSeperateLine(at index: Int) -> Bool {
  172. if let items = self.menuFormRepresentation?.submenu?.items {
  173. return items[index].isSeparatorItem
  174. }
  175. return false
  176. }
  177. func needHightLightLine(at index: Int) -> Bool {
  178. return false
  179. }
  180. func imageForLine(at index: Int) -> NSImage? {
  181. if let items = self.menuFormRepresentation?.submenu?.items {
  182. return items[index].image
  183. }
  184. return nil
  185. }
  186. func itemEnable(at index: Int) -> Bool {
  187. if self.itemIdentifier == KMToolbarToolFormAlignIdentifier {
  188. if index <= 6 {
  189. if let cnt = self.pdfView?.activeAnnotations.count, cnt >= 2 {
  190. return true
  191. }
  192. } else {
  193. if let cnt = self.pdfView?.activeAnnotations.count, cnt >= 3 {
  194. return true
  195. }
  196. }
  197. return false
  198. }
  199. return true
  200. }
  201. }