Chromium Code Reviews| Index: chrome/browser/ui/cocoa/translate/translate_bubble_controller.mm |
| diff --git a/chrome/browser/ui/cocoa/translate/translate_bubble_controller.mm b/chrome/browser/ui/cocoa/translate/translate_bubble_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cb8b2255cf3f4fceecdb97fc029d233344842c35 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/translate/translate_bubble_controller.mm |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2014 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. |
| + |
| +#import "chrome/browser/ui/cocoa/translate/translate_bubble_controller.h" |
| + |
| +#include "base/mac/scoped_nsobject.h" |
| +#include "chrome/browser/translate/translate_ui_delegate.h" |
| +#import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| +#import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| +#import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| +#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
| +#include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| +#import "ui/base/cocoa/flipped_view.h" |
| + |
| +namespace { |
| + |
| +const CGFloat kWindowWidth = 320; |
|
Nico
2014/02/05 06:07:18
"const" has implicit internal linkage, no need for
hajimehoshi
2014/02/05 11:08:03
Done.
|
| + |
| +} // namespace |
| + |
| +@implementation TranslateBubbleController |
| + |
| +static TranslateBubbleController* translateBubbleController_ = NULL; |
| + |
| ++ (void)showBubbleWithBrowserWindowController:(BrowserWindowController*)controller |
|
Nico
2014/02/05 06:07:18
80 cols
hajimehoshi
2014/02/05 11:08:03
Done.
|
| + webContents:(content::WebContents*)webContents |
| + viewState: |
| + (TranslateBubbleModel::ViewState)viewState |
| + errorType:(TranslateErrors::Type)errorType { |
| + if (translateBubbleController_) { |
| + // When the user reads the advanced setting panel, the bubble should not be |
| + // changed because he/she is focusing on the bubble. |
| + if (translateBubbleController_->webContents_ == webContents && |
| + translateBubbleController_->model_->GetViewState() == |
| + TranslateBubbleModel::VIEW_STATE_ADVANCED) { |
| + return; |
| + } |
| + if (viewState != TranslateBubbleModel::VIEW_STATE_ERROR) |
| + [translateBubbleController_ switchView:viewState]; |
| + else |
| + [translateBubbleController_ switchToErrorView:errorType]; |
| + return; |
| + } |
| + |
| + // FIXME(hajimehoshi): Set the initial languages correctly. |
|
Nico
2014/02/05 06:07:18
fixme
hajimehoshi
2014/02/05 11:08:03
Lower cases? Done.
|
| + std::string sourceLanguage = "xx"; |
| + std::string targetLanguage = "yy"; |
| + |
| + scoped_ptr<TranslateUIDelegate> uiDelegate( |
| + new TranslateUIDelegate(webContents, sourceLanguage, targetLanguage)); |
| + scoped_ptr<TranslateBubbleModel> model( |
| + new TranslateBubbleModelImpl(viewState, uiDelegate.Pass())); |
| + translateBubbleController_ = [[TranslateBubbleController alloc] |
| + initWithBrowserWindowController:controller |
| + model:model.Pass() |
| + webContents:webContents]; |
| + [translateBubbleController_ showWindow:nil]; |
| +} |
| + |
| +- (id)initWithBrowserWindowController:(BrowserWindowController*)controller |
| + model:(scoped_ptr<TranslateBubbleModel>)model |
| + webContents:(content::WebContents*)webContents { |
| + webContents_ = webContents; |
|
Nico
2014/02/05 06:07:18
You can't write to ivars before the `self = …` has
hajimehoshi
2014/02/05 11:08:03
Done.
|
| + model_ = model.Pass(); |
| + translateExecuted_ = NO; |
| + if (model_->GetViewState() != |
| + TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { |
| + translateExecuted_ = YES; |
| + } |
| + |
| + NSWindow* parentWindow = [controller window]; |
| + |
| + // Use an arbitrary size; it will be changed in performLayout. |
| + NSRect contentRect = NSMakeRect(0, 0, kWindowWidth, 100); |
| + base::scoped_nsobject<InfoBubbleWindow> window( |
| + [[InfoBubbleWindow alloc] initWithContentRect:contentRect |
| + styleMask:NSBorderlessWindowMask |
| + backing:NSBackingStoreBuffered |
| + defer:NO]); |
| + |
| + if ((self = [super initWithWindow:window |
| + parentWindow:parentWindow |
| + anchoredAt:NSZeroPoint])) { |
| + // Create the container view that uses flipped coordinates. |
| + CGFloat x = info_bubble::kBubbleCornerRadius; |
| + CGFloat y = -info_bubble::kBubbleCornerRadius; |
| + NSRect contentFrame = NSMakeRect(x, y, kWindowWidth, 100); |
| + NSView* contentView = [[FlippedView alloc] initWithFrame:contentFrame]; |
| + [[window contentView] setSubviews:@[contentView]]; |
| + |
| + [self performLayout]; |
| + |
| + // This class will release itself when the bubble closes. See |
| + // -[BaseBubbleController windowWillClose:]. |
| + [self retain]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)windowWillClose:(NSNotification*)notification { |
| + DCHECK_EQ(translateBubbleController_, self); |
| + translateBubbleController_ = NULL; |
| + [super windowWillClose:notification]; |
| +} |
| + |
| +- (void)showWindow:(id)sender { |
| + BrowserWindowController* controller = [self.parentWindow windowController]; |
| + NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; |
| + anchorPoint = [self.parentWindow convertBaseToScreen:anchorPoint]; |
| + [self setAnchorPoint:anchorPoint]; |
| + [super showWindow:sender]; |
| +} |
| + |
| +- (void)switchView:(TranslateBubbleModel::ViewState)viewState { |
| + if (model_->GetViewState() == viewState) |
| + return; |
| + |
| + model_->SetViewState(viewState); |
| + [self performLayout]; |
| +} |
| + |
| +- (void)switchToErrorView:(TranslateErrors::Type)errorType { |
| + // FIXME(hajimehoshi): Implement this. |
| +} |
| + |
| +- (void)performLayout { |
| + // FIXME(hajimehoshi): Now this shows just an empty bubble. Implement this. |
| + [[self window] display]; |
| +} |
| + |
| +@end |