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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/unified/feature_pod_button.h"
#include <string_view>
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_provider.h"
#include "ash/style/color_util.h"
#include "ash/style/style_util.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/feature_pod_controller_base.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"
namespace ash {
using ContentLayerType = AshColorProvider::ContentLayerType;
using ControlsLayerType = AshColorProvider::ControlsLayerType;
namespace {
void ConfigureFeaturePodLabel(views::Label* label,
int line_height,
int font_size) {
label->SetAutoColorReadabilityEnabled(false);
label->SetSubpixelRenderingEnabled(false);
label->SetCanProcessEventsWithinSubtree(false);
label->SetLineHeight(line_height);
gfx::Font default_font;
gfx::Font label_font =
default_font.Derive(font_size - default_font.GetFontSize(),
gfx::Font::NORMAL, gfx::Font::Weight::NORMAL);
gfx::FontList font_list(label_font);
label->SetFontList(font_list);
}
} // namespace
FeaturePodIconButton::FeaturePodIconButton(PressedCallback callback,
bool is_togglable)
: IconButton(std::move(callback),
IconButton::Type::kLarge,
/*icon=*/nullptr,
is_togglable,
/*has_border=*/true) {
SetFlipCanvasOnPaintForRTLUI(false);
GetViewAccessibility().SetIsLeaf(true);
}
FeaturePodIconButton::~FeaturePodIconButton() = default;
BEGIN_METADATA(FeaturePodIconButton)
END_METADATA
FeaturePodLabelButton::FeaturePodLabelButton(PressedCallback callback)
: Button(std::move(callback)),
label_(new views::Label),
sub_label_(new views::Label),
detailed_view_arrow_(new views::ImageView) {
SetBorder(views::CreateEmptyBorder(kUnifiedFeaturePodHoverPadding));
GetViewAccessibility().SetIsLeaf(true);
label_->SetLineHeight(kUnifiedFeaturePodLabelLineHeight);
label_->SetMultiLine(true);
label_->SetMaxLines(kUnifiedFeaturePodLabelMaxLines);
ConfigureFeaturePodLabel(label_, kUnifiedFeaturePodLabelLineHeight,
kUnifiedFeaturePodLabelFontSize);
ConfigureFeaturePodLabel(sub_label_, kUnifiedFeaturePodSubLabelLineHeight,
kUnifiedFeaturePodSubLabelFontSize);
sub_label_->SetVisible(false);
detailed_view_arrow_->SetCanProcessEventsWithinSubtree(false);
detailed_view_arrow_->SetVisible(false);
AddChildViewRaw(label_.get());
AddChildViewRaw(detailed_view_arrow_.get());
AddChildViewRaw(sub_label_.get());
StyleUtil::SetUpInkDropForButton(this);
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
views::InstallRoundRectHighlightPathGenerator(
this, gfx::Insets(), kUnifiedFeaturePodHoverCornerRadius);
views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);
}
FeaturePodLabelButton::~FeaturePodLabelButton() = default;
void FeaturePodLabelButton::Layout(PassKey) {
DCHECK(views::FocusRing::Get(this));
views::FocusRing::Get(this)->DeprecatedLayoutImmediately();
LayoutInCenter(label_, GetContentsBounds().y());
LayoutInCenter(sub_label_, GetContentsBounds().CenterPoint().y() +
kUnifiedFeaturePodInterLabelPadding);
if (!detailed_view_arrow_->GetVisible())
return;
// We need custom layout because |label_| is first laid out in the center
// without considering |detailed_view_arrow_|, then |detailed_view_arrow_| is
// placed on the right side of |label_|.
gfx::Size arrow_size = detailed_view_arrow_->GetPreferredSize();
detailed_view_arrow_->SetBoundsRect(gfx::Rect(
gfx::Point(label_->bounds().right() + kUnifiedFeaturePodArrowSpacing,
label_->bounds().CenterPoint().y() - arrow_size.height() / 2),
arrow_size));
}
gfx::Size FeaturePodLabelButton::CalculatePreferredSize(
const views::SizeBounds& available_size) const {
// Minimum width of the button
int width = kUnifiedFeaturePodLabelWidth + GetInsets().width();
if (detailed_view_arrow_->GetVisible()) {
const int label_width = std::min(
kUnifiedFeaturePodLabelWidth,
label_->GetPreferredSize(views::SizeBounds(label_->width(), {}))
.width());
// Symmetrically increase the width to accommodate the arrow
const int extra_space_for_arrow =
2 * (kUnifiedFeaturePodArrowSpacing +
detailed_view_arrow_->GetPreferredSize().width());
width = std::max(width,
label_width + extra_space_for_arrow + GetInsets().width());
}
// Make sure there is sufficient margin around the label.
int horizontal_margin =
width -
label_->GetPreferredSize(views::SizeBounds(label_->width(), {})).width();
if (horizontal_margin < 2 * kUnifiedFeaturePodMinimumHorizontalMargin)
width += 2 * kUnifiedFeaturePodMinimumHorizontalMargin - horizontal_margin;
int height = label_->GetPreferredSize(views::SizeBounds(label_->width(), {}))
.height() +
GetInsets().height();
if (sub_label_->GetVisible()) {
height +=
kUnifiedFeaturePodInterLabelPadding +
sub_label_->GetPreferredSize(views::SizeBounds(sub_label_->width(), {}))
.height();
}
return gfx::Size(width, height);
}
void FeaturePodLabelButton::OnThemeChanged() {
views::Button::OnThemeChanged();
OnEnabledChanged();
}
void FeaturePodLabelButton::SetLabel(std::u16string_view label) {
label_->SetText(label);
InvalidateLayout();
}
std::u16string_view FeaturePodLabelButton::GetLabelText() const {
return label_->GetText();
}
void FeaturePodLabelButton::SetSubLabel(std::u16string_view sub_label) {
sub_label_->SetText(sub_label);
sub_label_->SetVisible(!sub_label.empty());
label_->SetMultiLine(sub_label.empty());
InvalidateLayout();
}
std::u16string_view FeaturePodLabelButton::GetSubLabelText() const {
return sub_label_->GetText();
}
void FeaturePodLabelButton::ShowDetailedViewArrow() {
detailed_view_arrow_->SetVisible(true);
InvalidateLayout();
}
void FeaturePodLabelButton::OnEnabledChanged() {
views::Button::OnEnabledChanged();
const AshColorProvider* color_provider = AshColorProvider::Get();
const SkColor primary_text_color =
color_provider->GetContentLayerColor(ContentLayerType::kTextColorPrimary);
const SkColor secondary_text_color = color_provider->GetContentLayerColor(
ContentLayerType::kTextColorSecondary);
label_->SetEnabledColor(
GetEnabled() ? primary_text_color
: ColorUtil::GetDisabledColor(primary_text_color));
sub_label_->SetEnabledColor(
GetEnabled() ? secondary_text_color
: ColorUtil::GetDisabledColor(secondary_text_color));
const SkColor icon_color =
color_provider->GetContentLayerColor(ContentLayerType::kIconColorPrimary);
detailed_view_arrow_->SetImage(ui::ImageModel::FromVectorIcon(
kUnifiedMenuMoreIcon,
GetEnabled() ? icon_color : ColorUtil::GetDisabledColor(icon_color)));
}
void FeaturePodLabelButton::LayoutInCenter(views::View* child, int y) {
gfx::Rect contents_bounds = GetContentsBounds();
gfx::Size preferred_size =
child->GetPreferredSize(views::SizeBounds(child->width(), {}));
int child_width =
std::min(kUnifiedFeaturePodLabelWidth, preferred_size.width());
child->SetBounds(
contents_bounds.x() + (contents_bounds.width() - child_width) / 2, y,
child_width, preferred_size.height());
}
BEGIN_METADATA(FeaturePodLabelButton)
END_METADATA
FeaturePodButton::FeaturePodButton(FeaturePodControllerBase* controller,
bool is_togglable)
: icon_button_(new FeaturePodIconButton(
base::BindRepeating(&FeaturePodControllerBase::OnIconPressed,
base::Unretained(controller)),
is_togglable)),
label_button_(new FeaturePodLabelButton(
base::BindRepeating(&FeaturePodControllerBase::OnLabelPressed,
base::Unretained(controller)))) {
auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, gfx::Insets(),
kUnifiedFeaturePodSpacing));
layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kCenter);
AddChildViewRaw(icon_button_.get());
AddChildViewRaw(label_button_.get());
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
}
FeaturePodButton::~FeaturePodButton() = default;
double FeaturePodButton::GetOpacityForExpandedAmount(double expanded_amount) {
// TODO(amehfooz): Confirm the animation curve with UX.
return std::max(0., 5. * expanded_amount - 4.);
}
void FeaturePodButton::SetVectorIcon(const gfx::VectorIcon& icon) {
icon_button_->SetVectorIcon(icon);
}
void FeaturePodButton::SetLabel(const std::u16string& label) {
if (label_button_->GetLabelText() == label)
return;
label_button_->SetLabel(label);
DeprecatedLayoutImmediately();
label_button_->SchedulePaint();
}
void FeaturePodButton::SetSubLabel(const std::u16string& sub_label) {
if (label_button_->GetSubLabelText() == sub_label)
return;
label_button_->SetSubLabel(sub_label);
DeprecatedLayoutImmediately();
label_button_->SchedulePaint();
}
void FeaturePodButton::SetIconTooltip(const std::u16string& text) {
icon_button_->SetTooltipText(text);
}
void FeaturePodButton::SetLabelTooltip(const std::u16string& text) {
label_button_->SetTooltipText(text);
}
void FeaturePodButton::SetIconAndLabelTooltips(const std::u16string& text) {
SetIconTooltip(text);
SetLabelTooltip(text);
}
void FeaturePodButton::ShowDetailedViewArrow() {
label_button_->ShowDetailedViewArrow();
DeprecatedLayoutImmediately();
label_button_->SchedulePaint();
}
void FeaturePodButton::DisableLabelButtonFocus() {
label_button_->SetFocusBehavior(FocusBehavior::NEVER);
}
void FeaturePodButton::SetToggled(bool toggled) {
icon_button_->SetToggled(toggled);
}
void FeaturePodButton::SetExpandedAmount(double expanded_amount,
bool fade_icon_button) {
label_button_->SetVisible(expanded_amount > 0.0);
label_button_->layer()->SetOpacity(
GetOpacityForExpandedAmount(expanded_amount));
if (fade_icon_button)
layer()->SetOpacity(GetOpacityForExpandedAmount(expanded_amount));
else
layer()->SetOpacity(1.0);
}
void FeaturePodButton::SetVisibleByContainer(bool visible) {
View::SetVisible(visible);
}
void FeaturePodButton::SetVisible(bool visible) {
visible_preferred_ = visible;
View::SetVisible(visible);
}
bool FeaturePodButton::HasFocus() const {
return icon_button_->HasFocus() || label_button_->HasFocus();
}
void FeaturePodButton::RequestFocus() {
label_button_->RequestFocus();
}
void FeaturePodButton::OnEnabledChanged() {
icon_button_->SetEnabled(GetEnabled());
label_button_->SetEnabled(GetEnabled());
}
BEGIN_METADATA(FeaturePodButton)
END_METADATA
} // namespace ash
|