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
|
// Copyright 2020 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/controls/contextual_nudge.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shelf/shelf.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/style/typography.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/wm/collision_detection/collision_detection_utils.h"
#include "ui/aura/window.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_type.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/color_palette.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
namespace ash {
namespace {
views::BubbleBorder::Arrow GetArrowForPosition(
ContextualNudge::Position position) {
switch (position) {
case ContextualNudge::Position::kTop:
return views::BubbleBorder::BOTTOM_CENTER;
case ContextualNudge::Position::kBottom:
return views::BubbleBorder::TOP_CENTER;
}
}
} // namespace
ContextualNudge::ContextualNudge(views::View* anchor,
aura::Window* parent_window,
Position position,
const gfx::Insets& margins,
const std::u16string& text,
const base::RepeatingClosure& tap_callback)
: views::BubbleDialogDelegateView(anchor,
GetArrowForPosition(position),
views::BubbleBorder::NO_SHADOW),
tap_callback_(tap_callback) {
SetBackgroundColor(SK_ColorTRANSPARENT);
set_close_on_deactivate(false);
set_margins(gfx::Insets());
set_accept_events(!tap_callback.is_null());
SetCanActivate(false);
set_shadow(views::BubbleBorder::NO_SHADOW);
SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
set_layer_type(ui::LAYER_NOT_DRAWN);
if (parent_window) {
set_parent_window(parent_window);
} else if (anchor_widget()) {
set_parent_window(
anchor_widget()->GetNativeWindow()->GetRootWindow()->GetChildById(
kShellWindowId_ShelfContainer));
}
SetLayoutManager(std::make_unique<views::FillLayout>());
label_ = AddChildView(std::make_unique<views::Label>(text));
label_->SetPaintToLayer();
label_->layer()->SetFillsBoundsOpaquely(false);
label_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
label_->SetBackgroundColor(SK_ColorTRANSPARENT);
label_->SetBorder(views::CreateEmptyBorder(margins));
label_->SetEnabledColor(cros_tokens::kCrosSysSecondary);
TypographyProvider::Get()->StyleLabel(TypographyToken::kCrosAnnotation1,
*label_);
views::BubbleDialogDelegateView::CreateBubble(this);
// TODO(sanchit.abrol@microsoft.com): Move back among the other setters after
// the platform default setting is moved from
// |BubbleDialogDelegateView::CreateBubble| to being the default value at
// bubble construction.
set_adjust_if_offscreen(false);
// Text box for shelf nudge should be ignored for collision detection.
CollisionDetectionUtils::IgnoreWindowForCollisionDetection(
GetWidget()->GetNativeWindow());
}
ContextualNudge::~ContextualNudge() = default;
void ContextualNudge::UpdateAnchorRect(const gfx::Rect& rect) {
SetAnchorRect(rect);
}
void ContextualNudge::OnGestureEvent(ui::GestureEvent* event) {
if (event->type() == ui::EventType::kGestureTap && tap_callback_) {
event->StopPropagation();
tap_callback_.Run();
return;
}
// Pass on non tap events to the shelf (so it can handle swipe gestures that
// start on top of the nudge). Convert event to screen coordinates, as this is
// what Shelf::ProcessGestureEvent() expects.
ui::GestureEvent event_in_screen(*event);
gfx::Point location_in_screen(event->location());
View::ConvertPointToScreen(this, &location_in_screen);
event_in_screen.set_location(location_in_screen);
Shelf* shelf = Shelf::ForWindow(GetWidget()->GetNativeWindow());
if (shelf->ProcessGestureEvent(event_in_screen))
event->StopPropagation();
else
views::BubbleDialogDelegateView::OnGestureEvent(event);
}
} // namespace ash
|