Commit 6f245b1e authored by mlcui's avatar mlcui Committed by Commit Bot

Shortcut viewer: Determine string to use based on shortcut

Most shortcuts in the shortcut viewer use one of four strings,
representing a single key with 0-3 modifiers. This is currently
hard-coded with the shortcuts. This CL refactors that to automatically
determine the correct string based on the number of modifiers,
eliminating the need to specify the shortcut string for most shortcuts.

This refactor is intended to make way for having a runtime-determined
number of modifiers for a given shortcut, as #new-shortcut-mapping
changes shortcuts involving Shift - for those shortcuts, we need to
display the shifted key instead of Shift + unshifted key.

Bug: 1139080
Change-Id: I8bb2c71a31fb1eb546aee88d6690990afd6c2f92
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2494205
Commit-Queue: Michael Cui <mlcui@google.com>
Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820643}
parent 8f56e062
......@@ -18,7 +18,7 @@ bool AcceleratorId::operator<(const AcceleratorId& other) const {
KeyboardShortcutItem::KeyboardShortcutItem(
const std::vector<ShortcutCategory>& categories,
int description_message_id,
int shortcut_message_id,
base::Optional<int> shortcut_message_id,
const std::vector<AcceleratorId>& accelerator_ids,
const std::vector<ui::KeyboardCode>& shortcut_key_codes)
: categories(categories),
......
......@@ -9,6 +9,7 @@
#include "ash/shortcut_viewer/ksv_export.h"
#include "base/macros.h"
#include "base/optional.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace keyboard_shortcut_viewer {
......@@ -56,7 +57,7 @@ struct KSV_EXPORT KeyboardShortcutItem {
KeyboardShortcutItem(
const std::vector<ShortcutCategory>& categories,
int description_message_id,
int shortcut_message_id,
base::Optional<int> shortcut_message_id,
const std::vector<AcceleratorId>& accelerator_ids = {},
const std::vector<ui::KeyboardCode>& shortcut_key_codes = {});
explicit KeyboardShortcutItem(const KeyboardShortcutItem& other);
......@@ -70,7 +71,9 @@ struct KSV_EXPORT KeyboardShortcutItem {
// Id of the message template resource used to list the keys making up the
// shortcut.
int shortcut_message_id;
// If missing, automatically determine the ID based on the number of
// `shortcut_key_codes`.
base::Optional<int> shortcut_message_id;
// Multiple accelerators can be mapped to the same KeyboardShortcutItem.
// |shortcut_key_codes| could be auto-generated from |accelerator_ids| to
......
......@@ -14,6 +14,7 @@
#include "ash/shortcut_viewer/views/bubble_view.h"
#include "base/i18n/rtl.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -100,18 +101,44 @@ KeyboardShortcutItemView::KeyboardShortcutItemView(
: accessible_name);
}
int shortcut_message_id;
if (item.shortcut_message_id) {
shortcut_message_id = *item.shortcut_message_id;
} else {
// Automatically determine the shortcut message based on the number of
// shortcut_key_codes.
// As there are separators inserted between the modifiers, a shortcut with
// N modifiers has 2*N + 1 shortcut_key_codes.
switch (item.shortcut_key_codes.size()) {
case 1:
shortcut_message_id = IDS_KSV_SHORTCUT_ONE_KEY;
break;
case 3:
shortcut_message_id = IDS_KSV_SHORTCUT_ONE_MODIFIER_ONE_KEY;
break;
case 5:
shortcut_message_id = IDS_KSV_SHORTCUT_TWO_MODIFIERS_ONE_KEY;
break;
case 7:
shortcut_message_id = IDS_KSV_SHORTCUT_THREE_MODIFIERS_ONE_KEY;
break;
default:
NOTREACHED() << "Automatically determined shortcut has "
<< item.shortcut_key_codes.size() << " key codes.";
}
}
base::string16 shortcut_string;
base::string16 accessible_string;
if (replacement_strings.empty()) {
shortcut_string = l10n_util::GetStringUTF16(has_invalid_dom_key
? IDS_KSV_KEY_NO_MAPPING
: item.shortcut_message_id);
shortcut_string = l10n_util::GetStringUTF16(
has_invalid_dom_key ? IDS_KSV_KEY_NO_MAPPING : shortcut_message_id);
accessible_string = shortcut_string;
} else {
shortcut_string = l10n_util::GetStringFUTF16(item.shortcut_message_id,
shortcut_string = l10n_util::GetStringFUTF16(shortcut_message_id,
replacement_strings, &offsets);
accessible_string = l10n_util::GetStringFUTF16(
item.shortcut_message_id, accessible_names, /*offsets=*/nullptr);
shortcut_message_id, accessible_names, /*offsets=*/nullptr);
}
shortcut_label_view_ = AddChildView(std::make_unique<views::StyledLabel>());
shortcut_label_view_->SetText(shortcut_string);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment