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

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: Addressed tapted's comments and added a test Created 3 years, 11 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
OLDNEW
(Empty)
1 // Copyright 2016 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"
msw 2017/01/26 20:59:16 nit: I think this belongs at the top (like textfie
spqchan 2017/02/01 00:32:39 Done.
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 // The menu index for "Look Up".
24 constexpr int kLookupMenuIndex = 0;
msw 2017/01/26 20:59:16 optional nit: put this in the ctor (only used ther
spqchan 2017/02/01 00:32:38 Done.
25
26 // This class serves as a bridge to TextServicesContextMenu to add and handle
27 // text service items in the context menu. The items include Speech, Look Up
28 // and BiDi.
29 class ViewsTextServicesContextMenuMac
30 : public ViewsTextServicesContextMenu,
31 public ui::TextServicesContextMenu::Delegate {
32 public:
33 ViewsTextServicesContextMenuMac(ui::SimpleMenuModel* menu, Textfield* client);
msw 2017/01/26 20:59:16 optional nit: define all these methods inline
spqchan 2017/02/01 00:32:38 Done.
34 ~ViewsTextServicesContextMenuMac() override;
35
36 // Handler for the "Look Up" menu item.
37 void LookUpInDictionary();
38
39 // ViewTextServicesContextMenu:
40 bool HandlesCommandId(int command_id) const override;
41 bool IsCommandIdChecked(int command_id) const override;
42 bool IsCommandIdEnabled(int command_id) const override;
43 void ExecuteCommand(int command_id, int event_flags) override;
44 bool IsTextDirectionItemChecked(
45 base::i18n::TextDirection direction) const override;
46
47 // TextServicesContextMenu::Delegate:
48 base::string16 GetSelectedText() const override;
49 bool IsTextDirectionEnabled(
50 base::i18n::TextDirection direction) const override;
51 bool IsTextDirectionChecked(
52 base::i18n::TextDirection direction) const override;
53 void UpdateTextDirection(base::i18n::TextDirection direction) override;
54
55 private:
56 // Appends and handles the text service menu.
57 ui::TextServicesContextMenu menu_;
58
59 // The view associated with the menu. Weak. Owns |this|.
60 Textfield* client_;
61
62 DISALLOW_COPY_AND_ASSIGN(ViewsTextServicesContextMenuMac);
63 };
64
65 ViewsTextServicesContextMenuMac::ViewsTextServicesContextMenuMac(
66 ui::SimpleMenuModel* menu,
67 Textfield* client)
68 : menu_(this), client_(client) {
69 base::string16 text = GetSelectedText();
70 if (!text.empty()) {
71 menu->InsertItemAt(
72 kLookupMenuIndex, IDS_CONTENT_CONTEXT_LOOK_UP,
73 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP, text));
74 menu->InsertSeparatorAt(kLookupMenuIndex + 1, ui::NORMAL_SEPARATOR);
75 }
76 menu_.AppendToContextMenu(menu);
77 }
78
79 ViewsTextServicesContextMenuMac::~ViewsTextServicesContextMenuMac() {}
80
81 void ViewsTextServicesContextMenuMac::LookUpInDictionary() {
82 gfx::Point baseline_point;
83 gfx::DecoratedText text;
84 if (client_->GetDecoratedTextAndBaselineFromSelection(&text,
85 &baseline_point)) {
86 Widget* widget = client_->GetWidget();
87 gfx::NativeView view = widget->GetNativeView();
88 views::View::ConvertPointToTarget(client_, widget->GetRootView(),
89 &baseline_point);
90
91 NSPoint lookup_point = NSMakePoint(
92 baseline_point.x(), NSHeight([view frame]) - baseline_point.y());
93 [view showDefinitionForAttributedString:
94 gfx::GetAttributedStringFromDecoratedText(text)
95 atPoint:lookup_point];
96 }
97 }
98
99 bool ViewsTextServicesContextMenuMac::HandlesCommandId(int command_id) const {
100 return command_id == IDS_CONTENT_CONTEXT_LOOK_UP;
101 }
102
103 bool ViewsTextServicesContextMenuMac::IsCommandIdChecked(int command_id) const {
104 DCHECK_EQ(IDS_CONTENT_CONTEXT_LOOK_UP, command_id);
105 return false;
106 }
107
108 bool ViewsTextServicesContextMenuMac::IsCommandIdEnabled(int command_id) const {
109 DCHECK_EQ(IDS_CONTENT_CONTEXT_LOOK_UP, command_id);
110 return true;
111 }
112
113 void ViewsTextServicesContextMenuMac::ExecuteCommand(int command_id,
114 int event_flags) {
115 DCHECK_EQ(IDS_CONTENT_CONTEXT_LOOK_UP, command_id);
116 LookUpInDictionary();
117 }
118
119 bool ViewsTextServicesContextMenuMac::IsTextDirectionItemChecked(
120 base::i18n::TextDirection direction) const {
121 return IsTextDirectionChecked(direction);
122 }
123
124 base::string16 ViewsTextServicesContextMenuMac::GetSelectedText() const {
125 return client_->GetSelectedText();
126 }
127
128 bool ViewsTextServicesContextMenuMac::IsTextDirectionEnabled(
129 base::i18n::TextDirection direction) const {
130 return direction != base::i18n::UNKNOWN_DIRECTION;
131 }
132
133 bool ViewsTextServicesContextMenuMac::IsTextDirectionChecked(
134 base::i18n::TextDirection direction) const {
135 switch (direction) {
msw 2017/01/26 20:59:16 optional nit: return direction != base::i18n::UNKN
spqchan 2017/02/01 00:32:38 Done.
136 case base::i18n::UNKNOWN_DIRECTION:
137 return false;
138 case base::i18n::RIGHT_TO_LEFT:
139 return client_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
140 case base::i18n::LEFT_TO_RIGHT:
141 return client_->GetTextDirection() == base::i18n::LEFT_TO_RIGHT;
142 }
143 }
144
145 void ViewsTextServicesContextMenuMac::UpdateTextDirection(
146 base::i18n::TextDirection direction) {
147 DCHECK_NE(direction, base::i18n::UNKNOWN_DIRECTION);
148
149 base::i18n::TextDirection text_direction =
150 direction == base::i18n::LEFT_TO_RIGHT ? base::i18n::LEFT_TO_RIGHT
151 : base::i18n::RIGHT_TO_LEFT;
152 client_->ChangeTextDirectionAndLayoutAlignment(text_direction);
153 }
154
155 } // namespace
156
157 // static
158 std::unique_ptr<ViewsTextServicesContextMenu>
159 ViewsTextServicesContextMenu::Create(ui::SimpleMenuModel* menu,
160 Textfield* client) {
161 return base::MakeUnique<ViewsTextServicesContextMenuMac>(menu, client);
162 }
163
164 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698