From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 4 Oct 2018 14:57:02 -0700 Subject: fix: improve shortcut text of Accelerator This patch makes three changes to Accelerator::GetShortcutText to improve shortcut display text in menus: 1. Ctrl-Alt- accelerators show as Ctrl-Alt- instead of as Ctrl- 2. F2-F24 accelerators show up as such 3. Ctrl-Shift-= and Ctrl-Plus show up as such diff --git a/ui/base/accelerators/accelerator.cc b/ui/base/accelerators/accelerator.cc index 2b38e56d26ae7738d82e14094c2d63357a9eb1ef..23e73566e444ab9ff3e409b0b87024be327e2dc4 100644 --- a/ui/base/accelerators/accelerator.cc +++ b/ui/base/accelerators/accelerator.cc @@ -11,6 +11,7 @@ #include "base/i18n/rtl.h" #include "base/notreached.h" #include "base/strings/string_util.h" +#include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" #include "build/build_config.h" #include "build/chromeos_buildflags.h" @@ -238,6 +239,11 @@ std::u16string Accelerator::GetShortcutText() const { #endif if (shortcut.empty()) { + // When a shifted char is explicitly specified, for example Ctrl+Plus, + // use the shifted char directly. + if (shifted_char) { + shortcut += *shifted_char; + } else { #if BUILDFLAG(IS_WIN) // Our fallback is to try translate the key code to a regular character // unless it is one of digits (VK_0 to VK_9). Some keyboard @@ -261,6 +267,10 @@ std::u16string Accelerator::GetShortcutText() const { shortcut += static_cast(base::ToUpperASCII(c)); #endif + } + if (key_code_ > VKEY_F1 && key_code_ <= VKEY_F24) + shortcut = base::UTF8ToUTF16( + base::StringPrintf("F%d", key_code_ - VKEY_F1 + 1)); } #if BUILDFLAG(IS_MAC) @@ -445,7 +455,7 @@ std::u16string Accelerator::ApplyLongFormModifiers( const std::u16string& shortcut) const { std::u16string result = shortcut; - if (IsShiftDown()) + if (!shifted_char && IsShiftDown()) result = ApplyModifierToAcceleratorString(result, IDS_APP_SHIFT_KEY); // Note that we use 'else-if' in order to avoid using Ctrl+Alt as a shortcut. @@ -453,7 +463,7 @@ std::u16string Accelerator::ApplyLongFormModifiers( // more information. if (IsCtrlDown()) result = ApplyModifierToAcceleratorString(result, IDS_APP_CTRL_KEY); - else if (IsAltDown()) + if (IsAltDown()) result = ApplyModifierToAcceleratorString(result, IDS_APP_ALT_KEY); if (IsCmdDown()) { diff --git a/ui/base/accelerators/accelerator.h b/ui/base/accelerators/accelerator.h index bcc44522d01713aad2231417e1e6c89c9004dc29..1e2836e2de583cb3bd6d7fca5cc6b34f0f6e8418 100644 --- a/ui/base/accelerators/accelerator.h +++ b/ui/base/accelerators/accelerator.h @@ -16,6 +16,7 @@ #include #include "base/component_export.h" +#include "third_party/abseil-cpp/absl/types/optional.h" #include "base/time/time.h" #include "build/build_config.h" #include "ui/events/event_constants.h" @@ -130,6 +131,8 @@ class COMPONENT_EXPORT(UI_BASE) Accelerator { return interrupted_by_mouse_event_; } + absl::optional shifted_char; + private: friend class AcceleratorTestMac; std::u16string ApplyLongFormModifiers(const std::u16string& shortcut) const;