This source file includes following definitions.
- CommandIdToContentSetting
- menu_model_
- GetPreferredSize
- SetText
- GetAccessibleState
- OnMenuButtonClicked
- current_setting_
- AddObserver
- ChildPreferredSizeChanged
- ExecuteCommand
- IsCommandIdChecked
#include "chrome/browser/ui/views/website_settings/permission_selector_view.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/website_settings/website_settings_ui.h"
#include "grit/generated_resources.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace {
const int kPermissionIconMarginLeft = 6;
const int kPermissionIconColumnWidth = 20;
ContentSetting CommandIdToContentSetting(int command_id) {
switch (command_id) {
case PermissionMenuModel::COMMAND_SET_TO_DEFAULT:
return CONTENT_SETTING_DEFAULT;
case PermissionMenuModel::COMMAND_SET_TO_ALLOW:
return CONTENT_SETTING_ALLOW;
case PermissionMenuModel::COMMAND_SET_TO_BLOCK:
return CONTENT_SETTING_BLOCK;
default:
NOTREACHED();
return CONTENT_SETTING_DEFAULT;
}
}
}
namespace internal {
class PermissionMenuButton : public views::MenuButton,
public views::MenuButtonListener {
public:
PermissionMenuButton(const base::string16& text,
PermissionMenuModel* model,
bool show_menu_marker);
virtual ~PermissionMenuButton();
virtual gfx::Size GetPreferredSize() OVERRIDE;
virtual void SetText(const base::string16& text) OVERRIDE;
virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
private:
virtual void OnMenuButtonClicked(View* source,
const gfx::Point& point) OVERRIDE;
PermissionMenuModel* menu_model_;
scoped_ptr<views::MenuRunner> menu_runner_;
DISALLOW_COPY_AND_ASSIGN(PermissionMenuButton);
};
PermissionMenuButton::PermissionMenuButton(const base::string16& text,
PermissionMenuModel* model,
bool show_menu_marker)
: MenuButton(NULL, text, this, show_menu_marker),
menu_model_(model) {
SetEnabledColor(GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_LabelEnabledColor));
SetHoverColor(GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_LabelEnabledColor));
SetDisabledColor(GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_LabelDisabledColor));
}
PermissionMenuButton::~PermissionMenuButton() {
}
gfx::Size PermissionMenuButton::GetPreferredSize() {
gfx::Insets insets = GetInsets();
gfx::Size prefsize(text_size_.width() + insets.width(),
text_size_.height() + insets.height());
if (max_width_ > 0)
prefsize.set_width(std::min(max_width_, prefsize.width()));
if (show_menu_marker()) {
prefsize.Enlarge(menu_marker()->width() +
views::MenuButton::kMenuMarkerPaddingLeft +
views::MenuButton::kMenuMarkerPaddingRight,
0);
}
return prefsize;
}
void PermissionMenuButton::SetText(const base::string16& text) {
MenuButton::SetText(text);
SizeToPreferredSize();
}
void PermissionMenuButton::GetAccessibleState(ui::AXViewState* state) {
MenuButton::GetAccessibleState(state);
state->value = text();
}
void PermissionMenuButton::OnMenuButtonClicked(View* source,
const gfx::Point& point) {
menu_runner_.reset(new views::MenuRunner(menu_model_));
gfx::Point p(point);
p.Offset(-source->width(), 0);
if (menu_runner_->RunMenuAt(
source->GetWidget()->GetTopLevelWidget(),
this,
gfx::Rect(p, gfx::Size()),
views::MenuItemView::TOPLEFT,
ui::MENU_SOURCE_NONE,
views::MenuRunner::HAS_MNEMONICS) == views::MenuRunner::MENU_DELETED)
return;
}
}
PermissionSelectorView::PermissionSelectorView(
const GURL& url,
ContentSettingsType type,
ContentSetting default_setting,
ContentSetting current_setting,
content_settings::SettingSource source)
: icon_(NULL),
menu_button_(NULL),
type_(type),
default_setting_(default_setting),
current_setting_(current_setting) {
views::GridLayout* layout = new views::GridLayout(this);
SetLayoutManager(layout);
const int column_set_id = 0;
views::ColumnSet* column_set = layout->AddColumnSet(column_set_id);
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::FILL,
1,
views::GridLayout::FIXED,
kPermissionIconColumnWidth,
0);
column_set->AddPaddingColumn(0, kPermissionIconMarginLeft);
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::FILL,
1,
views::GridLayout::USE_PREF,
0,
0);
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::FILL,
1,
views::GridLayout::USE_PREF,
0,
0);
layout->StartRow(1, column_set_id);
icon_ = new views::ImageView();
ContentSetting setting = current_setting;
if (setting == CONTENT_SETTING_DEFAULT)
setting = default_setting;
const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(type, setting);
icon_->SetImage(image.ToImageSkia());
layout->AddView(icon_,
1,
1,
views::GridLayout::CENTER,
views::GridLayout::CENTER);
views::Label* label = new views::Label(
l10n_util::GetStringFUTF16(
IDS_WEBSITE_SETTINGS_PERMISSION_TYPE,
WebsiteSettingsUI::PermissionTypeToUIString(type)));
layout->AddView(label,
1,
1,
views::GridLayout::LEADING,
views::GridLayout::CENTER);
menu_button_model_.reset(new PermissionMenuModel(
this, url, type, default_setting, current_setting));
bool button_enabled = source == content_settings::SETTING_SOURCE_USER;
menu_button_ = new internal::PermissionMenuButton(
WebsiteSettingsUI::PermissionActionToUIString(current_setting,
default_setting,
source),
menu_button_model_.get(),
button_enabled);
menu_button_->SetEnabled(button_enabled);
menu_button_->SetFocusable(button_enabled);
menu_button_->SetAccessibleName(
WebsiteSettingsUI::PermissionTypeToUIString(type));
layout->AddView(menu_button_);
}
void PermissionSelectorView::AddObserver(
PermissionSelectorViewObserver* observer) {
observer_list_.AddObserver(observer);
}
void PermissionSelectorView::ChildPreferredSizeChanged(View* child) {
SizeToPreferredSize();
parent()->SizeToPreferredSize();
}
PermissionSelectorView::~PermissionSelectorView() {
}
void PermissionSelectorView::ExecuteCommand(int command_id) {
current_setting_ = CommandIdToContentSetting(command_id);
ContentSetting effective_setting = current_setting_;
if (effective_setting == CONTENT_SETTING_DEFAULT)
effective_setting = default_setting_;
const gfx::Image& image = WebsiteSettingsUI::GetPermissionIcon(
type_, effective_setting);
icon_->SetImage(image.ToImageSkia());
menu_button_->SetText(WebsiteSettingsUI::PermissionActionToUIString(
current_setting_,
default_setting_,
content_settings::SETTING_SOURCE_USER));
FOR_EACH_OBSERVER(PermissionSelectorViewObserver,
observer_list_,
OnPermissionChanged(this));
}
bool PermissionSelectorView::IsCommandIdChecked(int command_id) {
return current_setting_ == CommandIdToContentSetting(command_id);
}