summaryrefslogtreecommitdiffstats
path: root/macosx/HBPreviewView.m
blob: 4e7d81ae126bd5aeedc33388ce6aeb907d3733e3 (plain)
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
/* 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"
#import <QuartzCore/QuartzCore.h>

// the white border around the preview image
#define BORDER_SIZE 2.0

@interface HBPreviewView ()

@property (nonatomic) CALayer *backLayer;
@property (nonatomic) CALayer *pictureLayer;

@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.bounds = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);
    _backLayer.backgroundColor = NSColor.whiteColor.CGColor;
    _backLayer.shadowOpacity = 0.5f;
    _backLayer.shadowOffset = CGSizeZero;
    _backLayer.anchorPoint = CGPointZero;
    _backLayer.opaque = YES;

    _pictureLayer = [CALayer layer];
    _pictureLayer.bounds = CGRectMake(0.0, 0.0, self.frame.size.width - (BORDER_SIZE * 2), self.frame.size.height - (BORDER_SIZE * 2));
    _pictureLayer.anchorPoint = CGPointZero;
    _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;
}

- (void)viewDidChangeBackingProperties
{
    if (self.window)
    {
        self.needsLayout = YES;
    }
}

- (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.needsLayout = YES;
}

- (void)setFitToView:(BOOL)fitToView
{
    _fitToView = fitToView;
    self.needsLayout = YES;
}

- (void)setShowBorder:(BOOL)showBorder
{
    _showBorder = showBorder;
    self.backLayer.hidden = !showBorder;
    self.needsLayout = YES;
}

- (void)setShowShadow:(BOOL)showShadow
{
    _backLayer.shadowOpacity = showShadow ? 0.5f : 0;
}

- (CGFloat)scale
{
    if (self.image)
    {
        NSSize imageSize = NSMakeSize(CGImageGetWidth(self.image), CGImageGetHeight(self.image));
        CGFloat backingScaleFactor = self.window.backingScaleFactor;
        CGFloat borderSize = self.showBorder ? BORDER_SIZE : 0;

        NSSize imageScaledSize = [self imageScaledSize:imageSize toFit:self.frame.size borderSize:borderSize scaleFactor:self.window.backingScaleFactor];

        return (imageScaledSize.width - borderSize * 2) / imageSize.width * backingScaleFactor;
    }
    else
    {
        return 1;
    }
}

- (CGRect)pictureFrame
{
    return self.pictureLayer.frame;
}

- (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;
}

- (NSSize)imageScaledSize:(NSSize)source toFit:(NSSize)destination borderSize:(CGFloat)borderSize scaleFactor:(CGFloat)scaleFactor
{
    // HiDPI mode usually display everything
    // with double pixel count, but we don't
    // want to double the size of the video
    NSSize scaledSource = NSMakeSize(source.width / scaleFactor, source.height / scaleFactor);

    scaledSource.width += borderSize * 2;
    scaledSource.height += borderSize * 2;

    if (self.fitToView == YES || scaledSource.width > destination.width || scaledSource.height > destination.height)
    {
        // If the image is larger then the view or if we are in Fit to View mode, scale the image
        scaledSource = [self scaledSize:source toFit:destination];
    }

    return scaledSource;
}

- (void)layout
{
    // Set the picture size display fields below the Preview Picture
    NSSize imageSize = NSMakeSize(CGImageGetWidth(self.image), CGImageGetHeight(self.image));

    if (imageSize.width > 0 && imageSize.height > 0)
    {
        CGFloat borderSize = self.showBorder ? BORDER_SIZE : 0;
        NSSize frameSize = self.frame.size;

        NSSize imageScaledSize = [self imageScaledSize:imageSize
                                                 toFit:frameSize
                                            borderSize:borderSize
                                           scaleFactor:self.window.backingScaleFactor];

        [CATransaction begin];
        CATransaction.disableActions = YES;

        CGFloat width = imageScaledSize.width;
        CGFloat height = imageScaledSize.height;

        CGFloat offsetX = (frameSize.width - width) / 2;
        CGFloat offsetY = (frameSize.height - height) / 2;

        NSRect alignedRect = [self backingAlignedRect:NSMakeRect(offsetX, offsetY, width, height) options:NSAlignAllEdgesNearest];

        self.backLayer.frame = alignedRect;
        self.pictureLayer.frame = NSInsetRect(alignedRect, borderSize, borderSize);

        [CATransaction commit];
    }
}

/**
 * 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 scaleFactor:(CGFloat)scaleFactor
{
    NSSize resultSize = [self imageScaledSize:imageSize
                                        toFit:self.window.screen.visibleFrame.size
                                   borderSize:self.showBorder ? BORDER_SIZE : 0
                                  scaleFactor:scaleFactor];

    // 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;
    }

    NSRect alignedRect = [self backingAlignedRect:NSMakeRect(0, 0, resultSize.width, resultSize.height)
                                          options:NSAlignAllEdgesNearest];

    return alignedRect.size;
}

#pragma mark - Accessibility

- (BOOL)isAccessibilityElement
{
    return YES;
}

- (NSString *)accessibilityRole
{
    return NSAccessibilityImageRole;
}

- (NSString *)accessibilityLabel
{
    if (self.image)
    {
        return [NSString stringWithFormat:NSLocalizedString(@"Preview Image, Size: %zu x %zu, Scale: %.0f%%", @"Preview -> accessibility label"), CGImageGetWidth(self.image), CGImageGetHeight(self.image), self.scale * 100];
    }
    return NSLocalizedString(@"No image", @"Preview -> accessibility label");
}

@end