Add toplevel trace event to MessagePumpLibevent::OnLibeventNotification
[chromium-blink-merge.git] / ash / system / tray / tray_notification_view.cc
blob5cb2d16a218bad2f6d92e3207d28cde2be5d962b
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 "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/image/image_skia.h"
11 #include "ui/resources/grit/ui_resources.h"
12 #include "ui/views/background.h"
13 #include "ui/views/controls/button/image_button.h"
14 #include "ui/views/controls/image_view.h"
15 #include "ui/views/layout/grid_layout.h"
17 namespace ash {
19 TrayNotificationView::TrayNotificationView(SystemTrayItem* owner, int icon_id)
20 : owner_(owner),
21 icon_id_(icon_id),
22 icon_(NULL),
23 autoclose_delay_(0) {
26 TrayNotificationView::~TrayNotificationView() {
29 void TrayNotificationView::InitView(views::View* contents) {
30 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
32 views::GridLayout* layout = new views::GridLayout(this);
33 SetLayoutManager(layout);
35 views::ImageButton* close_button = new views::ImageButton(this);
36 close_button->SetImage(views::CustomButton::STATE_NORMAL,
37 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
38 IDR_MESSAGE_CLOSE));
39 close_button->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
40 views::ImageButton::ALIGN_MIDDLE);
42 icon_ = new views::ImageView;
43 if (icon_id_ != 0) {
44 icon_->SetImage(
45 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id_));
48 views::ColumnSet* columns = layout->AddColumnSet(0);
50 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
52 // Icon
53 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
54 0, /* resize percent */
55 views::GridLayout::FIXED,
56 kNotificationIconWidth, kNotificationIconWidth);
58 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
60 // Contents
61 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
62 100, /* resize percent */
63 views::GridLayout::FIXED,
64 kTrayNotificationContentsWidth,
65 kTrayNotificationContentsWidth);
67 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal / 2);
69 // Close button
70 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
71 0, /* resize percent */
72 views::GridLayout::FIXED,
73 kNotificationButtonWidth, kNotificationButtonWidth);
75 // Layout rows
76 layout->AddPaddingRow(0, kTrayPopupPaddingBetweenItems);
77 layout->StartRow(0, 0);
78 layout->AddView(icon_);
79 layout->AddView(contents);
80 layout->AddView(close_button);
81 layout->AddPaddingRow(0, kTrayPopupPaddingBetweenItems);
84 void TrayNotificationView::SetIconImage(const gfx::ImageSkia& image) {
85 icon_->SetImage(image);
86 SchedulePaint();
89 const gfx::ImageSkia& TrayNotificationView::GetIconImage() const {
90 return icon_->GetImage();
93 void TrayNotificationView::UpdateView(views::View* new_contents) {
94 RemoveAllChildViews(true);
95 InitView(new_contents);
96 Layout();
97 PreferredSizeChanged();
98 SchedulePaint();
101 void TrayNotificationView::UpdateViewAndImage(views::View* new_contents,
102 const gfx::ImageSkia& image) {
103 RemoveAllChildViews(true);
104 InitView(new_contents);
105 icon_->SetImage(image);
106 Layout();
107 PreferredSizeChanged();
108 SchedulePaint();
111 void TrayNotificationView::StartAutoCloseTimer(int seconds) {
112 autoclose_.Stop();
113 autoclose_delay_ = seconds;
114 if (autoclose_delay_) {
115 autoclose_.Start(FROM_HERE,
116 base::TimeDelta::FromSeconds(autoclose_delay_),
117 this, &TrayNotificationView::HandleClose);
121 void TrayNotificationView::StopAutoCloseTimer() {
122 autoclose_.Stop();
125 void TrayNotificationView::RestartAutoCloseTimer() {
126 if (autoclose_delay_)
127 StartAutoCloseTimer(autoclose_delay_);
130 void TrayNotificationView::ButtonPressed(views::Button* sender,
131 const ui::Event& event) {
132 HandleClose();
135 bool TrayNotificationView::OnMousePressed(const ui::MouseEvent& event) {
136 HandleClickAction();
137 return true;
140 void TrayNotificationView::OnGestureEvent(ui::GestureEvent* event) {
141 SlideOutView::OnGestureEvent(event);
142 if (event->handled())
143 return;
144 if (event->type() != ui::ET_GESTURE_TAP)
145 return;
146 HandleClickAction();
147 event->SetHandled();
150 void TrayNotificationView::OnClose() {
153 void TrayNotificationView::OnClickAction() {
156 void TrayNotificationView::OnSlideOut() {
157 owner_->HideNotificationView();
160 void TrayNotificationView::HandleClose() {
161 OnClose();
162 owner_->HideNotificationView();
165 void TrayNotificationView::HandleClickAction() {
166 HandleClose();
167 OnClickAction();
170 } // namespace ash