Browse Source

【2025】【组件库】弹窗位置计算处理

niehaoyu 1 month ago
parent
commit
524083a534

+ 35 - 27
PDF Office/KMComponentLibrary/KMComponentLibrary/View/GroupItem/ComponentGroup.swift

@@ -156,7 +156,7 @@ public class ComponentGroup: NSView, NibLoadable {
     }
     
     //MARK: - Show
-    
+    //Point是相对于当前WIndow的point, 
     public func showWithPoint(_ point: CGPoint, relativeTo positioningView: NSView?) {
         
         var screenRect = NSScreen.main!.frame
@@ -175,37 +175,45 @@ public class ComponentGroup: NSView, NibLoadable {
         windowVC.window?.backgroundColor = NSColor.clear
         windowVC.showWindow(nil)
         
+        var rect = self.frame
         if let subView = positioningView {
-            var rect = self.frame
-            if true {
-                let subviewRect = subView.bounds // NSView的坐标
-                let subviewWindow = subView.window
-                let subviewWindowRect = subView.convert(subviewRect, to: nil) // 转换到窗口坐标
-                let subviewscreenRect = subviewWindow?.convertToScreen(subviewWindowRect) ?? CGRectZero // 转换到屏幕坐标 ?
-                
-                rect.origin.x = subviewscreenRect.origin.x - screenRect.origin.x
-                rect.origin.y = subviewscreenRect.origin.y - screenRect.origin.y
-                rect.origin.y -= rect.size.height
-            }
-            var viewWidth: CGFloat = CGRectGetWidth(self.frame)
-            for item in menuItemArr {
-                ComponentLibrary.shared.configMenuItemComponent(properties: item)
-                viewWidth = max(viewWidth, item.propertyInfo.viewWidth)
-            }
-            rect.size.width = viewWidth
-            if(rect.origin.y < 0) {
-                rect.origin.y = 0
-            }
-            self.frame = rect
             
-            if let window = windowVC.window {
-                window.contentView?.addSubview(self)
-            }
+            let subviewRect = subView.bounds // NSView的坐标
+            let subviewWindow = subView.window
+            let subviewWindowRect = subView.convert(subviewRect, to: nil) // 转换到窗口坐标
+            let subviewscreenRect = subviewWindow?.convertToScreen(subviewWindowRect) ?? CGRectZero // 转换到屏幕坐标 ?
             
-            clearMonitor()
+            rect.origin.x = subviewscreenRect.origin.x - screenRect.origin.x
+            rect.origin.y = subviewscreenRect.origin.y - screenRect.origin.y
+            rect.origin.y -= rect.size.height
             
-            addMonitor()
+        } else {
+            var editPoint = point
+            if let keyWindow = NSApplication.shared.keyWindow, let screen = keyWindow.screen {
+                editPoint.x += keyWindow.frame.origin.x
+                editPoint.y += keyWindow.frame.origin.y
+            }
+            rect.origin.x = editPoint.x
+            rect.origin.y = editPoint.y
         }
+        var viewWidth: CGFloat = CGRectGetWidth(self.frame)
+        for item in menuItemArr {
+            ComponentLibrary.shared.configMenuItemComponent(properties: item)
+            viewWidth = max(viewWidth, item.propertyInfo.viewWidth)
+        }
+        rect.size.width = viewWidth
+        if(rect.origin.y < 0) {
+            rect.origin.y = 0
+        }
+        self.frame = rect
+        
+        if let window = windowVC.window {
+            window.contentView?.addSubview(self)
+        }
+        
+        clearMonitor()
+        
+        addMonitor()
         
     }
     

+ 7 - 7
PDF Office/PDF Master/KMClass/KMHomeViewController/Views/KMHomeRightView/KMHomeRightView.swift

@@ -142,7 +142,7 @@ public class KMHomeRightView: BaseXibView {
     }
     
     //显示右键菜单
-    func showFileMoreActionMenu(point: CGPoint) {
+    func showFileMoreActionMenu(point: CGPoint, _ actionView: NSView? = nil) {
         var viewHeight: CGFloat = 8
         var menuItemArr: [ComponentMenuitemProperty] = []
          
@@ -186,9 +186,9 @@ public class KMHomeRightView: BaseXibView {
         groupView?.frame = CGRectMake(0, 0, 180, viewHeight)
         groupView.updateGroupInfo(menuItemArr)
         if HistoryFilesManager.manager.showMode == .Thumbnail {
-            groupView.showWithPoint(CGPoint(x: point.x + 60 + CGRectGetWidth(groupView.frame), y: point.y - viewHeight), relativeTo: groupActionView)
+            groupView.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: actionView)
         } else {
-            groupView.showWithPoint(CGPoint(x: point.x + 40 + CGRectGetWidth(groupView.frame), y: point.y - viewHeight), relativeTo: groupActionView)
+            groupView.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: actionView)
         }
         
         menuActionIndexPaths = collectionView.selectionIndexPaths
@@ -277,7 +277,7 @@ public class KMHomeRightView: BaseXibView {
             }
             groupActionView = collectionView.item(at: indexPath)?.view
             groupViewPoint = point
-            showFileMoreActionMenu(point: point)
+            showFileMoreActionMenu(point: event.locationInWindow)
         }
         collectionViewSelectedChanged()
     }
@@ -498,7 +498,7 @@ extension KMHomeRightView: ComponentGroupDelegate {
 
 //MARK: - KMHistoryFileListItemDelegate
 extension KMHomeRightView: KMHistoryFileListItemDelegate {
-    public   func historyFileListItemDidClickedMore(_ view: KMHistoryFileListItem) {
+    public func historyFileListItemDidClickedMore(_ view: KMHistoryFileListItem) {
         if let indexPath = collectionView.indexPath(for: view) {
             if collectionView.selectionIndexPaths.contains(indexPath) {
                 
@@ -509,8 +509,8 @@ extension KMHomeRightView: KMHistoryFileListItemDelegate {
         }
         groupActionView = view.moreButton
 
-        var point = view.moreButton.superview?.convert(view.moreButton.frame.origin, to: self) ?? CGPointZero
-        showFileMoreActionMenu(point: point)
+        let point = view.moreButton.superview?.convert(view.moreButton.frame.origin, to: self) ?? CGPointZero
+        showFileMoreActionMenu(point: point, groupActionView)
     }
     
     public func historyFileListItemDidDoubleClicked(_ view: KMHistoryFileListItem) {