File: toolbar_layer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (236 lines) | stat: -rw-r--r-- 10,269 bytes parent folder | download | duplicates (2)
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
// Copyright 2014 The Chromium Authors
// 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/toolbar_layer.h"

#include "base/feature_list.h"
#include "cc/resources/scoped_ui_resource.h"
#include "cc/slim/layer.h"
#include "cc/slim/nine_patch_layer.h"
#include "cc/slim/solid_color_layer.h"
#include "cc/slim/ui_resource_layer.h"
#include "chrome/browser/android/compositor/resources/toolbar_resource.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "components/viz/common/quads/offset_tag.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/android/resources/nine_patch_resource.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/gfx/geometry/rounded_corners_f.h"

namespace android {

// static
scoped_refptr<ToolbarLayer> ToolbarLayer::Create(
    ui::ResourceManager* resource_manager) {
  return base::WrapRefCounted(new ToolbarLayer(resource_manager));
}

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

void ToolbarLayer::PushResource(int toolbar_resource_id,
                                int toolbar_background_color,
                                bool anonymize,
                                int toolbar_textbox_background_color,
                                int url_bar_background_resource_id,
                                float x_offset,
                                float content_offset,
                                bool show_debug,
                                bool clip_shadow,
                                const viz::OffsetTag& offset_tag) {
  ToolbarResource* resource =
      ToolbarResource::From(resource_manager_->GetResource(
          ui::ANDROID_RESOURCE_TYPE_DYNAMIC, toolbar_resource_id));

  // Ensure the toolbar resource is available before making the layer visible.
  layer_->SetHideLayerAndSubtree(!resource);
  if (!resource)
    return;

  // This layer effectively draws over the space the resource takes for shadows.
  // Set the bounds to the non-shadow size so that other things can properly
  // line up.
  gfx::Size toolbar_bounds =
      gfx::Size(resource->size().width(),
                resource->size().height() - resource->shadow_height());
  layer_->SetBounds(toolbar_bounds);

  toolbar_background_layer_->SetBounds(resource->toolbar_rect().size());
  toolbar_background_layer_->SetPosition(
      gfx::PointF(resource->toolbar_rect().origin()));
  // TODO(crbug.com/40219248): Remove FromColor and make all SkColor4f.
  toolbar_background_layer_->SetBackgroundColor(
      SkColor4f::FromColor(toolbar_background_color));

  bool url_bar_visible = resource->location_bar_content_rect().width() != 0;
  url_bar_background_layer_->SetHideLayerAndSubtree(!url_bar_visible);
  if (url_bar_visible) {
    ui::NinePatchResource* url_bar_background_resource;
    url_bar_background_resource = ui::NinePatchResource::From(
        resource_manager_->GetStaticResourceWithTint(
            url_bar_background_resource_id, toolbar_textbox_background_color));

    gfx::Size draw_size(url_bar_background_resource->DrawSize(
        resource->location_bar_content_rect().size()));
    gfx::Rect border(url_bar_background_resource->Border(draw_size));
    gfx::PointF position(url_bar_background_resource->DrawPosition(
        resource->location_bar_content_rect().origin()));

    url_bar_background_layer_->SetBounds(draw_size);
    url_bar_background_layer_->SetPosition(position);
    url_bar_background_layer_->SetBorder(border);
    url_bar_background_layer_->SetAperture(
        url_bar_background_resource->aperture());
    url_bar_background_layer_->SetUIResourceId(
        url_bar_background_resource->ui_resource()->id());
  }

  bitmap_layer_->SetUIResourceId(resource->ui_resource()->id());
  bitmap_layer_->SetBounds(resource->size());

  layer_->SetMasksToBounds(clip_shadow);

  // The location bar background doubles as the anonymize layer -- it just
  // needs to be drawn on top of the toolbar bitmap.
  int background_layer_index = GetIndexOfLayer(toolbar_background_layer_);

  bool needs_move_to_front =
      anonymize && layer_->children().back() != url_bar_background_layer_;
  bool needs_move_to_back =
      !anonymize &&
      layer_->children()[background_layer_index] != url_bar_background_layer_;

  // If the layer needs to move, remove and re-add it.
  if (needs_move_to_front) {
    layer_->AddChild(url_bar_background_layer_);
  } else if (needs_move_to_back) {
    layer_->InsertChild(url_bar_background_layer_, background_layer_index + 1);
  }

  debug_layer_->SetBounds(resource->size());
  if (show_debug && !debug_layer_->parent())
    layer_->AddChild(debug_layer_);
  else if (!show_debug && debug_layer_->parent())
    debug_layer_->RemoveFromParent();

  // Position the toolbar at the bottom of the space available for top controls.
  layer_->SetPosition(
      gfx::PointF(x_offset, content_offset - layer_->bounds().height()));

  layer_->SetOffsetTag(offset_tag);
}

int ToolbarLayer::GetIndexOfLayer(scoped_refptr<cc::slim::Layer> layer) {
  for (unsigned int i = 0; i < layer_->children().size(); ++i) {
    if (layer_->children()[i] == layer)
      return i;
  }
  return -1;
}

void ToolbarLayer::UpdateProgressBar(int progress_bar_x,
                                     int progress_bar_y,
                                     int progress_bar_width,
                                     int progress_bar_height,
                                     int progress_bar_color,
                                     int progress_bar_background_x,
                                     int progress_bar_background_y,
                                     int progress_bar_background_width,
                                     int progress_bar_background_height,
                                     int progress_bar_background_color,
                                     int progress_bar_static_background_x,
                                     int progress_bar_static_background_width,
                                     int progress_bar_static_background_color,
                                     float corner_radius,
                                     bool progress_bar_visual_update_available) {
  bool is_progress_bar_visible = SkColorGetA(progress_bar_background_color);

  progress_bar_background_layer_->SetHideLayerAndSubtree(!is_progress_bar_visible);
  progress_bar_layer_->SetHideLayerAndSubtree(!is_progress_bar_visible);
  progress_bar_static_background_layer_->SetHideLayerAndSubtree(
      !(is_progress_bar_visible && progress_bar_visual_update_available));

  if (is_progress_bar_visible) {
    progress_bar_background_layer_->SetPosition(
        gfx::PointF(progress_bar_background_x, progress_bar_background_y));
    progress_bar_background_layer_->SetBounds(
        gfx::Size(progress_bar_background_width,
                  progress_bar_background_height));
    // TODO(crbug.com/40219248): Remove FromColor and make all SkColor4f.
    progress_bar_background_layer_->SetBackgroundColor(
        SkColor4f::FromColor(progress_bar_background_color));
    progress_bar_background_layer_->SetRoundedCorner(gfx::RoundedCornersF(corner_radius));

    progress_bar_layer_->SetPosition(
        gfx::PointF(progress_bar_x, progress_bar_y));
    progress_bar_layer_->SetBounds(
        gfx::Size(progress_bar_width, progress_bar_height));
    // TODO(crbug.com/40219248): Remove FromColor and make all SkColor4f.
    progress_bar_layer_->SetBackgroundColor(
        SkColor4f::FromColor(progress_bar_color));
    progress_bar_layer_->SetRoundedCorner(gfx::RoundedCornersF(corner_radius));

    if (progress_bar_visual_update_available) {
      progress_bar_static_background_layer_->SetPosition(gfx::PointF(
          progress_bar_static_background_x, progress_bar_y));
      progress_bar_static_background_layer_->SetBounds(gfx::Size(
          progress_bar_static_background_width, progress_bar_height));
      progress_bar_static_background_layer_->SetBackgroundColor(
          SkColor4f::FromColor(progress_bar_static_background_color));
      progress_bar_static_background_layer_->SetRoundedCorner(gfx::RoundedCornersF(corner_radius));
    }
  }
}

void ToolbarLayer::SetOpacity(float opacity) {
  toolbar_background_layer_->SetOpacity(opacity);
  url_bar_background_layer_->SetOpacity(opacity);
  bitmap_layer_->SetOpacity(opacity);

  progress_bar_layer_->SetOpacity(opacity);
  progress_bar_background_layer_->SetOpacity(opacity);
  progress_bar_static_background_layer_->SetOpacity(opacity);
}

ToolbarLayer::ToolbarLayer(ui::ResourceManager* resource_manager)
    : resource_manager_(resource_manager),
      layer_(cc::slim::Layer::Create()),
      toolbar_background_layer_(cc::slim::SolidColorLayer::Create()),
      url_bar_background_layer_(cc::slim::NinePatchLayer::Create()),
      bitmap_layer_(cc::slim::UIResourceLayer::Create()),
      progress_bar_layer_(cc::slim::SolidColorLayer::Create()),
      progress_bar_background_layer_(cc::slim::SolidColorLayer::Create()),
      progress_bar_static_background_layer_(cc::slim::SolidColorLayer::Create()),
      debug_layer_(cc::slim::SolidColorLayer::Create()) {
  toolbar_background_layer_->SetIsDrawable(true);
  layer_->AddChild(toolbar_background_layer_);

  url_bar_background_layer_->SetIsDrawable(true);
  url_bar_background_layer_->SetFillCenter(true);
  layer_->AddChild(url_bar_background_layer_);

  bitmap_layer_->SetIsDrawable(true);
  layer_->AddChild(bitmap_layer_);

  progress_bar_static_background_layer_->SetIsDrawable(true);
  progress_bar_static_background_layer_->SetHideLayerAndSubtree(true);
  layer_->AddChild(progress_bar_static_background_layer_);

  progress_bar_background_layer_->SetIsDrawable(true);
  progress_bar_background_layer_->SetHideLayerAndSubtree(true);
  layer_->AddChild(progress_bar_background_layer_);

  progress_bar_layer_->SetIsDrawable(true);
  progress_bar_layer_->SetHideLayerAndSubtree(true);
  layer_->AddChild(progress_bar_layer_);

  debug_layer_->SetIsDrawable(true);
  debug_layer_->SetBackgroundColor(SkColors::kGreen);
  debug_layer_->SetOpacity(0.5f);
}

ToolbarLayer::~ToolbarLayer() = default;

}  //  namespace android