Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / system / tray / tray_notification_view.cc
blobbf977f513d378d8ef0cadec0145c9203d98f70f9
1 // Copyright (c) 2012 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 "ash/system/tray/tray_notification_view.h"
7 #include "ash/system/tray/system_tray_item.h"
8 #include "ash/system/tray/tray_constants.h"
9 #include "grit/ash_strings.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/resources/grit/ui_resources.h"
13 #include "ui/views/background.h"
14 #include "ui/views/controls/button/image_button.h"
15 #include "ui/views/controls/image_view.h"
16 #include "ui/views/layout/grid_layout.h"
18 namespace ash {
20 TrayNotificationView::TrayNotificationView(SystemTrayItem* owner, int icon_id)
21 : owner_(owner),
22 icon_id_(icon_id),
23 icon_(NULL),
24 autoclose_delay_(0) {
27 TrayNotificationView::~TrayNotificationView() {
30 void TrayNotificationView::InitView(views::View* contents) {
31 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
33 views::GridLayout* layout = new views::GridLayout(this);
34 SetLayoutManager(layout);
36 views::ImageButton* close_button = new views::ImageButton(this);
37 close_button->SetImage(views::CustomButton::STATE_NORMAL,
38 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
39 IDR_MESSAGE_CLOSE));
40 close_button->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
41 views::ImageButton::ALIGN_MIDDLE);
43 icon_ = new views::ImageView;
44 if (icon_id_ != 0) {
45 icon_->SetImage(
46 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id_));
49 views::ColumnSet* columns = layout->AddColumnSet(0);
51 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
53 // Icon
54 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
55 0, /* resize percent */
56 views::GridLayout::FIXED,
57 kNotificationIconWidth, kNotificationIconWidth);
59 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
61 // Contents
62 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
63 100, /* resize percent */
64 views::GridLayout::FIXED,
65 kTrayNotificationContentsWidth,
66 kTrayNotificationContentsWidth);
68 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
70 // Close button
71 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
72 0, /* resize percent */
73 views::GridLayout::FIXED,
74 kNotificationButtonWidth, kNotificationButtonWidth);
76 // Layout rows
77 layout->AddPaddingRow(0, kTrayPopupPaddingBetweenItems);
78 layout->StartRow(0, 0);
79 layout->AddView(icon_);
80 layout->AddView(contents);
81 layout->AddView(close_button);
82 layout->AddPaddingRow(0, kTrayPopupPaddingBetweenItems);
85 void TrayNotificationView::SetIconImage(const gfx::ImageSkia& image) {
86 icon_->SetImage(image);
87 SchedulePaint();
90 const gfx::ImageSkia& TrayNotificationView::GetIconImage() const {
91 return icon_->GetImage();
94 void TrayNotificationView::UpdateView(views::View* new_contents) {
95 RemoveAllChildViews(true);
96 InitView(new_contents);
97 Layout();
98 PreferredSizeChanged();
99 SchedulePaint();
102 void TrayNotificationView::UpdateViewAndImage(views::View* new_contents,
103 const gfx::ImageSkia& image) {
104 RemoveAllChildViews(true);
105 InitView(new_contents);
106 icon_->SetImage(image);
107 Layout();
108 PreferredSizeChanged();
109 SchedulePaint();
112 void TrayNotificationView::StartAutoCloseTimer(int seconds) {
113 autoclose_.Stop();
114 autoclose_delay_ = seconds;
115 if (autoclose_delay_) {
116 autoclose_.Start(FROM_HERE,
117 base::TimeDelta::FromSeconds(autoclose_delay_),
118 this, &TrayNotificationView::HandleClose);
122 void TrayNotificationView::StopAutoCloseTimer() {
123 autoclose_.Stop();
126 void TrayNotificationView::RestartAutoCloseTimer() {
127 if (autoclose_delay_)
128 StartAutoCloseTimer(autoclose_delay_);
131 void TrayNotificationView::ButtonPressed(views::Button* sender,
132 const ui::Event& event) {
133 HandleClose();
136 bool TrayNotificationView::OnMousePressed(const ui::MouseEvent& event) {
137 HandleClickAction();
138 return true;
141 void TrayNotificationView::OnGestureEvent(ui::GestureEvent* event) {
142 SlideOutView::OnGestureEvent(event);
143 if (event->handled())
144 return;
145 if (event->type() != ui::ET_GESTURE_TAP)
146 return;
147 HandleClickAction();
148 event->SetHandled();
151 void TrayNotificationView::OnClose() {
154 void TrayNotificationView::OnClickAction() {
157 void TrayNotificationView::OnSlideOut() {
158 owner_->HideNotificationView();
161 void TrayNotificationView::HandleClose() {
162 OnClose();
163 owner_->HideNotificationView();
166 void TrayNotificationView::HandleClickAction() {
167 HandleClose();
168 OnClickAction();
171 } // namespace ash