Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 60be026

Browse files
committed
[[ Bug 13510 ]] Make sure shutdownrequest is only sent once.
1 parent 482770c commit 60be026

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

docs/notes/bugfix-13510.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Shutdownrequest message sent twice when triggered from quit in menu or Cmd-Q on Mac.

engine/src/desktop-menu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ void MCScreenDC::updatemenubar(Boolean force)
611611
t_menu_button = (MCButton *)newMenuGroup -> findnum(CT_MENU, t_menu_button_index_i);
612612
if (t_menu_button == NULL)
613613
break;
614-
614+
615615
// Remove any menu shortcuts for the current button.
616616
MCstacks -> deleteaccelerator(t_menu_button, t_menu_button -> getstack());
617617

engine/src/mac-menu.mm

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
static uint32_t s_menu_select_lock = 0;
3939
static bool s_menu_select_occured = false;
40+
static bool s_quit_item_has_accelerator = false;
4041

4142
////////////////////////////////////////////////////////////////////////////////
4243

@@ -53,6 +54,11 @@
5354
// is the case, some items will be hidden and a (API-wise) invisible
5455
// menu will be inserted at the front (the application menu).
5556
bool is_menubar : 1;
57+
58+
// If the quit item in this menu has an accelerator, this is true.
59+
// (Cocoa seems to 'hide' the quit menu item accelerator for some inexplicable
60+
// reason - it returns 'empty').
61+
bool quit_has_accelerator : 1;
5662
};
5763

5864
////////////////////////////////////////////////////////////////////////////////
@@ -154,10 +160,21 @@ - (void)menuNeedsUpdate: (NSMenu *)menu
154160

155161
- (void)menuItemSelected: (id)sender
156162
{
157-
if (s_menu_select_lock == 0)
163+
NSMenuItem *t_item;
164+
t_item = (NSMenuItem *)sender;
165+
166+
// MW-2014-10-22: [[ Bug 13510 ]] As Cocoa hides Cmd-Q for some reason as a key equivalent
167+
// we mark the menu as having an accelerator for quit if one was specified. If no accelerator
168+
// was specified, we handle Cmd-Q as if it weren't an accelerator but was a select.
169+
// (This is for the case where the 'Exit' menu item has no accelerator, but Cocoa requires
170+
// said accelerator for conformance).
171+
bool t_item_is_quit_without_accelerator;
172+
t_item_is_quit_without_accelerator = false;
173+
if ([[t_item representedObject] isEqualToString: @"Quit"])
174+
t_item_is_quit_without_accelerator = ![(com_runrev_livecode_MCMenuDelegate *)[[t_item menu] delegate] platformMenuRef] -> quit_has_accelerator;
175+
176+
if (s_menu_select_lock == 0 || t_item_is_quit_without_accelerator)
158177
{
159-
NSMenuItem *t_item;
160-
t_item = (NSMenuItem *)sender;
161178
MCPlatformCallbackSendMenuSelect(m_menu, [[t_item menu] indexOfItem: t_item]);
162179
}
163180
else
@@ -179,6 +196,8 @@ - (BOOL)worksWhenModal
179196

180197
- (BOOL)menuHasKeyEquivalent:(NSMenu *)menu forEvent:(NSEvent *)event target:(id *)target action:(SEL *)action
181198
{
199+
// MW-2014-10-22: [[ Bug 13510 ]] Make sure we update menus before searching for accelerators.
200+
[[menu delegate] menuNeedsUpdate: menu];
182201
return NO;
183202
}
184203

@@ -236,12 +255,25 @@ - (void)preferencesMenuItemSelected: (id)sender
236255
[self shadowedMenuItemSelected: @"Preferences"];
237256
}
238257

258+
- (void)quitMenuItemSelected: (id)sender
259+
{
260+
[self shadowedMenuItemSelected: @"Quit"];
261+
}
262+
239263
- (void)menuNeedsUpdate: (NSMenu *)menu
240264
{
241-
NSMenuItem *t_prefs, *t_about;
265+
NSMenuItem *t_prefs, *t_about, *t_quit;
242266
t_prefs = [self findShadowedMenuItem: @"Preferences"];
243267
t_about = [self findShadowedMenuItem: @"About"];
268+
t_quit = [self findShadowedMenuItem: @"Quit"];
244269

270+
if (t_quit != nil)
271+
{
272+
[[[t_quit menu] delegate] menuNeedsUpdate: [t_prefs menu]];
273+
t_quit = [self findShadowedMenuItem: @"Quit"];
274+
[[menu itemAtIndex: 10] setEnabled: t_quit != nil ? [t_quit isEnabled] : NO];
275+
}
276+
245277
if (t_prefs != nil)
246278
{
247279
[[[t_prefs menu] delegate] menuNeedsUpdate: [t_prefs menu]];
@@ -597,6 +629,9 @@ void MCPlatformSetMenuItemProperty(MCPlatformMenuRef p_menu, uindex_t p_index, M
597629
[t_item setKeyEquivalent: @""];
598630
[t_item setKeyEquivalentModifierMask: 0];
599631
}
632+
633+
if ([[t_item representedObject] isEqualToString: @"Quit"])
634+
p_menu -> quit_has_accelerator = t_accelerator != 0;
600635
}
601636
break;
602637
case kMCPlatformMenuItemPropertyEnabled:
@@ -748,8 +783,9 @@ static void MCPlatformStartUsingMenuAsMenubar(MCPlatformMenuRef p_menu)
748783
keyEquivalent: @""];
749784
[t_app_menu addItem: [NSMenuItem separatorItem]];
750785
[t_app_menu addItemWithTitle: [NSString stringWithFormat: NSLocalizedString(@"Quit %@", @"Quit {Application Name}"), t_app_name]
751-
action: @selector(terminate:)
786+
action: @selector(quitMenuItemSelected:)
752787
keyEquivalent:@"q"];
788+
[[t_app_menu itemAtIndex: 10] setTarget: s_app_menu_delegate];
753789
[t_app_menu setDelegate: s_app_menu_delegate];
754790

755791
NSMenuItem *t_app_menu_item;

engine/src/stacklst.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ Boolean MCStacklist::doaccelerator(KeySym key)
321321
{
322322
if (key == (KeySym)accelerators[i] . key && (MCmodifierstate & t_mod_mask) == (accelerators[i].mods & t_mod_mask) && accelerators[i] . button -> getparent() == t_menubar)
323323
{
324-
t_menubar -> message_with_args(MCM_mouse_down, "");
324+
// MW-2014-10-22: [[ Bug 13510 ]] Make sure we send the update message to the menu of menubar - not
325+
// the menubar group.
326+
accelerators[i] . button -> message_with_args(MCM_mouse_down, "");
325327

326328
// We now need to re-search for the accelerator, since it could have gone/been deleted in the mouseDown
327329
for(uint2 i = 0; i < naccelerators; i++)

0 commit comments

Comments
 (0)