Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: ui/views/controls/views_text_services_context_menu_mac.mm

Issue 2164483006: [MacViews] Implemented text context menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/views_text_services_context_menu.cc ('k') | ui/views/word_lookup_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "ui/views/controls/views_text_services_context_menu.h"
5
6 #import <Cocoa/Cocoa.h>
7
8 #include "base/memory/ptr_util.h"
9 #include "ui/base/cocoa/text_services_context_menu.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/models/simple_menu_model.h"
12 #include "ui/gfx/decorated_text.h"
13 #import "ui/gfx/decorated_text_mac.h"
14 #include "ui/strings/grit/ui_strings.h"
15 #include "ui/views/controls/textfield/textfield.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
18
19 namespace views {
20
21 namespace {
22
23 // This class serves as a bridge to TextServicesContextMenu to add and handle
24 // text service items in the context menu. The items include Speech, Look Up
25 // and BiDi.
26 class ViewsTextServicesContextMenuMac
27 : public ViewsTextServicesContextMenu,
28 public ui::TextServicesContextMenu::Delegate {
29 public:
30 ViewsTextServicesContextMenuMac(ui::SimpleMenuModel* menu, Textfield* client)
31 : menu_(this), client_(client) {
32 // The menu index for "Look Up".
33 constexpr int kLookupMenuIndex = 0;
34
35 base::string16 text = GetSelectedText();
36 if (!text.empty()) {
37 menu->InsertItemAt(
38 kLookupMenuIndex, IDS_CONTENT_CONTEXT_LOOK_UP,
39 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP, text));
40 menu->InsertSeparatorAt(kLookupMenuIndex + 1, ui::NORMAL_SEPARATOR);
41 }
42 menu_.AppendToContextMenu(menu);
43 }
44
45 ~ViewsTextServicesContextMenuMac() override {}
46
47 // Handler for the "Look Up" menu item.
48 void LookUpInDictionary() {
49 gfx::Point baseline_point;
50 gfx::DecoratedText text;
51 if (client_->GetDecoratedTextAndBaselineFromSelection(&text,
52 &baseline_point)) {
53 Widget* widget = client_->GetWidget();
54 gfx::NativeView view = widget->GetNativeView();
55 views::View::ConvertPointToTarget(client_, widget->GetRootView(),
56 &baseline_point);
57
58 NSPoint lookup_point = NSMakePoint(
59 baseline_point.x(), NSHeight([view frame]) - baseline_point.y());
60 [view showDefinitionForAttributedString:
61 gfx::GetAttributedStringFromDecoratedText(text)
62 atPoint:lookup_point];
63 }
64 }
65
66 // ViewTextServicesContextMenu:
67 bool HandlesCommandId(int command_id) const override {
68 return command_id == IDS_CONTENT_CONTEXT_LOOK_UP;
69 }
70
71 bool IsCommandIdChecked(int command_id) const override {
72 DCHECK_EQ(IDS_CONTENT_CONTEXT_LOOK_UP, command_id);
73 return false;
74 }
75
76 bool IsCommandIdEnabled(int command_id) const override {
77 DCHECK_EQ(IDS_CONTENT_CONTEXT_LOOK_UP, command_id);
78 return true;
79 }
80
81 void ExecuteCommand(int command_id, int event_flags) override {
82 DCHECK_EQ(IDS_CONTENT_CONTEXT_LOOK_UP, command_id);
83 LookUpInDictionary();
84 }
85
86 bool IsTextDirectionCheckedForTesting(
87 base::i18n::TextDirection direction) const override {
88 return IsTextDirectionChecked(direction);
89 }
90
91 // TextServicesContextMenu::Delegate:
92 base::string16 GetSelectedText() const override {
93 return client_->GetSelectedText();
94 }
95
96 bool IsTextDirectionEnabled(
97 base::i18n::TextDirection direction) const override {
98 return direction != base::i18n::UNKNOWN_DIRECTION;
99 }
100
101 bool IsTextDirectionChecked(
102 base::i18n::TextDirection direction) const override {
103 return direction != base::i18n::UNKNOWN_DIRECTION &&
104 client_->GetTextDirection() == direction;
105 }
106
107 void UpdateTextDirection(base::i18n::TextDirection direction) override {
108 DCHECK_NE(direction, base::i18n::UNKNOWN_DIRECTION);
109
110 base::i18n::TextDirection text_direction =
111 direction == base::i18n::LEFT_TO_RIGHT ? base::i18n::LEFT_TO_RIGHT
112 : base::i18n::RIGHT_TO_LEFT;
113 client_->ChangeTextDirectionAndLayoutAlignment(text_direction);
114 }
115
116 private:
117 // Appends and handles the text service menu.
118 ui::TextServicesContextMenu menu_;
119
120 // The view associated with the menu. Weak. Owns |this|.
121 Textfield* client_;
122
123 DISALLOW_COPY_AND_ASSIGN(ViewsTextServicesContextMenuMac);
124 };
125
126 } // namespace
127
128 // static
129 std::unique_ptr<ViewsTextServicesContextMenu>
130 ViewsTextServicesContextMenu::Create(ui::SimpleMenuModel* menu,
131 Textfield* client) {
132 return base::MakeUnique<ViewsTextServicesContextMenuMac>(menu, client);
133 }
134
135 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/views_text_services_context_menu.cc ('k') | ui/views/word_lookup_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698