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

Side by Side Diff: chrome/browser/ui/views/content_setting_bubble_contents.cc

Issue 12208010: Adding device selection menus to the content setting bubble (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: used dynamic sizing for the menu buttons and TOPLEFT alignment Created 7 years, 10 months 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
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 "chrome/browser/ui/views/content_setting_bubble_contents.h" 5 #include "chrome/browser/ui/views/content_setting_bubble_contents.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/stl_util.h"
12 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/content_settings/host_content_settings_map.h" 16 #include "chrome/browser/content_settings/host_content_settings_map.h"
14 #include "chrome/browser/plugins/plugin_finder.h" 17 #include "chrome/browser/plugins/plugin_finder.h"
15 #include "chrome/browser/plugins/plugin_metadata.h" 18 #include "chrome/browser/plugins/plugin_metadata.h"
19 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/chrome_style.h" 20 #include "chrome/browser/ui/chrome_style.h"
17 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" 21 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
22 #include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h"
18 #include "chrome/browser/ui/views/browser_dialogs.h" 23 #include "chrome/browser/ui/views/browser_dialogs.h"
19 #include "content/public/browser/notification_source.h" 24 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/notification_types.h" 25 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/plugin_service.h" 26 #include "content/public/browser/plugin_service.h"
27 #include "content/public/browser/web_contents.h"
22 #include "grit/generated_resources.h" 28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/models/simple_menu_model.h"
32 #include "ui/views/controls/button/menu_button.h"
24 #include "ui/views/controls/button/radio_button.h" 33 #include "ui/views/controls/button/radio_button.h"
25 #include "ui/views/controls/button/text_button.h" 34 #include "ui/views/controls/button/text_button.h"
26 #include "ui/views/controls/image_view.h" 35 #include "ui/views/controls/image_view.h"
27 #include "ui/views/controls/label.h" 36 #include "ui/views/controls/label.h"
28 #include "ui/views/controls/link.h" 37 #include "ui/views/controls/link.h"
38 #include "ui/views/controls/menu/menu.h"
39 #include "ui/views/controls/menu/menu_model_adapter.h"
40 #include "ui/views/controls/menu/menu_runner.h"
29 #include "ui/views/controls/separator.h" 41 #include "ui/views/controls/separator.h"
30 #include "ui/views/layout/grid_layout.h" 42 #include "ui/views/layout/grid_layout.h"
31 #include "ui/views/layout/layout_constants.h" 43 #include "ui/views/layout/layout_constants.h"
32 44
33 #if defined(USE_AURA) 45 #if defined(USE_AURA)
34 #include "ui/base/cursor/cursor.h" 46 #include "ui/base/cursor/cursor.h"
35 #endif 47 #endif
36 48
49 namespace {
50
37 // If we don't clamp the maximum width, then very long URLs and titles can make 51 // If we don't clamp the maximum width, then very long URLs and titles can make
38 // the bubble arbitrarily wide. 52 // the bubble arbitrarily wide.
39 const int kMaxContentsWidth = 500; 53 const int kMaxContentsWidth = 500;
40 54
41 // When we have multiline labels, we should set a minimum width lest we get very 55 // When we have multiline labels, we should set a minimum width lest we get very
42 // narrow bubbles with lots of line-wrapping. 56 // narrow bubbles with lots of line-wrapping.
43 const int kMinMultiLineContentsWidth = 250; 57 const int kMinMultiLineContentsWidth = 250;
44 58
59 // The width of the media menu button.
Peter Kasting 2013/02/16 02:55:13 Nit: width -> minimum width Also, I'm a fan of de
no longer working on chromium 2013/02/18 17:20:04 Done changing the comment to minimum width. Regar
Peter Kasting 2013/02/18 18:23:18 Yeah, I was really suggesting fixing all of these
60 const int kMinMediaMenuButtonWidth = 100;
61
62 }
63
45 using content::PluginService; 64 using content::PluginService;
46 using content::WebContents; 65 using content::WebContents;
47 66
48 class ContentSettingBubbleContents::Favicon : public views::ImageView { 67 class ContentSettingBubbleContents::Favicon : public views::ImageView {
Peter Kasting 2013/02/16 02:55:13 Nit: Since you have a number of classes defined in
no longer working on chromium 2013/02/18 17:20:04 Done.
49 public: 68 public:
50 Favicon(const gfx::Image& image, 69 Favicon(const gfx::Image& image,
51 ContentSettingBubbleContents* parent, 70 ContentSettingBubbleContents* parent,
52 views::Link* link); 71 views::Link* link);
53 virtual ~Favicon(); 72 virtual ~Favicon();
54 73
55 private: 74 private:
56 // views::View overrides: 75 // views::View overrides:
57 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 76 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
58 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 77 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
59 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE; 78 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE;
60 79
61 ContentSettingBubbleContents* parent_; 80 ContentSettingBubbleContents* parent_;
62 views::Link* link_; 81 views::Link* link_;
63 }; 82 };
64 83
65 ContentSettingBubbleContents::Favicon::Favicon( 84 ContentSettingBubbleContents::Favicon::Favicon(
66 const gfx::Image& image, 85 const gfx::Image& image,
67 ContentSettingBubbleContents* parent, 86 ContentSettingBubbleContents* parent,
68 views::Link* link) 87 views::Link* link)
69 : parent_(parent), 88 : parent_(parent),
70 link_(link) { 89 link_(link) {
71 SetImage(image.AsImageSkia()); 90 SetImage(image.AsImageSkia());
72 } 91 }
73 92
74 ContentSettingBubbleContents::Favicon::~Favicon() { 93 ContentSettingBubbleContents::Favicon::~Favicon() {
75 } 94 }
76 95
96 struct ContentSettingBubbleContents::MediaMenuParts {
97 explicit MediaMenuParts(content::MediaStreamType type);
98 ~MediaMenuParts();
99
100 content::MediaStreamType type;
101 scoped_ptr<ui::SimpleMenuModel> menu_model;
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts);
105 };
106
107 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts(
108 content::MediaStreamType type)
109 : type(type) {}
110
111 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {}
112
77 bool ContentSettingBubbleContents::Favicon::OnMousePressed( 113 bool ContentSettingBubbleContents::Favicon::OnMousePressed(
78 const ui::MouseEvent& event) { 114 const ui::MouseEvent& event) {
79 return event.IsLeftMouseButton() || event.IsMiddleMouseButton(); 115 return event.IsLeftMouseButton() || event.IsMiddleMouseButton();
80 } 116 }
81 117
82 void ContentSettingBubbleContents::Favicon::OnMouseReleased( 118 void ContentSettingBubbleContents::Favicon::OnMouseReleased(
83 const ui::MouseEvent& event) { 119 const ui::MouseEvent& event) {
84 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) && 120 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
85 HitTestPoint(event.location())) { 121 HitTestPoint(event.location())) {
86 parent_->LinkClicked(link_, event.flags()); 122 parent_->LinkClicked(link_, event.flags());
(...skipping 22 matching lines...) Expand all
109 manage_link_(NULL), 145 manage_link_(NULL),
110 close_button_(NULL) { 146 close_button_(NULL) {
111 // Compensate for built-in vertical padding in the anchor view's image. 147 // Compensate for built-in vertical padding in the anchor view's image.
112 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); 148 set_anchor_insets(gfx::Insets(5, 0, 5, 0));
113 149
114 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 150 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
115 content::Source<WebContents>(web_contents)); 151 content::Source<WebContents>(web_contents));
116 } 152 }
117 153
118 ContentSettingBubbleContents::~ContentSettingBubbleContents() { 154 ContentSettingBubbleContents::~ContentSettingBubbleContents() {
155 STLDeleteValues(&media_menus_);
119 } 156 }
120 157
121 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { 158 gfx::Size ContentSettingBubbleContents::GetPreferredSize() {
122 gfx::Size preferred_size(views::View::GetPreferredSize()); 159 gfx::Size preferred_size(views::View::GetPreferredSize());
123 int preferred_width = 160 int preferred_width =
124 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && 161 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() &&
125 (kMinMultiLineContentsWidth > preferred_size.width())) ? 162 (kMinMultiLineContentsWidth > preferred_size.width())) ?
126 kMinMultiLineContentsWidth : preferred_size.width(); 163 kMinMultiLineContentsWidth : preferred_size.width();
127 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); 164 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth));
128 return preferred_size; 165 return preferred_size;
129 } 166 }
130 167
168 void ContentSettingBubbleContents::UpdateMenuLabel(
169 content::MediaStreamType type,
170 const std::string& label) {
171 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin();
172 it != media_menus_.end(); ++it) {
173 if (it->second->type == type) {
174 it->first->SetText(UTF8ToUTF16(label));
175 return;
176 }
177 }
178 NOTREACHED();
179 }
180
131 void ContentSettingBubbleContents::Init() { 181 void ContentSettingBubbleContents::Init() {
132 using views::GridLayout; 182 using views::GridLayout;
133 183
134 GridLayout* layout = new views::GridLayout(this); 184 GridLayout* layout = new views::GridLayout(this);
135 SetLayoutManager(layout); 185 SetLayoutManager(layout);
136 186
137 const int kSingleColumnSetId = 0; 187 const int kSingleColumnSetId = 0;
138 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); 188 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId);
139 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, 189 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
140 GridLayout::USE_PREF, 0, 0); 190 GridLayout::USE_PREF, 0, 0);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 layout->StartRow(0, indented_kSingleColumnSetId); 268 layout->StartRow(0, indented_kSingleColumnSetId);
219 layout->AddView(radio); 269 layout->AddView(radio);
220 bubble_content_empty = false; 270 bubble_content_empty = false;
221 } 271 }
222 DCHECK(!radio_group_.empty()); 272 DCHECK(!radio_group_.empty());
223 // Now that the buttons have been added to the view hierarchy, it's safe 273 // Now that the buttons have been added to the view hierarchy, it's safe
224 // to call SetChecked() on them. 274 // to call SetChecked() on them.
225 radio_group_[radio_group.default_item]->SetChecked(true); 275 radio_group_[radio_group.default_item]->SetChecked(true);
226 } 276 }
227 277
278 // Layout code for the media device menus.
279 if (content_setting_bubble_model_->content_type() ==
280 CONTENT_SETTINGS_TYPE_MEDIASTREAM) {
281 const int kMediaMenuColumnSetId = 2;
282 views::ColumnSet* menu_column_set =
283 layout->AddColumnSet(kMediaMenuColumnSetId);
284 menu_column_set->AddPaddingColumn(
285 0, chrome_style::kCheckboxIndent);
286 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0,
287 GridLayout::USE_PREF, 0, 0);
288 menu_column_set->AddPaddingColumn(
289 0, views::kRelatedControlHorizontalSpacing);
290 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
291 GridLayout::USE_PREF, 0, 0);
292
293 int max_menu_label_width = 0;
294 int menu_width = 0;
295 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i(
296 bubble_content.media_menus.begin());
297 i != bubble_content.media_menus.end(); ++i) {
298 if (!bubble_content_empty)
299 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
300 layout->StartRow(0, kMediaMenuColumnSetId);
301
302 views::Label* menu_label =
303 new views::Label(UTF8ToUTF16(i->second.label));
304 menu_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
305 views::MenuButton* menu_button = new views::MenuButton(
306 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, false);
307 menu_button->set_alignment(views::TextButton::ALIGN_LEFT);
308 menu_button->set_border(
309 new views::TextButtonNativeThemeBorder(menu_button));
310 menu_button->set_animate_on_state_change(false);
311 menu_button->SetEnabledColor(SK_ColorBLACK);
Peter Kasting 2013/02/16 02:55:13 Why do we need to set all these? Don't the defaul
no longer working on chromium 2013/02/18 17:20:04 It also works by using the default. I used those s
312 menu_button->SetHighlightColor(SK_ColorBLACK);
313 menu_button->SetHoverColor(SK_ColorBLACK);
314
315 MediaMenuParts* menu_view = new MediaMenuParts(i->first);
316 menu_view->menu_model.reset(new ContentSettingMediaMenuModel(
317 Profile::FromBrowserContext(
318 web_contents_->GetBrowserContext())->GetPrefs(),
319 i->second.default_device.type,
320 content_setting_bubble_model_.get(),
321 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel,
322 base::Unretained(this))));
323 media_menus_[menu_button] = menu_view;
324
325 // Calculate the optimal width for the menu buttons.
Peter Kasting 2013/02/16 02:55:13 Nit: Maybe instead of "optimal width" you could sa
no longer working on chromium 2013/02/18 17:20:04 Done with updating the comment.
326 int menu_preferred_width = GetPreferredMediaMenuWidth(
327 menu_button, menu_view->menu_model.get());
328 menu_width = std::max(menu_width, menu_preferred_width);
329 max_menu_label_width = std::max(max_menu_label_width,
330 menu_label->GetPreferredSize().width());
331 int avaialble_width =
Peter Kasting 2013/02/16 02:55:13 Nit: typo
no longer working on chromium 2013/02/18 17:20:04 Done.
332 layout->GetPreferredSize(this).width() -
333 chrome_style::kCheckboxIndent -
334 max_menu_label_width -
335 2 * views::kRelatedControlHorizontalSpacing;
336 menu_width = menu_width <= kMinMediaMenuButtonWidth ?
Peter Kasting 2013/02/16 02:55:13 Nit: You can write this more simply using std::max
no longer working on chromium 2013/02/18 17:20:04 Done with the suggested change. But just want to
Peter Kasting 2013/02/18 18:23:18 ? You're just restating what I said: Me: "if |ava
no longer working on chromium 2013/02/19 14:37:35 No, I asked the question precisely how we should h
337 kMinMediaMenuButtonWidth :
338 std::min(menu_width, avaialble_width);
339
340 layout->AddView(menu_label);
341 layout->AddView(menu_button);
342
343 bubble_content_empty = false;
344 }
345
346 // Set the optimal width for the menu buttons.
Peter Kasting 2013/02/16 02:55:13 Nit: Perhaps "// Now set all the menus to the widt
no longer working on chromium 2013/02/18 17:20:04 Done.
347 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin();
348 i != media_menus_.end(); ++i) {
349 i->first->set_min_width(menu_width);
350 i->first->set_max_width(menu_width);
351 }
352 }
353
228 gfx::Font domain_font = 354 gfx::Font domain_font =
229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); 355 views::Label().font().DeriveFont(0, gfx::Font::BOLD);
230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( 356 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i(
231 bubble_content.domain_lists.begin()); 357 bubble_content.domain_lists.begin());
232 i != bubble_content.domain_lists.end(); ++i) { 358 i != bubble_content.domain_lists.end(); ++i) {
233 layout->StartRow(0, kSingleColumnSetId); 359 layout->StartRow(0, kSingleColumnSetId);
234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); 360 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title));
235 section_title->SetMultiLine(true); 361 section_title->SetMultiLine(true);
236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); 362 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); 363 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 // CAREFUL: Showing the settings window activates it, which deactivates the 439 // CAREFUL: Showing the settings window activates it, which deactivates the
314 // info bubble, which causes it to close, which deletes us. 440 // info bubble, which causes it to close, which deletes us.
315 return; 441 return;
316 } 442 }
317 443
318 PopupLinks::const_iterator i(popup_links_.find(source)); 444 PopupLinks::const_iterator i(popup_links_.find(source));
319 DCHECK(i != popup_links_.end()); 445 DCHECK(i != popup_links_.end());
320 content_setting_bubble_model_->OnPopupClicked(i->second); 446 content_setting_bubble_model_->OnPopupClicked(i->second);
321 } 447 }
322 448
449 void ContentSettingBubbleContents::OnMenuButtonClicked(
450 views::View* source,
451 const gfx::Point& point) {
452 MediaMenuPartsMap::iterator i(media_menus_.find(
453 static_cast<views::MenuButton*>(source)));
454 DCHECK(i != media_menus_.end());
455
456 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get());
457 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
458
459 gfx::Point screen_location;
460 views::View::ConvertPointToScreen(i->first, &screen_location);
461 ignore_result(menu_runner_->RunMenuAt(
462 source->GetWidget(),
463 i->first,
464 gfx::Rect(screen_location, i->first->size()),
465 views::MenuItemView::TOPLEFT,
466 views::MenuRunner::HAS_MNEMONICS));
467 }
468
323 void ContentSettingBubbleContents::Observe( 469 void ContentSettingBubbleContents::Observe(
324 int type, 470 int type,
325 const content::NotificationSource& source, 471 const content::NotificationSource& source,
326 const content::NotificationDetails& details) { 472 const content::NotificationDetails& details) {
327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); 473 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type);
328 DCHECK(source == content::Source<WebContents>(web_contents_)); 474 DCHECK(source == content::Source<WebContents>(web_contents_));
329 web_contents_ = NULL; 475 web_contents_ = NULL;
330 } 476 }
477
478 int ContentSettingBubbleContents::GetPreferredMediaMenuWidth(
479 views::MenuButton* button, ui::SimpleMenuModel* menu_model) {
Peter Kasting 2013/02/16 02:55:13 Nit: One arg per line
no longer working on chromium 2013/02/18 17:20:04 Done.
480 // Store the current title for the button.
Peter Kasting 2013/02/16 02:55:13 Nit: The comment here and below are unnecessary, t
no longer working on chromium 2013/02/18 17:20:04 Done.
481 string16 title = button->text();
482
483 int width = button->GetPreferredSize().width();
484 for (int i = 0; i < menu_model->GetItemCount(); ++i) {
485 button->SetText(menu_model->GetLabelAt(i));
486 int item_width = button->GetPreferredSize().width();
487 width = width > item_width ? width : item_width;
Peter Kasting 2013/02/16 02:55:13 Nit: Simpler: width = std::max(width, button->G
no longer working on chromium 2013/02/18 17:20:04 Done.
488 }
489
490 // Recover the title for the menu button.
491 button->SetText(title);
492 return width;
493 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698