Revision: 15228
          http://sourceforge.net/p/skim-app/code/15228
Author:   hofman
Date:     2025-05-18 16:17:27 +0000 (Sun, 18 May 2025)
Log Message:
-----------
Cancel predrawn image block when removing

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

Modified: trunk/SKPresentationView.m
===================================================================
--- trunk/SKPresentationView.m  2025-05-18 15:54:37 UTC (rev 15227)
+++ trunk/SKPresentationView.m  2025-05-18 16:17:27 UTC (rev 15228)
@@ -481,11 +481,26 @@
     return nil;
 }
 
+static inline BOOL imageOrBlockIsBlock(id imageOrBlock) {
+    return imageOrBlock && [imageOrBlock isKindOfClass:[NSImage class]] == NO;
+}
+
+static inline void cancelIfBlock(id imageOrBlock) {
+    if (imageOrBlockIsBlock(imageOrBlock))
+        dispatch_block_cancel((dispatch_block_t)imageOrBlock);
+}
+
 - (void)removePredrawnImageAtIndex:(NSUInteger)pageIndex {
-    if (pageIndex == NSNotFound)
-        predrawnImages = nil;
-    else if (predrawnImages)
-        NSMapRemove(predrawnImages, (void *)pageIndex);
+    if (predrawnImages) {
+        if (pageIndex == NSNotFound) {
+            for (id imageOrBlock in NSAllMapTableValues(predrawnImages))
+                cancelIfBlock(imageOrBlock);
+            predrawnImages = nil;
+        } else {
+            cancelIfBlock((__bridge id)NSMapGet(predrawnImages, (void 
*)pageIndex));
+            NSMapRemove(predrawnImages, (void *)pageIndex);
+        }
+    }
 }
 
 - (void)displayCurrentPage:(void (^)(void))completionHandler {
@@ -506,9 +521,6 @@
     else if (NSMapGet(predrawnImages, (void *)pageIndex))
         return;
     
-    // set NSNull so we can invalidate this image
-    NSMapInsert(predrawnImages, (void *)pageIndex, (__bridge void *)[NSNull 
null]);
-    
     static dispatch_queue_t predrawingQueue = nil;
     if (predrawingQueue == nil) {
         dispatch_queue_attr_t queuePriority = 
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, 
QOS_CLASS_BACKGROUND, 0);
@@ -523,7 +535,7 @@
     PDFPage *thePage = [[page document] pageAtIndex:pageIndex];
     BOOL autoScales = [self autoScales];
     
-    dispatch_async(predrawingQueue, ^{
+    dispatch_block_t block = dispatch_block_create(0, ^{
         
         NSImage *image = imageByDrawingPageToBitmapImageRep(imageRep, thePage, 
autoScales);
         
@@ -530,7 +542,7 @@
         dispatch_async(dispatch_get_main_queue(), ^{
             
             if (predrawnImages) {
-                if ((__bridge id)NSMapGet(predrawnImages, (void *)pageIndex) 
== [NSNull null]) {
+                if (imageOrBlockIsBlock((__bridge id)NSMapGet(predrawnImages, 
(void *)pageIndex))) {
                     NSMapRemove(predrawnImages, (void *)pageIndex);
                     if (image) {
                         NSUInteger currentIndex = [page pageIndex];
@@ -546,6 +558,11 @@
         
     });
     
+    // set this block so we can cancel it
+    NSMapInsert(predrawnImages, (void *)pageIndex, (__bridge void *)block);
+    
+    dispatch_async(predrawingQueue, block);
+    
 }
 
 #pragma mark Accessors

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