Abandoned: macOS: support QuickLook preview #107014

Closed
Ankit Meel wants to merge 8 commits from ankitm:ankitm/quicklook into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
7 changed files with 249 additions and 6 deletions
Showing only changes of commit ddf41d0c4e - Show all commits

View File

@ -21,6 +21,10 @@ set(SRC
src/blendthumb_png.cc
)
set(LIB
bf_blenlib
)
if(WIN32)
# -----------------------------------------------------------------------------
# Build `BlendThumb.dll`
@ -36,10 +40,14 @@ if(WIN32)
add_library(BlendThumb SHARED ${SRC} ${SRC_WIN32})
target_link_libraries(BlendThumb bf_blenlib dbghelp.lib Version.lib)
list(APPEND LIB
dbghelp.lib
Version.lib
)
target_link_libraries(BlendThumb ${LIB})
set_target_properties(BlendThumb PROPERTIES LINK_FLAGS_DEBUG "/NODEFAULTLIB:msvcrt")
else()
endif()
if(UNIX AND NOT APPLE)
# -----------------------------------------------------------------------------
# Build `blender-thumbnailer` executable
@ -49,7 +57,41 @@ else()
add_executable(blender-thumbnailer ${SRC} ${SRC_CMD})
setup_platform_linker_flags(blender-thumbnailer)
target_link_libraries(blender-thumbnailer bf_blenlib)
target_link_libraries(blender-thumbnailer ${LIB})
if(DEFINED PTHREADS_LIBRARIES)
target_link_libraries(blender-thumbnailer ${PTHREADS_LIBRARIES})
endif()
endif()
if(APPLE)
# -----------------------------------------------------------------------------
# Build `blender-thumbnailer` appex
set(SRC_CMD
src/ThumbnailProvider.mm
src/ThumbnailProvider.h
src/Info.plist
)
add_executable(blender-thumbnailer MACOSX_BUNDLE ${SRC} ${SRC_CMD})
set_target_properties(blender-thumbnailer PROPERTIES
MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/src/Info.plist
MACOSX_FRAMEWORK_IDENTIFIER org.blenderfoundation.blender.thumbnailer)
set_target_properties(blender-thumbnailer PROPERTIES
XIB_RESOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/Base.lproj")
# Doesn't affect other generators. So need to specify extension for others.
set_target_properties(blender-thumbnailer PROPERTIES
XCODE_PRODUCT_TYPE com.apple.product-type.app-extension)
set_target_properties(blender-thumbnailer PROPERTIES
BUNDLE_EXTENSION appex)
setup_platform_linker_flags(blender-thumbnailer)
set_target_properties(blender-thumbnailer PROPERTIES BUILD_WITH_INSTALL_RPATH true)
list(APPEND LIB
"-framework QuickLookThumbnailing"
"-fobjc-link-runtime"
"-fapplication-extension"
"-e _NSExtensionMain"
)
target_link_libraries(blender-thumbnailer ${LIB})
if(DEFINED PTHREADS_LIBRARIES)
target_link_libraries(blender-thumbnailer ${PTHREADS_LIBRARIES})
endif()

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="PreviewViewController" customModuleProvider="">
<connections>
<outlet property="view" destination="c22-O7-iKe" id="NRM-P4-wb6"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe" userLabel="Preview View">
<rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
</objects>
</document>

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>blender_thumbnailer</string>
<key>CFBundleExecutable</key>
<string>blender-thumbnailer</string>
<key>CFBundleIdentifier</key>
<string>org.blenderfoundation.blender.thumbnailer</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>blender-thumbnailer</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>11.3</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>QLSupportedContentTypes</key>
<array>
<string>org.blenderfoundation.blender.file</string>
</array>
<key>QLThumbnailMinimumDimension</key>
<integer>0</integer>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.quicklook.preview</string>
<key>NSExtensionPrincipalClass</key>
<string>PreviewProvider</string>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,10 @@
#include <AppKit/AppKit.h>
#import <Quartz/Quartz.h>
NS_ASSUME_NONNULL_BEGIN
@interface PreviewViewController : NSViewController <QLPreviewingController>
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,119 @@
#include <cstdio>
#include <fstream>
#include <unistd.h>
#import <Foundation/Foundation.h>
#import <QuickLook/QLBase.h>
#import <QuickLook/QuickLook.h>
#import "ThumbnailProvider.h"
#include "BLI_fileops.h"
#include "BLI_filereader.h"
#include "blendthumb.hh"
class FileReaderRAII {
int src_file_ = -1;
public:
explicit FileReaderRAII(int src_file) : src_file_(src_file) {}
~FileReaderRAII()
{
if (src_file_ != -1) {
close(src_file_);
}
}
bool good()
{
return src_file_ != -1;
}
int get()
{
return src_file_;
}
};
static eThumbStatus creator_impl(const char *src_blend_path)
{
eThumbStatus err;
/* Open source file `src_blend`. */
FileReaderRAII src_file_fd = FileReaderRAII(BLI_open(src_blend_path, O_BINARY | O_RDONLY, 0));
if (!src_file_fd.good()) {
return BT_FILE_ERR;
}
/* Thumbnail reading is responsible for freeing `file` and closing `src_file`. */
FileReader *file_content = BLI_filereader_new_file(src_file_fd.get());
if (file_content == nullptr) {
return BT_FILE_ERR;
}
/* Extract thumbnail from file. */
Thumbnail thumb;
err = blendthumb_create_thumb_from_file(file_content, &thumb);
if (err != BT_OK) {
return err;
}
return err;
}
@implementation PreviewViewController
- (NSString *)nibName
{
return @"PreviewViewController";
}
- (void)loadView
{
[super loadView];
// Do any additional setup after loading the view.
}
/*
* Implement this method and set QLSupportsSearchableItems to YES in the Info.plist of the
extension if you support CoreSpotlight.
*
- (void)preparePreviewOfSearchableItemWithIdentifier:(NSString *)identifier queryString:(NSString
*)queryString completionHandler:(void (^)(NSError * _Nullable))handler {
// Perform any setup necessary in order to prepare the view.
// Call the completion handler so Quick Look knows that the preview is fully loaded.
// Quick Look will display a loading spinner while the completion handler is not called.
handler(nil);
}
*/
- (void)preparePreviewOfFileAtURL:(NSURL *)url
completionHandler:(void (^)(NSError *_Nullable))handler
{
// Add the supported content types to the QLSupportedContentTypes array in the Info.plist of the
// extension.
// Perform any setup necessary in order to prepare the view.
// Call the completion handler so Quick Look knows that the preview is fully loaded.
// Quick Look will display a loading spinner while the completion handler is not called.
NSLog(@"blender preparePreviewOfFileAtURL: %@", url);
std::ofstream file("/Users/ankit.kumar/apps/build_xcode/foobar.txt",
std::ios::out | std::ios::trunc | std::ios::binary);
file << "Hello World";
file.close();
std::cout << "Hello World" << std::endl;
// set image to file path /Users/ankit.kumar/Desktop/Screenshot 2021-09-02 at 1.59.47 PM.png
[[[self view] imageRepresentation]
setFileURL:[NSURL fileURLWithPath:@"file:///Users/ankit.kumar/Desktop/Screenshot.png"
isDirectory:NO]];
[[self view] display];
handler(nil);
}
@end

View File

@ -1352,8 +1352,14 @@ elseif(APPLE)
if(WITH_BLENDER_THUMBNAILER)
install(
TARGETS blender-thumbnailer
DESTINATION Blender.app/Contents/MacOS/
DESTINATION Blender.app/Contents/Plugins
)
get_target_property(XIB_RESOURCE blender-thumbnailer XIB_RESOURCE)
install_dir(
${XIB_RESOURCE}
Blender.app/Contents/Plugins/blender-thumbnailer.appex/Contents/Resources
)
endif()
if(PLATFORM_BUNDLED_LIBRARIES AND TARGETDIR_LIB)

View File

@ -34,7 +34,6 @@ endif()
blender_src_gtest_ex(
NAME blender
SRC "${SRC}"
EXTRA_LIBS "${TEST_LIBS}"
SKIP_ADD_TEST
)
setup_platform_linker_libs(blender_test)