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.
5 #include "chrome/browser/android/compositor/layer/contextual_search_layer.h"
7 #include "cc/layers/layer.h"
8 #include "cc/layers/nine_patch_layer.h"
9 #include "cc/layers/solid_color_layer.h"
10 #include "cc/layers/ui_resource_layer.h"
11 #include "content/public/browser/android/content_view_core.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/android/resources/resource_manager.h"
14 #include "ui/android/resources/ui_resource_android.h"
18 const SkColor kContextualSearchBarBorderColor
= SkColorSetRGB(0xf1, 0xf1, 0xf1);
26 scoped_refptr
<ContextualSearchLayer
> ContextualSearchLayer::Create(
27 ui::ResourceManager
* resource_manager
) {
28 return make_scoped_refptr(new ContextualSearchLayer(resource_manager
));
31 void ContextualSearchLayer::SetProperties(
32 int search_bar_background_resource_id
,
33 int search_bar_text_resource_id
,
34 int search_provider_icon_resource_id
,
35 int search_icon_resource_id
,
36 int progress_bar_background_resource_id
,
37 int progress_bar_resource_id
,
38 content::ContentViewCore
* content_view_core
,
40 float search_panel_width
,
41 float search_bar_margin_top
,
42 float search_bar_height
,
43 float search_bar_text_opacity
,
44 bool search_bar_border_visible
,
45 float search_bar_border_y
,
46 float search_bar_border_height
,
47 float search_provider_icon_opacity
,
48 float search_icon_padding_left
,
49 float search_icon_opacity
,
50 bool progress_bar_visible
,
52 float progress_bar_height
,
53 float progress_bar_opacity
,
54 int progress_bar_completion
) {
55 // Grab the dynamic Search Bar Text resource.
56 ui::ResourceManager::Resource
* search_bar_text_resource
=
57 resource_manager_
->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC
,
58 search_bar_text_resource_id
);
60 // Grab required static resources.
61 ui::ResourceManager::Resource
* search_bar_background_resource
=
62 resource_manager_
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
63 search_bar_background_resource_id
);
64 ui::ResourceManager::Resource
* search_provider_icon_resource
=
65 resource_manager_
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
66 search_provider_icon_resource_id
);
67 ui::ResourceManager::Resource
* search_icon_resource
=
68 resource_manager_
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
69 search_icon_resource_id
);
71 DCHECK(search_bar_background_resource
);
72 DCHECK(search_provider_icon_resource
);
73 DCHECK(search_icon_resource
);
75 // Round values to avoid pixel gap between layers.
76 search_bar_height
= floor(search_bar_height
);
77 search_bar_margin_top
= floor(search_bar_margin_top
);
79 // ---------------------------------------------------------------------------
80 // Search Bar Background
81 // ---------------------------------------------------------------------------
82 gfx::Size
background_size(search_panel_width
, search_bar_height
);
83 search_bar_background_
->SetUIResourceId(
84 search_bar_background_resource
->ui_resource
->id());
85 search_bar_background_
->SetBorder(
86 search_bar_background_resource
->Border(background_size
));
87 search_bar_background_
->SetAperture(search_bar_background_resource
->aperture
);
88 search_bar_background_
->SetBounds(background_size
);
90 // ---------------------------------------------------------------------------
92 // ---------------------------------------------------------------------------
93 if (search_bar_text_resource
) {
94 // Centralizes the text vertically in the Search Bar.
95 float search_bar_padding_top
=
96 search_bar_margin_top
+
97 (search_bar_height
- search_bar_margin_top
) / 2 -
98 search_bar_text_resource
->size
.height() / 2;
99 search_bar_text_
->SetUIResourceId(
100 search_bar_text_resource
->ui_resource
->id());
101 search_bar_text_
->SetBounds(search_bar_text_resource
->size
);
102 search_bar_text_
->SetPosition(gfx::PointF(0.f
, search_bar_padding_top
));
103 search_bar_text_
->SetOpacity(search_bar_text_opacity
);
106 // ---------------------------------------------------------------------------
107 // Search Provider Icon
108 // ---------------------------------------------------------------------------
109 // Centralizes the Search Provider Icon horizontally in the Search Bar.
110 float search_provider_icon_left
=
111 search_panel_width
/ 2.f
-
112 search_provider_icon_resource
->size
.width() / 2.f
;
113 search_provider_icon_
->SetUIResourceId(
114 search_provider_icon_resource
->ui_resource
->id());
115 search_provider_icon_
->SetBounds(search_provider_icon_resource
->size
);
116 search_provider_icon_
->SetPosition(
117 gfx::PointF(search_provider_icon_left
, 0.f
));
118 search_provider_icon_
->SetOpacity(search_provider_icon_opacity
);
120 // ---------------------------------------------------------------------------
122 // ---------------------------------------------------------------------------
123 // Centralizes the Search Icon vertically in the Search Bar.
124 float search_icon_padding_top
=
125 search_bar_margin_top
+ (search_bar_height
- search_bar_margin_top
) / 2 -
126 search_icon_resource
->size
.height() / 2;
127 search_icon_
->SetUIResourceId(search_icon_resource
->ui_resource
->id());
128 search_icon_
->SetBounds(search_icon_resource
->size
);
129 search_icon_
->SetPosition(
130 gfx::PointF(search_icon_padding_left
, search_icon_padding_top
));
131 search_icon_
->SetOpacity(search_icon_opacity
);
133 // ---------------------------------------------------------------------------
134 // Search Content View
135 // ---------------------------------------------------------------------------
136 content_view_container_
->SetPosition(gfx::PointF(0.f
, search_bar_height
));
137 if (content_view_core
&& content_view_core
->GetLayer().get()) {
138 scoped_refptr
<cc::Layer
> content_view_layer
= content_view_core
->GetLayer();
139 if (content_view_layer
->parent() != content_view_container_
)
140 content_view_container_
->AddChild(content_view_layer
);
142 content_view_container_
->RemoveAllChildren();
145 // ---------------------------------------------------------------------------
147 // ---------------------------------------------------------------------------
148 layer_
->SetPosition(gfx::PointF(0.f
, search_panel_y
));
150 // ---------------------------------------------------------------------------
152 // ---------------------------------------------------------------------------
153 bool should_render_progress_bar
=
154 progress_bar_visible
&& progress_bar_opacity
> 0.f
;
155 if (should_render_progress_bar
) {
156 // Load Progress Bar resources.
157 ui::ResourceManager::Resource
* progress_bar_background_resource
=
158 resource_manager_
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
159 progress_bar_background_resource_id
);
160 ui::ResourceManager::Resource
* progress_bar_resource
=
161 resource_manager_
->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC
,
162 progress_bar_resource_id
);
164 DCHECK(progress_bar_background_resource
);
165 DCHECK(progress_bar_resource
);
167 // Progress Bar Background
168 if (progress_bar_background_
->parent() != layer_
)
169 layer_
->AddChild(progress_bar_background_
);
171 gfx::Size
progress_bar_background_size(search_panel_width
,
172 progress_bar_height
);
173 progress_bar_background_
->SetUIResourceId(
174 progress_bar_background_resource
->ui_resource
->id());
175 progress_bar_background_
->SetBorder(
176 progress_bar_background_resource
->Border(progress_bar_background_size
));
177 progress_bar_background_
->SetAperture(
178 progress_bar_background_resource
->aperture
);
179 progress_bar_background_
->SetBounds(progress_bar_background_size
);
180 progress_bar_background_
->SetPosition(gfx::PointF(0.f
, progress_bar_y
));
181 progress_bar_background_
->SetOpacity(progress_bar_opacity
);
184 if (progress_bar_
->parent() != layer_
)
185 layer_
->AddChild(progress_bar_
);
187 float progress_bar_width
=
188 floor(search_panel_width
* progress_bar_completion
/ 100.f
);
189 gfx::Size
progress_bar_size(progress_bar_width
, progress_bar_height
);
190 progress_bar_
->SetUIResourceId(progress_bar_resource
->ui_resource
->id());
191 progress_bar_
->SetBorder(progress_bar_resource
->Border(progress_bar_size
));
192 progress_bar_
->SetAperture(progress_bar_resource
->aperture
);
193 progress_bar_
->SetBounds(progress_bar_size
);
194 progress_bar_
->SetPosition(gfx::PointF(0.f
, progress_bar_y
));
195 progress_bar_
->SetOpacity(progress_bar_opacity
);
197 // Removes Progress Bar and its Background from the Layer Tree.
198 if (progress_bar_background_
.get() && progress_bar_background_
->parent())
199 progress_bar_background_
->RemoveFromParent();
201 if (progress_bar_
.get() && progress_bar_
->parent())
202 progress_bar_
->RemoveFromParent();
205 // ---------------------------------------------------------------------------
206 // Search Bar border.
207 // ---------------------------------------------------------------------------
208 if (!should_render_progress_bar
&& search_bar_border_visible
) {
209 gfx::Size
search_bar_border_size(search_panel_width
,
210 search_bar_border_height
);
211 search_bar_border_
->SetBounds(search_bar_border_size
);
212 search_bar_border_
->SetPosition(gfx::PointF(0.f
, search_bar_border_y
));
213 layer_
->AddChild(search_bar_border_
);
214 } else if (search_bar_border_
.get() && search_bar_border_
->parent()) {
215 search_bar_border_
->RemoveFromParent();
219 ContextualSearchLayer::ContextualSearchLayer(
220 ui::ResourceManager
* resource_manager
)
221 : resource_manager_(resource_manager
),
222 layer_(cc::Layer::Create()),
223 search_bar_background_(cc::NinePatchLayer::Create()),
224 search_bar_text_(cc::UIResourceLayer::Create()),
225 search_provider_icon_(cc::UIResourceLayer::Create()),
226 search_icon_(cc::UIResourceLayer::Create()),
227 content_view_container_(cc::Layer::Create()),
228 search_bar_border_(cc::SolidColorLayer::Create()),
229 progress_bar_(cc::NinePatchLayer::Create()),
230 progress_bar_background_(cc::NinePatchLayer::Create()) {
231 layer_
->SetMasksToBounds(false);
232 layer_
->SetIsDrawable(true);
234 // Search Bar Background
235 search_bar_background_
->SetIsDrawable(true);
236 search_bar_background_
->SetFillCenter(true);
237 layer_
->AddChild(search_bar_background_
);
240 search_bar_text_
->SetIsDrawable(true);
241 layer_
->AddChild(search_bar_text_
);
243 // Search Provider Icon
244 search_provider_icon_
->SetIsDrawable(true);
245 layer_
->AddChild(search_provider_icon_
);
248 search_icon_
->SetIsDrawable(true);
249 layer_
->AddChild(search_icon_
);
252 search_bar_border_
->SetIsDrawable(true);
253 search_bar_border_
->SetBackgroundColor(kContextualSearchBarBorderColor
);
255 // Progress Bar Background
256 progress_bar_background_
->SetIsDrawable(true);
257 progress_bar_background_
->SetFillCenter(true);
260 progress_bar_
->SetIsDrawable(true);
261 progress_bar_
->SetFillCenter(true);
263 // Search Content View
264 layer_
->AddChild(content_view_container_
);
267 ContextualSearchLayer::~ContextualSearchLayer() {
270 scoped_refptr
<cc::Layer
> ContextualSearchLayer::layer() {
274 } // namespace android
275 } // namespace chrome