accelerator.patch 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Cheng Zhao <[email protected]>
  3. Date: Thu, 4 Oct 2018 14:57:02 -0700
  4. Subject: fix: improve shortcut text of Accelerator
  5. This patch makes three changes to Accelerator::GetShortcutText to improve shortcut display text in menus:
  6. 1. Ctrl-Alt-<Key> accelerators show as Ctrl-Alt-<Key> instead of as Ctrl-<Key>
  7. 2. F2-F24 accelerators show up as such
  8. 3. Ctrl-Shift-= and Ctrl-Plus show up as such
  9. diff --git a/ui/base/accelerators/accelerator.cc b/ui/base/accelerators/accelerator.cc
  10. index 9fca6ff3e62204095ff0edc6fafce3a61cd2ff5c..089f8b818018a600cc8c90811f09374a1f702d8b 100644
  11. --- a/ui/base/accelerators/accelerator.cc
  12. +++ b/ui/base/accelerators/accelerator.cc
  13. @@ -11,6 +11,7 @@
  14. #include "base/i18n/rtl.h"
  15. #include "base/notreached.h"
  16. #include "base/strings/string_util.h"
  17. +#include "base/strings/stringprintf.h"
  18. #include "base/strings/utf_string_conversions.h"
  19. #include "build/build_config.h"
  20. #include "build/chromeos_buildflags.h"
  21. @@ -238,6 +239,11 @@ std::u16string Accelerator::GetShortcutText() const {
  22. #endif
  23. if (shortcut.empty()) {
  24. + // When a shifted char is explicitly specified, for example Ctrl+Plus,
  25. + // use the shifted char directly.
  26. + if (shifted_char) {
  27. + shortcut += *shifted_char;
  28. + } else {
  29. #if BUILDFLAG(IS_WIN)
  30. // Our fallback is to try translate the key code to a regular character
  31. // unless it is one of digits (VK_0 to VK_9). Some keyboard
  32. @@ -261,6 +267,10 @@ std::u16string Accelerator::GetShortcutText() const {
  33. shortcut +=
  34. static_cast<std::u16string::value_type>(base::ToUpperASCII(c));
  35. #endif
  36. + }
  37. + if (key_code_ > VKEY_F1 && key_code_ <= VKEY_F24)
  38. + shortcut = base::UTF8ToUTF16(
  39. + base::StringPrintf("F%d", key_code_ - VKEY_F1 + 1));
  40. }
  41. #if BUILDFLAG(IS_MAC)
  42. @@ -447,7 +457,7 @@ std::u16string Accelerator::ApplyLongFormModifiers(
  43. const std::u16string& shortcut) const {
  44. std::u16string result = shortcut;
  45. - if (IsShiftDown())
  46. + if (!shifted_char && IsShiftDown())
  47. result = ApplyModifierToAcceleratorString(result, IDS_APP_SHIFT_KEY);
  48. // Note that we use 'else-if' in order to avoid using Ctrl+Alt as a shortcut.
  49. @@ -455,7 +465,7 @@ std::u16string Accelerator::ApplyLongFormModifiers(
  50. // more information.
  51. if (IsCtrlDown())
  52. result = ApplyModifierToAcceleratorString(result, IDS_APP_CTRL_KEY);
  53. - else if (IsAltDown())
  54. + if (IsAltDown())
  55. result = ApplyModifierToAcceleratorString(result, IDS_APP_ALT_KEY);
  56. if (IsCmdDown()) {
  57. diff --git a/ui/base/accelerators/accelerator.h b/ui/base/accelerators/accelerator.h
  58. index e0d9df439d120c0a47f55666b3818c7ba6796e70..283c6283f5aeaae1a5436e5fbb17ce2db4a9034e 100644
  59. --- a/ui/base/accelerators/accelerator.h
  60. +++ b/ui/base/accelerators/accelerator.h
  61. @@ -16,6 +16,7 @@
  62. #include <utility>
  63. #include "base/component_export.h"
  64. +#include "third_party/abseil-cpp/absl/types/optional.h"
  65. #include "base/time/time.h"
  66. #include "build/build_config.h"
  67. #include "ui/events/event_constants.h"
  68. @@ -130,6 +131,8 @@ class COMPONENT_EXPORT(UI_BASE) Accelerator {
  69. return interrupted_by_mouse_event_;
  70. }
  71. + absl::optional<char16_t> shifted_char;
  72. +
  73. private:
  74. friend class AcceleratorTestMac;
  75. std::u16string ApplyLongFormModifiers(const std::u16string& shortcut) const;