Revision: 14297
          http://sourceforge.net/p/skim-app/code/14297
Author:   hofman
Date:     2024-06-04 15:37:43 +0000 (Tue, 04 Jun 2024)
Log Message:
-----------
Pass backing scale to thumbnail methods. Base it on the current window. Update 
thumbnails when backing scale factor of the window changes. Make sure 
thumbnails get updated when displayed in presentation with a different backing 
scale.

Modified Paths:
--------------
    trunk/PDFPage_SKExtensions.h
    trunk/PDFPage_SKExtensions.m
    trunk/SKImageToolTipContext.m
    trunk/SKMainWindowController.h
    trunk/SKMainWindowController.m
    trunk/SKMainWindowController_FullScreen.m
    trunk/SKMainWindowController_UI.m
    trunk/SKSnapshotWindowController.h
    trunk/SKSnapshotWindowController.m

Modified: trunk/PDFPage_SKExtensions.h
===================================================================
--- trunk/PDFPage_SKExtensions.h        2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/PDFPage_SKExtensions.h        2024-06-04 15:37:43 UTC (rev 14297)
@@ -60,7 +60,7 @@
 @property (nonatomic, readonly) NSRect autoCropBox;
 @property (nonatomic, readonly) NSRect boundingBox;
 
-- (NSImage *)thumbnailWithSize:(CGFloat)size forBox:(PDFDisplayBox)box 
hasShadow:(BOOL)hasShadow highlights:(nullable NSArray *)highlights;
+- (NSImage *)thumbnailWithSize:(CGFloat)size scale:(CGFloat)backingScale 
forBox:(PDFDisplayBox)box hasShadow:(BOOL)hasShadow highlights:(nullable 
NSArray *)highlights;
 
 - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)size;
 @property (nonatomic, readonly) NSAttributedString *thumbnailAttachment;

Modified: trunk/PDFPage_SKExtensions.m
===================================================================
--- trunk/PDFPage_SKExtensions.m        2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/PDFPage_SKExtensions.m        2024-06-04 15:37:43 UTC (rev 14297)
@@ -149,12 +149,11 @@
     return NSIntersectionRect(NSInsetRect([self foregroundRect], -2.0, -2.0), 
[self boundsForBox:kPDFDisplayBoxCropBox]);
 }
 
-- (NSImage *)thumbnailWithSize:(CGFloat)aSize forBox:(PDFDisplayBox)box 
hasShadow:(BOOL)hasShadow highlights:(NSArray *)highlights {
+- (NSImage *)thumbnailWithSize:(CGFloat)aSize scale:(CGFloat)backingScale 
forBox:(PDFDisplayBox)box hasShadow:(BOOL)hasShadow highlights:(NSArray 
*)highlights {
     NSRect bounds = [self boundsForBox:box];
     NSSize pageSize = bounds.size;
     CGFloat scale = 1.0;
     NSSize thumbnailSize;
-    CGFloat backingScale = [NSScreen maxBackingScaleFactor];
     CGFloat shadowBlurRadius = 0.0;
     CGFloat shadowOffset = 0.0;
     NSRect pageRect = NSZeroRect;
@@ -220,7 +219,7 @@
 }
 
 - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)aSize {
-    NSImage *image = [self thumbnailWithSize:aSize 
forBox:kPDFDisplayBoxCropBox hasShadow:YES highlights:nil];
+    NSImage *image = [self thumbnailWithSize:aSize scale:[NSScreen 
maxBackingScaleFactor] forBox:kPDFDisplayBoxCropBox hasShadow:YES 
highlights:nil];
     
     NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
     NSString *filename = [NSString stringWithFormat:@"page_%lu.tiff", 
(unsigned long)([self pageIndex] + 1)];
@@ -266,7 +265,7 @@
 
 - (NSData *)TIFFDataForRect:(NSRect)rect {
     PDFDisplayBox box = NSEqualRects(rect, [self 
boundsForBox:kPDFDisplayBoxCropBox]) ? kPDFDisplayBoxCropBox : 
kPDFDisplayBoxMediaBox;
-    NSImage *pageImage = [self thumbnailWithSize:0.0 forBox:box hasShadow:NO 
highlights:nil];
+    NSImage *pageImage = [self thumbnailWithSize:0.0 scale:[NSScreen 
maxBackingScaleFactor] forBox:box hasShadow:NO highlights:nil];
     NSRect bounds = [self boundsForBox:box];
     
     if (NSEqualRects(rect, NSZeroRect) || NSEqualRects(rect, bounds))

Modified: trunk/SKImageToolTipContext.m
===================================================================
--- trunk/SKImageToolTipContext.m       2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/SKImageToolTipContext.m       2024-06-04 15:37:43 UTC (rev 14297)
@@ -110,7 +110,7 @@
     NSPoint point = [[self effectiveDestinationForView:nil] point];
     NSRect bounds = [page boundsForBox:kPDFDisplayBoxCropBox];
     CGFloat size = isScaled ? ceil(scale * fmax(NSWidth(bounds), 
NSHeight(bounds))) : 0.0;
-    NSImage *pageImage = [page thumbnailWithSize:size 
forBox:kPDFDisplayBoxCropBox hasShadow:NO highlights:selections];
+    NSImage *pageImage = [page thumbnailWithSize:size scale:[[NSScreen 
mainScreen] backingScaleFactor] forBox:kPDFDisplayBoxCropBox hasShadow:NO 
highlights:selections];
     NSRect pageImageRect = {NSZeroPoint, [pageImage size]};
     NSRect sourceRect = NSZeroRect;
     PDFSelection *pageSelection = [page selectionForRect:bounds];
@@ -257,7 +257,7 @@
 @implementation PDFPage (SKImageToolTipContext)
 
 - (NSImage *)toolTipImageWithScale:(CGFloat)scale {
-    NSImage *image = [self thumbnailWithSize:256.0 
forBox:kPDFDisplayBoxCropBox hasShadow:NO highlights:nil];
+    NSImage *image = [self thumbnailWithSize:256.0 scale:[[NSScreen 
mainScreen] backingScaleFactor] forBox:kPDFDisplayBoxCropBox hasShadow:NO 
highlights:nil];
     [[[image representations] firstObject] setOpaque:YES];
     return image;
 }

Modified: trunk/SKMainWindowController.h
===================================================================
--- trunk/SKMainWindowController.h      2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/SKMainWindowController.h      2024-06-04 15:37:43 UTC (rev 14297)
@@ -202,6 +202,7 @@
         unsigned int hasCropped:1;
         unsigned int fullSizeContent:1;
         unsigned int needsCleanup:1;
+        unsigned int presentationInvalidatesThumbnails:1;
     } mwcFlags;
 }
 

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/SKMainWindowController.m      2024-06-04 15:37:43 UTC (rev 14297)
@@ -1580,6 +1580,9 @@
     
     [[SKImageToolTipWindow sharedToolTipWindow] orderOut:nil];
     
+    if (isPresentation && fabs([[self window] backingScaleFactor] - 
[mainWindow backingScaleFactor]) > 0.0)
+        [self allThumbnailsNeedUpdate];
+    
     if (animate) {
         [NSAnimationContext runAnimationGroup:^(NSAnimationContext * context){
                 [[contentView animator] replaceSubview:oldView 
with:overviewContentView];
@@ -2320,7 +2323,7 @@
 }
 
 - (void)snapshotController:(SKSnapshotWindowController *)controller 
didFinishSetup:(SKSnapshotOpenType)openType {
-    NSImage *image = [controller thumbnailWithSize:snapshotCacheSize];
+    NSImage *image = [controller thumbnailWithSize:snapshotCacheSize 
scale:[[self window] backingScaleFactor]];
     
     [image setAccessibilityDescription:[NSString 
stringWithFormat:NSLocalizedString(@"Page %@", @""), [controller pageLabel]]];
     [controller setThumbnail:image];
@@ -2817,9 +2820,13 @@
     PDFPage *page = [self pageForThumbnail:thumbnail];
     NSArray *highlights = page && [[[pdfView readingBar] page] isEqual:page] ? 
@[[pdfView readingBar]] : nil;
     PDFDisplayBox box = [pdfView displayBox];
+    CGFloat scale = [[self window] backingScaleFactor];
     
+    if ([self interactionMode] == SKPresentationMode && 
mwcFlags.presentationInvalidatesThumbnails == 0 && fabs([mainWindow 
backingScaleFactor] - scale) > 0.0)
+        mwcFlags.presentationInvalidatesThumbnails = 1;
+    
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
0), ^{
-        NSImage *image = [page thumbnailWithSize:thumbnailCacheSize forBox:box 
hasShadow:YES highlights:highlights];
+        NSImage *image = [page thumbnailWithSize:thumbnailCacheSize 
scale:scale forBox:box hasShadow:YES highlights:highlights];
         [image setAccessibilityDescription:[NSString 
stringWithFormat:NSLocalizedString(@"Page %@", @""), [page displayLabel]]];
         
         dispatch_async(dispatch_get_main_queue(), ^{
@@ -2873,7 +2880,7 @@
     if ([[pdfView document] isLocked])
         [stamps addObject:[[SKThumbnailStamp alloc] 
initWithImage:[[NSWorkspace sharedWorkspace] 
iconForFileType:NSFileTypeForHFSTypeCode(kLockedBadgeIcon)] rect:rect 
fraction:0.5]];
     
-    return [page thumbnailWithSize:thumbnailCacheSize 
forBox:kPDFDisplayBoxMediaBox hasShadow:YES highlights:stamps];
+    return [page thumbnailWithSize:thumbnailCacheSize scale:[[self window] 
backingScaleFactor] forBox:kPDFDisplayBoxMediaBox hasShadow:YES 
highlights:stamps];
 }
 
 - (void)resetThumbnails {
@@ -3014,7 +3021,7 @@
     if ([dirtySnapshots count]) {
         SKSnapshotWindowController *controller = [dirtySnapshots 
objectAtIndex:0];
         NSSize newSize, oldSize = [[controller thumbnail] size];
-        NSImage *image = [controller thumbnailWithSize:snapshotCacheSize];
+        NSImage *image = [controller thumbnailWithSize:snapshotCacheSize 
scale:[[self window] backingScaleFactor]];
         
         [image setAccessibilityDescription:[NSString 
stringWithFormat:NSLocalizedString(@"Page %@", @""), [controller pageLabel]]];
         [controller setThumbnail:image];

Modified: trunk/SKMainWindowController_FullScreen.m
===================================================================
--- trunk/SKMainWindowController_FullScreen.m   2024-06-03 15:58:56 UTC (rev 
14296)
+++ trunk/SKMainWindowController_FullScreen.m   2024-06-04 15:37:43 UTC (rev 
14297)
@@ -535,6 +535,11 @@
     [pdfView layoutDocumentView];
     [pdfView requiresDisplay];
     
+    if (mwcFlags.presentationInvalidatesThumbnails) {
+        mwcFlags.presentationInvalidatesThumbnails = 0;
+        [self allThumbnailsNeedUpdate];
+    }
+    
     if ([[[self pdfView] currentPage] isEqual:page] == NO)
         [[self pdfView] goToCurrentPage:page];
     

Modified: trunk/SKMainWindowController_UI.m
===================================================================
--- trunk/SKMainWindowController_UI.m   2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/SKMainWindowController_UI.m   2024-06-04 15:37:43 UTC (rev 14297)
@@ -298,6 +298,16 @@
     }
 }
 
+- (void)windowDidChangeBackingProperties:(NSNotification *)notification {
+    if ([self interactionMode] == SKPresentationMode)
+        return;
+    CGFloat oldScale = [[[notification userInfo] 
objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue];
+    if (fabs(oldScale - [[self window] backingScaleFactor]) > 0.0) {
+        [self allThumbnailsNeedUpdate];
+        [self allSnapshotsNeedUpdate];
+    }
+}
+
 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject {
     if (fieldEditor == nil) {
         fieldEditor = [[SKFieldEditor alloc] init];

Modified: trunk/SKSnapshotWindowController.h
===================================================================
--- trunk/SKSnapshotWindowController.h  2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/SKSnapshotWindowController.h  2024-06-04 15:37:43 UTC (rev 14297)
@@ -87,7 +87,7 @@
 
 - (void)updatePageLabel;
 
-- (NSImage *)thumbnailWithSize:(CGFloat)size;
+- (NSImage *)thumbnailWithSize:(CGFloat)size scale:(CGFloat)scale;
 
 - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)size;
 

Modified: trunk/SKSnapshotWindowController.m
===================================================================
--- trunk/SKSnapshotWindowController.m  2024-06-03 15:58:56 UTC (rev 14296)
+++ trunk/SKSnapshotWindowController.m  2024-06-04 15:37:43 UTC (rev 14297)
@@ -472,17 +472,15 @@
 
 #pragma mark Thumbnails
 
-- (NSImage *)thumbnailWithSize:(CGFloat)size {
+- (NSImage *)thumbnailWithSize:(CGFloat)size scale:(CGFloat)scale {
     NSRect bounds = [pdfView visibleContentRect];
     NSAffineTransform *transform = [NSAffineTransform transform];
     NSSize thumbnailSize = bounds.size;
     CGFloat shadowBlurRadius = 0.0;
     CGFloat shadowOffset = 0.0;
-    CGFloat scale = 0.0;
     NSImage *image;
     
     if (size > 0.0) {
-        scale = [NSScreen maxBackingScaleFactor];
         shadowBlurRadius = round(scale * size / 32.0) / scale;
         shadowOffset = -ceil(scale * shadowBlurRadius * 0.75) / scale;
         if (NSHeight(bounds) > NSWidth(bounds))
@@ -492,7 +490,6 @@
         [transform translateXBy:shadowBlurRadius yBy:shadowBlurRadius - 
shadowOffset];
         [transform scaleXBy:(thumbnailSize.width - 2.0 * shadowBlurRadius) / 
NSWidth(bounds) yBy:(thumbnailSize.height - 2.0 * shadowBlurRadius) / 
NSHeight(bounds)];
     } else if (size < 0.0) {
-        scale = [[self window] backingScaleFactor];
         thumbnailSize = [[self window] frame].size;
     }
     
@@ -525,7 +522,7 @@
 }
 
 - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)size {
-    NSImage *image = [self thumbnailWithSize:size];
+    NSImage *image = [self thumbnailWithSize:size scale:[NSScreen 
maxBackingScaleFactor]];
     
     NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
     NSString *filename = [NSString 
stringWithFormat:@"snapshot_page_%lu.tiff",(unsigned long)( [self pageIndex] + 
1)];
@@ -572,7 +569,7 @@
     CGFloat thumbRatio = thumbSize.height / thumbSize.width;
     CGFloat dockRatio = NSHeight(dockRect) / NSWidth(dockRect);
     CGFloat scaleFactor;
-    CGFloat scale = [NSScreen maxBackingScaleFactor];
+    CGFloat scale = round([[[thumbnail representations] firstObject] 
pixelsHigh] / thumbSize.height);
     CGFloat shadowRadius = round(scale * fmax(thumbSize.width, 
thumbSize.height) / 32.0) / scale;
     CGFloat shadowOffset = ceil(0.75 * scale * shadowRadius) / scale;
     
@@ -623,7 +620,7 @@
     if ([[self delegate] 
respondsToSelector:@selector(snapshotController:miniaturizedRect:)]) {
         NSWindow *window = [self window];
         NSArray *filters = SKColorEffectFilters();
-        NSImage *contentImage = [self thumbnailWithSize:-1.0];
+        NSImage *contentImage = [self thumbnailWithSize:-1.0 scale:[[self 
window] backingScaleFactor]];
         NSImage *windowImage;
         NSRect windowRect = [window frame];
         NSRect dockRect = [[self delegate] snapshotController:self 
miniaturizedRect:miniaturize];
@@ -725,7 +722,7 @@
 
 - (void)filePromiseProvider:(NSFilePromiseProvider *)filePromiseProvider 
writePromiseToURL:(NSURL *)fileURL completionHandler:(void (^)(NSError 
*))completionHandler {
     NSError *error = nil;
-    [[[self thumbnailWithSize:0.0] TIFFRepresentation] writeToURL:fileURL 
options:NSDataWritingAtomic error:&error];
+    [[[self thumbnailWithSize:0.0 scale:[NSScreen maxBackingScaleFactor]] 
TIFFRepresentation] writeToURL:fileURL options:NSDataWritingAtomic 
error:&error];
     completionHandler(error);
 }
 

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