fix: keep shifted character in menu accelerator (#29202)
* fix: correctly handle shifted char in accelerator * test: use actual accelerator of NSMenuItem * chore: simplify KeyboardCodeFromStr * chore: GetAcceleratorTextAt is testing only
This commit is contained in:
parent
31190d4c6d
commit
3cfe5c6a21
11 changed files with 168 additions and 83 deletions
|
@ -31,10 +31,10 @@ bool StringToAccelerator(const std::string& shortcut,
|
|||
// Now, parse it into an accelerator.
|
||||
int modifiers = ui::EF_NONE;
|
||||
ui::KeyboardCode key = ui::VKEY_UNKNOWN;
|
||||
base::Optional<char16_t> shifted_char;
|
||||
for (const auto& token : tokens) {
|
||||
bool shifted = false;
|
||||
ui::KeyboardCode code = electron::KeyboardCodeFromStr(token, &shifted);
|
||||
if (shifted)
|
||||
ui::KeyboardCode code = electron::KeyboardCodeFromStr(token, &shifted_char);
|
||||
if (shifted_char)
|
||||
modifiers |= ui::EF_SHIFT_DOWN;
|
||||
switch (code) {
|
||||
// The token can be a modifier.
|
||||
|
@ -65,6 +65,7 @@ bool StringToAccelerator(const std::string& shortcut,
|
|||
}
|
||||
|
||||
*accelerator = ui::Accelerator(key, modifiers);
|
||||
accelerator->shifted_char = shifted_char;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,9 +21,9 @@
|
|||
#include "shell/browser/ui/electron_menu_model.h"
|
||||
#include "shell/browser/window_list.h"
|
||||
#include "ui/base/accelerators/accelerator.h"
|
||||
#include "ui/base/accelerators/platform_accelerator_cocoa.h"
|
||||
#include "ui/base/l10n/l10n_util_mac.h"
|
||||
#include "ui/events/cocoa/cocoa_event_utils.h"
|
||||
#include "ui/events/keycodes/keyboard_code_conversion_mac.h"
|
||||
#include "ui/gfx/image/image.h"
|
||||
#include "ui/strings/grit/ui_strings.h"
|
||||
|
||||
|
@ -392,11 +392,31 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
|
|||
ui::Accelerator accelerator;
|
||||
if (model->GetAcceleratorAtWithParams(index, useDefaultAccelerator_,
|
||||
&accelerator)) {
|
||||
NSString* key_equivalent;
|
||||
NSUInteger modifier_mask;
|
||||
GetKeyEquivalentAndModifierMaskFromAccelerator(
|
||||
accelerator, &key_equivalent, &modifier_mask);
|
||||
[item setKeyEquivalent:key_equivalent];
|
||||
// Note that we are not using Chromium's
|
||||
// GetKeyEquivalentAndModifierMaskFromAccelerator API,
|
||||
// because it will convert Shift+Character to ShiftedCharacter, for
|
||||
// example Shift+/ would be converted to ?, which is against macOS HIG.
|
||||
// See also https://github.com/electron/electron/issues/21790.
|
||||
NSUInteger modifier_mask = 0;
|
||||
if (accelerator.IsCtrlDown())
|
||||
modifier_mask |= NSEventModifierFlagControl;
|
||||
if (accelerator.IsAltDown())
|
||||
modifier_mask |= NSEventModifierFlagOption;
|
||||
if (accelerator.IsCmdDown())
|
||||
modifier_mask |= NSEventModifierFlagCommand;
|
||||
unichar character;
|
||||
if (accelerator.shifted_char) {
|
||||
// When a shifted char is explicitly specified, for example Ctrl+Plus,
|
||||
// use the shifted char directly.
|
||||
character = static_cast<unichar>(*accelerator.shifted_char);
|
||||
} else {
|
||||
// Otherwise use the unshifted combinations, for example Ctrl+Shift+=.
|
||||
if (accelerator.IsShiftDown())
|
||||
modifier_mask |= NSEventModifierFlagShift;
|
||||
ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), modifier_mask,
|
||||
nullptr, &character);
|
||||
}
|
||||
[item setKeyEquivalent:[NSString stringWithFormat:@"%C", character]];
|
||||
[item setKeyEquivalentModifierMask:modifier_mask];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue