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/browser/zoom_bubble_controller.h"
11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
13 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
14 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
15 #include "chrome/browser/ui/zoom/zoom_controller.h"
16 #include "grit/generated_resources.h"
17 #include "ui/base/l10n/l10n_util_mac.h"
19 ZoomDecoration::ZoomDecoration(LocationBarViewMac* owner)
24 ZoomDecoration::~ZoomDecoration() {
25 [bubble_ closeWithoutAnimation];
28 void ZoomDecoration::Update(ZoomController* zoom_controller) {
29 if (!ShouldShowDecoration()) {
35 SetImage(OmniboxViewMac::ImageForResource(
36 zoom_controller->GetResourceForZoomLevel()));
38 string16 zoom_percent = 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]);
45 [bubble_ onZoomChanged];
48 void ZoomDecoration::ShowBubble(BOOL auto_close) {
52 content::WebContents* web_contents = owner_->GetWebContents();
56 // Get the frame of the decoration.
57 AutocompleteTextField* field = owner_->GetAutocompleteTextField();
58 AutocompleteTextFieldCell* cell = [field cell];
59 const NSRect frame = [cell frameForDecoration:this
60 inFrame:[field bounds]];
62 // Find point for bubble's arrow in screen coordinates.
63 NSPoint anchor = GetBubblePointInFrame(frame);
64 anchor = [field convertPoint:anchor toView:nil];
65 anchor = [[field window] convertBaseToScreen:anchor];
68 void(^observer)(ZoomBubbleController*) = ^(ZoomBubbleController*) {
70 // If the page is at default zoom then hiding the zoom decoration was
71 // suppressed while the bubble was open. Now that the bubble is closed
72 // the decoration can be hidden.
73 if (IsAtDefaultZoom() && IsVisible()) {
75 owner_->OnDecorationsChanged();
79 [[ZoomBubbleController alloc] initWithParentWindow:[field window]
80 closeObserver:observer];
83 [bubble_ showForWebContents:web_contents
85 autoClose:auto_close];
88 void ZoomDecoration::CloseBubble() {
92 NSPoint ZoomDecoration::GetBubblePointInFrame(NSRect frame) {
93 return NSMakePoint(NSMaxX(frame), NSMaxY(frame));
96 bool ZoomDecoration::IsAtDefaultZoom() const {
97 content::WebContents* web_contents = owner_->GetWebContents();
100 ZoomController* zoomController =
101 ZoomController::FromWebContents(web_contents);
102 return zoomController && zoomController->IsAtDefaultZoom();
105 bool ZoomDecoration::ShouldShowDecoration() const {
106 return !owner_->GetToolbarModel()->input_in_progress() &&
107 (bubble_ || !IsAtDefaultZoom());
110 bool ZoomDecoration::AcceptsMousePress() {
114 bool ZoomDecoration::OnMousePressed(NSRect frame) {
122 NSString* ZoomDecoration::GetToolTip() {
123 return tooltip_.get();