From: Cheng Zhao Date: Tue, 14 May 2013 13:12:27 +0000 (+0800) Subject: Add StringToAccelerator to parse a string as an accelerator. X-Git-Tag: v0.1.0~177 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=014b0a9a3899f8e6ca4fc2ff8044ea93f0f1d723;p=electron.git Add StringToAccelerator to parse a string as an accelerator. --- diff --git a/atom.gyp b/atom.gyp index 5b366f69..01fc0c14 100644 --- a/atom.gyp +++ b/atom.gyp @@ -29,6 +29,8 @@ 'lib_sources': [ 'app/atom_main_delegate.cc', 'app/atom_main_delegate.h', + 'browser/accelerator_util.cc', + 'browser/accelerator_util.h', 'browser/api/atom_api_app.cc', 'browser/api/atom_api_app.h', 'browser/api/atom_api_browser_ipc.cc', diff --git a/browser/accelerator_util.cc b/browser/accelerator_util.cc new file mode 100644 index 00000000..74c5ff72 --- /dev/null +++ b/browser/accelerator_util.cc @@ -0,0 +1,84 @@ +// 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 + +#include "base/string_util.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_split.h" +#include "ui/base/accelerators/accelerator.h" + +namespace accelerator_util { + +namespace { + +// For Mac, we convert "Ctrl" to "Command" and "MacCtrl" to "Ctrl". Other +// platforms leave the shortcut untouched. +std::string NormalizeShortcutSuggestion(const std::string& suggestion) { +#if !defined(OS_MACOSX) + return suggestion; +#endif + + std::string key; + std::vector tokens; + base::SplitString(suggestion, '+', &tokens); + for (size_t i = 0; i < tokens.size(); i++) { + if (tokens[i] == "Ctrl") + tokens[i] = "Command"; + else if (tokens[i] == "MacCtrl") + tokens[i] = "Ctrl"; + } + return JoinString(tokens, '+'); +} + +} // namespace + +bool StringToAccelerator(const std::string& description, + ui::Accelerator* accelerator) { + std::string shortcut(NormalizeShortcutSuggestion(description)); + + std::vector tokens; + base::SplitString(shortcut, '+', &tokens); + if (tokens.size() < 2 || tokens.size() > 3) { + return false; + } + + // Now, parse it into an accelerator. + int modifiers = ui::EF_NONE; + ui::KeyboardCode key = ui::VKEY_UNKNOWN; + for (size_t i = 0; i < tokens.size(); i++) { + if (tokens[i] == "Ctrl") { + modifiers |= ui::EF_CONTROL_DOWN; + } else if (tokens[i] == "Command") { + modifiers |= ui::EF_COMMAND_DOWN; + } else if (tokens[i] == "Alt") { + modifiers |= ui::EF_ALT_DOWN; + } else if (tokens[i] == "Shift") { + modifiers |= ui::EF_SHIFT_DOWN; + } else if (tokens[i].size() == 1) { + if (key != ui::VKEY_UNKNOWN) { + // Multiple key assignments. + key = ui::VKEY_UNKNOWN; + break; + } + if (tokens[i][0] >= 'A' && tokens[i][0] <= 'Z') { + key = static_cast(ui::VKEY_A + (tokens[i][0] - 'A')); + } else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') { + key = static_cast(ui::VKEY_0 + (tokens[i][0] - '0')); + } else { + key = ui::VKEY_UNKNOWN; + break; + } + } else { + return false; + } + } + + *accelerator = ui::Accelerator(key, modifiers); + return true; +} + +} // namespace accelerator_util diff --git a/browser/accelerator_util.h b/browser/accelerator_util.h new file mode 100644 index 00000000..200f4a72 --- /dev/null +++ b/browser/accelerator_util.h @@ -0,0 +1,22 @@ +// 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. + +#ifndef BROWSER_ACCELERATOR_UTIL_H_ +#define BROWSER_ACCELERATOR_UTIL_H_ + +#include + +namespace ui { +class Accelerator; +} + +namespace accelerator_util { + +// Parse a string as an accelerator. +bool StringToAccelerator(const std::string& description, + ui::Accelerator* accelerator); + +} // namespace accelerator_util + +#endif // BROWSER_ACCELERATOR_UTIL_H_