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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/* HBPictureHUDController.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 "HBPictureHUDController.h"
#import "HBThumbnailItemView.h"
@interface HBPictureHUDController ()
@property (nonatomic, weak) IBOutlet NSTextField *scaleLabel;
@property (nonatomic, weak) IBOutlet NSTextField *infoLabel;
@property (nonatomic, weak) IBOutlet NSSlider *slider;
@property (nonatomic, weak) IBOutlet NSPopUpButton *durationPopUp;
@property (nonatomic, weak) IBOutlet NSButton *scaleToScreenButton;
@property (nonatomic, weak) IBOutlet NSTextField *durationLabel;
@property (nonatomic, weak) IBOutlet NSTextField *durationUnitLabel;
@property (nonatomic) BOOL fitToView;
@property (nonatomic) BOOL ignoreUpdates;
@end
@interface HBPictureHUDController (TouchBar) <NSTouchBarProvider, NSTouchBarDelegate, NSScrubberDataSource, NSScrubberDelegate>
- (void)_touchBar_reloadScrubberData;
- (void)_touchBar_updateScrubberSelectedIndex:(NSUInteger)selectedIndex;
- (void)_touchBar_updateFitToView:(BOOL)fitToView;
- (void)_touchBar_validateUserInterfaceItems;
@end
@implementation HBPictureHUDController
- (NSString *)nibName
{
return @"HBPictureHUDController";
}
- (void)viewDidLoad
{
[super viewDidLoad];
// we set the preview length popup in seconds
[self.durationPopUp removeAllItems];
[self.durationPopUp addItemsWithTitles:@[@"15", @"30", @"45", @"60", @"90",
@"120", @"150", @"180", @"210", @"240"]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewLength"])
{
[self.durationPopUp selectItemWithTitle:[[NSUserDefaults standardUserDefaults]
objectForKey:@"PreviewLength"]];
}
if (!self.durationPopUp.selectedItem)
{
// currently hard set default to 15 seconds
[self.durationPopUp selectItemAtIndex:0];
}
}
- (BOOL)canBeHidden
{
return YES;
}
- (void)setGenerator:(HBPreviewGenerator *)generator
{
_generator = generator;
NSUInteger imagesCount = generator.imagesCount;
if (imagesCount > 0)
{
self.slider.numberOfTickMarks = imagesCount;
self.slider.maxValue = imagesCount - 1;
if (self.selectedIndex > imagesCount)
{
self.selectedIndex = imagesCount - 1;
}
}
if (@available(macOS 10.12.2, *))
{
[self _touchBar_reloadScrubberData];
[self _touchBar_validateUserInterfaceItems];
}
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
_selectedIndex = selectedIndex;
self.slider.integerValue = selectedIndex;
if (@available(macOS 10.12.2, *))
{
[self _touchBar_updateScrubberSelectedIndex:selectedIndex];
}
[self.delegate displayPreviewAtIndex:self.selectedIndex];
}
- (void)setInfo:(NSString *)info
{
self.infoLabel.stringValue = info;
}
- (void)setScale:(NSString *)scale
{
self.scaleLabel.stringValue = scale;
}
- (void)setFitToView:(BOOL)fitToView
{
_fitToView = fitToView;
if (fitToView == NO)
{
self.scaleToScreenButton.title = NSLocalizedString(@"Scale To Screen", @"Picture HUD -> scale button");
}
else
{
self.scaleToScreenButton.title = NSLocalizedString(@"Actual Scale", @"Picture HUD -> scale button");
}
if (@available(macOS 10.12.2, *))
{
[self _touchBar_updateFitToView:fitToView];
}
}
- (BOOL)validateUserIterfaceItemForAction:(SEL)action
{
if (action == @selector(createMoviePreview:) || action == @selector(toggleScaleToScreen:))
{
return self.generator != nil;
}
return YES;
}
- (IBAction)previewDurationPopUpChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:self.durationPopUp.titleOfSelectedItem forKey:@"PreviewLength"];
}
- (IBAction)pictureSliderChanged:(id)sender
{
NSUInteger index = self.slider.integerValue;
self.selectedIndex = index;
}
- (IBAction)toggleScaleToScreen:(id)sender
{
[self.delegate toggleScaleToScreen];
self.fitToView = !self.fitToView;
}
- (IBAction)showPictureSettings:(id)sender
{
[self.delegate showPictureSettings];
}
- (IBAction)createMoviePreview:(id)sender
{
[self.delegate createMoviePreviewWithPictureIndex:self.selectedIndex duration:self.durationPopUp.titleOfSelectedItem.intValue];
}
- (BOOL)HB_keyDown:(NSEvent *)event
{
unichar key = [event.charactersIgnoringModifiers characterAtIndex:0];
if (key == NSLeftArrowFunctionKey)
{
self.ignoreUpdates = YES;
self.selectedIndex = self.selectedIndex > 0 ? self.selectedIndex - 1 : self.selectedIndex;
return YES;
}
else if (key == NSRightArrowFunctionKey)
{
self.ignoreUpdates = YES;
self.selectedIndex = self.selectedIndex < self.generator.imagesCount - 1 ? self.selectedIndex + 1 : self.selectedIndex;
return YES;
}
else
{
return NO;
}
}
- (BOOL)HB_scrollWheel:(NSEvent *)theEvent
{
if (theEvent.deltaY < 0)
{
self.selectedIndex = self.selectedIndex < self.generator.imagesCount - 1 ? self.selectedIndex + 1 : self.selectedIndex;
}
else if (theEvent.deltaY > 0)
{
self.selectedIndex = self.selectedIndex > 0 ? self.selectedIndex - 1 : self.selectedIndex;
}
return YES;
}
@end
@implementation HBPictureHUDController (TouchBar)
#pragma mark - NSScrubberDataSource
NSString *thumbnailScrubberItemIdentifier = @"thumbnailItem";
- (NSInteger)numberOfItemsForScrubber:(NSScrubber *)scrubber
{
return self.generator.imagesCount;
}
- (NSScrubberItemView *)scrubber:(NSScrubber *)scrubber viewForItemAtIndex:(NSInteger)index
{
HBThumbnailItemView *itemView = [scrubber makeItemWithIdentifier:thumbnailScrubberItemIdentifier owner:nil];
itemView.generator = self.generator;
itemView.thumbnailIndex = index;
return itemView;
}
#pragma mark - NSScrubberFlowLayoutDelegate
// Scrubber is asking for the size for a particular item.
- (NSSize)scrubber:(NSScrubber *)scrubber layout:(NSScrubberFlowLayout *)layout sizeForItemAtIndex:(NSInteger)itemIndex
{
NSInteger val = 50;
return NSMakeSize(val, 30);
}
#pragma mark - NSScrubberDelegate
- (void)scrubber:(NSScrubber *)scrubber didSelectItemAtIndex:(NSInteger)selectedIndex
{
if (self.selectedIndex != selectedIndex && self.ignoreUpdates == NO)
{
self.selectedIndex = selectedIndex;
}
self.ignoreUpdates = NO;
}
#pragma mark - NSTouchBar
static NSTouchBarItemIdentifier HBTouchBarMain = @"fr.handbrake.previewWindowTouchBar";
static NSTouchBarItemIdentifier HBTouchBarRip = @"fr.handbrake.rip";
static NSTouchBarItemIdentifier HBTouchBarScrubber = @"fr.handbrake.scrubber";
static NSTouchBarItemIdentifier HBTouchBarFitToScreen = @"fr.handbrake.fitToScreen";
@dynamic touchBar;
- (NSTouchBar *)makeTouchBar
{
NSTouchBar *bar = [[NSTouchBar alloc] init];
bar.delegate = self;
bar.defaultItemIdentifiers = @[HBTouchBarRip, NSTouchBarItemIdentifierFlexibleSpace, HBTouchBarScrubber, NSTouchBarItemIdentifierFlexibleSpace, HBTouchBarFitToScreen];
bar.customizationIdentifier = HBTouchBarMain;
bar.customizationAllowedItemIdentifiers = @[HBTouchBarRip, HBTouchBarScrubber, HBTouchBarFitToScreen, NSTouchBarItemIdentifierFlexibleSpace];
return bar;
}
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
if ([identifier isEqualTo:HBTouchBarRip])
{
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
item.customizationLabel = NSLocalizedString(@"Live Preview", @"Touch bar");
NSButton *button = [NSButton buttonWithImage:[NSImage imageNamed:NSImageNameTouchBarPlayTemplate]
target:self action:@selector(createMoviePreview:)];
item.view = button;
return item;
}
else if ([identifier isEqualTo:HBTouchBarScrubber])
{
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
item.customizationLabel = NSLocalizedString(@"Previews", @"Touch bar");
NSScrubber *scrubber = [[NSScrubber alloc] init];
scrubber.delegate = self;
scrubber.dataSource = self;
[scrubber registerClass:[HBThumbnailItemView class] forItemIdentifier:thumbnailScrubberItemIdentifier];
NSScrubberLayout *scrubberLayout = [[NSScrubberFlowLayout alloc] init];
scrubber.scrubberLayout = scrubberLayout;
scrubber.showsAdditionalContentIndicators = YES;
scrubber.selectedIndex = 0;
scrubber.selectionOverlayStyle = [NSScrubberSelectionStyle outlineOverlayStyle];
scrubber.continuous = YES;
scrubber.mode = NSScrubberModeFree;
scrubber.itemAlignment = NSScrubberAlignmentCenter;
// Set the layout constraints on this scrubber so that it's 400 pixels wide.
NSDictionary *items = NSDictionaryOfVariableBindings(scrubber);
NSArray *theConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[scrubber(500)]" options:0 metrics:nil views:items];
[NSLayoutConstraint activateConstraints:theConstraints];
item.view = scrubber;
return item;
}
else if ([identifier isEqualTo:HBTouchBarFitToScreen])
{
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
item.customizationLabel = NSLocalizedString(@"Scale To Screen", @"Touch bar");
NSButton *button = [NSButton buttonWithImage:[NSImage imageNamed:NSImageNameTouchBarEnterFullScreenTemplate]
target:self action:@selector(toggleScaleToScreen:)];
item.view = button;
return item;
}
return nil;
}
- (void)_touchBar_reloadScrubberData
{
NSScrubber *scrubber = (NSScrubber *)[[self.touchBar itemForIdentifier:HBTouchBarScrubber] view];
[scrubber reloadData];
if (self.selectedIndex < scrubber.numberOfItems)
{
scrubber.animator.selectedIndex = self.selectedIndex;
}
}
- (void)_touchBar_updateScrubberSelectedIndex:(NSUInteger)selectedIndex
{
NSScrubber *scrubber = (NSScrubber *)[[self.touchBar itemForIdentifier:HBTouchBarScrubber] view];
scrubber.animator.selectedIndex = selectedIndex;
}
- (void)_touchBar_updateFitToView:(BOOL)fitToView
{
NSButton *button = (NSButton *)[[self.touchBar itemForIdentifier:HBTouchBarFitToScreen] view];
if (fitToView == NO)
{
button.image = [NSImage imageNamed:NSImageNameTouchBarEnterFullScreenTemplate];
}
else
{
button.image = [NSImage imageNamed:NSImageNameTouchBarExitFullScreenTemplate];
}
}
- (void)_touchBar_validateUserInterfaceItems
{
for (NSTouchBarItemIdentifier identifier in self.touchBar.itemIdentifiers) {
NSTouchBarItem *item = [self.touchBar itemForIdentifier:identifier];
NSView *view = item.view;
if ([view isKindOfClass:[NSButton class]]) {
NSButton *button = (NSButton *)view;
BOOL enabled = [self validateUserIterfaceItemForAction:button.action];
button.enabled = enabled;
}
}
}
@end
|