Set windows size of QuickLook Plugin

2019-03-27 12:39发布

问题:

I'm building a QuickLook plugin. I want to change the width of the windows that pops up when user hits the spacebar.

I've read there are two keys in the info.plist file of the project where height and width are customisable. Even if I change those values I can't get the size of the preview windows to my desired one.

I don't know what else to try. Any idea?

Thanks!

回答1:

Thought I'd dig a little on this. I have not tried any of the following suggestions, so nobody get their hopes up. I'll assume you're using the generator callback:

OSStatus (*GeneratePreviewForURL)(
    void *thisInterface,
    QLPreviewRequestRef preview,
    CFURLRef url,
    CFStringRef contentTypeUTI,
    CFDictionaryRef options
);

Before anything else, you might manually check the options dictionary argument and verify that the kQLPreviewPropertyWidthKey and kQLPreviewPropertyHeightKey keys are indeed mapped to the desired CFNumber values.

Referring to each of these properties, the Apple QuickLook programming guide says:

Note that this property is a hint; Quick Look might set the width automatically for some types of previews. The value must be encapsulated in a CFNumber object.

(Edit: If your preview representation is flexible, you might try finding a preview type for which QuickLook honors your size hints, as per the statement above. Just a thought.)

Running nm on the QuickLook framework binary revealed some undocumented kQLPreviewProperty-- constants as well as the aforementioned width and height keys. One that caught my attention was kQLPreviewPropertyAutoSizeKey. Recalling Apple's statement about ignoring the hints to set the size automatically, this might be significant? Following the convention in QuickLook.framework/Headers/QLBase.h, you might try declaring

extern const CFStringRef kQLPreviewPropertyAutoSizeKey;

Then you could try associating a CFNumber 0 with that property key in the options dictionary. There are other undocumented keys of note, such as kQLPreviewPropertyAttributesKey.

Back to the Info.plist you mentioned, Apple says about those keys QLPreviewWidth and QLPreviewHeight:

This number gives Quick Look a hint for the width (in points) of previews. It uses these values if the generator takes too long to produce the preview. (emphasis added)

This is where someone makes the terrible suggestion of calling sleep() in your generator. But I'm perplexed as to why Apple would make following the size hints dependent on the generator latency. (?)

Edit: Also note the above statement says the Info.plist hints must be expressed in points (not pixels), a unit dependent on the user's screen resolution.



回答2:

Recently I was developing a Quick Look Plugin myself which uses HTML+CSS and faced the same problem. The solution for my was to test the plugin not within Xcode and qlmanage as the executable but instead to try the real .qlgenerator from my user library.

When invoking the generator from my user library, the Quick Look window was resized exactly the way I specified in the *-Info.plist.