Chromium Code Reviews| Index: chrome/browser/ui/cocoa/browser/zoom_bubble_controller.mm |
| diff --git a/chrome/browser/ui/cocoa/browser/zoom_bubble_controller.mm b/chrome/browser/ui/cocoa/browser/zoom_bubble_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6112074268c745d466e58f9be0fce2e230e19325 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/browser/zoom_bubble_controller.mm |
| @@ -0,0 +1,153 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/cocoa/browser/zoom_bubble_controller.h" |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "chrome/browser/chrome_page_zoom.h" |
| +#import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| +#import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| +#include "chrome/browser/ui/zoom/zoom_controller.h" |
| +#include "content/public/common/page_zoom.h" |
| +#include "grit/generated_resources.h" |
| +#import "ui/base/cocoa/window_size_constants.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +@interface ZoomBubbleController (Private) |
| +- (void)performLayout; |
|
sail
2013/02/15 21:05:12
how about initializeSubviews?
Robert Sesek
2013/02/15 21:39:20
We call it performLayout in all the other bubbles,
|
| +- (void)updateZoomPercent; |
| +- (void)autoCloseBubble; |
| +@end |
| + |
| +namespace { |
| + |
| +// The amount of time to wait before the bubble automatically closes. |
| +// Should keep in sync with kBubbleCloseDelay in |
| +// src/chrome/browser/ui/views/location_bar/zoom_bubble_view.cc. |
| +const NSTimeInterval kAutoCloseDelay = 1.5; |
| + |
| +// The amount of padding between the window frame and controls. |
| +const CGFloat kPadding = 10.0; |
| + |
| +} // namespace |
| + |
| +@implementation ZoomBubbleController |
| + |
| +- (id)initWithParentWindow:(NSWindow*)parentWindow |
| + closeObserver:(void(^)(ZoomBubbleController*))closeObserver { |
| + scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc] |
| + initWithContentRect:NSMakeRect(0, 0, 200, 100) |
| + styleMask:NSBorderlessWindowMask |
| + backing:NSBackingStoreBuffered |
| + defer:NO]); |
| + if ((self = [super initWithWindow:window |
| + parentWindow:parentWindow |
| + anchoredAt:NSZeroPoint])) { |
| + closeObserver_.reset(Block_copy(closeObserver)); |
| + [self performLayout]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)showForWebContents:(content::WebContents*)contents |
| + anchoredAt:(NSPoint)anchorPoint |
| + autoClose:(BOOL)autoClose { |
| + contents_ = contents; |
| + [self updateZoomPercent]; |
| + |
| + self.anchorPoint = anchorPoint; |
| + [self showWindow:nil]; |
| + |
| + autoClose_ = autoClose; |
| + if (autoClose_) { |
| + [self performSelector:@selector(autoCloseBubble) |
| + withObject:nil |
| + afterDelay:kAutoCloseDelay]; |
| + } |
| +} |
| + |
| +- (void)onZoomChanged { |
| + if (contents_) |
|
sail
2013/02/15 21:05:12
this test is already done in updateZoomPercent
Robert Sesek
2013/02/15 21:39:20
Yes, it didn't used to. Folded together.
|
| + [self updateZoomPercent]; |
| +} |
| + |
| +- (void)resetToDefault:(id)sender { |
| + [self close]; |
| + chrome_page_zoom::Zoom(contents_, content::PAGE_ZOOM_RESET); |
| +} |
| + |
| +- (void)windowWillClose:(NSNotification*)notification { |
| + contents_ = NULL; |
| + closeObserver_.get()(self); |
| + [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
|
sail
2013/02/15 21:05:12
Nit, cancelPreviousPerformRequestsWithTarget:selec
Robert Sesek
2013/02/15 21:39:20
Done.
|
| + [super windowWillClose:notification]; |
| +} |
| + |
| +// Private ///////////////////////////////////////////////////////////////////// |
| + |
| +- (void)performLayout { |
| + NSView* parent = [[self window] contentView]; |
| + |
| + // Create the reset zoom button. |
| + scoped_nsobject<NSButton> resetButton( |
| + [[NSButton alloc] initWithFrame:NSMakeRect(0, kPadding, 0, 0)]); |
| + [resetButton setTitle:l10n_util::GetNSStringWithFixup(IDS_ZOOM_SET_DEFAULT)]; |
| + [resetButton setButtonType:NSMomentaryPushInButton]; |
| + [[resetButton cell] setControlSize:NSSmallControlSize]; |
| + [resetButton setBezelStyle:NSRoundedBezelStyle]; |
| + [resetButton setTarget:self]; |
| + [resetButton setAction:@selector(resetToDefault:)]; |
| + [resetButton sizeToFit]; |
| + |
| + // Center it within the window. |
| + NSRect frame = [resetButton frame]; |
|
sail
2013/02/15 21:05:12
buttonFrame? specially since you use it bellow
Robert Sesek
2013/02/15 21:39:20
Done.
|
| + frame.origin.x = NSMidX([parent frame]) - NSMidX(frame); |
| + [resetButton setFrame:frame]; |
| + [parent addSubview:resetButton]; |
| + |
| + // Add a horizontal separator. |
|
sail
2013/02/15 21:05:12
Why add a separator, the Windows UI doesn't have o
Robert Sesek
2013/02/15 21:39:20
Didn't realize that. Removed.
|
| + [parent addSubview:[self separatorWithFrame: |
| + NSMakeRect(kPadding / 2, |
| + NSMaxY(frame) + kPadding, |
| + NSWidth([parent frame]) - kPadding, |
| + 0)]]; |
| + |
| + // Create the label to display the current zoom amount. |
| + zoomPercent_.reset([[NSTextField alloc] initWithFrame: |
| + NSMakeRect(0, NSMaxY(frame) + 2*kPadding, 0, 0)]); |
|
sail
2013/02/15 21:05:12
space before and after *
Robert Sesek
2013/02/15 21:39:20
Removed.
|
| + [zoomPercent_ sizeToFit]; |
|
sail
2013/02/15 21:05:12
should this go above addSubview?
Robert Sesek
2013/02/15 21:39:20
It does?
|
| + [zoomPercent_ setEditable:NO]; |
| + [zoomPercent_ setBezeled:NO]; |
| + [zoomPercent_ setStringValue:@"%"]; |
|
sail
2013/02/15 21:05:12
I don't think you need this line
Robert Sesek
2013/02/15 21:39:20
Done.
|
| + [parent addSubview:zoomPercent_]; |
| + |
| + // Adjust the height of the window to fit the content. |
| + frame = [[self window] frame]; |
| + frame.size.height = NSMaxY([zoomPercent_ frame]) + kPadding + |
| + info_bubble::kBubbleArrowHeight; |
| + [[self window] setFrame:frame display:YES]; |
| +} |
| + |
| +- (void)updateZoomPercent { |
| + if (!contents_) |
| + return; // NULL in tests. |
| + ZoomController* zoomController = ZoomController::FromWebContents(contents_); |
| + int percent = zoomController->zoom_percent(); |
| + [zoomPercent_ setStringValue: |
|
sail
2013/02/15 21:05:12
Do you have to worry about the text field being wi
Robert Sesek
2013/02/15 21:39:20
I played with a bunch of locales and couldn't get
|
| + l10n_util::GetNSStringF(IDS_TOOLTIP_ZOOM, base::IntToString16(percent))]; |
| + |
| + [zoomPercent_ sizeToFit]; |
|
sail
2013/02/15 21:05:12
Alternatively, you could make the text field as wi
Robert Sesek
2013/02/15 21:39:20
Done.
|
| + NSRect frame = [zoomPercent_ frame]; |
| + frame.origin.x = NSMidX([[[self window] contentView] frame]) - |
| + NSWidth(frame) / 2; |
| + [zoomPercent_ setFrame:frame]; |
| +} |
| + |
| +- (void)autoCloseBubble { |
| + if (!autoClose_) |
| + return; |
| + [self close]; |
| +} |
| + |
| +@end |