views_text_services_context_menu_impl.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/public/cpp/views_text_services_context_menu_impl.h"
  5. #include "ash/public/cpp/clipboard_history_controller.h"
  6. #include "chromeos/crosapi/mojom/clipboard_history.mojom.h"
  7. #include "ui/base/l10n/l10n_util.h"
  8. #include "ui/base/pointer/touch_editing_controller.h"
  9. #include "ui/strings/grit/ui_strings.h"
  10. #include "ui/views/controls/textfield/textfield.h"
  11. namespace ash {
  12. ViewsTextServicesContextMenuImpl::ViewsTextServicesContextMenuImpl(
  13. ui::SimpleMenuModel* menu,
  14. views::Textfield* client)
  15. : views::ViewsTextServicesContextMenuBase(menu, client) {
  16. AddClipboardHistoryMenuOption(menu);
  17. }
  18. ViewsTextServicesContextMenuImpl::~ViewsTextServicesContextMenuImpl() = default;
  19. bool ViewsTextServicesContextMenuImpl::GetAcceleratorForCommandId(
  20. int command_id,
  21. ui::Accelerator* accelerator) const {
  22. if (command_id == IDS_APP_SHOW_CLIPBOARD_HISTORY) {
  23. *accelerator = ui::Accelerator(ui::VKEY_V, ui::EF_COMMAND_DOWN);
  24. return true;
  25. }
  26. return ViewsTextServicesContextMenuBase::GetAcceleratorForCommandId(
  27. command_id, accelerator);
  28. }
  29. bool ViewsTextServicesContextMenuImpl::IsCommandIdChecked(
  30. int command_id) const {
  31. if (command_id == IDS_APP_SHOW_CLIPBOARD_HISTORY)
  32. return true;
  33. return ViewsTextServicesContextMenuBase::IsCommandIdChecked(command_id);
  34. }
  35. bool ViewsTextServicesContextMenuImpl::IsCommandIdEnabled(
  36. int command_id) const {
  37. if (command_id == IDS_APP_SHOW_CLIPBOARD_HISTORY)
  38. return ClipboardHistoryController::Get()->CanShowMenu();
  39. return ViewsTextServicesContextMenuBase::IsCommandIdEnabled(command_id);
  40. }
  41. void ViewsTextServicesContextMenuImpl::ExecuteCommand(int command_id,
  42. int event_flags) {
  43. if (command_id == IDS_APP_SHOW_CLIPBOARD_HISTORY) {
  44. auto* clipboard_history_controller = ClipboardHistoryController::Get();
  45. // Calculate the menu source type from `event_flags`.
  46. ui::MenuSourceType source_type;
  47. if (event_flags & ui::EF_LEFT_MOUSE_BUTTON)
  48. source_type = ui::MENU_SOURCE_MOUSE;
  49. else if (event_flags & ui::EF_FROM_TOUCH)
  50. source_type = ui::MENU_SOURCE_TOUCH;
  51. else
  52. source_type = ui::MENU_SOURCE_KEYBOARD;
  53. clipboard_history_controller->ShowMenu(
  54. client()->GetCaretBounds(), source_type,
  55. crosapi::mojom::ClipboardHistoryControllerShowSource::
  56. kTextfieldContextMenu);
  57. return;
  58. }
  59. ViewsTextServicesContextMenuBase::ExecuteCommand(command_id, event_flags);
  60. }
  61. bool ViewsTextServicesContextMenuImpl::SupportsCommand(int command_id) const {
  62. if (command_id == IDS_APP_SHOW_CLIPBOARD_HISTORY)
  63. return true;
  64. return ViewsTextServicesContextMenuBase::SupportsCommand(command_id);
  65. }
  66. void ViewsTextServicesContextMenuImpl::AddClipboardHistoryMenuOption(
  67. ui::SimpleMenuModel* menu) {
  68. const absl::optional<size_t> index_of_paste =
  69. menu->GetIndexOfCommandId(ui::TouchEditable::kPaste);
  70. // Only add the clipboard history menu option when having the menu option
  71. // for paste.
  72. if (!index_of_paste.has_value())
  73. return;
  74. const size_t target_index = index_of_paste.value() + 1;
  75. menu->InsertItemAt(target_index, IDS_APP_SHOW_CLIPBOARD_HISTORY,
  76. l10n_util::GetStringUTF16(IDS_APP_SHOW_CLIPBOARD_HISTORY));
  77. if (ClipboardHistoryController::Get()->ShouldShowNewFeatureBadge()) {
  78. menu->SetIsNewFeatureAt(target_index, true);
  79. ClipboardHistoryController::Get()->MarkNewFeatureBadgeShown();
  80. }
  81. }
  82. } // namespace ash