2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-14 13:12:27 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/ui/accelerator_util.h"
|
2013-05-14 13:12:27 +00:00
|
|
|
|
2013-10-21 07:33:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-05-14 13:12:27 +00:00
|
|
|
#include <string>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <vector>
|
2013-05-14 13:12:27 +00:00
|
|
|
|
2016-01-31 01:27:14 +00:00
|
|
|
#include "atom/common/keyboard_util.h"
|
2014-03-15 08:36:29 +00:00
|
|
|
#include "base/stl_util.h"
|
2013-05-14 13:12:27 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/string_split.h"
|
2014-03-15 08:36:29 +00:00
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "ui/base/models/simple_menu_model.h"
|
2013-05-14 13:12:27 +00:00
|
|
|
|
|
|
|
namespace accelerator_util {
|
|
|
|
|
|
|
|
bool StringToAccelerator(const std::string& description,
|
|
|
|
ui::Accelerator* accelerator) {
|
2014-07-28 07:29:51 +00:00
|
|
|
if (!base::IsStringASCII(description)) {
|
2013-10-21 05:39:55 +00:00
|
|
|
LOG(ERROR) << "The accelerator string can only contain ASCII characters";
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-07 11:56:23 +00:00
|
|
|
std::string shortcut(base::ToLowerASCII(description));
|
2013-05-14 13:12:27 +00:00
|
|
|
|
2015-12-07 11:56:23 +00:00
|
|
|
std::vector<std::string> tokens = base::SplitString(
|
|
|
|
shortcut, "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
|
2013-05-14 13:12:27 +00:00
|
|
|
|
|
|
|
// 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++) {
|
2013-10-21 06:25:36 +00:00
|
|
|
// We use straight comparing instead of map because the accelerator tends
|
|
|
|
// to be correct and usually only uses few special tokens.
|
2013-10-21 06:05:43 +00:00
|
|
|
if (tokens[i].size() == 1) {
|
|
|
|
bool shifted = false;
|
2015-09-18 05:33:06 +00:00
|
|
|
key = atom::KeyboardCodeFromCharCode(tokens[i][0], &shifted);
|
2013-10-21 06:05:43 +00:00
|
|
|
if (shifted)
|
|
|
|
modifiers |= ui::EF_SHIFT_DOWN;
|
2013-11-29 08:31:38 +00:00
|
|
|
} else if (tokens[i] == "ctrl" || tokens[i] == "control") {
|
2013-05-14 13:12:27 +00:00
|
|
|
modifiers |= ui::EF_CONTROL_DOWN;
|
2015-04-28 05:28:18 +00:00
|
|
|
} else if (tokens[i] == "super") {
|
|
|
|
modifiers |= ui::EF_COMMAND_DOWN;
|
|
|
|
#if defined(OS_MACOSX)
|
2013-11-29 08:31:38 +00:00
|
|
|
} else if (tokens[i] == "cmd" || tokens[i] == "command") {
|
2013-05-14 13:12:27 +00:00
|
|
|
modifiers |= ui::EF_COMMAND_DOWN;
|
2015-04-28 05:28:18 +00:00
|
|
|
#endif
|
2013-11-29 08:10:32 +00:00
|
|
|
} else if (tokens[i] == "commandorcontrol" || tokens[i] == "cmdorctrl") {
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
modifiers |= ui::EF_COMMAND_DOWN;
|
|
|
|
#else
|
|
|
|
modifiers |= ui::EF_CONTROL_DOWN;
|
|
|
|
#endif
|
2013-10-21 05:46:37 +00:00
|
|
|
} else if (tokens[i] == "alt") {
|
2013-05-14 13:12:27 +00:00
|
|
|
modifiers |= ui::EF_ALT_DOWN;
|
2013-10-21 05:46:37 +00:00
|
|
|
} else if (tokens[i] == "shift") {
|
2013-05-14 13:12:27 +00:00
|
|
|
modifiers |= ui::EF_SHIFT_DOWN;
|
2015-01-23 23:26:54 +00:00
|
|
|
} else if (tokens[i] == "plus") {
|
|
|
|
modifiers |= ui::EF_SHIFT_DOWN;
|
|
|
|
key = ui::VKEY_OEM_PLUS;
|
2013-10-21 06:25:36 +00:00
|
|
|
} else if (tokens[i] == "tab") {
|
|
|
|
key = ui::VKEY_TAB;
|
2013-10-21 07:35:54 +00:00
|
|
|
} else if (tokens[i] == "space") {
|
|
|
|
key = ui::VKEY_SPACE;
|
2013-10-21 06:25:36 +00:00
|
|
|
} else if (tokens[i] == "backspace") {
|
|
|
|
key = ui::VKEY_BACK;
|
|
|
|
} else if (tokens[i] == "delete") {
|
|
|
|
key = ui::VKEY_DELETE;
|
2014-06-14 14:25:21 +00:00
|
|
|
} else if (tokens[i] == "insert") {
|
|
|
|
key = ui::VKEY_INSERT;
|
2013-10-21 06:25:36 +00:00
|
|
|
} else if (tokens[i] == "enter" || tokens[i] == "return") {
|
|
|
|
key = ui::VKEY_RETURN;
|
|
|
|
} else if (tokens[i] == "up") {
|
|
|
|
key = ui::VKEY_UP;
|
|
|
|
} else if (tokens[i] == "down") {
|
|
|
|
key = ui::VKEY_DOWN;
|
|
|
|
} else if (tokens[i] == "left") {
|
|
|
|
key = ui::VKEY_LEFT;
|
|
|
|
} else if (tokens[i] == "right") {
|
|
|
|
key = ui::VKEY_RIGHT;
|
|
|
|
} else if (tokens[i] == "home") {
|
|
|
|
key = ui::VKEY_HOME;
|
|
|
|
} else if (tokens[i] == "end") {
|
|
|
|
key = ui::VKEY_END;
|
|
|
|
} else if (tokens[i] == "pageup") {
|
2015-07-01 13:24:51 +00:00
|
|
|
key = ui::VKEY_PRIOR;
|
|
|
|
} else if (tokens[i] == "pagedown") {
|
2013-10-21 06:25:36 +00:00
|
|
|
key = ui::VKEY_NEXT;
|
2014-06-17 21:47:35 +00:00
|
|
|
} else if (tokens[i] == "esc" || tokens[i] == "escape") {
|
2013-10-21 06:25:36 +00:00
|
|
|
key = ui::VKEY_ESCAPE;
|
|
|
|
} else if (tokens[i] == "volumemute") {
|
|
|
|
key = ui::VKEY_VOLUME_MUTE;
|
|
|
|
} else if (tokens[i] == "volumeup") {
|
|
|
|
key = ui::VKEY_VOLUME_UP;
|
|
|
|
} else if (tokens[i] == "volumedown") {
|
|
|
|
key = ui::VKEY_VOLUME_DOWN;
|
|
|
|
} else if (tokens[i] == "medianexttrack") {
|
|
|
|
key = ui::VKEY_MEDIA_NEXT_TRACK;
|
|
|
|
} else if (tokens[i] == "mediaprevioustrack") {
|
|
|
|
key = ui::VKEY_MEDIA_PREV_TRACK;
|
|
|
|
} else if (tokens[i] == "mediastop") {
|
|
|
|
key = ui::VKEY_MEDIA_STOP;
|
|
|
|
} else if (tokens[i] == "mediaplaypause") {
|
|
|
|
key = ui::VKEY_MEDIA_PLAY_PAUSE;
|
2013-10-21 07:33:19 +00:00
|
|
|
} else if (tokens[i].size() > 1 && tokens[i][0] == 'f') {
|
|
|
|
// F1 - F24.
|
|
|
|
int n;
|
2015-05-07 07:46:38 +00:00
|
|
|
if (base::StringToInt(tokens[i].c_str() + 1, &n) && n > 0 && n < 25) {
|
2013-10-21 07:33:19 +00:00
|
|
|
key = static_cast<ui::KeyboardCode>(ui::VKEY_F1 + n - 1);
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << tokens[i] << "is not available on keyboard";
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-14 13:12:27 +00:00
|
|
|
} else {
|
2013-05-16 07:24:18 +00:00
|
|
|
LOG(WARNING) << "Invalid accelerator token: " << tokens[i];
|
2013-05-14 13:12:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 06:05:43 +00:00
|
|
|
if (key == ui::VKEY_UNKNOWN) {
|
|
|
|
LOG(WARNING) << "The accelerator doesn't contain a valid key";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-14 13:12:27 +00:00
|
|
|
*accelerator = ui::Accelerator(key, modifiers);
|
2013-05-16 07:24:18 +00:00
|
|
|
SetPlatformAccelerator(accelerator);
|
2013-05-14 13:12:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-15 08:36:29 +00:00
|
|
|
void GenerateAcceleratorTable(AcceleratorTable* table, ui::MenuModel* model) {
|
|
|
|
int count = model->GetItemCount();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
ui::MenuModel::ItemType type = model->GetTypeAt(i);
|
|
|
|
if (type == ui::MenuModel::TYPE_SUBMENU) {
|
|
|
|
ui::MenuModel* submodel = model->GetSubmenuModelAt(i);
|
|
|
|
GenerateAcceleratorTable(table, submodel);
|
|
|
|
} else {
|
|
|
|
ui::Accelerator accelerator;
|
|
|
|
if (model->GetAcceleratorAt(i, &accelerator)) {
|
|
|
|
MenuItem item = { i, model };
|
|
|
|
(*table)[accelerator] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerAcceleratorTableCommand(AcceleratorTable* table,
|
|
|
|
const ui::Accelerator& accelerator) {
|
|
|
|
if (ContainsKey(*table, accelerator)) {
|
|
|
|
const accelerator_util::MenuItem& item = (*table)[accelerator];
|
|
|
|
item.model->ActivatedAt(item.position);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 13:12:27 +00:00
|
|
|
} // namespace accelerator_util
|