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
|
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "player/client.h"
#import "macosx_touchbar.h"
@implementation TouchBar
@synthesize app = _app;
@synthesize touchbarItems = _touchbar_items;
@synthesize duration = _duration;
@synthesize position = _position;
@synthesize pause = _pause;
- (id)init
{
if (self = [super init]) {
self.touchbarItems = @{
seekBar: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"slider",
@"name": @"Seek Bar",
@"cmd": @"seek %f absolute-percent"
}],
play: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Play Button",
@"cmd": @"cycle pause",
@"image": [NSImage imageNamed:NSImageNameTouchBarPauseTemplate],
@"imageAlt": [NSImage imageNamed:NSImageNameTouchBarPlayTemplate]
}],
previousItem: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Previous Playlist Item",
@"cmd": @"playlist-prev",
@"image": [NSImage imageNamed:NSImageNameTouchBarGoBackTemplate]
}],
nextItem: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Next Playlist Item",
@"cmd": @"playlist-next",
@"image": [NSImage imageNamed:NSImageNameTouchBarGoForwardTemplate]
}],
previousChapter: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Previous Chapter",
@"cmd": @"add chapter -1",
@"image": [NSImage imageNamed:NSImageNameTouchBarSkipBackTemplate]
}],
nextChapter: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Next Chapter",
@"cmd": @"add chapter 1",
@"image": [NSImage imageNamed:NSImageNameTouchBarSkipAheadTemplate]
}],
cycleAudio: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Cycle Audio",
@"cmd": @"cycle audio",
@"image": [NSImage imageNamed:NSImageNameTouchBarAudioInputTemplate]
}],
cycleSubtitle: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Cycle Subtitle",
@"cmd": @"cycle sub",
@"image": [NSImage imageNamed:NSImageNameTouchBarComposeTemplate]
}],
currentPosition: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"text",
@"name": @"Current Position"
}],
timeLeft: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"text",
@"name": @"Time Left"
}]
};
[self addObserver:self forKeyPath:@"visible" options:0 context:nil];
}
return self;
}
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"slider"]) {
NSCustomTouchBarItem *tbItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSSlider *slider = [NSSlider sliderWithTarget:self action:@selector(seekbarChanged:)];
slider.minValue = 0.0f;
slider.maxValue = 100.0f;
tbItem.view = slider;
tbItem.customizationLabel = self.touchbarItems[identifier][@"name"];
[self.touchbarItems[identifier] setObject:slider forKey:@"view"];
[self.touchbarItems[identifier] setObject:tbItem forKey:@"tbItem"];
[tbItem addObserver:self forKeyPath:@"visible" options:0 context:nil];
return tbItem;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"button"]) {
NSCustomTouchBarItem *tbItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSImage *tbImage = self.touchbarItems[identifier][@"image"];
NSButton *tbButton = [NSButton buttonWithImage:tbImage target:self action:@selector(buttonAction:)];
tbItem.view = tbButton;
tbItem.customizationLabel = self.touchbarItems[identifier][@"name"];
[self.touchbarItems[identifier] setObject:tbButton forKey:@"view"];
[self.touchbarItems[identifier] setObject:tbItem forKey:@"tbItem"];
[tbItem addObserver:self forKeyPath:@"visible" options:0 context:nil];
return tbItem;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"text"]) {
NSCustomTouchBarItem *tbItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSTextField *tbText = [NSTextField labelWithString:@"0:00"];
tbText.alignment = NSTextAlignmentCenter;
tbItem.view = tbText;
tbItem.customizationLabel = self.touchbarItems[identifier][@"name"];
[self.touchbarItems[identifier] setObject:tbText forKey:@"view"];
[self.touchbarItems[identifier] setObject:tbItem forKey:@"tbItem"];
[tbItem addObserver:self forKeyPath:@"visible" options:0 context:nil];
return tbItem;
}
return nil;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
if ([keyPath isEqualToString:@"visible"]) {
NSNumber *visible = [object valueForKey:@"visible"];
if (visible.boolValue) {
[self updateTouchBarTimeItems];
[self updatePlayButton];
}
}
}
- (void)updateTouchBarTimeItems
{
if (!self.isVisible)
return;
[self updateSlider];
[self updateTimeLeft];
[self updateCurrentPosition];
}
- (void)updateSlider
{
NSCustomTouchBarItem *tbItem = self.touchbarItems[seekBar][@"tbItem"];
if (!tbItem.visible)
return;
NSSlider *seekSlider = self.touchbarItems[seekBar][@"view"];
if (self.duration <= 0) {
seekSlider.enabled = NO;
seekSlider.doubleValue = 0;
} else {
seekSlider.enabled = YES;
if (!seekSlider.highlighted)
seekSlider.doubleValue = (self.position/self.duration)*100;
}
}
- (void)updateTimeLeft
{
NSCustomTouchBarItem *tbItem = self.touchbarItems[timeLeft][@"tbItem"];
if (!tbItem.visible)
return;
NSTextField *timeLeftItem = self.touchbarItems[timeLeft][@"view"];
[self removeConstraintForIdentifier:timeLeft];
if (self.duration <= 0) {
timeLeftItem.stringValue = @"";
} else {
int left = (int)(floor(self.duration)-floor(self.position));
NSString *leftFormat = [self formatTime:left];
NSString *durFormat = [self formatTime:self.duration];
timeLeftItem.stringValue = [NSString stringWithFormat:@"-%@", leftFormat];
[self applyConstraintFromString:[NSString stringWithFormat:@"-%@", durFormat]
forIdentifier:timeLeft];
}
}
- (void)updateCurrentPosition
{
NSCustomTouchBarItem *tbItem = self.touchbarItems[currentPosition][@"tbItem"];
if (!tbItem.visible)
return;
NSTextField *curPosItem = self.touchbarItems[currentPosition][@"view"];
NSString *posFormat = [self formatTime:(int)floor(self.position)];
curPosItem.stringValue = posFormat;
[self removeConstraintForIdentifier:currentPosition];
if (self.duration <= 0) {
[self applyConstraintFromString:[self formatTime:self.position]
forIdentifier:currentPosition];
} else {
NSString *durFormat = [self formatTime:self.duration];
[self applyConstraintFromString:durFormat forIdentifier:currentPosition];
}
}
- (void)updatePlayButton
{
NSCustomTouchBarItem *tbItem = self.touchbarItems[play][@"tbItem"];
if (!self.isVisible || !tbItem.visible)
return;
NSButton *playButton = self.touchbarItems[play][@"view"];
if (self.pause) {
playButton.image = self.touchbarItems[play][@"imageAlt"];
} else {
playButton.image = self.touchbarItems[play][@"image"];
}
}
- (void)buttonAction:(NSButton *)sender
{
NSString *identifier = [self getIdentifierFromView:sender];
[self.app queueCommand:(char *)[self.touchbarItems[identifier][@"cmd"] UTF8String]];
}
- (void)seekbarChanged:(NSSlider *)slider
{
NSString *identifier = [self getIdentifierFromView:slider];
NSString *seek = [NSString stringWithFormat:
self.touchbarItems[identifier][@"cmd"], slider.doubleValue];
[self.app queueCommand:(char *)[seek UTF8String]];
}
- (NSString *)formatTime:(int)time
{
int seconds = time % 60;
int minutes = (time / 60) % 60;
int hours = time / (60 * 60);
NSString *stime = hours > 0 ? [NSString stringWithFormat:@"%d:", hours] : @"";
stime = (stime.length > 0 || minutes > 9) ?
[NSString stringWithFormat:@"%@%02d:", stime, minutes] :
[NSString stringWithFormat:@"%d:", minutes];
stime = [NSString stringWithFormat:@"%@%02d", stime, seconds];
return stime;
}
- (void)removeConstraintForIdentifier:(NSTouchBarItemIdentifier)identifier
{
NSTextField *field = self.touchbarItems[identifier][@"view"];
[field removeConstraint:self.touchbarItems[identifier][@"constrain"]];
}
- (void)applyConstraintFromString:(NSString *)string
forIdentifier:(NSTouchBarItemIdentifier)identifier
{
NSTextField *field = self.touchbarItems[identifier][@"view"];
if (field) {
NSString *fString = [[string componentsSeparatedByCharactersInSet:
[NSCharacterSet decimalDigitCharacterSet]] componentsJoinedByString:@"0"];
NSTextField *textField = [NSTextField labelWithString:fString];
NSSize size = [textField frame].size;
NSLayoutConstraint *con =
[NSLayoutConstraint constraintWithItem:field
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:(int)ceil(size.width*1.1)];
[field addConstraint:con];
[self.touchbarItems[identifier] setObject:con forKey:@"constrain"];
}
}
- (NSString *)getIdentifierFromView:(id)view
{
NSString *identifier;
for (identifier in self.touchbarItems)
if([self.touchbarItems[identifier][@"view"] isEqual:view])
break;
return identifier;
}
- (void)processEvent:(struct mpv_event *)event
{
switch (event->event_id) {
case MPV_EVENT_END_FILE: {
self.position = 0;
self.duration = 0;
break;
}
case MPV_EVENT_PROPERTY_CHANGE: {
[self handlePropertyChange:(mpv_event_property *)event->data];
break;
}
}
}
- (void)handlePropertyChange:(struct mpv_event_property *)property
{
NSString *name = [NSString stringWithUTF8String:property->name];
mpv_format format = property->format;
if ([name isEqualToString:@"time-pos"] && format == MPV_FORMAT_DOUBLE) {
double newPosition = *(double *)property->data;
newPosition = newPosition < 0 ? 0 : newPosition;
if ((int)(floor(newPosition) - floor(self.position)) != 0) {
self.position = newPosition;
[self updateTouchBarTimeItems];
}
} else if ([name isEqualToString:@"duration"] && format == MPV_FORMAT_DOUBLE) {
self.duration = *(double *)property->data;
[self updateTouchBarTimeItems];
} else if ([name isEqualToString:@"pause"] && format == MPV_FORMAT_FLAG) {
self.pause = *(int *)property->data;
[self updatePlayButton];
}
}
@end
|