[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / zoom_decoration.mm
blob67e358bb79e331db242ec419f92633532500e929
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.
5 #import "chrome/browser/ui/cocoa/location_bar/zoom_decoration.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
12 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
13 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
14 #include "chrome/browser/ui/zoom/zoom_controller.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
18 ZoomDecoration::ZoomDecoration(LocationBarViewMac* owner)
19     : owner_(owner),
20       bubble_(nil) {
23 ZoomDecoration::~ZoomDecoration() {
24   [bubble_ closeWithoutAnimation];
27 void ZoomDecoration::Update(ZoomController* zoom_controller) {
28   if (!ShouldShowDecoration()) {
29     [bubble_ close];
30     SetVisible(false);
31     return;
32   }
34   SetImage(OmniboxViewMac::ImageForResource(
35       zoom_controller->GetResourceForZoomLevel()));
37   base::string16 zoom_percent =
38       base::IntToString16(zoom_controller->zoom_percent());
39   NSString* zoom_string =
40       l10n_util::GetNSStringFWithFixup(IDS_TOOLTIP_ZOOM, zoom_percent);
41   tooltip_.reset([zoom_string retain]);
43   SetVisible(true);
44   [bubble_ onZoomChanged];
47 void ZoomDecoration::ShowBubble(BOOL auto_close) {
48   if (bubble_)
49     return;
51   content::WebContents* web_contents = owner_->GetWebContents();
52   if (!web_contents)
53     return;
55   // Get the frame of the decoration.
56   AutocompleteTextField* field = owner_->GetAutocompleteTextField();
57   const NSRect frame =
58       [[field cell] frameForDecoration:this inFrame:[field bounds]];
60   // Find point for bubble's arrow in screen coordinates.
61   NSPoint anchor = GetBubblePointInFrame(frame);
62   anchor = [field convertPoint:anchor toView:nil];
63   anchor = [[field window] convertBaseToScreen:anchor];
65   bubble_ = [[ZoomBubbleController alloc] initWithParentWindow:[field window]
66                                                       delegate:this];
67   [bubble_ showAnchoredAt:anchor autoClose:auto_close];
70 void ZoomDecoration::CloseBubble() {
71   [bubble_ close];
74 NSPoint ZoomDecoration::GetBubblePointInFrame(NSRect frame) {
75   return NSMakePoint(NSMaxX(frame), NSMaxY(frame));
78 bool ZoomDecoration::IsAtDefaultZoom() const {
79   content::WebContents* web_contents = owner_->GetWebContents();
80   if (!web_contents)
81     return false;
83   ZoomController* zoomController =
84       ZoomController::FromWebContents(web_contents);
85   return zoomController && zoomController->IsAtDefaultZoom();
88 bool ZoomDecoration::ShouldShowDecoration() const {
89   return owner_->GetWebContents() != NULL &&
90       !owner_->GetToolbarModel()->input_in_progress() &&
91       (bubble_ || !IsAtDefaultZoom());
94 bool ZoomDecoration::AcceptsMousePress() {
95   return true;
98 bool ZoomDecoration::OnMousePressed(NSRect frame, NSPoint location) {
99   if (bubble_)
100     CloseBubble();
101   else
102     ShowBubble(NO);
103   return true;
106 NSString* ZoomDecoration::GetToolTip() {
107   return tooltip_.get();
110 content::WebContents* ZoomDecoration::GetWebContents() {
111   return owner_->GetWebContents();
114 void ZoomDecoration::OnClose() {
115   bubble_ = nil;
117   // If the page is at default zoom then hiding the zoom decoration
118   // was suppressed while the bubble was open. Now that the bubble is
119   // closed the decoration can be hidden.
120   if (IsAtDefaultZoom() && IsVisible()) {
121     SetVisible(false);
122     owner_->OnDecorationsChanged();
123   }