File: decoration_title.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (171 lines) | stat: -rw-r--r-- 6,093 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
// 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/decoration_title.h"

#include <android/bitmap.h>

// #include "base/i18n/rtl.h"
#include "cc/slim/layer.h"
#include "cc/slim/ui_resource_layer.h"
#include "components/viz/common/features.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/base/l10n/l10n_util_android.h"

namespace android {

DecorationTitle::DecorationTitle(ui::ResourceManager* resource_manager,
                                 int title_resource_id,
                                 int fade_width,
                                 bool is_incognito,
                                 bool is_rtl)
    : layer_(cc::slim::Layer::Create()),
      layer_opaque_(cc::slim::UIResourceLayer::Create()),
      layer_fade_(cc::slim::UIResourceLayer::Create()),
      title_resource_id_(title_resource_id),
      fade_width_(fade_width),
      is_incognito_(is_incognito),
      is_rtl_(is_rtl),
      resource_manager_(resource_manager) {
  layer_->AddChild(layer_opaque_);
  layer_->AddChild(layer_fade_);
}

DecorationTitle::~DecorationTitle() {
  layer_->RemoveFromParent();
}

void DecorationTitle::SetResourceManager(
    ui::ResourceManager* resource_manager) {
  resource_manager_ = resource_manager;
}

void DecorationTitle::Update(int title_resource_id,
                             int fade_width,
                             bool is_incognito,
                             bool is_rtl) {
  title_resource_id_ = title_resource_id;
  is_incognito_ = is_incognito;
  is_rtl_ = is_rtl;
  fade_width_ = fade_width;
}

void DecorationTitle::SetUIResourceIds() {
  ui::Resource* title_resource = resource_manager_->GetResource(
      ui::ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP, title_resource_id_);
  if (title_resource) {
    layer_opaque_->SetUIResourceId(title_resource->ui_resource()->id());
    layer_fade_->SetUIResourceId(title_resource->ui_resource()->id());
    title_size_ = title_resource->size();
  }
  size_ = calculateSize(0);
}

gfx::Size DecorationTitle::calculateSize(int favicon_width) {
  return gfx::Size(title_size_.width() + favicon_width, title_size_.height());
}

void DecorationTitle::setOpacity(float opacity) {
  layer_opaque_->SetOpacity(opacity);
  layer_fade_->SetOpacity(opacity);
}

void DecorationTitle::setBounds(const gfx::Size& bounds) {
  setBounds(bounds, 0);
}

void DecorationTitle::setBounds(const gfx::Size& bounds, int start_space) {
  layer_->SetBounds(gfx::Size(bounds.width(), size_.height()));

  if (bounds.GetArea() == 0.f) {
    layer_->SetHideLayerAndSubtree(true);
    return;
  }
  layer_->SetHideLayerAndSubtree(false);

  // Current implementation assumes there is always enough space
  // to draw favicon and title fade.

  // Note that favicon positon and title aligning depends on the system locale,
  // l10n_util::IsLayoutRtl(), while title starting and fade out direction
  // depends on the title text locale (DecorationTitle::is_rtl_).

  bool sys_rtl = l10n_util::IsLayoutRtl();
  int title_space = std::max(0, bounds.width() - start_space - fade_width_);
  int fade_space = std::max(0, bounds.width() - start_space - title_space);

  if (title_size_.width() <= title_space + fade_space)
    title_space += fade_space;

  if (title_size_.width() <= title_space)
    fade_space = 0.f;

  float title_offset_y = (size_.height() - title_size_.height()) / 2.f;

  //  Place the opaque title component.
  if (title_space > 0.f) {
    // Calculate the title position and size, taking into account both
    // system and title RTL.
    int width = std::min(title_space, title_size_.width());
    int x_offset = sys_rtl ? title_space - width : start_space;
    int x = x_offset + (is_rtl_ ? fade_space : 0);

    // Calculate the UV coordinates taking into account title RTL.
    float width_percentage = (float)width / title_size_.width();
    float u1 = is_rtl_ ? 1.f - width_percentage : 0.f;
    float u2 = is_rtl_ ? 1.f : width_percentage;

    layer_opaque_->SetIsDrawable(true);
    layer_opaque_->SetBounds(gfx::Size(width, title_size_.height()));
    layer_opaque_->SetPosition(gfx::PointF(x, title_offset_y));
    layer_opaque_->SetUV(gfx::PointF(u1, 0.f), gfx::PointF(u2, 1.f));
  } else {
    layer_opaque_->SetIsDrawable(false);
  }

  // Place the fade title component.
  if (fade_space > 0.f) {
    // Calculate the title position and size, taking into account both
    // system and title RTL.
    int x_offset = sys_rtl ? 0 : start_space;
    int x = x_offset + (is_rtl_ ? 0 : title_space);
    float title_amt = (float)title_space / title_size_.width();
    float fade_amt = (float)fade_space / title_size_.width();

    // Calculate UV coordinates taking into account title RTL.
    float u1 = is_rtl_ ? 1.f - title_amt - fade_amt : title_amt;
    float u2 = is_rtl_ ? 1.f - title_amt : title_amt + fade_amt;

    // Calculate vertex alpha taking into account title RTL.
    float max_alpha = (float)fade_space / fade_width_;
    float a1 = is_rtl_ ? 0.f : max_alpha;
    float a2 = is_rtl_ ? max_alpha : 0.f;

    layer_fade_->SetIsDrawable(true);
    layer_fade_->SetBounds(gfx::Size(fade_space, title_size_.height()));
    layer_fade_->SetPosition(gfx::PointF(x, title_offset_y));
    layer_fade_->SetUV(gfx::PointF(u1, 0.f), gfx::PointF(u2, 1.f));
    // Left to right gradient.
    gfx::LinearGradient gradient;
    gradient.AddStep(0.f, a1 * 255);
    gradient.AddStep(1.f, a2 * 255);
    gradient.set_angle(0);

    // TODO(b/361804880) With BCIV, offsets aren't applied correctly to layers
    // with clipped regions. Remove the fade effect from the title for now, so
    // we can still run the experiment for large screens.
    if (!base::FeatureList::IsEnabled(features::kAndroidBrowserControlsInViz)) {
      layer_fade_->SetGradientMask(gradient);
    }
  } else {
    layer_fade_->SetIsDrawable(false);
  }
}

scoped_refptr<cc::slim::Layer> DecorationTitle::layer() {
  DCHECK(layer_.get());
  return layer_;
}

}  // namespace android