-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYFileThumbnailView.m
131 lines (108 loc) · 4.16 KB
/
SSYFileThumbnailView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#import "SSYFileThumbnailView.h"
#import "Browser.h"
#import <QuickLook/QLThumbnailImage.h>
@implementation SSYFileThumbnailView
- (Browser*)browser {
Browser* browser = (Browser*)[[self window] windowController] ;
// As window is closing, browser will be nil.
if (browser) {
if (![browser respondsToSelector:@selector(selectedFileoid)]) {
NSLog(@"Internal Error 649-9283 %@", browser) ;
browser = nil ;
}
}
return browser ;
}
- (Fileoid*)fileoid {
return [[self browser] selectedFileoid] ;
}
- (void)awakeFromNib {
NSArrayController* fileoidController = [[self browser] fileoidController] ;
[fileoidController addObserver:self
forKeyPath:@"selectedObjects"
options:0
context:NULL] ;
}
- (void)dealloc {
[[[self browser] fileoidController] removeObserver:self
forKeyPath:@"selectedObjects"] ;
[super dealloc] ;
}
- (void)drawImage:(NSImage*)imageIn {
CGImageRef imageRef = (CGImageRef)imageIn ;
NSImage* image = nil ;
NSRect rect ;
rect.origin.x = 0 ;
rect.origin.y = 0 ;
rect.size.width = [self frame].size.width ;
rect.size.height = [self frame].size.height ;
if (imageRef) {
// The following kludge is because the QuickLook API (above) returns a
// CGImageRef.
// Maybe I could hand this off directly to an IKImageView ???
CGRect cgRect ;
cgRect.origin.x = 0 ;
cgRect.origin.y = 0 ;
cgRect.size.width = [self frame].size.width ;
cgRect.size.height = [self frame].size.height ;
image = [[NSImage alloc] initWithSize:rect.size] ;
[image lockFocus] ;
CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort],
cgRect,
imageRef) ;
[image unlockFocus] ;
}
else {
// QuickLook thumbnail is not available for this file.
// Use its icon instead.
image = [[[NSWorkspace sharedWorkspace] iconForFile:[[self fileoid] path]] retain] ;
[image setSize:rect.size] ;
}
[self setImage:image] ;
[image release] ;
}
- (void)updateImage {
Fileoid* fileoid = [self fileoid] ;
if (fileoid) {
NSString* path = [fileoid path] ;
NSURL* url = [NSURL fileURLWithPath:path] ;
NSRect frame = [self frame] ;
CGRect cgRect ;
cgRect.origin.x = 0 ;
cgRect.origin.y = 0 ;
cgRect.size.width = frame.size.width ;
cgRect.size.height = frame.size.height ;
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ;
dispatch_async(aQueue, ^{
// Before inserting the following dispatch_async, above, when this
// function ran a complaint would log in console from function
// QLError() stating that a thumbnail was being computed on the main
// thread and this could cause a nonresponsive user interface.
// Oddly, this would only happen with the preview pane (QLPreviewPane)
// was visible.
CGImageRef imageRef = QLThumbnailImageCreate(NULL,
(CFURLRef)url,
cgRect.size,
NULL) ;
[(id)imageRef autorelease] ;
// Must always update UI on main thread, so I'm told…
[self performSelectorOnMainThread:@selector(drawImage:)
withObject:(id)imageRef
waitUntilDone:NO] ;
} ) ;
}
else {
[self setImage:nil] ;
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
[self updateImage] ;
}
- (void)drawRect:(NSRect)dirtyRect {
[self updateImage] ;
[super drawRect:dirtyRect] ;
}
@end