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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
|
// Copyright 2023 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/toast/system_nudge_view.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/style/color_provider.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/keyboard_shortcut_view.h"
#include "ash/style/pill_button.h"
#include "ash/style/system_shadow.h"
#include "ash/style/typography.h"
#include "ash/system/toast/nudge_constants.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_tracker.h"
namespace ash {
namespace {
constexpr int kFocusableViewsGroupId = 1;
// Nudge constants
constexpr gfx::Insets kNudgeInteriorMargin = gfx::Insets::VH(20, 20);
constexpr gfx::Insets kTextOnlyNudgeInteriorMargin = gfx::Insets::VH(12, 20);
constexpr gfx::Insets kNudgeWithCloseButton_InteriorMargin =
gfx::Insets::TLBR(8, 20, 20, 8);
constexpr gfx::Insets
kNudgeWithCloseButton_ImageAndTextContainerInteriorMargin =
gfx::Insets::TLBR(12, 0, 0, 12);
constexpr gfx::Insets kNudgeWithCloseButton_ButtonContainerInteriorMargin =
gfx::Insets::TLBR(0, 0, 0, 12);
constexpr float kNudgeCornerRadius = 24.0f;
constexpr float kNudgePointyCornerRadius = 4.0f;
// Label constants
constexpr int kBodyLabelMaxLines = 3;
// Image constants
constexpr int kImageViewSize = 60;
constexpr int kImageViewCornerRadius = 12;
// Button constants
constexpr gfx::Insets kButtonsMargins = gfx::Insets::VH(0, 8);
// Padding constants
constexpr int kButtonContainerTopPadding = 16;
constexpr int kImageViewTrailingPadding = 16;
constexpr int kTitleBottomPadding = 4;
void AddPaddingView(views::View* parent, int width, int height) {
parent->AddChildView(std::make_unique<views::View>())
->SetPreferredSize(gfx::Size(width, height));
}
// Returns true if the provided arrow is located at a corner.
bool CalculateIsCornerAnchored(views::BubbleBorder::Arrow arrow) {
switch (arrow) {
case views::BubbleBorder::Arrow::TOP_LEFT:
case views::BubbleBorder::Arrow::TOP_RIGHT:
case views::BubbleBorder::Arrow::BOTTOM_LEFT:
case views::BubbleBorder::Arrow::BOTTOM_RIGHT:
case views::BubbleBorder::Arrow::LEFT_TOP:
case views::BubbleBorder::Arrow::RIGHT_TOP:
case views::BubbleBorder::Arrow::LEFT_BOTTOM:
case views::BubbleBorder::Arrow::RIGHT_BOTTOM:
return true;
default:
return false;
}
}
// Returns a `gfx::RoundedCornersF` object that has a single pointy corner which
// is defined based on the nudge's position in relation to its anchor view.
gfx::RoundedCornersF CalculatePointyAnchoredNudgeCorners(
views::View* nudge_view,
views::View* anchor_view) {
auto nudge_bounds = nudge_view->GetBoundsInScreen();
auto anchor_bounds = anchor_view->GetBoundsInScreen();
bool pointy_bottom = nudge_bounds.bottom() < anchor_bounds.bottom();
bool pointy_right = nudge_bounds.right() < anchor_bounds.right();
auto top_left_radius = !pointy_bottom && !pointy_right
? kNudgePointyCornerRadius
: kNudgeCornerRadius;
auto top_right_radius = !pointy_bottom && pointy_right
? kNudgePointyCornerRadius
: kNudgeCornerRadius;
auto bottom_right_radius = pointy_bottom && pointy_right
? kNudgePointyCornerRadius
: kNudgeCornerRadius;
auto bottom_left_radius = pointy_bottom && !pointy_right
? kNudgePointyCornerRadius
: kNudgeCornerRadius;
return gfx::RoundedCornersF(top_left_radius, top_right_radius,
bottom_right_radius, bottom_left_radius);
}
} // namespace
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(SystemNudgeView, kBubbleIdForTesting);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(SystemNudgeView,
kPrimaryButtonIdForTesting);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(SystemNudgeView,
kSecondaryButtonIdForTesting);
// FocusableChildrenObserver --------------------------------------------------
// Observes child view's focus state changes.
class SystemNudgeView::FocusableChildrenObserver : public views::ViewObserver {
public:
FocusableChildrenObserver(
std::vector<views::View*> observed_children,
base::RepeatingCallback<void(/*has_focus=*/bool)> focus_callback)
: observed_children_(std::move(observed_children)),
focus_callback_(std::move(focus_callback)) {
for (views::View* observed_child : observed_children_) {
observed_child->AddObserver(this);
}
}
FocusableChildrenObserver(const FocusableChildrenObserver&) = delete;
FocusableChildrenObserver& operator=(const FocusableChildrenObserver&) =
delete;
~FocusableChildrenObserver() override {
for (views::View* observed_child : observed_children_) {
observed_child->RemoveObserver(this);
}
}
private:
// ViewObserver:
void OnViewFocused(views::View* observed_view) override {
focus_callback_.Run(/*has_focus=*/true);
}
void OnViewBlurred(views::View* observed_view) override {
focus_callback_.Run(/*has_focus=*/false);
}
const std::vector<views::View*> observed_children_;
base::RepeatingCallback<void(/*has_focus=*/bool)> focus_callback_;
};
// SystemNudgeView ------------------------------------------------------------
SystemNudgeView::SystemNudgeView(
const AnchoredNudgeData& nudge_data,
base::RepeatingCallback<void(/*is_hovered_or_has_focus=*/bool)>
hover_or_focus_changed_callback)
: shadow_(SystemShadow::CreateShadowOnTextureLayer(
SystemShadow::Type::kElevation4)),
is_corner_anchored_(CalculateIsCornerAnchored(nudge_data.arrow)),
hover_changed_callback_(std::move(nudge_data.hover_changed_callback)),
hover_or_focus_changed_callback_(
std::move(hover_or_focus_changed_callback)) {
// Painted to layer so the view can be semi-transparent and set rounded
// corners.
SetPaintToLayer();
if (chromeos::features::IsSystemBlurEnabled()) {
layer()->SetFillsBoundsOpaquely(false);
layer()->SetBackgroundBlur(ColorProvider::kBackgroundBlurSigma);
layer()->SetBackdropFilterQuality(ColorProvider::kBackgroundBlurQuality);
}
const ui::ColorId default_background_color_id =
chromeos::features::IsSystemBlurEnabled()
? static_cast<ui::ColorId>(kColorAshShieldAndBase80)
: cros_tokens::kCrosSysSystemBaseElevatedOpaque;
SetBackground(views::CreateSolidBackground(
nudge_data.background_color_id.value_or(default_background_color_id)));
SetNotifyEnterExitOnChild(true);
SetProperty(views::kElementIdentifierKey, kBubbleIdForTesting);
// Cache the anchor view when the nudge anchors by its corner to set a pointy
// corner based on the nudge's position in relation to this anchor view.
if (nudge_data.is_anchored() && is_corner_anchored_) {
anchor_view_tracker_ = std::make_unique<views::ViewTracker>();
anchor_view_tracker_->SetView(nudge_data.GetAnchorView());
SetNudgeRoundedCornerRadius(CalculatePointyAnchoredNudgeCorners(
/*nudge_view=*/this, anchor_view_tracker_->view()));
} else {
SetNudgeRoundedCornerRadius(gfx::RoundedCornersF(kNudgeCornerRadius));
}
SetOrientation(views::LayoutOrientation::kVertical);
SetInteriorMargin(kNudgeInteriorMargin);
SetCrossAxisAlignment(views::LayoutAlignment::kStretch);
// TODO(crbug.com/40232718): See View::SetLayoutManagerUseConstrainedSpace
SetLayoutManagerUseConstrainedSpace(false);
const bool nudge_is_text_only = nudge_data.image_model.IsEmpty() &&
nudge_data.title_text.empty() &&
nudge_data.primary_button_text.empty() &&
nudge_data.keyboard_codes.empty();
// Nudges without an anchor view that are not text-only will have a close
// button that is visible on view hovered.
const bool has_close_button =
!nudge_data.is_anchored() && !nudge_is_text_only;
views::View* image_and_text_container;
auto image_and_text_container_unique =
views::Builder<views::FlexLayoutView>()
.SetOrientation(views::LayoutOrientation::kHorizontal)
.SetCrossAxisAlignment(views::LayoutAlignment::kStart)
.SetInteriorMargin(
has_close_button
? kNudgeWithCloseButton_ImageAndTextContainerInteriorMargin
: gfx::Insets())
.Build();
if (has_close_button) {
SetInteriorMargin(kNudgeWithCloseButton_InteriorMargin);
// Set the `image_and_text_container` parent to use a `FillLayout` so it can
// allow for overlap with the close button.
auto* fill_layout_container = AddChildView(std::make_unique<views::View>());
fill_layout_container->SetLayoutManager(
std::make_unique<views::FillLayout>());
image_and_text_container = fill_layout_container->AddChildView(
std::move(image_and_text_container_unique));
auto* close_button_container = fill_layout_container->AddChildView(
views::Builder<views::FlexLayoutView>()
.SetOrientation(views::LayoutOrientation::kHorizontal)
.SetMainAxisAlignment(views::LayoutAlignment::kEnd)
.SetCrossAxisAlignment(views::LayoutAlignment::kStart)
.Build());
close_button_ = close_button_container->AddChildView(
views::Builder<views::ImageButton>()
.SetID(VIEW_ID_SYSTEM_NUDGE_CLOSE_BUTTON)
.SetCallback(std::move(nudge_data.close_button_callback))
.SetImageModel(views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(
kCloseSmallIcon, cros_tokens::kCrosSysOnSurface))
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SYSTEM_NUDGE_CLOSE_BUTTON_TOOLTIP))
.SetVisible(false)
.Build());
} else {
image_and_text_container =
AddChildView(std::move(image_and_text_container_unique));
}
const bool has_image = !nudge_data.image_model.IsEmpty();
if (has_image) {
auto* image_view = image_and_text_container->AddChildView(
views::Builder<views::ImageView>()
.SetID(VIEW_ID_SYSTEM_NUDGE_IMAGE_VIEW)
.SetPreferredSize(gfx::Size(kImageViewSize, kImageViewSize))
.SetImage(nudge_data.image_model)
// Painted to layer to set rounded corners.
.SetPaintToLayer()
.Build());
// Certain `ImageModels` do not have the ability to set their size in the
// constructor, so instead we can do it here.
if (nudge_data.fill_image_size) {
image_view->SetImageSize(gfx::Size(kImageViewSize, kImageViewSize));
}
image_view->layer()->SetFillsBoundsOpaquely(false);
image_view->layer()->SetRoundedCornerRadius(
gfx::RoundedCornersF(kImageViewCornerRadius));
if (nudge_data.image_background_color_id) {
image_view->SetBackground(
views::CreateSolidBackground(*nudge_data.image_background_color_id));
}
AddPaddingView(image_and_text_container, kImageViewTrailingPadding,
kImageViewSize);
}
const bool has_title = !nudge_data.title_text.empty();
auto* text_container = image_and_text_container->AddChildView(
views::Builder<views::FlexLayoutView>()
.SetOrientation(views::LayoutOrientation::kVertical)
.SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::LayoutOrientation::kVertical,
views::MinimumFlexSizeRule::kPreferred,
views::MaximumFlexSizeRule::kUnbounded))
// If the nudge has an image and no title, vertically center the text.
.SetMainAxisAlignment(has_image && !has_title
? views::LayoutAlignment::kCenter
: views::LayoutAlignment::kStart)
.Build());
auto label_width = nudge_data.image_model.IsEmpty()
? kNudgeLabelWidth_NudgeWithoutLeadingImage
: kNudgeLabelWidth_NudgeWithLeadingImage;
if (has_title) {
auto* title_label = text_container->AddChildView(
views::Builder<views::Label>()
.SetID(VIEW_ID_SYSTEM_NUDGE_TITLE_LABEL)
.SetText(nudge_data.title_text)
.SetTooltipText(nudge_data.title_text)
.SetHorizontalAlignment(gfx::ALIGN_LEFT)
.SetEnabledColor(cros_tokens::kCrosSysOnSurface)
.SetAutoColorReadabilityEnabled(false)
.SetSubpixelRenderingEnabled(false)
.SetFontList(TypographyProvider::Get()->ResolveTypographyToken(
TypographyToken::kCrosButton1))
.SetMaximumWidthSingleLine(label_width)
.Build());
AddPaddingView(text_container, title_label->width(), kTitleBottomPadding);
}
auto* body_label = text_container->AddChildView(
views::Builder<views::Label>()
.SetID(VIEW_ID_SYSTEM_NUDGE_BODY_LABEL)
.SetText(nudge_data.body_text)
.SetTooltipText(nudge_data.body_text)
.SetHorizontalAlignment(gfx::ALIGN_LEFT)
.SetEnabledColor(cros_tokens::kCrosSysOnSurface)
.SetAutoColorReadabilityEnabled(false)
.SetSubpixelRenderingEnabled(false)
.SetFontList(TypographyProvider::Get()->ResolveTypographyToken(
TypographyToken::kCrosAnnotation1))
.SetMultiLine(true)
.SetMaxLines(kBodyLabelMaxLines)
.SizeToFit(label_width)
.Build());
// TODO(b/302368860): Add support for a view to display keyboard shortcuts in
// the same style as the launcher and the new keyboard shortcut app.
if (!nudge_data.keyboard_codes.empty()) {
AddPaddingView(text_container, image_and_text_container->width(),
kTitleBottomPadding);
text_container
->AddChildView(
std::make_unique<KeyboardShortcutView>(nudge_data.keyboard_codes))
->SetID(VIEW_ID_SYSTEM_NUDGE_SHORTCUT_VIEW);
}
// Return early if there are no buttons.
if (nudge_data.primary_button_text.empty()) {
CHECK(nudge_data.secondary_button_text.empty());
// Update nudge margins and body label max width if nudge only has text.
if (nudge_is_text_only) {
SetInteriorMargin(kTextOnlyNudgeInteriorMargin);
// `SizeToFit` is reset to zero so a maximum width can be set.
body_label->SizeToFit(0);
body_label->SetMaximumWidth(kNudgeLabelWidth_TextOnlyNudge);
}
return;
}
// Add top padding for the buttons row.
AddPaddingView(this, image_and_text_container->width(),
kButtonContainerTopPadding);
auto* buttons_container = AddChildView(
views::Builder<views::FlexLayoutView>()
.SetMainAxisAlignment(views::LayoutAlignment::kEnd)
.SetInteriorMargin(
has_close_button
? kNudgeWithCloseButton_ButtonContainerInteriorMargin
: gfx::Insets())
.SetIgnoreDefaultMainAxisMargins(true)
.SetCollapseMargins(true)
.Build());
buttons_container->SetDefault(views::kMarginsKey, kButtonsMargins);
std::vector<views::View*> focusable_children;
focusable_children.push_back(buttons_container->AddChildView(
views::Builder<PillButton>()
.SetID(VIEW_ID_SYSTEM_NUDGE_PRIMARY_BUTTON)
.SetGroup(kFocusableViewsGroupId)
.SetCallback(std::move(nudge_data.primary_button_callback))
.SetText(nudge_data.primary_button_text)
.SetTooltipText(nudge_data.primary_button_text)
.SetPillButtonType(PillButton::Type::kPrimaryWithoutIcon)
.SetFocusBehavior(views::View::FocusBehavior::ALWAYS)
.SetProperty(views::kElementIdentifierKey, kPrimaryButtonIdForTesting)
.Build()));
if (!nudge_data.secondary_button_text.empty()) {
focusable_children.push_back(buttons_container->AddChildViewAt(
views::Builder<PillButton>()
.SetID(VIEW_ID_SYSTEM_NUDGE_SECONDARY_BUTTON)
.SetGroup(kFocusableViewsGroupId)
.SetCallback(std::move(nudge_data.secondary_button_callback))
.SetText(nudge_data.secondary_button_text)
.SetTooltipText(nudge_data.secondary_button_text)
.SetPillButtonType(PillButton::Type::kSecondaryWithoutIcon)
.SetFocusBehavior(views::View::FocusBehavior::ALWAYS)
.SetProperty(views::kElementIdentifierKey,
kSecondaryButtonIdForTesting)
.Build(),
0));
}
focusable_children_observer_ = std::make_unique<FocusableChildrenObserver>(
std::move(focusable_children),
base::BindRepeating(&SystemNudgeView::HandleOnChildFocusStateChanged,
// Unretained is safe because `this` outlives the
// `FocusableChildrenObserver`.
base::Unretained(this)));
}
SystemNudgeView::~SystemNudgeView() {
auto* widget = GetWidget();
if (widget && widget->HasObserver(this)) {
widget->RemoveObserver(this);
}
}
void SystemNudgeView::AddedToWidget() {
GetWidget()->AddObserver(this);
// Attach the shadow at the bottom of the parent layer.
auto* shadow_layer = shadow_->GetLayer();
auto* parent_layer = layer()->parent();
parent_layer->Add(shadow_layer);
parent_layer->StackAtBottom(shadow_layer);
}
void SystemNudgeView::RemovedFromWidget() {
auto* widget = GetWidget();
if (widget && widget->HasObserver(this)) {
widget->RemoveObserver(this);
}
}
void SystemNudgeView::OnMouseEntered(const ui::MouseEvent& event) {
HandleOnMouseHovered(/*mouse_entered=*/true);
}
void SystemNudgeView::OnMouseExited(const ui::MouseEvent& event) {
HandleOnMouseHovered(/*mouse_entered=*/false);
}
void SystemNudgeView::OnWidgetBoundsChanged(views::Widget* widget,
const gfx::Rect& new_bounds) {
// `shadow_` should have the same bounds as the view's layer.
shadow_->SetContentBounds(layer()->bounds());
if (anchor_view_tracker_ && anchor_view_tracker_->view() &&
is_corner_anchored_) {
SetNudgeRoundedCornerRadius(CalculatePointyAnchoredNudgeCorners(
/*nudge_view=*/this, anchor_view_tracker_->view()));
}
}
void SystemNudgeView::OnWidgetDestroying(views::Widget* widget) {
if (widget && widget->HasObserver(this)) {
widget->RemoveObserver(this);
}
}
void SystemNudgeView::HandleOnChildFocusStateChanged(bool focus_entered) {
hover_or_focus_changed_callback_.Run(IsHoveredOrChildHasFocus());
}
void SystemNudgeView::HandleOnMouseHovered(bool mouse_entered) {
if (close_button_) {
close_button_->SetVisible(mouse_entered);
}
if (hover_changed_callback_) {
hover_changed_callback_.Run(mouse_entered);
}
hover_or_focus_changed_callback_.Run(IsHoveredOrChildHasFocus());
}
bool SystemNudgeView::IsHoveredOrChildHasFocus() {
views::View::Views focusable_views;
GetViewsInGroup(kFocusableViewsGroupId, &focusable_views);
bool child_has_focus =
std::find(focusable_views.begin(), focusable_views.end(),
GetWidget()->GetFocusManager()->GetFocusedView()) !=
focusable_views.end();
return IsMouseHovered() || child_has_focus;
}
void SystemNudgeView::SetNudgeRoundedCornerRadius(
const gfx::RoundedCornersF& rounded_corners) {
layer()->SetRoundedCornerRadius(rounded_corners);
SetBorder(std::make_unique<views::HighlightBorder>(
rounded_corners, views::HighlightBorder::Type::kHighlightBorderOnShadow));
shadow_->SetRoundedCorners(rounded_corners);
}
BEGIN_METADATA(SystemNudgeView)
END_METADATA
} // namespace ash
|