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 | 153 |
158 // Default to plain underline if we didn't find any span that we care about. | 154 // Default to plain underline if we didn't find any span that we care about. |
159 if (underlines.empty()) { | 155 if (underlines.empty()) { |
160 underlines.push_back(blink::WebCompositionUnderline( | 156 underlines.push_back(blink::WebCompositionUnderline( |
161 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | 157 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); |
162 } | 158 } |
163 // Sort spans by |.startOffset|. | |
164 std::sort(underlines.begin(), underlines.end()); | |
165 | 159 |
166 // relative_cursor_pos is as described in the Android API for | 160 // relative_cursor_pos is as described in the Android API for |
167 // InputConnection#setComposingText, whereas the parameters for | 161 // InputConnection#setComposingText, whereas the parameters for |
168 // ImeSetComposition are relative to the start of the composition. | 162 // ImeSetComposition are relative to the start of the composition. |
169 if (relative_cursor_pos > 0) | 163 if (relative_cursor_pos > 0) |
170 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; | 164 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; |
171 | 165 |
172 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), | 166 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), |
173 relative_cursor_pos, relative_cursor_pos); | 167 relative_cursor_pos, relative_cursor_pos); |
174 } | 168 } |
175 | 169 |
176 void ImeAdapterAndroid::CommitText(JNIEnv* env, | 170 void ImeAdapterAndroid::CommitText(JNIEnv* env, |
rlanday
2017/01/09 21:55:17
In theory I think this method should make the Andr
aelias_OOO_until_Jul13
2017/01/10 01:45:35
I think that will likely be needed sooner or later
rlanday
2017/01/10 05:02:17
That’s basically what I did in
https://codereview.
aelias_OOO_until_Jul13
2017/01/10 21:46:30
OK, this patch is large enough, let's land it as i
| |
177 const JavaParamRef<jobject>&, | 171 const JavaParamRef<jobject>& obj, |
172 const JavaParamRef<jobject>& text, | |
178 const JavaParamRef<jstring>& text_str, | 173 const JavaParamRef<jstring>& text_str, |
179 int relative_cursor_pos) { | 174 int relative_cursor_pos) { |
180 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 175 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
181 if (!rwhi) | 176 if (!rwhi) |
182 return; | 177 return; |
183 | 178 |
184 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 179 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
185 | 180 |
181 std::vector<blink::WebCompositionUnderline> underlines = | |
182 GetUnderlinesFromSpans(env, obj, text, text16); | |
183 | |
186 // relative_cursor_pos is as described in the Android API for | 184 // relative_cursor_pos is as described in the Android API for |
187 // InputConnection#commitText, whereas the parameters for | 185 // InputConnection#commitText, whereas the parameters for |
188 // ImeConfirmComposition are relative to the end of the composition. | 186 // ImeConfirmComposition are relative to the end of the composition. |
189 if (relative_cursor_pos > 0) | 187 if (relative_cursor_pos > 0) |
190 relative_cursor_pos--; | 188 relative_cursor_pos--; |
191 else | 189 else |
192 relative_cursor_pos -= text16.length(); | 190 relative_cursor_pos -= text16.length(); |
193 | 191 |
194 rwhi->ImeCommitText(text16, gfx::Range::InvalidRange(), relative_cursor_pos); | 192 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), |
193 relative_cursor_pos); | |
195 } | 194 } |
196 | 195 |
197 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 196 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
198 const JavaParamRef<jobject>&) { | 197 const JavaParamRef<jobject>&) { |
199 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 198 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
200 if (!rwhi) | 199 if (!rwhi) |
201 return; | 200 return; |
202 | 201 |
203 rwhi->ImeFinishComposingText(true); | 202 rwhi->ImeFinishComposingText(true); |
204 } | 203 } |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 return focused_frame->current_frame_host(); | 337 return focused_frame->current_frame_host(); |
339 } | 338 } |
340 | 339 |
341 WebContents* ImeAdapterAndroid::GetWebContents() { | 340 WebContents* ImeAdapterAndroid::GetWebContents() { |
342 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 341 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
343 if (!rwh) | 342 if (!rwh) |
344 return nullptr; | 343 return nullptr; |
345 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 344 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
346 } | 345 } |
347 | 346 |
347 std::vector<blink::WebCompositionUnderline> | |
348 ImeAdapterAndroid::GetUnderlinesFromSpans( | |
349 JNIEnv* env, | |
350 const base::android::JavaParamRef<jobject>& obj, | |
351 const base::android::JavaParamRef<jobject>& text, | |
352 const base::string16& text16) { | |
353 std::vector<blink::WebCompositionUnderline> underlines; | |
354 // Iterate over spans in |text|, dispatch those that we care about (e.g., | |
355 // BackgroundColorSpan) to a matching callback (e.g., | |
356 // AppendBackgroundColorSpan()), and populate |underlines|. | |
357 Java_ImeAdapter_populateUnderlinesFromSpans( | |
358 env, obj, text, reinterpret_cast<jlong>(&underlines)); | |
359 | |
360 // Sort spans by |.startOffset|. | |
361 std::sort(underlines.begin(), underlines.end()); | |
362 | |
363 return underlines; | |
364 } | |
365 | |
348 } // namespace content | 366 } // namespace content |
OLD | NEW |