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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
// Copyright 2020 The Chromium Authors
// 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/ui/ash/input_method/suggestion_details.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 {
constexpr char16_t kAnnouncementViewName[] = u"Assistive Input";
constexpr base::TimeDelta kAnnouncementDelay = base::Milliseconds(100);
constexpr base::TimeDelta kShowSuggestionDelay = base::Milliseconds(5);
gfx::NativeView GetParentView() {
gfx::NativeView parent = gfx::NativeView();
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::AnnouncementView* announcement_view)
: delegate_(delegate), announcement_view_(announcement_view) {}
AssistiveWindowController::~AssistiveWindowController() {
ClearPendingSuggestionTimer();
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 (announcement_view_ && announcement_view_->GetWidget()) {
announcement_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::InitAnnouncementView() {
if (announcement_view_) {
return;
}
// accessibility_view_ is deleted by DialogDelegateView::DeleteDelegate.
announcement_view_ =
new ui::ime::AnnouncementView(GetParentView(), kAnnouncementViewName);
announcement_view_->GetWidget()->AddObserver(this);
}
void AssistiveWindowController::OnWidgetDestroying(views::Widget* widget) {
ClearPendingSuggestionTimer();
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 (announcement_view_ && widget == announcement_view_->GetWidget()) {
widget->RemoveObserver(this);
announcement_view_ = nullptr;
}
}
void AssistiveWindowController::Announce(const std::u16string& message) {
if (!announcement_view_) {
InitAnnouncementView();
}
// Announcements for assistive suggestions often collide with key press or
// text update announcements from ChromeVox. By adding a very small delay
// these collisions are *mostly* avoided.
announcement_view_->AnnounceAfterDelay(message, kAnnouncementDelay);
}
// TODO(crbug.com/40145550): Update AcceptSuggestion signature (either use
// announce_string, or no string)
void AssistiveWindowController::AcceptSuggestion(
const std::u16string& suggestion) {
if (window_.type == ash::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_.clear();
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) {
if (bounds == bounds_) {
return;
}
bounds_ = bounds;
if (suggestion_window_view_) {
suggestion_window_view_->SetAnchorRect(bounds.caret);
}
if (grammar_suggestion_window_) {
grammar_suggestion_window_->SetBounds(bounds_.caret);
}
if (pending_suggestion_timer_ && pending_suggestion_timer_->IsRunning()) {
pending_suggestion_timer_->FireNow();
pending_suggestion_timer_ = nullptr;
}
}
void AssistiveWindowController::FocusStateChanged() {
HideSuggestion();
if (undo_window_) {
undo_window_->Hide();
}
}
void AssistiveWindowController::ShowSuggestion(
const ui::ime::SuggestionDetails& details) {
suggestion_text_ = details.text;
confirmed_length_ = details.confirmed_length;
// Delay the showing of a completion suggestion. This is required to solve
// b/241321719, where we receive a ShowSuggestion call prior to a
// corresponding SetBounds call. Delaying allows any relevant SetBounds calls
// to be received before we show the suggestion to the user.
ClearPendingSuggestionTimer();
pending_suggestion_timer_ = std::make_unique<base::OneShotTimer>();
pending_suggestion_timer_->Start(
FROM_HERE, kShowSuggestionDelay,
base::BindOnce(&AssistiveWindowController::DisplayCompletionSuggestion,
weak_ptr_factory_.GetWeakPtr(), details));
}
void AssistiveWindowController::SetButtonHighlighted(
const ui::ime::AssistiveWindowButton& button,
bool highlighted) {
switch (button.window_type) {
case ash::ime::AssistiveWindowType::kEmojiSuggestion:
case ash::ime::AssistiveWindowType::kPersonalInfoSuggestion:
case ash::ime::AssistiveWindowType::kMultiWordSuggestion:
case ash::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion:
if (!suggestion_window_view_) {
return;
}
suggestion_window_view_->SetButtonHighlighted(button, highlighted);
break;
case ash::ime::AssistiveWindowType::kLearnMore:
case ash::ime::AssistiveWindowType::kUndoWindow:
if (!undo_window_) {
return;
}
undo_window_->SetButtonHighlighted(button, highlighted);
break;
case ash::ime::AssistiveWindowType::kGrammarSuggestion:
if (!grammar_suggestion_window_) {
return;
}
grammar_suggestion_window_->SetButtonHighlighted(button, highlighted);
break;
case ash::ime::AssistiveWindowType::kNone:
break;
}
if (highlighted) {
Announce(button.announce_string);
}
}
std::u16string AssistiveWindowController::GetSuggestionText() const {
return suggestion_text_;
}
size_t AssistiveWindowController::GetConfirmedLength() const {
return confirmed_length_;
}
ui::ime::SuggestionWindowView::Orientation
AssistiveWindowController::WindowOrientationFor(
ash::ime::AssistiveWindowType window_type) {
switch (window_type) {
case ash::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion:
case ash::ime::AssistiveWindowType::kLearnMore:
return ui::ime::SuggestionWindowView::Orientation::kHorizontal;
case ash::ime::AssistiveWindowType::kUndoWindow:
case ash::ime::AssistiveWindowType::kEmojiSuggestion:
case ash::ime::AssistiveWindowType::kPersonalInfoSuggestion:
case ash::ime::AssistiveWindowType::kMultiWordSuggestion:
case ash::ime::AssistiveWindowType::kGrammarSuggestion:
return ui::ime::SuggestionWindowView::Orientation::kVertical;
case ash::ime::AssistiveWindowType::kNone:
NOTREACHED();
}
NOTREACHED();
}
void AssistiveWindowController::SetAssistiveWindowProperties(
const AssistiveWindowProperties& window) {
window_ = window;
// Make sure any pending timers are cleared before we attempt to show, or
// update, another assistive window.
ClearPendingSuggestionTimer();
switch (window.type) {
case ash::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(window.show_setting_link);
} else {
undo_window_->Hide();
}
break;
case ash::ime::AssistiveWindowType::kLearnMore:
case ash::ime::AssistiveWindowType::kEmojiSuggestion:
case ash::ime::AssistiveWindowType::kPersonalInfoSuggestion:
case ash::ime::AssistiveWindowType::kMultiWordSuggestion:
case ash::ime::AssistiveWindowType::kLongpressDiacriticsSuggestion:
if (!suggestion_window_view_) {
InitSuggestionWindow(WindowOrientationFor(window.type));
}
if (window_.visible) {
suggestion_window_view_->SetAnchorRect(bounds_.caret);
suggestion_window_view_->ShowMultipleCandidates(
window, WindowOrientationFor(window.type));
} else {
HideSuggestion();
}
break;
case ash::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 ash::ime::AssistiveWindowType::kNone:
break;
}
Announce(window.announce_string);
}
void AssistiveWindowController::DisplayCompletionSuggestion(
const ui::ime::SuggestionDetails& details) {
if (!suggestion_window_view_) {
InitSuggestionWindow(ui::ime::SuggestionWindowView::Orientation::kVertical);
}
suggestion_window_view_->SetAnchorRect(bounds_.caret);
suggestion_window_view_->Show(details);
}
void AssistiveWindowController::ClearPendingSuggestionTimer() {
if (pending_suggestion_timer_) {
if (pending_suggestion_timer_->IsRunning()) {
pending_suggestion_timer_->Stop();
}
pending_suggestion_timer_ = nullptr;
}
}
void AssistiveWindowController::AssistiveWindowButtonClicked(
const ui::ime::AssistiveWindowButton& button) const {
delegate_->AssistiveWindowButtonClicked(button);
}
void AssistiveWindowController::AssistiveWindowChanged(
const ash::ime::AssistiveWindow& window) const {
delegate_->AssistiveWindowChanged(window);
}
ui::ime::SuggestionWindowView*
AssistiveWindowController::GetSuggestionWindowViewForTesting() {
return suggestion_window_view_;
}
ui::ime::UndoWindow* AssistiveWindowController::GetUndoWindowForTesting()
const {
return undo_window_;
}
} // namespace input_method
} // namespace ash
|