File: contextual_search_layer.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (271 lines) | stat: -rw-r--r-- 11,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/compositor/layer/contextual_search_layer.h"

#include "cc/layers/layer.h"
#include "cc/layers/nine_patch_layer.h"
#include "cc/layers/solid_color_layer.h"
#include "cc/layers/ui_resource_layer.h"
#include "content/public/browser/android/content_view_core.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/android/resources/ui_resource_android.h"

namespace {

const SkColor kContextualSearchBarBorderColor = SkColorSetRGB(0xf1, 0xf1, 0xf1);

}  // namespace

namespace chrome {
namespace android {

// static
scoped_refptr<ContextualSearchLayer> ContextualSearchLayer::Create(
    ui::ResourceManager* resource_manager) {
  return make_scoped_refptr(new ContextualSearchLayer(resource_manager));
}

void ContextualSearchLayer::SetProperties(
    int search_bar_background_resource_id,
    int search_bar_text_resource_id,
    int search_provider_icon_resource_id,
    int search_icon_resource_id,
    int progress_bar_background_resource_id,
    int progress_bar_resource_id,
    content::ContentViewCore* content_view_core,
    float search_panel_y,
    float search_panel_width,
    float search_bar_margin_top,
    float search_bar_height,
    float search_bar_text_opacity,
    bool search_bar_border_visible,
    float search_bar_border_y,
    float search_bar_border_height,
    float search_provider_icon_opacity,
    float search_icon_padding_left,
    float search_icon_opacity,
    bool progress_bar_visible,
    float progress_bar_y,
    float progress_bar_height,
    float progress_bar_opacity,
    int progress_bar_completion) {
  // Grab the dynamic Search Bar Text resource.
  ui::ResourceManager::Resource* search_bar_text_resource =
      resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_DYNAMIC,
                                     search_bar_text_resource_id);

  // Grab required static resources.
  ui::ResourceManager::Resource* search_bar_background_resource =
      resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC,
                                     search_bar_background_resource_id);
  ui::ResourceManager::Resource* search_provider_icon_resource =
      resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC,
                                     search_provider_icon_resource_id);
  ui::ResourceManager::Resource* search_icon_resource =
      resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC,
                                     search_icon_resource_id);

  DCHECK(search_bar_background_resource);
  DCHECK(search_provider_icon_resource);
  DCHECK(search_icon_resource);

  // ---------------------------------------------------------------------------
  // Search Bar Background
  // ---------------------------------------------------------------------------
  gfx::Size background_size(search_panel_width, search_bar_height);
  search_bar_background_->SetUIResourceId(
      search_bar_background_resource->ui_resource->id());
  search_bar_background_->SetBorder(
      search_bar_background_resource->Border(background_size));
  search_bar_background_->SetAperture(search_bar_background_resource->aperture);
  search_bar_background_->SetBounds(background_size);

  // ---------------------------------------------------------------------------
  // Search Bar Text
  // ---------------------------------------------------------------------------
  if (search_bar_text_resource) {
    // Centralizes the text vertically in the Search Bar.
    float search_bar_padding_top =
        search_bar_margin_top +
        (search_bar_height - search_bar_margin_top) / 2 -
        search_bar_text_resource->size.height() / 2;
    search_bar_text_->SetUIResourceId(
        search_bar_text_resource->ui_resource->id());
    search_bar_text_->SetBounds(search_bar_text_resource->size);
    search_bar_text_->SetPosition(gfx::PointF(0.f, search_bar_padding_top));
    search_bar_text_->SetOpacity(search_bar_text_opacity);
  }

  // ---------------------------------------------------------------------------
  // Search Provider Icon
  // ---------------------------------------------------------------------------
  // Centralizes the Search Provider Icon horizontally in the Search Bar.
  float search_provider_icon_left =
      search_panel_width / 2.f -
      search_provider_icon_resource->size.width() / 2.f;
  search_provider_icon_->SetUIResourceId(
      search_provider_icon_resource->ui_resource->id());
  search_provider_icon_->SetBounds(search_provider_icon_resource->size);
  search_provider_icon_->SetPosition(
      gfx::PointF(search_provider_icon_left, 0.f));
  search_provider_icon_->SetOpacity(search_provider_icon_opacity);

  // ---------------------------------------------------------------------------
  // Search Icon
  // ---------------------------------------------------------------------------
  // Centralizes the Search Icon vertically in the Search Bar.
  float search_icon_padding_top =
      search_bar_margin_top + (search_bar_height - search_bar_margin_top) / 2 -
      search_icon_resource->size.height() / 2;
  search_icon_->SetUIResourceId(search_icon_resource->ui_resource->id());
  search_icon_->SetBounds(search_icon_resource->size);
  search_icon_->SetPosition(
      gfx::PointF(search_icon_padding_left, search_icon_padding_top));
  search_icon_->SetOpacity(search_icon_opacity);

  // ---------------------------------------------------------------------------
  // Search Content View
  // ---------------------------------------------------------------------------
  content_view_container_->SetPosition(gfx::PointF(0.f, search_bar_height));
  if (content_view_core && content_view_core->GetLayer().get()) {
    scoped_refptr<cc::Layer> content_view_layer = content_view_core->GetLayer();
    if (content_view_layer->parent() != content_view_container_)
      content_view_container_->AddChild(content_view_layer);
  } else {
    content_view_container_->RemoveAllChildren();
  }

  // ---------------------------------------------------------------------------
  // Search Panel.
  // ---------------------------------------------------------------------------
  layer_->SetPosition(gfx::PointF(0.f, search_panel_y));

  // ---------------------------------------------------------------------------
  // Progress Bar
  // ---------------------------------------------------------------------------
  bool should_render_progress_bar =
      progress_bar_visible && progress_bar_opacity > 0.f;
  if (should_render_progress_bar) {
    // Load Progress Bar resources.
    ui::ResourceManager::Resource* progress_bar_background_resource =
        resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC,
                                       progress_bar_background_resource_id);
    ui::ResourceManager::Resource* progress_bar_resource =
        resource_manager_->GetResource(ui::ANDROID_RESOURCE_TYPE_STATIC,
                                       progress_bar_resource_id);

    DCHECK(progress_bar_background_resource);
    DCHECK(progress_bar_resource);

    // Progress Bar Background
    if (progress_bar_background_->parent() != layer_)
      layer_->AddChild(progress_bar_background_);

    gfx::Size progress_bar_background_size(search_panel_width,
                                           progress_bar_height);
    progress_bar_background_->SetUIResourceId(
        progress_bar_background_resource->ui_resource->id());
    progress_bar_background_->SetBorder(
        progress_bar_background_resource->Border(progress_bar_background_size));
    progress_bar_background_->SetAperture(
        progress_bar_background_resource->aperture);
    progress_bar_background_->SetBounds(progress_bar_background_size);
    progress_bar_background_->SetPosition(gfx::PointF(0.f, progress_bar_y));
    progress_bar_background_->SetOpacity(progress_bar_opacity);

    // Progress Bar
    if (progress_bar_->parent() != layer_)
      layer_->AddChild(progress_bar_);

    float progress_bar_width =
        floor(search_panel_width * progress_bar_completion / 100.f);
    gfx::Size progress_bar_size(progress_bar_width, progress_bar_height);
    progress_bar_->SetUIResourceId(progress_bar_resource->ui_resource->id());
    progress_bar_->SetBorder(progress_bar_resource->Border(progress_bar_size));
    progress_bar_->SetAperture(progress_bar_resource->aperture);
    progress_bar_->SetBounds(progress_bar_size);
    progress_bar_->SetPosition(gfx::PointF(0.f, progress_bar_y));
    progress_bar_->SetOpacity(progress_bar_opacity);
  } else {
    // Removes Progress Bar and its Background from the Layer Tree.
    if (progress_bar_background_.get() && progress_bar_background_->parent())
      progress_bar_background_->RemoveFromParent();

    if (progress_bar_.get() && progress_bar_->parent())
      progress_bar_->RemoveFromParent();
  }

  // ---------------------------------------------------------------------------
  // Search Bar border.
  // ---------------------------------------------------------------------------
  if (!should_render_progress_bar && search_bar_border_visible) {
    gfx::Size search_bar_border_size(search_panel_width,
                                     search_bar_border_height);
    search_bar_border_->SetBounds(search_bar_border_size);
    search_bar_border_->SetPosition(gfx::PointF(0.f, search_bar_border_y));
    layer_->AddChild(search_bar_border_);
  } else if (search_bar_border_.get() && search_bar_border_->parent()) {
    search_bar_border_->RemoveFromParent();
  }
}

ContextualSearchLayer::ContextualSearchLayer(
    ui::ResourceManager* resource_manager)
    : resource_manager_(resource_manager),
      layer_(cc::Layer::Create()),
      search_bar_background_(cc::NinePatchLayer::Create()),
      search_bar_text_(cc::UIResourceLayer::Create()),
      search_provider_icon_(cc::UIResourceLayer::Create()),
      search_icon_(cc::UIResourceLayer::Create()),
      content_view_container_(cc::Layer::Create()),
      search_bar_border_(cc::SolidColorLayer::Create()),
      progress_bar_(cc::NinePatchLayer::Create()),
      progress_bar_background_(cc::NinePatchLayer::Create()) {
  layer_->SetMasksToBounds(false);
  layer_->SetIsDrawable(true);

  // Search Bar Background
  search_bar_background_->SetIsDrawable(true);
  search_bar_background_->SetFillCenter(true);
  layer_->AddChild(search_bar_background_);

  // Search Bar Text
  search_bar_text_->SetIsDrawable(true);
  layer_->AddChild(search_bar_text_);

  // Search Provider Icon
  search_provider_icon_->SetIsDrawable(true);
  layer_->AddChild(search_provider_icon_);

  // Search Icon
  search_icon_->SetIsDrawable(true);
  layer_->AddChild(search_icon_);

  // Search Bar Border
  search_bar_border_->SetIsDrawable(true);
  search_bar_border_->SetBackgroundColor(kContextualSearchBarBorderColor);

  // Progress Bar Background
  progress_bar_background_->SetIsDrawable(true);
  progress_bar_background_->SetFillCenter(true);

  // Progress Bar
  progress_bar_->SetIsDrawable(true);
  progress_bar_->SetFillCenter(true);

  // Search Content View
  layer_->AddChild(content_view_container_);
}

ContextualSearchLayer::~ContextualSearchLayer() {
}

scoped_refptr<cc::Layer> ContextualSearchLayer::layer() {
  return layer_;
}

}  //  namespace android
}  //  namespace chrome