1 // Copyright 2015 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.
5 #include "chrome/browser/android/compositor/scene_layer/tab_strip_scene_layer.h"
7 #include "base/android/jni_android.h"
8 #include "chrome/browser/android/compositor/layer/tab_handle_layer.h"
9 #include "chrome/browser/android/compositor/layer_title_cache.h"
10 #include "jni/TabStripSceneLayer_jni.h"
11 #include "ui/android/resources/resource_manager_impl.h"
12 #include "ui/android/resources/ui_resource_android.h"
17 TabStripSceneLayer::TabStripSceneLayer(JNIEnv
* env
, jobject jobj
)
18 : SceneLayer(env
, jobj
),
19 background_layer_(cc::SolidColorLayer::Create()),
20 new_tab_button_(cc::UIResourceLayer::Create()),
21 model_selector_button_(cc::UIResourceLayer::Create()),
22 strip_brightness_(1.f
),
24 content_tree_(nullptr) {
25 background_layer_
->SetBackgroundColor(SK_ColorBLACK
);
26 background_layer_
->SetIsDrawable(true);
27 new_tab_button_
->SetIsDrawable(true);
28 model_selector_button_
->SetIsDrawable(true);
29 layer()->AddChild(background_layer_
);
30 layer()->AddChild(new_tab_button_
);
31 layer()->AddChild(model_selector_button_
);
34 TabStripSceneLayer::~TabStripSceneLayer() {
37 void TabStripSceneLayer::SetContentTree(JNIEnv
* env
,
39 jobject jcontent_tree
) {
40 SceneLayer
* content_tree
= FromJavaObject(env
, jcontent_tree
);
42 (!content_tree_
->layer()->parent() ||
43 content_tree_
->layer()->parent()->id() != layer()->id()))
44 content_tree_
= nullptr;
46 if (content_tree
!= content_tree_
) {
48 content_tree_
->layer()->RemoveFromParent();
49 content_tree_
= content_tree
;
51 layer()->InsertChild(content_tree
->layer(), 0);
52 content_tree
->layer()->SetPosition(
53 gfx::PointF(0, -layer()->position().y()));
58 void TabStripSceneLayer::BeginBuildingFrame(JNIEnv
* env
, jobject jobj
) {
62 void TabStripSceneLayer::FinishBuildingFrame(JNIEnv
* env
, jobject jobj
) {
63 for (unsigned i
= write_index_
; i
< tab_handle_layers_
.size(); ++i
)
64 tab_handle_layers_
[i
]->layer()->RemoveFromParent();
66 tab_handle_layers_
.erase(tab_handle_layers_
.begin() + write_index_
,
67 tab_handle_layers_
.end());
70 void TabStripSceneLayer::UpdateTabStripLayer(JNIEnv
* env
,
75 jfloat strip_brightness
) {
76 strip_brightness_
= strip_brightness
;
77 gfx::RectF
content(0, y_offset
, width
, height
);
78 layer()->SetPosition(gfx::PointF(0, y_offset
));
79 background_layer_
->SetBounds(gfx::Size(width
, height
));
81 content_tree_
->layer()->SetPosition(gfx::PointF(0, -y_offset
));
84 void TabStripSceneLayer::UpdateNewTabButton(JNIEnv
* env
,
92 jobject jresource_manager
) {
93 ui::ResourceManager
* resource_manager
=
94 ui::ResourceManagerImpl::FromJavaObject(jresource_manager
);
95 ui::ResourceManager::Resource
* button_resource
=
96 resource_manager
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
99 new_tab_button_
->SetUIResourceId(button_resource
->ui_resource
->id());
100 float left_offset
= (width
- button_resource
->size
.width()) / 2;
101 float top_offset
= (height
- button_resource
->size
.height()) / 2;
102 new_tab_button_
->SetPosition(gfx::PointF(x
+ left_offset
, y
+ top_offset
));
103 new_tab_button_
->SetBounds(button_resource
->size
);
104 new_tab_button_
->SetHideLayerAndSubtree(!visible
);
107 void TabStripSceneLayer::UpdateModelSelectorButton(JNIEnv
* env
,
116 jobject jresource_manager
) {
117 ui::ResourceManager
* resource_manager
=
118 ui::ResourceManagerImpl::FromJavaObject(jresource_manager
);
119 ui::ResourceManager::Resource
* button_resource
=
120 resource_manager
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
123 model_selector_button_
->SetUIResourceId(button_resource
->ui_resource
->id());
124 float left_offset
= (width
- button_resource
->size
.width()) / 2;
125 float top_offset
= (height
- button_resource
->size
.height()) / 2;
126 model_selector_button_
->SetPosition(
127 gfx::PointF(x
+ left_offset
, y
+ top_offset
));
128 model_selector_button_
->SetBounds(button_resource
->size
);
129 model_selector_button_
->SetHideLayerAndSubtree(!visible
);
132 void TabStripSceneLayer::PutStripTabLayer(JNIEnv
* env
,
135 jint close_resource_id
,
136 jint handle_resource_id
,
138 jboolean close_pressed
,
139 jfloat toolbar_width
,
144 jfloat content_offset_x
,
145 jfloat close_button_alpha
,
147 jfloat spinner_rotation
,
148 jfloat border_opacity
,
149 jobject jlayer_title_cache
,
150 jobject jresource_manager
) {
151 LayerTitleCache
* layer_title_cache
=
152 LayerTitleCache::FromJavaObject(jlayer_title_cache
);
153 ui::ResourceManager
* resource_manager
=
154 ui::ResourceManagerImpl::FromJavaObject(jresource_manager
);
155 scoped_refptr
<TabHandleLayer
> layer
= GetNextLayer(layer_title_cache
);
156 ui::ResourceManager::Resource
* tab_handle_resource
=
157 resource_manager
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
159 ui::ResourceManager::Resource
* close_button_resource
=
160 resource_manager
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
162 layer
->SetProperties(id
, close_button_resource
, tab_handle_resource
,
163 foreground
, close_pressed
, toolbar_width
, x
, y
, width
,
164 height
, content_offset_x
, close_button_alpha
, is_loading
,
165 spinner_rotation
, strip_brightness_
, border_opacity
);
168 scoped_refptr
<TabHandleLayer
> TabStripSceneLayer::GetNextLayer(
169 LayerTitleCache
* layer_title_cache
) {
170 if (write_index_
< tab_handle_layers_
.size())
171 return tab_handle_layers_
[write_index_
++];
173 scoped_refptr
<TabHandleLayer
> layer_tree
=
174 TabHandleLayer::Create(layer_title_cache
);
175 tab_handle_layers_
.push_back(layer_tree
);
176 this->layer()->AddChild(layer_tree
->layer());
181 static jlong
Init(JNIEnv
* env
, jobject jobj
) {
182 // This will automatically bind to the Java object and pass ownership there.
183 TabStripSceneLayer
* scene_layer
= new TabStripSceneLayer(env
, jobj
);
184 return reinterpret_cast<intptr_t>(scene_layer
);
187 bool RegisterTabStripSceneLayer(JNIEnv
* env
) {
188 return RegisterNativesImpl(env
);
191 } // namespace android
192 } // namespace chrome