Fixed bug 4415 - SDL menu bar is nonstandard on Mac
authorSam Lantinga <slouken@libsdl.org>
Sat, 08 Dec 2018 11:06:40 -0800
changeset 12468209066a82aef
parent 12467 2d7891d05d8d
child 12469 99d8b18acf8a
Fixed bug 4415 - SDL menu bar is nonstandard on Mac

foo.null

I'm on macOS 10.14 and I think I'm using or around SDL 2.0.9. This is about the menu bar that SDL sets up which looks like:

<App Name> <Window> <View>

1. View menu never proceeds after the Window menu in any Mac application (it is always before).
2. For SDL, the only purpose of the View menu is for a single fullscreen menu item, which is not justifiable enough to reserve space for a menu. The View menu should thus be removed, and the full screen menu item should be added at the end inside of Window's menu. See built in apps like Dictionary, Chess, App Store (on 10.14) that do this.
3. SDL should add a "Close" menu item to the Window's submenu, and it should be the first item. Its key equivalent should map to command w. Without this, you cannot close the game window via this shortcut, and you cannot close the app's About window via this shortcut.
4. Apps typically use "Enter Full Screen" or "Exit Full Screen" depending on context, not "Toggle Full Screen" which is less user friendly -- I personally care about this point the least.
src/video/cocoa/SDL_cocoaevents.m
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Fri Dec 07 12:02:08 2018 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Sat Dec 08 11:06:40 2018 -0800
     1.3 @@ -275,7 +275,6 @@
     1.4      NSMenu *appleMenu;
     1.5      NSMenu *serviceMenu;
     1.6      NSMenu *windowMenu;
     1.7 -    NSMenu *viewMenu;
     1.8      NSMenuItem *menuItem;
     1.9      NSMenu *mainMenu;
    1.10  
    1.11 @@ -342,9 +341,22 @@
    1.12      windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
    1.13  
    1.14      /* Add menu items */
    1.15 +    [windowMenu addItemWithTitle:@"Close" action:@selector(performClose:) keyEquivalent:@"w"];
    1.16 +
    1.17      [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
    1.18  
    1.19      [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
    1.20 +    
    1.21 +    /* Add the fullscreen toggle menu option, if supported */
    1.22 +    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
    1.23 +        /* Cocoa should update the title to Enter or Exit Full Screen automatically.
    1.24 +         * But if not, then just fallback to Toggle Full Screen.
    1.25 +         */
    1.26 +        menuItem = [[NSMenuItem alloc] initWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
    1.27 +        [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
    1.28 +        [windowMenu addItem:menuItem];
    1.29 +        [menuItem release];
    1.30 +    }
    1.31  
    1.32      /* Put menu into the menubar */
    1.33      menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
    1.34 @@ -355,25 +367,6 @@
    1.35      /* Tell the application object that this is now the window menu */
    1.36      [NSApp setWindowsMenu:windowMenu];
    1.37      [windowMenu release];
    1.38 -
    1.39 -
    1.40 -    /* Add the fullscreen view toggle menu option, if supported */
    1.41 -    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
    1.42 -        /* Create the view menu */
    1.43 -        viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
    1.44 -
    1.45 -        /* Add menu items */
    1.46 -        menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
    1.47 -        [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
    1.48 -
    1.49 -        /* Put menu into the menubar */
    1.50 -        menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
    1.51 -        [menuItem setSubmenu:viewMenu];
    1.52 -        [[NSApp mainMenu] addItem:menuItem];
    1.53 -        [menuItem release];
    1.54 -
    1.55 -        [viewMenu release];
    1.56 -    }
    1.57  }
    1.58  
    1.59  void
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Fri Dec 07 12:02:08 2018 -0800
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Sat Dec 08 11:06:40 2018 -0800
     2.3 @@ -1356,6 +1356,11 @@
     2.4          return SDL_SetError("%s", [[e reason] UTF8String]);
     2.5      }
     2.6  
     2.7 +    /* By default, don't allow users to make our window tabbed in 10.12 or later */
     2.8 +    if ([nswindow respondsToSelector:@selector(setTabbingMode:)]) {
     2.9 +        [nswindow setTabbingMode:NSWindowTabbingModeDisallowed];
    2.10 +    }
    2.11 +
    2.12      if (videodata->allow_spaces) {
    2.13          SDL_assert(floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6);
    2.14          SDL_assert([nswindow respondsToSelector:@selector(toggleFullScreen:)]);