| 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 "content/browser/renderer_host/render_widget_host_impl.h" | 7 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 8 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 8 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 9 #include "content/common/view_messages.h" |
| 9 | 10 |
| 10 namespace content { | 11 namespace content { |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 bool AreDifferentTextInputStates(const content::TextInputState& old_state, | 15 bool AreDifferentTextInputStates(const content::TextInputState& old_state, |
| 15 const content::TextInputState& new_state) { | 16 const content::TextInputState& new_state) { |
| 16 #if defined(USE_AURA) | 17 #if defined(USE_AURA) |
| 17 return old_state.type != new_state.type || old_state.mode != new_state.mode || | 18 return old_state.type != new_state.type || old_state.mode != new_state.mode || |
| 18 old_state.flags != new_state.flags || | 19 old_state.flags != new_state.flags || |
| (...skipping 28 matching lines...) Expand all Loading... |
| 47 const TextInputState* TextInputManager::GetTextInputState() { | 48 const TextInputState* TextInputManager::GetTextInputState() { |
| 48 return !!active_view_ ? &text_input_state_map_[active_view_] : nullptr; | 49 return !!active_view_ ? &text_input_state_map_[active_view_] : nullptr; |
| 49 } | 50 } |
| 50 | 51 |
| 51 RenderWidgetHostImpl* TextInputManager::GetActiveWidget() const { | 52 RenderWidgetHostImpl* TextInputManager::GetActiveWidget() const { |
| 52 return !!active_view_ ? static_cast<RenderWidgetHostImpl*>( | 53 return !!active_view_ ? static_cast<RenderWidgetHostImpl*>( |
| 53 active_view_->GetRenderWidgetHost()) | 54 active_view_->GetRenderWidgetHost()) |
| 54 : nullptr; | 55 : nullptr; |
| 55 } | 56 } |
| 56 | 57 |
| 58 gfx::Rect TextInputManager::GetSelectionBoundsRect() { |
| 59 if (!active_view_) |
| 60 return gfx::Rect(); |
| 61 |
| 62 return gfx::RectBetweenSelectionBounds( |
| 63 selection_bound_map_[active_view_].first, |
| 64 selection_bound_map_[active_view_].second); |
| 65 } |
| 66 |
| 57 void TextInputManager::UpdateTextInputState( | 67 void TextInputManager::UpdateTextInputState( |
| 58 RenderWidgetHostViewBase* view, | 68 RenderWidgetHostViewBase* view, |
| 59 const TextInputState& text_input_state) { | 69 const TextInputState& text_input_state) { |
| 60 DCHECK(IsRegistered(view)); | 70 DCHECK(IsRegistered(view)); |
| 61 | 71 |
| 62 // Since |view| is registgered, we already have a previous value for its | 72 // Since |view| is registgered, we already have a previous value for its |
| 63 // TextInputState. | 73 // TextInputState. |
| 64 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], | 74 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], |
| 65 text_input_state); | 75 text_input_state); |
| 66 | 76 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 77 | 87 |
| 78 NotifyObserversAboutInputStateUpdate(view, changed); | 88 NotifyObserversAboutInputStateUpdate(view, changed); |
| 79 } | 89 } |
| 80 | 90 |
| 81 void TextInputManager::ImeCancelComposition(RenderWidgetHostViewBase* view) { | 91 void TextInputManager::ImeCancelComposition(RenderWidgetHostViewBase* view) { |
| 82 DCHECK(IsRegistered(view)); | 92 DCHECK(IsRegistered(view)); |
| 83 FOR_EACH_OBSERVER(Observer, observer_list_, | 93 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 84 OnImeCancelComposition(this, view)); | 94 OnImeCancelComposition(this, view)); |
| 85 } | 95 } |
| 86 | 96 |
| 97 void TextInputManager::SelectionBoundsChanged( |
| 98 RenderWidgetHostViewBase* view, |
| 99 const ViewHostMsg_SelectionBounds_Params& params) { |
| 100 DCHECK(IsRegistered(view)); |
| 101 // TODO(ekaramad): Implement the logic for other platforms (crbug.com/578168). |
| 102 #if defined(USE_AURA) |
| 103 gfx::SelectionBound anchor_bound, focus_bound; |
| 104 anchor_bound.SetEdge(gfx::PointF(params.anchor_rect.origin()), |
| 105 gfx::PointF(params.anchor_rect.bottom_left())); |
| 106 focus_bound.SetEdge(gfx::PointF(params.focus_rect.origin()), |
| 107 gfx::PointF(params.focus_rect.bottom_left())); |
| 108 |
| 109 if (params.anchor_rect == params.focus_rect) { |
| 110 anchor_bound.set_type(gfx::SelectionBound::CENTER); |
| 111 focus_bound.set_type(gfx::SelectionBound::CENTER); |
| 112 } else { |
| 113 // Whether text is LTR at the anchor handle. |
| 114 bool anchor_LTR = params.anchor_dir == blink::WebTextDirectionLeftToRight; |
| 115 // Whether text is LTR at the focus handle. |
| 116 bool focus_LTR = params.focus_dir == blink::WebTextDirectionLeftToRight; |
| 117 |
| 118 if ((params.is_anchor_first && anchor_LTR) || |
| 119 (!params.is_anchor_first && !anchor_LTR)) { |
| 120 anchor_bound.set_type(gfx::SelectionBound::LEFT); |
| 121 } else { |
| 122 anchor_bound.set_type(gfx::SelectionBound::RIGHT); |
| 123 } |
| 124 if ((params.is_anchor_first && focus_LTR) || |
| 125 (!params.is_anchor_first && !focus_LTR)) { |
| 126 focus_bound.set_type(gfx::SelectionBound::RIGHT); |
| 127 } else { |
| 128 focus_bound.set_type(gfx::SelectionBound::LEFT); |
| 129 } |
| 130 } |
| 131 |
| 132 if (anchor_bound == selection_bound_map_[view].first && |
| 133 focus_bound == selection_bound_map_[view].second) |
| 134 return; |
| 135 |
| 136 selection_bound_map_[view].first = anchor_bound; |
| 137 selection_bound_map_[view].second = focus_bound; |
| 138 |
| 139 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 140 OnSelectionBoundsChanged(this, view)); |
| 141 #endif |
| 142 } |
| 143 |
| 87 void TextInputManager::Register(RenderWidgetHostViewBase* view) { | 144 void TextInputManager::Register(RenderWidgetHostViewBase* view) { |
| 88 DCHECK(!IsRegistered(view)); | 145 DCHECK(!IsRegistered(view)); |
| 89 | 146 |
| 90 text_input_state_map_[view] = TextInputState(); | 147 text_input_state_map_[view] = TextInputState(); |
| 91 } | 148 } |
| 92 | 149 |
| 93 void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { | 150 void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { |
| 94 DCHECK(IsRegistered(view)); | 151 DCHECK(IsRegistered(view)); |
| 95 | 152 |
| 96 text_input_state_map_.erase(view); | 153 text_input_state_map_.erase(view); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 125 | 182 |
| 126 void TextInputManager::NotifyObserversAboutInputStateUpdate( | 183 void TextInputManager::NotifyObserversAboutInputStateUpdate( |
| 127 RenderWidgetHostViewBase* updated_view, | 184 RenderWidgetHostViewBase* updated_view, |
| 128 bool did_update_state) { | 185 bool did_update_state) { |
| 129 FOR_EACH_OBSERVER( | 186 FOR_EACH_OBSERVER( |
| 130 Observer, observer_list_, | 187 Observer, observer_list_, |
| 131 OnUpdateTextInputStateCalled(this, updated_view, did_update_state)); | 188 OnUpdateTextInputStateCalled(this, updated_view, did_update_state)); |
| 132 } | 189 } |
| 133 | 190 |
| 134 } // namespace content | 191 } // namespace content |
| OLD | NEW |