| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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/cocoa/browser/zoom_bubble_controller.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "chrome/browser/chrome_page_zoom.h" |
| 9 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 10 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| 11 #include "chrome/browser/ui/zoom/zoom_controller.h" |
| 12 #include "content/public/common/page_zoom.h" |
| 13 #include "grit/generated_resources.h" |
| 14 #import "ui/base/cocoa/window_size_constants.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 |
| 17 @interface ZoomBubbleController (Private) |
| 18 - (void)performLayout; |
| 19 - (void)autoCloseBubble; |
| 20 @end |
| 21 |
| 22 namespace { |
| 23 |
| 24 // The amount of time to wait before the bubble automatically closes. |
| 25 // Should keep in sync with kBubbleCloseDelay in |
| 26 // src/chrome/browser/ui/views/location_bar/zoom_bubble_view.cc. |
| 27 const NSTimeInterval kAutoCloseDelay = 1.5; |
| 28 |
| 29 // The amount of padding between the window frame and controls. |
| 30 const CGFloat kPadding = 10.0; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 @implementation ZoomBubbleController |
| 35 |
| 36 - (id)initWithParentWindow:(NSWindow*)parentWindow |
| 37 closeObserver:(void(^)(ZoomBubbleController*))closeObserver { |
| 38 scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc] |
| 39 initWithContentRect:NSMakeRect(0, 0, 200, 100) |
| 40 styleMask:NSBorderlessWindowMask |
| 41 backing:NSBackingStoreBuffered |
| 42 defer:NO]); |
| 43 if ((self = [super initWithWindow:window |
| 44 parentWindow:parentWindow |
| 45 anchoredAt:NSZeroPoint])) { |
| 46 closeObserver_.reset(Block_copy(closeObserver)); |
| 47 [self performLayout]; |
| 48 } |
| 49 return self; |
| 50 } |
| 51 |
| 52 - (void)showForWebContents:(content::WebContents*)contents |
| 53 anchoredAt:(NSPoint)anchorPoint |
| 54 autoClose:(BOOL)autoClose { |
| 55 contents_ = contents; |
| 56 [self onZoomChanged]; |
| 57 |
| 58 self.anchorPoint = anchorPoint; |
| 59 [self showWindow:nil]; |
| 60 |
| 61 autoClose_ = autoClose; |
| 62 if (autoClose_) { |
| 63 [self performSelector:@selector(autoCloseBubble) |
| 64 withObject:nil |
| 65 afterDelay:kAutoCloseDelay]; |
| 66 } |
| 67 } |
| 68 |
| 69 - (void)onZoomChanged { |
| 70 if (!contents_) |
| 71 return; // NULL in tests. |
| 72 |
| 73 ZoomController* zoomController = ZoomController::FromWebContents(contents_); |
| 74 int percent = zoomController->zoom_percent(); |
| 75 [zoomPercent_ setStringValue: |
| 76 l10n_util::GetNSStringF(IDS_TOOLTIP_ZOOM, base::IntToString16(percent))]; |
| 77 } |
| 78 |
| 79 - (void)resetToDefault:(id)sender { |
| 80 [self close]; |
| 81 chrome_page_zoom::Zoom(contents_, content::PAGE_ZOOM_RESET); |
| 82 } |
| 83 |
| 84 - (void)windowWillClose:(NSNotification*)notification { |
| 85 contents_ = NULL; |
| 86 closeObserver_.get()(self); |
| 87 [NSObject cancelPreviousPerformRequestsWithTarget:self |
| 88 selector:@selector(autoCloseBubble) |
| 89 object:nil]; |
| 90 [super windowWillClose:notification]; |
| 91 } |
| 92 |
| 93 // Private ///////////////////////////////////////////////////////////////////// |
| 94 |
| 95 - (void)performLayout { |
| 96 NSView* parent = [[self window] contentView]; |
| 97 |
| 98 // Create the reset zoom button. |
| 99 scoped_nsobject<NSButton> resetButton( |
| 100 [[NSButton alloc] initWithFrame:NSMakeRect(0, kPadding, 0, 0)]); |
| 101 [resetButton setTitle:l10n_util::GetNSStringWithFixup(IDS_ZOOM_SET_DEFAULT)]; |
| 102 [resetButton setButtonType:NSMomentaryPushInButton]; |
| 103 [[resetButton cell] setControlSize:NSSmallControlSize]; |
| 104 [resetButton setBezelStyle:NSRoundedBezelStyle]; |
| 105 [resetButton setTarget:self]; |
| 106 [resetButton setAction:@selector(resetToDefault:)]; |
| 107 [resetButton sizeToFit]; |
| 108 |
| 109 // Center it within the window. |
| 110 NSRect buttonFrame = [resetButton frame]; |
| 111 buttonFrame.origin.x = NSMidX([parent frame]) - NSMidX(buttonFrame); |
| 112 [resetButton setFrame:buttonFrame]; |
| 113 [parent addSubview:resetButton]; |
| 114 |
| 115 NSRect windowFrame = [[self window] frame]; |
| 116 |
| 117 // Create the label to display the current zoom amount. |
| 118 zoomPercent_.reset([[NSTextField alloc] initWithFrame: |
| 119 NSMakeRect(kPadding, NSMaxY(buttonFrame), |
| 120 NSWidth(windowFrame) - 2 * kPadding, 22)]); |
| 121 [zoomPercent_ setEditable:NO]; |
| 122 [zoomPercent_ setBezeled:NO]; |
| 123 [zoomPercent_ setAlignment:NSCenterTextAlignment]; |
| 124 [parent addSubview:zoomPercent_]; |
| 125 |
| 126 // Adjust the height of the window to fit the content. |
| 127 windowFrame.size.height = NSMaxY([zoomPercent_ frame]) + kPadding + |
| 128 info_bubble::kBubbleArrowHeight; |
| 129 [[self window] setFrame:windowFrame display:YES]; |
| 130 } |
| 131 |
| 132 - (void)autoCloseBubble { |
| 133 if (!autoClose_) |
| 134 return; |
| 135 [self close]; |
| 136 } |
| 137 |
| 138 @end |
| OLD | NEW |