Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 5 | #include "components/exo/text_input.h" |
| 6 | |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Darren Shen | cb250844 | 2019-07-03 21:48:23 | [diff] [blame] | 9 | #include "ash/keyboard/ui/keyboard_ui_controller.h" |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 11 | #include "components/exo/surface.h" |
| 12 | #include "components/exo/wm_helper.h" |
| 13 | #include "third_party/icu/source/common/unicode/uchar.h" |
| 14 | #include "ui/aura/window.h" |
| 15 | #include "ui/aura/window_tree_host.h" |
| 16 | #include "ui/base/ime/input_method.h" |
| 17 | #include "ui/events/event.h" |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 18 | |
| 19 | namespace exo { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | ui::InputMethod* GetInputMethod(aura::Window* window) { |
| 24 | if (!window || !window->GetHost()) |
| 25 | return nullptr; |
| 26 | return window->GetHost()->GetInputMethod(); |
| 27 | } |
| 28 | |
| 29 | } // namespace |
| 30 | |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 31 | size_t OffsetFromUTF8Offset(const base::StringPiece& text, uint32_t offset) { |
| 32 | return base::UTF8ToUTF16(text.substr(0, offset)).size(); |
| 33 | } |
| 34 | |
| 35 | size_t OffsetFromUTF16Offset(const base::StringPiece16& text, uint32_t offset) { |
| 36 | return base::UTF16ToUTF8(text.substr(0, offset)).size(); |
| 37 | } |
| 38 | |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 39 | TextInput::TextInput(std::unique_ptr<Delegate> delegate) |
| 40 | : delegate_(std::move(delegate)) {} |
| 41 | |
| 42 | TextInput::~TextInput() { |
Darren Shen | cb250844 | 2019-07-03 21:48:23 | [diff] [blame] | 43 | if (keyboard_ui_controller_) |
| 44 | keyboard_ui_controller_->RemoveObserver(this); |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 45 | if (input_method_) |
| 46 | Deactivate(); |
| 47 | } |
| 48 | |
| 49 | void TextInput::Activate(Surface* surface) { |
| 50 | DLOG_IF(ERROR, window_) << "Already activated with " << window_; |
| 51 | DCHECK(surface); |
| 52 | |
| 53 | window_ = surface->window(); |
| 54 | AttachInputMethod(); |
| 55 | } |
| 56 | |
| 57 | void TextInput::Deactivate() { |
| 58 | DetachInputMethod(); |
| 59 | window_ = nullptr; |
| 60 | } |
| 61 | |
| 62 | void TextInput::ShowVirtualKeyboardIfEnabled() { |
| 63 | // Some clients may ask showing virtual keyboard before sending activation. |
| 64 | if (!input_method_) { |
| 65 | pending_vk_visible_ = true; |
| 66 | return; |
| 67 | } |
| 68 | input_method_->ShowVirtualKeyboardIfEnabled(); |
| 69 | } |
| 70 | |
| 71 | void TextInput::HideVirtualKeyboard() { |
Darren Shen | cb250844 | 2019-07-03 21:48:23 | [diff] [blame] | 72 | if (keyboard_ui_controller_) |
| 73 | keyboard_ui_controller_->HideKeyboardByUser(); |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 74 | pending_vk_visible_ = false; |
| 75 | } |
| 76 | |
| 77 | void TextInput::Resync() { |
| 78 | if (input_method_) |
| 79 | input_method_->OnCaretBoundsChanged(this); |
| 80 | } |
| 81 | |
| 82 | void TextInput::SetSurroundingText(const base::string16& text, |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 83 | uint32_t cursor_pos, |
| 84 | uint32_t anchor) { |
| 85 | surrounding_text_ = text; |
| 86 | cursor_pos_ = gfx::Range(cursor_pos); |
| 87 | if (anchor < cursor_pos) |
| 88 | cursor_pos_->set_start(anchor); |
| 89 | else |
| 90 | cursor_pos_->set_end(anchor); |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void TextInput::SetTypeModeFlags(ui::TextInputType type, |
| 94 | ui::TextInputMode mode, |
| 95 | int flags, |
| 96 | bool should_do_learning) { |
| 97 | if (!input_method_) |
| 98 | return; |
| 99 | bool changed = (input_type_ != type); |
| 100 | input_type_ = type; |
| 101 | input_mode_ = mode; |
| 102 | flags_ = flags; |
| 103 | should_do_learning_ = should_do_learning; |
| 104 | if (changed) |
| 105 | input_method_->OnTextInputTypeChanged(this); |
| 106 | } |
| 107 | |
| 108 | void TextInput::SetCaretBounds(const gfx::Rect& bounds) { |
| 109 | if (caret_bounds_ == bounds) |
| 110 | return; |
| 111 | caret_bounds_ = bounds; |
| 112 | if (!input_method_) |
| 113 | return; |
| 114 | input_method_->OnCaretBoundsChanged(this); |
| 115 | } |
| 116 | |
| 117 | void TextInput::SetCompositionText(const ui::CompositionText& composition) { |
| 118 | composition_ = composition; |
| 119 | delegate_->SetCompositionText(composition); |
| 120 | } |
| 121 | |
Keith Lee | e10ead4 | 2020-07-28 08:16:33 | [diff] [blame] | 122 | uint32_t TextInput::ConfirmCompositionText(bool keep_selection) { |
Keith Lee | 407770c2 | 2019-11-08 05:29:48 | [diff] [blame] | 123 | // TODO(b/134473433) Modify this function so that when keep_selection is |
| 124 | // true, the selection is not changed when text committed |
| 125 | if (keep_selection) { |
| 126 | NOTIMPLEMENTED_LOG_ONCE(); |
| 127 | } |
Keith Lee | e10ead4 | 2020-07-28 08:16:33 | [diff] [blame] | 128 | const uint32_t composition_text_length = |
| 129 | static_cast<uint32_t>(composition_.text.length()); |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 130 | delegate_->Commit(composition_.text); |
Keith Lee | e10ead4 | 2020-07-28 08:16:33 | [diff] [blame] | 131 | return composition_text_length; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void TextInput::ClearCompositionText() { |
| 135 | composition_ = ui::CompositionText(); |
| 136 | delegate_->SetCompositionText(composition_); |
| 137 | } |
| 138 | |
| 139 | void TextInput::InsertText(const base::string16& text) { |
| 140 | delegate_->Commit(text); |
| 141 | } |
| 142 | |
| 143 | void TextInput::InsertChar(const ui::KeyEvent& event) { |
| 144 | base::char16 ch = event.GetCharacter(); |
| 145 | if (u_isprint(ch)) { |
| 146 | InsertText(base::string16(1, ch)); |
| 147 | return; |
| 148 | } |
| 149 | delegate_->SendKey(event); |
| 150 | } |
| 151 | |
| 152 | ui::TextInputType TextInput::GetTextInputType() const { |
| 153 | return input_type_; |
| 154 | } |
| 155 | |
| 156 | ui::TextInputMode TextInput::GetTextInputMode() const { |
| 157 | return input_mode_; |
| 158 | } |
| 159 | |
| 160 | base::i18n::TextDirection TextInput::GetTextDirection() const { |
| 161 | return direction_; |
| 162 | } |
| 163 | |
| 164 | int TextInput::GetTextInputFlags() const { |
| 165 | return flags_; |
| 166 | } |
| 167 | |
| 168 | bool TextInput::CanComposeInline() const { |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | gfx::Rect TextInput::GetCaretBounds() const { |
| 173 | return caret_bounds_ + window_->GetBoundsInScreen().OffsetFromOrigin(); |
| 174 | } |
| 175 | |
| 176 | bool TextInput::GetCompositionCharacterBounds(uint32_t index, |
| 177 | gfx::Rect* rect) const { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool TextInput::HasCompositionText() const { |
| 182 | return !composition_.text.empty(); |
| 183 | } |
| 184 | |
| 185 | ui::TextInputClient::FocusReason TextInput::GetFocusReason() const { |
| 186 | NOTIMPLEMENTED_LOG_ONCE(); |
| 187 | return ui::TextInputClient::FOCUS_REASON_OTHER; |
| 188 | } |
| 189 | |
| 190 | bool TextInput::GetTextRange(gfx::Range* range) const { |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 191 | if (!cursor_pos_) |
| 192 | return false; |
| 193 | range->set_start(0); |
| 194 | if (composition_.text.empty()) { |
| 195 | range->set_end(surrounding_text_.size()); |
| 196 | } else { |
| 197 | range->set_end(surrounding_text_.size() - cursor_pos_->length() + |
| 198 | composition_.text.size()); |
| 199 | } |
| 200 | return true; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool TextInput::GetCompositionTextRange(gfx::Range* range) const { |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 204 | if (!cursor_pos_ || composition_.text.empty()) |
| 205 | return false; |
| 206 | |
| 207 | range->set_start(cursor_pos_->start()); |
| 208 | range->set_end(cursor_pos_->start() + composition_.text.size()); |
| 209 | return true; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 210 | } |
| 211 | |
Yuichiro Hanada | 65eaaf4 | 2018-12-14 07:18:38 | [diff] [blame] | 212 | bool TextInput::GetEditableSelectionRange(gfx::Range* range) const { |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 213 | if (!cursor_pos_) |
| 214 | return false; |
| 215 | range->set_start(cursor_pos_->start()); |
| 216 | range->set_end(cursor_pos_->end()); |
| 217 | return true; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 218 | } |
| 219 | |
Yuichiro Hanada | 65eaaf4 | 2018-12-14 07:18:38 | [diff] [blame] | 220 | bool TextInput::SetEditableSelectionRange(const gfx::Range& range) { |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 221 | if (surrounding_text_.size() < range.GetMax()) |
| 222 | return false; |
| 223 | delegate_->SetCursor( |
| 224 | gfx::Range(OffsetFromUTF16Offset(surrounding_text_, range.start()), |
| 225 | OffsetFromUTF16Offset(surrounding_text_, range.end()))); |
| 226 | return true; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | bool TextInput::DeleteRange(const gfx::Range& range) { |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 230 | if (surrounding_text_.size() < range.GetMax()) |
| 231 | return false; |
| 232 | delegate_->DeleteSurroundingText( |
| 233 | gfx::Range(OffsetFromUTF16Offset(surrounding_text_, range.start()), |
| 234 | OffsetFromUTF16Offset(surrounding_text_, range.end()))); |
| 235 | return true; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | bool TextInput::GetTextFromRange(const gfx::Range& range, |
| 239 | base::string16* text) const { |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 240 | gfx::Range text_range; |
| 241 | if (!GetTextRange(&text_range) || !text_range.Contains(range)) |
| 242 | return false; |
| 243 | if (composition_.text.empty() || range.GetMax() <= cursor_pos_->GetMin()) { |
| 244 | text->assign(surrounding_text_, range.GetMin(), range.length()); |
| 245 | return true; |
| 246 | } |
| 247 | size_t composition_end = cursor_pos_->GetMin() + composition_.text.size(); |
| 248 | if (range.GetMin() >= composition_end) { |
| 249 | size_t start = |
| 250 | range.GetMin() - composition_.text.size() + cursor_pos_->length(); |
| 251 | text->assign(surrounding_text_, start, range.length()); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | size_t start_in_composition = 0; |
| 256 | if (range.GetMin() <= cursor_pos_->GetMin()) { |
| 257 | text->assign(surrounding_text_, range.GetMin(), |
| 258 | cursor_pos_->GetMin() - range.GetMin()); |
| 259 | } else { |
| 260 | start_in_composition = range.GetMin() - cursor_pos_->GetMin(); |
| 261 | } |
| 262 | if (range.GetMax() <= composition_end) { |
| 263 | text->append(composition_.text, start_in_composition, |
| 264 | range.GetMax() - cursor_pos_->GetMin() - start_in_composition); |
| 265 | } else { |
| 266 | text->append(composition_.text, start_in_composition, |
| 267 | composition_.text.size() - start_in_composition); |
| 268 | text->append(surrounding_text_, cursor_pos_->GetMax(), |
| 269 | range.GetMax() - composition_end); |
| 270 | } |
| 271 | return true; |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void TextInput::OnInputMethodChanged() { |
| 275 | ui::InputMethod* input_method = GetInputMethod(window_); |
| 276 | if (input_method == input_method_) |
| 277 | return; |
| 278 | input_method_->DetachTextInputClient(this); |
| 279 | input_method_ = input_method; |
| 280 | input_method_->SetFocusedTextInputClient(this); |
| 281 | } |
| 282 | |
| 283 | bool TextInput::ChangeTextDirectionAndLayoutAlignment( |
| 284 | base::i18n::TextDirection direction) { |
| 285 | if (direction == direction_) |
| 286 | return true; |
| 287 | direction_ = direction; |
| 288 | delegate_->OnTextDirectionChanged(direction_); |
| 289 | return true; |
| 290 | } |
| 291 | |
Jun Mukai | cf1f036 | 2019-02-07 00:39:33 | [diff] [blame] | 292 | void TextInput::ExtendSelectionAndDelete(size_t before, size_t after) { |
| 293 | if (!cursor_pos_) |
| 294 | return; |
| 295 | uint32_t start = |
| 296 | (cursor_pos_->GetMin() < before) ? 0 : (cursor_pos_->GetMin() - before); |
| 297 | uint32_t end = |
| 298 | std::min(cursor_pos_->GetMax() + after, surrounding_text_.size()); |
| 299 | delegate_->DeleteSurroundingText( |
| 300 | gfx::Range(OffsetFromUTF16Offset(surrounding_text_, start), |
| 301 | OffsetFromUTF16Offset(surrounding_text_, end))); |
| 302 | } |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 303 | |
| 304 | void TextInput::EnsureCaretNotInRect(const gfx::Rect& rect) {} |
| 305 | |
| 306 | bool TextInput::IsTextEditCommandEnabled(ui::TextEditCommand command) const { |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | void TextInput::SetTextEditCommandForNextKeyEvent(ui::TextEditCommand command) { |
| 311 | } |
| 312 | |
| 313 | ukm::SourceId TextInput::GetClientSourceForMetrics() const { |
| 314 | NOTIMPLEMENTED_LOG_ONCE(); |
| 315 | return ukm::kInvalidSourceId; |
| 316 | } |
| 317 | |
| 318 | bool TextInput::ShouldDoLearning() { |
| 319 | return should_do_learning_; |
| 320 | } |
| 321 | |
Darren Shen | 8f63c55 | 2019-05-09 02:07:58 | [diff] [blame] | 322 | bool TextInput::SetCompositionFromExistingText( |
Darren Shen | ff9457f3 | 2019-05-01 07:46:26 | [diff] [blame] | 323 | const gfx::Range& range, |
Darren Shen | 8f63c55 | 2019-05-09 02:07:58 | [diff] [blame] | 324 | const std::vector<ui::ImeTextSpan>& ui_ime_text_spans) { |
| 325 | // TODO(https://crbug.com/952757): Implement this method. |
| 326 | NOTIMPLEMENTED_LOG_ONCE(); |
| 327 | return false; |
| 328 | } |
Darren Shen | ff9457f3 | 2019-05-01 07:46:26 | [diff] [blame] | 329 | |
Keith Lee | 8c0b487 | 2020-07-29 07:20:47 | [diff] [blame] | 330 | gfx::Range TextInput::GetAutocorrectRange() const { |
| 331 | // TODO(https://crbug.com/952757): Implement this method. |
| 332 | NOTIMPLEMENTED_LOG_ONCE(); |
| 333 | return gfx::Range(); |
| 334 | } |
| 335 | |
Keith Lee | f7c2bc2 | 2020-07-27 23:18:31 | [diff] [blame] | 336 | gfx::Rect TextInput::GetAutocorrectCharacterBounds() const { |
| 337 | // TODO(https://crbug.com/952757): Implement this method. |
| 338 | NOTIMPLEMENTED_LOG_ONCE(); |
| 339 | return gfx::Rect(); |
| 340 | } |
| 341 | |
Keith Lee | 5e1d2bc | 2020-06-18 09:26:53 | [diff] [blame] | 342 | // TODO(crbug.com/1091088) Implement setAutocorrectRange |
| 343 | bool TextInput::SetAutocorrectRange(const base::string16& autocorrect_text, |
| 344 | const gfx::Range& range) { |
| 345 | NOTIMPLEMENTED_LOG_ONCE(); |
| 346 | return false; |
| 347 | } |
| 348 | |
Keith Lee | f92d3c0d | 2020-08-14 06:23:07 | [diff] [blame] | 349 | // TODO(crbug.com/1091088) Implement ClearAutocorrectRange |
| 350 | void TextInput::ClearAutocorrectRange() { |
| 351 | NOTIMPLEMENTED_LOG_ONCE(); |
| 352 | } |
| 353 | |
Darren Shen | 4f67ec2 | 2019-06-13 00:25:36 | [diff] [blame] | 354 | void TextInput::OnKeyboardVisibilityChanged(bool is_visible) { |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 355 | delegate_->OnVirtualKeyboardVisibilityChanged(is_visible); |
| 356 | } |
| 357 | |
| 358 | void TextInput::AttachInputMethod() { |
| 359 | DCHECK(!input_method_); |
| 360 | |
| 361 | ui::InputMethod* input_method = GetInputMethod(window_); |
| 362 | if (!input_method) { |
| 363 | LOG(ERROR) << "input method not found"; |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | input_mode_ = ui::TEXT_INPUT_MODE_TEXT; |
| 368 | input_type_ = ui::TEXT_INPUT_TYPE_TEXT; |
| 369 | input_method_ = input_method; |
| 370 | input_method_->SetFocusedTextInputClient(this); |
| 371 | delegate_->Activated(); |
| 372 | |
Darren Shen | cb250844 | 2019-07-03 21:48:23 | [diff] [blame] | 373 | if (!keyboard_ui_controller_ && |
| 374 | keyboard::KeyboardUIController::HasInstance()) { |
| 375 | auto* keyboard_ui_controller = keyboard::KeyboardUIController::Get(); |
| 376 | if (keyboard_ui_controller->IsEnabled()) { |
| 377 | keyboard_ui_controller_ = keyboard_ui_controller; |
| 378 | keyboard_ui_controller_->AddObserver(this); |
Jun Mukai | a446ee7f | 2018-07-20 22:55:15 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | if (pending_vk_visible_) { |
| 383 | input_method_->ShowVirtualKeyboardIfEnabled(); |
| 384 | pending_vk_visible_ = false; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | void TextInput::DetachInputMethod() { |
| 389 | if (!input_method_) { |
| 390 | DLOG(ERROR) << "input method already detached"; |
| 391 | return; |
| 392 | } |
| 393 | input_mode_ = ui::TEXT_INPUT_MODE_DEFAULT; |
| 394 | input_type_ = ui::TEXT_INPUT_TYPE_NONE; |
| 395 | input_method_->DetachTextInputClient(this); |
| 396 | input_method_ = nullptr; |
| 397 | delegate_->Deactivated(); |
| 398 | } |
| 399 | |
| 400 | } // namespace exo |