File: contextual_nudge.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (130 lines) | stat: -rw-r--r-- 4,743 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
// 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 "chromeos/constants/chromeos_features.h"
#include "ui/aura/window.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.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) {
  // Bubbles that use transparent colors should not paint their ClientViews to a
  // layer as doing so could result in visual artifacts.
  SetPaintClientToLayer(false);
  set_color(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(ui::DIALOG_BUTTON_NONE);

  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));
  if (chromeos::features::IsJellyEnabled()) {
    label_->SetEnabledColorId(cros_tokens::kCrosSysSecondary);
    TypographyProvider::Get()->StyleLabel(TypographyToken::kCrosAnnotation1,
                                          *label_);
  } else {
    label_->SetEnabledColorId(kColorAshTextColorPrimary);
  }

  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);
}

ui::LayerType ContextualNudge::GetLayerType() const {
  return ui::LAYER_NOT_DRAWN;
}

void ContextualNudge::OnGestureEvent(ui::GestureEvent* event) {
  if (event->type() == ui::ET_GESTURE_TAP && 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