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" | |
groby-ooo-7-16
2014/03/25 18:49:05
You don't need that any more.
hajimehoshi
2014/03/26 11:06:05
Done.
| |
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 { | |
26 public: | |
27 explicit TranslateDenialComboboxModel( | |
28 const base::string16& original_language_name) { | |
29 // Dummy menu item, which is shown on the top of a NSPopUpButton. The top | |
30 // text of the denial pop up menu should be IDS_TRANSLATE_BUBBLE_DENY, while | |
31 // it is impossible to use it here because NSPopUpButtons' addItemWithTitle | |
32 // removes a duplicated menu item. Instead, the title will be set later by | |
33 // NSMenuItem's setTitle. | |
groby-ooo-7-16
2014/03/25 18:49:05
Having an entry twice in a popup menu is counter t
hajimehoshi
2014/03/26 11:06:05
The context is that we wanted the 'fancy' button (
groby-ooo-7-16
2014/03/27 00:39:39
Ah, I understand. Thank you for the explanation, a
| |
34 items_.push_back(base::string16()); | |
35 | |
36 // Menu items in the drop down menu. | |
37 items_.push_back(l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); | |
38 items_.push_back(l10n_util::GetStringFUTF16( | |
39 IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_LANG, | |
40 original_language_name)); | |
41 items_.push_back(l10n_util::GetStringUTF16( | |
42 IDS_TRANSLATE_BUBBLE_NEVER_TRANSLATE_SITE)); | |
43 } | |
44 virtual ~TranslateDenialComboboxModel() {} | |
45 | |
46 private: | |
47 // ComboboxModel: | |
48 virtual int GetItemCount() const OVERRIDE { | |
49 return items_.size(); | |
50 } | |
51 virtual base::string16 GetItemAt(int index) OVERRIDE { | |
52 return items_[index]; | |
53 } | |
54 virtual bool IsItemSeparatorAt(int index) OVERRIDE { | |
55 return false; | |
56 } | |
57 virtual int GetDefaultIndex() const OVERRIDE { | |
58 return 0; | |
59 } | |
60 | |
61 std::vector<base::string16> items_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(TranslateDenialComboboxModel); | |
64 }; | |
65 | |
66 const CGFloat kWindowWidth = 320; | |
67 | |
68 // Padding between the window frame and content. | |
69 const CGFloat kFramePadding = 16; | |
70 | |
71 const CGFloat kRelatedControlHorizontalSpacing = -2; | |
72 | |
73 const CGFloat kRelatedControlVerticalSpacing = 4; | |
74 const CGFloat kUnrelatedControlVerticalSpacing = 20; | |
75 | |
76 const CGFloat kContentWidth = kWindowWidth - 2 * kFramePadding; | |
16 | 77 |
17 @implementation TranslateBubbleController | 78 @implementation TranslateBubbleController |
18 | 79 |
19 - (id)initWithParentWindow:(BrowserWindowController*)controller | 80 - (id)initWithParentWindow:(BrowserWindowController*)controller |
20 model:(scoped_ptr<TranslateBubbleModel>)model | 81 model:(scoped_ptr<TranslateBubbleModel>)model |
21 webContents:(content::WebContents*)webContents { | 82 webContents:(content::WebContents*)webContents { |
22 NSWindow* parentWindow = [controller window]; | 83 NSWindow* parentWindow = [controller window]; |
23 | 84 |
24 // Use an arbitrary size; it will be changed in performLayout. | 85 // Use an arbitrary size; it will be changed in performLayout. |
25 NSRect contentRect = ui::kWindowSizeDeterminedLater; | 86 NSRect contentRect = ui::kWindowSizeDeterminedLater; |
26 base::scoped_nsobject<InfoBubbleWindow> window( | 87 base::scoped_nsobject<InfoBubbleWindow> window( |
27 [[InfoBubbleWindow alloc] initWithContentRect:contentRect | 88 [[InfoBubbleWindow alloc] initWithContentRect:contentRect |
28 styleMask:NSBorderlessWindowMask | 89 styleMask:NSBorderlessWindowMask |
29 backing:NSBackingStoreBuffered | 90 backing:NSBackingStoreBuffered |
30 defer:NO]); | 91 defer:NO]); |
31 | 92 |
32 if ((self = [super initWithWindow:window | 93 if ((self = [super initWithWindow:window |
33 parentWindow:parentWindow | 94 parentWindow:parentWindow |
34 anchoredAt:NSZeroPoint])) { | 95 anchoredAt:NSZeroPoint])) { |
35 webContents_ = webContents; | 96 webContents_ = webContents; |
36 model_ = model.Pass(); | 97 model_ = model.Pass(); |
37 if (model_->GetViewState() != | 98 if (model_->GetViewState() != |
38 TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { | 99 TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE) { |
39 translateExecuted_ = YES; | 100 translateExecuted_ = YES; |
40 } | 101 } |
41 | 102 |
103 beforeTranslateView_.reset([self createViewBeforeTranslate]); | |
104 translatingView_.reset([self createViewTranslating]); | |
105 afterTranslateView_.reset([self createViewAfterTranslate]); | |
106 errorView_.reset([self createViewError]); | |
107 advancedView_.reset([self createViewAdvanced]); | |
108 | |
42 [self performLayout]; | 109 [self performLayout]; |
43 } | 110 } |
44 return self; | 111 return self; |
45 } | 112 } |
46 | 113 |
47 @synthesize webContents = webContents_; | 114 @synthesize webContents = webContents_; |
48 | 115 |
116 - (NSView*)currentView { | |
117 switch (model_->GetViewState()) { | |
118 case TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE: | |
119 return beforeTranslateView_; | |
120 case TranslateBubbleModel::VIEW_STATE_TRANSLATING: | |
121 return translatingView_; | |
122 case TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE: | |
123 return afterTranslateView_; | |
124 case TranslateBubbleModel::VIEW_STATE_ERROR: | |
125 return errorView_; | |
126 case TranslateBubbleModel::VIEW_STATE_ADVANCED: | |
127 return advancedView_; | |
128 } | |
129 NOTREACHED(); | |
130 return nil; | |
131 } | |
132 | |
49 - (const TranslateBubbleModel*)model { | 133 - (const TranslateBubbleModel*)model { |
50 return model_.get(); | 134 return model_.get(); |
51 } | 135 } |
52 | 136 |
53 - (void)showWindow:(id)sender { | 137 - (void)showWindow:(id)sender { |
54 BrowserWindowController* controller = [[self parentWindow] windowController]; | 138 BrowserWindowController* controller = [[self parentWindow] windowController]; |
55 NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; | 139 NSPoint anchorPoint = [[controller toolbarController] translateBubblePoint]; |
56 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint]; | 140 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint]; |
57 [self setAnchorPoint:anchorPoint]; | 141 [self setAnchorPoint:anchorPoint]; |
58 [super showWindow:sender]; | 142 [super showWindow:sender]; |
59 } | 143 } |
60 | 144 |
61 - (void)switchView:(TranslateBubbleModel::ViewState)viewState { | 145 - (void)switchView:(TranslateBubbleModel::ViewState)viewState { |
62 if (model_->GetViewState() == viewState) | 146 if (model_->GetViewState() == viewState) |
63 return; | 147 return; |
64 | 148 |
65 model_->SetViewState(viewState); | 149 model_->SetViewState(viewState); |
66 [self performLayout]; | 150 [self performLayout]; |
67 } | 151 } |
68 | 152 |
69 - (void)switchToErrorView:(TranslateErrors::Type)errorType { | 153 - (void)switchToErrorView:(TranslateErrors::Type)errorType { |
70 // TODO(hajimehoshi): Implement this. | 154 // TODO(hajimehoshi): Implement this. |
71 } | 155 } |
72 | 156 |
73 - (void)performLayout { | 157 - (void)performLayout { |
74 // TODO(hajimehoshi): Now this shows just an empty bubble. Implement this. | 158 NSWindow* window = [self window]; |
75 [[self window] display]; | 159 [[window contentView] setSubviews:@[ [self currentView] ]]; |
160 | |
161 CGFloat height = NSHeight([[self currentView] frame]) + | |
162 2 * kFramePadding + info_bubble::kBubbleArrowHeight; | |
163 | |
164 NSRect windowFrame = [window contentRectForFrameRect:[[self window] frame]]; | |
165 NSRect newWindowFrame = [window frameRectForContentRect:NSMakeRect( | |
166 NSMinX(windowFrame), NSMaxY(windowFrame) - height, kWindowWidth, height)]; | |
167 [window setFrame:newWindowFrame | |
168 display:YES | |
169 animate:[[self window] isVisible]]; | |
170 } | |
171 | |
172 - (NSView*)createViewBeforeTranslate { | |
groby-ooo-7-16
2014/03/25 18:49:05
Please declare private methods in a private interf
hajimehoshi
2014/03/26 11:06:05
Done. As you mentioned before, should I split this
| |
173 NSRect contentFrame = NSMakeRect( | |
174 kFramePadding, | |
175 kFramePadding, | |
176 kContentWidth, | |
177 0); | |
178 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
179 | |
180 NSString* message = | |
181 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_BEFORE_TRANSLATE); | |
182 NSTextField* textLabel = [self addText:message | |
183 toView:view]; | |
184 message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); | |
185 NSButton* advancedLinkButton = | |
186 [self addLinkButtonWithText:message | |
187 action:@selector(handleAdvancedLinkButtonPressed) | |
188 toView:view]; | |
189 | |
190 NSString* title = | |
191 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); | |
192 NSButton* translateButton = | |
193 [self addButton:title | |
194 action:@selector(handleTranslateButtonPressed) | |
195 toView:view]; | |
196 | |
197 base::string16 originalLanguageName = | |
198 model_->GetLanguageNameAt(model_->GetOriginalLanguageIndex()); | |
199 translateDenialComboboxModel_.reset( | |
200 new TranslateDenialComboboxModel(originalLanguageName)); | |
groby-ooo-7-16
2014/03/25 18:49:05
Note for later CL's: When you factor out the Trans
hajimehoshi
2014/03/26 11:06:05
Thanks.
| |
201 NSPopUpButton* denyPopUpButton = | |
202 [self addPopUpButton:translateDenialComboboxModel_.get() | |
203 action:nil | |
204 toView:view]; | |
205 [denyPopUpButton setPullsDown:YES]; | |
206 [[denyPopUpButton itemAtIndex:1] setTarget:self]; | |
207 [[denyPopUpButton itemAtIndex:1] | |
208 setAction:@selector(handleDenialPopUpButtonNopeSelected)]; | |
209 [[denyPopUpButton itemAtIndex:2] setTarget:self]; | |
210 [[denyPopUpButton itemAtIndex:2] | |
211 setAction:@selector(handleDenialPopUpButtonNeverTranslateLanguageSelected)]; | |
212 [[denyPopUpButton itemAtIndex:3] setTarget:self]; | |
213 [[denyPopUpButton itemAtIndex:3] | |
214 setAction:@selector(handleDenialPopUpButtonNeverTranslateSiteSelected)]; | |
215 | |
216 title = base::SysUTF16ToNSString( | |
217 l10n_util::GetStringUTF16(IDS_TRANSLATE_BUBBLE_DENY)); | |
218 [[denyPopUpButton itemAtIndex:0] setTitle:title]; | |
219 | |
220 // Adjust width for the first item. | |
221 std::vector<NSString*> titles; | |
222 NSArray* items = [denyPopUpButton itemArray]; | |
223 int index = 0; | |
224 for (NSMenuItem* item in items) { | |
225 if (index != 0) { | |
groby-ooo-7-16
2014/03/25 18:49:05
No need for this check (or for tracking index). It
hajimehoshi
2014/03/26 11:06:05
Thanks, but I used your suggestion of copying NSCe
| |
226 titles.push_back([item title]); | |
227 [item setTitle:@""]; | |
228 } | |
229 index++; | |
230 } | |
231 [denyPopUpButton sizeToFit]; | |
232 for (int i = 1; i < [denyPopUpButton numberOfItems]; i++) { | |
233 NSMenuItem* item = [denyPopUpButton itemAtIndex:i]; | |
234 [item setTitle:titles[i-1]]; | |
235 } | |
236 | |
237 // Layout | |
238 CGFloat yPos = 0; | |
239 | |
240 [translateButton setFrameOrigin:NSMakePoint( | |
241 kContentWidth - NSWidth([translateButton frame]), yPos)]; | |
242 | |
243 NSRect denyPopUpButtonFrame = [denyPopUpButton frame]; | |
244 CGFloat diffY = [[denyPopUpButton cell] | |
245 titleRectForBounds:[denyPopUpButton bounds]].origin.y; | |
246 [denyPopUpButton setFrameOrigin:NSMakePoint( | |
247 NSMinX([translateButton frame]) - denyPopUpButtonFrame.size.width | |
248 - kRelatedControlHorizontalSpacing, | |
249 yPos + diffY)]; | |
250 | |
251 yPos += NSHeight([translateButton frame]) + | |
252 kUnrelatedControlVerticalSpacing; | |
253 | |
254 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
255 [advancedLinkButton setFrameOrigin:NSMakePoint( | |
256 NSWidth([textLabel frame]), yPos)]; | |
257 | |
258 [view setFrameSize:NSMakeSize(kContentWidth, NSMaxY([textLabel frame]))]; | |
259 | |
260 return view; | |
261 } | |
262 | |
263 - (NSView*)createViewTranslating { | |
groby-ooo-7-16
2014/03/25 18:49:05
See above re: name
hajimehoshi
2014/03/26 11:06:05
Done.
| |
264 NSRect contentFrame = NSMakeRect( | |
265 kFramePadding, | |
266 kFramePadding, | |
267 kContentWidth, | |
268 0); | |
269 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
270 | |
271 NSString* message = | |
272 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATING); | |
273 NSTextField* textLabel = [self addText:message | |
274 toView:view]; | |
275 NSString* title = | |
276 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); | |
277 NSButton* showOriginalButton = | |
278 [self addButton:title | |
279 action:@selector(handleShowOriginalButtonPressed) | |
280 toView:view]; | |
281 [showOriginalButton setEnabled:NO]; | |
282 | |
283 // Layout | |
284 CGFloat yPos = 0; | |
285 | |
286 [showOriginalButton setFrameOrigin:NSMakePoint( | |
287 kContentWidth - NSWidth([showOriginalButton frame]), yPos)]; | |
288 | |
289 yPos += NSHeight([showOriginalButton frame]) + | |
290 kUnrelatedControlVerticalSpacing; | |
291 | |
292 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
293 | |
294 [view setFrameSize:NSMakeSize(kContentWidth, NSMaxY([textLabel frame]))]; | |
295 | |
296 return view; | |
297 } | |
298 | |
299 - (NSView*)createViewAfterTranslate { | |
300 NSRect contentFrame = NSMakeRect( | |
301 kFramePadding, | |
302 kFramePadding, | |
303 kContentWidth, | |
304 0); | |
305 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
306 | |
307 NSString* message = | |
308 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_TRANSLATED); | |
309 NSTextField* textLabel = [self addText:message | |
310 toView:view]; | |
311 message = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ADVANCED); | |
312 NSButton* advancedLinkButton = | |
313 [self addLinkButtonWithText:message | |
314 action:@selector(handleAdvancedLinkButtonPressed) | |
315 toView:view]; | |
316 NSString* title = | |
317 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_REVERT); | |
318 NSButton* showOriginalButton = | |
319 [self addButton:title | |
320 action:@selector(handleShowOriginalButtonPressed) | |
321 toView:view]; | |
322 | |
323 // Layout | |
324 CGFloat yPos = 0; | |
325 | |
326 [showOriginalButton setFrameOrigin:NSMakePoint( | |
327 kContentWidth - NSWidth([showOriginalButton frame]), yPos)]; | |
328 | |
329 yPos += NSHeight([showOriginalButton frame]) + | |
330 kUnrelatedControlVerticalSpacing; | |
331 | |
332 [textLabel setFrameOrigin:NSMakePoint(0, yPos)]; | |
333 [advancedLinkButton setFrameOrigin:NSMakePoint( | |
334 NSMaxX([textLabel frame]), yPos)]; | |
335 | |
336 [view setFrameSize:NSMakeSize(kContentWidth, NSMaxY([textLabel frame]))]; | |
337 | |
338 return view; | |
339 } | |
340 | |
341 - (NSView*)createViewError { | |
342 NSRect contentFrame = NSMakeRect( | |
343 kFramePadding, | |
344 kFramePadding, | |
345 kContentWidth, | |
346 0); | |
347 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
348 | |
349 // TODO(hajimehoshi): Implement this. | |
350 | |
351 return view; | |
352 } | |
353 | |
354 - (NSView*)createViewAdvanced { | |
355 NSRect contentFrame = NSMakeRect( | |
356 kFramePadding, | |
357 kFramePadding, | |
358 kContentWidth, | |
359 0); | |
360 NSView* view = [[NSView alloc] initWithFrame:contentFrame]; | |
361 | |
362 NSString* title = l10n_util::GetNSStringWithFixup( | |
363 IDS_TRANSLATE_BUBBLE_PAGE_LANGUAGE); | |
364 NSTextField* sourceLanguageLabel = [self addText:title | |
365 toView:view]; | |
366 title = l10n_util::GetNSStringWithFixup( | |
367 IDS_TRANSLATE_BUBBLE_TRANSLATION_LANGUAGE); | |
368 NSTextField* targetLanguageLabel = [self addText:title | |
369 toView:view]; | |
370 | |
371 // combobox | |
372 int sourceDefaultIndex = model_->GetOriginalLanguageIndex(); | |
373 int targetDefaultIndex = model_->GetTargetLanguageIndex(); | |
374 sourceLanguageComboboxModel_.reset( | |
375 new LanguageComboboxModel(sourceDefaultIndex, model_.get())); | |
376 targetLanguageComboboxModel_.reset( | |
377 new LanguageComboboxModel(targetDefaultIndex, model_.get())); | |
378 SEL action = @selector(handleSourceLanguagePopUpButtonSelectedItemChanged:); | |
379 NSPopUpButton* sourcePopUpButton = | |
380 [self addPopUpButton:sourceLanguageComboboxModel_.get() | |
381 action:action | |
382 toView:view]; | |
383 action = @selector(handleTargetLanguagePopUpButtonSelectedItemChanged:); | |
384 NSPopUpButton* targetPopUpButton = | |
385 [self addPopUpButton:targetLanguageComboboxModel_.get() | |
386 action:action | |
387 toView:view]; | |
388 | |
389 // 'Always translate' checkbox | |
390 BOOL isIncognitoWindow = webContents_ ? | |
391 webContents_->GetBrowserContext()->IsOffTheRecord() : NO; | |
392 if (!isIncognitoWindow) { | |
393 NSString* title = | |
394 l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ALWAYS); | |
395 alwaysTranslateCheckbox_ = [self addCheckbox:title | |
396 toView:view]; | |
397 } | |
398 | |
399 // Buttons | |
400 advancedDoneButton_ = | |
401 [self addButton:l10n_util::GetNSStringWithFixup(IDS_DONE) | |
402 action:@selector(handleDoneButtonPressed) | |
403 toView:view]; | |
404 advancedCancelButton_ = | |
405 [self addButton:l10n_util::GetNSStringWithFixup(IDS_CANCEL) | |
406 action:@selector(handleCancelButtonPressed) | |
407 toView:view]; | |
408 | |
409 // Layout | |
410 CGFloat textLabelWidth = NSWidth([sourceLanguageLabel frame]); | |
411 if (textLabelWidth < NSWidth([targetLanguageLabel frame])) | |
412 textLabelWidth = NSWidth([targetLanguageLabel frame]); | |
413 | |
414 CGFloat yPos = 0; | |
415 | |
416 [advancedDoneButton_ setFrameOrigin:NSMakePoint(0, yPos)]; | |
417 [advancedCancelButton_ setFrameOrigin:NSMakePoint(0, yPos)]; | |
418 | |
419 yPos += NSHeight([advancedDoneButton_ frame]) + | |
420 kUnrelatedControlVerticalSpacing; | |
421 | |
422 if (alwaysTranslateCheckbox_) { | |
423 [alwaysTranslateCheckbox_ setFrameOrigin:NSMakePoint(textLabelWidth, yPos)]; | |
424 | |
425 yPos += NSHeight([alwaysTranslateCheckbox_ frame]) + | |
426 kRelatedControlVerticalSpacing; | |
427 } | |
428 | |
429 CGFloat diffY = [[sourcePopUpButton cell] | |
430 titleRectForBounds:[sourcePopUpButton bounds]].origin.y; | |
431 | |
432 [targetLanguageLabel setFrameOrigin:NSMakePoint( | |
433 textLabelWidth - NSWidth([targetLanguageLabel frame]), yPos + diffY)]; | |
434 | |
435 NSRect frame = [targetPopUpButton frame]; | |
436 frame.origin = NSMakePoint(textLabelWidth, yPos); | |
437 frame.size.width = (kWindowWidth - 2 * kFramePadding) - textLabelWidth; | |
438 [targetPopUpButton setFrame:frame]; | |
439 | |
440 yPos += NSHeight([targetPopUpButton frame]) + | |
441 kRelatedControlVerticalSpacing; | |
442 | |
443 [sourceLanguageLabel setFrameOrigin:NSMakePoint( | |
444 textLabelWidth - NSWidth([sourceLanguageLabel frame]), yPos + diffY)]; | |
445 | |
446 frame = [sourcePopUpButton frame]; | |
447 frame.origin = NSMakePoint(textLabelWidth, yPos); | |
448 frame.size.width = NSWidth([targetPopUpButton frame]); | |
groby-ooo-7-16
2014/03/25 18:49:05
Are you sure you don't want target & source width
hajimehoshi
2014/03/26 11:06:05
The sets of source and target languages are same,
| |
449 [sourcePopUpButton setFrame:frame]; | |
450 | |
451 [view setFrameSize:NSMakeSize(kContentWidth, | |
452 NSMaxY([sourcePopUpButton frame]))]; | |
453 | |
454 [self updateAdvancedView]; | |
455 | |
456 return view; | |
457 } | |
458 | |
459 - (void)updateAdvancedView { | |
460 NSInteger state = model_->ShouldAlwaysTranslate() ? NSOnState : NSOffState; | |
461 [alwaysTranslateCheckbox_ setState:state]; | |
462 | |
463 NSString* title; | |
464 if (model_->IsPageTranslatedInCurrentLanguages()) | |
465 title = l10n_util::GetNSStringWithFixup(IDS_DONE); | |
466 else | |
467 title = l10n_util::GetNSStringWithFixup(IDS_TRANSLATE_BUBBLE_ACCEPT); | |
468 [advancedDoneButton_ setTitle:title]; | |
469 [advancedDoneButton_ sizeToFit]; | |
470 | |
471 NSRect frame = [advancedDoneButton_ frame]; | |
472 frame.origin.x = (kWindowWidth - 2 * kFramePadding) - NSWidth(frame); | |
473 [advancedDoneButton_ setFrameOrigin:frame.origin]; | |
474 | |
475 frame = [advancedCancelButton_ frame]; | |
476 frame.origin.x = NSMinX([advancedDoneButton_ frame]) - NSWidth(frame) | |
477 - kRelatedControlHorizontalSpacing; | |
478 [advancedCancelButton_ setFrameOrigin:frame.origin]; | |
479 } | |
480 | |
481 - (NSTextField*)addText:(NSString*)text | |
482 toView:(NSView*)view { | |
483 base::scoped_nsobject<NSTextField> textField( | |
484 [[NSTextField alloc] initWithFrame:NSZeroRect]); | |
485 [textField setEditable:NO]; | |
486 [textField setSelectable:YES]; | |
487 [textField setDrawsBackground:NO]; | |
488 [textField setBezeled:NO]; | |
489 [textField setStringValue:text]; | |
490 NSFont* font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; | |
491 [textField setFont:font]; | |
492 [textField setAutoresizingMask:NSViewWidthSizable]; | |
493 [view addSubview:textField.get()]; | |
494 | |
495 [textField sizeToFit]; | |
496 return textField.get(); | |
497 } | |
498 | |
499 - (NSButton*)addLinkButtonWithText:(NSString*)text | |
500 action:(SEL)action | |
501 toView:(NSView*)view { | |
502 base::scoped_nsobject<NSButton> button( | |
503 [[HyperlinkButtonCell buttonWithString:text] retain]); | |
504 | |
505 [button setButtonType:NSMomentaryPushInButton]; | |
506 [button setBezelStyle:NSRegularSquareBezelStyle]; | |
507 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
508 [button sizeToFit]; | |
509 [button setTarget:self]; | |
510 [button setAction:action]; | |
511 | |
512 [view addSubview:button.get()]; | |
513 | |
514 return button.get(); | |
515 } | |
516 | |
517 - (NSButton*)addButton:(NSString*)title | |
518 action:(SEL)action | |
519 toView:(NSView*)view { | |
520 base::scoped_nsobject<NSButton> button( | |
521 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
522 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
523 [button setTitle:title]; | |
524 [button setBezelStyle:NSRoundedBezelStyle]; | |
525 [[button cell] setControlSize:NSSmallControlSize]; | |
526 [button sizeToFit]; | |
527 [button setTarget:self]; | |
528 [button setAction:action]; | |
529 | |
530 [view addSubview:button.get()]; | |
531 | |
532 return button.get(); | |
533 } | |
534 | |
535 - (NSButton*)addCheckbox:(NSString*)title | |
536 toView:(NSView*)view { | |
537 base::scoped_nsobject<NSButton> button( | |
538 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
539 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
540 [button setTitle:title]; | |
541 [[button cell] setControlSize:NSSmallControlSize]; | |
542 [button setButtonType:NSSwitchButton]; | |
543 [button sizeToFit]; | |
544 | |
545 [view addSubview:button.get()]; | |
546 | |
547 return button.get(); | |
548 } | |
549 | |
550 - (NSPopUpButton*)addPopUpButton:(ui::ComboboxModel*)model | |
551 action:(SEL)action | |
552 toView:(NSView*)view { | |
553 base::scoped_nsobject<NSPopUpButton> button( | |
554 [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO]); | |
555 [button setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | |
556 [button setBordered:YES]; | |
557 [[button cell] setControlSize:NSSmallControlSize]; | |
558 [button setTarget:self]; | |
559 [button setAction:action]; | |
560 | |
561 for (int i = 0; i < model->GetItemCount(); ++i) | |
groby-ooo-7-16
2014/03/25 18:49:05
This works, but doesn't account for separators. Ar
hajimehoshi
2014/03/26 11:06:05
Correct. I'll fix this. Sorry but I have to leave
hajimehoshi
2014/03/27 10:37:28
Done.
| |
562 [button addItemWithTitle:base::SysUTF16ToNSString(model->GetItemAt(i))]; | |
563 [button selectItemAtIndex:model->GetDefaultIndex()]; | |
564 | |
565 [button sizeToFit]; | |
566 | |
567 [view addSubview:button.get()]; | |
568 | |
569 return button.get(); | |
570 } | |
571 | |
572 - (void)handleTranslateButtonPressed { | |
groby-ooo-7-16
2014/03/25 18:49:05
I'd think most of the handleXYZ callbacks could li
hajimehoshi
2014/03/26 11:06:05
Not planned, but it seems not easy. Let me think a
| |
573 translateExecuted_ = YES; | |
574 model_->Translate(); | |
575 } | |
576 | |
577 - (void)handleNopeButtonPressed { | |
578 [self close]; | |
579 } | |
580 | |
581 - (void)handleDoneButtonPressed { | |
582 if (alwaysTranslateCheckbox_) { | |
583 model_->SetAlwaysTranslate( | |
584 [alwaysTranslateCheckbox_ state] == NSOnState); | |
585 } | |
586 if (model_->IsPageTranslatedInCurrentLanguages()) { | |
587 model_->GoBackFromAdvanced(); | |
588 [self performLayout]; | |
589 } else { | |
590 translateExecuted_ = true; | |
591 model_->Translate(); | |
592 [self switchView:TranslateBubbleModel::VIEW_STATE_TRANSLATING]; | |
593 } | |
594 } | |
595 | |
596 - (void)handleCancelButtonPressed { | |
597 model_->GoBackFromAdvanced(); | |
598 [self performLayout]; | |
599 } | |
600 | |
601 - (void)handleShowOriginalButtonPressed { | |
602 model_->RevertTranslation(); | |
603 [self close]; | |
604 } | |
605 | |
606 - (void)handleAdvancedLinkButtonPressed { | |
607 [self switchView:TranslateBubbleModel::VIEW_STATE_ADVANCED]; | |
608 } | |
609 | |
610 - (void)handleDenialPopUpButtonNopeSelected { | |
611 [self close]; | |
612 } | |
613 | |
614 - (void)handleDenialPopUpButtonNeverTranslateLanguageSelected { | |
615 model_->SetNeverTranslateLanguage(true); | |
616 [self close]; | |
617 } | |
618 | |
619 - (void)handleDenialPopUpButtonNeverTranslateSiteSelected { | |
620 model_->SetNeverTranslateSite(true); | |
621 [self close]; | |
622 } | |
623 | |
624 - (void)handleSourceLanguagePopUpButtonSelectedItemChanged:(id)sender { | |
625 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
626 model_->UpdateOriginalLanguageIndex([button indexOfSelectedItem]); | |
627 [self updateAdvancedView]; | |
628 } | |
629 | |
630 - (void)handleTargetLanguagePopUpButtonSelectedItemChanged:(id)sender { | |
631 NSPopUpButton* button = base::mac::ObjCCastStrict<NSPopUpButton>(sender); | |
632 model_->UpdateTargetLanguageIndex([button indexOfSelectedItem]); | |
633 [self updateAdvancedView]; | |
76 } | 634 } |
77 | 635 |
78 @end | 636 @end |
OLD | NEW |