Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2005)

Side by Side Diff: ui/message_center/notification_view.cc

Issue 11639041: Added support for image notifications. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/message_center/notification_list.cc ('k') | ui/notifications/notification_types.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/message_center/notification_view.h" 5 #include "ui/message_center/notification_view.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "grit/ui_resources.h" 8 #include "grit/ui_resources.h"
9 #include "ui/base/accessibility/accessible_view_state.h" 9 #include "ui/base/accessibility/accessible_view_state.h"
10 #include "ui/base/resource/resource_bundle.h" 10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/base/text/text_elider.h" 11 #include "ui/base/text/text_elider.h"
12 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/size.h" 13 #include "ui/gfx/size.h"
14 #include "ui/message_center/message_center_constants.h" 14 #include "ui/message_center/message_center_constants.h"
15 #include "ui/native_theme/native_theme.h" 15 #include "ui/native_theme/native_theme.h"
16 #include "ui/views/controls/button/image_button.h" 16 #include "ui/views/controls/button/image_button.h"
17 #include "ui/views/controls/image_view.h" 17 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/label.h" 18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/box_layout.h" 19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/layout/grid_layout.h" 20 #include "ui/views/layout/grid_layout.h"
21 21
22 namespace { 22 namespace {
23 23
24 // Notification dimensions. 24 // Notification dimensions.
25 const int kIconTopPadding = 0;
25 const int kIconLeftPadding = 0; 26 const int kIconLeftPadding = 0;
27 const int kIconBottomPadding = 0;
26 const int kIconColumnWidth = message_center::kNotificationIconWidth; 28 const int kIconColumnWidth = message_center::kNotificationIconWidth;
27 const int kIconToTextPadding = 15; 29 const int kIconToTextPadding = 15;
30 const int kTextTopPadding = 9;
31 const int kTextBottomPadding = 12;
28 const int kTextToClosePadding = 10; 32 const int kTextToClosePadding = 10;
33 const int kCloseTopPadding = 6;
34 const int kCloseRightPadding = 6;
29 const int kCloseColumnWidth = 8; 35 const int kCloseColumnWidth = 8;
30 const int kCloseRightPadding = 6;
31 const int kIconTopPadding = 0;
32 const int kTextTopPadding = 9;
33 const int kCloseTopPadding = 6;
34 const int kIconBottomPadding = 0;
35 const int kTextBottomPadding = 12;
36 const int kItemTitleToDetailsPadding = 3; 36 const int kItemTitleToDetailsPadding = 3;
37 const int kImageTopPadding = 0;
38 const int kImageLeftPadding = 0;
39 const int kImageBottomPadding = 0;
40 const int kImageRightPadding = 0;
37 41
38 // Notification colors. The text background colors below are used only to keep 42 // Notification colors. The text background colors below are used only to keep
39 // view::Label from modifying the text color and will not actually be drawn. 43 // view::Label from modifying the text color and will not actually be drawn.
40 // See view::Label's SetEnabledColor() and SetBackgroundColor() for details. 44 // See view::Label's SetEnabledColor() and SetBackgroundColor() for details.
41 const SkColor kBackgroundColor = SkColorSetRGB(255, 255, 255); 45 const SkColor kBackgroundColor = SkColorSetRGB(255, 255, 255);
42 const SkColor kTitleColor = SkColorSetRGB(68, 68, 68); 46 const SkColor kTitleColor = SkColorSetRGB(68, 68, 68);
43 const SkColor kTitleBackgroundColor = SK_ColorWHITE; 47 const SkColor kTitleBackgroundColor = SK_ColorWHITE;
44 const SkColor kMessageColor = SkColorSetRGB(136, 136, 136); 48 const SkColor kMessageColor = SkColorSetRGB(136, 136, 136);
45 const SkColor kMessageBackgroundColor = SK_ColorBLACK; 49 const SkColor kMessageBackgroundColor = SK_ColorBLACK;
46 50
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 details->SetBackgroundColor(kMessageBackgroundColor); 85 details->SetBackgroundColor(kMessageBackgroundColor);
82 AddChildViewAt(details, 1); 86 AddChildViewAt(details, 1);
83 87
84 PreferredSizeChanged(); 88 PreferredSizeChanged();
85 SchedulePaint(); 89 SchedulePaint();
86 } 90 }
87 91
88 ItemView::~ItemView() { 92 ItemView::~ItemView() {
89 } 93 }
90 94
95 // ProportionalImageViews match their heights to their widths to preserve the
96 // proportions of their images.
97 class ProportionalImageView : public views::ImageView {
98 public:
99 ProportionalImageView();
100 virtual ~ProportionalImageView();
101
102 // Overridden from views::View.
103 virtual int GetHeightForWidth(int width) OVERRIDE;
104 };
105
106 ProportionalImageView::ProportionalImageView() {
107 }
108
109 ProportionalImageView::~ProportionalImageView() {
110 }
111
112 int ProportionalImageView::GetHeightForWidth(int width) {
113 int height = 0;
114 gfx::ImageSkia image = GetImage();
115 if (image.width() > 0 && image.height() > 0) {
116 double proportion = image.height() / (double) image.width();
117 height = 0.5 + width * proportion;
118 if (height > message_center::kNotificationMaximumImageHeight) {
119 height = message_center::kNotificationMaximumImageHeight;
120 width = 0.5 + height / proportion;
121 }
122 SetImageSize(gfx::Size(width, height));
123 }
124 return height;
125 }
126
91 } // namespace 127 } // namespace
92 128
93 namespace message_center { 129 namespace message_center {
94 130
95 NotificationView::NotificationView( 131 NotificationView::NotificationView(
96 NotificationList::Delegate* list_delegate, 132 NotificationList::Delegate* list_delegate,
97 const NotificationList::Notification& notification) 133 const NotificationList::Notification& notification)
98 : MessageView(list_delegate, notification) { 134 : MessageView(list_delegate, notification) {
99 } 135 }
100 136
(...skipping 22 matching lines...) Expand all
123 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 159 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
124 100, views::GridLayout::USE_PREF, 160 100, views::GridLayout::USE_PREF,
125 0, 0); 161 0, 0);
126 // Text + padding (kTextToClosePadding). 162 // Text + padding (kTextToClosePadding).
127 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING, 163 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING,
128 0, views::GridLayout::FIXED, 164 0, views::GridLayout::FIXED,
129 kCloseColumnWidth + kCloseRightPadding, 165 kCloseColumnWidth + kCloseRightPadding,
130 kCloseColumnWidth + kCloseRightPadding); 166 kCloseColumnWidth + kCloseRightPadding);
131 // Close button + padding. 167 // Close button + padding.
132 168
133 // First row: Icon. This vertically spans the close button padding row, the 169 // Figure out how many rows the icon should span.
134 // close button row, and all item rows. 170 int span = 2; // Two rows for the close button padding and close button.
171 if (notification_.items.size() > 0) {
172 span += notification_.items.size(); // One row per item.
173 } else {
174 span += 1; // One row for the message.
175 }
Jun Mukai 2012/12/20 18:26:49 remove braces for 1-line if.
dharcourt 2012/12/20 20:51:32 Done.
176
177 // First row: Icon.
135 layout->StartRow(0, 0); 178 layout->StartRow(0, 0);
136 views::ImageView* icon = new views::ImageView(); 179 views::ImageView* icon = new views::ImageView();
137 icon->SetImageSize(gfx::Size(message_center::kNotificationIconWidth, 180 icon->SetImageSize(gfx::Size(message_center::kNotificationIconWidth,
138 message_center::kNotificationIconWidth)); 181 message_center::kNotificationIconWidth));
139 icon->SetImage(notification_.primary_icon); 182 icon->SetImage(notification_.primary_icon);
140 icon->SetHorizontalAlignment(views::ImageView::LEADING); 183 icon->SetHorizontalAlignment(views::ImageView::LEADING);
141 icon->SetVerticalAlignment(views::ImageView::LEADING); 184 icon->SetVerticalAlignment(views::ImageView::LEADING);
142 icon->set_border(MakePadding(kIconTopPadding, kIconLeftPadding, 185 icon->set_border(MakePadding(kIconTopPadding, kIconLeftPadding,
143 kIconBottomPadding, kIconToTextPadding)); 186 kIconBottomPadding, kIconToTextPadding));
144 layout->AddView(icon, 1, 2 + notification_.items.size()); 187 layout->AddView(icon, 1, span);
145 188
146 // First row: Title. This vertically spans the close button padding row and 189 // First row: Title. This vertically spans the close button padding row and
147 // the close button row. 190 // the close button row.
148 // TODO(dharcourt): Skip the title Label when there's no title text. 191 // TODO(dharcourt): Skip the title Label when there's no title text.
149 views::Label* title = new views::Label(notification_.title); 192 views::Label* title = new views::Label(notification_.title);
150 title->SetHorizontalAlignment(gfx::ALIGN_LEFT); 193 title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
151 title->SetFont(title->font().DeriveFont(4)); 194 title->SetFont(title->font().DeriveFont(4));
152 title->SetEnabledColor(kTitleColor); 195 title->SetEnabledColor(kTitleColor);
153 title->SetBackgroundColor(kTitleBackgroundColor); 196 title->SetBackgroundColor(kTitleBackgroundColor);
154 title->set_border(MakePadding(kTextTopPadding, 0, 3, kTextToClosePadding)); 197 title->set_border(MakePadding(kTextTopPadding, 0, 3, kTextToClosePadding));
155 layout->AddView(title, 1, 2); 198 layout->AddView(title, 1, 2,
199 views::GridLayout::LEADING, views::GridLayout::LEADING);
156 200
157 // First row: Close button padding. 201 // First row: Close button padding.
158 views::View* padding = new views::ImageView(); 202 views::View* padding = new views::ImageView();
159 padding->set_border(MakePadding(kCloseTopPadding, 1, 0, 0)); 203 padding->set_border(MakePadding(kCloseTopPadding, 1, 0, 0));
160 layout->AddView(padding); 204 layout->AddView(padding);
161 205
162 // Second row: Close button, which has to be on a row of its own because its 206 // Second row: Close button, which has to be on a row of its own because its
163 // top padding can't be set using empty borders (ImageButtons don't support 207 // top padding can't be set using empty borders (ImageButtons don't support
164 // borders). The resize factor of this row (100) is much higher than that of 208 // borders). The resize factor of this row (1) is higher than that of the
165 // other rows (0) to ensure the first row's height stays at kCloseTopPadding. 209 // first rows (0) to ensure the first row's height stays at kCloseTopPadding.
166 layout->StartRow(100, 0); 210 layout->StartRow(1, 0);
167 layout->SkipColumns(2); 211 layout->SkipColumns(2);
168 DCHECK(close_button_); 212 DCHECK(close_button_);
169 layout->AddView(close_button_); 213 layout->AddView(close_button_);
170 214
171 // One row for each notification item, including appropriate padding. 215 // One row for the message if appropriate. The resize factor of this row (2)
216 // is higher than that of preceding rows (0 and 1) to ensure the content of
217 // the notification is top-aligned.
218 if (notification_.items.size() == 0) {
219 layout->StartRow(2, 0);
220 layout->SkipColumns(1);
221 views::Label* message = new views::Label(notification_.message);
222 message->SetHorizontalAlignment(gfx::ALIGN_LEFT);
223 message->SetElideBehavior(views::Label::ELIDE_AT_END);
224 message->SetEnabledColor(kMessageColor);
225 message->SetBackgroundColor(kMessageBackgroundColor);
226 message->set_border(MakePadding(0, 0, 3, kTextToClosePadding));
227 layout->AddView(message, 1, 1,
228 views::GridLayout::LEADING, views::GridLayout::LEADING);
229 layout->SkipColumns(1);
230 }
231
232 // One row for each notification item, including appropriate padding. The
233 // resize factor of the last row of items (3) is higher than that of all
234 // preceding rows (0, 1, and 2) to ensure the content of the notification is
235 // top-aligned.
172 for (int i = 0, n = notification_.items.size(); i < n; ++i) { 236 for (int i = 0, n = notification_.items.size(); i < n; ++i) {
173 int bottom_padding = (i < n - 1) ? 4 : (kTextBottomPadding - 2); 237 int bottom_padding = (i < n - 1) ? 4 : (kTextBottomPadding - 2);
174 layout->StartRow(0, 0); 238 int resize_factor = (i < n - 1) ? 2 : 3;
239 layout->StartRow(resize_factor, 0);
175 layout->SkipColumns(1); 240 layout->SkipColumns(1);
176 ItemView* item = new ItemView(notification_.items[i]); 241 ItemView* item = new ItemView(notification_.items[i]);
177 item->set_border(MakePadding(0, 0, bottom_padding, kTextToClosePadding)); 242 item->set_border(MakePadding(0, 0, bottom_padding, kTextToClosePadding));
178 layout->AddView(item); 243 layout->AddView(item);
179 layout->SkipColumns(1); 244 layout->SkipColumns(1);
180 } 245 }
246
247 // One row for the image.
248 layout->StartRow(0, 0);
249 views::ImageView* image = new ProportionalImageView();
250 image->SetImageSize(notification_.image.size());
251 image->SetImage(notification_.image);
252 image->SetHorizontalAlignment(views::ImageView::CENTER);
253 image->SetVerticalAlignment(views::ImageView::LEADING);
254 image->set_border(MakePadding(kImageTopPadding, kImageLeftPadding,
255 kImageBottomPadding, kImageRightPadding));
256 layout->AddView(image, 3, 1,
257 views::GridLayout::FILL, views::GridLayout::LEADING);
181 } 258 }
182 259
183 } // namespace message_center 260 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/notification_list.cc ('k') | ui/notifications/notification_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698