OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/infobars/translate_infobar_base.h" | 5 #import "chrome/browser/ui/cocoa/infobars/translate_infobar_base.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
10 #include "chrome/browser/translate/translate_infobar_delegate.h" | 10 #include "chrome/browser/translate/translate_infobar_delegate.h" |
11 #import "chrome/browser/ui/cocoa/hover_close_button.h" | 11 #import "chrome/browser/ui/cocoa/hover_close_button.h" |
12 #include "chrome/browser/ui/cocoa/infobars/after_translate_infobar_controller.h" | 12 #include "chrome/browser/ui/cocoa/infobars/after_translate_infobar_controller.h" |
13 #import "chrome/browser/ui/cocoa/infobars/before_translate_infobar_controller.h" | 13 #import "chrome/browser/ui/cocoa/infobars/before_translate_infobar_controller.h" |
14 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h" | 14 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h" |
15 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | 15 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" |
16 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" | 16 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" |
17 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h" | 17 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h" |
18 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h" | 18 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h" |
19 #include "chrome/browser/ui/cocoa/infobars/translate_message_infobar_controller.
h" | 19 #include "chrome/browser/ui/cocoa/infobars/translate_message_infobar_controller.
h" |
20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
21 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | 21 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 | 23 |
24 using InfoBarUtilities::MoveControl; | 24 using InfoBarUtilities::MoveControl; |
25 using InfoBarUtilities::VerticallyCenterView; | 25 using InfoBarUtilities::VerticallyCenterView; |
26 using InfoBarUtilities::VerifyControlOrderAndSpacing; | 26 using InfoBarUtilities::VerifyControlOrderAndSpacing; |
27 using InfoBarUtilities::CreateLabel; | 27 using InfoBarUtilities::CreateLabel; |
28 using InfoBarUtilities::AddMenuItem; | 28 using InfoBarUtilities::AddMenuItem; |
29 | 29 |
30 // static | 30 // TranslateInfoBarDelegate views specific method: |
31 scoped_ptr<InfoBar> TranslateInfoBarDelegate::CreateInfoBar( | 31 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { |
32 scoped_ptr<TranslateInfoBarDelegate> delegate) { | 32 scoped_ptr<InfoBarCocoa> infobar(new InfoBarCocoa(owner, this)); |
33 scoped_ptr<InfoBarCocoa> infobar( | |
34 new InfoBarCocoa(delegate.PassAs<InfoBarDelegate>())); | |
35 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller; | 33 base::scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller; |
36 switch (infobar->delegate()->AsTranslateInfoBarDelegate()->infobar_type()) { | 34 switch (infobar_type_) { |
37 case BEFORE_TRANSLATE: | 35 case BEFORE_TRANSLATE: |
38 infobar_controller.reset([[BeforeTranslateInfobarController alloc] | 36 infobar_controller.reset([[BeforeTranslateInfobarController alloc] |
39 initWithInfoBar:infobar.get()]); | 37 initWithInfoBar:infobar.get()]); |
40 break; | 38 break; |
41 case AFTER_TRANSLATE: | 39 case AFTER_TRANSLATE: |
42 infobar_controller.reset([[AfterTranslateInfobarController alloc] | 40 infobar_controller.reset([[AfterTranslateInfobarController alloc] |
43 initWithInfoBar:infobar.get()]); | 41 initWithInfoBar:infobar.get()]); |
44 break; | 42 break; |
45 case TRANSLATING: | 43 case TRANSLATING: |
46 case TRANSLATION_ERROR: | 44 case TRANSLATION_ERROR: |
47 infobar_controller.reset([[TranslateMessageInfobarController alloc] | 45 infobar_controller.reset([[TranslateMessageInfobarController alloc] |
48 initWithInfoBar:infobar.get()]); | 46 initWithInfoBar:infobar.get()]); |
49 break; | 47 break; |
50 default: | 48 default: |
51 NOTREACHED(); | 49 NOTREACHED(); |
52 } | 50 } |
53 infobar->set_controller(infobar_controller); | 51 infobar->set_controller(infobar_controller); |
54 return infobar.PassAs<InfoBar>(); | 52 return infobar.release(); |
55 } | 53 } |
56 | 54 |
57 @implementation TranslateInfoBarControllerBase (FrameChangeObserver) | 55 @implementation TranslateInfoBarControllerBase (FrameChangeObserver) |
58 | 56 |
59 // Triggered when the frame changes. This will figure out what size and | 57 // Triggered when the frame changes. This will figure out what size and |
60 // visibility the options popup should be. | 58 // visibility the options popup should be. |
61 - (void)didChangeFrame:(NSNotification*)notification { | 59 - (void)didChangeFrame:(NSNotification*)notification { |
62 [self adjustOptionsButtonSizeAndVisibilityForView: | 60 [self adjustOptionsButtonSizeAndVisibilityForView: |
63 [[self visibleControls] lastObject]]; | 61 [[self visibleControls] lastObject]]; |
64 } | 62 } |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 [infoBarView_ setPostsFrameChangedNotifications:YES]; | 352 [infoBarView_ setPostsFrameChangedNotifications:YES]; |
355 [[NSNotificationCenter defaultCenter] | 353 [[NSNotificationCenter defaultCenter] |
356 addObserver:self | 354 addObserver:self |
357 selector:@selector(didChangeFrame:) | 355 selector:@selector(didChangeFrame:) |
358 name:NSViewFrameDidChangeNotification | 356 name:NSViewFrameDidChangeNotification |
359 object:infoBarView_]; | 357 object:infoBarView_]; |
360 // Show and place GUI elements. | 358 // Show and place GUI elements. |
361 [self updateState]; | 359 [self updateState]; |
362 } | 360 } |
363 | 361 |
364 - (void)infobarWillHide { | |
365 [[fromLanguagePopUp_ menu] cancelTracking]; | |
366 [[toLanguagePopUp_ menu] cancelTracking]; | |
367 [[optionsPopUp_ menu] cancelTracking]; | |
368 [super infobarWillHide]; | |
369 } | |
370 | |
371 - (void)infobarWillClose { | 362 - (void)infobarWillClose { |
372 [self disablePopUpMenu:[fromLanguagePopUp_ menu]]; | 363 [self disablePopUpMenu:[fromLanguagePopUp_ menu]]; |
373 [self disablePopUpMenu:[toLanguagePopUp_ menu]]; | 364 [self disablePopUpMenu:[toLanguagePopUp_ menu]]; |
374 [self disablePopUpMenu:[optionsPopUp_ menu]]; | 365 [self disablePopUpMenu:[optionsPopUp_ menu]]; |
| 366 // [super infobarWillClose] clears the owner field which is relied on by the |
| 367 // notification handler, so remove the handler first. |
375 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 368 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
376 [super infobarWillClose]; | 369 [super infobarWillClose]; |
377 } | 370 } |
378 | 371 |
379 - (void)adjustOptionsButtonSizeAndVisibilityForView:(NSView*)lastView { | 372 - (void)adjustOptionsButtonSizeAndVisibilityForView:(NSView*)lastView { |
380 [optionsPopUp_ setHidden:NO]; | 373 [optionsPopUp_ setHidden:NO]; |
381 [self rebuildOptionsMenu:NO]; | 374 [self rebuildOptionsMenu:NO]; |
382 [[optionsPopUp_ cell] setArrowPosition:NSPopUpArrowAtBottom]; | 375 [[optionsPopUp_ cell] setArrowPosition:NSPopUpArrowAtBottom]; |
383 [optionsPopUp_ sizeToFit]; | 376 [optionsPopUp_ sizeToFit]; |
384 | 377 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 // Danger Will Robinson! : This call can release the infobar (e.g. invoking | 451 // Danger Will Robinson! : This call can release the infobar (e.g. invoking |
459 // "About Translate" can open a new tab). | 452 // "About Translate" can open a new tab). |
460 // Do not access member variables after this line! | 453 // Do not access member variables after this line! |
461 optionsMenuModel_->ExecuteCommand(cmd, 0); | 454 optionsMenuModel_->ExecuteCommand(cmd, 0); |
462 } else { | 455 } else { |
463 NOTREACHED(); | 456 NOTREACHED(); |
464 } | 457 } |
465 } | 458 } |
466 | 459 |
467 - (void)dealloc { | 460 - (void)dealloc { |
| 461 // Perhaps this was removed as an observer in -infobarWillClose, but there's |
| 462 // no guarantee that that was the case. |
| 463 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
468 [showOriginalButton_ setTarget:nil]; | 464 [showOriginalButton_ setTarget:nil]; |
469 [translateMessageButton_ setTarget:nil]; | 465 [translateMessageButton_ setTarget:nil]; |
470 [super dealloc]; | 466 [super dealloc]; |
471 } | 467 } |
472 | 468 |
473 #pragma mark NSMenuDelegate | 469 #pragma mark NSMenuDelegate |
474 | 470 |
475 // Invoked by virtue of us being set as the delegate for the options menu. | 471 // Invoked by virtue of us being set as the delegate for the options menu. |
476 - (void)menuNeedsUpdate:(NSMenu *)menu { | 472 - (void)menuNeedsUpdate:(NSMenu *)menu { |
477 [self adjustOptionsButtonSizeAndVisibilityForView: | 473 [self adjustOptionsButtonSizeAndVisibilityForView: |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 [[control description] UTF8String] <<[title UTF8String]; | 542 [[control description] UTF8String] <<[title UTF8String]; |
547 return false; | 543 return false; |
548 } | 544 } |
549 previousControl = control; | 545 previousControl = control; |
550 } | 546 } |
551 | 547 |
552 return true; | 548 return true; |
553 } | 549 } |
554 | 550 |
555 @end // TranslateInfoBarControllerBase (TestingAPI) | 551 @end // TranslateInfoBarControllerBase (TestingAPI) |
OLD | NEW |