DevTools: kill codeschool extension from whitelist
[chromium-blink-merge.git] / ui / message_center / views / notification_button.cc
blobb5a1240933bb8a6b09312ddc662cbccff2763742
1 // Copyright 2013 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.
5 #include "ui/message_center/views/notification_button.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/message_center/message_center_style.h"
9 #include "ui/message_center/views/constants.h"
10 #include "ui/views/background.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/image_view.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/painter.h"
17 namespace message_center {
19 NotificationButton::NotificationButton(views::ButtonListener* listener)
20 : views::CustomButton(listener),
21 icon_(NULL),
22 title_(NULL),
23 focus_painter_(views::Painter::CreateSolidFocusPainter(
24 message_center::kFocusBorderColor,
25 gfx::Insets(1, 2, 2, 2))) {
26 SetFocusable(true);
27 // Create a background so that it does not change when the MessageView
28 // background changes to show touch feedback
29 set_background(views::Background::CreateSolidBackground(
30 kNotificationBackgroundColor));
31 set_request_focus_on_press(false);
32 set_notify_enter_exit_on_child(true);
33 SetLayoutManager(
34 new views::BoxLayout(views::BoxLayout::kHorizontal,
35 message_center::kButtonHorizontalPadding,
36 kButtonVecticalPadding,
37 message_center::kButtonIconToTitlePadding));
40 NotificationButton::~NotificationButton() {
43 void NotificationButton::SetIcon(const gfx::ImageSkia& image) {
44 if (icon_ != NULL)
45 delete icon_; // This removes the icon from this view's children.
46 if (image.isNull()) {
47 icon_ = NULL;
48 } else {
49 icon_ = new views::ImageView();
50 icon_->SetImageSize(gfx::Size(message_center::kNotificationButtonIconSize,
51 message_center::kNotificationButtonIconSize));
52 icon_->SetImage(image);
53 icon_->SetHorizontalAlignment(views::ImageView::LEADING);
54 icon_->SetVerticalAlignment(views::ImageView::LEADING);
55 icon_->SetBorder(views::Border::CreateEmptyBorder(
56 message_center::kButtonIconTopPadding, 0, 0, 0));
57 AddChildViewAt(icon_, 0);
61 void NotificationButton::SetTitle(const base::string16& title) {
62 if (title_ != NULL)
63 delete title_; // This removes the title from this view's children.
64 if (title.empty()) {
65 title_ = NULL;
66 } else {
67 title_ = new views::Label(title);
68 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
69 title_->SetEnabledColor(message_center::kRegularTextColor);
70 title_->SetBackgroundColor(kRegularTextBackgroundColor);
71 title_->SetBorder(
72 views::Border::CreateEmptyBorder(kButtonTitleTopPadding, 0, 0, 0));
73 AddChildView(title_);
75 SetAccessibleName(title);
78 gfx::Size NotificationButton::GetPreferredSize() const {
79 return gfx::Size(message_center::kNotificationWidth,
80 message_center::kButtonHeight);
83 int NotificationButton::GetHeightForWidth(int width) const {
84 return message_center::kButtonHeight;
87 void NotificationButton::OnPaint(gfx::Canvas* canvas) {
88 CustomButton::OnPaint(canvas);
89 views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
92 void NotificationButton::OnFocus() {
93 views::CustomButton::OnFocus();
94 ScrollRectToVisible(GetLocalBounds());
95 // We render differently when focused.
96 SchedulePaint();
99 void NotificationButton::OnBlur() {
100 views::CustomButton::OnBlur();
101 // We render differently when focused.
102 SchedulePaint();
105 void NotificationButton::ViewHierarchyChanged(
106 const ViewHierarchyChangedDetails& details) {
107 // We disable view hierarchy change detection in the parent
108 // because it resets the hoverstate, which we do not want
109 // when we update the view to contain a new label or image.
110 views::View::ViewHierarchyChanged(details);
113 void NotificationButton::StateChanged() {
114 if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
115 set_background(views::Background::CreateSolidBackground(
116 message_center::kHoveredButtonBackgroundColor));
117 } else {
118 set_background(views::Background::CreateSolidBackground(
119 kNotificationBackgroundColor));
123 } // namespace message_center