Revision: 14018
          http://sourceforge.net/p/skim-app/code/14018
Author:   hofman
Date:     2024-01-26 00:28:55 +0000 (Fri, 26 Jan 2024)
Log Message:
-----------
Use blocks forr timers with weak references to self to avoid a retain loop

Modified Paths:
--------------
    trunk/SKFileUpdateChecker.m
    trunk/SKMainWindowController.h
    trunk/SKMainWindowController.m
    trunk/SKPDFView.m
    trunk/SKSyncDot.m
    trunk/SKTypeSelectHelper.m

Modified: trunk/SKFileUpdateChecker.m
===================================================================
--- trunk/SKFileUpdateChecker.m 2024-01-20 22:23:42 UTC (rev 14017)
+++ trunk/SKFileUpdateChecker.m 2024-01-26 00:28:55 UTC (rev 14018)
@@ -111,7 +111,7 @@
     }
 }
 
-- (void)checkForFileReplacement:(NSTimer *)timer {
+- (void)checkForFileReplacement {
     if ([[[document fileURL] URLByResolvingSymlinksInPath] 
checkResourceIsReachableAndReturnError:NULL]) {
         // the deleted file was replaced at the old path, restart the file 
updating for the replacement file and note the update
         [self reset];
@@ -119,8 +119,11 @@
     }
 }
 
-- (void)startTimerWithSelector:(SEL)aSelector {
-    fileUpdateTimer = [[NSTimer alloc] initWithFireDate:[NSDate 
dateWithTimeIntervalSinceNow:0.1] interval:2.0 target:self selector:aSelector 
userInfo:nil repeats:YES];
+- (void)startTimerCheckingFileReplacement {
+    __weak SKFileUpdateChecker *weakSelf = self;
+    fileUpdateTimer = [[NSTimer alloc] initWithFireDate:[NSDate 
dateWithTimeIntervalSinceNow:0.1] interval:2.0 repeats:YES block:^(NSTimer 
*timer){
+        [weakSelf checkForFileReplacement];
+    }];
     [[NSRunLoop currentRunLoop] addTimer:fileUpdateTimer 
forMode:NSDefaultRunLoopMode];
 }
 
@@ -269,7 +272,7 @@
     [self stop];
     fucFlags.fileChangedOnDisk = YES;
     // poll the (old) path to see whether the deleted file will be replaced
-    [self startTimerWithSelector:@selector(checkForFileReplacement:)];
+    [self startTimerCheckingFileReplacement];
 }
 
 - (void)setEnabled:(BOOL)flag {

Modified: trunk/SKMainWindowController.h
===================================================================
--- trunk/SKMainWindowController.h      2024-01-20 22:23:42 UTC (rev 14017)
+++ trunk/SKMainWindowController.h      2024-01-26 00:28:55 UTC (rev 14018)
@@ -318,7 +318,6 @@
 - (void)snapshotNeedsUpdate:(SKSnapshotWindowController *)dirstySnapshot;
 - (void)allSnapshotsNeedUpdate;
 - (void)updateSnapshotsIfNeeded;
-- (void)updateSnapshot:(NSTimer *)timer;
 
 - (void)setPdfDocument:(nullable PDFDocument *)pdfDocument 
addAnnotationsFromDictionaries:(nullable NSArray<NSDictionary<NSString *, id> 
*> *)noteDicts;
 - (void)addAnnotationsFromDictionaries:(NSArray<NSDictionary<NSString *, id> 
*> *)noteDicts removeAnnotations:(nullable NSArray<PDFAnnotation *> 
*)notesToRemove;

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2024-01-20 22:23:42 UTC (rev 14017)
+++ trunk/SKMainWindowController.m      2024-01-26 00:28:55 UTC (rev 14018)
@@ -2994,12 +2994,7 @@
     [self updateSnapshotsIfNeeded];
 }
 
-- (void)updateSnapshotsIfNeeded {
-    if ([rightSideController.snapshotTableView window] != nil && 
[dirtySnapshots count] > 0 && snapshotTimer == nil)
-        snapshotTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 
target:self selector:@selector(updateSnapshot:) userInfo:NULL repeats:YES];
-}
-
-- (void)updateSnapshot:(NSTimer *)timer {
+- (void)updateSnapshot {
     if ([dirtySnapshots count]) {
         SKSnapshotWindowController *controller = [dirtySnapshots 
objectAtIndex:0];
         NSSize newSize, oldSize = [[controller thumbnail] size];
@@ -3022,6 +3017,15 @@
     }
 }
 
+- (void)updateSnapshotsIfNeeded {
+    if ([rightSideController.snapshotTableView window] != nil && 
[dirtySnapshots count] > 0 && snapshotTimer == nil) {
+        __weak SKMainWindowController *weakSelf = self;
+        snapshotTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 
repeats:YES block:^(NSTimer *timer){
+            [weakSelf updateSnapshot];
+        }];
+    }
+}
+
 - (void)updateSnapshotFilterPredicate {
     NSString *searchString = [rightSideController.searchField stringValue];
     NSPredicate *filterPredicate = nil;

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2024-01-20 22:23:42 UTC (rev 14017)
+++ trunk/SKPDFView.m   2024-01-26 00:28:55 UTC (rev 14018)
@@ -1099,7 +1099,7 @@
     }
 }
 
-- (void)pacerScroll:(NSTimer *)timer {
+- (void)pacerScroll {
     NSScrollView *scrollView = [self scrollView];
     NSClipView *clipView = [scrollView contentView];
     NSRect bounds = [clipView bounds];
@@ -1121,7 +1121,7 @@
         [clipView scrollToPoint:bounds.origin];
 }
 
-- (void)pacerMoveReadingBar:(NSTimer *)timer {
+- (void)pacerMoveReadingBar {
     [readingBar goToNextLine];
 }
 
@@ -1130,15 +1130,16 @@
         [self stopPacer];
     } else if (pacerSpeed > 0.0 && [[self document] isLocked] == NO) {
         CGFloat interval;
-        SEL selector;
+        __weak SKPDFView *weakSelf = self;
+        void (^block)(NSTimer *) = nil;
         if ([self hasReadingBar]) {
             interval = PACER_LINE_HEIGHT / pacerSpeed;
-            selector = @selector(pacerMoveReadingBar:);
+            block = ^(NSTimer *timer){ [weakSelf pacerMoveReadingBar]; };
         } else {
             interval = 1.0 / (pacerSpeed * [([self window] ?: (NSWindow 
*)[NSScreen mainScreen]) backingScaleFactor] * [self scaleFactor]);
-            selector = @selector(pacerScroll:);
+            block = ^(NSTimer *timer){ [weakSelf pacerScroll]; };
         }
-        pacerTimer = [NSTimer scheduledTimerWithTimeInterval:interval 
target:self selector:selector userInfo:nil repeats:YES];
+        pacerTimer = [NSTimer scheduledTimerWithTimeInterval:interval 
repeats:YES block:block];
         [[NSNotificationCenter defaultCenter] 
postNotificationName:SKPDFViewPacerStartedOrStoppedNotification object:self];
     }
 }

Modified: trunk/SKSyncDot.m
===================================================================
--- trunk/SKSyncDot.m   2024-01-20 22:23:42 UTC (rev 14017)
+++ trunk/SKSyncDot.m   2024-01-26 00:28:55 UTC (rev 14018)
@@ -43,7 +43,7 @@
 
 @interface SKSyncDot (SKPrivate)
 - (void)finish:(NSTimer *)aTimer;
-- (void)animate:(NSTimer *)aTimer;
+- (void)animate;
 @end
 
 
@@ -58,7 +58,8 @@
         point = aPoint;
         page = aPage;
         phase = 0;
-        timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self 
selector:@selector(animate:) userInfo:NULL repeats:YES];
+        __weak SKSyncDot *weakSelf = self;
+        timer = [NSTimer scheduledTimerWithTimeInterval:0.05 repeats:YES 
block:^(NSTimer *timer){ [weakSelf animate]; }];
         handler = [aHandler copy];
     }
     return self;
@@ -77,7 +78,7 @@
     }
 }
 
-- (void)animate:(NSTimer *)aTimer {
+- (void)animate {
     if (atomic_fetch_add(&phase, 1) >= 9) {
         [timer invalidate];
         timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self 
selector:@selector(finish:) userInfo:NULL repeats:NO];

Modified: trunk/SKTypeSelectHelper.m
===================================================================
--- trunk/SKTypeSelectHelper.m  2024-01-20 22:23:42 UTC (rev 14017)
+++ trunk/SKTypeSelectHelper.m  2024-01-26 00:28:55 UTC (rev 14018)
@@ -62,7 +62,7 @@
 - (NSArray *)searchCache;
 - (void)searchWithStickyMatch:(BOOL)allowUpdate;
 - (void)stopTimer;
-- (void)startTimerForSelector:(SEL)selector;
+- (void)startTimerWithBlock:(void (^)(NSTimer *timer))block;
 - (void)typeSelectSearchTimeout:(id)sender;
 - (void)typeSelectCleanTimeout:(id)sender;
 - (NSUInteger)indexOfMatchedItemAfterIndex:(NSUInteger)selectedIndex;
@@ -207,7 +207,8 @@
     [self updateSearchString:searchString];
     
     // Reset the timer if it hasn't expired yet
-    [self startTimerForSelector:@selector(typeSelectSearchTimeout:)];
+    __weak SKTypeSelectHelper *weakSelf = self;
+    [self startTimerWithBlock:^(NSTimer *timer){ [weakSelf 
typeSelectSearchTimeout:timer]; }];
     
     if (matchOption != SKFullStringMatch)
         [self searchWithStickyMatch:isProcessing];
@@ -221,7 +222,8 @@
     if ([searchString length])
         [self updateSearchString:searchString];
     
-    [self startTimerForSelector:@selector(typeSelectCleanTimeout:)];
+    __weak SKTypeSelectHelper *weakSelf = self;
+    [self startTimerWithBlock:^(NSTimer *timer){ [weakSelf 
typeSelectCleanTimeout:timer]; }];
     
     isProcessing = NO;
 }
@@ -268,9 +270,9 @@
     timer = nil;
 }
 
-- (void)startTimerForSelector:(SEL)selector {
+- (void)startTimerWithBlock:(void (^)(NSTimer *timer))block {
     [self stopTimer];
-    timer = [[NSTimer alloc] initWithFireDate:[NSDate 
dateWithTimeIntervalSinceNow:[self timeoutInterval]] interval:0 target:self 
selector:selector userInfo:NULL repeats:NO];
+    timer = [[NSTimer alloc] initWithFireDate:[NSDate 
dateWithTimeIntervalSinceNow:[self timeoutInterval]] interval:0 repeats:NO 
block:block];
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
 }
 

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



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to