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 |
| index 577da21c46945240cd981e26285ec200d6bea601..a72c365b7cabda80776bf2609b5953a3c1e9d8cd 100644 |
| --- a/chrome/browser/ui/cocoa/translate/translate_bubble_controller.mm |
| +++ b/chrome/browser/ui/cocoa/translate/translate_bubble_controller.mm |
| @@ -4,15 +4,102 @@ |
| #import "chrome/browser/ui/cocoa/translate/translate_bubble_controller.h" |
| +#include "base/mac/foundation_util.h" |
| #include "base/mac/scoped_nsobject.h" |
| +#include "base/strings/sys_string_conversions.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/language_combobox_model.h" |
| #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "grit/generated_resources.h" |
| +#include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| +#import "ui/base/cocoa/controls/hyperlink_button_cell.h" |
| #import "ui/base/cocoa/flipped_view.h" |
| #import "ui/base/cocoa/window_size_constants.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/models/combobox_model.h" |
| + |
| +namespace { |
| + |
| +enum ButtonId { |
| + kButtonIdTranslate, |
| + kButtonIdNope, |
| + kButtonIdDone, |
| + kButtonIdCancel, |
| + kButtonIdShowOriginal, |
| + kButtonIdTryAgain, |
| + kButtonIdAlwaysTranslate, |
| + kButtonIdLinkAdvanced, |
| +}; |
| + |
| +enum PopUpButtonId { |
| + kPopUpButtonIdDenial, |
| + kPopUpButtonIdSourceLanguage, |
| + kPopUpButtonIdTargetLanguage, |
| +}; |
| + |
| +} // namespace |
| + |
| +class TranslateDenialComboboxModel : public ui::ComboboxModel { |
| + public: |
| + enum { |
| + kIndexNope = 1, |
| + kIndexNeverTranslateLanguage = 2, |
| + kIndexNeverTranslateSite = 3, |
| + }; |
| + |
| + explicit TranslateDenialComboboxModel( |
| + const base::string16& original_language_name) { |
| + // Dummy menu item, which is shown on the top of a NSPopUpButton. The top |
| + // text of the denial pop up menu should be IDS_TRANSLATE_BUBBLE_DENY, while |
| + // it is impossible to use it here because NSPopUpButtons' addItemWithTitle |
| + // removes a duplicated menu item. Instead, the title will be set later by |
| + // NSMenuItem's setTitle. |
| + items_.push_back(base::string16()); |
| + |
| + // Menu items in the drop down menu. |
| + items_.push_back(l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); |
| + items_.push_back(l10n_util::GetStringFUTF16( |
| + IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_LANG, |
| + original_language_name)); |
| + items_.push_back(l10n_util::GetStringUTF16( |
| + IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_SITE)); |
| + } |
| + virtual ~TranslateDenialComboboxModel() {} |
| + |
| + private: |
| + // ComboboxModel: |
| + virtual int GetItemCount() const OVERRIDE { |
| + return items_.size(); |
| + } |
| + virtual base::string16 GetItemAt(int index) OVERRIDE { |
| + return items_[index]; |
| + } |
| + virtual bool IsItemSeparatorAt(int index) OVERRIDE { |
| + return false; |
| + } |
| + virtual int GetDefaultIndex() const OVERRIDE { |
| + return 0; |
| + } |
| + |
| + std::vector<base::string16> items_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TranslateDenialComboboxModel); |
| +}; |
| + |
| +const CGFloat kWindowWidth = 320; |
| + |
| +// Padding between the window frame and content. |
| +const CGFloat kFramePadding = 16; |
| + |
| +const CGFloat kRelatedControlHorizontalSpacing = -2; |
| + |
| +const CGFloat kRelatedControlVerticalSpacing = 4; |
| +const CGFloat kUnrelatedControlVerticalSpacing = 20; |
| @implementation TranslateBubbleController |
| @@ -39,6 +126,19 @@ |
| translateExecuted_ = YES; |
| } |
| + // Create the container view that uses flipped coordinates. |
| + CGFloat x = info_bubble::kBubbleCornerRadius; |
| + CGFloat y = -info_bubble::kBubbleCornerRadius; |
| + NSRect contentFrame = NSMakeRect(x, y, kWindowWidth, 1); |
| + NSView* contentView = [[FlippedView alloc] initWithFrame:contentFrame]; |
|
groby-ooo-7-16
2014/03/18 17:58:18
Why not just replace contentView, if you need a Fl
hajimehoshi
2014/03/19 07:33:22
Done (Removed).
|
| + [[window contentView] setSubviews:@[contentView]]; |
| + |
| + beforeTranslateView_.reset([self createViewBeforeTranslate]); |
| + translatingView_.reset([self createViewTranslating]); |
| + afterTranslateView_.reset([self createViewAfterTranslate]); |
| + errorView_.reset([self createViewError]); |
| + advancedView_.reset([self createViewAdvanced]); |
| + |
| [self performLayout]; |
| } |
| return self; |
| @@ -46,6 +146,23 @@ |
| @synthesize webContents = webContents_; |
| +- (NSView*)currentView { |
| + switch (model_->GetViewState()) { |
| + case TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE: |
| + return beforeTranslateView_; |
| + case TranslateBubbleModel::VIEW_STATE_TRANSLATING: |
| + return translatingView_; |
| + case TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE: |
| + return afterTranslateView_; |
| + case TranslateBubbleModel::VIEW_STATE_ERROR: |
| + return errorView_; |
| + case TranslateBubbleModel::VIEW_STATE_ADVANCED: |
| + return advancedView_; |
| + } |
| + NOTREACHED(); |
| + return nil; |
| +} |
| + |
| - (const TranslateBubbleModel*)model { |
| return model_.get(); |
| } |
| @@ -71,8 +188,595 @@ |
| } |
| - (void)performLayout { |
| - // TODO(hajimehoshi): Now this shows just an empty bubble. Implement this. |
| - [[self window] display]; |
| + NSView* subview = [[[[self window] contentView] subviews] objectAtIndex:0]; |
|
groby-ooo-7-16
2014/03/18 17:58:18
It might be worth storing that object on the contr
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + [subview setSubviews:@[[self currentView]]]; |
|
groby-ooo-7-16
2014/03/18 17:58:18
nit - @[] has spaces after/before bracket. I.e. @[
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + |
| + CGFloat height = NSHeight([[self currentView] frame]) + |
| + 2 * kFramePadding + info_bubble::kBubbleArrowHeight; |
| + |
| + NSRect subviewFrame = [subview frame]; |
| + subviewFrame.size.height = height; |
| + [subview setFrame:subviewFrame]; |
| + |
| + NSRect windowFrame = [[self window] frame]; |
| + windowFrame.origin.y += NSHeight(windowFrame) - height; |
| + windowFrame.size.width = kWindowWidth; |
| + windowFrame.size.height = height; |
| + [[self window] setFrame:windowFrame |
| + display:YES |
| + animate:[[self window] isVisible]]; |
| +} |
| + |
| +- (NSView*)createViewBeforeTranslate { |
| + CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; |
| + NSRect contentFrame = NSMakeRect( |
| + kFramePadding, |
| + kFramePadding + info_bubble::kBubbleArrowHeight, |
| + contentWidth, |
| + 1); |
| + NSView* view = [[FlippedView alloc] initWithFrame:contentFrame]; |
|
groby-ooo-7-16
2014/03/18 17:58:18
In general, if you can avoid FlippedView, I would.
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + |
| + NSString* message = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_BEFORE_TRANSLATE); |
| + NSTextField* textLabel = [self addText:message |
| + withSize:[NSFont smallSystemFontSize] |
| + bold:NO |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); |
| + NSButton* advancedLinkButton = |
| + [self addLinkButtonWithText:message |
| + tag:kButtonIdLinkAdvanced |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + |
| + NSString* title = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); |
| + NSButton* translateButton = |
| + [self addButton:title |
| + withTag:kButtonIdTranslate |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + |
| + base::string16 originalLanguageName = |
| + model_->GetLanguageNameAt(model_->GetOriginalLanguageIndex()); |
| + translateDenialComboboxModel_.reset( |
| + new TranslateDenialComboboxModel(originalLanguageName)); |
| + NSPopUpButton* denyPopUpButton = |
| + [self addPopUpButton:translateDenialComboboxModel_.get() |
| + withTag:kPopUpButtonIdDenial |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + [denyPopUpButton setPullsDown:YES]; |
| + title = base::SysUTF16ToNSString( |
| + l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); |
| + [[denyPopUpButton itemAtIndex:0] setTitle:title]; |
| + |
| + // Adjust width for the first item. |
| + std::vector<NSString*> titles; |
| + for (int i = 1; i < [denyPopUpButton numberOfItems]; i++) { |
| + NSMenuItem* item = [denyPopUpButton itemAtIndex:i]; |
| + titles.push_back([item title]); |
| + [item setTitle:@""]; |
| + } |
| + [denyPopUpButton sizeToFit]; |
| + for (int i = 1; i < [denyPopUpButton numberOfItems]; i++) { |
| + NSMenuItem* item = [denyPopUpButton itemAtIndex:i]; |
| + [item setTitle:titles[i-1]]; |
| + } |
| + |
| + // Layout |
| + CGFloat yPos = 0; |
| + |
| + NSRect advancedLinkButtonFrame = [advancedLinkButton frame]; |
| + advancedLinkButtonFrame.origin.x = NSWidth([textLabel frame]); |
| + [advancedLinkButton setFrame:advancedLinkButtonFrame]; |
| + |
| + yPos += NSHeight([textLabel frame]); |
| + yPos += kUnrelatedControlVerticalSpacing; |
| + |
| + NSRect translateButtonFrame = [translateButton frame]; |
| + translateButtonFrame.origin.x = |
| + contentWidth - NSWidth([translateButton frame]); |
| + translateButtonFrame.origin.y = yPos; |
| + [translateButton setFrame:translateButtonFrame]; |
| + |
| + NSRect denyPopUpButtonFrame = [denyPopUpButton frame]; |
| + CGFloat diffY = [[denyPopUpButton cell] |
| + titleRectForBounds:[denyPopUpButton bounds]].origin.y; |
| + denyPopUpButtonFrame.origin.x = |
| + NSMinX([translateButton frame]) - denyPopUpButtonFrame.size.width |
| + - kRelatedControlHorizontalSpacing; |
| + denyPopUpButtonFrame.origin.y = yPos + diffY; |
| + [denyPopUpButton setFrame:denyPopUpButtonFrame]; |
| + |
| + yPos += NSHeight([translateButton frame]); |
| + |
| + contentFrame = [view frame]; |
| + contentFrame.size.height = yPos; |
| + [view setFrame:contentFrame]; |
| + |
| + return view; |
| +} |
| + |
| +- (NSView*)createViewTranslating { |
| + CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; |
| + NSRect contentFrame = NSMakeRect( |
| + kFramePadding, |
| + kFramePadding + info_bubble::kBubbleArrowHeight, |
| + contentWidth, |
| + 1); |
| + NSView* view = [[FlippedView alloc] initWithFrame:contentFrame]; |
| + |
| + CGFloat yPos = 0; |
| + |
| + NSString* message = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATING); |
| + NSTextField* textLabel = [self addText:message |
| + withSize:[NSFont smallSystemFontSize] |
| + bold:NO |
| + toView:view |
| + atPoint:NSMakePoint(0, yPos)]; |
| + yPos += NSHeight([textLabel frame]); |
| + yPos += kUnrelatedControlVerticalSpacing; |
| + |
| + NSString* title = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); |
| + NSButton* showOriginalButton = |
| + [self addButton:title |
| + withTag:kButtonIdShowOriginal |
| + toView:view |
| + atPoint:NSMakePoint(0, yPos)]; |
| + [showOriginalButton setEnabled:NO]; |
| + NSRect showOriginalButtonFrame = [showOriginalButton frame]; |
| + showOriginalButtonFrame.origin.x = |
| + contentWidth - NSWidth([showOriginalButton frame]); |
| + [showOriginalButton setFrame:showOriginalButtonFrame]; |
| + |
| + yPos += NSHeight([showOriginalButton frame]); |
| + |
| + contentFrame = [view frame]; |
| + contentFrame.size.height = yPos; |
| + [view setFrame:contentFrame]; |
| + |
| + return view; |
| +} |
| + |
| +- (NSView*)createViewAfterTranslate { |
| + CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; |
| + NSRect contentFrame = NSMakeRect( |
| + kFramePadding, |
| + kFramePadding + info_bubble::kBubbleArrowHeight, |
| + contentWidth, |
| + 1); |
| + NSView* view = [[FlippedView alloc] initWithFrame:contentFrame]; |
| + |
| + CGFloat yPos = 0; |
| + |
| + NSString* message = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATED); |
| + NSTextField* textLabel = [self addText:message |
| + withSize:[NSFont smallSystemFontSize] |
| + bold:NO |
| + toView:view |
| + atPoint:NSMakePoint(0, yPos)]; |
| + message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); |
| + [self addLinkButtonWithText:message |
| + tag:kButtonIdLinkAdvanced |
| + toView:view |
| + atPoint:NSMakePoint(NSWidth([textLabel frame]), yPos)]; |
| + yPos += NSHeight([textLabel frame]); |
| + yPos += kUnrelatedControlVerticalSpacing; |
| + |
| + NSString* title = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); |
| + NSButton* showOriginalButton = |
| + [self addButton:title |
| + withTag:kButtonIdShowOriginal |
| + toView:view |
| + atPoint:NSMakePoint(0, yPos)]; |
| + NSRect showOriginalButtonFrame = [showOriginalButton frame]; |
| + showOriginalButtonFrame.origin.x = |
| + contentWidth - NSWidth([showOriginalButton frame]); |
| + [showOriginalButton setFrame:showOriginalButtonFrame]; |
| + |
| + yPos += NSHeight([showOriginalButton frame]); |
| + |
| + contentFrame = [view frame]; |
| + contentFrame.size.height = yPos; |
| + [view setFrame:contentFrame]; |
| + |
| + return view; |
| +} |
| + |
| +- (NSView*)createViewError { |
| + CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; |
| + NSRect contentFrame = NSMakeRect( |
| + kFramePadding, |
| + kFramePadding + info_bubble::kBubbleArrowHeight, |
| + contentWidth, |
| + 1); |
| + NSView* view = [[FlippedView alloc] initWithFrame:contentFrame]; |
| + |
| + // TODO(hajimehoshi): Implement this. |
| + |
| + return view; |
| +} |
| + |
| +- (NSView*)createViewAdvanced { |
| + CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; |
| + NSRect contentFrame = NSMakeRect( |
| + kFramePadding, |
| + kFramePadding + info_bubble::kBubbleArrowHeight, |
| + contentWidth, |
| + 1); |
| + NSView* view = [[FlippedView alloc] initWithFrame:contentFrame]; |
| + |
| + NSString* title = l10n_util::GetNSStringWithFixup( |
| + IDS_TRANSLATE_BUBBLE_PAGE_LANGUAGE); |
| + NSTextField* sourceLanguageLabel = [self addText:title |
| + withSize:[NSFont smallSystemFontSize] |
| + bold:NO |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + title = l10n_util::GetNSStringWithFixup( |
| + IDS_TRANSLATE_BUBBLE_TRANSLATION_LANGUAGE); |
| + NSTextField* targetLanguageLabel = [self addText:title |
| + withSize:[NSFont smallSystemFontSize] |
| + bold:NO |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + |
| + // combobox |
| + int sourceDefaultIndex = model_->GetOriginalLanguageIndex(); |
| + int targetDefaultIndex = model_->GetTargetLanguageIndex(); |
| + sourceLanguageComboboxModel_.reset( |
| + new LanguageComboboxModel(sourceDefaultIndex, model_.get())); |
| + targetLanguageComboboxModel_.reset( |
| + new LanguageComboboxModel(targetDefaultIndex, model_.get())); |
| + NSPopUpButton* sourcePopUpButton = |
| + [self addPopUpButton:sourceLanguageComboboxModel_.get() |
| + withTag:kPopUpButtonIdSourceLanguage |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + NSPopUpButton* targetPopUpButton = |
| + [self addPopUpButton:targetLanguageComboboxModel_.get() |
| + withTag:kPopUpButtonIdTargetLanguage |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + |
| + // 'Always translate' checkbox |
| + BOOL isIncognitoWindow = webContents_ ? |
| + webContents_->GetBrowserContext()->IsOffTheRecord() : NO; |
| + if (!isIncognitoWindow) { |
| + NSString* title = |
| + l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ALWAYS); |
| + alwaysTranslateCheckbox_ = |
| + [self addCheckbox:title |
| + withTag:kButtonIdAlwaysTranslate |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + } |
| + |
| + // Buttons |
| + advancedDoneButton_ = |
| + [self addButton:l10n_util::GetNSStringWithFixup(IDS_DONE) |
| + withTag:kButtonIdDone |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + advancedCancelButton_ = |
| + [self addButton:l10n_util::GetNSStringWithFixup(IDS_CANCEL) |
| + withTag:kButtonIdCancel |
| + toView:view |
| + atPoint:NSZeroPoint]; |
| + |
| + // Layout |
| + CGFloat textLabelWidth = NSWidth([sourceLanguageLabel frame]); |
| + if (textLabelWidth < NSWidth([targetLanguageLabel frame])) |
| + textLabelWidth = NSWidth([targetLanguageLabel frame]); |
| + |
| + CGFloat yPos = 0; |
| + CGFloat diffY = [[sourcePopUpButton cell] |
| + titleRectForBounds:[sourcePopUpButton bounds]].origin.y; |
| + NSRect frame = [sourceLanguageLabel frame]; |
| + frame.origin.x = textLabelWidth - NSWidth([sourceLanguageLabel frame]); |
| + frame.origin.y = yPos + diffY; |
| + [sourceLanguageLabel setFrame:frame]; |
| + |
| + frame = [sourcePopUpButton frame]; |
| + frame.origin.x = textLabelWidth; |
| + frame.origin.y = yPos; |
| + frame.size.width = (kWindowWidth - 2 * kFramePadding) - textLabelWidth; |
| + [sourcePopUpButton setFrame:frame]; |
| + |
| + yPos += NSHeight([sourcePopUpButton frame]); |
| + yPos += kRelatedControlVerticalSpacing; |
| + |
| + frame = [targetLanguageLabel frame]; |
| + frame.origin.x = textLabelWidth - NSWidth([targetLanguageLabel frame]); |
| + frame.origin.y = yPos + diffY; |
| + [targetLanguageLabel setFrame:frame]; |
| + |
| + frame = [targetPopUpButton frame]; |
| + frame.origin.x = textLabelWidth; |
| + frame.origin.y = yPos; |
| + frame.size.width = (kWindowWidth - 2 * kFramePadding) - textLabelWidth; |
| + [targetPopUpButton setFrame:frame]; |
| + |
| + yPos += NSHeight([targetPopUpButton frame]); |
| + |
| + if (alwaysTranslateCheckbox_) { |
| + yPos += kRelatedControlVerticalSpacing; |
| + |
| + NSRect frame = [alwaysTranslateCheckbox_ frame]; |
| + frame.origin.x = textLabelWidth; |
| + frame.origin.y = yPos; |
| + [alwaysTranslateCheckbox_ setFrame:frame]; |
| + |
| + yPos += NSHeight([alwaysTranslateCheckbox_ frame]); |
| + } |
| + |
| + yPos += kUnrelatedControlVerticalSpacing; |
| + |
| + frame = [advancedDoneButton_ frame]; |
| + frame.origin.y = yPos; |
| + [advancedDoneButton_ setFrame:frame]; |
| + |
| + frame = [advancedCancelButton_ frame]; |
| + frame.origin.y = yPos; |
| + [advancedCancelButton_ setFrame:frame]; |
| + |
| + yPos += NSHeight([advancedDoneButton_ frame]); |
| + |
| + contentFrame = [view frame]; |
| + contentFrame.size.height = yPos; |
| + [view setFrame:contentFrame]; |
| + |
| + [self updateAdvancedView]; |
| + |
| + return view; |
| +} |
| + |
| +- (void)updateAdvancedView { |
|
groby-ooo-7-16
2014/03/18 17:58:18
I think it might be clearer to subclass NSView for
hajimehoshi
2014/03/19 07:33:22
Nothing special. I wasn't able to find such bubble
|
| + if (alwaysTranslateCheckbox_) { |
| + NSInteger state = model_->ShouldAlwaysTranslate() ? NSOnState : NSOffState; |
| + [alwaysTranslateCheckbox_ setState:state]; |
| + } |
| + |
| + NSString* title; |
| + if (model_->IsPageTranslatedInCurrentLanguages()) |
| + title = l10n_util::GetNSStringWithFixup(IDS_DONE); |
| + else |
| + title = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); |
| + [advancedDoneButton_ setTitle:title]; |
| + [advancedDoneButton_ sizeToFit]; |
| + |
| + NSRect frame = [advancedDoneButton_ frame]; |
| + frame.origin.x = (kWindowWidth - 2 * kFramePadding) - frame.size.width; |
| + [advancedDoneButton_ setFrame:frame]; |
| + |
| + frame = [advancedCancelButton_ frame]; |
| + frame.origin.x = NSMinX([advancedDoneButton_ frame]) - frame.size.width |
| + - kRelatedControlHorizontalSpacing; |
| + [advancedCancelButton_ setFrame:frame]; |
| +} |
| + |
| +// Create a new text field and add it to the given array of subviews. |
| +// The array will retain a reference to the object. |
| +- (NSTextField*)addText:(NSString*)text |
| + withSize:(CGFloat)fontSize |
| + bold:(BOOL)bold |
| + toView:(NSView*)view |
| + atPoint:(NSPoint)point { |
| + CGFloat width = NSWidth([view frame]) - point.x; |
| + NSRect frame = NSMakeRect(point.x, point.y, width, 100); |
| + base::scoped_nsobject<NSTextField> textField( |
| + [[NSTextField alloc] initWithFrame:frame]); |
| + [textField setEditable:NO]; |
| + [textField setSelectable:YES]; |
| + [textField setDrawsBackground:NO]; |
| + [textField setBezeled:NO]; |
| + [textField setStringValue:text]; |
| + NSFont* font = bold ? [NSFont boldSystemFontOfSize:fontSize] |
| + : [NSFont systemFontOfSize:fontSize]; |
| + [textField setFont:font]; |
| + |
| + frame = [textField frame]; |
| + frame.size.height += |
| + [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: |
| + textField]; |
| + [textField setFrame:frame]; |
| + |
| + [textField setAutoresizingMask:NSViewWidthSizable]; |
| + [view addSubview:textField.get()]; |
| + |
| + [textField sizeToFit]; |
| + return textField.get(); |
| +} |
| + |
| +- (NSButton*)addLinkButtonWithText:(NSString*)text |
| + tag:(NSInteger)tag |
| + toView:(NSView*)view |
| + atPoint:(NSPoint)point { |
| + base::scoped_nsobject<NSButton> button( |
| + [[NSButton alloc] initWithFrame:NSZeroRect]); |
| + [button setTag:tag]; |
| + base::scoped_nsobject<HyperlinkButtonCell> cell( |
| + [[HyperlinkButtonCell alloc] initTextCell:text]); |
| + [cell setControlSize:NSSmallControlSize]; |
| + [button setCell:cell.get()]; |
| + |
| + [button setButtonType:NSMomentaryPushInButton]; |
| + [button setBezelStyle:NSRegularSquareBezelStyle]; |
| + [button sizeToFit]; |
| + NSRect frame = NSMakeRect( |
| + point.x, point.y, |
| + NSWidth([button frame]), NSHeight([button frame])); |
| + [button setFrame:frame]; |
|
groby-ooo-7-16
2014/03/18 17:58:18
see below for setFrameOrigin:
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + [button setAction:@selector(handleButtonPressed:)]; |
| + [button setTarget:self]; |
| + |
| + [view addSubview:button.get()]; |
| + |
| + return button.get(); |
| +} |
| + |
| +- (NSButton*)addButton:(NSString*)title |
| + withTag:(NSInteger)tag |
| + toView:(NSView*)view |
| + atPoint:(NSPoint)point { |
| + base::scoped_nsobject<NSButton> button( |
| + [[NSButton alloc] initWithFrame:NSZeroRect]); |
| + [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| + [button setTag:tag]; |
| + [button setTitle:title]; |
| + [button setBezelStyle:NSRoundedBezelStyle]; |
| + [[button cell] setControlSize:NSSmallControlSize]; |
| + [button sizeToFit]; |
| + NSRect frame = NSMakeRect( |
|
groby-ooo-7-16
2014/03/18 17:58:18
I think the next 4 lines (until -setFrame:) might
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + point.x, point.y, |
| + NSWidth([button frame]), NSHeight([button frame])); |
| + [button setFrame:frame]; |
| + [button setAction:@selector(handleButtonPressed:)]; |
| + [button setTarget:self]; |
| + |
| + [view addSubview:button.get()]; |
| + |
| + return button.get(); |
| +} |
| + |
| +- (NSButton*)addCheckbox:(NSString*)title |
| + withTag:(NSInteger)tag |
| + toView:(NSView*)view |
| + atPoint:(NSPoint)point { |
| + base::scoped_nsobject<NSButton> button( |
| + [[NSButton alloc] initWithFrame:NSZeroRect]); |
| + [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| + [button setTag:tag]; |
| + [button setTitle:title]; |
| + [[button cell] setControlSize:NSSmallControlSize]; |
| + [button setButtonType:NSSwitchButton]; |
| + [button sizeToFit]; |
| + NSRect frame = NSMakeRect( |
| + point.x, point.y, |
| + NSWidth([button frame]), NSHeight([button frame])); |
| + [button setFrame:frame]; |
|
groby-ooo-7-16
2014/03/18 17:58:18
setFrameOrigin:, as above
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + [button setAction:@selector(handleButtonPressed:)]; |
| + [button setTarget:self]; |
| + |
| + [view addSubview:button.get()]; |
| + |
| + return button.get(); |
| +} |
| + |
| +- (NSPopUpButton*)addPopUpButton:(ui::ComboboxModel*)model |
| + withTag:(NSInteger)tag |
| + toView:(NSView*)view |
| + atPoint:(NSPoint)point { |
| + base::scoped_nsobject<NSPopUpButton> button( |
| + [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO]); |
| + [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| + [button setBordered:YES]; |
| + [[button cell] setControlSize:NSSmallControlSize]; |
| + [button setTag:tag]; |
| + [button setAction:@selector(handlePopUpButtonSelectedItemChanged:)]; |
| + [button setTarget:self]; |
| + |
| + for (int i = 0; i < model->GetItemCount(); ++i) |
| + [button addItemWithTitle:base::SysUTF16ToNSString(model->GetItemAt(i))]; |
| + [button selectItemAtIndex:model->GetDefaultIndex()]; |
| + |
| + [button sizeToFit]; |
| + NSRect frame = NSMakeRect( |
| + point.x, point.y, NSWidth([button frame]), NSHeight([button frame])); |
| + [button setFrame:frame]; |
| + |
| + [view addSubview:button.get()]; |
| + |
| + return button.get(); |
| +} |
| + |
| +- (void)handleButtonPressed:(id)sender { |
| + NSButton* button = base::mac::ObjCCastStrict<NSButton>(sender); |
|
groby-ooo-7-16
2014/03/18 17:58:18
Is there a contraint that requires a switch based
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + NSInteger tag = [button tag]; |
| + switch (tag) { |
| + case kButtonIdTranslate: { |
| + translateExecuted_ = YES; |
| + model_->Translate(); |
| + break; |
| + } |
| + case kButtonIdNope: { |
| + [self close]; |
| + break; |
| + } |
| + case kButtonIdDone: { |
| + if (alwaysTranslateCheckbox_) { |
| + model_->SetAlwaysTranslate( |
| + [alwaysTranslateCheckbox_ state] == NSOnState); |
| + } |
| + if (model_->IsPageTranslatedInCurrentLanguages()) { |
| + model_->GoBackFromAdvanced(); |
| + [self performLayout]; |
| + } else { |
| + translateExecuted_ = true; |
| + model_->Translate(); |
| + [self switchView:TranslateBubbleModel::VIEW_STATE_TRANSLATING]; |
| + } |
| + break; |
| + } |
| + case kButtonIdCancel: { |
| + model_->GoBackFromAdvanced(); |
| + [self performLayout]; |
| + break; |
| + } |
| + case kButtonIdShowOriginal: { |
| + model_->RevertTranslation(); |
| + [self close]; |
| + break; |
| + } |
| + case kButtonIdLinkAdvanced: { |
| + [self switchView:TranslateBubbleModel::VIEW_STATE_ADVANCED]; |
| + break; |
| + } |
| + case kButtonIdAlwaysTranslate: { |
| + // Do nothing. The state of the checkbox affects only when the 'Done' |
| + // button is pressed. |
|
groby-ooo-7-16
2014/03/18 17:58:18
Can you simply not have an action for the checkbox
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + break; |
| + } |
| + default: |
| + break; |
| + } |
| +} |
| + |
| +- (void)handlePopUpButtonSelectedItemChanged:(id)sender { |
|
groby-ooo-7-16
2014/03/18 17:58:18
As for the button delegate, I'd much prefer a sepa
hajimehoshi
2014/03/19 07:33:22
Done.
|
| + NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); |
| + NSInteger tag = [button tag]; |
| + switch (tag) { |
| + case kPopUpButtonIdDenial: { |
| + switch ([button indexOfSelectedItem]) { |
| + case TranslateDenialComboboxModel::kIndexNope: |
| + break; |
| + case TranslateDenialComboboxModel::kIndexNeverTranslateLanguage: |
| + model_->SetNeverTranslateLanguage(true); |
| + break; |
| + case TranslateDenialComboboxModel::kIndexNeverTranslateSite: |
| + model_->SetNeverTranslateSite(true); |
| + break; |
| + } |
| + [self close]; |
| + break; |
| + } |
| + case kPopUpButtonIdSourceLanguage: { |
| + model_->UpdateOriginalLanguageIndex([button indexOfSelectedItem]); |
| + [self updateAdvancedView]; |
| + break; |
| + } |
| + case kPopUpButtonIdTargetLanguage: { |
| + model_->UpdateTargetLanguageIndex([button indexOfSelectedItem]); |
| + [self updateAdvancedView]; |
| + break; |
| + } |
| + } |
| } |
| @end |