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/website_settings/permission_selector_row.h" | 5 #include "chrome/browser/ui/views/website_settings/permission_selector_row.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h " | 10 #include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h " |
| 11 #include "chrome/browser/ui/website_settings/permission_menu_model.h" | 11 #include "chrome/browser/ui/website_settings/permission_menu_model.h" |
| 12 #include "chrome/browser/ui/website_settings/website_settings_ui.h" | 12 #include "chrome/browser/ui/website_settings/website_settings_ui.h" |
| 13 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "ui/accessibility/ax_view_state.h" | 14 #include "ui/accessibility/ax_view_state.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/material_design/material_design_controller.h" | 16 #include "ui/base/material_design/material_design_controller.h" |
| 17 #include "ui/base/models/combobox_model.h" | 17 #include "ui/base/models/combobox_model.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | |
| 19 #include "ui/gfx/font_list.h" | |
| 18 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
| 19 #include "ui/views/controls/button/menu_button.h" | 21 #include "ui/views/controls/button/menu_button.h" |
| 20 #include "ui/views/controls/combobox/combobox.h" | 22 #include "ui/views/controls/combobox/combobox.h" |
| 21 #include "ui/views/controls/combobox/combobox_listener.h" | 23 #include "ui/views/controls/combobox/combobox_listener.h" |
| 22 #include "ui/views/controls/image_view.h" | 24 #include "ui/views/controls/image_view.h" |
| 23 #include "ui/views/controls/label.h" | 25 #include "ui/views/controls/label.h" |
| 24 #include "ui/views/controls/menu/menu_runner.h" | 26 #include "ui/views/controls/menu/menu_runner.h" |
| 25 #include "ui/views/layout/grid_layout.h" | 27 #include "ui/views/layout/grid_layout.h" |
| 26 #include "ui/views/view.h" | 28 #include "ui/views/view.h" |
| 27 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
| 28 | 30 |
| 31 namespace { | |
| 32 // Minimum distance between the label and its corresponding menu. | |
| 33 int kMinSeparationBetweenLabelAndMenu = 16; | |
| 34 } | |
| 35 | |
| 29 namespace internal { | 36 namespace internal { |
| 30 | 37 |
| 31 // The |PermissionMenuButton| provides a menu for selecting a setting a | 38 // The |PermissionMenuButton| provides a menu for selecting a setting a |
| 32 // permissions type. | 39 // permissions type. |
| 33 class PermissionMenuButton : public views::MenuButton, | 40 class PermissionMenuButton : public views::MenuButton, |
| 34 public views::MenuButtonListener { | 41 public views::MenuButtonListener { |
| 35 public: | 42 public: |
| 36 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership | 43 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership |
| 37 // of the |model| remains with the caller and is not transfered to the | 44 // of the |model| remains with the caller and is not transfered to the |
| 38 // |PermissionMenuButton|. If the |show_menu_marker| flag is true, then a | 45 // |PermissionMenuButton|. If the |show_menu_marker| flag is true, then a |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 } | 228 } |
| 222 | 229 |
| 223 } // namespace internal | 230 } // namespace internal |
| 224 | 231 |
| 225 /////////////////////////////////////////////////////////////////////////////// | 232 /////////////////////////////////////////////////////////////////////////////// |
| 226 // PermissionSelectorRow | 233 // PermissionSelectorRow |
| 227 /////////////////////////////////////////////////////////////////////////////// | 234 /////////////////////////////////////////////////////////////////////////////// |
| 228 | 235 |
| 229 PermissionSelectorRow::PermissionSelectorRow( | 236 PermissionSelectorRow::PermissionSelectorRow( |
| 230 const GURL& url, | 237 const GURL& url, |
| 231 const WebsiteSettingsUI::PermissionInfo& permission) | 238 const WebsiteSettingsUI::PermissionInfo& permission, |
| 232 : icon_(NULL), menu_button_(NULL), combobox_(NULL) { | 239 views::GridLayout* layout) |
| 233 views::GridLayout* layout = new views::GridLayout(this); | 240 : icon_(nullptr), |
| 234 SetLayoutManager(layout); | 241 menu_button_(nullptr), |
| 235 const int column_set_id = 0; | 242 combobox_(nullptr) { // Create the permission icon. |
|
msw
2016/09/30 00:37:04
nit: comment belongs on next line, right?
lgarron
2016/09/30 05:05:56
Moved.
| |
| 236 views::ColumnSet* column_set = layout->AddColumnSet(column_set_id); | |
| 237 column_set->AddColumn(views::GridLayout::FILL, | |
| 238 views::GridLayout::FILL, | |
| 239 1, | |
| 240 views::GridLayout::FIXED, | |
| 241 kPermissionIconColumnWidth, | |
| 242 0); | |
| 243 column_set->AddPaddingColumn(0, kPermissionIconMarginLeft); | |
| 244 column_set->AddColumn(views::GridLayout::FILL, | |
| 245 views::GridLayout::FILL, | |
| 246 1, | |
| 247 views::GridLayout::USE_PREF, | |
| 248 0, | |
| 249 0); | |
| 250 column_set->AddColumn(views::GridLayout::FILL, | |
| 251 views::GridLayout::FILL, | |
| 252 1, | |
| 253 views::GridLayout::USE_PREF, | |
| 254 0, | |
| 255 0); | |
| 256 | |
| 257 layout->StartRow(1, column_set_id); | |
| 258 // Create the permission icon. | |
| 259 icon_ = new views::ImageView(); | 243 icon_ = new views::ImageView(); |
| 260 const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(permission); | 244 const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(permission); |
| 261 icon_->SetImage(image.ToImageSkia()); | 245 icon_->SetImage(image.ToImageSkia()); |
| 262 layout->AddView(icon_, | 246 layout->AddView(icon_); |
| 263 1, | 247 |
| 264 1, | 248 // Create a wrapper to hold the label and the menu. |
| 265 views::GridLayout::CENTER, | 249 views::View* wrapper = new views::View(); |
| 266 views::GridLayout::CENTER); | 250 views::GridLayout* wrapper_layout = new views::GridLayout(wrapper); |
| 251 wrapper->SetLayoutManager(wrapper_layout); | |
| 252 layout->AddView(wrapper); | |
| 253 | |
| 254 const int column_set_id = 0; | |
| 255 views::ColumnSet* column_set = wrapper_layout->AddColumnSet(column_set_id); | |
| 256 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
| 257 views::GridLayout::USE_PREF, 0, 0); | |
| 258 // We set the resize value to 1 to absorb all extra space. | |
| 259 // This keep the subviews aligned at their respective ends. | |
| 260 column_set->AddPaddingColumn(1, kMinSeparationBetweenLabelAndMenu); | |
| 261 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
| 262 views::GridLayout::USE_PREF, 0, 0); | |
| 263 wrapper_layout->StartRow(1, column_set_id); | |
| 264 | |
| 267 // Create the label that displays the permission type. | 265 // Create the label that displays the permission type. |
| 268 views::Label* label = new views::Label(l10n_util::GetStringFUTF16( | 266 const gfx::FontList& font_list = |
| 269 IDS_WEBSITE_SETTINGS_PERMISSION_TYPE, | 267 ui::ResourceBundle::GetSharedInstance().GetFontListWithDelta(1); |
| 270 WebsiteSettingsUI::PermissionTypeToUIString(permission.type))); | 268 views::Label* label = new views::Label( |
| 271 layout->AddView(label, | 269 WebsiteSettingsUI::PermissionTypeToUIString(permission.type), font_list); |
| 272 1, | 270 wrapper_layout->AddView(label); |
| 273 1, | 271 |
| 274 views::GridLayout::LEADING, | |
| 275 views::GridLayout::CENTER); | |
| 276 // Create the menu model. | 272 // Create the menu model. |
| 277 menu_model_.reset(new PermissionMenuModel( | 273 menu_model_.reset(new PermissionMenuModel( |
| 278 url, | 274 url, |
| 279 permission, | 275 permission, |
| 280 base::Bind(&PermissionSelectorRow::PermissionChanged, | 276 base::Bind(&PermissionSelectorRow::PermissionChanged, |
| 281 base::Unretained(this)))); | 277 base::Unretained(this)))); |
| 282 | 278 |
| 283 // Create the permission menu button. | 279 // Create the permission menu button. |
| 284 #if defined(OS_MACOSX) | 280 #if defined(OS_MACOSX) |
| 285 bool use_real_combobox = true; | 281 bool use_real_combobox = true; |
| 286 #else | 282 #else |
| 287 bool use_real_combobox = | 283 bool use_real_combobox = |
| 288 ui::MaterialDesignController::IsSecondaryUiMaterial(); | 284 ui::MaterialDesignController::IsSecondaryUiMaterial(); |
| 289 #endif | 285 #endif |
| 290 if (use_real_combobox) | 286 if (use_real_combobox) |
| 291 InitializeComboboxView(layout, permission); | 287 InitializeComboboxView(wrapper_layout, permission); |
| 292 else | 288 else |
| 293 InitializeMenuButtonView(layout, permission); | 289 InitializeMenuButtonView(wrapper_layout, permission); |
| 294 } | 290 } |
| 295 | 291 |
| 296 void PermissionSelectorRow::AddObserver( | 292 void PermissionSelectorRow::AddObserver( |
| 297 PermissionSelectorRowObserver* observer) { | 293 PermissionSelectorRowObserver* observer) { |
| 298 observer_list_.AddObserver(observer); | 294 observer_list_.AddObserver(observer); |
| 299 } | 295 } |
| 300 | 296 |
| 301 void PermissionSelectorRow::ChildPreferredSizeChanged(View* child) { | |
| 302 SizeToPreferredSize(); | |
| 303 // FIXME: The parent is only a plain |View| that is used as a | |
| 304 // container/box/panel. The SizeToPreferredSize method of the parent is | |
| 305 // called here directly in order not to implement a custom |View| class with | |
| 306 // its own implementation of the ChildPreferredSizeChanged method. | |
| 307 parent()->SizeToPreferredSize(); | |
| 308 } | |
| 309 | |
| 310 PermissionSelectorRow::~PermissionSelectorRow() { | 297 PermissionSelectorRow::~PermissionSelectorRow() { |
| 311 // Gross. On paper the Combobox and the ComboboxModelAdapter are both owned by | |
| 312 // this class, but actually, the Combobox is owned by View and will be | |
| 313 // destroyed in ~View(), which runs *after* ~PermissionSelectorRow() is done, | |
| 314 // which means the Combobox gets destroyed after its ComboboxModel, which | |
| 315 // causes an explosion when the Combobox attempts to stop observing the | |
| 316 // ComboboxModel. This hack ensures the Combobox is deleted before its | |
| 317 // ComboboxModel. | |
| 318 // | |
| 319 // Technically, the MenuButton has the same problem, but MenuButton doesn't | |
| 320 // use its model in its destructor. | |
| 321 if (combobox_) | |
| 322 RemoveChildView(combobox_); | |
| 323 } | 298 } |
| 324 | 299 |
| 325 void PermissionSelectorRow::InitializeMenuButtonView( | 300 void PermissionSelectorRow::InitializeMenuButtonView( |
| 326 views::GridLayout* layout, | 301 views::GridLayout* layout, |
| 327 const WebsiteSettingsUI::PermissionInfo& permission) { | 302 const WebsiteSettingsUI::PermissionInfo& permission) { |
| 328 bool button_enabled = | 303 bool button_enabled = |
| 329 permission.source == content_settings::SETTING_SOURCE_USER; | 304 permission.source == content_settings::SETTING_SOURCE_USER; |
| 330 menu_button_ = new internal::PermissionMenuButton( | 305 menu_button_ = new internal::PermissionMenuButton( |
| 331 WebsiteSettingsUI::PermissionActionToUIString( | 306 WebsiteSettingsUI::PermissionActionToUIString( |
| 332 permission.type, permission.setting, permission.default_setting, | 307 permission.type, permission.setting, permission.default_setting, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 menu_button_->SizeToPreferredSize(); | 346 menu_button_->SizeToPreferredSize(); |
| 372 } else if (combobox_) { | 347 } else if (combobox_) { |
| 373 bool use_default = permission.setting == CONTENT_SETTING_DEFAULT; | 348 bool use_default = permission.setting == CONTENT_SETTING_DEFAULT; |
| 374 combobox_->UpdateSelectedIndex(use_default); | 349 combobox_->UpdateSelectedIndex(use_default); |
| 375 } | 350 } |
| 376 | 351 |
| 377 FOR_EACH_OBSERVER(PermissionSelectorRowObserver, | 352 FOR_EACH_OBSERVER(PermissionSelectorRowObserver, |
| 378 observer_list_, | 353 observer_list_, |
| 379 OnPermissionChanged(permission)); | 354 OnPermissionChanged(permission)); |
| 380 } | 355 } |
| OLD | NEW |