Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / zoom_decoration.mm
blob659f2a6e1a8744d6752b300a6add5d3bf94b5c9e
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/grit/generated_resources.h"
15 #include "components/ui/zoom/zoom_controller.h"
16 #include "grit/theme_resources.h"
17 #include "ui/base/l10n/l10n_util_mac.h"
19 ZoomDecoration::ZoomDecoration(LocationBarViewMac* owner)
20     : owner_(owner),
21       bubble_(nil) {
24 ZoomDecoration::~ZoomDecoration() {
25   [bubble_ closeWithoutAnimation];
26   bubble_.delegate = nil;
29 bool ZoomDecoration::UpdateIfNecessary(
30     ui_zoom::ZoomController* zoom_controller, bool default_zoom_changed) {
31   if (!ShouldShowDecoration()) {
32     if (!IsVisible() && !bubble_)
33       return false;
35     HideUI();
36     return true;
37   }
39   base::string16 zoom_percent =
40       base::IntToString16(zoom_controller->GetZoomPercent());
41   NSString* zoom_string =
42       l10n_util::GetNSStringFWithFixup(IDS_TOOLTIP_ZOOM, zoom_percent);
44   if (IsVisible() && [tooltip_ isEqualToString:zoom_string] &&
45       !default_zoom_changed) {
46     return false;
47   }
49   ShowAndUpdateUI(zoom_controller, zoom_string);
50   return true;
53 void ZoomDecoration::ShowBubble(BOOL auto_close) {
54   if (bubble_) {
55     bubble_.delegate = nil;
56     [[bubble_.window parentWindow] removeChildWindow:bubble_.window];
57     [bubble_.window orderOut:nil];
58     [bubble_ closeWithoutAnimation];
59   }
61   content::WebContents* web_contents = owner_->GetWebContents();
62   if (!web_contents)
63     return;
65   // Get the frame of the decoration.
66   AutocompleteTextField* field = owner_->GetAutocompleteTextField();
67   const NSRect frame =
68       [[field cell] frameForDecoration:this inFrame:[field bounds]];
70   // Find point for bubble's arrow in screen coordinates.
71   NSPoint anchor = GetBubblePointInFrame(frame);
72   anchor = [field convertPoint:anchor toView:nil];
73   anchor = [[field window] convertBaseToScreen:anchor];
75   bubble_ = [[ZoomBubbleController alloc] initWithParentWindow:[field window]
76                                                       delegate:this];
77   [bubble_ showAnchoredAt:anchor autoClose:auto_close];
80 void ZoomDecoration::CloseBubble() {
81   [bubble_ close];
84 void ZoomDecoration::HideUI() {
85   [bubble_ close];
86   SetVisible(false);
89 void ZoomDecoration::ShowAndUpdateUI(ui_zoom::ZoomController* zoom_controller,
90                                      NSString* tooltip_string) {
91   int image_id = IDR_ZOOM_NORMAL;
92   ui_zoom::ZoomController::RelativeZoom relative_zoom =
93       zoom_controller->GetZoomRelativeToDefault();
94   if (relative_zoom == ui_zoom::ZoomController::ZOOM_BELOW_DEFAULT_ZOOM)
95     image_id = IDR_ZOOM_MINUS;
96   else if (relative_zoom == ui_zoom::ZoomController::ZOOM_ABOVE_DEFAULT_ZOOM)
97     image_id = IDR_ZOOM_PLUS;
99   SetImage(OmniboxViewMac::ImageForResource(image_id));
101   tooltip_.reset([tooltip_string retain]);
103   SetVisible(true);
104   [bubble_ onZoomChanged];
107 NSPoint ZoomDecoration::GetBubblePointInFrame(NSRect frame) {
108   return NSMakePoint(NSMaxX(frame), NSMaxY(frame));
111 bool ZoomDecoration::IsAtDefaultZoom() const {
112   content::WebContents* web_contents = owner_->GetWebContents();
113   if (!web_contents)
114     return false;
116   ui_zoom::ZoomController* zoomController =
117       ui_zoom::ZoomController::FromWebContents(web_contents);
118   return zoomController && zoomController->IsAtDefaultZoom();
121 bool ZoomDecoration::ShouldShowDecoration() const {
122   return owner_->GetWebContents() != NULL &&
123       !owner_->GetToolbarModel()->input_in_progress() &&
124       (bubble_ || !IsAtDefaultZoom());
127 bool ZoomDecoration::AcceptsMousePress() {
128   return true;
131 bool ZoomDecoration::OnMousePressed(NSRect frame, NSPoint location) {
132   if (bubble_)
133     CloseBubble();
134   else
135     ShowBubble(NO);
136   return true;
139 NSString* ZoomDecoration::GetToolTip() {
140   return tooltip_.get();
143 content::WebContents* ZoomDecoration::GetWebContents() {
144   return owner_->GetWebContents();
147 void ZoomDecoration::OnClose() {
148   bubble_.delegate = nil;
149   bubble_ = nil;
151   // If the page is at default zoom then hiding the zoom decoration
152   // was suppressed while the bubble was open. Now that the bubble is
153   // closed the decoration can be hidden.
154   if (IsAtDefaultZoom() && IsVisible()) {
155     SetVisible(false);
156     owner_->OnDecorationsChanged();
157   }