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:
parent
995b9dacc9
commit
b16c19ce32
4 changed files with 42 additions and 0 deletions
1
atom.gyp
1
atom.gyp
|
@ -31,6 +31,7 @@
|
||||||
'app/atom_main_delegate.h',
|
'app/atom_main_delegate.h',
|
||||||
'browser/accelerator_util.cc',
|
'browser/accelerator_util.cc',
|
||||||
'browser/accelerator_util.h',
|
'browser/accelerator_util.h',
|
||||||
|
'browser/accelerator_util_mac.mm',
|
||||||
'browser/api/atom_api_app.cc',
|
'browser/api/atom_api_app.cc',
|
||||||
'browser/api/atom_api_app.h',
|
'browser/api/atom_api_app.h',
|
||||||
'browser/api/atom_api_browser_ipc.cc',
|
'browser/api/atom_api_browser_ipc.cc',
|
||||||
|
|
|
@ -43,6 +43,7 @@ bool StringToAccelerator(const std::string& description,
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
base::SplitString(shortcut, '+', &tokens);
|
base::SplitString(shortcut, '+', &tokens);
|
||||||
if (tokens.size() < 2 || tokens.size() > 3) {
|
if (tokens.size() < 2 || tokens.size() > 3) {
|
||||||
|
LOG(WARNING) << "Invalid accelerator description: " << description;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,15 +70,18 @@ bool StringToAccelerator(const std::string& description,
|
||||||
} else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') {
|
} else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') {
|
||||||
key = static_cast<ui::KeyboardCode>(ui::VKEY_0 + (tokens[i][0] - '0'));
|
key = static_cast<ui::KeyboardCode>(ui::VKEY_0 + (tokens[i][0] - '0'));
|
||||||
} else {
|
} else {
|
||||||
|
LOG(WARNING) << "Invalid accelerator character: " << tokens[i];
|
||||||
key = ui::VKEY_UNKNOWN;
|
key = ui::VKEY_UNKNOWN;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
LOG(WARNING) << "Invalid accelerator token: " << tokens[i];
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*accelerator = ui::Accelerator(key, modifiers);
|
*accelerator = ui::Accelerator(key, modifiers);
|
||||||
|
SetPlatformAccelerator(accelerator);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,9 @@ namespace accelerator_util {
|
||||||
bool StringToAccelerator(const std::string& description,
|
bool StringToAccelerator(const std::string& description,
|
||||||
ui::Accelerator* accelerator);
|
ui::Accelerator* accelerator);
|
||||||
|
|
||||||
|
// Set platform accelerator for the Accelerator.
|
||||||
|
void SetPlatformAccelerator(ui::Accelerator* accelerator);
|
||||||
|
|
||||||
} // namespace accelerator_util
|
} // namespace accelerator_util
|
||||||
|
|
||||||
#endif // BROWSER_ACCELERATOR_UTIL_H_
|
#endif // BROWSER_ACCELERATOR_UTIL_H_
|
||||||
|
|
34
browser/accelerator_util_mac.mm
Normal file
34
browser/accelerator_util_mac.mm
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue