Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / website_settings / permission_selector_view.cc
blob3e110f787e13a4dffda2855578bf755560ec09c3
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/website_settings/permission_menu_model.h"
9 #include "chrome/browser/ui/website_settings/website_settings_ui.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/gfx/image/image.h"
14 #include "ui/views/controls/button/menu_button.h"
15 #include "ui/views/controls/image_view.h"
16 #include "ui/views/controls/label.h"
17 #include "ui/views/controls/menu/menu_runner.h"
18 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/view.h"
20 #include "ui/views/widget/widget.h"
22 namespace {
24 // Left icon margin.
25 const int kPermissionIconMarginLeft = 6;
26 // The width of the column that contains the permissions icons.
27 const int kPermissionIconColumnWidth = 20;
29 } // namespace
31 namespace internal {
33 // The |PermissionMenuButton| provides a menu for selecting a setting a
34 // permissions type.
35 class PermissionMenuButton : public views::MenuButton,
36 public views::MenuButtonListener {
37 public:
38 // Creates a new |PermissionMenuButton| with the passed |text|. The ownership
39 // of the |model| remains with the caller and is not transfered to the
40 // |PermissionMenuButton|. If the |show_menu_marker| flag is true, then a
41 // small icon is be displayed next to the button |text|, indicating that the
42 // button opens a drop down menu.
43 PermissionMenuButton(const base::string16& text,
44 PermissionMenuModel* model,
45 bool show_menu_marker);
46 ~PermissionMenuButton() override;
48 // Overridden from views::LabelButton.
49 void SetText(const base::string16& text) override;
51 // Overridden from views::View.
52 void GetAccessibleState(ui::AXViewState* state) override;
53 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
55 private:
56 // Overridden from views::MenuButtonListener.
57 void OnMenuButtonClicked(View* source, const gfx::Point& point) override;
59 PermissionMenuModel* menu_model_; // Owned by |PermissionSelectorView|.
60 scoped_ptr<views::MenuRunner> menu_runner_;
62 DISALLOW_COPY_AND_ASSIGN(PermissionMenuButton);
65 ///////////////////////////////////////////////////////////////////////////////
66 // PermissionMenuButton
67 ///////////////////////////////////////////////////////////////////////////////
69 PermissionMenuButton::PermissionMenuButton(const base::string16& text,
70 PermissionMenuModel* model,
71 bool show_menu_marker)
72 : MenuButton(NULL, text, this, show_menu_marker),
73 menu_model_(model) {
76 PermissionMenuButton::~PermissionMenuButton() {
79 void PermissionMenuButton::SetText(const base::string16& text) {
80 MenuButton::SetText(text);
81 SizeToPreferredSize();
84 void PermissionMenuButton::GetAccessibleState(ui::AXViewState* state) {
85 MenuButton::GetAccessibleState(state);
86 state->value = GetText();
89 void PermissionMenuButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
90 SetTextColor(views::Button::STATE_NORMAL, GetNativeTheme()->GetSystemColor(
91 ui::NativeTheme::kColorId_LabelEnabledColor));
92 SetTextColor(views::Button::STATE_HOVERED, GetNativeTheme()->GetSystemColor(
93 ui::NativeTheme::kColorId_LabelEnabledColor));
94 SetTextColor(views::Button::STATE_DISABLED, GetNativeTheme()->GetSystemColor(
95 ui::NativeTheme::kColorId_LabelDisabledColor));
98 void PermissionMenuButton::OnMenuButtonClicked(View* source,
99 const gfx::Point& point) {
100 menu_runner_.reset(
101 new views::MenuRunner(menu_model_, views::MenuRunner::HAS_MNEMONICS));
103 gfx::Point p(point);
104 p.Offset(-source->width(), 0);
105 if (menu_runner_->RunMenuAt(source->GetWidget()->GetTopLevelWidget(),
106 this,
107 gfx::Rect(p, gfx::Size()),
108 views::MENU_ANCHOR_TOPLEFT,
109 ui::MENU_SOURCE_NONE) ==
110 views::MenuRunner::MENU_DELETED) {
111 return;
115 } // namespace internal
117 ///////////////////////////////////////////////////////////////////////////////
118 // PermissionSelectorView
119 ///////////////////////////////////////////////////////////////////////////////
121 PermissionSelectorView::PermissionSelectorView(
122 const GURL& url,
123 const WebsiteSettingsUI::PermissionInfo& permission)
124 : icon_(NULL), menu_button_(NULL) {
125 views::GridLayout* layout = new views::GridLayout(this);
126 SetLayoutManager(layout);
127 const int column_set_id = 0;
128 views::ColumnSet* column_set = layout->AddColumnSet(column_set_id);
129 column_set->AddColumn(views::GridLayout::FILL,
130 views::GridLayout::FILL,
132 views::GridLayout::FIXED,
133 kPermissionIconColumnWidth,
135 column_set->AddPaddingColumn(0, kPermissionIconMarginLeft);
136 column_set->AddColumn(views::GridLayout::FILL,
137 views::GridLayout::FILL,
139 views::GridLayout::USE_PREF,
142 column_set->AddColumn(views::GridLayout::FILL,
143 views::GridLayout::FILL,
145 views::GridLayout::USE_PREF,
149 layout->StartRow(1, column_set_id);
150 // Create the permission icon.
151 icon_ = new views::ImageView();
152 const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(permission);
153 icon_->SetImage(image.ToImageSkia());
154 layout->AddView(icon_,
157 views::GridLayout::CENTER,
158 views::GridLayout::CENTER);
159 // Create the label that displays the permission type.
160 views::Label* label = new views::Label(l10n_util::GetStringFUTF16(
161 IDS_WEBSITE_SETTINGS_PERMISSION_TYPE,
162 WebsiteSettingsUI::PermissionTypeToUIString(permission.type)));
163 layout->AddView(label,
166 views::GridLayout::LEADING,
167 views::GridLayout::CENTER);
168 // Create the menu model.
169 menu_model_.reset(new PermissionMenuModel(
170 url,
171 permission,
172 base::Bind(&PermissionSelectorView::PermissionChanged,
173 base::Unretained(this))));
174 // Create the permission menu button.
175 bool button_enabled =
176 permission.source == content_settings::SETTING_SOURCE_USER;
177 menu_button_ = new internal::PermissionMenuButton(
178 WebsiteSettingsUI::PermissionActionToUIString(
179 permission.type, permission.setting, permission.default_setting,
180 permission.source),
181 menu_model_.get(), button_enabled);
182 menu_button_->SetEnabled(button_enabled);
183 menu_button_->SetFocusable(button_enabled);
184 menu_button_->SetAccessibleName(
185 WebsiteSettingsUI::PermissionTypeToUIString(permission.type));
186 layout->AddView(menu_button_);
189 void PermissionSelectorView::AddObserver(
190 PermissionSelectorViewObserver* observer) {
191 observer_list_.AddObserver(observer);
194 void PermissionSelectorView::ChildPreferredSizeChanged(View* child) {
195 SizeToPreferredSize();
196 // FIXME: The parent is only a plain |View| that is used as a
197 // container/box/panel. The SizeToPreferredSize method of the parent is
198 // called here directly in order not to implement a custom |View| class with
199 // its own implementation of the ChildPreferredSizeChanged method.
200 parent()->SizeToPreferredSize();
203 PermissionSelectorView::~PermissionSelectorView() {
206 void PermissionSelectorView::PermissionChanged(
207 const WebsiteSettingsUI::PermissionInfo& permission) {
208 // Change the permission icon to reflect the selected setting.
209 const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(permission);
210 icon_->SetImage(image.ToImageSkia());
212 // Update the menu button text to reflect the new setting.
213 menu_button_->SetText(WebsiteSettingsUI::PermissionActionToUIString(
214 permission.type, permission.setting, permission.default_setting,
215 content_settings::SETTING_SOURCE_USER));
217 FOR_EACH_OBSERVER(PermissionSelectorViewObserver,
218 observer_list_,
219 OnPermissionChanged(permission));