Skip to content

Commit

Permalink
Add a WebUI page where it's possible to view all commands and their
Browse files Browse the repository at this point in the history
accelerators
  • Loading branch information
fallaciousreasoning committed Feb 17, 2023
1 parent cb5521e commit b5d6f44
Show file tree
Hide file tree
Showing 37 changed files with 1,403 additions and 28 deletions.
79 changes: 79 additions & 0 deletions app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ import("//build/config/locales.gni")

source_set("command_ids") {
sources = [ "brave_command_ids.h" ]

deps = [
"//base",
"//chrome/app:command_ids",
]

if (!is_android && !is_ios) {
sources += [
"command_utils.cc",
"command_utils.h",
]

deps += [ "//brave/components/commands/common" ]
}
}

brave_grit("brave_generated_resources_grit") {
Expand All @@ -36,5 +50,70 @@ brave_grit("brave_generated_resources_grit") {
}
}

source_set("browser_tests") {
testonly = true

if (!is_android && !is_ios) {
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

sources = [
"brave_main_delegate_browsertest.cc",
"brave_main_delegate_runtime_flags_browsertest.cc",
"command_utils_browsertest.cc",
]

deps = [
":command_ids",
"//base",
"//brave/components/commands/common",
"//chrome/app:command_ids",
"//chrome/browser",
"//chrome/browser/enterprise/connectors/analysis:features",
"//chrome/browser/ui",
"//chrome/common:chrome_features",
"//chrome/common:non_code_constants",
"//chrome/test",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//components/autofill/core/common",
"//components/commerce/core:feature_list",
"//components/component_updater",
"//components/embedder_support",
"//components/history_clusters/core",
"//components/language/core/common",
"//components/lens",
"//components/network_time",
"//components/omnibox/browser",
"//components/optimization_guide/core",
"//components/permissions",
"//components/privacy_sandbox",
"//components/reading_list/features:flags",
"//components/segmentation_platform/public",
"//components/send_tab_to_self",
"//components/subresource_filter/core/common",
"//components/translate/core/common",
"//extensions/common",
"//services/device/public/cpp:device_features",
"//testing/gtest",
]
}
}

source_set("unit_tests") {
testonly = true

if (!is_android && !is_ios) {
sources = [ "command_utils_unittest.cc" ]
deps = [
":command_ids",
"//base",
"//base/test:test_support",
"//brave/components/commands/common",
"//chrome/browser/ui/views",
"//testing/gtest",
]
}
}

group("app") {
}
246 changes: 246 additions & 0 deletions app/command_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/app/command_utils.h"

#include <iterator>
#include <map>
#include <string>
#include <vector>

#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "brave/app/brave_command_ids.h"
#include "brave/components/commands/common/features.h"
#include "chrome/app/chrome_command_ids.h"

#define ADD_UNTRANSLATED_COMMAND(name) \
{ \
IDC_##name, { \
GetName(#name) \
} \
}

namespace commands {
namespace {

std::string GetName(const std::string& raw_name) {
auto words = base::SplitString(raw_name, "_", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
for (auto& word : words) {
if (word.size() == 1) {
continue;
}
word = word[0] + base::ToLowerASCII(word.substr(1));
}
return base::JoinString(words, " ");
}

const base::flat_map<uint16_t, std::string>& GetCommandInfo() {
static const base::NoDestructor<base::flat_map<uint16_t, std::string>>
kCommands({
// Navigation commands.
ADD_UNTRANSLATED_COMMAND(BACK), ADD_UNTRANSLATED_COMMAND(FORWARD),
ADD_UNTRANSLATED_COMMAND(RELOAD),
ADD_UNTRANSLATED_COMMAND(RELOAD_BYPASSING_CACHE),
ADD_UNTRANSLATED_COMMAND(RELOAD_CLEARING_CACHE),
ADD_UNTRANSLATED_COMMAND(HOME), ADD_UNTRANSLATED_COMMAND(STOP),

// Window management commands
ADD_UNTRANSLATED_COMMAND(NEW_WINDOW),
ADD_UNTRANSLATED_COMMAND(NEW_INCOGNITO_WINDOW),
ADD_UNTRANSLATED_COMMAND(CLOSE_WINDOW),
ADD_UNTRANSLATED_COMMAND(NEW_TAB),
ADD_UNTRANSLATED_COMMAND(CLOSE_TAB),
ADD_UNTRANSLATED_COMMAND(SELECT_NEXT_TAB),
ADD_UNTRANSLATED_COMMAND(SELECT_PREVIOUS_TAB),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_0),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_1),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_2),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_3),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_4),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_5),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_6),
ADD_UNTRANSLATED_COMMAND(SELECT_TAB_7),
ADD_UNTRANSLATED_COMMAND(SELECT_LAST_TAB),
ADD_UNTRANSLATED_COMMAND(MOVE_TAB_TO_NEW_WINDOW),
ADD_UNTRANSLATED_COMMAND(DUPLICATE_TAB),
ADD_UNTRANSLATED_COMMAND(RESTORE_TAB),
ADD_UNTRANSLATED_COMMAND(FULLSCREEN),
ADD_UNTRANSLATED_COMMAND(EXIT),
ADD_UNTRANSLATED_COMMAND(MOVE_TAB_NEXT),
ADD_UNTRANSLATED_COMMAND(MOVE_TAB_PREVIOUS),
ADD_UNTRANSLATED_COMMAND(SEARCH),
ADD_UNTRANSLATED_COMMAND(DEBUG_FRAME_TOGGLE),
ADD_UNTRANSLATED_COMMAND(WINDOW_MENU),
ADD_UNTRANSLATED_COMMAND(MINIMIZE_WINDOW),
ADD_UNTRANSLATED_COMMAND(MAXIMIZE_WINDOW),
ADD_UNTRANSLATED_COMMAND(NAME_WINDOW),

#if BUILDFLAG(IS_LINUX)
ADD_UNTRANSLATED_COMMAND(USE_SYSTEM_TITLE_BAR),
ADD_UNTRANSLATED_COMMAND(RESTORE_WINDOW),
#endif

// Web app window commands
ADD_UNTRANSLATED_COMMAND(OPEN_IN_PWA_WINDOW),
ADD_UNTRANSLATED_COMMAND(COPY_URL),
ADD_UNTRANSLATED_COMMAND(SITE_SETTINGS),
ADD_UNTRANSLATED_COMMAND(WEB_APP_MENU_APP_INFO),

// Page related commands
ADD_UNTRANSLATED_COMMAND(BOOKMARK_THIS_TAB),
ADD_UNTRANSLATED_COMMAND(BOOKMARK_ALL_TABS),
ADD_UNTRANSLATED_COMMAND(VIEW_SOURCE),
ADD_UNTRANSLATED_COMMAND(PRINT),
ADD_UNTRANSLATED_COMMAND(SAVE_PAGE),
ADD_UNTRANSLATED_COMMAND(EMAIL_PAGE_LOCATION),
ADD_UNTRANSLATED_COMMAND(BASIC_PRINT),
ADD_UNTRANSLATED_COMMAND(TRANSLATE_PAGE),
ADD_UNTRANSLATED_COMMAND(WINDOW_MUTE_SITE),
ADD_UNTRANSLATED_COMMAND(WINDOW_PIN_TAB),
ADD_UNTRANSLATED_COMMAND(WINDOW_GROUP_TAB),
ADD_UNTRANSLATED_COMMAND(QRCODE_GENERATOR),
ADD_UNTRANSLATED_COMMAND(WINDOW_CLOSE_TABS_TO_RIGHT),
ADD_UNTRANSLATED_COMMAND(WINDOW_CLOSE_OTHER_TABS),
ADD_UNTRANSLATED_COMMAND(NEW_TAB_TO_RIGHT),

// Page manipulation for specific tab
ADD_UNTRANSLATED_COMMAND(MUTE_TARGET_SITE),
ADD_UNTRANSLATED_COMMAND(PIN_TARGET_TAB),
ADD_UNTRANSLATED_COMMAND(GROUP_TARGET_TAB),
ADD_UNTRANSLATED_COMMAND(DUPLICATE_TARGET_TAB),

// Edit
ADD_UNTRANSLATED_COMMAND(CUT), ADD_UNTRANSLATED_COMMAND(COPY),
ADD_UNTRANSLATED_COMMAND(PASTE),
ADD_UNTRANSLATED_COMMAND(EDIT_MENU),

// Find
ADD_UNTRANSLATED_COMMAND(FIND), ADD_UNTRANSLATED_COMMAND(FIND_NEXT),
ADD_UNTRANSLATED_COMMAND(FIND_PREVIOUS),
ADD_UNTRANSLATED_COMMAND(CLOSE_FIND_OR_STOP),

// Zoom
ADD_UNTRANSLATED_COMMAND(ZOOM_MENU),
ADD_UNTRANSLATED_COMMAND(ZOOM_PLUS),
ADD_UNTRANSLATED_COMMAND(ZOOM_NORMAL),
ADD_UNTRANSLATED_COMMAND(ZOOM_MINUS),
ADD_UNTRANSLATED_COMMAND(ZOOM_PERCENT_DISPLAY),

// Focus
ADD_UNTRANSLATED_COMMAND(FOCUS_TOOLBAR),
ADD_UNTRANSLATED_COMMAND(FOCUS_LOCATION),
ADD_UNTRANSLATED_COMMAND(FOCUS_SEARCH),
ADD_UNTRANSLATED_COMMAND(FOCUS_MENU_BAR),
ADD_UNTRANSLATED_COMMAND(FOCUS_NEXT_PANE),
ADD_UNTRANSLATED_COMMAND(FOCUS_PREVIOUS_PANE),
ADD_UNTRANSLATED_COMMAND(FOCUS_BOOKMARKS),
ADD_UNTRANSLATED_COMMAND(FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY),
ADD_UNTRANSLATED_COMMAND(FOCUS_WEB_CONTENTS_PANE),

// UI bits
ADD_UNTRANSLATED_COMMAND(OPEN_FILE),
ADD_UNTRANSLATED_COMMAND(CREATE_SHORTCUT),
ADD_UNTRANSLATED_COMMAND(DEVELOPER_MENU),
ADD_UNTRANSLATED_COMMAND(DEV_TOOLS),
ADD_UNTRANSLATED_COMMAND(DEV_TOOLS_CONSOLE),
ADD_UNTRANSLATED_COMMAND(TASK_MANAGER),
ADD_UNTRANSLATED_COMMAND(DEV_TOOLS_DEVICES),
ADD_UNTRANSLATED_COMMAND(FEEDBACK),
ADD_UNTRANSLATED_COMMAND(SHOW_BOOKMARK_BAR),
ADD_UNTRANSLATED_COMMAND(SHOW_HISTORY),
ADD_UNTRANSLATED_COMMAND(SHOW_BOOKMARK_MANAGER),
ADD_UNTRANSLATED_COMMAND(SHOW_DOWNLOADS),
ADD_UNTRANSLATED_COMMAND(CLEAR_BROWSING_DATA),
ADD_UNTRANSLATED_COMMAND(IMPORT_SETTINGS),
ADD_UNTRANSLATED_COMMAND(OPTIONS),
ADD_UNTRANSLATED_COMMAND(EDIT_SEARCH_ENGINES),
ADD_UNTRANSLATED_COMMAND(VIEW_PASSWORDS),
ADD_UNTRANSLATED_COMMAND(ABOUT),
ADD_UNTRANSLATED_COMMAND(HELP_PAGE_VIA_KEYBOARD),
ADD_UNTRANSLATED_COMMAND(SHOW_APP_MENU),
ADD_UNTRANSLATED_COMMAND(MANAGE_EXTENSIONS),
ADD_UNTRANSLATED_COMMAND(DEV_TOOLS_INSPECT),
ADD_UNTRANSLATED_COMMAND(BOOKMARKS_MENU),
ADD_UNTRANSLATED_COMMAND(SHOW_AVATAR_MENU),
ADD_UNTRANSLATED_COMMAND(TOGGLE_REQUEST_TABLET_SITE),
ADD_UNTRANSLATED_COMMAND(DEV_TOOLS_TOGGLE),
ADD_UNTRANSLATED_COMMAND(TAKE_SCREENSHOT),
ADD_UNTRANSLATED_COMMAND(TOGGLE_FULLSCREEN_TOOLBAR),
ADD_UNTRANSLATED_COMMAND(INSTALL_PWA),
ADD_UNTRANSLATED_COMMAND(PASTE_AND_GO),
ADD_UNTRANSLATED_COMMAND(SHOW_FULL_URLS),
ADD_UNTRANSLATED_COMMAND(CARET_BROWSING_TOGGLE),
ADD_UNTRANSLATED_COMMAND(TOGGLE_QUICK_COMMANDS),

// Media
ADD_UNTRANSLATED_COMMAND(CONTENT_CONTEXT_PLAYPAUSE),
ADD_UNTRANSLATED_COMMAND(CONTENT_CONTEXT_MUTE),
ADD_UNTRANSLATED_COMMAND(CONTENT_CONTEXT_LOOP),
ADD_UNTRANSLATED_COMMAND(CONTENT_CONTEXT_CONTROLS),

#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
// Screen AI Visual Annotations.
ADD_UNTRANSLATED_COMMAND(RUN_SCREEN_AI_VISUAL_ANNOTATIONS),
#endif

// Tab search
ADD_UNTRANSLATED_COMMAND(TAB_SEARCH),
ADD_UNTRANSLATED_COMMAND(TAB_SEARCH_CLOSE),

// Brave Commands
ADD_UNTRANSLATED_COMMAND(SHOW_BRAVE_REWARDS),
ADD_UNTRANSLATED_COMMAND(NEW_TOR_CONNECTION_FOR_SITE),
ADD_UNTRANSLATED_COMMAND(NEW_OFFTHERECORD_WINDOW_TOR),
ADD_UNTRANSLATED_COMMAND(SHOW_BRAVE_SYNC),
ADD_UNTRANSLATED_COMMAND(SHOW_BRAVE_WALLET),
ADD_UNTRANSLATED_COMMAND(ADD_NEW_PROFILE),
ADD_UNTRANSLATED_COMMAND(OPEN_GUEST_PROFILE),
ADD_UNTRANSLATED_COMMAND(SHOW_BRAVE_WALLET_PANEL),
ADD_UNTRANSLATED_COMMAND(SHOW_BRAVE_VPN_PANEL),
ADD_UNTRANSLATED_COMMAND(TOGGLE_BRAVE_VPN_TOOLBAR_BUTTON),
ADD_UNTRANSLATED_COMMAND(MANAGE_BRAVE_VPN_PLAN),
ADD_UNTRANSLATED_COMMAND(TOGGLE_BRAVE_VPN),
ADD_UNTRANSLATED_COMMAND(COPY_CLEAN_LINK),
ADD_UNTRANSLATED_COMMAND(SIDEBAR_TOGGLE_POSITION),
ADD_UNTRANSLATED_COMMAND(TOGGLE_TAB_MUTE)
});
return *kCommands;
}

} // namespace

const std::vector<uint16_t>& GetCommands() {
DCHECK(base::FeatureList::IsEnabled(features::kBraveCommands))
<< "This should only be used when |kBraveCommands| is enabled.";
static base::NoDestructor<std::vector<uint16_t>> result([]() {
std::vector<uint16_t> result;
base::ranges::transform(
GetCommandInfo(), std::back_inserter(result),
/*id projection*/
&base::flat_map<uint16_t, std::string>::value_type::first);
return result;
}());

return *result;
}

const std::string& GetCommandName(int command_id) {
DCHECK(base::FeatureList::IsEnabled(features::kBraveCommands))
<< "This should only be used when |kBraveCommands| is enabled.";
const auto& info = GetCommandInfo();
auto it = info.find(command_id);
CHECK(it != info.end()) << "Unknown command " << command_id
<< ". This function should only be used for known "
"commands (i.e. commands in |GetCommandInfo|). "
"This command should probably be added.";
return it->second;
}

} // namespace commands
26 changes: 26 additions & 0 deletions app/command_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_APP_COMMAND_UTILS_H_
#define BRAVE_APP_COMMAND_UTILS_H_

#include <string>
#include <vector>

namespace commands {

// Gets the command ids of all commands which don't require parameters and can
// be executed in the main browser window. This is used for listing the
// shortcuts available to users and will eventually be used to allow configuring
// shortcuts.
const std::vector<uint16_t>& GetCommands();

// Gets a string representing a command. In future this will be translated, but
// while we're prototyping the feature it will always returns English strings.
const std::string& GetCommandName(int command_id);

} // namespace commands

#endif // BRAVE_APP_COMMAND_UTILS_H_
Loading

0 comments on commit b5d6f44

Please sign in to comment.