Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / compositor / layer / tab_handle_layer.cc
blob965e0547ff0ea0dea4b8da55c9ebfb16e716f493
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/layer/tab_handle_layer.h"
7 #include "base/i18n/rtl.h"
8 #include "cc/layers/layer.h"
9 #include "cc/layers/solid_color_layer.h"
10 #include "chrome/browser/android/compositor/decoration_title.h"
11 #include "chrome/browser/android/compositor/layer_title_cache.h"
12 #include "content/public/browser/android/compositor.h"
13 #include "ui/android/resources/resource_manager.h"
14 #include "ui/android/resources/ui_resource_android.h"
15 #include "ui/base/l10n/l10n_util_android.h"
17 namespace chrome {
18 namespace android {
20 // static
21 scoped_refptr<TabHandleLayer> TabHandleLayer::Create(
22 LayerTitleCache* layer_title_cache) {
23 return make_scoped_refptr(new TabHandleLayer(layer_title_cache));
26 void TabHandleLayer::SetProperties(
27 int id,
28 ui::ResourceManager::Resource* close_button_resource,
29 ui::ResourceManager::Resource* tab_handle_resource,
30 bool foreground,
31 bool close_pressed,
32 float toolbar_width,
33 float x,
34 float y,
35 float width,
36 float height,
37 float content_offset_x,
38 float close_button_alpha,
39 bool is_loading,
40 float spinner_rotation,
41 float brightness,
42 float border_opacity) {
43 if (brightness != brightness_ || foreground != foreground_) {
44 brightness_ = brightness;
45 foreground_ = foreground;
46 cc::FilterOperations filters;
47 if (brightness_ != 1.0f && !foreground_)
48 filters.Append(cc::FilterOperation::CreateBrightnessFilter(brightness_));
49 layer_->SetFilters(filters);
52 float original_x = x;
53 float original_y = y;
54 if (foreground_) {
55 if (!border_->parent()) {
56 layer_->InsertChild(border_, 0);
57 border_->SetIsDrawable(true);
59 border_->SetBackgroundColor(SK_ColorBLACK);
60 border_->SetPosition(gfx::PointF(0, height - 1));
61 border_->SetBounds(gfx::Size(toolbar_width, 1));
62 border_->SetOpacity(border_opacity);
64 x = 0;
65 y = 0;
66 } else if (border_->parent()) {
67 border_->RemoveFromParent();
70 bool is_rtl = l10n_util::IsLayoutRtl();
72 float margin_width =
73 tab_handle_resource->size.width() - tab_handle_resource->aperture.width();
74 float margin_height = tab_handle_resource->size.height() -
75 tab_handle_resource->aperture.height();
77 // In the inset case, the |decoration_tab_| nine-patch layer should not have a
78 // margin that is greater than the content size of the layer. This case can
79 // happen at initialization. The sizes used below are just temp values until
80 // the real content sizes arrive.
81 if (margin_width >= width) {
82 // Shift the left edge over by the adjusted amount for more
83 // aesthetic animations.
84 x = x - (margin_width - width);
85 width = margin_width;
87 if (margin_height >= height) {
88 y = y - (margin_height - height);
89 height = margin_height;
91 gfx::Size tab_bounds(width, height);
93 layer_->SetPosition(gfx::Point(x, y));
94 DecorationTitle* title_layer = nullptr;
95 if (layer_title_cache_)
96 title_layer = layer_title_cache_->GetTitleLayer(id);
98 if (title_layer) {
99 title_layer->setOpacity(1.0f);
100 unsigned expected_children = 3;
101 if (foreground_)
102 expected_children += 1;
103 title_layer_ = title_layer->layer();
104 if (layer_->children().size() < expected_children) {
105 layer_->AddChild(title_layer_);
106 } else if (layer_->children()[expected_children - 1]->id() !=
107 title_layer_->id()) {
108 layer_->ReplaceChild((layer_->children()[expected_children - 1]).get(),
109 title_layer_);
111 title_layer->SetUIResourceIds();
112 } else if (title_layer_.get()) {
113 title_layer_->RemoveFromParent();
114 title_layer_ = nullptr;
117 decoration_tab_->SetUIResourceId(tab_handle_resource->ui_resource->id());
118 decoration_tab_->SetAperture(tab_handle_resource->aperture);
119 decoration_tab_->SetFillCenter(true);
120 decoration_tab_->SetBounds(tab_bounds);
121 decoration_tab_->SetBorder(
122 tab_handle_resource->Border(decoration_tab_->bounds()));
124 if (foreground_)
125 decoration_tab_->SetPosition(gfx::PointF(original_x, original_y));
126 else
127 decoration_tab_->SetPosition(gfx::PointF(0, 0));
129 close_button_->SetUIResourceId(close_button_resource->ui_resource->id());
130 close_button_->SetBounds(close_button_resource->size);
131 const float padding_right =
132 tab_handle_resource->size.width() - tab_handle_resource->padding.right();
133 const float padding_left = tab_handle_resource->padding.x();
134 const float close_width = close_button_->bounds().width();
135 if (title_layer) {
136 int title_y = tab_handle_resource->padding.y() / 2 + height / 2 -
137 title_layer->size().height() / 2;
138 int title_x = is_rtl ? padding_left + close_width : padding_left;
139 title_x += is_rtl ? 0 : content_offset_x;
140 title_layer->setBounds(gfx::Size(
141 width - padding_right - padding_left - close_width - content_offset_x,
142 height));
143 if (foreground_) {
144 title_x += original_x;
145 title_y += original_y;
147 title_layer->layer()->SetPosition(gfx::Point(title_x, title_y));
148 if (is_loading) {
149 title_layer->SetIsLoading(true);
150 title_layer->SetSpinnerRotation(spinner_rotation);
151 } else {
152 title_layer->SetIsLoading(false);
156 if (close_button_alpha == 0.f) {
157 close_button_->SetIsDrawable(false);
158 } else {
159 close_button_->SetIsDrawable(true);
160 const float close_max_width = close_button_->bounds().width();
161 int close_y = (tab_handle_resource->padding.y() + height) / 2 -
162 close_button_->bounds().height() / 2;
163 int close_x = is_rtl ? padding_left - close_max_width + close_width
164 : width - padding_right - close_width;
165 if (foreground_) {
166 close_y += original_y;
167 close_x += original_x;
170 close_button_->SetPosition(gfx::PointF(close_x, close_y));
171 close_button_->SetOpacity(close_button_alpha);
175 scoped_refptr<cc::Layer> TabHandleLayer::layer() {
176 return layer_;
179 TabHandleLayer::TabHandleLayer(LayerTitleCache* layer_title_cache)
180 : layer_title_cache_(layer_title_cache),
181 layer_(cc::Layer::Create(content::Compositor::LayerSettings())),
182 close_button_(
183 cc::UIResourceLayer::Create(content::Compositor::LayerSettings())),
184 decoration_tab_(
185 cc::NinePatchLayer::Create(content::Compositor::LayerSettings())),
186 border_(
187 cc::SolidColorLayer::Create(content::Compositor::LayerSettings())),
188 brightness_(1.0f),
189 foreground_(false) {
190 decoration_tab_->SetIsDrawable(true);
191 layer_->AddChild(decoration_tab_);
192 layer_->AddChild(close_button_);
195 TabHandleLayer::~TabHandleLayer() {
198 } // namespace android
199 } // namespace chrome