Jelajahi Sumber

【PDFListView】多选注释效果优化

dinglingui 1 tahun lalu
induk
melakukan
0b71320e42

+ 21 - 7
PDF Office/PDF Office/Class/PDFWindowController/PDFListView/CPDFListView.m

@@ -84,6 +84,16 @@ NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPD
     return self;
 }
 
+- (void)updateLayer {
+    [super updateLayer];
+    if (@available(macOS 10.14, *)) {
+        if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewBackgroundColor)]) {
+            NSColor *color = [self.pdfListViewDelegate PDFListViewBackgroundColor];
+            self.backgroundColor = color;
+        }
+    }
+}
+
 - (void)commonInitialization {
     self.selectionPageIndex = NSNotFound;
     self.clickLineAnnotation = nil;
@@ -760,15 +770,19 @@ NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPD
     
     [self drawSelectionForPage:pdfPage inContext:context];
     
-    if(self.activeAnnotations.count > 1 && [self.activeAnnotation.page isEqual:pdfPage]) {
-        BOOL isIncludText = NO;
-        for (CPDFAnnotation *an in self.activeAnnotations) {
-            if([an isKindOfClass:[CPDFFreeTextAnnotation class]]) {
+    BOOL isIncludText = NO;
+    NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
+    for (CPDFAnnotation *an in self.activeAnnotations) {
+        if([an.page isEqual:pdfPage]) {
+            if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
                 isIncludText = YES;
-                break;
-            }
+            
+            [currentActiveAnnotations addObject:an];
         }
-        CGRect selectAnnotationBounds = [self selectionMultipleAnnotationBoundsWithPage:pdfPage];
+    }
+    
+    if(currentActiveAnnotations.count > 1) {
+        CGRect selectAnnotationBounds = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
         
         NSRect rect = selectAnnotationBounds;
 

+ 45 - 32
PDF Office/PDF Office/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Event.m

@@ -244,6 +244,17 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
     
     if(!self.isEditing) {
         [self.dragHoverPoints removeAllObjects];
+        
+        BOOL isIncludeText = NO;
+        NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
+        for (CPDFAnnotation *an in self.activeAnnotations) {
+            if([an.page isEqual:page]) {
+                if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
+                    isIncludeText = YES;
+                
+                [currentActiveAnnotations addObject:an];
+            }
+        }
 
         if(CNoteToolMode == self.toolMode ||
            CTextToolMode == self.toolMode ||
@@ -251,19 +262,9 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
            CSelfSignMode == self.toolMode ||
            CRedactToolMode == self.toolMode ||
            CRedactErasureToolMode == self.toolMode) {
-            if(self.activeAnnotations.count > 1) {
-                NSRect rect = [self selectionMultipleAnnotationBoundsWithPage:page];
+            if(currentActiveAnnotations.count > 1) {
+                NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
                 CRectEdges resizeHandle = CPDFListViewResizeHandleForPointFromRect(point, rect, 4.0 / [self scaleFactor]);
-                BOOL isIncludeText = NO;
-
-                if(self.activeAnnotations.count > 1) {
-                    for (CPDFAnnotation *an in self.activeAnnotations) {
-                        if([an isKindOfClass:[CPDFFreeTextAnnotation class]]) {
-                            isIncludeText = YES;
-                            break;
-                        }
-                    }
-                }
                 
                 if (isIncludeText) {
                     if (((resizeHandle & CMaxXEdgeMask) && (resizeHandle & CMinYEdgeMask)) ||
@@ -2257,9 +2258,16 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
     }
 }
 
-- (void)doMutResizeDragOffsetPoint:(CGPoint)relPoint resizeHandle:(CRectEdges *)resizeHandlePtr {
-    CPDFPage *page = self.activeAnnotation.page;
-    CGRect orgBounds = [self selectionMultipleAnnotationBoundsWithPage:page];
+- (void)doMutResizeDragOffsetPoint:(CGPoint)relPoint page:(CPDFPage *)page resizeHandle:(CRectEdges *)resizeHandlePtr {
+    
+    NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
+    for (CPDFAnnotation *an in self.activeAnnotations) {
+        if([an.page isEqual:page]) {
+            [currentActiveAnnotations addObject:an];
+        }
+    }
+
+    CGRect orgBounds = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
     NSRect newBounds = orgBounds;
 
     CRectEdges resizeHandle = *resizeHandlePtr;
@@ -2438,8 +2446,15 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
         return YES;
     }
     
-    if(self.activeAnnotations.count > 1) {
-        NSRect rect = [self selectionMultipleAnnotationBoundsWithPage:page];
+    NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
+    for (CPDFAnnotation *an in self.activeAnnotations) {
+        if([an.page isEqual:page]) {
+            [currentActiveAnnotations addObject:an];
+        }
+    }
+    
+    if(currentActiveAnnotations.count > 1) {
+        NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
         CRectEdges resizeHandle = CPDFListViewResizeHandleForPointFromRect(point, rect, 4.0 / [self scaleFactor]);
         if(resizeHandle != 0) {
             *annotation = self.activeAnnotation;
@@ -2505,24 +2520,22 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
 - (void)doDragMultipleAnnotationWithEvent:(NSEvent *)theEvent {
     NSPoint pagePoint = NSZeroPoint;
     CPDFPage *page = [self pageAndPoint:&pagePoint forEvent:theEvent nearest:YES];
+    NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
+    BOOL isIncludText = NO;
+    for (CPDFAnnotation *an in self.activeAnnotations) {
+        if([an.page isEqual:page]) {
+            [currentActiveAnnotations addObject:an];
+            if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
+                isIncludText = YES;
+        }
+    }
     
     NSUInteger eventMask = NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
     NSEvent *lastMouseEvent = theEvent;
-    NSRect rect = [self selectionMultipleAnnotationBoundsWithPage:page];
+    NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
     CRectEdges resizeHandle = CPDFListViewResizeHandleForPointFromRect(pagePoint, rect, 4.0 / [self scaleFactor]);
     
-    BOOL isIncludeText = NO;
-
-    if(self.activeAnnotations.count > 1) {
-        for (CPDFAnnotation *an in self.activeAnnotations) {
-            if([an isKindOfClass:[CPDFFreeTextAnnotation class]]) {
-                isIncludeText = YES;
-                break;
-            }
-        }
-    }
-    
-    if (isIncludeText) {
+    if (isIncludText) {
         if (((resizeHandle & CMaxXEdgeMask) && (resizeHandle & CMinYEdgeMask)) ||
             ((resizeHandle & CMinXEdgeMask) && (resizeHandle & CMaxYEdgeMask)) ||
             ((resizeHandle & CMaxXEdgeMask) && (resizeHandle & CMaxYEdgeMask)) ||
@@ -2557,7 +2570,7 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
             if (CGRectContainsPoint(zRect, point)) {continue;}
 
             [self.dragHoverPoints removeAllObjects];
-            CGRect borderRect = [self selectionMultipleAnnotationBoundsWithPage:page];
+            CGRect borderRect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
             [self.dragHoverPoints addObjectsFromArray:[self dragHoverPointsForRect:borderRect forPage:page resizeHandle:resizeHandle]];
             NSPoint relPoint = CPDFListViewSubstractPoints(point, lastPoint);
             if(CGRectGetMinX(borderRect) + relPoint.x < 0) {
@@ -2590,7 +2603,7 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
                 
                 lastMouseEvent = theEvent;
             } else {
-                [self doMutResizeDragOffsetPoint:relPoint resizeHandle:&resizeHandle];;
+                [self doMutResizeDragOffsetPoint:relPoint page:page resizeHandle:&resizeHandle];
 
                 lastMouseEvent = theEvent;
             }

+ 1 - 1
PDF Office/PDF Office/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Tool.h

@@ -49,7 +49,7 @@
 
 - (BOOL)isEditingAnnotation:(CPDFAnnotation *)annotation;
 
-- (CGRect)selectionMultipleAnnotationBoundsWithPage:(CPDFPage *)pdfPage;
+- (CGRect)selectionMultipleBoundsWithAnnotations:(NSArray<CPDFAnnotation *> *)annotations;
 
 - (NSMutableArray *)dragHoverPointsForRect:(CGRect)borderRect forPage:(CPDFPage*)page resizeHandle:(CRectEdges)resizeHandle;
 

+ 4 - 5
PDF Office/PDF Office/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Tool.m

@@ -297,11 +297,10 @@
         return NO;
 }
 
-- (CGRect)selectionMultipleAnnotationBoundsWithPage:(CPDFPage *)pdfPage {
-    CGRect selectAnnotationBounds = self.activeAnnotations.firstObject.bounds;
-    for (CPDFAnnotation *annotation in self.activeAnnotations) {
-        if (annotation.page && [annotation.page isEqual:pdfPage])
-            selectAnnotationBounds = CPDFListViewIntersectionAndSetRect(annotation.bounds, selectAnnotationBounds);
+- (CGRect)selectionMultipleBoundsWithAnnotations:(NSArray <CPDFAnnotation *>*)annotations {
+    CGRect selectAnnotationBounds = annotations.firstObject.bounds;
+    for (CPDFAnnotation *annotation in annotations) {
+        selectAnnotationBounds = CPDFListViewIntersectionAndSetRect(annotation.bounds, selectAnnotationBounds);
     }
     
     NSNumber *offsetNum = [CPDFListViewConfig defaultManager].annotationBorderOffset;

+ 2 - 2
PDF Office/PDF Office/Class/PDFWindowController/Side/LeftSide/Annotation/KMAnnotationViewController.swift

@@ -428,7 +428,7 @@ extension KMAnnotationViewController: NSTableViewDelegate,NSTableViewDataSource
                 }
             }
             self.listView.updateActiveAnnotations(newAnnonations)
-            self.listView.setNeedsDisplayForVisiblePages()
+            self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
         } else {
             let selectedRow = self.tableView.selectedRow
             if selectedRow >= 0 && selectedRow < self.annotations.count {
@@ -437,7 +437,7 @@ extension KMAnnotationViewController: NSTableViewDelegate,NSTableViewDataSource
                     self.listView.go(to: (annotation as! CPDFAnnotation).bounds, on: (annotation as! CPDFAnnotation).page, animated: true)
                     
                     self.listView.updateActiveAnnotations([annotation as! CPDFAnnotation])
-                    self.listView.setNeedsDisplayForVisiblePages()
+                    self.listView.setNeedsDisplayAnnotationViewForVisiblePages()
                 }
             }
         }