KMEditPDFToolbarItemView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // KMEditPDFToolbarItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/6/25.
  6. //
  7. import Cocoa
  8. class KMEditPDFToolbarItemView: NSView {
  9. private lazy var contentBox_: NSBox = {
  10. let view = NSBox()
  11. view.boxType = .custom
  12. view.titlePosition = .noTitle
  13. view.contentViewMargins = .zero
  14. view.borderWidth = 0
  15. return view
  16. }()
  17. var view: NSView? {
  18. didSet {
  19. if let data = self.view {
  20. self.contentBox_.contentView = data
  21. }
  22. self.needsLayout = true
  23. }
  24. }
  25. var obj: AnyObject?
  26. private var area_: NSTrackingArea?
  27. private var popOver_: NSPopover?
  28. var popOver: NSPopover? {
  29. get {
  30. return self.popOver_
  31. }
  32. set {
  33. if self.popOver_ == nil || self.popOver_!.isEqual(to: newValue) == false {
  34. self.popOver_ = newValue
  35. if (newValue != nil) {
  36. // self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  37. } else {
  38. // self.updateSelectBackground()
  39. }
  40. }
  41. }
  42. }
  43. var popToolTip: String?
  44. deinit {
  45. KMPrint("KMEditPDFToolbarItemView deinit.")
  46. if let trackingArea = self.area_ {
  47. self.removeTrackingArea(trackingArea)
  48. }
  49. }
  50. convenience init() {
  51. self.init(frame: .zero)
  52. self.addSubview(self.contentBox_)
  53. }
  54. override func layout() {
  55. super.layout()
  56. let height = NSHeight(self.bounds)
  57. let width = NSWidth(self.bounds)
  58. // let boxFrame = self.contentBox_.contentView?.frame ?? .zero
  59. // let boxX = (width - boxFrame.size.width) * 0.5
  60. // let boxY = (height - boxFrame.size.height) * 0.5
  61. // self.contentBox_.frame = NSMakeRect(boxX, boxY, boxFrame.size.width, boxFrame.size.height)
  62. self.contentBox_.frame = self.bounds
  63. // KMPrint("-------- \(boxFrame)")
  64. }
  65. override func updateTrackingAreas() {
  66. super.updateTrackingAreas()
  67. if let existingArea = self.area_ {
  68. self.removeTrackingArea(existingArea)
  69. self.area_ = nil
  70. }
  71. let opts: NSTrackingArea.Options = [.mouseEnteredAndExited, .mouseMoved, .activeAlways]
  72. self.area_ = NSTrackingArea(rect: bounds, options: opts, owner: self, userInfo: nil)
  73. if let trackingArea = self.area_ {
  74. self.addTrackingArea(trackingArea)
  75. }
  76. }
  77. override func mouseEntered(with event: NSEvent) {
  78. if let data = self.popToolTip, data.isEmpty == false {
  79. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showPopTip), object: self)
  80. self.perform(#selector(_showPopTip), with: self, afterDelay: 0.8)
  81. }
  82. }
  83. override func mouseExited(with event: NSEvent) {
  84. if let data = self.popToolTip, data.isEmpty == false {
  85. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showPopTip), object: self)
  86. self._closePop()
  87. }
  88. }
  89. @objc func _showPopTip(_ sender: NSView) {
  90. if (self.popOver_ != nil) {
  91. return
  92. }
  93. if self.window == nil {
  94. return
  95. }
  96. let popViewController = KMToolbarItemPopViewController()
  97. self.popOver = NSPopover()
  98. self.popOver?.contentViewController = popViewController
  99. self.popOver?.animates = false
  100. self.popOver?.behavior = .semitransient
  101. self.popOver?.contentSize = popViewController.view.frame.size
  102. popViewController.updateWithHelpTip(helpTip: self.popToolTip ?? "")
  103. self.popOver?.show(relativeTo: KMRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
  104. }
  105. private func _closePop() {
  106. self.popOver?.close()
  107. self.popOver = nil
  108. }
  109. }