Browse Source

【综合】整理代码

tangchao 11 months ago
parent
commit
4c830dff21

+ 32 - 9
PDF Office/PDF Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift

@@ -196,6 +196,8 @@ extension KMToolbarItemView {
     
     var itemWidth: CGFloat = 0
     var itemHeight: CGFloat = 0
+    var isPopToolTip = false
+    private var area_: NSTrackingArea?
     
     static let kDividerWidth: CGFloat = 8
     static let kHSpace: CGFloat = 4
@@ -257,7 +259,10 @@ extension KMToolbarItemView {
     private var _originalHelpTip: String?
     
     deinit {
-//        Swift.debugPrint("KMToolbarItemView deinit")
+        if (self.area_ != nil) {
+            self.removeTrackingArea(self.area_!)
+            self.area_ = nil
+        }
         NotificationCenter.default.removeObserver(self)
     }
     
@@ -367,6 +372,24 @@ extension KMToolbarItemView {
         }
     }
     
+    override func updateTrackingAreas() {
+        super.updateTrackingAreas()
+
+        if let _area = self.area_, _area.rect.isEmpty == false {
+            if (_area.rect.equalTo(self.bounds)) {
+                return
+            }
+        }
+        
+        if (self.area_ != nil) {
+            self.removeTrackingArea(self.area_!)
+            self.area_ = nil
+        }
+        
+        self.area_ = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeInKeyWindow], owner: self)
+        self.addTrackingArea(self.area_!)
+    }
+    
     override func mouseEntered(with event: NSEvent) {
         super.mouseEntered(with: event)
         
@@ -398,7 +421,7 @@ extension KMToolbarItemView {
                 }
             }
         }
-        if self.toolTip?.count ?? 0 > 0 {
+        if self.isPopToolTip {
             self._showPopTip(self)
         }else {
             if (self.needExpandAction) {
@@ -450,7 +473,7 @@ extension KMToolbarItemView {
         if(self.nameBtn.superview != nil && !self.isSelected) {
             self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
         }
-        if self.toolTip?.count ?? 0 > 0 {
+        if self.isPopToolTip {
             NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showHUDHint), object: nil)
             self._closePop()
         }else{
@@ -495,8 +518,8 @@ extension KMToolbarItemView {
 
 extension KMToolbarItemView {
     private func _addTrackingArea() {
-        let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeInKeyWindow], owner: self)
-        self.addTrackingArea(trackingArea)
+//        let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeInKeyWindow], owner: self)
+//        self.addTrackingArea(trackingArea)
     }
     
     private func _showPop(_ sender: NSView) {
@@ -531,8 +554,8 @@ extension KMToolbarItemView {
         sourcesRect = sender.convert(sourcesRect, to: nil)
         sourcesRect.origin.y -= 20
         sourcesRect.size.height += 20
-        self.window?.popover = self.popOver
-        self.window?.sourcesRect = sourcesRect
+//        self.window?.popover = self.popOver
+//        self.window?.sourcesRect = sourcesRect
         let popViewController = KMToolbarItemPopViewController()
         self.popOver = NSPopover()
         self.popOver?.contentViewController = popViewController
@@ -542,8 +565,8 @@ extension KMToolbarItemView {
         
         popViewController.updateWithHelpTip(helpTip: self.toolTip ?? "")
 
-        self.window?.popover = self.popOver
-        self.window?.sourcesRect = sourcesRect
+//        self.window?.popover = self.popOver
+//        self.window?.sourcesRect = sourcesRect
         
         self.popOver?.show(relativeTo: CGRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
     }

+ 42 - 0
PDF Office/PDF Master/Class/PDFWindowController/Toolbar/KMToolbarViewController.swift

@@ -1357,7 +1357,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsPantool)
             item?.target = self
             item?.btnTag = KMToolbarViewType.Move.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Scroll Tool", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(leftControllButtonAction)
         } else if identifier == KMToolbarMagnifyToolModeItemIdentifier {
@@ -1365,7 +1367,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsMagnifier)
             item?.target = self
             item?.btnTag = KMToolbarViewType.Magnify.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Magnify Tool", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(leftControllButtonAction)
         } else if identifier == KMToolbarSelectToolModeItemIdentifier {
@@ -1373,7 +1377,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsContentsel)
             item?.target = self
             item?.btnTag = KMToolbarViewType.Select.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Content Selection Tool", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(leftControllButtonAction)
         } else if identifier == KMToolbarZoomToSelectionItemIdentifier {
@@ -1381,7 +1387,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsAreazoom)
             item?.target = self
             item?.btnTag = KMToolbarViewType.SelectZoom.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Zoom to selected area", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(leftControllButtonAction)
         } else if identifier == KMToolbarHighlightAnnotationItemIdentifier {
@@ -1392,7 +1400,9 @@ extension KMToolbarViewController {
             item?.image = imageV.noteTypeImage(withType: SKNHighlightString, color: color)
             item?.target = self
             item?.btnTag = CAnnotationType.highlight.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = String(format: "%@ (⌃⌘H)", NSLocalizedString("Highlight", comment: ""))
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             
             item?.btnAction = #selector(changeAnnotationMode)
@@ -1404,7 +1414,9 @@ extension KMToolbarViewController {
             item?.image = imageV.noteTypeImage(withType: SKNUnderlineString, color: color)
             item?.target = self
             item?.btnTag = CAnnotationType.underline.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = String(format: "%@ (⇧⌘M)", NSLocalizedString("Underline", comment: ""))
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarStrikeOutAnnotationItemIdentifier {
@@ -1415,7 +1427,9 @@ extension KMToolbarViewController {
             item?.image = imageV.noteTypeImage(withType: SKNStrikeOutString, color: color)
             item?.target = self
             item?.btnTag = CAnnotationType.strikeOut.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = String(format: "%@ (⌃⌘M)", NSLocalizedString("Strikethrough", comment: ""))
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarInkAnnotationItemIdentifier {
@@ -1426,7 +1440,9 @@ extension KMToolbarViewController {
             item?.image = imageV.noteTypeImage(withType: SKNInkString, color: color)
             item?.target = self
             item?.btnTag = CAnnotationType.ink.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Add freehand drawings by hand writing just like you do with a pen.", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarFreeTextAnnotationItemIdentifier {
@@ -1434,7 +1450,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsText)
             item?.target = self
             item?.btnTag = CAnnotationType.freeText.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = String(format: "%@ (⌘N)", NSLocalizedString("Text Note", comment: ""))
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarAnchoredAnnotationItemIdentifier {
@@ -1442,7 +1460,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsNote)
             item?.target = self
             item?.btnTag = CAnnotationType.anchored.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = String(format: "%@ (⌥⌘N)", NSLocalizedString("Anchored Note", comment: ""))
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarSquareAnnotationItemIdentifier {
@@ -1450,7 +1470,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsRec)
             item?.target = self
             item?.btnTag = CAnnotationType.square.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Draw rectangle; draw square by holding Shift key", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         }else if identifier == KMToolbarCircleAnnotationItemIdentifier {
@@ -1458,7 +1480,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsOval)
             item?.target = self
             item?.btnTag = CAnnotationType.circle.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Draw oval; draw circle by holding Shift key", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         }  else if identifier == KMToolbarArrowAnnotationItemIdentifier {
@@ -1466,7 +1490,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsArrow)
             item?.target = self
             item?.btnTag = CAnnotationType.arrow.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Draw arrow; draw straight arrow by holding Shift key", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         }  else if identifier == KMToolbarLineAnnotationItemIdentifier {
@@ -1474,7 +1500,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsLine)
             item?.target = self
             item?.btnTag = CAnnotationType.line.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Draw line; draw straight line by holding Shift key", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         }  else if identifier ==  KMToolbarLinkAnnotationItemIdentifier {
@@ -1482,7 +1510,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsLink)
             item?.target = self
             item?.btnTag = CAnnotationType.link.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Select an area or text on a page for a link to be inserted.", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarSignSignatureAnnotationItemIdentifier {
@@ -1490,7 +1520,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsSign)
             item?.target = self
             item?.btnTag = CAnnotationType.signSignature.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Add beautiful handwriting or use your trackpad to create your own signature.", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMAnnotationStampToolbarItemIdentifier {
@@ -1498,7 +1530,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsStamp)
             item?.target = self
             item?.btnTag = CAnnotationType.stamp.rawValue
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Add New Stamp", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(changeAnnotationMode)
         } else if identifier == KMToolbarShowToolbarItemIdentifier {
@@ -1507,14 +1541,18 @@ extension KMToolbarViewController {
             item?.alternateImage = NSImage(named: "KMImageNameMainToolShow")
             item?.selectedBackgroundColor = NSColor.clear
             item?.target = self
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Hide", comment: "")
+            item?.isPopToolTip = true
             item?.titleName = NSLocalizedString("Hide", comment: "")
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(showPDFLayoutMode)
         } else if identifier == KMToolbarAnnotationSettingItemIdentifier {
             item?.image = NSImage(named: "KMImageNameUXIconBtnSetNor")
             item?.target = self
+            item?.isShowCustomToolTip = true
             item?.toolTip = String(format: "%@\n%@", NSLocalizedString("Customize Toolbar", comment: ""),NSLocalizedString("Drag-and-drop tools to change their order", comment: ""))
+            item?.isPopToolTip = true
             item?.titleName = NSLocalizedString("Setting", comment: "")
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(_itemAction)
@@ -1523,7 +1561,9 @@ extension KMToolbarViewController {
             item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsImage)
             item?.target = self
             item?.titleName = NSLocalizedString("Image", comment: "")
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Choose an image to insert in a page.", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(_itemAction)
         } else if identifier == KMAnnotationTableToolbarItemIdentifier {
@@ -1531,7 +1571,9 @@ extension KMToolbarViewController {
             item?.target = self
             item?.btnTag = CAnnotationType.table.rawValue
             item?.titleName = NSLocalizedString("Table", comment: "")
+            item?.isShowCustomToolTip = true
             item?.toolTip = NSLocalizedString("Table", comment: "")
+            item?.isPopToolTip = true
             item?.boxImagePosition = .imageOnly
             item?.btnAction = #selector(_itemAction)
         }