-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
assistive_window_controller.cc
311 lines (277 loc) · 10.6 KB
/
assistive_window_controller.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/input_method/assistive_window_controller.h"
#include <string>
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h"
#include "ash/wm/window_util.h"
#include "chrome/browser/ash/input_method/assistive_window_controller_delegate.h"
#include "chrome/browser/ash/input_method/assistive_window_properties.h"
#include "chrome/browser/ash/input_method/ui/suggestion_details.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace input_method {
namespace {
gfx::NativeView GetParentView() {
gfx::NativeView parent = nullptr;
aura::Window* active_window = ash::window_util::GetActiveWindow();
// Use MenuContainer so that it works even with a system modal dialog.
parent = ash::Shell::GetContainer(
active_window ? active_window->GetRootWindow()
: ash::Shell::GetRootWindowForNewWindows(),
ash::kShellWindowId_MenuContainer);
return parent;
}
} // namespace
AssistiveWindowController::AssistiveWindowController(
AssistiveWindowControllerDelegate* delegate,
Profile* profile,
ui::ime::AssistiveAccessibilityView* accessibility_view)
: delegate_(delegate), accessibility_view_(accessibility_view) {}
AssistiveWindowController::~AssistiveWindowController() {
if (suggestion_window_view_ && suggestion_window_view_->GetWidget())
suggestion_window_view_->GetWidget()->RemoveObserver(this);
if (undo_window_ && undo_window_->GetWidget())
undo_window_->GetWidget()->RemoveObserver(this);
if (grammar_suggestion_window_ && grammar_suggestion_window_->GetWidget())
grammar_suggestion_window_->GetWidget()->RemoveObserver(this);
if (accessibility_view_ && accessibility_view_->GetWidget())
accessibility_view_->GetWidget()->RemoveObserver(this);
CHECK(!IsInObserverList());
}
void AssistiveWindowController::InitSuggestionWindow(
ui::ime::SuggestionWindowView::Orientation orientation) {
if (suggestion_window_view_)
return;
// suggestion_window_view_ is deleted by DialogDelegateView::DeleteDelegate.
suggestion_window_view_ =
ui::ime::SuggestionWindowView::Create(GetParentView(), this, orientation);
views::Widget* widget = suggestion_window_view_->GetWidget();
widget->AddObserver(this);
widget->Show();
}
void AssistiveWindowController::InitUndoWindow() {
if (undo_window_)
return;
// undo_window is deleted by DialogDelegateView::DeleteDelegate.
undo_window_ = new ui::ime::UndoWindow(GetParentView(), this);
views::Widget* widget = undo_window_->InitWidget();
widget->AddObserver(this);
widget->Show();
}
void AssistiveWindowController::InitGrammarSuggestionWindow() {
if (grammar_suggestion_window_)
return;
// grammar_suggestion_window_ is deleted by
// DialogDelegateView::DeleteDelegate.
grammar_suggestion_window_ =
new ui::ime::GrammarSuggestionWindow(GetParentView(), this);
views::Widget* widget = grammar_suggestion_window_->InitWidget();
widget->AddObserver(this);
widget->Show();
}
void AssistiveWindowController::InitAccessibilityView() {
if (accessibility_view_)
return;
// accessibility_view_ is deleted by DialogDelegateView::DeleteDelegate.
accessibility_view_ =
new ui::ime::AssistiveAccessibilityView(GetParentView());
accessibility_view_->GetWidget()->AddObserver(this);
}
void AssistiveWindowController::OnWidgetDestroying(views::Widget* widget) {
if (suggestion_window_view_ &&
widget == suggestion_window_view_->GetWidget()) {
widget->RemoveObserver(this);
suggestion_window_view_ = nullptr;
}
if (undo_window_ && widget == undo_window_->GetWidget()) {
widget->RemoveObserver(this);
undo_window_ = nullptr;
}
if (grammar_suggestion_window_ &&
widget == grammar_suggestion_window_->GetWidget()) {
widget->RemoveObserver(this);
grammar_suggestion_window_ = nullptr;
}
if (accessibility_view_ && widget == accessibility_view_->GetWidget()) {
widget->RemoveObserver(this);
accessibility_view_ = nullptr;
}
}
void AssistiveWindowController::Announce(const std::u16string& message) {
if (!accessibility_view_)
InitAccessibilityView();
accessibility_view_->Announce(message);
}
// TODO(crbug/1119570): Update AcceptSuggestion signature (either use
// announce_string, or no string)
void AssistiveWindowController::AcceptSuggestion(
const std::u16string& suggestion) {
if (window_.type == ui::ime::AssistiveWindowType::kEmojiSuggestion) {
Announce(l10n_util::GetStringUTF16(IDS_SUGGESTION_EMOJI_SUGGESTED));
} else {
Announce(l10n_util::GetStringUTF16(IDS_SUGGESTION_INSERTED));
}
HideSuggestion();
}
void AssistiveWindowController::HideSuggestion() {
suggestion_text_ = base::EmptyString16();
confirmed_length_ = 0;
if (suggestion_window_view_)
suggestion_window_view_->GetWidget()->Close();
if (grammar_suggestion_window_)
grammar_suggestion_window_->GetWidget()->Close();
}
void AssistiveWindowController::SetBounds(const Bounds& bounds) {
bounds_ = bounds;
// Sets suggestion_window_view_'s bounds here for most up-to-date cursor
// position. This is different from UndoWindow because UndoWindow gets cursors
// position before showing.
// TODO(crbug/1112982): Investigate getting bounds to suggester before sending
// show suggestion request.
if (suggestion_window_view_) {
suggestion_window_view_->SetAnchorRect(bounds.caret);
}
if (grammar_suggestion_window_) {
grammar_suggestion_window_->SetBounds(bounds_.caret);
}
}
void AssistiveWindowController::FocusStateChanged() {
HideSuggestion();
if (undo_window_)
undo_window_->Hide();
}
void AssistiveWindowController::ShowSuggestion(
const ui::ime::SuggestionDetails& details) {
if (!suggestion_window_view_)
// Since there is only one suggestion text in ShowSuggestion, we default to
// vertical layout.
InitSuggestionWindow(ui::ime::SuggestionWindowView::Orientation::kVertical);
suggestion_text_ = details.text;
confirmed_length_ = details.confirmed_length;
suggestion_window_view_->Show(details);
}
void AssistiveWindowController::SetButtonHighlighted(
const ui::ime::AssistiveWindowButton& button,
bool highlighted) {
switch (button.window_type) {
case ui::ime::AssistiveWindowType::kEmojiSuggestion:
case ui::ime::AssistiveWindowType::kPersonalInfoSuggestion:
case ui::ime::AssistiveWindowType::kMultiWordSuggestion:
case ui::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion:
if (!suggestion_window_view_)
return;
suggestion_window_view_->SetButtonHighlighted(button, highlighted);
if (highlighted)
Announce(button.announce_string);
break;
case ui::ime::AssistiveWindowType::kUndoWindow:
if (!undo_window_)
return;
undo_window_->SetButtonHighlighted(button, highlighted);
Announce(button.announce_string);
break;
case ui::ime::AssistiveWindowType::kGrammarSuggestion:
if (!grammar_suggestion_window_)
return;
grammar_suggestion_window_->SetButtonHighlighted(button, highlighted);
if (highlighted)
Announce(button.announce_string);
break;
case ui::ime::AssistiveWindowType::kNone:
break;
}
}
std::u16string AssistiveWindowController::GetSuggestionText() const {
return suggestion_text_;
}
size_t AssistiveWindowController::GetConfirmedLength() const {
return confirmed_length_;
}
ui::ime::SuggestionWindowView::Orientation
AssistiveWindowController::WindowOrientationFor(
ui::ime::AssistiveWindowType window_type) {
switch (window_type) {
case ui::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion:
return ui::ime::SuggestionWindowView::Orientation::kHorizontal;
case ui::ime::AssistiveWindowType::kUndoWindow:
case ui::ime::AssistiveWindowType::kEmojiSuggestion:
case ui::ime::AssistiveWindowType::kPersonalInfoSuggestion:
case ui::ime::AssistiveWindowType::kMultiWordSuggestion:
case ui::ime::AssistiveWindowType::kGrammarSuggestion:
return ui::ime::SuggestionWindowView::Orientation::kVertical;
case ui::ime::AssistiveWindowType::kNone:
NOTREACHED();
}
NOTREACHED();
return ui::ime::SuggestionWindowView::Orientation::kVertical;
}
void AssistiveWindowController::SetAssistiveWindowProperties(
const AssistiveWindowProperties& window) {
window_ = window;
switch (window.type) {
case ui::ime::AssistiveWindowType::kUndoWindow:
if (!undo_window_)
InitUndoWindow();
if (window.visible) {
// Apply 4px padding to move the window away from the cursor.
gfx::Rect anchor_rect =
bounds_.autocorrect.IsEmpty() ? bounds_.caret : bounds_.autocorrect;
anchor_rect.Inset(-4);
undo_window_->SetAnchorRect(anchor_rect);
undo_window_->Show();
} else {
undo_window_->Hide();
}
break;
case ui::ime::AssistiveWindowType::kEmojiSuggestion:
case ui::ime::AssistiveWindowType::kPersonalInfoSuggestion:
case ui::ime::AssistiveWindowType::kMultiWordSuggestion:
case ui::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion:
if (!suggestion_window_view_)
InitSuggestionWindow(WindowOrientationFor(window.type));
if (window_.visible) {
suggestion_window_view_->ShowMultipleCandidates(window);
} else {
HideSuggestion();
}
break;
case ui::ime::AssistiveWindowType::kGrammarSuggestion:
if (window.candidates.size() == 0)
return;
if (!grammar_suggestion_window_)
InitGrammarSuggestionWindow();
if (window.visible) {
grammar_suggestion_window_->SetBounds(bounds_.caret);
grammar_suggestion_window_->SetSuggestion(window.candidates[0]);
grammar_suggestion_window_->Show();
} else {
grammar_suggestion_window_->Hide();
}
break;
case ui::ime::AssistiveWindowType::kNone:
break;
}
Announce(window.announce_string);
}
void AssistiveWindowController::AssistiveWindowButtonClicked(
const ui::ime::AssistiveWindowButton& button) const {
delegate_->AssistiveWindowButtonClicked(button);
}
ui::ime::SuggestionWindowView*
AssistiveWindowController::GetSuggestionWindowViewForTesting() {
return suggestion_window_view_;
}
ui::ime::UndoWindow* AssistiveWindowController::GetUndoWindowForTesting()
const {
return undo_window_;
}
} // namespace input_method
} // namespace ash