123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // KMEditPDFToolbarItemView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2024/6/25.
- //
- import Cocoa
- class KMEditPDFToolbarItemView: NSView {
- private lazy var contentBox_: NSBox = {
- let view = NSBox()
- view.boxType = .custom
- view.titlePosition = .noTitle
- view.contentViewMargins = .zero
- view.borderWidth = 0
- return view
- }()
-
- var view: NSView? {
- didSet {
- if let data = self.view {
- self.contentBox_.contentView = data
- }
-
- self.needsLayout = true
- }
- }
-
- var obj: AnyObject?
-
- private var area_: NSTrackingArea?
-
- private var popOver_: NSPopover?
- var popOver: NSPopover? {
- get {
- return self.popOver_
- }
- set {
- if self.popOver_ == nil || self.popOver_!.isEqual(to: newValue) == false {
- self.popOver_ = newValue
- if (newValue != nil) {
- // self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
- } else {
- // self.updateSelectBackground()
- }
- }
- }
- }
- var popToolTip: String?
-
- deinit {
- KMPrint("KMEditPDFToolbarItemView deinit.")
- if let trackingArea = self.area_ {
- self.removeTrackingArea(trackingArea)
- }
- }
-
- convenience init() {
- self.init(frame: .zero)
- self.addSubview(self.contentBox_)
- }
-
- override func layout() {
- super.layout()
-
- let height = NSHeight(self.bounds)
- let width = NSWidth(self.bounds)
- // let boxFrame = self.contentBox_.contentView?.frame ?? .zero
- // let boxX = (width - boxFrame.size.width) * 0.5
- // let boxY = (height - boxFrame.size.height) * 0.5
- // self.contentBox_.frame = NSMakeRect(boxX, boxY, boxFrame.size.width, boxFrame.size.height)
- self.contentBox_.frame = self.bounds
-
- // KMPrint("-------- \(boxFrame)")
- }
-
- override func updateTrackingAreas() {
- super.updateTrackingAreas()
- if let existingArea = self.area_ {
- self.removeTrackingArea(existingArea)
- self.area_ = nil
- }
- let opts: NSTrackingArea.Options = [.mouseEnteredAndExited, .mouseMoved, .activeAlways]
- self.area_ = NSTrackingArea(rect: bounds, options: opts, owner: self, userInfo: nil)
- if let trackingArea = self.area_ {
- self.addTrackingArea(trackingArea)
- }
- }
-
- override func mouseEntered(with event: NSEvent) {
- if let data = self.popToolTip, data.isEmpty == false {
- Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showPopTip), object: self)
- self.perform(#selector(_showPopTip), with: self, afterDelay: 0.8)
- }
- }
-
- override func mouseExited(with event: NSEvent) {
- if let data = self.popToolTip, data.isEmpty == false {
- Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showPopTip), object: self)
- self._closePop()
- }
- }
-
- @objc func _showPopTip(_ sender: NSView) {
- if (self.popOver_ != nil) {
- return
- }
- if self.window == nil {
- return
- }
- let popViewController = KMToolbarItemPopViewController()
- self.popOver = NSPopover()
- self.popOver?.contentViewController = popViewController
- self.popOver?.animates = false
- self.popOver?.behavior = .semitransient
- self.popOver?.contentSize = popViewController.view.frame.size
-
- popViewController.updateWithHelpTip(helpTip: self.popToolTip ?? "")
- self.popOver?.show(relativeTo: KMRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
- }
-
- private func _closePop() {
- self.popOver?.close()
- self.popOver = nil
- }
- }
|