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"
20 TrayNotificationView::TrayNotificationView(SystemTrayItem
* owner
, int icon_id
)
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(
40 close_button
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
41 views::ImageButton::ALIGN_MIDDLE
);
43 icon_
= new views::ImageView
;
46 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id_
));
49 views::ColumnSet
* columns
= layout
->AddColumnSet(0);
51 columns
->AddPaddingColumn(0, kTrayPopupPaddingHorizontal
/ 2);
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);
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);
71 columns
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::LEADING
,
72 0, /* resize percent */
73 views::GridLayout::FIXED
,
74 kNotificationButtonWidth
, kNotificationButtonWidth
);
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
);
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
);
98 PreferredSizeChanged();
102 void TrayNotificationView::UpdateViewAndImage(views::View
* new_contents
,
103 const gfx::ImageSkia
& image
) {
104 RemoveAllChildViews(true);
105 InitView(new_contents
);
106 icon_
->SetImage(image
);
108 PreferredSizeChanged();
112 void TrayNotificationView::StartAutoCloseTimer(int seconds
) {
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() {
126 void TrayNotificationView::RestartAutoCloseTimer() {
127 if (autoclose_delay_
)
128 StartAutoCloseTimer(autoclose_delay_
);
131 void TrayNotificationView::ButtonPressed(views::Button
* sender
,
132 const ui::Event
& event
) {
136 bool TrayNotificationView::OnMousePressed(const ui::MouseEvent
& event
) {
141 void TrayNotificationView::OnGestureEvent(ui::GestureEvent
* event
) {
142 SlideOutView::OnGestureEvent(event
);
143 if (event
->handled())
145 if (event
->type() != ui::ET_GESTURE_TAP
)
151 void TrayNotificationView::OnClose() {
154 void TrayNotificationView::OnClickAction() {
157 void TrayNotificationView::OnSlideOut() {
158 owner_
->HideNotificationView();
161 void TrayNotificationView::HandleClose() {
163 owner_
->HideNotificationView();
166 void TrayNotificationView::HandleClickAction() {