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 SetScaleAndTranslate(float scale, |
| 40 const gfx::Vector2dF& translate) override; |
| 41 void SetSelectionRange(const gfx::Range& range) override; |
| 42 |
| 43 static bool Register(JNIEnv* env); |
| 44 |
| 45 private: |
| 46 base::android::ScopedJavaGlobalRef<jobject> builder_; |
| 47 base::MRUCache<base::string16, base::android::ScopedJavaGlobalRef<jstring>> |
| 48 composing_text_cache_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(CursorAnchorInfoBuilderImpl); |
| 51 }; |
| 52 |
| 53 CursorAnchorInfoBuilderImpl::CursorAnchorInfoBuilderImpl() |
| 54 : builder_(Java_CursorAnchorInfoBuilder_create(AttachCurrentThread())), |
| 55 composing_text_cache_(kComposingTextCacheSize) { |
| 56 } |
| 57 |
| 58 CursorAnchorInfoBuilderImpl::~CursorAnchorInfoBuilderImpl() { |
| 59 } |
| 60 |
| 61 void CursorAnchorInfoBuilderImpl::AddCharacterBounds( |
| 62 int index, const gfx::RectF& rect, uint32 flags) { |
| 63 JNIEnv* env = AttachCurrentThread(); |
| 64 Java_CursorAnchorInfoBuilder_addCharacterBounds( |
| 65 env, |
| 66 builder_.obj(), |
| 67 index, |
| 68 rect.x(), |
| 69 rect.y(), |
| 70 rect.right(), |
| 71 rect.bottom(), |
| 72 flags); |
| 73 } |
| 74 |
| 75 base::android::ScopedJavaLocalRef<jobject> |
| 76 CursorAnchorInfoBuilderImpl::Build() { |
| 77 JNIEnv* env = AttachCurrentThread(); |
| 78 return Java_CursorAnchorInfoBuilder_build(env, builder_.obj()); |
| 79 } |
| 80 |
| 81 void CursorAnchorInfoBuilderImpl::Reset() { |
| 82 JNIEnv* env = AttachCurrentThread(); |
| 83 Java_CursorAnchorInfoBuilder_reset(env, builder_.obj()); |
| 84 } |
| 85 |
| 86 void CursorAnchorInfoBuilderImpl::SetComposingText( |
| 87 int composing_text_start, base::StringPiece16 composing_text) { |
| 88 JNIEnv* env = AttachCurrentThread(); |
| 89 const auto& key = composing_text.as_string(); |
| 90 auto it = composing_text_cache_.Get(key); |
| 91 if (it != composing_text_cache_.end()) { |
| 92 Java_CursorAnchorInfoBuilder_setComposingText( |
| 93 env, builder_.obj(), composing_text_start, it->second.obj()); |
| 94 } else { |
| 95 auto jstr = base::android::ConvertUTF16ToJavaString(env, composing_text); |
| 96 Java_CursorAnchorInfoBuilder_setComposingText( |
| 97 env, builder_.obj(), composing_text_start, jstr.obj()); |
| 98 composing_text_cache_.Put( |
| 99 key, base::android::ScopedJavaGlobalRef<jstring>(jstr)); |
| 100 } |
| 101 } |
| 102 |
| 103 void CursorAnchorInfoBuilderImpl::SetInsertionMarkerLocation( |
| 104 float horizontalPosition, |
| 105 float lineTop, |
| 106 float lineBaseline, |
| 107 float lineBottom, |
| 108 uint32 flags) { |
| 109 JNIEnv* env = AttachCurrentThread(); |
| 110 Java_CursorAnchorInfoBuilder_setInsertionMarkerLocation( |
| 111 env, |
| 112 builder_.obj(), |
| 113 horizontalPosition, |
| 114 lineTop, |
| 115 lineBaseline, |
| 116 lineBottom, |
| 117 static_cast<jint>(flags)); |
| 118 } |
| 119 |
| 120 void CursorAnchorInfoBuilderImpl::SetScaleAndTranslate( |
| 121 float scale, |
| 122 const gfx::Vector2dF& translate) { |
| 123 JNIEnv* env = AttachCurrentThread(); |
| 124 Java_CursorAnchorInfoBuilder_setScaleAndTranslate( |
| 125 env, |
| 126 builder_.obj(), |
| 127 scale, |
| 128 translate.x(), |
| 129 translate.y()); |
| 130 } |
| 131 |
| 132 void CursorAnchorInfoBuilderImpl::SetSelectionRange(const gfx::Range& range) { |
| 133 JNIEnv* env = AttachCurrentThread(); |
| 134 Java_CursorAnchorInfoBuilder_setSelectionRange( |
| 135 env, builder_.obj(), range.start(), range.end()); |
| 136 } |
| 137 |
| 138 } // namespace |
| 139 |
| 140 CursorAnchorInfoBuilder::CursorAnchorInfoBuilder() { |
| 141 } |
| 142 |
| 143 CursorAnchorInfoBuilder::~CursorAnchorInfoBuilder() { |
| 144 } |
| 145 |
| 146 // static |
| 147 scoped_ptr<CursorAnchorInfoBuilder> |
| 148 CursorAnchorInfoBuilder::Create() { |
| 149 return scoped_ptr<CursorAnchorInfoBuilder>(new CursorAnchorInfoBuilderImpl); |
| 150 } |
| 151 |
| 152 // static |
| 153 bool CursorAnchorInfoBuilder::Register(JNIEnv* env) { |
| 154 return RegisterNativesImpl(env); |
| 155 } |
| 156 |
| 157 } // namespace ui |
OLD | NEW |