summaryrefslogtreecommitdiffstats
path: root/macosx/HBPreviewViewController.m
blob: 2890d3ecd194248d751f4ef1376b2d67fd76472e (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
//
//  HBPreviewViewController.m
//  HandBrake
//
//  Created by Damiano Galassi on 14/12/2017.
//

#import <QuartzCore/QuartzCore.h>
#import "HBPreviewViewController.h"

#import "HBPreviewView.h"
#import "HBPreviewGenerator.h"
#import "HBPreviewController.h"

@interface HBPreviewViewController ()

@property (nonatomic, strong) IBOutlet HBPreviewView *previewView;

@property (nonatomic, strong) IBOutlet NSView *hud;

@property (nonatomic) NSInteger selectedIndex;
@property (nonatomic) BOOL visible;

@property (nonatomic) NSTimer *hudTimer;
@property (nonatomic) BOOL mouseInView;

@end

@implementation HBPreviewViewController

- (instancetype)init
{
    self = [super initWithNibName:@"HBPreviewViewController" bundle:nil];
    if (self)
    {
        _selectedIndex = 1;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.visible = YES;
    self.previewView.showShadow = NO;

    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.view.frame
                                                                options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingInVisibleRect | NSTrackingActiveAlways
                                                                  owner:self
                                                               userInfo:nil];
    [self.view addTrackingArea:trackingArea];
    self.hud.hidden = YES;
    self.hud.layer.opacity = 0;
}

- (void)viewWillAppear
{
    self.visible = YES;
    [self updatePicture];
}

- (void)viewDidDisappear
{
    self.visible = NO;
}

- (void)setGenerator:(HBPreviewGenerator *)generator
{
    _generator = generator;
    if (generator)
    {
        self.selectedIndex = self.selectedIndex;
        [self updatePicture];
    }
    else
    {
        self.previewView.image = nil;
    }
}

- (void)update
{
    [self updatePicture];
}

#pragma mark - HUD

- (void)mouseEntered:(NSEvent *)theEvent
{
    if (self.generator)
    {
        [self showHudWithAnimation:self.hud];
    }
    self.mouseInView = YES;
}

- (void)mouseMoved:(NSEvent *)theEvent
{
    [super mouseMoved:theEvent];

    // Test for mouse location to show/hide hud controls
    if (self.generator && self.mouseInView)
    {
        [self showHudWithAnimation:self.hud];
    }
}

- (void)mouseExited:(NSEvent *)theEvent
{
    [self hideHudWithAnimation:self.hud];
    self.mouseInView = NO;
}

#define ANIMATION_DUR 0.15

- (void)showHudWithAnimation:(NSView *)hud
{
    // The standard view animator doesn't play
    // nicely with the Yosemite visual effects yet.
    // So let's do the fade ourself.
    if (hud.layer.opacity == 0 || hud.isHidden)
    {
        hud.hidden = NO;

        [CATransaction begin];
        CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadeInAnimation.fromValue = @(hud.layer.presentationLayer.opacity);
        fadeInAnimation.toValue = @(1.0);
        fadeInAnimation.beginTime = 0.0;
        fadeInAnimation.duration = ANIMATION_DUR;

        [hud.layer addAnimation:fadeInAnimation forKey:nil];
        [hud.layer setOpacity:1];

        [CATransaction commit];
    }
}

- (void)hideHudWithAnimation:(NSView *)hud
{
    if (hud.layer.opacity != 0)
    {
        [CATransaction begin];
        CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadeOutAnimation.fromValue = @(hud.layer.presentationLayer.opacity);
        fadeOutAnimation.toValue = @(0.0);
        fadeOutAnimation.beginTime = 0.0;
        fadeOutAnimation.duration = ANIMATION_DUR;

        [hud.layer addAnimation:fadeOutAnimation forKey:nil];
        [hud.layer setOpacity:0];

        [CATransaction commit];
    }
}

#pragma mark - Preview index

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    NSInteger count = self.generator.imagesCount;
    if (selectedIndex >= count)
    {
        selectedIndex = count -1;
    }
    else if (selectedIndex < 0)
    {
        selectedIndex = 0;
    }
    _selectedIndex = selectedIndex;
}

- (IBAction)next:(id)sender
{
    self.selectedIndex += 1;
    [self updatePicture];
}

- (IBAction)previous:(id)sender
{
    self.selectedIndex -= 1;
    [self updatePicture];
}

- (void)updatePicture
{
    if (self.generator && self.visible)
    {
        CGImageRef fPreviewImage = [self.generator copyImageAtIndex:self.selectedIndex shouldCache:YES];
        self.previewView.image = fPreviewImage;
        CFRelease(fPreviewImage);
    }
}

- (void)scrollWheel:(NSEvent *)theEvent
{
    if (theEvent.deltaY < 0)
    {
        self.selectedIndex += 1;
        [self updatePicture];
    }
    else if (theEvent.deltaY > 0)
    {
        self.selectedIndex -= 1;
        [self updatePicture];
    }
}

@end