Skip to content

Commit 934adb5

Browse files
committed
MacOS: Send window with event, fixed non-updating flip flop issue.
1 parent 1e01ce4 commit 934adb5

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/video/cocoa/SDL_cocoavideo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ typedef enum
6666

6767
@interface PlatformMenuData : NSObject {
6868
@public
69-
Uint16 user_event_type;
69+
SDL_MenuBar *menu_bar;
7070
NSMenu *menu;
7171
NSMenuItem *menu_item;
72+
Uint16 user_event_type;
7273
}
7374

7475
- (void) Cocoa_PlatformMenuData_MenuButtonClicked: (id)sender;

src/video/cocoa/SDL_cocoavideo.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ - (void) Cocoa_PlatformMenuData_MenuButtonClicked: (id)sender;{
344344
event.type = SDL_EVENT_MENU_BUTTON_CLICKED;
345345
event.menu.timestamp = SDL_GetTicksNS();
346346
event.menu.user_event_type = user_event_type;
347+
event.menu.windowID = menu_bar->window->id;
347348

348349
SDL_PushEvent(&event);
349350
}
@@ -355,13 +356,20 @@ bool Cocoa_CreateMenuBar(SDL_MenuBar *menu_bar)
355356
PlatformMenuData* platform_menu =[PlatformMenuData new];
356357
platform_menu->menu = [NSMenu new];
357358

358-
NSMenuItem *appMenuItem = [NSMenuItem new];
359-
NSMenu *appMenu = [NSMenu new];
360-
[appMenu setAutoenablesItems:false];
359+
SDL_MenuItem* app_menu = SDL_calloc_REAL(1, sizeof(SDL_MenuItem));
360+
app_menu->common.type = SDL_MENUITEM_SUBMENU;
361+
app_menu->common.enabled = true;
362+
app_menu->common.parent = (SDL_MenuItem*)menu_bar;
361363

362-
[appMenuItem setSubmenu:appMenu];
364+
PlatformMenuData* app_menu_platform_data = [PlatformMenuData new];
365+
app_menu->common.platform_data = (void*)CFBridgingRetain(app_menu_platform_data);
363366

364-
[platform_menu->menu addItem:appMenuItem];
367+
app_menu_platform_data->menu = [NSMenu new];
368+
app_menu_platform_data->menu_item = [NSMenuItem new];
369+
[app_menu_platform_data->menu setAutoenablesItems:false];
370+
[app_menu_platform_data->menu_item setSubmenu:app_menu_platform_data->menu];
371+
372+
[platform_menu->menu addItem:app_menu_platform_data->menu_item];
365373

366374
menu_bar->common.item_common.platform_data = (void*)CFBridgingRetain(platform_menu);
367375

@@ -376,11 +384,12 @@ bool Cocoa_SetWindowMenuBar(SDL_Window *window, SDL_MenuBar *menu_bar)
376384
}
377385

378386
// We don't actually set the menubar until the window is in focus
379-
380387
if (!(menu_bar->window->flags & SDL_WINDOW_INPUT_FOCUS)) {
381388
return true;
382389
}
383390

391+
// If we don't set it to nil first, it won't change menu bars until you switch off and back on focus.
392+
[NSApp setMainMenu:nil];
384393
PlatformMenuData* platform_data = (__bridge PlatformMenuData*)menu_bar->common.item_common.platform_data;
385394
[NSApp setMainMenu:platform_data->menu];
386395

@@ -392,6 +401,7 @@ bool Cocoa_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const char *n
392401
PlatformMenuData* platform_data = [PlatformMenuData new];
393402
menu_item->common.platform_data = (void*)CFBridgingRetain(platform_data);
394403
platform_data->user_event_type = event_type;
404+
platform_data->menu_bar = menu_item->common.menu_bar;
395405

396406
PlatformMenuData* parent_platform_data = (__bridge id _Nullable)(menu_item->common.parent->common.platform_data);
397407
NSString* name_ns = [NSString stringWithUTF8String:name];

0 commit comments

Comments
 (0)