Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add mac_default_menu #631

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions sokol_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -3082,13 +3084,61 @@ _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();
[NSApplication sharedApplication];
// 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];
Expand Down