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

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: Fix for tapted Created 4 years 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 #import <Cocoa/Cocoa.h>
5
6 #include "base/memory/ptr_util.h"
7 #include "ui/base/cocoa/text_services_context_menu.h"
8 #include "ui/base/models/simple_menu_model.h"
9 #include "ui/gfx/decorated_text.h"
10 #include "ui/gfx/decorated_text_mac.h"
11 #include "ui/views/controls/textfield/textfield.h"
12 #include "ui/views/controls/views_text_services_context_menu.h"
13 #include "ui/views/view.h"
14 #include "ui/views/widget/widget.h"
15
16 namespace views {
17
18 namespace {
19
20 // This class serves as a bridge to TextServicesContextMenu to add and handle
21 // text service items in the context menu. The items include Speech, Look Up
22 // and BiDi.
23 class ViewsTextServicesContextMenuMac
24 : public ViewsTextServicesContextMenu,
25 public ui::TextServicesContextMenu::Delegate {
26 public:
27 ViewsTextServicesContextMenuMac(ui::SimpleMenuModel* menu, Textfield* client);
28 ~ViewsTextServicesContextMenuMac() override;
29
30 bool IsTextServicesCommandId(int command_id) const override;
31 void ExecuteCommand(int command_id, int event_flags) override;
32 bool IsCommandIdChecked(int command_id) const override;
33 bool IsCommandIdEnabled(int command_id) const override;
34
35 // TextServicesContextMenu::Delegate:
36 base::string16 GetSelectedText() const override;
37 void OnSpeakRequested() override;
38 bool IsLookUpAvailable() const override;
39 void LookUpInDictionary() override;
40 bool IsTextDirectionEnabled(
41 base::i18n::TextDirection direction) const override;
42 bool IsTextDirectionChecked(
43 base::i18n::TextDirection direction) const override;
44 void UpdateTextDirection(base::i18n::TextDirection direction) override;
45
46 private:
47 // Appends and handles the text service menu.
48 ui::TextServicesContextMenu menu_;
49
50 // The view associated with the menu. Weak.
51 Textfield* client_;
52
53 DISALLOW_COPY_AND_ASSIGN(ViewsTextServicesContextMenuMac);
54 };
55
56 ViewsTextServicesContextMenuMac::ViewsTextServicesContextMenuMac(
57 ui::SimpleMenuModel* menu,
58 Textfield* client)
59 : menu_(this), client_(client) {
60 menu_.AppendToContextMenu(menu);
61 }
62
63 ViewsTextServicesContextMenuMac::~ViewsTextServicesContextMenuMac() {}
64
65 bool ViewsTextServicesContextMenuMac::IsTextServicesCommandId(
66 int command_id) const {
67 return menu_.IsTextServicesCommandId(command_id);
68 }
69
70 void ViewsTextServicesContextMenuMac::ExecuteCommand(int command_id,
71 int event_flags) {
72 DCHECK(IsTextServicesCommandId(command_id));
73 menu_.ExecuteCommand(command_id, event_flags);
74 }
75
76 bool ViewsTextServicesContextMenuMac::IsCommandIdChecked(int command_id) const {
77 DCHECK(IsTextServicesCommandId(command_id));
78 return menu_.IsCommandIdChecked(command_id);
79 }
80
81 bool ViewsTextServicesContextMenuMac::IsCommandIdEnabled(int command_id) const {
82 DCHECK(IsTextServicesCommandId(command_id));
83 return menu_.IsCommandIdEnabled(command_id);
84 }
85
86 base::string16 ViewsTextServicesContextMenuMac::GetSelectedText() const {
87 gfx::Range range;
88 base::string16 text;
89 client_->GetSelectionRange(&range);
90 client_->GetTextFromRange(range, &text);
91 return text;
92 }
93
94 void ViewsTextServicesContextMenuMac::OnSpeakRequested() {
95 menu_.SpeakText(GetSelectedText());
96 }
97
98 bool ViewsTextServicesContextMenuMac::IsLookUpAvailable() const {
99 return client_->HasSelection();
100 }
101
102 void ViewsTextServicesContextMenuMac::LookUpInDictionary() {
103 gfx::Point baseline_point;
104 gfx::DecoratedText text;
105 client_->GetDecoratedTextFromSelection(&text, &baseline_point);
106
107 Widget* widget = client_->GetWidget();
108 gfx::NativeView view = widget->GetNativeView();
109 views::View::ConvertPointToTarget(client_, widget->GetRootView(),
110 &baseline_point);
111
112 NSPoint lookup_point = NSMakePoint(
113 baseline_point.x(), NSHeight([view frame]) - baseline_point.y());
114 [view showDefinitionForAttributedString:
115 gfx::GetAttributedStringFromDecoratedText(text)
116 atPoint:lookup_point];
117 }
118
119 bool ViewsTextServicesContextMenuMac::IsTextDirectionEnabled(
120 base::i18n::TextDirection direction) const {
121 return direction != base::i18n::TextDirection::UNKNOWN_DIRECTION;
122 }
123
124 bool ViewsTextServicesContextMenuMac::IsTextDirectionChecked(
125 base::i18n::TextDirection direction) const {
126 switch (direction) {
127 case base::i18n::TextDirection::UNKNOWN_DIRECTION:
128 return false;
129 case base::i18n::TextDirection::RIGHT_TO_LEFT:
130 return client_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
131 case base::i18n::TextDirection::LEFT_TO_RIGHT:
132 return client_->GetTextDirection() == base::i18n::LEFT_TO_RIGHT;
133 default:
134 NOTREACHED();
135 return false;
136 }
137 }
138
139 void ViewsTextServicesContextMenuMac::UpdateTextDirection(
140 base::i18n::TextDirection direction) {
141 DCHECK_NE(direction, base::i18n::TextDirection::UNKNOWN_DIRECTION);
142
143 base::i18n::TextDirection text_direction =
144 direction == base::i18n::TextDirection::LEFT_TO_RIGHT
145 ? base::i18n::LEFT_TO_RIGHT
146 : base::i18n::RIGHT_TO_LEFT;
147 client_->ChangeTextDirectionAndLayoutAlignment(text_direction);
148 }
149
150 } // namespace
151
152 // static
153 std::unique_ptr<ViewsTextServicesContextMenu>
154 ViewsTextServicesContextMenu::Create(ui::SimpleMenuModel* menu,
155 Textfield* client) {
156 return base::MakeUnique<ViewsTextServicesContextMenuMac>(menu, client);
157 }
158
159 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698