summaryrefslogtreecommitdiff
path: root/chromium/ui/message_center/views/notification_control_buttons_view.cc
blob: bb10972f525389d9f294386c14b4d0eccdfac09f (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/message_center/views/notification_control_buttons_view.h"

#include "base/memory/ptr_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/layer.h"
#include "ui/events/event.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/message_center/message_center_style.h"
#include "ui/message_center/views/message_view.h"
#include "ui/message_center/views/padded_button.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/background.h"
#include "ui/views/layout/box_layout.h"

namespace {

// This value should be the same as the duration of reveal animation of
// the settings view of an Android notification.
constexpr auto kBackgroundColorChangeDuration =
    base::TimeDelta::FromMilliseconds(360);

// The initial background color of the view.
constexpr SkColor kInitialBackgroundColor =
    message_center::kControlButtonBackgroundColor;

}  // anonymous namespace

namespace message_center {

const char NotificationControlButtonsView::kViewClassName[] =
    "NotificationControlButtonsView";

NotificationControlButtonsView::NotificationControlButtonsView(
    MessageView* message_view)
    : message_view_(message_view),
      bgcolor_origin_(kInitialBackgroundColor),
      bgcolor_target_(kInitialBackgroundColor) {
  DCHECK(message_view);
  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal));

  // Use layer to change the opacity.
  SetPaintToLayer();
  layer()->SetFillsBoundsOpaquely(false);

  SetBackground(views::CreateSolidBackground(kInitialBackgroundColor));
}

NotificationControlButtonsView::~NotificationControlButtonsView() = default;

void NotificationControlButtonsView::ShowCloseButton(bool show) {
  if (show && !close_button_) {
    close_button_ = base::MakeUnique<message_center::PaddedButton>(this);
    close_button_->set_owned_by_client();
    close_button_->SetImage(views::CustomButton::STATE_NORMAL,
                            message_center::GetCloseIcon());
    close_button_->SetAccessibleName(l10n_util::GetStringUTF16(
        IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME));
    close_button_->SetTooltipText(l10n_util::GetStringUTF16(
        IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_TOOLTIP));
    close_button_->SetBackground(
        views::CreateSolidBackground(SK_ColorTRANSPARENT));

    // Add the button at the last.
    DCHECK_LE(child_count(), 1);
    AddChildView(close_button_.get());
  } else if (!show && close_button_) {
    DCHECK(Contains(close_button_.get()));
    close_button_.reset();
  }
}

void NotificationControlButtonsView::ShowSettingsButton(bool show) {
  if (show && !settings_button_) {
    settings_button_ = base::MakeUnique<message_center::PaddedButton>(this);
    settings_button_->set_owned_by_client();
    settings_button_->SetImage(views::CustomButton::STATE_NORMAL,
                               message_center::GetSettingsIcon());
    settings_button_->SetAccessibleName(l10n_util::GetStringUTF16(
        IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
    settings_button_->SetTooltipText(l10n_util::GetStringUTF16(
        IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
    settings_button_->SetBackground(
        views::CreateSolidBackground(SK_ColorTRANSPARENT));

    // Add the button at the first.
    DCHECK_LE(child_count(), 1);
    AddChildViewAt(settings_button_.get(), 0);
  } else if (!show && settings_button_) {
    DCHECK(Contains(settings_button_.get()));
    settings_button_.reset();
  }
}

void NotificationControlButtonsView::SetBackgroundColor(
    const SkColor& target_bgcolor) {
  DCHECK(background());
  if (background()->get_color() != target_bgcolor) {
    bgcolor_origin_ = background()->get_color();
    bgcolor_target_ = target_bgcolor;

    if (bgcolor_animation_)
      bgcolor_animation_->End();
    bgcolor_animation_.reset(new gfx::LinearAnimation(this));
    bgcolor_animation_->SetDuration(kBackgroundColorChangeDuration);
    bgcolor_animation_->Start();
  }
}

void NotificationControlButtonsView::SetVisible(bool visible) {
  DCHECK(layer());
  // Manipulate the opacity instead of changing the visibility to keep the tab
  // order even when the view is invisible.
  layer()->SetOpacity(visible ? 1. : 0.);
  set_can_process_events_within_subtree(visible);
}

void NotificationControlButtonsView::RequestFocusOnCloseButton() {
  if (close_button_)
    close_button_->RequestFocus();
}

bool NotificationControlButtonsView::IsCloseButtonFocused() const {
  return close_button_ && close_button_->HasFocus();
}

bool NotificationControlButtonsView::IsSettingsButtonFocused() const {
  return settings_button_ && settings_button_->HasFocus();
}

message_center::PaddedButton* NotificationControlButtonsView::close_button()
    const {
  return close_button_.get();
}

message_center::PaddedButton* NotificationControlButtonsView::settings_button()
    const {
  return settings_button_.get();
}

const char* NotificationControlButtonsView::GetClassName() const {
  return kViewClassName;
}

void NotificationControlButtonsView::ButtonPressed(views::Button* sender,
                                                   const ui::Event& event) {
  if (close_button_ && sender == close_button_.get()) {
    message_view_->OnCloseButtonPressed();
  } else if (settings_button_ && sender == settings_button_.get()) {
    message_view_->OnSettingsButtonPressed();
  }
}

void NotificationControlButtonsView::AnimationProgressed(
    const gfx::Animation* animation) {
  DCHECK_EQ(animation, bgcolor_animation_.get());

  const SkColor color = gfx::Tween::ColorValueBetween(
      animation->GetCurrentValue(), bgcolor_origin_, bgcolor_target_);
  SetBackground(views::CreateSolidBackground(color));
  SchedulePaint();
}

void NotificationControlButtonsView::AnimationEnded(
    const gfx::Animation* animation) {
  DCHECK_EQ(animation, bgcolor_animation_.get());
  bgcolor_animation_.reset();
  bgcolor_origin_ = bgcolor_target_;
}

void NotificationControlButtonsView::AnimationCanceled(
    const gfx::Animation* animation) {
  // The animation is never cancelled explicitly.
  NOTREACHED();

  bgcolor_origin_ = bgcolor_target_;
  SetBackground(views::CreateSolidBackground(bgcolor_target_));
  SchedulePaint();
}

}  // namespace message_center