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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* HBPreviewView.m
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "HBPreviewView.h"
// the white border around the preview image
#define BORDER_SIZE 2.0
@interface HBPreviewView ()
@property (nonatomic) CALayer *backLayer;
@property (nonatomic) CALayer *pictureLayer;
@property (nonatomic, readwrite) CGFloat scale;
@property (nonatomic, readwrite) NSRect pictureFrame;
@end
@implementation HBPreviewView
- (instancetype)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
[self setUp];
}
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self)
{
[self setUp];
}
return self;
}
/**
* Setups the sublayers,
* called by every initializer.
*/
- (void)setUp
{
// Make it a layer hosting view
self.layer = [CALayer new];
self.wantsLayer = YES;
_backLayer = [CALayer layer];
[_backLayer setBounds:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)];
CGColorRef white = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0);
[_backLayer setBackgroundColor: white];
CFRelease(white);
[_backLayer setShadowOpacity:0.5f];
[_backLayer setShadowOffset:CGSizeMake(0, 0)];
[_backLayer setAnchorPoint:CGPointMake(0, 0)];
_backLayer.opaque = YES;
_pictureLayer = [CALayer layer];
[_pictureLayer setBounds:CGRectMake(0.0, 0.0, self.frame.size.width - (BORDER_SIZE * 2), self.frame.size.height - (BORDER_SIZE * 2))];
[_pictureLayer setAnchorPoint:CGPointMake(0, 0)];
_pictureLayer.opaque = YES;
// Disable fade on contents change.
NSMutableDictionary *actions = [NSMutableDictionary dictionary];
if (_pictureLayer.actions)
{
[actions addEntriesFromDictionary:_pictureLayer.actions];
}
actions[@"contents"] = [NSNull null];
_pictureLayer.actions = actions;
[self.layer addSublayer:_backLayer];
[self.layer addSublayer:_pictureLayer];
_pictureLayer.hidden = YES;
_backLayer.hidden = YES;
_showBorder = YES;
_scale = 1;
_pictureFrame = _pictureLayer.frame;
}
- (void)setImage:(CGImageRef)image
{
_image = image;
self.pictureLayer.contents = (__bridge id)(image);
// Hide the layers if there is no image
BOOL hidden = _image == nil ? YES : NO;
self.pictureLayer.hidden = hidden ;
self.backLayer.hidden = hidden || !self.showBorder;
[self _updatePreviewLayout];
}
- (void)setFitToView:(BOOL)fitToView
{
_fitToView = fitToView;
[self _updatePreviewLayout];
}
- (void)setShowBorder:(BOOL)showBorder
{
_showBorder = showBorder;
self.backLayer.hidden = !showBorder;
[self _updatePreviewLayout];
}
- (void)setShowShadow:(BOOL)showShadow
{
_backLayer.shadowOpacity = showShadow ? 0.5f : 0;
}
- (void)setFrame:(NSRect)newRect {
// A change in size has required the view to be invalidated.
if ([self inLiveResize]) {
[super setFrame:newRect];
}
else {
[super setFrame:newRect];
}
[self _updatePreviewLayout];
}
- (NSSize)scaledSize:(NSSize)source toFit:(NSSize)destination
{
NSSize result;
CGFloat sourceAspectRatio = source.width / source.height;
CGFloat destinationAspectRatio = destination.width / destination.height;
// Source is larger than screen in one or more dimensions
if (sourceAspectRatio > destinationAspectRatio)
{
// Source aspect wider than screen aspect, snap to max width and vary height
result.width = destination.width;
result.height = result.width / sourceAspectRatio;
}
else
{
// Source aspect narrower than screen aspect, snap to max height vary width
result.height = destination.height;
result.width = result.height * sourceAspectRatio;
}
return result;
}
/**
* Updates the sublayers layout.
*/
- (void)_updatePreviewLayout
{
// Set the picture size display fields below the Preview Picture
NSSize imageSize = NSMakeSize(CGImageGetWidth(self.image), CGImageGetHeight(self.image));
CGFloat backingScaleFactor = 1.0;
if (imageSize.width > 0 && imageSize.height > 0) {
if (self.window)
{
backingScaleFactor = self.window.backingScaleFactor;
}
// HiDPI mode usually display everything
// with douple pixel count, but we don't
// want to double the size of the video
NSSize imageScaledSize = NSMakeSize(imageSize.width / backingScaleFactor, imageSize.height / backingScaleFactor);
NSSize frameSize = self.frame.size;
if (self.showBorder == YES)
{
frameSize.width -= BORDER_SIZE * 2;
frameSize.height -= BORDER_SIZE * 2;
}
if (self.fitToView == YES)
{
// We are in Fit to View mode so, we have to get the ratio for height and width against the window
// size so we can scale from there.
imageScaledSize = [self scaledSize:imageScaledSize toFit:frameSize];
}
else
{
// If the image is larger then the view, scale the image
if (imageScaledSize.width > frameSize.width || imageScaledSize.height > frameSize.height)
{
imageScaledSize = [self scaledSize:imageScaledSize toFit:frameSize];
}
}
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0];
// Resize the CALayers
CGRect backRect = CGRectMake(0, 0, imageScaledSize.width + (BORDER_SIZE * 2), imageScaledSize.height + (BORDER_SIZE * 2));
CGRect pictureRect = CGRectMake(0, 0, imageScaledSize.width, imageScaledSize.height);
backRect = CGRectIntegral(backRect);
pictureRect = CGRectIntegral(pictureRect);
self.backLayer.bounds = backRect;
self.pictureLayer.bounds = pictureRect;
// Position the CALayers
CGPoint anchor = CGPointMake(floor((self.frame.size.width - pictureRect.size.width) / 2),
floor((self.frame.size.height - pictureRect.size.height) / 2));
[self.pictureLayer setPosition:anchor];
CGPoint backAchor = CGPointMake(anchor.x - BORDER_SIZE, anchor.y - BORDER_SIZE);
[self.backLayer setPosition:backAchor];
[NSAnimationContext endGrouping];
// Update the proprierties
self.scale = self.pictureLayer.frame.size.width / imageSize.width * backingScaleFactor;
self.pictureFrame = self.pictureLayer.frame;
}
}
/**
* Given the size of the preview image to be shown, returns the best possible
* size for the view.
*/
- (NSSize)optimalViewSizeForImageSize:(NSSize)imageSize minSize:(NSSize)minSize
{
if (self.window.backingScaleFactor != 1.0)
{
// HiDPI mode usually display everything
// with douple pixel count, but we don't
// want to double the size of the video
imageSize.height /= self.window.backingScaleFactor;
imageSize.width /= self.window.backingScaleFactor;
}
NSSize screenSize = self.window.screen.visibleFrame.size;
CGFloat maxWidth = screenSize.width;
CGFloat maxHeight = screenSize.height;
NSSize resultSize = imageSize;
if (resultSize.width > maxWidth || resultSize.height > maxHeight)
{
resultSize = [self scaledSize:resultSize toFit:screenSize];
}
// If necessary, grow to minimum dimensions to ensure controls overlay is not obstructed
if (resultSize.width < minSize.width)
{
resultSize.width = minSize.width;
}
if (resultSize.height < minSize.height)
{
resultSize.height = minSize.height;
}
// Add the border
if (self.showBorder)
{
resultSize.width += BORDER_SIZE * 2;
resultSize.height += BORDER_SIZE * 2;
}
resultSize.width = floor(resultSize.width);
resultSize.height = floor(resultSize.height);
return resultSize;
}
@end
|