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.
4
5 #include "ash/system/tray/tray_notification_view.h"
6
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 "grit/ui_resources.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/image/image_skia.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"
17
18 namespace ash {
19 namespace internal {
20
TrayNotificationView(SystemTrayItem * owner,int icon_id)21 TrayNotificationView::TrayNotificationView(SystemTrayItem* owner, int icon_id)
22 : owner_(owner),
23 icon_id_(icon_id),
24 icon_(NULL) {
25 }
26
~TrayNotificationView()27 TrayNotificationView::~TrayNotificationView() {
28 }
29
InitView(views::View * contents)30 void TrayNotificationView::InitView(views::View* contents) {
31 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
32
33 views::GridLayout* layout = new views::GridLayout(this);
34 SetLayoutManager(layout);
35
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);
42
43 icon_ = new views::ImageView;
44 if (icon_id_ != 0) {
45 icon_->SetImage(
46 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id_));
47 }
48
49 views::ColumnSet* columns = layout->AddColumnSet(0);
50
51 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
52
53 // Icon
54 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
55 0, /* resize percent */
56 views::GridLayout::FIXED,
57 kNotificationIconWidth, kNotificationIconWidth);
58
59 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
60
61 // Contents
62 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
63 100, /* resize percent */
64 views::GridLayout::FIXED,
65 kTrayNotificationContentsWidth,
66 kTrayNotificationContentsWidth);
67
68 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
69
70 // Close button
71 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
72 0, /* resize percent */
73 views::GridLayout::FIXED,
74 kNotificationButtonWidth, kNotificationButtonWidth);
75
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);
83 }
84
SetIconImage(const gfx::ImageSkia & image)85 void TrayNotificationView::SetIconImage(const gfx::ImageSkia& image) {
86 icon_->SetImage(image);
87 SchedulePaint();
88 }
89
GetIconImage() const90 const gfx::ImageSkia& TrayNotificationView::GetIconImage() const {
91 return icon_->GetImage();
92 }
93
UpdateView(views::View * new_contents)94 void TrayNotificationView::UpdateView(views::View* new_contents) {
95 RemoveAllChildViews(true);
96 InitView(new_contents);
97 Layout();
98 PreferredSizeChanged();
99 SchedulePaint();
100 }
101
UpdateViewAndImage(views::View * new_contents,const gfx::ImageSkia & image)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();
110 }
111
StartAutoCloseTimer(int seconds)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);
119 }
120 }
121
StopAutoCloseTimer()122 void TrayNotificationView::StopAutoCloseTimer() {
123 autoclose_.Stop();
124 }
125
RestartAutoCloseTimer()126 void TrayNotificationView::RestartAutoCloseTimer() {
127 if (autoclose_delay_)
128 StartAutoCloseTimer(autoclose_delay_);
129 }
130
ButtonPressed(views::Button * sender,const ui::Event & event)131 void TrayNotificationView::ButtonPressed(views::Button* sender,
132 const ui::Event& event) {
133 HandleClose();
134 }
135
OnMousePressed(const ui::MouseEvent & event)136 bool TrayNotificationView::OnMousePressed(const ui::MouseEvent& event) {
137 HandleClickAction();
138 return true;
139 }
140
OnGestureEvent(ui::GestureEvent * event)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();
149 }
150
OnClose()151 void TrayNotificationView::OnClose() {
152 }
153
OnClickAction()154 void TrayNotificationView::OnClickAction() {
155 }
156
OnSlideOut()157 void TrayNotificationView::OnSlideOut() {
158 owner_->HideNotificationView();
159 }
160
HandleClose()161 void TrayNotificationView::HandleClose() {
162 OnClose();
163 owner_->HideNotificationView();
164 }
165
HandleClickAction()166 void TrayNotificationView::HandleClickAction() {
167 HandleClose();
168 OnClickAction();
169 }
170
171 } // namespace internal
172 } // namespace ash
173