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" |
27 #include "ui/views/layout/box_layout.h" | |
msw
2016/09/26 20:47:55
Remove; not used.
lgarron
2016/09/28 21:11:46
Done.
| |
25 #include "ui/views/layout/grid_layout.h" | 28 #include "ui/views/layout/grid_layout.h" |
26 #include "ui/views/view.h" | 29 #include "ui/views/view.h" |
27 #include "ui/views/widget/widget.h" | 30 #include "ui/views/widget/widget.h" |
28 | 31 |
32 namespace { | |
33 // Minimum distance between the label and its corresponding menu. | |
msw
2016/09/26 20:47:55
nit: '... in DIPs.' or similar.
lgarron
2016/09/28 21:11:46
Aren't all layout constants using the same scale?
msw
2016/09/30 00:37:04
Yes, most Views code uses DIPs, so specifying isn'
| |
34 int kMinSeparationBetweenLabelAndMenu = 16; | |
35 } | |
36 | |
29 namespace internal { | 37 namespace internal { |
30 | 38 |
31 // The |PermissionMenuButton| provides a menu for selecting a setting a | 39 // The |PermissionMenuButton| provides a menu for selecting a setting a |
32 // permissions type. | 40 // permissions type. |
33 class PermissionMenuButton : public views::MenuButton, | 41 class PermissionMenuButton : public views::MenuButton, |
34 public views::MenuButtonListener { | 42 public views::MenuButtonListener { |
35 public: | 43 public: |
36 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership | 44 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership |
37 // of the |model| remains with the caller and is not transfered to the | 45 // 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 | 46 // |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 } | 229 } |
222 | 230 |
223 } // namespace internal | 231 } // namespace internal |
224 | 232 |
225 /////////////////////////////////////////////////////////////////////////////// | 233 /////////////////////////////////////////////////////////////////////////////// |
226 // PermissionSelectorRow | 234 // PermissionSelectorRow |
227 /////////////////////////////////////////////////////////////////////////////// | 235 /////////////////////////////////////////////////////////////////////////////// |
228 | 236 |
229 PermissionSelectorRow::PermissionSelectorRow( | 237 PermissionSelectorRow::PermissionSelectorRow( |
230 const GURL& url, | 238 const GURL& url, |
231 const WebsiteSettingsUI::PermissionInfo& permission) | 239 const WebsiteSettingsUI::PermissionInfo& permission, |
232 : icon_(NULL), menu_button_(NULL), combobox_(NULL) { | 240 views::GridLayout* layout) |
233 views::GridLayout* layout = new views::GridLayout(this); | 241 : icon_(NULL), |
msw
2016/09/26 20:47:55
nit: nullptr here and elsewhere
lgarron
2016/09/28 21:11:46
Done.
| |
234 SetLayoutManager(layout); | 242 menu_button_(NULL), |
235 const int column_set_id = 0; | 243 combobox_(NULL) { // Create the permission icon. |
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(); | 244 icon_ = new views::ImageView(); |
260 const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(permission); | 245 const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(permission); |
261 icon_->SetImage(image.ToImageSkia()); | 246 icon_->SetImage(image.ToImageSkia()); |
262 layout->AddView(icon_, | 247 layout->AddView(icon_); |
263 1, | 248 |
264 1, | 249 // Create a wrapper to hold the label and the menu. |
265 views::GridLayout::CENTER, | 250 views::View* wrapper = new views::View(); |
msw
2016/09/26 20:47:55
Ditto: it'd be nice to avoid this view, but not ne
lgarron
2016/09/28 21:11:46
I tried hard to avoid this, but it seems to be nec
msw
2016/09/30 00:37:04
Ah, so all rows would share the same width for a g
lgarron
2016/09/30 05:05:55
BoxLayout doesn't support MAIN_AXIS_ALIGNMENT_JUST
| |
266 views::GridLayout::CENTER); | 251 views::GridLayout* wrapper_layout = new views::GridLayout(wrapper); |
252 wrapper->SetLayoutManager(wrapper_layout); | |
253 layout->AddView(wrapper); | |
254 | |
255 const int column_set_id = 0; | |
256 views::ColumnSet* column_set = wrapper_layout->AddColumnSet(column_set_id); | |
257 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
258 views::GridLayout::USE_PREF, 0, 0); | |
259 // We set the resize value to 1 to absorb all extra space. | |
260 // This keep the subviews aligned at their respective ends. | |
261 column_set->AddPaddingColumn(1, kMinSeparationBetweenLabelAndMenu); | |
262 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
263 views::GridLayout::USE_PREF, 0, 0); | |
264 wrapper_layout->StartRow(1, column_set_id); | |
265 | |
267 // Create the label that displays the permission type. | 266 // Create the label that displays the permission type. |
268 views::Label* label = new views::Label(l10n_util::GetStringFUTF16( | 267 const gfx::FontList& font_list = |
269 IDS_WEBSITE_SETTINGS_PERMISSION_TYPE, | 268 ui::ResourceBundle::GetSharedInstance().GetFontListWithDelta(1); |
msw
2016/09/26 20:47:55
Remove string resources if they are no longer used
lgarron
2016/09/28 21:11:46
*woo, another one finally bites the dust*
Done.
|
msw
2016/09/26 20:47:55
Ditto: consider adding constants for text sizes.
|
270 WebsiteSettingsUI::PermissionTypeToUIString(permission.type))); | 269 views::Label* label = new views::Label( |
271 layout->AddView(label, | 270 WebsiteSettingsUI::PermissionTypeToUIString(permission.type), font_list); |
272 1, | 271 wrapper_layout->AddView(label); |
273 1, | 272 |
274 views::GridLayout::LEADING, | |
275 views::GridLayout::CENTER); | |
276 // Create the menu model. | 273 // Create the menu model. |
277 menu_model_.reset(new PermissionMenuModel( | 274 menu_model_.reset(new PermissionMenuModel( |
278 url, | 275 url, |
279 permission, | 276 permission, |
280 base::Bind(&PermissionSelectorRow::PermissionChanged, | 277 base::Bind(&PermissionSelectorRow::PermissionChanged, |
281 base::Unretained(this)))); | 278 base::Unretained(this)))); |
282 | 279 |
283 // Create the permission menu button. | 280 // Create the permission menu button. |
284 #if defined(OS_MACOSX) | 281 #if defined(OS_MACOSX) |
285 bool use_real_combobox = true; | 282 bool use_real_combobox = true; |
286 #else | 283 #else |
287 bool use_real_combobox = | 284 bool use_real_combobox = |
288 ui::MaterialDesignController::IsSecondaryUiMaterial(); | 285 ui::MaterialDesignController::IsSecondaryUiMaterial(); |
289 #endif | 286 #endif |
290 if (use_real_combobox) | 287 if (use_real_combobox) |
291 InitializeComboboxView(layout, permission); | 288 InitializeComboboxView(wrapper_layout, permission); |
292 else | 289 else |
293 InitializeMenuButtonView(layout, permission); | 290 InitializeMenuButtonView(wrapper_layout, permission); |
294 } | 291 } |
295 | 292 |
296 void PermissionSelectorRow::AddObserver( | 293 void PermissionSelectorRow::AddObserver( |
297 PermissionSelectorRowObserver* observer) { | 294 PermissionSelectorRowObserver* observer) { |
298 observer_list_.AddObserver(observer); | 295 observer_list_.AddObserver(observer); |
299 } | 296 } |
300 | 297 |
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() { | 298 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 } | 299 } |
324 | 300 |
325 void PermissionSelectorRow::InitializeMenuButtonView( | 301 void PermissionSelectorRow::InitializeMenuButtonView( |
326 views::GridLayout* layout, | 302 views::GridLayout* layout, |
327 const WebsiteSettingsUI::PermissionInfo& permission) { | 303 const WebsiteSettingsUI::PermissionInfo& permission) { |
328 bool button_enabled = | 304 bool button_enabled = |
329 permission.source == content_settings::SETTING_SOURCE_USER; | 305 permission.source == content_settings::SETTING_SOURCE_USER; |
330 menu_button_ = new internal::PermissionMenuButton( | 306 menu_button_ = new internal::PermissionMenuButton( |
331 WebsiteSettingsUI::PermissionActionToUIString( | 307 WebsiteSettingsUI::PermissionActionToUIString( |
332 permission.type, permission.setting, permission.default_setting, | 308 permission.type, permission.setting, permission.default_setting, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 menu_button_->SizeToPreferredSize(); | 347 menu_button_->SizeToPreferredSize(); |
372 } else if (combobox_) { | 348 } else if (combobox_) { |
373 bool use_default = permission.setting == CONTENT_SETTING_DEFAULT; | 349 bool use_default = permission.setting == CONTENT_SETTING_DEFAULT; |
374 combobox_->UpdateSelectedIndex(use_default); | 350 combobox_->UpdateSelectedIndex(use_default); |
375 } | 351 } |
376 | 352 |
377 FOR_EACH_OBSERVER(PermissionSelectorRowObserver, | 353 FOR_EACH_OBSERVER(PermissionSelectorRowObserver, |
378 observer_list_, | 354 observer_list_, |
379 OnPermissionChanged(permission)); | 355 OnPermissionChanged(permission)); |
380 } | 356 } |
OLD | NEW |