1// Copyright 2013 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.
4
5#include "chrome/browser/ui/views/passwords/manage_passwords_icon_view.h"
6
7#include "chrome/app/chrome_command_ids.h"
8#include "chrome/browser/command_updater.h"
9#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
10#include "chrome/browser/ui/view_ids.h"
11#include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h"
12#include "components/password_manager/core/common/password_manager_ui.h"
13#include "ui/base/l10n/l10n_util.h"
14#include "ui/base/resource/resource_bundle.h"
15
16ManagePasswordsIconView::ManagePasswordsIconView(CommandUpdater* updater)
17    : BubbleIconView(updater, IDC_MANAGE_PASSWORDS_FOR_PAGE) {
18  set_id(VIEW_ID_MANAGE_PASSWORDS_ICON_BUTTON);
19  SetFocusable(true);
20  UpdateVisibleUI();
21}
22
23ManagePasswordsIconView::~ManagePasswordsIconView() {}
24
25void ManagePasswordsIconView::UpdateVisibleUI() {
26  if (state() == password_manager::ui::INACTIVE_STATE) {
27    SetVisible(false);
28    if (ManagePasswordsBubbleView::IsShowing())
29      ManagePasswordsBubbleView::CloseBubble();
30    return;
31  }
32
33  SetVisible(true);
34  SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id_));
35  SetTooltipText(l10n_util::GetStringUTF16(tooltip_text_id_));
36
37  // We may be about to automatically pop up a ManagePasswordsBubbleView.
38  // Force layout of the icon's parent now; the bubble will be incorrectly
39  // positioned otherwise, as the icon won't have been drawn into position.
40  parent()->Layout();
41}
42
43bool ManagePasswordsIconView::IsBubbleShowing() const {
44  // If the bubble is being destroyed, it's considered showing though it may be
45  // already invisible currently.
46  return ManagePasswordsBubbleView::manage_password_bubble() != NULL;
47}
48
49void ManagePasswordsIconView::OnExecuting(
50    BubbleIconView::ExecuteSource source) {
51}
52
53bool ManagePasswordsIconView::OnMousePressed(const ui::MouseEvent& event) {
54  bool result = BubbleIconView::OnMousePressed(event);
55  if (IsBubbleShowing())
56    ManagePasswordsBubbleView::CloseBubble();
57  return result;
58}
59
60bool ManagePasswordsIconView::OnKeyPressed(const ui::KeyEvent& event) {
61  // Space is always ignored because otherwise the bubble appears with the
62  // default button down. Releasing the space is equivalent to clicking this
63  // button.
64  if (event.key_code() == ui::VKEY_SPACE)
65    return true;
66  if (event.key_code() == ui::VKEY_RETURN && active()) {
67    // If the icon is active, it should transfer its focus to the bubble.
68    // If it still somehow got this key event, the bubble shouldn't be reopened.
69    return true;
70  }
71  return BubbleIconView::OnKeyPressed(event);
72}
73
74void ManagePasswordsIconView::AboutToRequestFocusFromTabTraversal(
75    bool reverse) {
76  if (active())
77    ManagePasswordsBubbleView::ActivateBubble();
78}
79