OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "ui/base/ime/android/cursor_anchor_info_builder.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/jni_weak_ref.h" |
| 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/containers/mru_cache.h" |
| 12 #include "jni/CursorAnchorInfoBuilder_jni.h" |
| 13 |
| 14 namespace ui { |
| 15 namespace { |
| 16 |
| 17 using base::android::AttachCurrentThread; |
| 18 |
| 19 const size_t kComposingTextCacheSize = 2; |
| 20 |
| 21 class CursorAnchorInfoBuilderImpl final |
| 22 : public CursorAnchorInfoBuilder { |
| 23 public: |
| 24 CursorAnchorInfoBuilderImpl(); |
| 25 ~CursorAnchorInfoBuilderImpl() override; |
| 26 |
| 27 void AddCharacterBounds(int index, |
| 28 const gfx::RectF& rect, |
| 29 uint32 flags) override; |
| 30 base::android::ScopedJavaLocalRef<jobject> Build() override; |
| 31 void Reset() override; |
| 32 void SetComposingText(int composingTextStart, |
| 33 base::StringPiece16 composingText) override; |
| 34 void SetInsertionMarkerLocation(float horizontalPosition, |
| 35 float lineTop, |
| 36 float lineBaseline, |
| 37 float lineBottom, |
| 38 uint32 flags) override; |
| 39 void SetMatrix(const gfx::Matrix3F& matrix) override; |
| 40 void SetSelectionRange(const gfx::Range& range) override; |
| 41 |
| 42 static bool Register(JNIEnv* env); |
| 43 |
| 44 private: |
| 45 base::android::ScopedJavaGlobalRef<jobject> builder_; |
| 46 base::MRUCache<base::string16, base::android::ScopedJavaGlobalRef<jstring>> |
| 47 composing_text_cache_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(CursorAnchorInfoBuilderImpl); |
| 50 }; |
| 51 |
| 52 CursorAnchorInfoBuilderImpl::CursorAnchorInfoBuilderImpl() |
| 53 : builder_(Java_CursorAnchorInfoBuilder_create(AttachCurrentThread())), |
| 54 composing_text_cache_(kComposingTextCacheSize) { |
| 55 } |
| 56 |
| 57 CursorAnchorInfoBuilderImpl::~CursorAnchorInfoBuilderImpl() { |
| 58 } |
| 59 |
| 60 void CursorAnchorInfoBuilderImpl::AddCharacterBounds( |
| 61 int index, const gfx::RectF& rect, uint32 flags) { |
| 62 JNIEnv* env = AttachCurrentThread(); |
| 63 Java_CursorAnchorInfoBuilder_addCharacterBounds( |
| 64 env, |
| 65 builder_.obj(), |
| 66 index, |
| 67 rect.x(), |
| 68 rect.y(), |
| 69 rect.right(), |
| 70 rect.bottom(), |
| 71 flags); |
| 72 } |
| 73 |
| 74 base::android::ScopedJavaLocalRef<jobject> |
| 75 CursorAnchorInfoBuilderImpl::Build() { |
| 76 JNIEnv* env = AttachCurrentThread(); |
| 77 return Java_CursorAnchorInfoBuilder_build(env, builder_.obj()); |
| 78 } |
| 79 |
| 80 void CursorAnchorInfoBuilderImpl::Reset() { |
| 81 JNIEnv* env = AttachCurrentThread(); |
| 82 Java_CursorAnchorInfoBuilder_reset(env, builder_.obj()); |
| 83 } |
| 84 |
| 85 void CursorAnchorInfoBuilderImpl::SetComposingText( |
| 86 int composing_text_start, base::StringPiece16 composing_text) { |
| 87 JNIEnv* env = AttachCurrentThread(); |
| 88 const auto& key = composing_text.as_string(); |
| 89 auto it = composing_text_cache_.Get(key); |
| 90 if (it != composing_text_cache_.end()) { |
| 91 Java_CursorAnchorInfoBuilder_setComposingText( |
| 92 env, builder_.obj(), composing_text_start, it->second.obj()); |
| 93 } else { |
| 94 auto jstr = base::android::ConvertUTF16ToJavaString(env, composing_text); |
| 95 Java_CursorAnchorInfoBuilder_setComposingText( |
| 96 env, builder_.obj(), composing_text_start, jstr.obj()); |
| 97 composing_text_cache_.Put( |
| 98 key, base::android::ScopedJavaGlobalRef<jstring>(jstr)); |
| 99 } |
| 100 } |
| 101 |
| 102 void CursorAnchorInfoBuilderImpl::SetInsertionMarkerLocation( |
| 103 float horizontalPosition, |
| 104 float lineTop, |
| 105 float lineBaseline, |
| 106 float lineBottom, |
| 107 uint32 flags) { |
| 108 JNIEnv* env = AttachCurrentThread(); |
| 109 Java_CursorAnchorInfoBuilder_setInsertionMarkerLocation( |
| 110 env, |
| 111 builder_.obj(), |
| 112 horizontalPosition, |
| 113 lineTop, |
| 114 lineBaseline, |
| 115 lineBottom, |
| 116 static_cast<jint>(flags)); |
| 117 } |
| 118 |
| 119 void CursorAnchorInfoBuilderImpl::SetMatrix(const gfx::Matrix3F& matrix) { |
| 120 JNIEnv* env = AttachCurrentThread(); |
| 121 Java_CursorAnchorInfoBuilder_setMatrix( |
| 122 env, |
| 123 builder_.obj(), |
| 124 matrix.get(0, 0), |
| 125 matrix.get(0, 1), |
| 126 matrix.get(0, 2), |
| 127 matrix.get(1, 0), |
| 128 matrix.get(1, 1), |
| 129 matrix.get(1, 2), |
| 130 matrix.get(2, 0), |
| 131 matrix.get(2, 1), |
| 132 matrix.get(2, 2)); |
| 133 } |
| 134 |
| 135 void CursorAnchorInfoBuilderImpl::SetSelectionRange(const gfx::Range& range) { |
| 136 JNIEnv* env = AttachCurrentThread(); |
| 137 Java_CursorAnchorInfoBuilder_setSelectionRange( |
| 138 env, builder_.obj(), range.start(), range.end()); |
| 139 } |
| 140 |
| 141 } // namespace |
| 142 |
| 143 CursorAnchorInfoBuilder::CursorAnchorInfoBuilder() { |
| 144 } |
| 145 |
| 146 CursorAnchorInfoBuilder::~CursorAnchorInfoBuilder() { |
| 147 } |
| 148 |
| 149 // static |
| 150 scoped_ptr<CursorAnchorInfoBuilder> |
| 151 CursorAnchorInfoBuilder::Create() { |
| 152 return scoped_ptr<CursorAnchorInfoBuilder>(new CursorAnchorInfoBuilderImpl); |
| 153 } |
| 154 |
| 155 // static |
| 156 bool CursorAnchorInfoBuilder::Register(JNIEnv* env) { |
| 157 return RegisterNativesImpl(env); |
| 158 } |
| 159 |
| 160 } // namespace ui |
OLD | NEW |