From 48ab72b4781b01e334b60d6f5e71afe72900fa58 Mon Sep 17 00:00:00 2001 From: Akemi Date: Sat, 29 Jul 2017 15:26:58 +0200 Subject: osx: move menu bar creation into its own file --- osdep/macosx_application.h | 9 +-- osdep/macosx_application.m | 134 ++---------------------------------- osdep/macosx_application_objc.h | 7 +- osdep/macosx_menubar.h | 30 +++++++++ osdep/macosx_menubar.m | 146 ++++++++++++++++++++++++++++++++++++++++ osdep/macosx_menubar_objc.h | 27 ++++++++ 6 files changed, 211 insertions(+), 142 deletions(-) create mode 100644 osdep/macosx_menubar.h create mode 100644 osdep/macosx_menubar.m create mode 100644 osdep/macosx_menubar_objc.h (limited to 'osdep') diff --git a/osdep/macosx_application.h b/osdep/macosx_application.h index 82d18b0058..96a861fa35 100644 --- a/osdep/macosx_application.h +++ b/osdep/macosx_application.h @@ -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[]); diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m index ae04ae2975..7fc1218fee 100644 --- a/osdep/macosx_application.m +++ b/osdep/macosx_application.m @@ -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. diff --git a/osdep/macosx_application_objc.h b/osdep/macosx_application_objc.h index 6fc6d61a55..7e2993fb2f 100644 --- a/osdep/macosx_application_objc.h +++ b/osdep/macosx_application_objc.h @@ -17,18 +17,17 @@ #import #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 diff --git a/osdep/macosx_menubar.h b/osdep/macosx_menubar.h new file mode 100644 index 0000000000..e0466ee501 --- /dev/null +++ b/osdep/macosx_menubar.h @@ -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 . + */ + +#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 */ diff --git a/osdep/macosx_menubar.m b/osdep/macosx_menubar.m new file mode 100644 index 0000000000..aa8e0e5054 --- /dev/null +++ b/osdep/macosx_menubar.m @@ -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 . + */ + +#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 diff --git a/osdep/macosx_menubar_objc.h b/osdep/macosx_menubar_objc.h new file mode 100644 index 0000000000..c0d42cf577 --- /dev/null +++ b/osdep/macosx_menubar_objc.h @@ -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 . + */ + +#import +#include "osdep/macosx_menubar.h" + +@interface MenuBar : NSObject + +- (void)registerSelector:(SEL)action forKey:(MPMenuKey)key; + +@property(nonatomic, retain) NSMutableDictionary *menuItems; + +@end -- cgit v1.2.3