Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ime_adapter_android.h" | 5 #include "content/browser/renderer_host/ime_adapter_android.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <android/input.h> | 8 #include <android/input.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 const JavaParamRef<jobject>& obj, | 141 const JavaParamRef<jobject>& obj, |
| 142 const JavaParamRef<jobject>& text, | 142 const JavaParamRef<jobject>& text, |
| 143 const JavaParamRef<jstring>& text_str, | 143 const JavaParamRef<jstring>& text_str, |
| 144 int relative_cursor_pos) { | 144 int relative_cursor_pos) { |
| 145 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 145 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 146 if (!rwhi) | 146 if (!rwhi) |
| 147 return; | 147 return; |
| 148 | 148 |
| 149 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 149 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 150 | 150 |
| 151 std::vector<blink::WebCompositionUnderline> underlines; | 151 std::vector<blink::WebCompositionUnderline> underlines = |
| 152 // Iterate over spans in |text|, dispatch those that we care about (e.g., | 152 GetUnderlinesFromSpans(env, obj, text, text16); |
| 153 // BackgroundColorSpan) to a matching callback (e.g., | |
| 154 // AppendBackgroundColorSpan()), and populate |underlines|. | |
| 155 Java_ImeAdapter_populateUnderlinesFromSpans( | |
| 156 env, obj, text, reinterpret_cast<jlong>(&underlines)); | |
| 157 | |
| 158 // Default to plain underline if we didn't find any span that we care about. | |
| 159 if (underlines.empty()) { | |
| 160 underlines.push_back(blink::WebCompositionUnderline( | |
| 161 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | |
| 162 } | |
| 163 // Sort spans by |.startOffset|. | |
| 164 std::sort(underlines.begin(), underlines.end()); | |
| 165 | 153 |
| 166 // relative_cursor_pos is as described in the Android API for | 154 // relative_cursor_pos is as described in the Android API for |
| 167 // InputConnection#setComposingText, whereas the parameters for | 155 // InputConnection#setComposingText, whereas the parameters for |
| 168 // ImeSetComposition are relative to the start of the composition. | 156 // ImeSetComposition are relative to the start of the composition. |
| 169 if (relative_cursor_pos > 0) | 157 if (relative_cursor_pos > 0) |
| 170 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; | 158 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; |
| 171 | 159 |
| 172 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), | 160 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), |
| 173 relative_cursor_pos, relative_cursor_pos); | 161 relative_cursor_pos, relative_cursor_pos); |
| 174 } | 162 } |
| 175 | 163 |
| 176 void ImeAdapterAndroid::CommitText(JNIEnv* env, | 164 void ImeAdapterAndroid::CommitText(JNIEnv* env, |
| 177 const JavaParamRef<jobject>&, | 165 const JavaParamRef<jobject>& obj, |
| 166 const JavaParamRef<jobject>& text, | |
| 178 const JavaParamRef<jstring>& text_str, | 167 const JavaParamRef<jstring>& text_str, |
| 179 int relative_cursor_pos) { | 168 int relative_cursor_pos) { |
| 180 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 169 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 181 if (!rwhi) | 170 if (!rwhi) |
| 182 return; | 171 return; |
| 183 | 172 |
| 184 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 173 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 185 | 174 |
| 175 std::vector<blink::WebCompositionUnderline> underlines = | |
| 176 GetUnderlinesFromSpans(env, obj, text, text16); | |
| 177 | |
| 186 // relative_cursor_pos is as described in the Android API for | 178 // relative_cursor_pos is as described in the Android API for |
| 187 // InputConnection#commitText, whereas the parameters for | 179 // InputConnection#commitText, whereas the parameters for |
| 188 // ImeConfirmComposition are relative to the end of the composition. | 180 // ImeConfirmComposition are relative to the end of the composition. |
| 189 if (relative_cursor_pos > 0) | 181 if (relative_cursor_pos > 0) |
| 190 relative_cursor_pos--; | 182 relative_cursor_pos--; |
| 191 else | 183 else |
| 192 relative_cursor_pos -= text16.length(); | 184 relative_cursor_pos -= text16.length(); |
| 193 | 185 |
| 194 rwhi->ImeCommitText(text16, gfx::Range::InvalidRange(), relative_cursor_pos); | 186 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), |
| 187 relative_cursor_pos); | |
| 195 } | 188 } |
| 196 | 189 |
| 197 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 190 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
| 198 const JavaParamRef<jobject>&) { | 191 const JavaParamRef<jobject>&) { |
| 199 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 192 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 200 if (!rwhi) | 193 if (!rwhi) |
| 201 return; | 194 return; |
| 202 | 195 |
| 203 rwhi->ImeFinishComposingText(true); | 196 rwhi->ImeFinishComposingText(true); |
| 204 } | 197 } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 return focused_frame->current_frame_host(); | 331 return focused_frame->current_frame_host(); |
| 339 } | 332 } |
| 340 | 333 |
| 341 WebContents* ImeAdapterAndroid::GetWebContents() { | 334 WebContents* ImeAdapterAndroid::GetWebContents() { |
| 342 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 335 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
| 343 if (!rwh) | 336 if (!rwh) |
| 344 return nullptr; | 337 return nullptr; |
| 345 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 338 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
| 346 } | 339 } |
| 347 | 340 |
| 341 std::vector<blink::WebCompositionUnderline> | |
| 342 ImeAdapterAndroid::GetUnderlinesFromSpans( | |
| 343 JNIEnv* env, | |
| 344 const base::android::JavaParamRef<jobject>& obj, | |
| 345 const base::android::JavaParamRef<jobject>& text, | |
| 346 const base::string16& text16) { | |
| 347 std::vector<blink::WebCompositionUnderline> underlines; | |
| 348 // Iterate over spans in |text|, dispatch those that we care about (e.g., | |
| 349 // BackgroundColorSpan) to a matching callback (e.g., | |
| 350 // AppendBackgroundColorSpan()), and populate |underlines|. | |
| 351 Java_ImeAdapter_populateUnderlinesFromSpans( | |
| 352 env, obj, text, reinterpret_cast<jlong>(&underlines)); | |
| 353 | |
| 354 // Default to plain underline if we didn't find any span that we care about. | |
| 355 if (underlines.empty()) { | |
| 356 underlines.push_back(blink::WebCompositionUnderline( | |
|
rlanday
2016/12/14 00:04:53
Actually I think we probably don't want this for c
| |
| 357 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | |
| 358 } | |
| 359 // Sort spans by |.startOffset|. | |
| 360 std::sort(underlines.begin(), underlines.end()); | |
| 361 | |
| 362 return underlines; | |
| 363 } | |
| 364 | |
| 348 } // namespace content | 365 } // namespace content |
| OLD | NEW |