Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: ui/message_center/views/notification_control_buttons_view.cc

Issue 2960333003: Extract the view of control buttons on notification into a separated class (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/message_center/views/notification_control_buttons_view.h"
6
7 #include "ui/base/l10n/l10n_util.h"
8 #include "ui/compositor/layer.h"
9 #include "ui/events/event.h"
10 #include "ui/gfx/animation/linear_animation.h"
11 #include "ui/message_center/message_center_style.h"
12 #include "ui/message_center/views/message_view.h"
13 #include "ui/message_center/views/padded_button.h"
14 #include "ui/strings/grit/ui_strings.h"
15 #include "ui/views/background.h"
16 #include "ui/views/layout/box_layout.h"
17
18 namespace {
19
20 // This value should be the same as the duration of reveal animation of
21 // the settings view of an Android notification.
22 constexpr int kBackgroundColorChangeDuration = 360;
23
24 // The initial background color of the view.
25 constexpr SkColor kInitialBackgroundColor =
26 message_center::kControlButtonBackgroundColor;
27
28 } // anonymous namespace
29
30 namespace message_center {
31
32 const char NotificationControlButtonsView::kViewClassName[] =
33 "NotificationControlButtonsView";
34
35 NotificationControlButtonsView::NotificationControlButtonsView(
36 MessageView* message_view)
37 : message_view_(message_view),
38 bgcolor_origin_(kInitialBackgroundColor),
39 bgcolor_target_(kInitialBackgroundColor) {
40 DCHECK(message_view);
41 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal));
42 SetBackground(views::CreateSolidBackground(kInitialBackgroundColor));
43 }
44
45 NotificationControlButtonsView::~NotificationControlButtonsView() = default;
46
47 void NotificationControlButtonsView::ShowCloseButton(bool show) {
yhanada 2017/06/29 04:14:47 What will happen if the notification is pinned the
yoshiki 2017/06/29 06:29:42 Good catch. I fixed the issue.
48 if (show && !close_button_) {
49 close_button_ = new message_center::PaddedButton(this);
50 close_button_->SetImage(views::CustomButton::STATE_NORMAL,
51 message_center::GetCloseIcon());
52 close_button_->SetAccessibleName(l10n_util::GetStringUTF16(
53 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME));
54 close_button_->SetTooltipText(l10n_util::GetStringUTF16(
55 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_TOOLTIP));
56 close_button_->SetBackground(
57 views::CreateSolidBackground(SK_ColorTRANSPARENT));
58 AddChildView(close_button_);
59 } else if (!show && close_button_) {
60 RemoveChildView(close_button_);
61 close_button_ = nullptr;
62 }
63 }
64
65 void NotificationControlButtonsView::ShowSettingsButton(bool show) {
66 if (show && !settings_button_) {
67 settings_button_ = new message_center::PaddedButton(this);
68 settings_button_->SetImage(views::CustomButton::STATE_NORMAL,
69 message_center::GetSettingsIcon());
70 settings_button_->SetAccessibleName(l10n_util::GetStringUTF16(
71 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
72 settings_button_->SetTooltipText(l10n_util::GetStringUTF16(
73 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
74 settings_button_->SetBackground(
75 views::CreateSolidBackground(SK_ColorTRANSPARENT));
76 AddChildView(settings_button_);
77 } else if (!show && close_button_) {
78 RemoveChildView(settings_button_);
79 settings_button_ = nullptr;
80 }
81 }
82
83 void NotificationControlButtonsView::SetBackgroundColor(
84 const SkColor& target_bgcolor) {
85 if (background()->get_color() != target_bgcolor) {
86 bgcolor_origin_ = background()->get_color();
87 bgcolor_target_ = target_bgcolor;
88
89 if (bgcolor_animation_)
90 bgcolor_animation_->End();
91 bgcolor_animation_.reset(new gfx::LinearAnimation(this));
92 bgcolor_animation_->SetDuration(kBackgroundColorChangeDuration);
93 bgcolor_animation_->Start();
94 }
95 }
96
97 void NotificationControlButtonsView::RequestFocusOnCloseButton() {
98 if (close_button_)
99 close_button_->RequestFocus();
100 }
101
102 bool NotificationControlButtonsView::IsCloseButtonFocused() const {
103 return close_button_ && close_button_->HasFocus();
104 }
105
106 bool NotificationControlButtonsView::IsSettingsButtonFocused() const {
107 return settings_button_ && settings_button_->HasFocus();
108 }
109
110 const char* NotificationControlButtonsView::GetClassName() const {
111 return kViewClassName;
112 }
113
114 void NotificationControlButtonsView::ButtonPressed(views::Button* sender,
115 const ui::Event& event) {
116 if (close_button_ && sender == close_button_) {
117 message_view_->OnCloseButtonPressed();
118 } else if (settings_button_ && sender == settings_button_) {
119 message_view_->OnSettingsButtonPressed();
120 }
121 }
122
123 void NotificationControlButtonsView::AnimationProgressed(
124 const gfx::Animation* animation) {
125 DCHECK_EQ(animation, bgcolor_animation_.get());
126
127 const SkColor color = gfx::Tween::ColorValueBetween(
128 animation->GetCurrentValue(), bgcolor_origin_, bgcolor_target_);
129 SetBackground(views::CreateSolidBackground(color));
130 SchedulePaint();
131 }
132
133 void NotificationControlButtonsView::AnimationEnded(
134 const gfx::Animation* animation) {
135 DCHECK_EQ(animation, bgcolor_animation_.get());
136 bgcolor_animation_.reset();
137 bgcolor_origin_ = bgcolor_target_;
138 }
139
140 void NotificationControlButtonsView::AnimationCanceled(
141 const gfx::Animation* animation) {
142 // The animation is never cancelled explicitly.
143 NOTREACHED();
144
145 bgcolor_origin_ = bgcolor_target_;
146 SetBackground(views::CreateSolidBackground(bgcolor_target_));
147 SchedulePaint();
148 }
149
150 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698