Upstream oodles of Chrome for Android code into Chromium.
[chromium-blink-merge.git] / chrome / browser / android / compositor / scene_layer / tab_strip_scene_layer.cc
blob3a997a1b68578e5e80759e0b978fb3e4086d0a3c
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"
14 namespace chrome {
15 namespace android {
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),
23 write_index_(0),
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,
38 jobject jobj,
39 jobject jcontent_tree) {
40 SceneLayer* content_tree = FromJavaObject(env, jcontent_tree);
41 if (content_tree_ &&
42 (!content_tree_->layer()->parent() ||
43 content_tree_->layer()->parent()->id() != layer()->id()))
44 content_tree_ = nullptr;
46 if (content_tree != content_tree_) {
47 if (content_tree_)
48 content_tree_->layer()->RemoveFromParent();
49 content_tree_ = content_tree;
50 if (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) {
59 write_index_ = 0;
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,
71 jobject jobj,
72 jfloat width,
73 jfloat height,
74 jfloat y_offset,
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));
80 if (content_tree_)
81 content_tree_->layer()->SetPosition(gfx::PointF(0, -y_offset));
84 void TabStripSceneLayer::UpdateNewTabButton(JNIEnv* env,
85 jobject jobj,
86 jint resource_id,
87 jfloat x,
88 jfloat y,
89 jfloat width,
90 jfloat height,
91 jboolean visible,
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,
97 resource_id);
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,
108 jobject jobj,
109 jint resource_id,
110 jfloat x,
111 jfloat y,
112 jfloat width,
113 jfloat height,
114 jboolean incognito,
115 jboolean visible,
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,
121 resource_id);
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,
133 jobject jobj,
134 jint id,
135 jint close_resource_id,
136 jint handle_resource_id,
137 jboolean foreground,
138 jboolean close_pressed,
139 jfloat toolbar_width,
140 jfloat x,
141 jfloat y,
142 jfloat width,
143 jfloat height,
144 jfloat content_offset_x,
145 jfloat close_button_alpha,
146 jboolean is_loading,
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,
158 handle_resource_id);
159 ui::ResourceManager::Resource* close_button_resource =
160 resource_manager->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC,
161 close_resource_id);
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());
177 write_index_++;
178 return layer_tree;
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