Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/translate/translate_bubble_controller.h" | 5 #import "chrome/browser/ui/cocoa/translate/translate_bubble_controller.h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | |
| 7 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" | |
| 8 #include "chrome/browser/translate/translate_ui_delegate.h" | 10 #include "chrome/browser/translate/translate_ui_delegate.h" |
| 9 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 11 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 10 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | 12 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| 11 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 13 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 12 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" | 14 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
| 15 #include "chrome/browser/ui/translate/language_combobox_model.h" | |
| 13 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" | 16 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| 14 #import "ui/base/cocoa/flipped_view.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "grit/generated_resources.h" | |
| 19 #include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutT weaker.h" | |
| 20 #import "ui/base/cocoa/controls/hyperlink_button_cell.h" | |
| 15 #import "ui/base/cocoa/window_size_constants.h" | 21 #import "ui/base/cocoa/window_size_constants.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | |
| 23 #include "ui/base/models/combobox_model.h" | |
| 24 | |
| 25 class TranslateDenialComboboxModel : public ui::ComboboxModel { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
This seems identical to the Views combobox model -
hajimehoshi
2014/03/24 07:16:30
They are a little different in that the first item
groby-ooo-7-16
2014/03/25 18:49:04
True, OSX needs a dummy item - but that should be
hajimehoshi
2014/03/26 11:06:05
Sure, but I'll leave it until your question about
| |
| 26 public: | |
| 27 enum { | |
| 28 kIndexNope = 1, | |
| 29 kIndexNeverTranslateLanguage = 2, | |
| 30 kIndexNeverTranslateSite = 3, | |
| 31 }; | |
| 32 | |
| 33 explicit TranslateDenialComboboxModel( | |
| 34 const base::string16& original_language_name) { | |
| 35 // Dummy menu item, which is shown on the top of a NSPopUpButton. The top | |
| 36 // text of the denial pop up menu should be IDS_TRANSLATE_BUBBLE_DENY, while | |
| 37 // it is impossible to use it here because NSPopUpButtons' addItemWithTitle | |
| 38 // removes a duplicated menu item. Instead, the title will be set later by | |
| 39 // NSMenuItem's setTitle. | |
| 40 items_.push_back(base::string16()); | |
| 41 | |
| 42 // Menu items in the drop down menu. | |
| 43 items_.push_back(l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); | |
| 44 items_.push_back(l10n_util::GetStringFUTF16( | |
| 45 IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_LANG, | |
| 46 original_language_name)); | |
| 47 items_.push_back(l10n_util::GetStringUTF16( | |
| 48 IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_SITE)); | |
| 49 } | |
| 50 virtual ~TranslateDenialComboboxModel() {} | |
| 51 | |
| 52 private: | |
| 53 // ComboboxModel: | |
| 54 virtual int GetItemCount() const OVERRIDE { | |
| 55 return items_.size(); | |
| 56 } | |
| 57 virtual base::string16 GetItemAt(int index) OVERRIDE { | |
| 58 return items_[index]; | |
| 59 } | |
| 60 virtual bool IsItemSeparatorAt(int index) OVERRIDE { | |
| 61 return false; | |
| 62 } | |
| 63 virtual int GetDefaultIndex() const OVERRIDE { | |
| 64 return 0; | |
| 65 } | |
| 66 | |
| 67 std::vector<base::string16> items_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(TranslateDenialComboboxModel); | |
| 70 }; | |
| 71 | |
| 72 const CGFloat kWindowWidth = 320; | |
| 73 | |
| 74 // Padding between the window frame and content. | |
| 75 const CGFloat kFramePadding = 16; | |
| 76 | |
| 77 const CGFloat kRelatedControlHorizontalSpacing = -2; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Why is this negative? Intuitively, I would read th
hajimehoshi
2014/03/24 07:16:30
This is actually negative. I put two buttons side
groby-ooo-7-16
2014/03/25 18:49:04
That's odd. Looking at the mocks, I see no two but
hajimehoshi
2014/03/26 11:06:05
Yes, there is space between two buttons, while the
groby-ooo-7-16
2014/03/27 00:39:39
They sort-of do. The frame indicates "most" of the
| |
| 78 | |
| 79 const CGFloat kRelatedControlVerticalSpacing = 4; | |
| 80 const CGFloat kUnrelatedControlVerticalSpacing = 20; | |
| 16 | 81 |
| 17 @implementation TranslateBubbleController | 82 @implementation TranslateBubbleController |
| 18 | 83 |
| 19 - (id)initWithParentWindow:(BrowserWindowController*)controller | 84 - (id)initWithParentWindow:(BrowserWindowController*)controller |
| 20 model:(scoped_ptr<TranslateBubbleModel>)model | 85 model:(scoped_ptr<TranslateBubbleModel>)model |
| 21 webContents:(content::WebContents*)webContents { | 86 webContents:(content::WebContents*)webContents { |
| 22 NSWindow* parentWindow = [controller window]; | 87 NSWindow* parentWindow = [controller window]; |
| 23 | 88 |
| 24 // Use an arbitrary size; it will be changed in performLayout. | 89 // Use an arbitrary size; it will be changed in performLayout. |
| 25 NSRect contentRect = ui::kWindowSizeDeterminedLater; | 90 NSRect contentRect = ui::kWindowSizeDeterminedLater; |
| 26 base::scoped_nsobject<InfoBubbleWindow> window( | 91 base::scoped_nsobject<InfoBubbleWindow> window( |
| 27 [[InfoBubbleWindow alloc] initWithContentRect:contentRect | 92 [[InfoBubbleWindow alloc] initWithContentRect:contentRect |
| 28 styleMask:NSBorderlessWindowMask | 93 styleMask:NSBorderlessWindowMask |
| 29 backing:NSBackingStoreBuffered | 94 backing:NSBackingStoreBuffered |
| 30 defer:NO]); | 95 defer:NO]); |
| 31 | 96 |
| 32 if ((self = [super initWithWindow:window | 97 if ((self = [super initWithWindow:window |
| 33 parentWindow:parentWindow | 98 parentWindow:parentWindow |
| 34 anchoredAt:NSZeroPoint])) { | 99 anchoredAt:NSZeroPoint])) { |
| 35 webContents_ = webContents; | 100 webContents_ = webContents; |
| 36 model_ = model.Pass(); | 101 model_ = model.Pass(); |
| 37 if (model_->GetViewState() != | 102 if (model_->GetViewState() != |
| 38 TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { | 103 TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { |
| 39 translateExecuted_ = YES; | 104 translateExecuted_ = YES; |
| 40 } | 105 } |
| 41 | 106 |
| 107 beforeTranslateView_.reset([self createViewBeforeTranslate]); | |
| 108 translatingView_.reset([self createViewTranslating]); | |
| 109 afterTranslateView_.reset([self createViewAfterTranslate]); | |
| 110 errorView_.reset([self createViewError]); | |
| 111 advancedView_.reset([self createViewAdvanced]); | |
| 112 | |
| 42 [self performLayout]; | 113 [self performLayout]; |
| 43 } | 114 } |
| 44 return self; | 115 return self; |
| 45 } | 116 } |
| 46 | 117 |
| 47 @synthesize webContents = webContents_; | 118 @synthesize webContents = webContents_; |
| 48 | 119 |
| 120 - (NSView*)currentView { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
I'm wondering if it would be nicer to simply have
hajimehoshi
2014/03/24 07:16:30
It seems that ScopedPtrHashMap can't be used for N
groby-ooo-7-16
2014/03/25 18:49:04
You are of course right, sorry. I should have said
hajimehoshi
2014/03/26 11:06:05
Done.
| |
| 121 switch (model_->GetViewState()) { | |
| 122 case TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE: | |
| 123 return beforeTranslateView_; | |
| 124 case TranslateBubbleModel::VIEW_STATE_TRANSLATING: | |
| 125 return translatingView_; | |
| 126 case TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE: | |
| 127 return afterTranslateView_; | |
| 128 case TranslateBubbleModel::VIEW_STATE_ERROR: | |
| 129 return errorView_; | |
| 130 case TranslateBubbleModel::VIEW_STATE_ADVANCED: | |
| 131 return advancedView_; | |
| 132 } | |
| 133 NOTREACHED(); | |
| 134 return nil; | |
| 135 } | |
| 136 | |
| 49 - (const TranslateBubbleModel*)model { | 137 - (const TranslateBubbleModel*)model { |
| 50 return model_.get(); | 138 return model_.get(); |
| 51 } | 139 } |
| 52 | 140 |
| 53 - (void)showWindow:(id)sender { | 141 - (void)showWindow:(id)sender { |
| 54 BrowserWindowController* controller = [[self parentWindow] windowController]; | 142 BrowserWindowController* controller = [[self parentWindow] windowController]; |
| 55 NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; | 143 NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; |
| 56 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint]; | 144 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint]; |
| 57 [self setAnchorPoint:anchorPoint]; | 145 [self setAnchorPoint:anchorPoint]; |
| 58 [super showWindow:sender]; | 146 [super showWindow:sender]; |
| 59 } | 147 } |
| 60 | 148 |
| 61 - (void)switchView:(TranslateBubbleModel::ViewState)viewState { | 149 - (void)switchView:(TranslateBubbleModel::ViewState)viewState { |
| 62 if (model_->GetViewState() == viewState) | 150 if (model_->GetViewState() == viewState) |
| 63 return; | 151 return; |
| 64 | 152 |
| 65 model_->SetViewState(viewState); | 153 model_->SetViewState(viewState); |
| 66 [self performLayout]; | 154 [self performLayout]; |
| 67 } | 155 } |
| 68 | 156 |
| 69 - (void)switchToErrorView:(TranslateErrors::Type)errorType { | 157 - (void)switchToErrorView:(TranslateErrors::Type)errorType { |
| 70 // TODO(hajimehoshi): Implement this. | 158 // TODO(hajimehoshi): Implement this. |
| 71 } | 159 } |
| 72 | 160 |
| 73 - (void)performLayout { | 161 - (void)performLayout { |
| 74 // TODO(hajimehoshi): Now this shows just an empty bubble. Implement this. | 162 [[[self window] contentView] setSubviews:@[ [self currentView] ]]; |
| 75 [[self window] display]; | 163 |
| 164 CGFloat height = NSHeight([[self currentView] frame]) + | |
| 165 2 * kFramePadding + info_bubble::kBubbleArrowHeight; | |
| 166 | |
| 167 NSRect contentViewFrame = [[[self window] contentView] frame]; | |
| 168 contentViewFrame.size.height = height; | |
| 169 [[[self window] contentView] setFrame:contentViewFrame]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
If you don't need to animate the window, you proba
hajimehoshi
2014/03/24 07:16:30
Done (animation is needed, so I tried to use frame
| |
| 170 | |
| 171 NSRect windowFrame = [[self window] frame]; | |
| 172 windowFrame.origin.y += NSHeight(windowFrame) - height; | |
| 173 windowFrame.size.width = kWindowWidth; | |
| 174 windowFrame.size.height = height; | |
| 175 [[self window] setFrame:windowFrame | |
| 176 display:YES | |
| 177 animate:[[self window] isVisible]]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
I assume you want the window to just pop up if it
hajimehoshi
2014/03/24 07:16:30
Right. It needs animation when changing the view.
| |
| 178 } | |
| 179 | |
| 180 - (NSView*)createViewBeforeTranslate { | |
| 181 CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
That seems to always be the contentWidth - maybe j
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 182 NSRect contentFrame = NSMakeRect( | |
| 183 kFramePadding, | |
| 184 kFramePadding, | |
| 185 contentWidth, | |
| 186 1); | |
|
groby-ooo-7-16
2014/03/21 01:21:42
It's OK to use 0 for the height - only NSWindow do
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 187 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
| 188 | |
| 189 NSString* message = | |
| 190 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_BEFORE_TRANSLATE); | |
| 191 NSTextField* textLabel = [self addText:message | |
| 192 withSize:[NSFont smallSystemFontSize] | |
| 193 bold:NO | |
| 194 toView:view]; | |
| 195 message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); | |
| 196 NSButton* advancedLinkButton = | |
| 197 [self addLinkButtonWithText:message | |
| 198 target:self | |
| 199 action:@selector(handleAdvancedLinkButtonPressed) | |
| 200 toView:view]; | |
| 201 | |
| 202 NSString* title = | |
| 203 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); | |
| 204 NSButton* translateButton = | |
| 205 [self addButton:title | |
| 206 target:self | |
| 207 action:@selector(handleTranslateButtonPressed) | |
| 208 toView:view]; | |
| 209 | |
| 210 base::string16 originalLanguageName = | |
| 211 model_->GetLanguageNameAt(model_->GetOriginalLanguageIndex()); | |
| 212 translateDenialComboboxModel_.reset( | |
| 213 new TranslateDenialComboboxModel(originalLanguageName)); | |
| 214 SEL action = @selector(handleDenialPopUpButtonSelectedItemChanged:); | |
| 215 NSPopUpButton* denyPopUpButton = | |
| 216 [self addPopUpButton:translateDenialComboboxModel_.get() | |
| 217 target:self | |
| 218 action:action | |
| 219 toView:view]; | |
| 220 [denyPopUpButton setPullsDown:YES]; | |
| 221 title = base::SysUTF16ToNSString( | |
| 222 l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); | |
| 223 [[denyPopUpButton itemAtIndex:0] setTitle:title]; | |
| 224 | |
| 225 // Adjust width for the first item. | |
|
groby-ooo-7-16
2014/03/21 01:21:42
A shorter version would be to just copy the popup,
hajimehoshi
2014/03/24 07:16:30
Oh, cool! However, it seems that NSButton is not c
groby-ooo-7-16
2014/03/25 18:49:04
Sigh. I forgot, sorry. The cell is copyable - but
hajimehoshi
2014/03/26 11:06:05
Done.
| |
| 226 std::vector<NSString*> titles; | |
| 227 for (int i = 1; i < [denyPopUpButton numberOfItems]; i++) { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
If the above solution works, you don't need this,
hajimehoshi
2014/03/24 07:16:30
Done leaving the above as it is.
| |
| 228 NSMenuItem* item = [denyPopUpButton itemAtIndex:i]; | |
| 229 titles.push_back([item title]); | |
| 230 [item setTitle:@""]; | |
| 231 } | |
| 232 [denyPopUpButton sizeToFit]; | |
| 233 for (int i = 1; i < [denyPopUpButton numberOfItems]; i++) { | |
| 234 NSMenuItem* item = [denyPopUpButton itemAtIndex:i]; | |
| 235 [item setTitle:titles[i-1]]; | |
| 236 } | |
| 237 | |
| 238 // Layout | |
| 239 CGFloat yPos = 0; | |
| 240 | |
| 241 [translateButton setFrameOrigin:NSMakePoint( | |
| 242 contentWidth - NSWidth([translateButton frame]), yPos)]; | |
| 243 | |
| 244 NSRect denyPopUpButtonFrame = [denyPopUpButton frame]; | |
| 245 CGFloat diffY = [[denyPopUpButton cell] | |
| 246 titleRectForBounds:[denyPopUpButton bounds]].origin.y; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Usually, we don't base layout computations on cell
hajimehoshi
2014/03/24 07:16:30
Since NSPopUpButton and NSButtond didn't align ver
groby-ooo-7-16
2014/03/25 18:49:04
Ah, you're adjusting the baseline of the texts to
| |
| 247 [denyPopUpButton setFrameOrigin:NSMakePoint( | |
| 248 NSMinX([translateButton frame]) - denyPopUpButtonFrame.size.width | |
| 249 - kRelatedControlHorizontalSpacing, | |
| 250 yPos + diffY)]; | |
| 251 | |
| 252 yPos += NSHeight([translateButton frame]); | |
|
groby-ooo-7-16
2014/03/21 01:21:42
You can roll these two into one line :)
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 253 yPos += kUnrelatedControlVerticalSpacing; | |
| 254 | |
| 255 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
| 256 [advancedLinkButton setFrameOrigin:NSMakePoint( | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Are you sure you don't want any horizontal spacing
hajimehoshi
2014/03/24 07:16:30
Yes because the link seems like a text and I think
groby-ooo-7-16
2014/03/25 18:49:04
I'm OK with this, but out of curiosity: From the m
hajimehoshi
2014/03/26 11:06:05
Your assuming is right. I don't put any space, but
groby-ooo-7-16
2014/03/27 00:39:39
This is strange, but that doesn't impact the CL :)
hajimehoshi
2014/03/27 10:37:28
I didn't know that. Thank you for the information!
| |
| 257 NSWidth([textLabel frame]), yPos)]; | |
| 258 | |
| 259 yPos += NSHeight([textLabel frame]); | |
|
groby-ooo-7-16
2014/03/21 01:21:42
It's probably shorter to skip everything until the
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 260 | |
| 261 contentFrame = [view frame]; | |
| 262 contentFrame.size.height = yPos; | |
| 263 [view setFrame:contentFrame]; | |
| 264 | |
| 265 return view; | |
| 266 } | |
| 267 | |
| 268 - (NSView*)createViewTranslating { | |
| 269 CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; | |
| 270 NSRect contentFrame = NSMakeRect( | |
| 271 kFramePadding, | |
| 272 kFramePadding, | |
| 273 contentWidth, | |
| 274 1); | |
| 275 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
| 276 | |
| 277 NSString* message = | |
| 278 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATING); | |
| 279 NSTextField* textLabel = [self addText:message | |
| 280 withSize:[NSFont smallSystemFontSize] | |
| 281 bold:NO | |
| 282 toView:view]; | |
| 283 NSString* title = | |
| 284 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); | |
| 285 NSButton* showOriginalButton = | |
| 286 [self addButton:title | |
| 287 target:self | |
| 288 action:@selector(handleShowOriginalButtonPressed) | |
| 289 toView:view]; | |
| 290 [showOriginalButton setEnabled:NO]; | |
| 291 | |
| 292 // Layout | |
| 293 CGFloat yPos = 0; | |
| 294 | |
| 295 [showOriginalButton setFrameOrigin:NSMakePoint( | |
| 296 contentWidth - NSWidth([showOriginalButton frame]), yPos)]; | |
| 297 | |
| 298 yPos += NSHeight([showOriginalButton frame]); | |
| 299 yPos += kUnrelatedControlVerticalSpacing; | |
| 300 | |
| 301 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
| 302 | |
| 303 yPos += NSHeight([textLabel frame]); | |
|
groby-ooo-7-16
2014/03/21 01:21:42
it seems most of your controls are only vertically
hajimehoshi
2014/03/24 07:16:30
Thanks. I'll do this if I have time. I think other
groby-ooo-7-16
2014/03/25 18:49:04
That is fine.
| |
| 304 | |
| 305 contentFrame = [view frame]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
See comment about setFrameSize: above.
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 306 contentFrame.size.height = yPos; | |
| 307 [view setFrame:contentFrame]; | |
| 308 | |
| 309 return view; | |
| 310 } | |
| 311 | |
| 312 - (NSView*)createViewAfterTranslate { | |
| 313 CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
As above, maybe have a file-wide kContentWidth. In
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 314 NSRect contentFrame = NSMakeRect( | |
| 315 kFramePadding, | |
| 316 kFramePadding, | |
| 317 contentWidth, | |
| 318 1); | |
| 319 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
| 320 | |
| 321 NSString* message = | |
| 322 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATED); | |
| 323 NSTextField* textLabel = [self addText:message | |
| 324 withSize:[NSFont smallSystemFontSize] | |
| 325 bold:NO | |
| 326 toView:view]; | |
| 327 message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); | |
| 328 NSButton* advancedLinkButton = | |
| 329 [self addLinkButtonWithText:message | |
| 330 target:self | |
| 331 action:@selector(handleAdvancedLinkButtonPressed) | |
| 332 toView:view]; | |
| 333 NSString* title = | |
| 334 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); | |
| 335 NSButton* showOriginalButton = | |
| 336 [self addButton:title | |
| 337 target:self | |
| 338 action:@selector(handleShowOriginalButtonPressed) | |
| 339 toView:view]; | |
| 340 | |
| 341 // Layout | |
| 342 CGFloat yPos = 0; | |
| 343 | |
| 344 [showOriginalButton setFrameOrigin:NSMakePoint( | |
| 345 contentWidth - NSWidth([showOriginalButton frame]), yPos)]; | |
| 346 | |
| 347 yPos += NSHeight([showOriginalButton frame]); | |
| 348 yPos += kUnrelatedControlVerticalSpacing; | |
| 349 | |
| 350 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
| 351 [advancedLinkButton setFrameOrigin:NSMakePoint( | |
| 352 NSWidth([textLabel frame]), yPos)]; | |
| 353 | |
| 354 yPos += NSHeight([textLabel frame]); | |
| 355 | |
| 356 contentFrame = [view frame]; | |
| 357 contentFrame.size.height = yPos; | |
| 358 [view setFrame:contentFrame]; | |
| 359 | |
| 360 return view; | |
| 361 } | |
| 362 | |
| 363 - (NSView*)createViewError { | |
| 364 CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; | |
| 365 NSRect contentFrame = NSMakeRect( | |
| 366 kFramePadding, | |
| 367 kFramePadding, | |
| 368 contentWidth, | |
| 369 1); | |
| 370 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
| 371 | |
| 372 // TODO(hajimehoshi): Implement this. | |
| 373 | |
| 374 return view; | |
| 375 } | |
| 376 | |
| 377 - (NSView*)createViewAdvanced { | |
| 378 CGFloat contentWidth = kWindowWidth - 2 * kFramePadding; | |
| 379 NSRect contentFrame = NSMakeRect( | |
| 380 kFramePadding, | |
| 381 kFramePadding, | |
| 382 contentWidth, | |
| 383 1); | |
| 384 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
| 385 | |
| 386 NSString* title = l10n_util::GetNSStringWithFixup( | |
| 387 IDS_TRANSLATE_BUBBLE_PAGE_LANGUAGE); | |
| 388 NSTextField* sourceLanguageLabel = [self addText:title | |
| 389 withSize:[NSFont smallSystemFontSize] | |
| 390 bold:NO | |
| 391 toView:view]; | |
| 392 title = l10n_util::GetNSStringWithFixup( | |
| 393 IDS_TRANSLATE_BUBBLE_TRANSLATION_LANGUAGE); | |
| 394 NSTextField* targetLanguageLabel = [self addText:title | |
| 395 withSize:[NSFont smallSystemFontSize] | |
| 396 bold:NO | |
| 397 toView:view]; | |
| 398 | |
| 399 // combobox | |
| 400 int sourceDefaultIndex = model_->GetOriginalLanguageIndex(); | |
| 401 int targetDefaultIndex = model_->GetTargetLanguageIndex(); | |
| 402 sourceLanguageComboboxModel_.reset( | |
| 403 new LanguageComboboxModel(sourceDefaultIndex, model_.get())); | |
| 404 targetLanguageComboboxModel_.reset( | |
| 405 new LanguageComboboxModel(targetDefaultIndex, model_.get())); | |
| 406 SEL action = @selector(handleSourceLanguagePopUpButtonSelectedItemChanged:); | |
| 407 NSPopUpButton* sourcePopUpButton = | |
| 408 [self addPopUpButton:sourceLanguageComboboxModel_.get() | |
| 409 target:self | |
| 410 action:action | |
| 411 toView:view]; | |
| 412 action = @selector(handleTargetLanguagePopUpButtonSelectedItemChanged:); | |
| 413 NSPopUpButton* targetPopUpButton = | |
| 414 [self addPopUpButton:targetLanguageComboboxModel_.get() | |
| 415 target:self | |
| 416 action:action | |
| 417 toView:view]; | |
| 418 | |
| 419 // 'Always translate' checkbox | |
| 420 BOOL isIncognitoWindow = webContents_ ? | |
| 421 webContents_->GetBrowserContext()->IsOffTheRecord() : NO; | |
| 422 if (!isIncognitoWindow) { | |
| 423 NSString* title = | |
| 424 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ALWAYS); | |
| 425 alwaysTranslateCheckbox_ = [self addCheckbox:title | |
| 426 toView:view]; | |
| 427 } | |
| 428 | |
| 429 // Buttons | |
| 430 advancedDoneButton_ = | |
| 431 [self addButton:l10n_util::GetNSStringWithFixup(IDS_DONE) | |
| 432 target:self | |
| 433 action:@selector(handleDoneButtonPressed) | |
| 434 toView:view]; | |
| 435 advancedCancelButton_ = | |
| 436 [self addButton:l10n_util::GetNSStringWithFixup(IDS_CANCEL) | |
| 437 target:self | |
| 438 action:@selector(handleCancelButtonPressed) | |
| 439 toView:view]; | |
| 440 | |
| 441 // Layout | |
| 442 CGFloat textLabelWidth = NSWidth([sourceLanguageLabel frame]); | |
| 443 if (textLabelWidth < NSWidth([targetLanguageLabel frame])) | |
| 444 textLabelWidth = NSWidth([targetLanguageLabel frame]); | |
| 445 | |
| 446 CGFloat yPos = 0; | |
| 447 | |
| 448 [advancedDoneButton_ setFrameOrigin:NSMakePoint(0, yPos)]; | |
| 449 [advancedCancelButton_ setFrameOrigin:NSMakePoint(0, yPos)]; | |
| 450 | |
| 451 yPos += NSHeight([advancedDoneButton_ frame]); | |
| 452 yPos += kUnrelatedControlVerticalSpacing; | |
| 453 | |
| 454 if (alwaysTranslateCheckbox_) { | |
| 455 [alwaysTranslateCheckbox_ setFrameOrigin:NSMakePoint(textLabelWidth, yPos)]; | |
| 456 | |
| 457 yPos += NSHeight([alwaysTranslateCheckbox_ frame]); | |
| 458 yPos += kRelatedControlVerticalSpacing; | |
| 459 } | |
| 460 | |
| 461 CGFloat diffY = [[sourcePopUpButton cell] | |
|
groby-ooo-7-16
2014/03/21 01:21:42
As above, I'm not sure why you need the cell's tit
hajimehoshi
2014/03/24 07:16:30
Same reason as above.
| |
| 462 titleRectForBounds:[sourcePopUpButton bounds]].origin.y; | |
| 463 | |
| 464 [targetLanguageLabel setFrameOrigin:NSMakePoint( | |
| 465 textLabelWidth - NSWidth([targetLanguageLabel frame]), yPos + diffY)]; | |
| 466 | |
| 467 NSRect frame = [targetPopUpButton frame]; | |
| 468 frame.origin.x = textLabelWidth; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Please use frame.origin = NSMakePoint(textLabelWid
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 469 frame.origin.y = yPos; | |
| 470 frame.size.width = (kWindowWidth - 2 * kFramePadding) - textLabelWidth; | |
| 471 [targetPopUpButton setFrame:frame]; | |
| 472 | |
| 473 yPos += NSHeight([targetPopUpButton frame]); | |
| 474 yPos += kRelatedControlVerticalSpacing; | |
| 475 | |
| 476 [sourceLanguageLabel setFrameOrigin:NSMakePoint( | |
| 477 textLabelWidth - NSWidth([sourceLanguageLabel frame]), yPos + diffY)]; | |
| 478 | |
| 479 frame = [sourcePopUpButton frame]; | |
| 480 frame.origin.x = textLabelWidth; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
NSMakePoint, please.
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 481 frame.origin.y = yPos; | |
| 482 frame.size.width = (kWindowWidth - 2 * kFramePadding) - textLabelWidth; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
You can probably just use NSWidth([targetPopUpButt
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 483 [sourcePopUpButton setFrame:frame]; | |
| 484 | |
| 485 yPos += NSHeight([sourcePopUpButton frame]); | |
| 486 | |
| 487 contentFrame = [view frame]; | |
| 488 contentFrame.size.height = yPos; | |
| 489 [view setFrame:contentFrame]; | |
| 490 | |
| 491 [self updateAdvancedView]; | |
| 492 | |
| 493 return view; | |
| 494 } | |
| 495 | |
| 496 - (void)updateAdvancedView { | |
| 497 if (alwaysTranslateCheckbox_) { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
No need to if-check - you can send messages to nil
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 498 NSInteger state = model_->ShouldAlwaysTranslate() ? NSOnState : NSOffState; | |
| 499 [alwaysTranslateCheckbox_ setState:state]; | |
| 500 } | |
| 501 | |
| 502 NSString* title; | |
| 503 if (model_->IsPageTranslatedInCurrentLanguages()) | |
| 504 title = l10n_util::GetNSStringWithFixup(IDS_DONE); | |
| 505 else | |
| 506 title = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); | |
| 507 [advancedDoneButton_ setTitle:title]; | |
| 508 [advancedDoneButton_ sizeToFit]; | |
| 509 | |
| 510 NSRect frame = [advancedDoneButton_ frame]; | |
| 511 frame.origin.x = (kWindowWidth - 2 * kFramePadding) - frame.size.width; | |
| 512 [advancedDoneButton_ setFrame:frame]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
setFrameOrigin, please, since you don't change siz
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 513 | |
| 514 frame = [advancedCancelButton_ frame]; | |
| 515 frame.origin.x = NSMinX([advancedDoneButton_ frame]) - frame.size.width | |
| 516 - kRelatedControlHorizontalSpacing; | |
| 517 [advancedCancelButton_ setFrame:frame]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
setFrameOrigin, please. And NSWidth(frame) instead
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 518 } | |
| 519 | |
| 520 // Create a new text field and add it to the given array of subviews. | |
| 521 // The array will retain a reference to the object. | |
| 522 - (NSTextField*)addText:(NSString*)text | |
| 523 withSize:(CGFloat)fontSize | |
|
groby-ooo-7-16
2014/03/21 01:21:42
You seem to call this always with [NSFont smallSys
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 524 bold:(BOOL)bold | |
| 525 toView:(NSView*)view { | |
| 526 CGFloat width = NSWidth([view frame]); | |
| 527 NSRect frame = NSMakeRect(0, 0, width, 100); | |
| 528 base::scoped_nsobject<NSTextField> textField( | |
| 529 [[NSTextField alloc] initWithFrame:frame]); | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Since you call -sizeToFit at the end anyways, you
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 530 [textField setEditable:NO]; | |
| 531 [textField setSelectable:YES]; | |
| 532 [textField setDrawsBackground:NO]; | |
| 533 [textField setBezeled:NO]; | |
| 534 [textField setStringValue:text]; | |
| 535 NSFont* font = bold ? [NSFont boldSystemFontOfSize:fontSize] | |
| 536 : [NSFont systemFontOfSize:fontSize]; | |
| 537 [textField setFont:font]; | |
| 538 | |
| 539 frame = [textField frame]; | |
| 540 frame.size.height += | |
| 541 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: | |
| 542 textField]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Since you call sizeToFit later, do you need to do
hajimehoshi
2014/03/24 07:16:30
Oh, calling GTM.. wasn't needed... Done.
| |
| 543 [textField setFrame:frame]; | |
| 544 | |
| 545 [textField setAutoresizingMask:NSViewWidthSizable]; | |
| 546 [view addSubview:textField.get()]; | |
| 547 | |
| 548 [textField sizeToFit]; | |
| 549 return textField.get(); | |
| 550 } | |
| 551 | |
| 552 - (NSButton*)addLinkButtonWithText:(NSString*)text | |
| 553 target:(id)target | |
|
groby-ooo-7-16
2014/03/21 01:21:42
Since |target| is always self, can you remove that
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 554 action:(SEL)action | |
| 555 toView:(NSView*)view { | |
| 556 base::scoped_nsobject<NSButton> button( | |
| 557 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
| 558 base::scoped_nsobject<HyperlinkButtonCell> cell( | |
| 559 [[HyperlinkButtonCell alloc] initTextCell:text]); | |
| 560 [cell setControlSize:NSSmallControlSize]; | |
| 561 [button setCell:cell.get()]; | |
|
groby-ooo-7-16
2014/03/21 01:21:42
No need to set the cell - you can just do [HyperLi
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 562 | |
| 563 [button setButtonType:NSMomentaryPushInButton]; | |
| 564 [button setBezelStyle:NSRegularSquareBezelStyle]; | |
| 565 [button sizeToFit]; | |
| 566 if (target != nil && action != nil) { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
You can just set target and action - it's OK for t
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 567 [button setTarget:target]; | |
| 568 [button setAction:action]; | |
| 569 } | |
| 570 | |
| 571 [view addSubview:button.get()]; | |
| 572 | |
| 573 return button.get(); | |
| 574 } | |
| 575 | |
| 576 - (NSButton*)addButton:(NSString*)title | |
| 577 target:(id)target | |
|
groby-ooo-7-16
2014/03/21 01:21:42
See above
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 578 action:(SEL)action | |
| 579 toView:(NSView*)view { | |
| 580 base::scoped_nsobject<NSButton> button( | |
| 581 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
| 582 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
| 583 [button setTitle:title]; | |
| 584 [button setBezelStyle:NSRoundedBezelStyle]; | |
| 585 [[button cell] setControlSize:NSSmallControlSize]; | |
| 586 [button sizeToFit]; | |
| 587 if (target != nil && action != nil) { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
See above
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 588 [button setTarget:target]; | |
| 589 [button setAction:action]; | |
| 590 } | |
| 591 | |
| 592 [view addSubview:button.get()]; | |
| 593 | |
| 594 return button.get(); | |
| 595 } | |
| 596 | |
| 597 - (NSButton*)addCheckbox:(NSString*)title | |
| 598 toView:(NSView*)view { | |
| 599 base::scoped_nsobject<NSButton> button( | |
| 600 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
| 601 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
| 602 [button setTitle:title]; | |
| 603 [[button cell] setControlSize:NSSmallControlSize]; | |
| 604 [button setButtonType:NSSwitchButton]; | |
| 605 [button sizeToFit]; | |
| 606 | |
| 607 [view addSubview:button.get()]; | |
| 608 | |
| 609 return button.get(); | |
| 610 } | |
| 611 | |
| 612 - (NSPopUpButton*)addPopUpButton:(ui::ComboboxModel*)model | |
| 613 target:(id)target | |
| 614 action:(SEL)action | |
| 615 toView:(NSView*)view { | |
| 616 base::scoped_nsobject<NSPopUpButton> button( | |
| 617 [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO]); | |
| 618 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
| 619 [button setBordered:YES]; | |
| 620 [[button cell] setControlSize:NSSmallControlSize]; | |
| 621 if (target != nil && action != nil) { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
See above
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 622 [button setTarget:target]; | |
| 623 [button setAction:action]; | |
| 624 } | |
| 625 | |
| 626 for (int i = 0; i < model->GetItemCount(); ++i) | |
| 627 [button addItemWithTitle:base::SysUTF16ToNSString(model->GetItemAt(i))]; | |
| 628 [button selectItemAtIndex:model->GetDefaultIndex()]; | |
| 629 | |
| 630 [button sizeToFit]; | |
| 631 | |
| 632 [view addSubview:button.get()]; | |
| 633 | |
| 634 return button.get(); | |
| 635 } | |
| 636 | |
| 637 - (void)handleTranslateButtonPressed { | |
| 638 translateExecuted_ = YES; | |
| 639 model_->Translate(); | |
| 640 } | |
| 641 | |
| 642 - (void)handleNopeButtonPressed { | |
| 643 [self close]; | |
| 644 } | |
| 645 | |
| 646 - (void)handleDoneButtonPressed { | |
| 647 if (alwaysTranslateCheckbox_) { | |
| 648 model_->SetAlwaysTranslate( | |
| 649 [alwaysTranslateCheckbox_ state] == NSOnState); | |
| 650 } | |
| 651 if (model_->IsPageTranslatedInCurrentLanguages()) { | |
| 652 model_->GoBackFromAdvanced(); | |
| 653 [self performLayout]; | |
| 654 } else { | |
| 655 translateExecuted_ = true; | |
| 656 model_->Translate(); | |
| 657 [self switchView:TranslateBubbleModel::VIEW_STATE_TRANSLATING]; | |
| 658 } | |
| 659 } | |
| 660 | |
| 661 - (void)handleCancelButtonPressed { | |
| 662 model_->GoBackFromAdvanced(); | |
| 663 [self performLayout]; | |
| 664 } | |
| 665 | |
| 666 - (void)handleShowOriginalButtonPressed { | |
| 667 model_->RevertTranslation(); | |
| 668 [self close]; | |
| 669 } | |
| 670 | |
| 671 - (void)handleAdvancedLinkButtonPressed { | |
| 672 [self switchView:TranslateBubbleModel::VIEW_STATE_ADVANCED]; | |
| 673 } | |
| 674 | |
| 675 - (void)handleDenialPopUpButtonSelectedItemChanged:(id)sender { | |
| 676 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
| 677 switch ([button indexOfSelectedItem]) { | |
|
groby-ooo-7-16
2014/03/21 01:21:42
You can directly set a target/action on the MenuIt
hajimehoshi
2014/03/24 07:16:30
Done.
| |
| 678 case TranslateDenialComboboxModel::kIndexNope: | |
| 679 break; | |
| 680 case TranslateDenialComboboxModel::kIndexNeverTranslateLanguage: | |
| 681 model_->SetNeverTranslateLanguage(true); | |
| 682 break; | |
| 683 case TranslateDenialComboboxModel::kIndexNeverTranslateSite: | |
| 684 model_->SetNeverTranslateSite(true); | |
| 685 break; | |
| 686 } | |
| 687 [self close]; | |
| 688 } | |
| 689 | |
| 690 - (void)handleSourceLanguagePopUpButtonSelectedItemChanged:(id)sender { | |
| 691 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
| 692 model_->UpdateOriginalLanguageIndex([button indexOfSelectedItem]); | |
| 693 [self updateAdvancedView]; | |
| 694 } | |
| 695 | |
| 696 - (void)handleTargetLanguagePopUpButtonSelectedItemChanged:(id)sender { | |
| 697 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
| 698 model_->UpdateTargetLanguageIndex([button indexOfSelectedItem]); | |
| 699 [self updateAdvancedView]; | |
| 76 } | 700 } |
| 77 | 701 |
| 78 @end | 702 @end |
| OLD | NEW |