summaryrefslogtreecommitdiffstats
path: root/osdep/macosx_menubar.m
blob: aa8e0e5054fc3527889ea7764b67c10bb02a6f05 (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
/*
 * 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"

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

@implementation MenuBar

@synthesize menuItems = _menu_items;

- (id)init
{
    if (self = [super init]) {
        self.menuItems = [[[NSMutableDictionary alloc] init] autorelease];

        NSMenu *main_menu = [[NSMenu new] autorelease];
        [NSApp setMainMenu:main_menu];
        [NSApp performSelector:@selector(setAppleMenu:)
                    withObject:[self appleMenuWithMainMenu:main_menu]];

        [self mainMenuItemWithParent:main_menu child:[self videoMenu]];
        [self mainMenuItemWithParent:main_menu child:[self windowMenu]];
    }

    return self;
}

- (NSMenu *)appleMenuWithMainMenu:(NSMenu *)mainMenu
{
    NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Apple Menu"];
    [self mainMenuItemWithParent:mainMenu child:menu];
    [self menuItemWithParent:menu title:@"Hide mpv" action:@selector(hide:)
                          keyEquivalent: @"h" target:NSApp];
    [menu addItem:[NSMenuItem separatorItem]];
    [self menuItemWithParent:menu title:@"Quit mpv" action:@selector(quit)
                          keyEquivalent:@"q" target:self];
    return [menu autorelease];
}

#define _R(P, T, E, K) \
    { \
        NSMenuItem *tmp = [self menuItemWithParent:(P) title:(T) \
                                            action:nil keyEquivalent:(E) \
                                            target:nil]; \
        [self registerMenuItem:tmp forKey:(K)]; \
    }

- (NSMenu *)videoMenu
{
    NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Video"];
    _R(menu, @"Half Size",   @"0", MPM_H_SIZE)
    _R(menu, @"Normal Size", @"1", MPM_N_SIZE)
    _R(menu, @"Double Size", @"2", MPM_D_SIZE)
    return [menu autorelease];
}

- (NSMenu *)windowMenu
{
    NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Window"];
    _R(menu, @"Minimize", @"m", MPM_MINIMIZE)
    _R(menu, @"Zoom",     @"z", MPM_ZOOM)

#if HAVE_MACOS_TOUCHBAR
    if ([NSApp respondsToSelector:@selector(touchBar)]) {
        [menu addItem:[NSMenuItem separatorItem]];
        [self menuItemWithParent:menu title:@"Customize Touch Bar…"
                          action:@selector(toggleTouchBarMenu)
                   keyEquivalent:@"" target:self];
    }
#endif

    return [menu autorelease];
}

#undef _R

- (NSMenuItem *)mainMenuItemWithParent:(NSMenu *)parent
                                 child:(NSMenu *)child
{
    NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@""
                                                  action:nil
                                           keyEquivalent:@""];
    [item setSubmenu:child];
    [parent addItem:item];
    return [item autorelease];
}

- (NSMenuItem *)menuItemWithParent:(NSMenu *)parent
                             title:(NSString *)title
                            action:(SEL)action
                     keyEquivalent:(NSString*)key
                            target:(id)target
{

    NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title
                                                  action:action
                                           keyEquivalent:key];
    if (target)
        [item setTarget:target];
    [parent addItem:item];
    return [item autorelease];
}

- (void)registerMenuItem:(NSMenuItem*)menuItem forKey:(MPMenuKey)key
{
    [self.menuItems setObject:menuItem forKey:[NSNumber numberWithInt:key]];
}

- (void)registerSelector:(SEL)action forKey:(MPMenuKey)key
{
    NSNumber *boxedKey = [NSNumber numberWithInt:key];
    NSMenuItem *item   = [self.menuItems objectForKey:boxedKey];
    if (item) {
        [item setAction:action];
    }
}

#if HAVE_MACOS_TOUCHBAR
- (void)toggleTouchBarMenu
{
    [NSApp toggleTouchBarCustomizationPalette:self];
}
#endif

- (void)quit
{
    [(Application *)NSApp stopMPV:"quit"];
}

@end