manage_passwords_icon_view.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/browser/ui/views/passwords/manage_passwords_bubble_view.h"
8#include "grit/generated_resources.h"
9#include "grit/theme_resources.h"
10#include "ui/base/accessibility/accessible_view_state.h"
11#include "ui/base/l10n/l10n_util.h"
12#include "ui/base/resource/resource_bundle.h"
13
14ManagePasswordsIconView::ManagePasswordsIconView(
15    LocationBarView::Delegate* location_bar_delegate)
16    : location_bar_delegate_(location_bar_delegate) {
17  set_accessibility_focusable(true);
18  Update(NULL);
19  LocationBarView::InitTouchableLocationBarChildView(this);
20}
21
22ManagePasswordsIconView::~ManagePasswordsIconView() {}
23
24void ManagePasswordsIconView::Update(
25    ManagePasswordsIconController* manage_passwords_icon_controller) {
26  SetVisible(
27      manage_passwords_icon_controller &&
28      manage_passwords_icon_controller->manage_passwords_icon_to_be_shown() &&
29      !location_bar_delegate_->GetToolbarModel()->input_in_progress());
30  if (!visible()) {
31    ManagePasswordsBubbleView::CloseBubble();
32    return;
33  }
34  SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
35      IDR_SAVE_PASSWORD));
36  SetTooltip(manage_passwords_icon_controller->password_to_be_saved());
37}
38
39void ManagePasswordsIconView::ShowBubbleIfNeeded(
40    ManagePasswordsIconController* manage_passwords_icon_controller) {
41  if (manage_passwords_icon_controller->
42          manage_passwords_bubble_needs_showing() &&
43      visible() && !ManagePasswordsBubbleView::IsShowing()) {
44    ManagePasswordsBubbleView::ShowBubble(
45        location_bar_delegate_->GetWebContents(), this);
46    manage_passwords_icon_controller->OnBubbleShown();
47  }
48}
49
50void ManagePasswordsIconView::SetTooltip(bool password_to_be_saved) {
51  SetTooltipText(l10n_util::GetStringUTF16(
52      (password_to_be_saved ?
53          IDS_PASSWORD_MANAGER_TOOLTIP_SAVE :
54          IDS_PASSWORD_MANAGER_TOOLTIP_MANAGE)));
55}
56
57bool ManagePasswordsIconView::GetTooltipText(const gfx::Point& p,
58                                             string16* tooltip) const {
59  // Don't show tooltip if the password bubble is displayed.
60  return !ManagePasswordsBubbleView::IsShowing() &&
61      ImageView::GetTooltipText(p, tooltip);
62}
63
64void ManagePasswordsIconView::OnGestureEvent(ui::GestureEvent* event) {
65  if (event->type() == ui::ET_GESTURE_TAP) {
66    ManagePasswordsBubbleView::ShowBubble(
67        location_bar_delegate_->GetWebContents(), this);
68    event->SetHandled();
69  }
70}
71
72bool ManagePasswordsIconView::OnMousePressed(const ui::MouseEvent& event) {
73  // Do nothing until the mouse button is released.
74  return true;
75}
76
77void ManagePasswordsIconView::OnMouseReleased(const ui::MouseEvent& event) {
78  if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location()))
79    ManagePasswordsBubbleView::ShowBubble(
80        location_bar_delegate_->GetWebContents(), this);
81}
82