From 27117e0ffcb4ededec552f95fdb3361c51ba187b Mon Sep 17 00:00:00 2001 From: Jonas Holzman Date: Tue, 11 Mar 2025 16:09:42 +0100 Subject: [PATCH] Fix: macOS: Wrong QuickLook aspect ratio for screenshot previews of blend files This patch fixes a visual issue where the QuickLook previews/thumbnails of blend file (introduced in PR #107072) would have a distorted square aspect ratio instead of their original aspect ratio. Fixed by using the original image aspect ratio scaled to honor the QLFileThumbnailRequest's maximumSize as per the official documentation[1]. [1]: https://developer.apple.com/documentation/quicklookthumbnailing/qlthumbnailreply/init(contextsize:currentcontextdrawing:)?language=objc Pull Request: https://projects.blender.org/blender/blender/pulls/135691 --- .../blendthumb/src/thumbnail_provider.mm | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/source/blender/blendthumb/src/thumbnail_provider.mm b/source/blender/blendthumb/src/thumbnail_provider.mm index 6ca562e23e9..985b0e052e1 100644 --- a/source/blender/blendthumb/src/thumbnail_provider.mm +++ b/source/blender/blendthumb/src/thumbnail_provider.mm @@ -152,23 +152,33 @@ static NSImage *generate_nsimage_for_file(const char *src_blend_path, NSError *e NSLog(@"Generating thumbnail for %@", request.fileURL.path); @autoreleasepool { NSError *error = nil; - NSImage *ns_image = generate_nsimage_for_file(request.fileURL.path.fileSystemRepresentation, - error); - if (ns_image == nil) { + NSImage *image = generate_nsimage_for_file(request.fileURL.path.fileSystemRepresentation, + error); + if (image == nil || image.size.width <= 0 || image.size.height <= 0) { handler(nil, error); return; } - handler([QLThumbnailReply replyWithContextSize:request.maximumSize - currentContextDrawingBlock:^BOOL { - [ns_image drawInRect:NSMakeRect(0, - 0, - request.maximumSize.width, - request.maximumSize.height)]; - // Release the ns_image that was strongly captured by the block. - [ns_image release]; - return YES; - }], - nil); + + const CGFloat width_ratio = request.maximumSize.width / image.size.width; + const CGFloat height_ratio = request.maximumSize.height / image.size.height; + const CGFloat scale_factor = MIN(width_ratio, height_ratio); + + const NSSize context_size = NSMakeSize(image.size.width * scale_factor, + image.size.height * scale_factor); + + const NSRect context_rect = NSMakeRect(0, 0, context_size.width, context_size.height); + + QLThumbnailReply *thumbnailReply = [QLThumbnailReply replyWithContextSize:context_size + currentContextDrawingBlock:^BOOL { + [image drawInRect:context_rect]; + /* Release the image that was strongly + * captured by this block. */ + [image release]; + return YES; + }]; + + /* Return the thumbnail reply. */ + handler(thumbnailReply, nil); } NSLog(@"Thumbnail generation succcessfully completed"); }