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

Commit a46f7a6

Browse files
[[ Bug 13940 ]] 'LiveCode' menubar element building and reaction to shortcuts updated.
1 parent 60428a1 commit a46f7a6

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

docs/notes/bugfix-13940.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# An application with no menus can not be quit with the menu or key commands

engine/src/desktop-menu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,8 @@ void MCPlatformHandleMenuUpdate(MCPlatformMenuRef p_menu)
915915
// MW-2014-06-10: [[ Bug 12590 ]] Make sure we lock screen around the menu update message.
916916
MCRedrawLockScreen();
917917
s_menubar_lock_count += 1;
918-
s_menubar_targets[t_parent_menu_index] -> Get() -> message_with_args(MCM_mouse_down, "1");
918+
// SN-2014-11-06: [[ Bug 13940 ]] Keep the behaviour as previously: mouseDown "" is sent when updating menus
919+
s_menubar_targets[t_parent_menu_index] -> Get() -> message_with_args(MCM_mouse_down, "");
919920
s_menubar_lock_count -= 1;
920921
MCRedrawUnlockScreen();
921922
}

engine/src/mac-internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ class MCMacPlatformSurface;
343343

344344
- (void)aboutMenuItemSelected: (id)sender;
345345
- (void)preferencesMenuItemSelected: (id)sender;
346+
// SN-2014-11-06: [[ Bug 13940 ]] Added declaration for quitMenuItemSelected
347+
// and quitApplicationSelected, the latter quitting the app straight.
348+
- (void)quitMenuItemSelected: (id)sender;
349+
- (void)quitApplicationSelected: (id)sender;
346350

347351
- (void)menuNeedsUpdate: (NSMenu *)menu;
348352

engine/src/mac-menu.mm

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
// reason - it returns 'empty').
6262
bool quit_has_accelerator : 1;
6363

64+
// SN-2014-11-06: [[ Bu 13940 ]] Add a flag for the presence of a Preferences shortcut
65+
// to allow the menu item to be disabled.
66+
bool preferences_has_accelerator : 1;
67+
6468
// SN-2014-11-06: [[ Bug 13836 ]] Set to true if the menu needs to be updated.
6569
bool needs_update : 1;
6670
};
@@ -185,13 +189,14 @@ - (void)menuItemSelected: (id)sender
185189
// was specified, we handle Cmd-Q as if it weren't an accelerator but was a select.
186190
// (This is for the case where the 'Exit' menu item has no accelerator, but Cocoa requires
187191
// said accelerator for conformance).
188-
// bool t_item_is_quit_without_accelerator;
189-
// t_item_is_quit_without_accelerator = false;
190-
// if ([[t_item representedObject] isEqualToString: @"Quit"])
191-
// t_item_is_quit_without_accelerator = ![(com_runrev_livecode_MCMenuDelegate *)[[t_item menu] delegate] platformMenuRef] -> quit_has_accelerator;
192+
bool t_quit_accelerator_present;
193+
t_quit_accelerator_present = false;
194+
if ([[t_item keyEquivalent] isEqualToString: @"q"])
195+
t_quit_accelerator_present = [(com_runrev_livecode_MCMenuDelegate *)[[t_item menu] delegate] platformMenuRef] -> quit_has_accelerator;
192196

193-
if (s_menu_select_lock == 0/* || t_item_is_quit_without_accelerator*/)
197+
if (s_menu_select_lock == 0 || t_quit_accelerator_present)
194198
MCPlatformCallbackSendMenuSelect(m_menu, [[t_item menu] indexOfItem: t_item]);
199+
195200
// SN-2014-11-06: [[ Bug 13836 ]] s_menu_select_occured was not used.
196201
}
197202

@@ -283,35 +288,18 @@ - (void)quitMenuItemSelected: (id)sender
283288
[self shadowedMenuItemSelected: @"Quit"];
284289
}
285290

286-
- (void)menuNeedsUpdate: (NSMenu *)menu
291+
// SN-2014-11-06: [[ Bug 13940 ]] The user asked to quit - and no accelerator was added for this,
292+
// so we quit!
293+
- (void)quitApplicationSelected: (id)sender
287294
{
288-
NSMenuItem *t_prefs, *t_about, *t_quit;
289-
t_prefs = [self findShadowedMenuItem: @"Preferences"];
290-
t_about = [self findShadowedMenuItem: @"About"];
291-
t_quit = [self findShadowedMenuItem: @"Quit"];
292-
293-
if (t_quit != nil)
294-
{
295-
[[[t_quit menu] delegate] menuNeedsUpdate: [t_prefs menu]];
296-
t_quit = [self findShadowedMenuItem: @"Quit"];
297-
[[menu itemAtIndex: 10] setEnabled: t_quit != nil ? [t_quit isEnabled] : NO];
298-
}
295+
NSApplication* t_app = [NSApplication sharedApplication];
296+
297+
[t_app terminate:t_app];
298+
}
299299

300-
if (t_prefs != nil)
301-
{
302-
[[[t_prefs menu] delegate] menuNeedsUpdate: [t_prefs menu]];
303-
t_prefs = [self findShadowedMenuItem: @"Preferences"];
304-
[[menu itemAtIndex: 2] setEnabled: t_prefs != nil ? [t_prefs isEnabled] : NO];
305-
}
306-
307-
if (t_about != nil)
308-
{
309-
if (t_prefs == nil ||
310-
[t_about menu] != [t_prefs menu])
311-
[[[t_about menu] delegate] menuNeedsUpdate: [t_about menu]];
312-
t_about = [self findShadowedMenuItem: @"About"];
313-
[[menu itemAtIndex: 0] setEnabled: t_about != nil ? [t_about isEnabled] : NO];
314-
}
300+
// SN-2014-11-06: [[ Bug 13940 ]] menuNeedsUpdate not longer needed at the Application level
301+
- (void)menuNeedsUpdate: (NSMenu *)menu
302+
{
315303
}
316304

317305
- (BOOL)validateMenuItem: (NSMenuItem *)item
@@ -500,6 +488,10 @@ void MCPlatformCreateMenu(MCPlatformMenuRef& r_menu)
500488
// Turn on auto-enablement - this allows dialogs to control the enablement
501489
// of items with appropriate tag.
502490
[t_menu -> menu setAutoenablesItems: YES];
491+
492+
// SN-2014-11-06: [[ Bug 13940 ]] Initialises the accelerator presence flag.
493+
t_menu -> quit_has_accelerator = false;
494+
t_menu -> preferences_has_accelerator = false;
503495

504496
r_menu = t_menu;
505497
}
@@ -657,7 +649,22 @@ void MCPlatformSetMenuItemProperty(MCPlatformMenuRef p_menu, uindex_t p_index, M
657649
[t_item setTarget: p_menu -> menu_delegate];
658650
}
659651
else
660-
{
652+
{
653+
// SN-2014-11-06: [[ Bug 13940 ]] Update the parent - if any - to know that his submenu has
654+
// a Quit or a Preferences accelerator
655+
NSMenu *t_supermenu;
656+
t_supermenu = [p_menu -> menu supermenu];
657+
658+
if (t_supermenu != nil)
659+
{
660+
MCPlatformMenuRef t_supermenu_ref;
661+
t_supermenu_ref = [(MCMenuDelegate *)[t_supermenu delegate] platformMenuRef];
662+
if (t_action == kMCPlatformMenuItemActionQuit)
663+
t_supermenu_ref -> quit_has_accelerator = true;
664+
else if (t_action == kMCPlatformMenuItemActionPreferences)
665+
t_supermenu_ref -> preferences_has_accelerator = true;
666+
}
667+
661668
SEL t_selector;
662669
if (MCMacPlatformMapMenuItemActionToSelector(t_action, t_selector))
663670
{
@@ -699,9 +706,6 @@ void MCPlatformSetMenuItemProperty(MCPlatformMenuRef p_menu, uindex_t p_index, M
699706
[t_item setKeyEquivalent: @""];
700707
[t_item setKeyEquivalentModifierMask: 0];
701708
}
702-
703-
if ([[t_item representedObject] isEqualToString: @"Quit"])
704-
p_menu -> quit_has_accelerator = t_accelerator != 0;
705709
}
706710
break;
707711
case kMCPlatformMenuItemPropertyEnabled:
@@ -833,7 +837,12 @@ static void MCPlatformStartUsingMenuAsMenubar(MCPlatformMenuRef p_menu)
833837
[t_app_menu addItemWithTitle: NSLocalizedString(@"Preferences...", nil)
834838
action: @selector(preferencesMenuItemSelected:)
835839
keyEquivalent: @","];
836-
[[t_app_menu itemAtIndex: 2] setTarget: s_app_menu_delegate];
840+
// SN-2014-11-06: [[ Bug 13940 ]] Only enable the Preference menu if the shortcut exists in the menubar
841+
if (p_menu -> preferences_has_accelerator)
842+
[[t_app_menu itemAtIndex: 2] setTarget: s_app_menu_delegate];
843+
else
844+
[[t_app_menu itemAtIndex: 2] setEnabled: NO];
845+
837846
[t_app_menu addItem: [NSMenuItem separatorItem]];
838847
[t_app_menu addItemWithTitle: NSLocalizedString(@"Services", nil)
839848
action: nil
@@ -855,7 +864,14 @@ static void MCPlatformStartUsingMenuAsMenubar(MCPlatformMenuRef p_menu)
855864
[t_app_menu addItemWithTitle: [NSString stringWithFormat: NSLocalizedString(@"Quit %@", @"Quit {Application Name}"), t_app_name]
856865
action: @selector(quitMenuItemSelected:)
857866
keyEquivalent:@"q"];
858-
[[t_app_menu itemAtIndex: 10] setTarget: s_app_menu_delegate];
867+
// SN-2014-11-06: [[ Bug 13940 ]] In case there is no Quit shortcut in this menubar,
868+
// the action will simply be to close the application.
869+
if (p_menu -> quit_has_accelerator)
870+
[[t_app_menu itemAtIndex: 10] setAction:@selector(quitMenuItemSelected:)];
871+
else
872+
[[t_app_menu itemAtIndex: 10] setAction:@selector(quitApplicationSelected:)];
873+
874+
[[t_app_menu itemAtIndex: 10] setTarget: s_app_menu_delegate];
859875
[t_app_menu setDelegate: s_app_menu_delegate];
860876

861877
NSMenuItem *t_app_menu_item;

0 commit comments

Comments
 (0)