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

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: addressed comments 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) {
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
59 // Add the button at the last.
60 DCHECK_LE(child_count(), 1);
61 AddChildView(close_button_);
62 } else if (!show && close_button_) {
63 RemoveChildView(close_button_);
64 close_button_ = nullptr;
65 }
66 }
67
68 void NotificationControlButtonsView::ShowSettingsButton(bool show) {
69 if (show && !settings_button_) {
70 settings_button_ = new message_center::PaddedButton(this);
71 settings_button_->SetImage(views::CustomButton::STATE_NORMAL,
72 message_center::GetSettingsIcon());
73 settings_button_->SetAccessibleName(l10n_util::GetStringUTF16(
74 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
75 settings_button_->SetTooltipText(l10n_util::GetStringUTF16(
76 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
77 settings_button_->SetBackground(
78 views::CreateSolidBackground(SK_ColorTRANSPARENT));
79
80 // Add the button at the first.
81 DCHECK_LE(child_count(), 1);
82 AddChildViewAt(settings_button_, 0);
83 } else if (!show && close_button_) {
84 RemoveChildView(settings_button_);
85 settings_button_ = nullptr;
86 }
87 }
88
89 void NotificationControlButtonsView::SetBackgroundColor(
90 const SkColor& target_bgcolor) {
91 if (background()->get_color() != target_bgcolor) {
92 bgcolor_origin_ = background()->get_color();
93 bgcolor_target_ = target_bgcolor;
94
95 if (bgcolor_animation_)
96 bgcolor_animation_->End();
97 bgcolor_animation_.reset(new gfx::LinearAnimation(this));
98 bgcolor_animation_->SetDuration(kBackgroundColorChangeDuration);
99 bgcolor_animation_->Start();
100 }
101 }
102
103 void NotificationControlButtonsView::RequestFocusOnCloseButton() {
104 if (close_button_)
105 close_button_->RequestFocus();
106 }
107
108 bool NotificationControlButtonsView::IsCloseButtonFocused() const {
109 return close_button_ && close_button_->HasFocus();
110 }
111
112 bool NotificationControlButtonsView::IsSettingsButtonFocused() const {
113 return settings_button_ && settings_button_->HasFocus();
114 }
115
116 const char* NotificationControlButtonsView::GetClassName() const {
117 return kViewClassName;
118 }
119
120 void NotificationControlButtonsView::ButtonPressed(views::Button* sender,
121 const ui::Event& event) {
122 if (close_button_ && sender == close_button_) {
123 message_view_->OnCloseButtonPressed();
124 } else if (settings_button_ && sender == settings_button_) {
125 message_view_->OnSettingsButtonPressed();
126 }
127 }
128
129 void NotificationControlButtonsView::AnimationProgressed(
130 const gfx::Animation* animation) {
131 DCHECK_EQ(animation, bgcolor_animation_.get());
132
133 const SkColor color = gfx::Tween::ColorValueBetween(
134 animation->GetCurrentValue(), bgcolor_origin_, bgcolor_target_);
135 SetBackground(views::CreateSolidBackground(color));
136 SchedulePaint();
137 }
138
139 void NotificationControlButtonsView::AnimationEnded(
140 const gfx::Animation* animation) {
141 DCHECK_EQ(animation, bgcolor_animation_.get());
142 bgcolor_animation_.reset();
143 bgcolor_origin_ = bgcolor_target_;
144 }
145
146 void NotificationControlButtonsView::AnimationCanceled(
147 const gfx::Animation* animation) {
148 // The animation is never cancelled explicitly.
149 NOTREACHED();
150
151 bgcolor_origin_ = bgcolor_target_;
152 SetBackground(views::CreateSolidBackground(bgcolor_target_));
153 SchedulePaint();
154 }
155
156 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/views/notification_control_buttons_view.h ('k') | ui/message_center/views/notification_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698