OLD | NEW |
| (Empty) |
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 "ui/message_center/views/message_simple_view.h" | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "grit/ui_resources.h" | |
9 #include "ui/base/resource/resource_bundle.h" | |
10 #include "ui/message_center/message_center_style.h" | |
11 #include "ui/message_center/notification.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/controls/label.h" | |
16 #include "ui/views/controls/scroll_view.h" | |
17 #include "ui/views/layout/grid_layout.h" | |
18 | |
19 namespace message_center { | |
20 | |
21 const SkColor kNotificationColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); | |
22 const SkColor kNotificationReadColor = SkColorSetRGB(0xfa, 0xfa, 0xfa); | |
23 | |
24 MessageSimpleView::MessageSimpleView(const Notification& notification, | |
25 MessageCenter* message_center) | |
26 : MessageView(notification, message_center, NULL, false) { | |
27 set_focusable(true); | |
28 views::ImageButton* close = new views::ImageButton(this); | |
29 close->SetImage( | |
30 views::CustomButton::STATE_NORMAL, | |
31 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(IDR_MESSAGE_CLOSE)); | |
32 close->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | |
33 views::ImageButton::ALIGN_MIDDLE); | |
34 old_style_close_button_.reset(close); | |
35 SetUpView(notification); | |
36 } | |
37 | |
38 MessageSimpleView::~MessageSimpleView() { | |
39 // old_style_close_button_ has to be cleared before content_view_ is cleared. | |
40 // Otherwise, resetting content_view_ will delete old_style_close_button_ so | |
41 // a double-free happens. | |
42 old_style_close_button_.reset(); | |
43 content_view_.reset(); | |
44 } | |
45 | |
46 void MessageSimpleView::Layout() { | |
47 if (content_view_) { | |
48 gfx::Rect contents_bounds = GetContentsBounds(); | |
49 content_view_->SetBoundsRect(contents_bounds); | |
50 } | |
51 } | |
52 | |
53 gfx::Size MessageSimpleView::GetPreferredSize() { | |
54 if (!content_view_) | |
55 return gfx::Size(); | |
56 gfx::Size size = content_view_->GetPreferredSize(); | |
57 if (border()) { | |
58 gfx::Insets border_insets = border()->GetInsets(); | |
59 size.Enlarge(border_insets.width(), border_insets.height()); | |
60 } | |
61 return size; | |
62 } | |
63 | |
64 int MessageSimpleView::GetHeightForWidth(int width) { | |
65 if (!content_view_) | |
66 return 0; | |
67 return content_view_->GetHeightForWidth(width); | |
68 } | |
69 | |
70 void MessageSimpleView::SetUpView(const Notification& notification) { | |
71 views::ImageView* icon = new views::ImageView; | |
72 icon->SetImageSize( | |
73 gfx::Size(kWebNotificationIconSize, kWebNotificationIconSize)); | |
74 icon->SetImage(notification.icon().AsImageSkia()); | |
75 | |
76 views::Label* title = new views::Label(notification.title()); | |
77 title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
78 title->SetFont(title->font().DeriveFont(0, gfx::Font::BOLD)); | |
79 views::Label* message = new views::Label(notification.message()); | |
80 message->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
81 message->SetMultiLine(true); | |
82 | |
83 SkColor bg_color = notification.is_read() ? | |
84 kNotificationReadColor : kNotificationColor; | |
85 content_view_.reset(new views::View); | |
86 content_view_->set_background( | |
87 views::Background::CreateSolidBackground(bg_color)); | |
88 AddChildView(content_view_.get()); | |
89 views::GridLayout* layout = new views::GridLayout(content_view_.get()); | |
90 content_view_->SetLayoutManager(layout); | |
91 | |
92 views::ColumnSet* columns = layout->AddColumnSet(0); | |
93 | |
94 const int padding_width = kPaddingHorizontal / 2; | |
95 columns->AddPaddingColumn(0, padding_width); | |
96 | |
97 // Notification Icon. | |
98 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING, | |
99 0, /* resize percent */ | |
100 views::GridLayout::FIXED, | |
101 kWebNotificationIconSize, kWebNotificationIconSize); | |
102 | |
103 columns->AddPaddingColumn(0, padding_width); | |
104 | |
105 // Notification message text. | |
106 const int message_width = kNotificationWidth - kWebNotificationIconSize - | |
107 kWebNotificationButtonWidth - (padding_width * 3) - | |
108 (scroller() ? scroller()->GetScrollBarWidth() : 0); | |
109 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
110 100, /* resize percent */ | |
111 views::GridLayout::FIXED, | |
112 message_width, message_width); | |
113 | |
114 columns->AddPaddingColumn(0, padding_width); | |
115 message->SizeToFit(message_width); | |
116 | |
117 // Close button. | |
118 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING, | |
119 0, /* resize percent */ | |
120 views::GridLayout::FIXED, | |
121 kWebNotificationButtonWidth, | |
122 kWebNotificationButtonWidth); | |
123 | |
124 // Layout rows | |
125 layout->AddPaddingRow(0, kPaddingBetweenItems); | |
126 | |
127 layout->StartRow(0, 0); | |
128 layout->AddView(icon, 1, 2); | |
129 layout->AddView(title, 1, 1); | |
130 layout->AddView(old_style_close_button_.get(), 1, 1); | |
131 | |
132 layout->StartRow(0, 0); | |
133 layout->SkipColumns(2); | |
134 layout->AddView(message, 1, 1); | |
135 layout->AddPaddingRow(0, kPaddingBetweenItems); | |
136 set_accessible_name( | |
137 notification.title() + base::ASCIIToUTF16("\n") + notification.message()); | |
138 } | |
139 | |
140 void MessageSimpleView::ButtonPressed(views::Button* sender, | |
141 const ui::Event& event) { | |
142 bool close = (sender == old_style_close_button_.get()); | |
143 MessageView::ButtonPressed(close ? close_button() : sender, event); | |
144 } | |
145 | |
146 } // namespace message_center | |
OLD | NEW |