OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/views/notifier_settings_view.h" | 5 #include "ui/message_center/views/notifier_settings_view.h" |
6 | 6 |
7 #include "grit/ui_strings.h" | 7 #include "grit/ui_strings.h" |
8 #include "third_party/skia/include/core/SkColor.h" | 8 #include "third_party/skia/include/core/SkColor.h" |
9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/gfx/canvas.h" | |
10 #include "ui/gfx/image/image.h" | 12 #include "ui/gfx/image/image.h" |
11 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
12 #include "ui/message_center/message_center_constants.h" | 14 #include "ui/message_center/message_center_constants.h" |
13 #include "ui/views/background.h" | 15 #include "ui/views/background.h" |
14 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
15 #include "ui/views/controls/button/checkbox.h" | 17 #include "ui/views/controls/button/checkbox.h" |
16 #include "ui/views/controls/button/custom_button.h" | 18 #include "ui/views/controls/button/custom_button.h" |
17 #include "ui/views/controls/image_view.h" | 19 #include "ui/views/controls/image_view.h" |
18 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
21 #include "ui/views/controls/scroll_view.h" | |
19 #include "ui/views/layout/box_layout.h" | 22 #include "ui/views/layout/box_layout.h" |
20 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
21 | 24 |
22 #if defined(USE_AURA) | 25 #if defined(USE_AURA) |
23 #include "ui/aura/window.h" | 26 #include "ui/aura/window.h" |
24 #endif | 27 #endif |
25 | 28 |
26 namespace message_center { | 29 namespace message_center { |
27 namespace { | 30 namespace { |
28 | 31 |
29 const int kSpaceInButtonComponents = 16; | 32 const int kSpaceInButtonComponents = 16; |
30 const int kMarginWidth = 16; | 33 const int kMarginWidth = 16; |
34 const int kMinimumWindowWidth = 320; | |
35 const int kMinimumWindowHeight = 480; | |
36 const int kEntryHeight = kMinimumWindowHeight / 10; | |
37 const SkColor kSeparatorColor = SkColorSetRGB(0xcc, 0xcc, 0xcc); | |
31 | 38 |
32 NotifierSettingsView* settings_view_ = NULL; | 39 NotifierSettingsView* settings_view_ = NULL; |
33 | 40 |
41 // The layout manager to guarantee the 48px height and place the view at the | |
42 // middle. It also guarantee the left margin. | |
43 class NotifierSettingsEntryLayoutManager : public views::LayoutManager { | |
dharcourt
2013/03/13 23:33:56
Nit: Since this class is private to NotifierSettin
dharcourt
2013/03/13 23:33:56
Suggestion: Having an EntryView that implements th
Jun Mukai
2013/03/14 06:47:48
Changed to EntryView. The code gets more concise.
| |
44 public: | |
45 NotifierSettingsEntryLayoutManager() {} | |
46 virtual ~NotifierSettingsEntryLayoutManager() {} | |
dharcourt
2013/03/13 23:33:56
Nit: Chromium style recommends against inlining ev
Jun Mukai
2013/03/14 06:47:48
fixed...
| |
47 | |
48 virtual void Layout(views::View* host) OVERRIDE; | |
49 virtual gfx::Size GetPreferredSize(views::View* host) OVERRIDE; | |
50 | |
51 private: | |
52 DISALLOW_COPY_AND_ASSIGN(NotifierSettingsEntryLayoutManager); | |
53 }; | |
54 | |
55 void NotifierSettingsEntryLayoutManager::Layout(views::View* host) { | |
56 if (!host->has_children()) | |
57 return; | |
58 | |
59 views::View* child = host->child_at(0); | |
60 gfx::Size size = child->GetPreferredSize(); | |
61 int y = 0; | |
62 if (size.height() < host->height()) | |
63 y = (host->height() - size.height()) / 2; | |
64 child->SetBounds( | |
65 kMarginWidth, y, host->width() - kMarginWidth, size.height()); | |
66 } | |
67 | |
68 gfx::Size NotifierSettingsEntryLayoutManager::GetPreferredSize( | |
69 views::View* host) { | |
70 DCHECK_EQ(1, host->child_count()); | |
dharcourt
2013/03/13 23:33:56
Q: Why is this a DCHECK whereas a similar check in
Jun Mukai
2013/03/14 06:47:48
Added DCHECK to both.
| |
71 gfx::Size size = host->child_at(0)->GetPreferredSize(); | |
72 size.ClampToMin(gfx::Size(kMinimumWindowWidth, kEntryHeight)); | |
73 return size; | |
74 } | |
75 | |
76 // The horizontal bar between title and scroller. | |
77 class SeparatorView : public views::View { | |
dharcourt
2013/03/13 23:33:56
Suggestion: It's kind of a hack, but you could rep
Jun Mukai
2013/03/14 06:47:48
Thinking about this more, I decided to change it t
| |
78 public: | |
79 SeparatorView() {} | |
80 virtual ~SeparatorView() {} | |
81 | |
82 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
83 | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(SeparatorView); | |
86 }; | |
87 | |
88 void SeparatorView::OnPaint(gfx::Canvas* canvas) { | |
89 canvas->DrawLine(gfx::Point(kMarginWidth, 0), | |
90 gfx::Point(width() - kMarginWidth, 0), | |
91 kSeparatorColor); | |
92 } | |
93 | |
34 } // namespace | 94 } // namespace |
35 | 95 |
36 NotifierSettingsDelegate* ShowSettings(NotifierSettingsProvider* provider, | 96 NotifierSettingsDelegate* ShowSettings(NotifierSettingsProvider* provider, |
37 gfx::NativeView context) { | 97 gfx::NativeView context) { |
38 if (!settings_view_) { | 98 if (!settings_view_) { |
39 settings_view_ = NotifierSettingsView::Create(provider, context); | 99 settings_view_ = NotifierSettingsView::Create(provider, context); |
40 } | 100 } |
41 return settings_view_; | 101 return settings_view_; |
42 } | 102 } |
43 | 103 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 return; | 205 return; |
146 } | 206 } |
147 } | 207 } |
148 } | 208 } |
149 | 209 |
150 NotifierSettingsView::NotifierSettingsView( | 210 NotifierSettingsView::NotifierSettingsView( |
151 NotifierSettingsProvider* delegate) | 211 NotifierSettingsProvider* delegate) |
152 : delegate_(delegate) { | 212 : delegate_(delegate) { |
153 DCHECK(delegate_); | 213 DCHECK(delegate_); |
154 | 214 |
155 SetLayoutManager(new views::BoxLayout( | |
156 views::BoxLayout::kVertical, kMarginWidth, kMarginWidth, kMarginWidth)); | |
157 set_background(views::Background::CreateSolidBackground(SK_ColorWHITE)); | 215 set_background(views::Background::CreateSolidBackground(SK_ColorWHITE)); |
158 | 216 |
159 views::Label* top_label = new views::Label( | 217 title_entry_ = new views::View(); |
160 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_FOOTER_TITLE)); | 218 title_entry_->SetLayoutManager(new NotifierSettingsEntryLayoutManager()); |
219 gfx::Font title_font = | |
220 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont); | |
221 views::Label* title_label = new views::Label( | |
222 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS_BUTTON_LABEL), | |
223 title_font); | |
224 title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
225 title_label->SetMultiLine(true); | |
226 title_entry_->AddChildView(title_label); | |
227 AddChildView(title_entry_); | |
228 | |
229 separator_ = new SeparatorView(); | |
230 AddChildView(separator_); | |
231 | |
232 scroller_ = new views::ScrollView(); | |
233 // TODO(mukai): set kennedy scroll bar here. | |
234 AddChildView(scroller_); | |
235 | |
236 views::View* contents_view = new views::View(); | |
237 contents_view->SetLayoutManager(new views::BoxLayout( | |
238 views::BoxLayout::kVertical, 0, 0, 0)); | |
239 | |
240 views::View* top_entry = new views::View(); | |
241 top_entry->SetLayoutManager(new NotifierSettingsEntryLayoutManager()); | |
242 views::Label* top_label = new views::Label(l10n_util::GetStringUTF16( | |
243 IDS_MESSAGE_CENTER_SETTINGS_DIALOG_DESCRIPTION)); | |
161 top_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 244 top_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
162 AddChildView(top_label); | 245 top_label->SetMultiLine(true); |
163 | 246 top_label->SizeToFit(kMinimumWindowWidth - kMarginWidth); |
164 views::View* items = new views::View(); | 247 top_entry->AddChildView(top_label); |
165 items->SetLayoutManager(new views::BoxLayout( | 248 contents_view->AddChildView(top_entry); |
166 views::BoxLayout::kVertical, 0, 0, kMarginWidth)); | |
167 items->set_border(views::Border::CreateEmptyBorder(0, kMarginWidth, 0, 0)); | |
168 AddChildView(items); | |
169 | 249 |
170 std::vector<Notifier*> notifiers; | 250 std::vector<Notifier*> notifiers; |
171 delegate_->GetNotifierList(¬ifiers); | 251 delegate_->GetNotifierList(¬ifiers); |
172 for (size_t i = 0; i < notifiers.size(); ++i) { | 252 for (size_t i = 0; i < notifiers.size(); ++i) { |
173 NotifierButton* button = new NotifierButton(notifiers[i], this); | 253 NotifierButton* button = new NotifierButton(notifiers[i], this); |
174 items->AddChildView(button); | 254 views::View* button_entry = new views::View(); |
255 button_entry->SetLayoutManager(new NotifierSettingsEntryLayoutManager()); | |
256 button_entry->AddChildView(button); | |
257 contents_view->AddChildView(button_entry); | |
175 buttons_.insert(button); | 258 buttons_.insert(button); |
176 } | 259 } |
260 scroller_->SetContents(contents_view); | |
261 | |
262 gfx::Size contents_size = contents_view->GetPreferredSize(); | |
263 if (kMinimumWindowWidth < | |
dharcourt
2013/03/13 23:33:56
Typo? Shouldn't this be kMinimumWindowHeight, and
Jun Mukai
2013/03/14 06:47:48
Aww, right. fixed. Thanks!
| |
264 title_entry_->GetPreferredSize().height() + contents_size.height()) { | |
265 contents_size.Enlarge(-scroller_->GetScrollBarWidth(), 0); | |
266 } | |
267 contents_view->SetBoundsRect(gfx::Rect(contents_size)); | |
177 } | 268 } |
178 | 269 |
179 NotifierSettingsView::~NotifierSettingsView() { | 270 NotifierSettingsView::~NotifierSettingsView() { |
180 settings_view_ = NULL; | 271 settings_view_ = NULL; |
181 } | 272 } |
182 | 273 |
183 bool NotifierSettingsView::CanResize() const { | |
184 return true; | |
185 } | |
186 | |
187 string16 NotifierSettingsView::GetWindowTitle() const { | |
188 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS_BUTTON_LABEL); | |
189 } | |
190 | |
191 void NotifierSettingsView::WindowClosing() { | 274 void NotifierSettingsView::WindowClosing() { |
192 if (delegate_) | 275 if (delegate_) |
193 delegate_->OnNotifierSettingsClosing(); | 276 delegate_->OnNotifierSettingsClosing(); |
194 } | 277 } |
195 | 278 |
196 views::View* NotifierSettingsView::GetContentsView() { | 279 views::View* NotifierSettingsView::GetContentsView() { |
197 return this; | 280 return this; |
198 } | 281 } |
199 | 282 |
283 void NotifierSettingsView::Layout() { | |
284 int title_height = title_entry_->GetPreferredSize().height(); | |
285 title_entry_->SetBounds(0, 0, width(), title_height); | |
286 separator_->SetBounds(0, title_height, | |
287 width() - scroller_->GetScrollBarWidth(), 1); | |
288 scroller_->SetBounds(0, title_height, width(), height() - title_height); | |
dharcourt
2013/03/13 23:33:56
Typo? Shouldn't y be (title_height + 1) and height
Jun Mukai
2013/03/14 06:47:48
Since separator_ is gone (it now belongs to scroll
| |
289 } | |
290 | |
291 gfx::Size NotifierSettingsView::GetMinimumSize() { | |
292 return gfx::Size(kMinimumWindowWidth, kMinimumWindowHeight); | |
293 } | |
294 | |
200 void NotifierSettingsView::ButtonPressed(views::Button* sender, | 295 void NotifierSettingsView::ButtonPressed(views::Button* sender, |
201 const ui::Event& event) { | 296 const ui::Event& event) { |
202 std::set<NotifierButton*>::iterator iter = buttons_.find( | 297 std::set<NotifierButton*>::iterator iter = buttons_.find( |
203 static_cast<NotifierButton*>(sender)); | 298 static_cast<NotifierButton*>(sender)); |
204 DCHECK(iter != buttons_.end()); | 299 DCHECK(iter != buttons_.end()); |
205 | 300 |
206 (*iter)->SetChecked(!(*iter)->checked()); | 301 (*iter)->SetChecked(!(*iter)->checked()); |
207 if (delegate_) | 302 if (delegate_) |
208 delegate_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked()); | 303 delegate_->SetNotifierEnabled((*iter)->notifier(), (*iter)->checked()); |
209 } | 304 } |
210 | 305 |
211 } // namespace message_center | 306 } // namespace message_center |
OLD | NEW |