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
This commit is contained in:
Jonas Holzman
2025-03-11 16:09:42 +01:00
parent 45949cffe2
commit 27117e0ffc

View File

@@ -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");
}