forked from FreeCAD/FreeCAD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macOS] Adding QuickLook feature (FreeCAD#7491)
* [macOS] Adding QuickLook feature * [macOS] Adding QuickLook support for Conda and Homebrew. * [macOS] Support non-square app icons in thumbnails. * [macOS] adding icon for .FCScript files
- Loading branch information
1 parent
c9aadee
commit f33be68
Showing
10 changed files
with
581 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
#cmake_minimum_required(VERSION 3.23) | ||
#project(FCQuickLook) | ||
|
||
add_library( | ||
QuicklookFCStd | ||
SHARED | ||
GeneratePreviewForURL.m | ||
GenerateThumbnailForURL.m | ||
main.c | ||
) | ||
|
||
set_target_properties( | ||
QuicklookFCStd | ||
PROPERTIES | ||
FRAMEWORK TRUE | ||
MACOSX_FRAMEWORK_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/QuicklookFCStd.qlgenerator/Contents/Info.plist" | ||
#SUFFIX .qlgenerator | ||
) | ||
|
||
target_link_libraries( | ||
QuicklookFCStd | ||
"-framework AppKit" | ||
"-framework ApplicationServices" | ||
"-framework CoreData" | ||
"-framework CoreFoundation" | ||
"-framework CoreServices" | ||
"-framework Foundation" | ||
"-framework QuickLook" | ||
) | ||
|
||
set_target_properties( | ||
QuicklookFCStd | ||
PROPERTIES LINK_FLAGS "-Wl,-F/Library/Frameworks" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <CoreServices/CoreServices.h> | ||
#include <QuickLook/QuickLook.h> | ||
#include <Foundation/Foundation.h> | ||
#include <AppKit/AppKit.h> | ||
|
||
/* ----------------------------------------------------------------------------- | ||
Generate a preview for file | ||
This function's job is to create preview for designated file | ||
----------------------------------------------------------------------------- */ | ||
|
||
|
||
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options) | ||
{ | ||
OSStatus ret = coreFoundationUnknownErr; | ||
|
||
@autoreleasepool { | ||
|
||
// unzip -qq -o -j -d /tmp ~/test.FCStd thumbnails/Thumbnail.png | ||
// -qq : be really quiet | ||
// -o : overwrite without prompt | ||
// -j : don't create archive paths | ||
// -d : destination path (create this path) | ||
// extracts thumbnails/Thumbnail.png to /tmp/Thumbnail.png . | ||
// We add a UUID and use a system temp directory here. | ||
|
||
// TODO: do we need to release any of these resources? | ||
NSUUID *uuid = [NSUUID UUID]; | ||
NSString *uuidPath = [uuid UUIDString]; | ||
NSString *unzipDstPath = [NSString stringWithFormat:@"%@/%@/", NSTemporaryDirectory(), uuidPath]; | ||
NSString *unzipDstFile = [NSString stringWithFormat:@"%@%@", unzipDstPath, @"Thumbnail.png"]; | ||
NSURL *zipFile = (__bridge NSURL *)url; | ||
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" | ||
arguments:@[@"-qq", @"-o", @"-j", @"-d", unzipDstPath, [zipFile path], @"thumbnails/Thumbnail.png"]]; | ||
[task waitUntilExit]; | ||
// NSLog(@"%@", unzipDstPath); | ||
// NSLog(@"%@", unzipDstFile); | ||
|
||
if ( [[NSFileManager defaultManager] fileExistsAtPath:unzipDstFile] ) | ||
{ | ||
// Preview will be drawn in a vectorized context | ||
CGSize canvasSize = CGSizeMake(512, 512); | ||
CGContextRef cgContext = QLPreviewRequestCreateContext(preview, canvasSize, true, NULL); | ||
if(cgContext) | ||
{ | ||
CGDataProviderRef pngDP = CGDataProviderCreateWithFilename([unzipDstFile fileSystemRepresentation]); | ||
CGImageRef image = CGImageCreateWithPNGDataProvider(pngDP, NULL, true, kCGRenderingIntentDefault); | ||
|
||
CGContextDrawImage(cgContext,CGRectMake(0, 0, 512, 512), image); | ||
|
||
QLPreviewRequestFlushContext(preview, cgContext); | ||
ret = noErr; | ||
|
||
CFRelease(cgContext); | ||
CFRelease(image); | ||
} | ||
} | ||
|
||
if ( [[NSFileManager defaultManager] fileExistsAtPath:unzipDstFile] ) | ||
[[NSFileManager defaultManager] removeItemAtPath:unzipDstFile error:nil]; | ||
|
||
if ( [[NSFileManager defaultManager] fileExistsAtPath:unzipDstPath] ) | ||
[[NSFileManager defaultManager] removeItemAtPath:unzipDstPath error:nil]; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
|
||
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview) | ||
{ | ||
// implement only if supported | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <CoreFoundation/CoreFoundation.h> | ||
#include <CoreServices/CoreServices.h> | ||
#include <QuickLook/QuickLook.h> | ||
#include <Foundation/Foundation.h> | ||
#include <AppKit/AppKit.h> | ||
|
||
/* ----------------------------------------------------------------------------- | ||
Generate a thumbnail for file | ||
This function's job is to create thumbnail for designated file as fast as possible | ||
----------------------------------------------------------------------------- */ | ||
|
||
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) | ||
{ | ||
OSStatus ret = coreFoundationUnknownErr; | ||
|
||
@autoreleasepool { | ||
|
||
// unzip -qq -o -j -d /tmp ~/test.FCStd thumbnails/Thumbnail.png | ||
// -qq : be really quiet | ||
// -o : overwrite without prompt | ||
// -j : don't create archive paths | ||
// -d : destination path (create this path) | ||
// extracts thumbnails/Thumbnail.png to /tmp/Thumbnail.png . | ||
// We add a UUID and use a system temp directory here. | ||
|
||
NSUUID *uuid = [NSUUID UUID]; | ||
NSString *uuidPath = [uuid UUIDString]; | ||
NSString *unzipDstPath = [NSString stringWithFormat:@"%@/%@/", NSTemporaryDirectory(), uuidPath]; | ||
NSString *unzipDstFile = [NSString stringWithFormat:@"%@%@", unzipDstPath, @"Thumbnail.png"]; | ||
NSURL *zipFile = (__bridge NSURL *)url; | ||
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" | ||
arguments:@[@"-qq", @"-o", @"-j", @"-d", unzipDstPath, [zipFile path], @"thumbnails/Thumbnail.png"]]; | ||
[task waitUntilExit]; | ||
// NSLog(@"%@", unzipDstPath); | ||
// NSLog(@"%@", unzipDstFile); | ||
|
||
if ( [[NSFileManager defaultManager] fileExistsAtPath:unzipDstFile] ) | ||
{ | ||
CGSize canvasSize = CGSizeMake(128, 128); | ||
CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, /*maxSize*/canvasSize, true, NULL); | ||
if(cgContext) | ||
{ | ||
CGDataProviderRef pngDP = CGDataProviderCreateWithFilename([unzipDstFile fileSystemRepresentation]); | ||
CGImageRef image = CGImageCreateWithPNGDataProvider(pngDP, NULL, true, kCGRenderingIntentDefault); | ||
CGDataProviderRelease(pngDP); | ||
|
||
CGContextDrawImage(cgContext,CGRectMake(0, 0, 128, 128), image); | ||
|
||
QLThumbnailRequestFlushContext(thumbnail, cgContext); | ||
ret = noErr; | ||
|
||
CFRelease(cgContext); | ||
CFRelease(image); | ||
} | ||
} | ||
|
||
if ( [[NSFileManager defaultManager] isDeletableFileAtPath:unzipDstFile] ) | ||
[[NSFileManager defaultManager] removeItemAtPath:unzipDstFile error:nil]; | ||
|
||
if ( [[NSFileManager defaultManager] isDeletableFileAtPath:unzipDstPath] ) | ||
[[NSFileManager defaultManager] removeItemAtPath:unzipDstPath error:nil]; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
|
||
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail) | ||
{ | ||
// implement only if supported | ||
} |
Oops, something went wrong.