Revision: 15218
          http://sourceforge.net/p/skim-app/code/15218
Author:   hofman
Date:     2025-05-16 16:34:35 +0000 (Fri, 16 May 2025)
Log Message:
-----------
Handle nil page separately in setter. Rename a method to avoid confusion. 
Modify layout calculation of drawn page.

Modified Paths:
--------------
    trunk/SKPresentationView.m

Modified: trunk/SKPresentationView.m
===================================================================
--- trunk/SKPresentationView.m  2025-05-16 14:40:23 UTC (rev 15217)
+++ trunk/SKPresentationView.m  2025-05-16 16:34:35 UTC (rev 15218)
@@ -80,7 +80,7 @@
 static NSInteger navigationMode = SKNavigationBottom;
 
 @interface SKPDFPageView ()
-- (void)displayPage:(void (^)(void))completionHandler;
+- (void)displayCurrentPage:(void (^)(void))completionHandler;
 @end
 
 @implementation SKPDFPageView
@@ -147,7 +147,7 @@
     if (fabs([pageLayer contentsScale] - scale) > 0.0) {
         [pageLayer setContentsScale:scale];
         [self removePredrawnImageAtIndex:NSNotFound];
-        [self displayPage:nil];
+        [self displayCurrentPage:nil];
     }
 }
 
@@ -172,14 +172,7 @@
 
 - (void)displayPage:(PDFPage *)newPage completionHandler:(void 
(^)(void))completionHandler {
     page = newPage;
-    if (page) {
-        [self displayPage:completionHandler];
-    } else {
-        [self removePredrawnImageAtIndex:NSNotFound];
-        [pageLayer setContents:nil];
-        if (completionHandler)
-            completionHandler();
-    }
+    [self displayCurrentPage:completionHandler];
     [[NSNotificationCenter defaultCenter] 
postNotificationName:SKPresentationViewPageChangedNotification object:self];
 }
 
@@ -230,8 +223,16 @@
 #pragma mark Accessors
 
 - (void)setPage:(PDFPage *)newPage {
-    if (newPage != page)
-        [self displayPage:newPage completionHandler:nil];
+    if (newPage != page) {
+        if (newPage) {
+            [self displayPage:newPage completionHandler:nil];
+        } else {
+            page = nil;
+            [self removePredrawnImageAtIndex:NSNotFound];
+            [pageLayer setContents:nil];
+            // nothing needs the notification when set to nil
+        }
+    }
 }
 
 - (BOOL)autoScales { return YES; }
@@ -295,12 +296,19 @@
 - (void)removePredrawnImageAtIndex:(NSUInteger)pageIndex {}
 
 - (NSImage *)imageWithImageRep:(NSBitmapImageRep *)imageRep page:(PDFPage 
*)aPage autoScales:(BOOL)autoScales {
-    NSRect bounds = {NSZeroPoint, [imageRep size]};
+    NSSize size = [imageRep size];
     NSRect pageRect = [aPage boundsForBox:kPDFDisplayBoxCropBox];
     if (([page rotation] % 180) != 0)
         pageRect = NSMakeRect(0.0, 0.0, NSHeight(pageRect), NSWidth(pageRect));
-    CGFloat scale = autoScales ? fmin(NSHeight(bounds) / NSHeight(pageRect), 
NSWidth(bounds) / NSWidth(pageRect)) : 1.0;
-    pageRect = NSInsetRect(bounds, 0.5 * (NSWidth(bounds) - scale * 
NSWidth(pageRect)), 0.5 * (NSHeight(bounds) - scale * NSHeight(pageRect)));
+    CGFloat scale = 1.0;
+    if (autoScales) {
+        scale = fmin(size.height / NSHeight(pageRect), size.width / 
NSWidth(pageRect));
+        pageRect.size.width *= scale;
+        pageRect.size.height *= scale;
+    }
+    pageRect.origin.x = 0.5 * (size.width - NSWidth(pageRect));
+    pageRect.origin.y = 0.5 * (size.height - NSHeight(pageRect));
+    
     CGContextRef context = [[NSGraphicsContext 
graphicsContextWithBitmapImageRep:imageRep] CGContext];
     
     CGContextSaveGState(context);
@@ -314,15 +322,18 @@
     [aPage drawWithBox:kPDFDisplayBoxCropBox toContext:context];
     CGContextRestoreGState(context);
     
-    NSImage *image = [[NSImage alloc] initWithSize:bounds.size];
+    NSImage *image = [[NSImage alloc] initWithSize:size];
     [image addRepresentation:imageRep];
     
     return image;
 }
 
-- (void)displayPage:(void (^)(void))completionHandler {
-    if (page == nil)
+- (void)displayCurrentPage:(void (^)(void))completionHandler {
+    if (page == nil) {
+        if (completionHandler)
+            completionHandler();
         return;
+    }
     
     NSUInteger pageIndex = [page pageIndex];
     NSImage *predrawnImage = [self predrawnImageAtIndex:pageIndex];
@@ -430,14 +441,14 @@
 - (void)viewDidEndLiveResize {
     [super viewDidEndLiveResize];
     [self removePredrawnImageAtIndex:NSNotFound];
-    [self displayPage:nil];
+    [self displayCurrentPage:nil];
 }
 
 - (void)updatedAnnotationOnPage:(PDFPage *)aPage {
     if (page == aPage) {
         [self removePredrawnImageAtIndex:[aPage pageIndex]];
-        [[self class] cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(displayPage:) object:nil];
-        [self performSelector:@selector(displayPage:) withObject:nil 
afterDelay:0.0];
+        [[self class] cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(displayCurrentPage:) object:nil];
+        [self performSelector:@selector(displayCurrentPage:) withObject:nil 
afterDelay:0.0];
     }
 }
 
@@ -457,12 +468,12 @@
         NSMapRemove(predrawnImages, (void *)pageIndex);
 }
 
-- (void)displayPage:(void (^)(void))completionHandler {
+- (void)displayCurrentPage:(void (^)(void))completionHandler {
+    [super displayCurrentPage:completionHandler];
+    
     if (page == nil)
         return;
     
-    [super displayPage:completionHandler];
-    
     // generate an image for the next page in the background, which is usually 
needed next for a presentation
     
     NSUInteger pageIndex = [page pageIndex] + 1;
@@ -526,7 +537,7 @@
         pvFlags.autoScales = flag;
         [pageLayer setContentsGravity:flag ? kCAGravityResizeAspectFill : 
kCAGravityCenter];
         [self removePredrawnImageAtIndex:NSNotFound];
-        [self displayPage:nil];
+        [self displayCurrentPage:nil];
         [[NSNotificationCenter defaultCenter] 
postNotificationName:SKPresentationViewAutoScalesChangedNotification 
object:self];
     }
 }
@@ -933,8 +944,8 @@
             [annotation setColor:tmpColor];
         [[page document] addAnnotation:annotation toPage:page];
         
-        [[self class] cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(displayPage:) object:nil];
-        [self displayPage:^{
+        [[self class] cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(displayCurrentPage:) object:nil];
+        [self displayCurrentPage:^{
             [layer removeFromSuperlayer];
         }];
     } else {

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