Revision: 15394
          http://sourceforge.net/p/skim-app/code/15394
Author:   hofman
Date:     2025-06-06 22:28:49 +0000 (Fri, 06 Jun 2025)
Log Message:
-----------
autoUpdateString for new annotations before adding to a page bby using a 
variant passing the page to add to

Modified Paths:
--------------
    trunk/PDFAnnotationCircle_SKExtensions.m
    trunk/PDFAnnotationMarkup_SKExtensions.m
    trunk/PDFAnnotationSquare_SKExtensions.m
    trunk/PDFAnnotation_SKExtensions.h
    trunk/PDFAnnotation_SKExtensions.m
    trunk/SKMainWindowController.m
    trunk/SKPDFView.m

Modified: trunk/PDFAnnotationCircle_SKExtensions.m
===================================================================
--- trunk/PDFAnnotationCircle_SKExtensions.m    2025-06-06 21:44:13 UTC (rev 
15393)
+++ trunk/PDFAnnotationCircle_SKExtensions.m    2025-06-06 22:28:49 UTC (rev 
15394)
@@ -103,8 +103,8 @@
     return dx * dx + dy * dy >= 1.0;
 }
 
-- (void)autoUpdateString {
-    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableUpdateContentsFromEnclosedTextKey])
+- (void)autoUpdateStringWithPage:(PDFPage *)page {
+    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableUpdateContentsFromEnclosedTextKey] || page == nil)
         return;
     // this calculation is roughly the inverse of -[PDFView 
addAnnotationWithType:context:]
     NSRect bounds = NSInsetRect([self bounds], [self lineWidth] - 1.0, [self 
lineWidth] - 1.0);
@@ -113,7 +113,7 @@
         return;
     t = 0.5 * w * h * (w + h - sqrt(2.0 * w * h)) / (w * w + h * h);
     bounds = NSInsetRect(bounds, t, t);
-    NSString *selString = [[[self page] selectionForRect:bounds] 
cleanedString];
+    NSString *selString = [[page selectionForRect:bounds] cleanedString];
     if ([selString length])
         [self setString:selString];
 }

Modified: trunk/PDFAnnotationMarkup_SKExtensions.m
===================================================================
--- trunk/PDFAnnotationMarkup_SKExtensions.m    2025-06-06 21:44:13 UTC (rev 
15393)
+++ trunk/PDFAnnotationMarkup_SKExtensions.m    2025-06-06 22:28:49 UTC (rev 
15394)
@@ -199,7 +199,7 @@
     return lineRects;
 }
 
-- (PDFSelection *)selection {
+- (PDFSelection *)selectionWithPage:(PDFPage *)page {
     NSMutableArray *selections = [NSMutableArray array];
     NSPointerArray *lines = [self lineRects];
     NSUInteger i, iMax = [lines count];
@@ -206,7 +206,7 @@
     
     for (i = 0; i < iMax; i++) {
         // slightly outset the rect to avoid rounding errors, as 
selectionForRect is pretty strict in some OS versions, but unfortunately not in 
others
-        PDFSelection *selection = [[self page] 
selectionForRect:NSInsetRect([lines rectAtIndex:i], -1.0, -1.0)];
+        PDFSelection *selection = [page selectionForRect:NSInsetRect([lines 
rectAtIndex:i], -1.0, -1.0)];
         if ([selection hasCharacters])
             [selections addObject:selection];
     }
@@ -213,6 +213,10 @@
     return [PDFSelection selectionByAddingSelections:selections];
 }
 
+- (PDFSelection *)selection {
+    return [self selectionWithPage:[self page]];
+}
+
 - (BOOL)hitTest:(NSPoint)point {
     if ([super hitTest:point] == NO)
         return NO;
@@ -303,6 +307,14 @@
         [self setString:selString];
 }
 
+- (void)autoUpdateStringWithPage:(PDFPage *)page {
+    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableUpdateContentsFromEnclosedTextKey])
+        return;
+    NSString *selString = [[self selectionWithPage:page] cleanedString];
+    if ([selString length])
+        [self setString:selString];
+}
+
 - (NSSet *)keysForValuesToObserveForUndo {
     static NSSet *markupKeys = nil;
     if (markupKeys == nil) {

Modified: trunk/PDFAnnotationSquare_SKExtensions.m
===================================================================
--- trunk/PDFAnnotationSquare_SKExtensions.m    2025-06-06 21:44:13 UTC (rev 
15393)
+++ trunk/PDFAnnotationSquare_SKExtensions.m    2025-06-06 22:28:49 UTC (rev 
15394)
@@ -93,13 +93,13 @@
     return NSWidth(bounds) <= 2.0 * delta || NSHeight(bounds) <= 2.0 * delta 
|| NSPointInRect(point, NSInsetRect(bounds, delta, delta)) == NO;
 }
 
-- (void)autoUpdateString {
-    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableUpdateContentsFromEnclosedTextKey])
+- (void)autoUpdateStringWithPage:(PDFPage *)page {
+    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableUpdateContentsFromEnclosedTextKey] || page == nil)
         return;
     NSRect bounds = NSInsetRect([self bounds], [self lineWidth] - 1.0, [self 
lineWidth] - 1.0);
     if (NSWidth(bounds) <= 0.0 || NSHeight(bounds) <= 0.0)
         return;
-    NSString *selString = [[[self page] selectionForRect:bounds] 
cleanedString];
+    NSString *selString = [[page selectionForRect:bounds] cleanedString];
     if ([selString length])
         [self setString:selString];
 }

Modified: trunk/PDFAnnotation_SKExtensions.h
===================================================================
--- trunk/PDFAnnotation_SKExtensions.h  2025-06-06 21:44:13 UTC (rev 15393)
+++ trunk/PDFAnnotation_SKExtensions.h  2025-06-06 22:28:49 UTC (rev 15394)
@@ -125,6 +125,7 @@
 - (void)registerUserName;
 
 - (void)autoUpdateString;
+- (void)autoUpdateStringWithPage:(PDFPage *)page;
 
 @property (nonatomic, readonly) NSString *uniqueID;
 

Modified: trunk/PDFAnnotation_SKExtensions.m
===================================================================
--- trunk/PDFAnnotation_SKExtensions.m  2025-06-06 21:44:13 UTC (rev 15393)
+++ trunk/PDFAnnotation_SKExtensions.m  2025-06-06 22:28:49 UTC (rev 15394)
@@ -505,7 +505,8 @@
         [self setModificationDate:[NSDate date]];
 }
 
-- (void)autoUpdateString {}
+- (void)autoUpdateString { if ([self page]) [self 
autoUpdateStringWithPage:[self page]]; }
+- (void)autoUpdateStringWithPage:(PDFPage *)page {}
 
 - (NSString *)colorDefaultKey { return nil; }
 

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2025-06-06 21:44:13 UTC (rev 15393)
+++ trunk/SKMainWindowController.m      2025-06-06 22:28:49 UTC (rev 15394)
@@ -988,9 +988,10 @@
                 [pageIndexes addIndex:pageIndex];
                 [annotation setShouldDisplay:shouldDisplay];
                 [annotation setShouldPrint:shouldDisplay];
-                [pdfDoc addAnnotation:annotation toPage:[pdfDoc 
pageAtIndex:pageIndex]];
+                PDFPage *page = [pdfDoc pageAtIndex:pageIndex];
                 if (autoUpdate && [[annotation contents] length] == 0)
-                    [annotation autoUpdateString];
+                    [annotation autoUpdateStringWithPage:page];
+                [pdfDoc addAnnotation:annotation toPage:page];
                 [notesToAdd addObject:annotation];
             }
         }

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2025-06-06 21:44:13 UTC (rev 15393)
+++ trunk/SKPDFView.m   2025-06-06 22:28:49 UTC (rev 15394)
@@ -2460,9 +2460,9 @@
             if ([text length] > 0 || [newAnnotation string] == nil)
                 [newAnnotation setString:text ?: @""];
             [newAnnotation registerUserName];
+            if ([text length] == 0 && isInitial == NO)
+                [newAnnotation autoUpdateStringWithPage:page];
             [[self document] addAnnotation:newAnnotation toPage:page];
-            if ([text length] == 0 && isInitial == NO)
-                [newAnnotation autoUpdateString];
         }
         [self setUndoActionName:NSLocalizedString(@"Add Note", @"Undo action 
name")];
 
@@ -2476,11 +2476,11 @@
         if (annotationType != SKNoteTypeLine && annotationType != 
SKNoteTypeInk && [text length] > 0)
             [newAnnotation setString:text];
         [newAnnotation registerUserName];
-        [[self document] addAnnotation:newAnnotation toPage:page];
         if ([text length] == 0 && isInitial == NO)
-            [newAnnotation autoUpdateString];
+            [newAnnotation autoUpdateStringWithPage:page];
         if ([newAnnotation string] == nil)
             [newAnnotation setString:@""];
+        [[self document] addAnnotation:newAnnotation toPage:page];
         [self setUndoActionName:NSLocalizedString(@"Add Note", @"Undo action 
name")];
 
         if (toolMode == SKToolModeText || toolMode == SKToolModeNote)

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