diff --git a/sokol_app.h b/sokol_app.h index 09ab13b9e..18e631fdb 100644 --- a/sokol_app.h +++ b/sokol_app.h @@ -1398,6 +1398,7 @@ typedef struct sapp_desc { bool html5_premultiplied_alpha; // HTML5 only: whether the rendered pixels use premultiplied alpha convention bool html5_ask_leave_site; // initial state of the internal html5_ask_leave_site flag (see sapp_html5_ask_leave_site()) bool ios_keyboard_resizes_canvas; // if true, showing the iOS keyboard shrinks the canvas + bool mac_default_menu; // if true, showing the default menu on the Mac (it includes quit and hide). } sapp_desc; /* HTML5 specific: request and response structs for @@ -2675,6 +2676,7 @@ _SOKOL_PRIVATE sapp_desc _sapp_desc_defaults(const sapp_desc* in_desc) { desc.max_dropped_files = _sapp_def(desc.max_dropped_files, 1); desc.max_dropped_file_path_length = _sapp_def(desc.max_dropped_file_path_length, 2048); desc.window_title = _sapp_def(desc.window_title, "sokol_app"); + desc.mac_default_menu = _sapp_def(desc.mac_default_menu, true); return desc; } @@ -3082,6 +3084,51 @@ _SOKOL_PRIVATE void _sapp_macos_discard_state(void) { _SAPP_OBJC_RELEASE(_sapp.macos.window); } +_SOKOL_PRIVATE void _sapp_macos_create_default_menu(const sapp_desc* desc) { + NSMenu* menu_bar = [[NSMenu alloc] init]; + NSMenuItem* app_menu_item = [[NSMenuItem alloc] init]; + [menu_bar addItem:app_menu_item]; + NSApp.mainMenu = menu_bar; + NSMenu* app_menu = [[NSMenu alloc] init]; + NSString* window_title_as_nsstring = [NSString stringWithUTF8String:desc->window_title]; + + NSString* quit_title = [@"Quit " stringByAppendingString:window_title_as_nsstring]; + NSMenuItem* quit_menu_item = [[NSMenuItem alloc] + initWithTitle:quit_title + action:@selector(terminate:) + keyEquivalent:@"q"]; + + NSString* hide_title = [@"Hide " stringByAppendingString:window_title_as_nsstring]; + NSMenuItem* hide_menu_item = [[NSMenuItem alloc] + initWithTitle:hide_title + action:@selector(hide:) + keyEquivalent:@"h"]; + + NSMenuItem* hide_others_item = [[NSMenuItem alloc] + initWithTitle:@"Hide Others" + action:@selector(hideOtherApplications:) + keyEquivalent:@"h"]; + + [hide_others_item setKeyEquivalentModifierMask: NSEventModifierFlagOption]; + + NSMenuItem* show_all_item = [[NSMenuItem alloc] + initWithTitle:@"Show All" + action:@selector(unhideAllApplications:) + keyEquivalent:@""]; + + [app_menu addItem:hide_menu_item]; + [app_menu addItem:hide_others_item]; + [app_menu addItem:show_all_item]; + [app_menu addItem:[NSMenuItem separatorItem]]; + [app_menu addItem:quit_menu_item]; + app_menu_item.submenu = app_menu; + + _SAPP_OBJC_RELEASE(window_title_as_nsstring); + _SAPP_OBJC_RELEASE(app_menu); + _SAPP_OBJC_RELEASE(app_menu_item); + _SAPP_OBJC_RELEASE(menu_bar); +} + _SOKOL_PRIVATE void _sapp_macos_run(const sapp_desc* desc) { _sapp_init_state(desc); _sapp_macos_init_keytable(); @@ -3089,6 +3136,9 @@ _SOKOL_PRIVATE void _sapp_macos_run(const sapp_desc* desc) { // set the application dock icon as early as possible, otherwise // the dummy icon will be visible for a short time sapp_set_icon(&_sapp.desc.icon); + if (_sapp.desc.mac_default_menu) { + _sapp_macos_create_default_menu(desc); + } _sapp.macos.app_dlg = [[_sapp_macos_app_delegate alloc] init]; NSApp.delegate = _sapp.macos.app_dlg; [NSApp run];