Convert generic accelerator to platform accelerator.

When creating menus, the accelerators must be converted to platform
accelerators before they can be used.
This commit is contained in:
Cheng Zhao 2013-05-16 15:24:18 +08:00
parent 995b9dacc9
commit b16c19ce32
4 changed files with 42 additions and 0 deletions

View file

@ -31,6 +31,7 @@
'app/atom_main_delegate.h',
'browser/accelerator_util.cc',
'browser/accelerator_util.h',
'browser/accelerator_util_mac.mm',
'browser/api/atom_api_app.cc',
'browser/api/atom_api_app.h',
'browser/api/atom_api_browser_ipc.cc',

View file

@ -43,6 +43,7 @@ bool StringToAccelerator(const std::string& description,
std::vector<std::string> tokens;
base::SplitString(shortcut, '+', &tokens);
if (tokens.size() < 2 || tokens.size() > 3) {
LOG(WARNING) << "Invalid accelerator description: " << description;
return false;
}
@ -69,15 +70,18 @@ bool StringToAccelerator(const std::string& description,
} else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') {
key = static_cast<ui::KeyboardCode>(ui::VKEY_0 + (tokens[i][0] - '0'));
} else {
LOG(WARNING) << "Invalid accelerator character: " << tokens[i];
key = ui::VKEY_UNKNOWN;
break;
}
} else {
LOG(WARNING) << "Invalid accelerator token: " << tokens[i];
return false;
}
}
*accelerator = ui::Accelerator(key, modifiers);
SetPlatformAccelerator(accelerator);
return true;
}

View file

@ -17,6 +17,9 @@ namespace accelerator_util {
bool StringToAccelerator(const std::string& description,
ui::Accelerator* accelerator);
// Set platform accelerator for the Accelerator.
void SetPlatformAccelerator(ui::Accelerator* accelerator);
} // namespace accelerator_util
#endif // BROWSER_ACCELERATOR_UTIL_H_

View file

@ -0,0 +1,34 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "browser/accelerator_util.h"
#include "ui/base/accelerators/accelerator.h"
#import "ui/base/accelerators/platform_accelerator_cocoa.h"
#import "ui/base/keycodes/keyboard_code_conversion_mac.h"
namespace accelerator_util {
void SetPlatformAccelerator(ui::Accelerator* accelerator) {
unichar character;
unichar characterIgnoringModifiers;
ui::MacKeyCodeForWindowsKeyCode(accelerator->key_code(),
0,
&character,
&characterIgnoringModifiers);
NSString* characters =
[[[NSString alloc] initWithCharacters:&character length:1] autorelease];
NSUInteger modifiers =
(accelerator->IsCtrlDown() ? NSControlKeyMask : 0) |
(accelerator->IsCmdDown() ? NSCommandKeyMask : 0) |
(accelerator->IsAltDown() ? NSAlternateKeyMask : 0) |
(accelerator->IsShiftDown() ? NSShiftKeyMask : 0);
scoped_ptr<ui::PlatformAccelerator> platform_accelerator(
new ui::PlatformAcceleratorCocoa(characters, modifiers));
accelerator->set_platform_accelerator(platform_accelerator.Pass());
}
} // namespace accelerator_util