mirror of
https://github.com/Relintai/sdl2_frt.git
synced 2024-12-29 20:27:12 +01:00
Fixed bug 4877 - Add support for loading menus from a nib/xib instead of building a hardcoded minimum set
Eric Shepherd Currently, SDL on Cocoa macOS creates a rudimentary menu bar programmatically if none is already present when the app is registered during setup. SDL could be much more easily and flexibly used on macOS if upon finding that no menus are currently in place, it first looked for the application's main menu nib or xib file and, if found, loaded that instead of programmatically building the menus. This would then let developers simply drop in a nib file with a menu bar defined in it and it would be installed and used automatically. Attached is a patch that does just this. It changes the SDL_cocoaevents.m file to: * In Cocoa_RegisterApp(), before calling CreateApplicationMenus(), it calls a new function, LoadMainMenuNibIfAvailable(), which attempts to load and install the main menu nib file, using the nib name fetched from the Info.plist file. If that succeeds, LoadMainMenuNibIfAvailable() returns true; otherwise false. * If LMMNIA() returns false, CreateApplicationMenus() is called to programmatically build the menus as before. * Otherwise, we're done, and using the menus from the nib/xib file! I made these changes to support a project I'm working on, and felt they were useful enough to be worth offering them for uplift. They should have zero impact on existing projects' behavior, but make Cocoa SDL development miles easier.
This commit is contained in:
parent
3da6a0b20e
commit
7a3ae59037
@ -267,6 +267,25 @@ GetApplicationName(void)
|
|||||||
return appName;
|
return appName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
LoadMainMenuNibIfAvailable(void)
|
||||||
|
{
|
||||||
|
NSDictionary *infoDict;
|
||||||
|
NSString *mainNibFileName;
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
|
infoDict = [[NSBundle mainBundle] infoDictionary];
|
||||||
|
if (infoDict) {
|
||||||
|
mainNibFileName = [infoDict valueForKey:@"NSMainNibFile"];
|
||||||
|
|
||||||
|
if (mainNibFileName) {
|
||||||
|
success = [[NSBundle mainBundle] loadNibNamed:mainNibFileName owner:[NSApplication sharedApplication] topLevelObjects:nil];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
CreateApplicationMenus(void)
|
CreateApplicationMenus(void)
|
||||||
{
|
{
|
||||||
@ -385,9 +404,18 @@ Cocoa_RegisterApp(void)
|
|||||||
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If there aren't already menus in place, look to see if there's
|
||||||
|
* a nib we should use. If not, then manually create the basic
|
||||||
|
* menus we meed.
|
||||||
|
*/
|
||||||
if ([NSApp mainMenu] == nil) {
|
if ([NSApp mainMenu] == nil) {
|
||||||
|
bool nibLoaded;
|
||||||
|
|
||||||
|
nibLoaded = LoadMainMenuNibIfAvailable();
|
||||||
|
if (!nibLoaded) {
|
||||||
CreateApplicationMenus();
|
CreateApplicationMenus();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
[NSApp finishLaunching];
|
[NSApp finishLaunching];
|
||||||
if ([NSApp delegate]) {
|
if ([NSApp delegate]) {
|
||||||
/* The SDL app delegate calls this in didFinishLaunching if it's
|
/* The SDL app delegate calls this in didFinishLaunching if it's
|
||||||
|
Loading…
Reference in New Issue
Block a user