accelerator.patch 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 5590f9d425229d87373c5b53651d2e2d17b779e4..407cbd7e7811e3053e35e26f24e3c049cdd59a46 100644
  11. --- a/ui/base/accelerators/accelerator.cc
  12. +++ b/ui/base/accelerators/accelerator.cc
  13. @@ -12,6 +12,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 "base/types/cxx23_to_underlying.h"
  20. #include "build/build_config.h"
  21. @@ -109,6 +110,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. @@ -133,6 +139,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. @@ -317,7 +327,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. }
  49. diff --git a/ui/base/accelerators/accelerator.h b/ui/base/accelerators/accelerator.h
  50. index 198c7469f410d3516b8a18493c5e4588d02487c2..e4995824ae2f3bb8045a3841a6213a4b315845a8 100644
  51. --- a/ui/base/accelerators/accelerator.h
  52. +++ b/ui/base/accelerators/accelerator.h
  53. @@ -18,6 +18,7 @@
  54. #include <utility>
  55. #include "base/component_export.h"
  56. +#include "third_party/abseil-cpp/absl/types/optional.h"
  57. #include "base/time/time.h"
  58. #include "build/blink_buildflags.h"
  59. #include "build/build_config.h"
  60. @@ -185,6 +186,8 @@ class COMPONENT_EXPORT(UI_BASE) Accelerator {
  61. return interrupted_by_mouse_event_;
  62. }
  63. + absl::optional<char16_t> shifted_char;
  64. +
  65. private:
  66. friend class AcceleratorTestMac;
  67. std::u16string ApplyLongFormModifiers(const std::u16string& shortcut) const;