summaryrefslogtreecommitdiffstats
path: root/macosx/HBPlayerHUDController.m
blob: 98ab96fa50e1e6ad6c78e1ffeb4c5abcaaf1b1fd (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*  HBPlayerHUDController.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 "HBPlayerHUDController.h"

@interface HBPlayerHUDController ()

@property (weak) IBOutlet NSButton *playButton;
@property (weak) IBOutlet NSSlider *slider;

@property (weak) IBOutlet NSSlider *volumeSlider;

@property (weak) IBOutlet NSTextField *currentTimeLabel;
@property (weak) IBOutlet NSTextField *remaingTimeLabel;

@property (weak) IBOutlet NSPopUpButton *tracksSelection;

@property (nonatomic, readonly) NSDictionary *monospacedAttr;

@property (nonatomic, readwrite) id rateObserver;
@property (nonatomic, readwrite) id periodicObserver;

@end

@implementation HBPlayerHUDController

- (NSString *)nibName
{
    return @"HBPlayerHUDController";
}

- (void)loadView
{
    [super loadView];

    if ([[NSFont class] respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
        _monospacedAttr = @{NSFontAttributeName: [NSFont monospacedDigitSystemFontOfSize:[NSFont smallSystemFontSize] weight:NSFontWeightRegular]};
    }
    else {
        _monospacedAttr = @{NSFontAttributeName: [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]};
    }
}

- (BOOL)canBeHidden
{
    return YES;
}

- (void)setPlayer:(id<HBPlayer>)player
{
    if (_player)
    {
        [self.player removeRateObserver:self.rateObserver];
        [self.player removeTimeObserver:self.periodicObserver];
        self.rateObserver = nil;
        self.periodicObserver = nil;
        [self _clearTracksMenu];
    }

    _player = player;

    if (player)
    {
        [self _buildTracksMenu];

        // 10.7 does not supports weak NSViewController,
        // so use self and disable the warning for now.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"

        self.periodicObserver = [self.player addPeriodicTimeObserverUsingBlock:^(NSTimeInterval time) {
            [self _refreshUI];
        }];

        self.rateObserver = [self.player addRateObserverUsingBlock:^{
            if (self.player.rate != 0.0)
            {
                self.playButton.image = [NSImage imageNamed:@"PauseTemplate"];
            }
            else
            {
                self.playButton.image = [NSImage imageNamed:@"PlayTemplate"];
            }
        }];

        [self.slider setMinValue:0.0];
        [self.slider setMaxValue:self.player.duration];
        [self.slider setDoubleValue:0.0];

        self.player.volume = self.volumeSlider.floatValue;

        [self.player play];
    }
}

- (void)dealloc
{
    if (_rateObserver)
    {
        [_player removeRateObserver:_rateObserver];
        _rateObserver = nil;
    }
    if (_periodicObserver)
    {
        [_player removeTimeObserver:_periodicObserver];
        _periodicObserver = nil;
    }
}

#pragma mark - Audio and subtitles selection menu

- (void)_buildTracksMenu
{
    [self _clearTracksMenu];

    NSArray<HBPlayerTrack *> *audioTracks = self.player.audioTracks;
    if (audioTracks.count)
    {
        [self _addSectionTitle:NSLocalizedString(@"Audio", nil)];
        [self _addTracksItemFromArray:audioTracks selector:@selector(enableAudioTrack:)];
    }

    NSArray<HBPlayerTrack *>  *subtitlesTracks = self.player.subtitlesTracks;
    if (subtitlesTracks.count)
    {
        if (audioTracks.count)
        {
            [self.tracksSelection.menu addItem:[NSMenuItem separatorItem]];
        }
        [self _addSectionTitle:NSLocalizedString(@"Subtitles", nil)];
        [self _addTracksItemFromArray:subtitlesTracks selector:@selector(enableSubtitlesTrack:)];
    }
}

- (void)_clearTracksMenu
{
    for (NSMenuItem *item in [self.tracksSelection.menu.itemArray copy])
    {
        if (item.tag != 1)
        {
            [self.tracksSelection.menu removeItem:item];
        }
    }
}

- (void)_addSectionTitle:(NSString *)title
{
    NSMenuItem *sectionTitle = [[NSMenuItem alloc] init];
    sectionTitle.title = title;
    sectionTitle.enabled = NO;
    sectionTitle.indentationLevel = 0;
    [self.tracksSelection.menu addItem:sectionTitle];
}

- (void)_addTracksItemFromArray:(NSArray<HBPlayerTrack *> *)tracks selector:(SEL)selector
{
    for (HBPlayerTrack *track in tracks)
    {
        NSMenuItem *item = [[NSMenuItem alloc] init];
        item.title = track.name;
        item.enabled = YES;
        item.indentationLevel = 1;
        item.action = selector;
        item.target = self;
        item.state = track.enabled;
        item.representedObject = track;
        [self.tracksSelection.menu addItem:item];
    }
}

- (void)_updateTracksMenuState
{
    for (NSMenuItem *item in self.tracksSelection.menu.itemArray)
    {
        if (item.representedObject)
        {
            HBPlayerTrack *track = (HBPlayerTrack *)item.representedObject;
            item.state = track.enabled;
        }
    }
}

- (IBAction)enableAudioTrack:(NSMenuItem *)sender
{
    [self.player enableAudioTrack:sender.representedObject];
    [self _updateTracksMenuState];
}

- (IBAction)enableSubtitlesTrack:(NSMenuItem *)sender
{
    [self.player enableSubtitlesTrack:sender.representedObject];
    [self _updateTracksMenuState];
}

- (NSString *)_timeToTimecode:(NSTimeInterval)timeInSeconds
{
    UInt16 seconds = (UInt16)fmod(timeInSeconds, 60.0);
    UInt16 minutes = (UInt16)fmod(timeInSeconds / 60.0, 60.0);
    UInt16 milliseconds = (UInt16)((timeInSeconds - (int) timeInSeconds) * 1000);

    return [NSString stringWithFormat:@"%02d:%02d.%03d", minutes, seconds, milliseconds];
}

- (NSAttributedString *)_monospacedString:(NSString *)string
{
    return [[NSAttributedString alloc] initWithString:string attributes:self.monospacedAttr];

}

- (void)_refreshUI
{
    if (self.player)
    {
        NSTimeInterval currentTime = self.player.currentTime;
        NSTimeInterval duration = self.player.duration;

        self.slider.doubleValue = currentTime;
        self.currentTimeLabel.attributedStringValue = [self _monospacedString:[self _timeToTimecode:currentTime]];
        self.remaingTimeLabel.attributedStringValue = [self _monospacedString:[self _timeToTimecode:duration - currentTime]];
    }
}

- (IBAction)playPauseToggle:(id)sender
{
    if (self.player.rate != 0.0)
    {
        [self.player pause];
    }
    else
    {
        [self.player play];
    }
}

- (IBAction)goToBeginning:(id)sender
{
    [self.player gotoBeginning];
}

- (IBAction)goToEnd:(id)sender
{
    [self.player gotoEnd];
}

- (IBAction)showPicturesPreview:(id)sender
{
    [self.delegate stopPlayer];
}

- (IBAction)sliderChanged:(NSSlider *)sender
{
    self.player.currentTime = sender.doubleValue;
}

- (IBAction)maxVolume:(id)sender
{
    self.volumeSlider.doubleValue = 1;
    self.player.volume = 1;
}

- (IBAction)mute:(id)sender
{
    self.volumeSlider.doubleValue = 0;
    self.player.volume = 0;
}

- (IBAction)volumeSliderChanged:(NSSlider *)sender
{
    self.player.volume = sender.floatValue;
}

#pragma mark - Keyboard and mouse wheel control

- (BOOL)HB_keyDown:(NSEvent *)event
{
    unichar key = [event.charactersIgnoringModifiers characterAtIndex:0];

    if (self.player)
    {
        if (key == 32)
        {
            if (self.player.rate != 0.0)
                [self.player pause];
            else
                [self.player play];
        }
        else if (key == 'k')
            [self.player pause];
        else if (key == 'l')
        {
            float rate = self.player.rate;
            rate += 1.0f;
            [self.player play];
            self.player.rate = rate;
        }
        else if (key == 'j')
        {
            float rate = self.player.rate;
            rate -= 1.0f;
            [self.player play];
            self.player.rate = rate;
        }
        else if (event.modifierFlags & NSAlternateKeyMask && key == NSLeftArrowFunctionKey)
        {
            [self.player gotoBeginning];
        }
        else if (event.modifierFlags & NSAlternateKeyMask && key == NSRightArrowFunctionKey)
        {
            [self.player gotoEnd];
        }
        else if (key == NSLeftArrowFunctionKey)
        {
            [self.player stepBackward];
        }
        else if (key == NSRightArrowFunctionKey)
        {
            [self.player stepForward];
        }
        else
        {
            return NO;
        }
    }
    else
    {
        return NO;
    }

    return YES;
}

- (BOOL)HB_scrollWheel:(NSEvent *)theEvent;
{
    if (theEvent.deltaY < 0)
    {
        self.player.currentTime = self.player.currentTime + 0.5;
    }
    else if (theEvent.deltaY > 0)
    {
        self.player.currentTime = self.player.currentTime - 0.5;
    }
    return YES;
}

@end