summaryrefslogtreecommitdiffstats
path: root/osdep/macosx_menubar.m
blob: 558a33a905c8d8bdd6bf237b4874a42c54446a5e (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
 * 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 "config.h"
#include "common/common.h"

#import "macosx_menubar_objc.h"
#import "osdep/macosx_application_objc.h"
#include "osdep/macosx_compat.h"

@implementation MenuBar
{
    NSArray *menuTree;
}

- (id)init
{
    if (self = [super init]) {
        NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
        [userDefaults setBool:NO forKey:@"NSFullScreenMenuItemEverywhere"];
        [userDefaults setBool:YES forKey:@"NSDisabledDictationMenuItem"];
        [userDefaults setBool:YES forKey:@"NSDisabledCharacterPaletteMenuItem"];

        if ([NSWindow respondsToSelector:@selector(setAllowsAutomaticWindowTabbing:)])
            [NSWindow setAllowsAutomaticWindowTabbing: NO];

        menuTree = @[
            @{
                @"name": @"Apple",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"About mpv",
                        @"action"     : @"about",
                        @"key"        : @"",
                        @"target"     : self
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Preferences…",
                        @"action"     : @"preferences:",
                        @"key"        : @",",
                        @"target"     : self,
                        @"file"       : @"mpv.conf",
                        @"alertTitle1": @"No Application found to open your config file.",
                        @"alertText1" : @"Please open the mpv.conf file with "
                                        "your preferred text editor in the now "
                                        "open folder to edit your config.",
                        @"alertTitle2": @"No config file found.",
                        @"alertText2" : @"Please create a mpv.conf file with your "
                                        "preferred text editor in the now open folder.",
                        @"alertTitle3": @"No config path or file found.",
                        @"alertText3" : @"Please create the following path ~/.config/mpv/ "
                                        "and a mpv.conf file within with your preferred "
                                        "text editor."
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Keyboard Shortcuts Config…",
                        @"action"     : @"preferences:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"file"       : @"input.conf",
                        @"alertTitle1": @"No Application found to open your config file.",
                        @"alertText1" : @"Please open the input.conf file with "
                                        "your preferred text editor in the now "
                                        "open folder to edit your config.",
                        @"alertTitle2": @"No config file found.",
                        @"alertText2" : @"Please create a input.conf file with your "
                                        "preferred text editor in the now open folder.",
                        @"alertTitle3": @"No config path or file found.",
                        @"alertText3" : @"Please create the following path ~/.config/mpv/ "
                                        "and a input.conf file within with your preferred "
                                        "text editor."
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Services",
                        @"key"        : @"",
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Hide mpv",
                        @"action"     : @"hide:",
                        @"key"        : @"h",
                        @"target"     : NSApp
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Hide Others",
                        @"action"     : @"hideOtherApplications:",
                        @"key"        : @"h",
                        @"modifiers"  : [NSNumber numberWithUnsignedInteger:
                                            NSEventModifierFlagCommand |
                                            NSEventModifierFlagOption],
                        @"target"     : NSApp
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Show All",
                        @"action"     : @"unhideAllApplications:",
                        @"key"        : @"",
                        @"target"     : NSApp
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Quit and Remember Position",
                        @"action"     : @"quit:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"quit-watch-later"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Quit mpv",
                        @"action"     : @"quit:",
                        @"key"        : @"q",
                        @"target"     : self,
                        @"cmd"        : @"quit"
                    }]
                ]
            },
            @{
                @"name": @"File",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Open File…",
                        @"action"     : @"openFile",
                        @"key"        : @"o",
                        @"target"     : self
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Open URL…",
                        @"action"     : @"openURL",
                        @"key"        : @"O",
                        @"target"     : self
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Close",
                        @"action"     : @"performClose:",
                        @"key"        : @"w"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Save Screenshot",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"async screenshot"
                    }]
                ]
            },
            @{
                @"name": @"Edit",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Undo",
                        @"action"     : @"undo:",
                        @"key"        : @"z"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Redo",
                        @"action"     : @"redo:",
                        @"key"        : @"Z"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Cut",
                        @"action"     : @"cut:",
                        @"key"        : @"x"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Copy",
                        @"action"     : @"copy:",
                        @"key"        : @"c"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Paste",
                        @"action"     : @"paste:",
                        @"key"        : @"v"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Select All",
                        @"action"     : @"selectAll:",
                        @"key"        : @"a"
                    }]
                ]
            },
            @{
                @"name": @"View",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Fullscreen",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle fullscreen"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Float on Top",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle ontop"
                    }],
#if HAVE_MACOS_TOUCHBAR
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Customize Touch Bar…",
                        @"action"     : @"toggleTouchBarCustomizationPalette:",
                        @"key"        : @"",
                        @"target"     : NSApp
                    }]
#endif
                ]
            },
            @{
                @"name": @"Video",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Zoom Out",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add panscan -0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Zoom In",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add panscan 0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Reset Zoom",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set panscan 0"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Aspect Ratio 4:3",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set video-aspect \"4:3\""
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Aspect Ratio 16:9",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set video-aspect \"16:9\""
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Aspect Ratio 1.85:1",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set video-aspect \"1.85:1\""
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Aspect Ratio 2.35:1",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set video-aspect \"2.35:1\""
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Reset Aspect Ratio",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set video-aspect \"-1\""
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Half Size",
                        @"key"        : @"0",
                        @"cmdSpecial" : [NSNumber numberWithInt:MPM_H_SIZE]
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Normal Size",
                        @"key"        : @"1",
                        @"cmdSpecial" : [NSNumber numberWithInt:MPM_N_SIZE]
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Double Size",
                        @"key"        : @"2",
                        @"cmdSpecial" : [NSNumber numberWithInt:MPM_D_SIZE]
                    }]
                ]
            },
            @{
                @"name": @"Audio",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Next Audio Track",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle audio"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Previous Audio Track",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle audio down"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Mute",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle mute"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Play Audio Later",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add audio-delay 0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Play Audio Earlier",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add audio-delay -0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Reset Audio Delay",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set audio-delay 0.0 "
                    }]
                ]
            },
            @{
                @"name": @"Subtitle",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Next Subtitle Track",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle sub"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Previous Subtitle Track",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle sub down"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Force Style",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle-values sub-ass-override \"force\" \"no\""
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Display Subtitles Later",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add sub-delay 0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Display Subtitles Earlier",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add sub-delay -0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Reset Subtitle Delay",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set sub-delay 0.0"
                    }]
                ]
            },
            @{
                @"name": @"Playback",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Pause",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle pause"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Increase Speed",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add speed 0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Decrease Speed",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add speed -0.1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Reset Speed",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"set speed 1.0"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Show Playlist",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"script-message osc-playlist"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Show Chapters",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"script-message osc-chapterlist"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Show Tracks",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"script-message osc-tracklist"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Next File",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"playlist-next"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Previous File",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"playlist-prev"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Loop File",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle-values loop-file \"inf\" \"no\""
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Toggle Loop Playlist",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"cycle-values loop-playlist \"inf\" \"no\""
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Shuffle",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"playlist-shuffle"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Next Chapter",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add chapter 1"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Previous Chapter",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"add chapter -1"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Step Forward",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"frame-step"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Step Backward",
                        @"action"     : @"cmd:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"cmd"        : @"frame-back-step"
                    }]
                ]
            },
            @{
                @"name": @"Window",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Minimize",
                        @"key"        : @"m",
                        @"cmdSpecial" : [NSNumber numberWithInt:MPM_MINIMIZE]
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Zoom",
                        @"key"        : @"z",
                        @"cmdSpecial" : [NSNumber numberWithInt:MPM_ZOOM]
                    }]
                ]
            },
            @{
                @"name": @"Help",
                @"menu": @[
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"mpv Website…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://mpv.io"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"mpv on github…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://github.com/mpv-player/mpv"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Online Manual…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://mpv.io/manual/master/"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Online Wiki…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://github.com/mpv-player/mpv/wiki"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Release Notes…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://github.com/mpv-player/mpv/blob/master/RELEASE_NOTES"
                    }],
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Keyboard Shortcuts…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://github.com/mpv-player/mpv/blob/master/etc/input.conf"
                    }],
                    @{ @"name": @"separator" },
                    [NSMutableDictionary dictionaryWithDictionary:@{
                        @"name"       : @"Report Issue…",
                        @"action"     : @"url:",
                        @"key"        : @"",
                        @"target"     : self,
                        @"url"        : @"https://github.com/mpv-player/mpv/issues/new"
                    }]
                ]
            }
        ];

        [NSApp setMainMenu:[self mainMenu]];
    }

    return self;
}

- (NSMenu *)mainMenu
{
    NSMenu *mainMenu = [[NSMenu alloc] initWithTitle:@"MainMenu"];
    [NSApp setServicesMenu:[[NSMenu alloc] init]];

    for(id mMenu in menuTree) {
        NSMenu *menu = [[NSMenu alloc] initWithTitle:mMenu[@"name"]];
        NSMenuItem *mItem = [mainMenu addItemWithTitle:mMenu[@"name"]
                                                action:nil
                                         keyEquivalent:@""];
        [mainMenu setSubmenu:menu forItem:mItem];

        for(id subMenu in mMenu[@"menu"]) {
#if HAVE_MACOS_TOUCHBAR
            if ([subMenu[@"action"] isEqual:@"toggleTouchBarCustomizationPalette:"]) {
                if (![NSApp respondsToSelector:@selector(touchBar)])
                    continue;
            }
#endif
            if ([subMenu[@"name"] isEqual:@"separator"]) {
                [menu addItem:[NSMenuItem separatorItem]];
            } else {
                NSMenuItem *iItem = [menu addItemWithTitle:subMenu[@"name"]
                               action:NSSelectorFromString(subMenu[@"action"])
                        keyEquivalent:subMenu[@"key"]];
                [iItem setTarget:subMenu[@"target"]];
                [subMenu setObject:iItem forKey:@"menuItem"];

                NSNumber *m = subMenu[@"modifiers"];
                if (m) {
                    [iItem setKeyEquivalentModifierMask:m.unsignedIntegerValue];
                }

                if ([subMenu[@"name"] isEqual:@"Services"]) {
                    iItem.submenu = [NSApp servicesMenu];
                }
            }
        }
    }

    return mainMenu;
}

- (void)about
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
        @"mpv", @"ApplicationName",
        [(Application *)NSApp getMPVIcon], @"ApplicationIcon",
        [NSString stringWithUTF8String:mpv_copyright], @"Copyright",
        [NSString stringWithUTF8String:mpv_version], @"ApplicationVersion",
        nil];
    [NSApp orderFrontStandardAboutPanelWithOptions:options];
}

- (void)preferences:(NSMenuItem *)menuItem
{
    NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSMutableDictionary *mItemDict = [self getDictFromMenuItem:menuItem];
    NSArray *configPaths  = @[
        [NSString stringWithFormat:@"%@/.mpv/", NSHomeDirectory()],
        [NSString stringWithFormat:@"%@/.config/mpv/", NSHomeDirectory()]];

    for (id path in configPaths) {
        NSString *fileP = [path stringByAppendingString:mItemDict[@"file"]];
        if ([fileManager fileExistsAtPath:fileP]){
            if ([workspace openFile:fileP])
                return;
            [workspace openFile:path];
            [self alertWithTitle:mItemDict[@"alertTitle1"]
                         andText:mItemDict[@"alertText1"]];
            return;
        }
        if ([workspace openFile:path]) {
            [self alertWithTitle:mItemDict[@"alertTitle2"]
                         andText:mItemDict[@"alertText2"]];
            return;
        }
    }

    [self alertWithTitle:mItemDict[@"alertTitle3"]
                 andText:mItemDict[@"alertText3"]];
}

- (void)quit:(NSMenuItem *)menuItem
{
    NSString *cmd = [self getDictFromMenuItem:menuItem][@"cmd"];
    [(Application *)NSApp stopMPV:(char *)[cmd UTF8String]];
}

- (void)openFile
{
    NSOpenPanel *panel = [[NSOpenPanel alloc] init];
    [panel setCanChooseDirectories:YES];
    [panel setAllowsMultipleSelection:YES];

    if ([panel runModal] == NSModalResponseOK){
        NSMutableArray *fileArray = [[NSMutableArray alloc] init];
        for (id url in [panel URLs])
            [fileArray addObject:[url path]];
        [(Application *)NSApp openFiles:fileArray];
    }
}

- (void)openURL
{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:@"Open URL"];
    [alert addButtonWithTitle:@"Ok"];
    [alert addButtonWithTitle:@"Cancel"];
    [alert setIcon:[(Application *)NSApp getMPVIcon]];

    NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
    [input setPlaceholderString:@"URL"];
    [alert setAccessoryView:input];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [input becomeFirstResponder];
    });

    if ([alert runModal] == NSAlertFirstButtonReturn && [input stringValue].length > 0) {
        NSArray *url = [NSArray arrayWithObjects:[input stringValue], nil];
        [(Application *)NSApp openFiles:url];
    }
}

- (void)cmd:(NSMenuItem *)menuItem
{
    NSString *cmd = [self getDictFromMenuItem:menuItem][@"cmd"];
    [(Application *)NSApp queueCommand:(char *)[cmd UTF8String]];
}

- (void)url:(NSMenuItem *)menuItem
{
    NSString *url = [self getDictFromMenuItem:menuItem][@"url"];
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
}

- (void)alertWithTitle:(NSString *)title andText:(NSString *)text
{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:title];
    [alert setInformativeText:text];
    [alert addButtonWithTitle:@"Ok"];
    [alert setIcon:[(Application *)NSApp getMPVIcon]];
    [alert runModal];
}

- (NSMutableDictionary *)getDictFromMenuItem:(NSMenuItem *)menuItem
{
    for(id mMenu in menuTree) {
        for(id subMenu in mMenu[@"menu"]) {
            if([subMenu[@"menuItem"] isEqual:menuItem])
                return subMenu;
        }
    }

    return nil;
}

- (void)registerSelector:(SEL)action forKey:(MPMenuKey)key
{
    for(id mMenu in menuTree) {
        for(id subMenu in mMenu[@"menu"]) {
            if([subMenu[@"cmdSpecial"] isEqual:[NSNumber numberWithInt:key]]) {
                [subMenu[@"menuItem"] setAction:action];