Commit 5b5cc773 authored by Thomas Tangl's avatar Thomas Tangl Committed by Commit Bot

[Dice] Add action handling to accounts submenu in user menu

This CL adds the action handling to the accounts menu added
in https://crrev.com/c/893570.

Bug: 786369
Change-Id: I80a6eb6af477f6a5458dfeb2f2deeb068455760d
Reviewed-on: https://chromium-review.googlesource.com/897325
Commit-Queue: Thomas Tangl <tangltom@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#534042}
parent 1c625ad3
......@@ -15,6 +15,9 @@ namespace {
constexpr int kAvatarIconSize = 16;
// Used to identify the "Use another account" button.
constexpr int kUseAnotherAccountCmdId = std::numeric_limits<int>::max();
gfx::Image SizeAndCircleIcon(const gfx::Image& icon) {
return profiles::GetSizedAvatarIcon(icon, true, kAvatarIconSize,
kAvatarIconSize, profiles::SHAPE_CIRCLE);
......@@ -23,8 +26,11 @@ gfx::Image SizeAndCircleIcon(const gfx::Image& icon) {
} // namespace
DiceAccountsMenu::DiceAccountsMenu(const std::vector<AccountInfo>& accounts,
const std::vector<gfx::Image>& icons)
: menu_(this), accounts_(accounts) {
const std::vector<gfx::Image>& icons,
Callback account_selected_callback)
: menu_(this),
accounts_(accounts),
account_selected_callback_(std::move(account_selected_callback)) {
DCHECK_EQ(accounts.size(), icons.size());
gfx::Image default_icon =
ui::ResourceBundle::GetSharedInstance().GetImageNamed(
......@@ -40,9 +46,9 @@ DiceAccountsMenu::DiceAccountsMenu(const std::vector<AccountInfo>& accounts,
}
// Add the "Use another account" button at the bottom.
menu_.AddItem(
accounts.size(),
kUseAnotherAccountCmdId,
l10n_util::GetStringUTF16(IDS_PROFILES_DICE_USE_ANOTHER_ACCOUNT_BUTTON));
menu_.SetIcon(menu_.GetIndexOfCommandId(accounts.size()),
menu_.SetIcon(menu_.GetIndexOfCommandId(kUseAnotherAccountCmdId),
SizeAndCircleIcon(default_icon));
menu_.AddSeparator(ui::SPACING_SEPARATOR);
}
......@@ -67,5 +73,10 @@ bool DiceAccountsMenu::IsCommandIdEnabled(int command_id) const {
}
void DiceAccountsMenu::ExecuteCommand(int id, int event_flags) {
NOTIMPLEMENTED() << "Selected id: " << id;
DCHECK((0 <= id && static_cast<size_t>(id) < accounts_.size()) ||
id == kUseAnotherAccountCmdId);
base::Optional<AccountInfo> account;
if (id != kUseAnotherAccountCmdId)
account = accounts_[id];
std::move(account_selected_callback_).Run(account);
}
......@@ -7,6 +7,8 @@
#include <vector>
#include "base/callback_forward.h"
#include "base/optional.h"
#include "components/signin/core/browser/account_info.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image.h"
......@@ -16,17 +18,23 @@ namespace views {
class View;
} // namespace views
// Accounts menu class used to select an account for turning on Sync when DICE
// is enabled.
// TODO(tangltom): Add action handling.
// Menu allowing the user to select an account for enabling Sync when desktop
// identity consistency (aka DICE) is enabled.
class DiceAccountsMenu : public ui::SimpleMenuModel::Delegate {
public:
// Callback to enable Sync for |account| if available. |account| corresponds
// to the account that was selected or |nullopt| if the "Use another account"
// button was selected.
using Callback =
base::OnceCallback<void(const base::Optional<AccountInfo>& account)>;
// Builds the accounts menu. Each account from |accounts| is placed in a menu
// item showing the email and the corresponding icon from |icons|. The last
// item in the accounts menu is the "Use another accounts" button. Separators
// are added at the top, bottom and between each item to increase the spacing.
DiceAccountsMenu(const std::vector<AccountInfo>& accounts,
const std::vector<gfx::Image>& icons);
const std::vector<gfx::Image>& icons,
Callback account_selected_callback);
~DiceAccountsMenu() override;
// Shows the accounts menu below |anchor_view|. This method can only be called
......@@ -44,6 +52,8 @@ class DiceAccountsMenu : public ui::SimpleMenuModel::Delegate {
std::vector<AccountInfo> accounts_;
Callback account_selected_callback_;
DISALLOW_COPY_AND_ASSIGN(DiceAccountsMenu);
};
......
......@@ -672,8 +672,12 @@ void ProfileChooserView::ButtonPressed(views::Button* sender,
// Display a submenu listing the GAIA web accounts (without the first one).
std::vector<AccountInfo> accounts(dice_sync_promo_accounts_.begin() + 1,
dice_sync_promo_accounts_.end());
// Using base::Unretained(this) is safe here because |dice_accounts_menu_|
// is owned by |ProfileChooserView|, i.e. |this|.
dice_accounts_menu_ = std::make_unique<DiceAccountsMenu>(
accounts, GetImagesForAccounts(accounts, browser_->profile()));
accounts, GetImagesForAccounts(accounts, browser_->profile()),
base::BindOnce(&ProfileChooserView::EnableSync,
base::Unretained(this)));
dice_accounts_menu_->Show(sender);
} else {
// Either one of the "other profiles", or one of the profile accounts
......@@ -1420,3 +1424,12 @@ void ProfileChooserView::PostActionPerformed(
ProfileMetrics::LogProfileDesktopMenu(action_performed, gaia_service_type_);
gaia_service_type_ = signin::GAIA_SERVICE_TYPE_NONE;
}
void ProfileChooserView::EnableSync(
const base::Optional<AccountInfo>& account) {
Hide();
if (account)
signin_ui_util::EnableSync(browser_, account.value(), access_point_);
else
ShowViewFromMode(profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN);
}
......@@ -177,6 +177,9 @@ class ProfileChooserView : public content::WebContentsDelegate,
// Clean-up done after an action was performed in the ProfileChooser.
void PostActionPerformed(ProfileMetrics::ProfileDesktopMenu action_performed);
// Callback for DiceAccountsMenu.
void EnableSync(const base::Optional<AccountInfo>& account);
std::unique_ptr<AvatarMenu> avatar_menu_;
Browser* const browser_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment