Revision: 12672
          http://sourceforge.net/p/skim-app/code/12672
Author:   hofman
Date:     2022-01-02 10:28:14 +0000 (Sun, 02 Jan 2022)
Log Message:
-----------
Draw drag highlight in a shape layer. Separate method to prepare a shape layer 
for drag highlight or rect selection.

Modified Paths:
--------------
    trunk/SKPDFView.h
    trunk/SKPDFView.m

Modified: trunk/SKPDFView.h
===================================================================
--- trunk/SKPDFView.h   2022-01-01 22:41:47 UTC (rev 12671)
+++ trunk/SKPDFView.h   2022-01-02 10:28:14 UTC (rev 12672)
@@ -144,6 +144,8 @@
     
     SKSyncDot *syncDot;
     
+    CAShapeLayer *rectLayer;
+    
     NSWindow *loupeWindow;
     NSInteger loupeLevel;
     CGFloat magnification;

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2022-01-01 22:41:47 UTC (rev 12671)
+++ trunk/SKPDFView.m   2022-01-02 10:28:14 UTC (rev 12672)
@@ -196,7 +196,6 @@
 @interface SKPDFView ()
 @property (retain) SKReadingBar *readingBar;
 @property (retain) SKSyncDot *syncDot;
-@property (retain) PDFAnnotation *highlightAnnotation;
 @end
 
 @interface SKPDFView (Private)
@@ -254,7 +253,7 @@
 
 @implementation SKPDFView
 
-@synthesize toolMode, annotationMode, temporaryToolMode, interactionMode, 
activeAnnotation, readingBar, pacerSpeed, transitionController, 
typeSelectHelper, syncDot, highlightAnnotation;
+@synthesize toolMode, annotationMode, temporaryToolMode, interactionMode, 
activeAnnotation, readingBar, pacerSpeed, transitionController, 
typeSelectHelper, syncDot;
 @synthesize currentMagnification=magnification, zooming;
 @dynamic extendedDisplayMode, displaysHorizontally, displaysRightToLeft, 
hideNotes, hasReadingBar, hasPacer, currentSelectionPage, currentSelectionRect, 
needsRewind, editing;
 
@@ -366,6 +365,7 @@
     SKDESTROY(readingBar);
     SKDESTROY(editor);
     SKDESTROY(highlightAnnotation);
+    SKDESTROY(rectLayer);
     SKDESTROY(rewindPage);
     [super dealloc];
 }
@@ -478,19 +478,6 @@
     }
 }
 
-- (void)drawDragHighlightInContext:(CGContextRef)context {
-    PDFAnnotation *annotation = [self highlightAnnotation];
-    if (annotation) {
-        PDFPage *page = [annotation page];
-        CGFloat width = [self unitWidthOnPage:page];
-        CGContextSaveGState(context);
-        CGContextSetStrokeColorWithColor(context, 
CGColorGetConstantColor(kCGColorBlack));
-        NSRect rect = [self backingAlignedRect:[annotation bounds] 
onPage:page];
-        CGContextStrokeRectWithWidth(context, 
CGRectInset(NSRectToCGRect(rect), 0.5 * width, 0.5 * width), width);
-        CGContextRestoreGState(context);
-    }
-}
-
 - (void)drawPageHighlights:(PDFPage *)pdfPage toContext:(CGContextRef)context {
     CGContextSaveGState(context);
     
@@ -510,8 +497,6 @@
     
     [self drawSelectionForPage:pdfPage inContext:context];
     
-    [self drawDragHighlightInContext:context];
-    
     SKSyncDot *aSyncDot = [self syncDot];
     if ([[aSyncDot page] isEqual:pdfPage])
         [aSyncDot drawInContext:context];
@@ -536,6 +521,24 @@
     }
 }
 
+- (void)makeRectLayer {
+    if (rectLayer) {
+        [rectLayer removeFromSuperlayer];
+        [rectLayer release];
+    }
+    rectLayer = [[CAShapeLayer alloc] init];
+    [rectLayer setStrokeColor:CGColorGetConstantColor(kCGColorBlack)];
+    [rectLayer setFillColor:NULL];
+    [rectLayer setLineWidth:1.0];
+    [rectLayer setFrame:NSRectToCGRect([self visibleContentRect])];
+    [rectLayer setBounds:[rectLayer frame]];
+    [rectLayer setMasksToBounds:YES];
+    [rectLayer setZPosition:1.0];
+    [rectLayer setContentsScale:[[self layer] contentsScale]];
+    [rectLayer setFilters:SKColorEffectFilters()];
+    [[self layer] addSublayer:rectLayer];
+}
+
 #pragma mark Accessors
 
 - (void)setDocument:(PDFDocument *)document {
@@ -958,6 +961,25 @@
     return transitionController;
 }
 
+- (void)setHighlightAnnotation:(PDFAnnotation *)annotation {
+    if (annotation != highlightAnnotation) {
+        [highlightAnnotation release];
+        highlightAnnotation = [annotation retain];
+        if (highlightAnnotation) {
+            if (rectLayer == nil)
+                [self makeRectLayer];
+            
+            NSRect rect = [self backingAlignedRect:[self 
convertRect:[highlightAnnotation bounds] fromPage:[highlightAnnotation page]] 
options:NSAlignAllEdgesOutward];
+            CGPathRef path = 
CGPathCreateWithRect(CGRectInset(NSRectToCGRect(rect), -0.5, -0.5), NULL);
+            [rectLayer setPath:path];
+            CGPathRelease(path);
+        } else if (rectLayer) {
+            [rectLayer removeFromSuperlayer];
+            SKDESTROY(rectLayer);
+        }
+    }
+}
+
 #pragma mark Reading bar
 
 - (BOOL)hasReadingBar {
@@ -2231,21 +2253,14 @@
                 annotation = [annotations objectAtIndex:i];
                 if ([annotation isSkimNote] && [annotation hitTest:location] &&
                     ([pboard canReadItemWithDataConformingToTypes:[NSArray 
arrayWithObjects:NSPasteboardTypeColor, nil]] || [annotation hasBorder])) {
-                    if ([annotation isEqual:highlightAnnotation] == NO) {
-                        if (highlightAnnotation)
-                            [self 
setNeedsDisplayForAnnotation:highlightAnnotation];
-                        [self setHighlightAnnotation:annotation];
-                        [self 
setNeedsDisplayForAnnotation:highlightAnnotation];
-                    }
+                    [self setHighlightAnnotation:annotation];
                     dragOp = NSDragOperationGeneric;
                     break;
                 }
             }
         }
-        if (dragOp == NSDragOperationNone && highlightAnnotation) {
-            [self setNeedsDisplayForAnnotation:highlightAnnotation];
+        if (dragOp == NSDragOperationNone)
             [self setHighlightAnnotation:nil];
-        }
     } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
         dragOp = [super draggingUpdated:sender];
     }
@@ -2254,14 +2269,10 @@
 
 - (void)draggingExited:(id <NSDraggingInfo>)sender {
     NSPasteboard *pboard = [sender draggingPasteboard];
-    if ([pboard canReadItemWithDataConformingToTypes:[NSArray 
arrayWithObjects:NSPasteboardTypeColor, SKPasteboardTypeLineStyle, nil]]) {
-        if (highlightAnnotation) {
-            [self setNeedsDisplayForAnnotation:highlightAnnotation];
-            [self setHighlightAnnotation:nil];
-        }
-    } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
+    if ([pboard canReadItemWithDataConformingToTypes:[NSArray 
arrayWithObjects:NSPasteboardTypeColor, SKPasteboardTypeLineStyle, nil]])
+        [self setHighlightAnnotation:nil];
+    else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd])
         [super draggingExited:sender];
-    }
 }
 
 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
@@ -2291,7 +2302,6 @@
                 }
                 performedDrag = YES;
             }
-            [self setNeedsDisplayForAnnotation:highlightAnnotation];
             [self setHighlightAnnotation:nil];
         }
     } else if ([[SKPDFView superclass] instancesRespondToSelector:_cmd]) {
@@ -4627,22 +4637,10 @@
     NSPoint currentPoint;
     NSRect selRect = {startPoint, NSZeroSize};
     BOOL dragged = NO;
-    CAShapeLayer *layer = nil;
     NSWindow *window = [self window];
-    CGRect layerRect = NSRectToCGRect([self visibleContentRect]);
-    layer = [CAShapeLayer layer];
-    [layer setStrokeColor:CGColorGetConstantColor(kCGColorBlack)];
-    [layer setFillColor:NULL];
-    [layer setLineWidth:1.0];
-    [layer setFrame:layerRect];
-    [layer setBounds:layerRect];
-    [layer setMasksToBounds:YES];
-    [layer setZPosition:1.0];
     
-    [layer setContentsScale:[[self layer] contentsScale]];
-    [[self layer] addSublayer:layer];
-    [layer setFilters:SKColorEffectFilters()];
-        
+    [self makeRectLayer];
+    
     while (YES) {
         theEvent = [window nextEventMatchingMask: NSLeftMouseUpMask | 
NSLeftMouseDraggedMask | NSFlagsChangedMask];
         
@@ -4670,11 +4668,12 @@
         selRect = SKIntersectionRect(selRect, [[self documentView] bounds]);
         
         CGPathRef path = 
CGPathCreateWithRect(NSRectToCGRect(NSInsetRect(NSIntegralRect([self 
convertRect:selRect fromView:[self documentView]]), 0.5, 0.5)), NULL);
-        [layer setPath:path];
+        [rectLayer setPath:path];
         CGPathRelease(path);
     }
     
-    [layer removeFromSuperlayer];
+    [rectLayer removeFromSuperlayer];
+    SKDESTROY(rectLayer);
 
     [self setCursorForMouse:theEvent];
     

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to