Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | 12 #include "base/bind.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/content_settings/host_content_settings_map.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 16 #include "chrome/browser/plugins/plugin_finder.h" | 16 #include "chrome/browser/plugins/plugin_finder.h" |
| 17 #include "chrome/browser/plugins/plugin_metadata.h" | 17 #include "chrome/browser/plugins/plugin_metadata.h" |
| 18 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" | 18 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" |
| 19 #include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h" | 19 #include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h" |
| 20 #include "chrome/browser/ui/views/browser_dialogs.h" | 20 #include "chrome/browser/ui/views/browser_dialogs.h" |
| 21 #include "content/public/browser/plugin_service.h" | 21 #include "content/public/browser/plugin_service.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 24 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/models/simple_menu_model.h" | 26 #include "ui/base/models/simple_menu_model.h" |
| 27 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "ui/gfx/font_list.h" | 28 #include "ui/gfx/font_list.h" |
| 29 #include "ui/gfx/text_utils.h" | |
| 29 #include "ui/views/controls/button/label_button.h" | 30 #include "ui/views/controls/button/label_button.h" |
| 30 #include "ui/views/controls/button/menu_button.h" | 31 #include "ui/views/controls/button/menu_button.h" |
| 31 #include "ui/views/controls/button/radio_button.h" | 32 #include "ui/views/controls/button/radio_button.h" |
| 32 #include "ui/views/controls/image_view.h" | 33 #include "ui/views/controls/image_view.h" |
| 33 #include "ui/views/controls/label.h" | 34 #include "ui/views/controls/label.h" |
| 34 #include "ui/views/controls/link.h" | 35 #include "ui/views/controls/link.h" |
| 35 #include "ui/views/controls/menu/menu.h" | 36 #include "ui/views/controls/menu/menu.h" |
| 37 #include "ui/views/controls/menu/menu_config.h" | |
| 36 #include "ui/views/controls/menu/menu_runner.h" | 38 #include "ui/views/controls/menu/menu_runner.h" |
| 37 #include "ui/views/controls/separator.h" | 39 #include "ui/views/controls/separator.h" |
| 38 #include "ui/views/layout/grid_layout.h" | 40 #include "ui/views/layout/grid_layout.h" |
| 39 #include "ui/views/layout/layout_constants.h" | 41 #include "ui/views/layout/layout_constants.h" |
| 40 | 42 |
| 41 #if defined(USE_AURA) | 43 #if defined(USE_AURA) |
| 42 #include "ui/base/cursor/cursor.h" | 44 #include "ui/base/cursor/cursor.h" |
| 43 #endif | 45 #endif |
| 44 | 46 |
| 45 namespace { | 47 namespace { |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 views::ColumnSet* menu_column_set = | 286 views::ColumnSet* menu_column_set = |
| 285 layout->AddColumnSet(kMediaMenuColumnSetId); | 287 layout->AddColumnSet(kMediaMenuColumnSetId); |
| 286 menu_column_set->AddPaddingColumn(0, views::kCheckboxIndent); | 288 menu_column_set->AddPaddingColumn(0, views::kCheckboxIndent); |
| 287 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, | 289 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, |
| 288 GridLayout::USE_PREF, 0, 0); | 290 GridLayout::USE_PREF, 0, 0); |
| 289 menu_column_set->AddPaddingColumn( | 291 menu_column_set->AddPaddingColumn( |
| 290 0, views::kRelatedControlHorizontalSpacing); | 292 0, views::kRelatedControlHorizontalSpacing); |
| 291 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, | 293 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, |
| 292 GridLayout::USE_PREF, 0, 0); | 294 GridLayout::USE_PREF, 0, 0); |
| 293 | 295 |
| 294 int menu_width = 0; | |
| 295 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i( | 296 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i( |
| 296 bubble_content.media_menus.begin()); | 297 bubble_content.media_menus.begin()); |
| 297 i != bubble_content.media_menus.end(); ++i) { | 298 i != bubble_content.media_menus.end(); ++i) { |
| 298 if (!bubble_content_empty) | 299 if (!bubble_content_empty) |
| 299 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 300 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 300 layout->StartRow(0, kMediaMenuColumnSetId); | 301 layout->StartRow(0, kMediaMenuColumnSetId); |
| 301 | 302 |
| 302 views::Label* label = | 303 views::Label* label = |
| 303 new views::Label(base::UTF8ToUTF16(i->second.label)); | 304 new views::Label(base::UTF8ToUTF16(i->second.label)); |
| 304 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 305 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 324 menu_button->SetText( | 325 menu_button->SetText( |
| 325 l10n_util::GetStringUTF16(IDS_MEDIA_MENU_NO_DEVICE_TITLE)); | 326 l10n_util::GetStringUTF16(IDS_MEDIA_MENU_NO_DEVICE_TITLE)); |
| 326 menu_button->SetEnabled(false); | 327 menu_button->SetEnabled(false); |
| 327 } | 328 } |
| 328 | 329 |
| 329 // Disable the device selection when the website is managing the devices | 330 // Disable the device selection when the website is managing the devices |
| 330 // itself. | 331 // itself. |
| 331 if (i->second.disabled) | 332 if (i->second.disabled) |
| 332 menu_button->SetEnabled(false); | 333 menu_button->SetEnabled(false); |
| 333 | 334 |
| 334 // Use the longest width of the menus as the width of the menu buttons. | |
| 335 menu_width = std::max(menu_width, | |
| 336 GetPreferredMediaMenuWidth( | |
| 337 menu_button, menu_view->menu_model.get())); | |
| 338 | |
| 339 layout->AddView(label); | 335 layout->AddView(label); |
| 340 layout->AddView(menu_button); | 336 layout->AddView(menu_button); |
| 341 | 337 |
| 342 bubble_content_empty = false; | 338 bubble_content_empty = false; |
| 343 } | 339 } |
| 340 } | |
| 344 | 341 |
| 345 // Make sure the width is at least kMinMediaMenuButtonWidth. The | 342 UpdateMenuButtonSizes(GetNativeTheme()); |
| 346 // maximum width will be clamped by kMaxContentsWidth of the view. | |
| 347 menu_width = std::max(kMinMediaMenuButtonWidth, menu_width); | |
| 348 | |
| 349 // Set all the menu buttons to the width we calculated above. | |
| 350 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin(); | |
| 351 i != media_menus_.end(); ++i) { | |
| 352 i->first->set_min_size(gfx::Size(menu_width, 0)); | |
| 353 i->first->set_max_size(gfx::Size(menu_width, 0)); | |
| 354 } | |
| 355 } | |
| 356 | 343 |
| 357 const gfx::FontList& domain_font = | 344 const gfx::FontList& domain_font = |
| 358 ui::ResourceBundle::GetSharedInstance().GetFontList( | 345 ui::ResourceBundle::GetSharedInstance().GetFontList( |
| 359 ui::ResourceBundle::BoldFont); | 346 ui::ResourceBundle::BoldFont); |
| 360 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( | 347 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( |
| 361 bubble_content.domain_lists.begin()); | 348 bubble_content.domain_lists.begin()); |
| 362 i != bubble_content.domain_lists.end(); ++i) { | 349 i != bubble_content.domain_lists.end(); ++i) { |
| 363 layout->StartRow(0, kSingleColumnSetId); | 350 layout->StartRow(0, kSingleColumnSetId); |
| 364 views::Label* section_title = new views::Label(base::UTF8ToUTF16(i->title)); | 351 views::Label* section_title = new views::Label(base::UTF8ToUTF16(i->title)); |
| 365 section_title->SetMultiLine(true); | 352 section_title->SetMultiLine(true); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 | 404 |
| 418 void ContentSettingBubbleContents::DidNavigateMainFrame( | 405 void ContentSettingBubbleContents::DidNavigateMainFrame( |
| 419 const content::LoadCommittedDetails& details, | 406 const content::LoadCommittedDetails& details, |
| 420 const content::FrameNavigateParams& params) { | 407 const content::FrameNavigateParams& params) { |
| 421 // Content settings are based on the main frame, so if it switches then | 408 // Content settings are based on the main frame, so if it switches then |
| 422 // close up shop. | 409 // close up shop. |
| 423 content_setting_bubble_model_->OnDoneClicked(); | 410 content_setting_bubble_model_->OnDoneClicked(); |
| 424 GetWidget()->Close(); | 411 GetWidget()->Close(); |
| 425 } | 412 } |
| 426 | 413 |
| 414 void ContentSettingBubbleContents::OnNativeThemeChanged( | |
| 415 const ui::NativeTheme* theme) { | |
| 416 views::BubbleDelegateView::OnNativeThemeChanged(theme); | |
| 417 UpdateMenuButtonSizes(theme); | |
| 418 } | |
| 419 | |
| 427 void ContentSettingBubbleContents::ButtonPressed(views::Button* sender, | 420 void ContentSettingBubbleContents::ButtonPressed(views::Button* sender, |
| 428 const ui::Event& event) { | 421 const ui::Event& event) { |
| 429 RadioGroup::const_iterator i( | 422 RadioGroup::const_iterator i( |
| 430 std::find(radio_group_.begin(), radio_group_.end(), sender)); | 423 std::find(radio_group_.begin(), radio_group_.end(), sender)); |
| 431 if (i != radio_group_.end()) { | 424 if (i != radio_group_.end()) { |
| 432 content_setting_bubble_model_->OnRadioClicked(i - radio_group_.begin()); | 425 content_setting_bubble_model_->OnRadioClicked(i - radio_group_.begin()); |
| 433 return; | 426 return; |
| 434 } | 427 } |
| 435 DCHECK_EQ(sender, close_button_); | 428 DCHECK_EQ(sender, close_button_); |
| 436 content_setting_bubble_model_->OnDoneClicked(); | 429 content_setting_bubble_model_->OnDoneClicked(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 views::View::ConvertPointToScreen(j->first, &screen_location); | 467 views::View::ConvertPointToScreen(j->first, &screen_location); |
| 475 ignore_result( | 468 ignore_result( |
| 476 menu_runner_->RunMenuAt(source->GetWidget(), | 469 menu_runner_->RunMenuAt(source->GetWidget(), |
| 477 j->first, | 470 j->first, |
| 478 gfx::Rect(screen_location, j->first->size()), | 471 gfx::Rect(screen_location, j->first->size()), |
| 479 views::MENU_ANCHOR_TOPLEFT, | 472 views::MENU_ANCHOR_TOPLEFT, |
| 480 ui::MENU_SOURCE_NONE, | 473 ui::MENU_SOURCE_NONE, |
| 481 views::MenuRunner::HAS_MNEMONICS)); | 474 views::MenuRunner::HAS_MNEMONICS)); |
| 482 } | 475 } |
| 483 | 476 |
| 484 int ContentSettingBubbleContents::GetPreferredMediaMenuWidth( | 477 void ContentSettingBubbleContents::UpdateMenuButtonSizes( |
| 485 views::MenuButton* button, | 478 const ui::NativeTheme* theme) { |
| 486 ui::SimpleMenuModel* menu_model) { | 479 const views::MenuConfig config = views::MenuConfig(theme); |
| 487 base::string16 title = button->GetText(); | 480 const int margins = config.item_left_margin + config.check_width + |
| 481 config.label_to_arrow_padding + config.arrow_width + | |
| 482 config.arrow_to_edge_padding; | |
| 488 | 483 |
| 489 int width = button->GetPreferredSize().width(); | 484 // The preferred media menu size sort of copies the logic in |
| 490 for (int i = 0; i < menu_model->GetItemCount(); ++i) { | 485 // MenuItemView::CalculateDimensions(). When this was using TextButton, it |
| 491 button->SetText(menu_model->GetLabelAt(i)); | 486 // completely coincidentally matched the logic in MenuItemView. We now need |
| 492 width = std::max(width, button->GetPreferredSize().width()); | 487 // to redo this manually. |
| 488 int menu_width = 0; | |
| 489 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin(); | |
| 490 i != media_menus_.end(); ++i) { | |
| 491 for (int j = 0; j < i->second->menu_model->GetItemCount(); ++j) { | |
| 492 int string_width = gfx::GetStringWidth( | |
| 493 i->second->menu_model->GetLabelAt(j), | |
| 494 config.font_list); | |
| 495 | |
| 496 menu_width = std::max(menu_width, string_width + margins); | |
| 497 } | |
| 493 } | 498 } |
| 494 | 499 |
| 495 // Recover the title for the menu button. | 500 // Make sure the width is at least kMinMediaMenuButtonWidth. The |
| 496 button->SetText(title); | 501 // maximum width will be clamped by kMaxContentsWidth of the view. |
| 497 return width; | 502 menu_width = std::max(kMinMediaMenuButtonWidth, menu_width); |
|
msw
2014/07/15 02:46:37
nit: just add |margins| to |menu_width| once here
| |
| 503 | |
| 504 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin(); | |
| 505 i != media_menus_.end(); ++i) { | |
| 506 i->first->set_min_size(gfx::Size(menu_width, 0)); | |
|
msw
2014/07/15 02:46:37
nit: fix indent
| |
| 507 i->first->set_max_size(gfx::Size(menu_width, 0)); | |
| 508 } | |
| 498 } | 509 } |
| OLD | NEW |