OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/text_input_manager.h" | 5 #include "content/browser/renderer_host/text_input_manager.h" |
6 | 6 |
| 7 #include "base/strings/string16.h" |
7 #include "content/browser/renderer_host/render_widget_host_impl.h" | 8 #include "content/browser/renderer_host/render_widget_host_impl.h" |
8 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 9 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
9 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
10 #include "ui/gfx/geometry/rect.h" | 11 #include "ui/gfx/geometry/rect.h" |
| 12 #include "ui/gfx/range/range.h" |
11 | 13 |
12 namespace content { | 14 namespace content { |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 bool AreDifferentTextInputStates(const content::TextInputState& old_state, | 18 bool AreDifferentTextInputStates(const content::TextInputState& old_state, |
17 const content::TextInputState& new_state) { | 19 const content::TextInputState& new_state) { |
18 #if defined(USE_AURA) | 20 #if defined(USE_AURA) |
19 return old_state.type != new_state.type || old_state.mode != new_state.mode || | 21 return old_state.type != new_state.type || old_state.mode != new_state.mode || |
20 old_state.flags != new_state.flags || | 22 old_state.flags != new_state.flags || |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 selection_region_map_.at(active_view_).focus); | 67 selection_region_map_.at(active_view_).focus); |
66 } | 68 } |
67 | 69 |
68 const std::vector<gfx::Rect>* TextInputManager::GetCompositionCharacterBounds() | 70 const std::vector<gfx::Rect>* TextInputManager::GetCompositionCharacterBounds() |
69 const { | 71 const { |
70 return !!active_view_ | 72 return !!active_view_ |
71 ? &composition_range_info_map_.at(active_view_).character_bounds | 73 ? &composition_range_info_map_.at(active_view_).character_bounds |
72 : nullptr; | 74 : nullptr; |
73 } | 75 } |
74 | 76 |
| 77 const TextInputManager::TextSelection* TextInputManager::GetTextSelection( |
| 78 RenderWidgetHostViewBase* view) const { |
| 79 DCHECK(!view || IsRegistered(view)); |
| 80 if (!view) |
| 81 view = active_view_; |
| 82 return !!view ? &text_selection_map_.at(view) : nullptr; |
| 83 } |
| 84 |
75 void TextInputManager::UpdateTextInputState( | 85 void TextInputManager::UpdateTextInputState( |
76 RenderWidgetHostViewBase* view, | 86 RenderWidgetHostViewBase* view, |
77 const TextInputState& text_input_state) { | 87 const TextInputState& text_input_state) { |
78 DCHECK(IsRegistered(view)); | 88 DCHECK(IsRegistered(view)); |
79 | 89 |
80 // Since |view| is registgered, we already have a previous value for its | 90 // Since |view| is registgered, we already have a previous value for its |
81 // TextInputState. | 91 // TextInputState. |
82 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], | 92 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], |
83 text_input_state); | 93 text_input_state); |
84 | 94 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // before being stored. | 177 // before being stored. |
168 for (auto rect : character_bounds) { | 178 for (auto rect : character_bounds) { |
169 composition_range_info_map_[view].character_bounds.emplace_back(gfx::Rect( | 179 composition_range_info_map_[view].character_bounds.emplace_back(gfx::Rect( |
170 view->TransformPointToRootCoordSpace(rect.origin()), rect.size())); | 180 view->TransformPointToRootCoordSpace(rect.origin()), rect.size())); |
171 } | 181 } |
172 | 182 |
173 FOR_EACH_OBSERVER(Observer, observer_list_, | 183 FOR_EACH_OBSERVER(Observer, observer_list_, |
174 OnImeCompositionRangeChanged(this, view)); | 184 OnImeCompositionRangeChanged(this, view)); |
175 } | 185 } |
176 | 186 |
| 187 void TextInputManager::SelectionChanged(RenderWidgetHostViewBase* view, |
| 188 const base::string16& text, |
| 189 size_t offset, |
| 190 const gfx::Range& range) { |
| 191 DCHECK(IsRegistered(view)); |
| 192 |
| 193 text_selection_map_[view].text = text; |
| 194 text_selection_map_[view].offset = offset; |
| 195 text_selection_map_[view].range.set_start(range.start()); |
| 196 text_selection_map_[view].range.set_end(range.end()); |
| 197 |
| 198 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 199 OnTextSelectionChanged(this, view)); |
| 200 } |
| 201 |
177 void TextInputManager::Register(RenderWidgetHostViewBase* view) { | 202 void TextInputManager::Register(RenderWidgetHostViewBase* view) { |
178 DCHECK(!IsRegistered(view)); | 203 DCHECK(!IsRegistered(view)); |
179 | 204 |
180 text_input_state_map_[view] = TextInputState(); | 205 text_input_state_map_[view] = TextInputState(); |
181 selection_region_map_[view] = SelectionRegion(); | 206 selection_region_map_[view] = SelectionRegion(); |
182 composition_range_info_map_[view] = CompositionRangeInfo(); | 207 composition_range_info_map_[view] = CompositionRangeInfo(); |
| 208 text_selection_map_[view] = TextSelection(); |
183 } | 209 } |
184 | 210 |
185 void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { | 211 void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { |
186 DCHECK(IsRegistered(view)); | 212 DCHECK(IsRegistered(view)); |
187 | 213 |
188 text_input_state_map_.erase(view); | 214 text_input_state_map_.erase(view); |
189 selection_region_map_.erase(view); | 215 selection_region_map_.erase(view); |
190 composition_range_info_map_.erase(view); | 216 composition_range_info_map_.erase(view); |
| 217 text_selection_map_.erase(view); |
191 | 218 |
192 if (active_view_ == view) { | 219 if (active_view_ == view) { |
193 active_view_ = nullptr; | 220 active_view_ = nullptr; |
194 NotifyObserversAboutInputStateUpdate(view, true); | 221 NotifyObserversAboutInputStateUpdate(view, true); |
195 } | 222 } |
196 view->DidUnregisterFromTextInputManager(this); | 223 view->DidUnregisterFromTextInputManager(this); |
197 } | 224 } |
198 | 225 |
199 bool TextInputManager::IsRegistered(RenderWidgetHostViewBase* view) const { | 226 bool TextInputManager::IsRegistered(RenderWidgetHostViewBase* view) const { |
200 return text_input_state_map_.count(view) == 1; | 227 return text_input_state_map_.count(view) == 1; |
(...skipping 30 matching lines...) Expand all Loading... |
231 TextInputManager::SelectionRegion::SelectionRegion( | 258 TextInputManager::SelectionRegion::SelectionRegion( |
232 const SelectionRegion& other) = default; | 259 const SelectionRegion& other) = default; |
233 | 260 |
234 TextInputManager::CompositionRangeInfo::CompositionRangeInfo() {} | 261 TextInputManager::CompositionRangeInfo::CompositionRangeInfo() {} |
235 | 262 |
236 TextInputManager::CompositionRangeInfo::CompositionRangeInfo( | 263 TextInputManager::CompositionRangeInfo::CompositionRangeInfo( |
237 const CompositionRangeInfo& other) = default; | 264 const CompositionRangeInfo& other) = default; |
238 | 265 |
239 TextInputManager::CompositionRangeInfo::~CompositionRangeInfo() {} | 266 TextInputManager::CompositionRangeInfo::~CompositionRangeInfo() {} |
240 | 267 |
| 268 TextInputManager::TextSelection::TextSelection() |
| 269 : offset(0), range(gfx::Range::InvalidRange()), text(base::string16()) {} |
| 270 |
| 271 TextInputManager::TextSelection::TextSelection(const TextSelection& other) = |
| 272 default; |
| 273 |
| 274 TextInputManager::TextSelection::~TextSelection() {} |
| 275 |
241 } // namespace content | 276 } // namespace content |
OLD | NEW |