Home | History | Annotate | Download | only in location_bar
      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.
      4 
      5 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
      6 
      7 #include "chrome/browser/ui/toolbar/toolbar_model.h"
      8 #include "chrome/browser/ui/view_ids.h"
      9 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
     10 #include "chrome/browser/ui/zoom/zoom_controller.h"
     11 #include "chrome/grit/generated_resources.h"
     12 #include "ui/accessibility/ax_view_state.h"
     13 #include "ui/base/l10n/l10n_util.h"
     14 #include "ui/base/resource/resource_bundle.h"
     15 #include "ui/events/event.h"
     16 #include "ui/gfx/size.h"
     17 
     18 ZoomView::ZoomView(LocationBarView::Delegate* location_bar_delegate)
     19     : location_bar_delegate_(location_bar_delegate) {
     20   SetAccessibilityFocusable(true);
     21   Update(NULL);
     22 }
     23 
     24 ZoomView::~ZoomView() {
     25 }
     26 
     27 void ZoomView::Update(ZoomController* zoom_controller) {
     28   if (!zoom_controller || zoom_controller->IsAtDefaultZoom() ||
     29       location_bar_delegate_->GetToolbarModel()->input_in_progress()) {
     30     SetVisible(false);
     31     ZoomBubbleView::CloseBubble();
     32     return;
     33   }
     34 
     35   SetTooltipText(l10n_util::GetStringFUTF16Int(
     36       IDS_TOOLTIP_ZOOM, zoom_controller->GetZoomPercent()));
     37   SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
     38       zoom_controller->GetResourceForZoomLevel()));
     39   SetVisible(true);
     40 }
     41 
     42 void ZoomView::GetAccessibleState(ui::AXViewState* state) {
     43   state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_ZOOM);
     44   state->role = ui::AX_ROLE_BUTTON;
     45 }
     46 
     47 bool ZoomView::GetTooltipText(const gfx::Point& p,
     48                               base::string16* tooltip) const {
     49   // Don't show tooltip if the zoom bubble is displayed.
     50   return !ZoomBubbleView::IsShowing() && ImageView::GetTooltipText(p, tooltip);
     51 }
     52 
     53 bool ZoomView::OnMousePressed(const ui::MouseEvent& event) {
     54   // Do nothing until mouse is released.
     55   return true;
     56 }
     57 
     58 void ZoomView::OnMouseReleased(const ui::MouseEvent& event) {
     59   if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location()))
     60     ActivateBubble();
     61 }
     62 
     63 bool ZoomView::OnKeyPressed(const ui::KeyEvent& event) {
     64   if (event.key_code() != ui::VKEY_SPACE &&
     65       event.key_code() != ui::VKEY_RETURN) {
     66     return false;
     67   }
     68 
     69   ActivateBubble();
     70   return true;
     71 }
     72 
     73 void ZoomView::OnGestureEvent(ui::GestureEvent* event) {
     74   if (event->type() == ui::ET_GESTURE_TAP) {
     75     ActivateBubble();
     76     event->SetHandled();
     77   }
     78 }
     79 
     80 void ZoomView::ActivateBubble() {
     81   ZoomBubbleView::ShowBubble(location_bar_delegate_->GetWebContents(), false);
     82 }
     83