1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-24 07:42:17 +00:00

osx: move menu bar creation into its own file

This commit is contained in:
Akemi 2017-07-29 15:26:58 +02:00
parent 1f7fe1597d
commit 48ab72b478
7 changed files with 212 additions and 142 deletions

View File

@ -18,14 +18,7 @@
#ifndef MPV_MACOSX_APPLICATION
#define MPV_MACOSX_APPLICATION
// Menu Keys identifing menu items
typedef enum {
MPM_H_SIZE,
MPM_N_SIZE,
MPM_D_SIZE,
MPM_MINIMIZE,
MPM_ZOOM,
} MPMenuKey;
#include "osdep/macosx_menubar.h"
// multithreaded wrapper for mpv_main
int cocoa_main(int argc, char *argv[]);

View File

@ -47,21 +47,6 @@ static pthread_t playback_thread_id;
EventsResponder *_eventsResponder;
}
- (NSMenuItem *)menuItemWithParent:(NSMenu *)parent
title:(NSString *)title
action:(SEL)selector
keyEquivalent:(NSString*)key;
- (NSMenuItem *)mainMenuItemWithParent:(NSMenu *)parent
child:(NSMenu *)child;
- (void)registerMenuItem:(NSMenuItem*)menuItem forKey:(MPMenuKey)key;
- (NSMenu *)appleMenuWithMainMenu:(NSMenu *)mainMenu;
- (NSMenu *)videoMenu;
- (NSMenu *)windowMenu;
@end
@interface NSApplication (NiblessAdditions)
- (void)setAppleMenu:(NSMenu *)aMenu;
@end
static Application *mpv_shared_app(void)
@ -76,7 +61,7 @@ static void terminate_cocoa_application(void)
}
@implementation Application
@synthesize menuItems = _menu_items;
@synthesize menuBar = _menu_Bar;
@synthesize openCount = _open_count;
- (void)sendEvent:(NSEvent *)event
@ -89,7 +74,6 @@ static void terminate_cocoa_application(void)
- (id)init
{
if (self = [super init]) {
self.menuItems = [[[NSMutableDictionary alloc] init] autorelease];
_eventsResponder = [EventsResponder sharedInstance];
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
@ -125,11 +109,6 @@ static void terminate_cocoa_application(void)
currentPosition, timeLeft];
return tBar;
}
- (void)toggleTouchBarMenu
{
[NSApp toggleTouchBarCustomizationPalette:self];
}
#endif
- (void)processEvent:(struct mpv_event *)event
@ -145,117 +124,12 @@ static void terminate_cocoa_application(void)
[_eventsResponder queueCommand:cmd];
}
#define _R(P, T, E, K) \
{ \
NSMenuItem *tmp = [self menuItemWithParent:(P) title:(T) \
action:nil keyEquivalent:(E)]; \
[self registerMenuItem:tmp forKey:(K)]; \
}
- (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"];
[menu addItem:[NSMenuItem separatorItem]];
[self menuItemWithParent:menu title:@"Quit mpv"
action:@selector(stopPlayback) keyEquivalent: @"q"];
return [menu autorelease];
}
- (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 ([self respondsToSelector:@selector(touchBar)]) {
[menu addItem:[NSMenuItem separatorItem]];
[self menuItemWithParent:menu title:@"Customize Touch Bar…"
action:@selector(toggleTouchBarMenu) keyEquivalent: @""];
}
#endif
return [menu autorelease];
}
- (void)initialize_menu
{
NSMenu *main_menu = [[NSMenu new] autorelease];
[NSApp setMainMenu:main_menu];
[NSApp setAppleMenu:[self appleMenuWithMainMenu:main_menu]];
[NSApp mainMenuItemWithParent:main_menu child:[self videoMenu]];
[NSApp mainMenuItemWithParent:main_menu child:[self windowMenu]];
}
#undef _R
- (void)stopPlayback
{
[self stopMPV:"quit"];
}
- (void)stopPlaybackAndRememberPosition
{
[self stopMPV:"quit-watch-later"];
}
- (void)stopMPV:(char *)cmd
{
if (![_eventsResponder queueCommand:cmd])
terminate_cocoa_application();
}
- (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];
}
}
- (NSMenuItem *)menuItemWithParent:(NSMenu *)parent
title:(NSString *)title
action:(SEL)action
keyEquivalent:(NSString*)key
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title
action:action
keyEquivalent:key];
[parent addItem:item];
return [item autorelease];
}
- (NSMenuItem *)mainMenuItemWithParent:(NSMenu *)parent
child:(NSMenu *)child
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@""
action:nil
keyEquivalent:@""];
[item setSubmenu:child];
[parent addItem:item];
return [item autorelease];
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
@ -268,7 +142,7 @@ static void terminate_cocoa_application(void)
- (void)handleQuitEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
[self stopPlayback];
[self stopMPV:"quit"];
}
- (void)getUrl:(NSAppleEventDescriptor *)event
@ -325,14 +199,14 @@ static void *playback_thread(void *ctx_obj)
void cocoa_register_menu_item_action(MPMenuKey key, void* action)
{
if (application_instantiated)
[NSApp registerSelector:(SEL)action forKey:key];
[[NSApp menuBar] registerSelector:(SEL)action forKey:key];
}
static void init_cocoa_application(bool regular)
{
NSApp = mpv_shared_app();
[NSApp setDelegate:NSApp];
[NSApp initialize_menu];
[NSApp setMenuBar:[[MenuBar alloc] init]];
// Will be set to Regular from cocoa_common during UI creation so that we
// don't create an icon when playing audio only files.

View File

@ -17,18 +17,17 @@
#import <Cocoa/Cocoa.h>
#include "osdep/macosx_application.h"
#import "osdep/macosx_menubar_objc.h"
struct mpv_event;
@interface Application : NSApplication
- (void)initialize_menu;
- (void)registerSelector:(SEL)selector forKey:(MPMenuKey)key;
- (void)stopPlayback;
- (void)processEvent:(struct mpv_event *)event;
- (void)queueCommand:(char *)cmd;
- (void)stopMPV:(char *)cmd;
@property(nonatomic, retain) NSMutableDictionary *menuItems;
@property(nonatomic, retain) MenuBar *menuBar;
@property(nonatomic, retain) NSArray *files;
@property(nonatomic, assign) size_t openCount;
@end

30
osdep/macosx_menubar.h Normal file
View File

@ -0,0 +1,30 @@
/*
* 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/>.
*/
#ifndef MPV_MACOSX_MENU
#define MPV_MACOSX_MENU
// Menu Keys identifing menu items
typedef enum {
MPM_H_SIZE,
MPM_N_SIZE,
MPM_D_SIZE,
MPM_MINIMIZE,
MPM_ZOOM,
} MPMenuKey;
#endif /* MPV_MACOSX_MENU */

146
osdep/macosx_menubar.m Normal file
View File

@ -0,0 +1,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

View File

@ -0,0 +1,27 @@
/*
* 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/>.
*/
#import <Cocoa/Cocoa.h>
#include "osdep/macosx_menubar.h"
@interface MenuBar : NSObject
- (void)registerSelector:(SEL)action forKey:(MPMenuKey)key;
@property(nonatomic, retain) NSMutableDictionary *menuItems;
@end

View File

@ -461,6 +461,7 @@ def build(ctx):
( "osdep/ar/HIDRemote.m", "apple-remote" ),
( "osdep/macosx_application.m", "cocoa" ),
( "osdep/macosx_events.m", "cocoa" ),
( "osdep/macosx_menubar.m", "cocoa" ),
( "osdep/macosx_touchbar.m", "macos-touchbar" ),
( "osdep/semaphore_osx.c" ),
( "osdep/subprocess.c" ),