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/web_intent_sheet_controller.h" | 5 #import "chrome/browser/ui/cocoa/web_intent_sheet_controller.h" |
6 | 6 |
7 #include "base/memory/scoped_nsobject.h" | 7 #include "base/memory/scoped_nsobject.h" |
8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/ui/browser_list.h" | 10 #include "chrome/browser/ui/browser_list.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 NSButton* button = [[NSButton alloc] initWithFrame:frame]; | 65 NSButton* button = [[NSButton alloc] initWithFrame:frame]; |
66 scoped_nsobject<HyperlinkButtonCell> cell( | 66 scoped_nsobject<HyperlinkButtonCell> cell( |
67 [[HyperlinkButtonCell alloc] initTextCell:title]); | 67 [[HyperlinkButtonCell alloc] initTextCell:title]); |
68 [cell setControlSize:NSSmallControlSize]; | 68 [cell setControlSize:NSSmallControlSize]; |
69 [button setCell:cell.get()]; | 69 [button setCell:cell.get()]; |
70 [button setButtonType:NSMomentaryPushInButton]; | 70 [button setButtonType:NSMomentaryPushInButton]; |
71 [button setBezelStyle:NSRegularSquareBezelStyle]; | 71 [button setBezelStyle:NSRegularSquareBezelStyle]; |
72 | 72 |
73 return button; | 73 return button; |
74 } | 74 } |
75 | |
76 // Sets properties on the given |field| to act as title or description labels. | |
77 + (void)configureTextFieldAsLabel:(NSTextField*)field { | |
Nico
2012/05/16 22:02:45
nit: static methods can usually just be static top
groby-ooo-7-16
2012/05/16 23:27:02
Done.
| |
78 [field setEditable:NO]; | |
79 [field setSelectable:YES]; | |
80 [field setDrawsBackground:NO]; | |
81 [field setBezeled:NO]; | |
82 } | |
75 @end | 83 @end |
76 | 84 |
77 // This simple NSView subclass is used as the single subview of the page info | 85 // This simple NSView subclass is used as the single subview of the page info |
78 // bubble's window's contentView. Drawing is flipped so that layout of the | 86 // bubble's window's contentView. Drawing is flipped so that layout of the |
79 // sections is easier. Apple recommends flipping the coordinate origin when | 87 // sections is easier. Apple recommends flipping the coordinate origin when |
80 // doing a lot of text layout because it's more natural. | 88 // doing a lot of text layout because it's more natural. |
81 @interface WebIntentsContentView : NSView | 89 @interface WebIntentsContentView : NSView |
82 @end | 90 @end |
83 @implementation WebIntentsContentView | 91 @implementation WebIntentsContentView |
84 - (BOOL)isFlipped { | 92 - (BOOL)isFlipped { |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 [throbber_ stopAnimation:self]; | 327 [throbber_ stopAnimation:self]; |
320 [installButton_ setHidden:NO]; | 328 [installButton_ setHidden:NO]; |
321 } | 329 } |
322 | 330 |
323 @end | 331 @end |
324 | 332 |
325 @interface SuggestionView : NSView { | 333 @interface SuggestionView : NSView { |
326 @private | 334 @private |
327 // Used to forward button clicks. Weak reference. | 335 // Used to forward button clicks. Weak reference. |
328 WebIntentPickerSheetController* controller_; | 336 WebIntentPickerSheetController* controller_; |
337 scoped_nsobject<NSTextField> suggestionLabel_; | |
329 } | 338 } |
330 | 339 |
331 - (id)initWithModel:(WebIntentPickerModel*)model | 340 - (id)initWithModel:(WebIntentPickerModel*)model |
332 forController:(WebIntentPickerSheetController*)controller; | 341 forController:(WebIntentPickerSheetController*)controller; |
333 - (void)startThrobberForRow:(NSInteger)index; | 342 - (void)startThrobberForRow:(NSInteger)index; |
334 - (void)stopThrobber; | 343 - (void)stopThrobber; |
335 @end | 344 @end |
336 | 345 |
337 @implementation SuggestionView | 346 @implementation SuggestionView |
338 - (id)initWithModel:(WebIntentPickerModel*)model | 347 - (id)initWithModel:(WebIntentPickerModel*)model |
339 forController:(WebIntentPickerSheetController*)controller { | 348 forController:(WebIntentPickerSheetController*)controller { |
340 const CGFloat kYMargin = 16.0; | 349 const CGFloat kYMargin = 16.0; |
341 size_t count = model->GetSuggestedExtensionCount(); | 350 size_t count = model->GetSuggestedExtensionCount(); |
342 if (count == 0) | 351 if (count == 0) |
343 return nil; | 352 return nil; |
344 | 353 |
345 NSMutableArray* subviews = [NSMutableArray array]; | 354 NSMutableArray* subviews = [NSMutableArray array]; |
346 | 355 |
356 NSRect textFrame = NSMakeRect(0, 0, | |
357 kTextWidth, 1); | |
358 suggestionLabel_.reset([[NSTextField alloc] initWithFrame:textFrame]); | |
359 [WebIntentPickerSheetController configureTextFieldAsLabel:suggestionLabel_]; | |
360 | |
347 CGFloat offset = kYMargin; | 361 CGFloat offset = kYMargin; |
348 for (size_t i = count; i > 0; --i) { | 362 for (size_t i = count; i > 0; --i) { |
349 const WebIntentPickerModel::SuggestedExtension& ext = | 363 const WebIntentPickerModel::SuggestedExtension& ext = |
350 model->GetSuggestedExtensionAt(i - 1); | 364 model->GetSuggestedExtensionAt(i - 1); |
351 scoped_nsobject<NSView> suggestView( | 365 scoped_nsobject<NSView> suggestView( |
352 [[SingleSuggestionView alloc] initWithExtension:&ext | 366 [[SingleSuggestionView alloc] initWithExtension:&ext |
353 withIndex:i-1 | 367 withIndex:i-1 |
354 forController:controller]); | 368 forController:controller]); |
355 offset += [self addStackedView:suggestView.get() | 369 offset += [self addStackedView:suggestView.get() |
356 toSubviews:subviews | 370 toSubviews:subviews |
357 atOffset:offset]; | 371 atOffset:offset]; |
358 } | 372 } |
373 | |
374 [self updateSuggestionLabelForModel:model]; | |
375 offset += [self addStackedView:suggestionLabel_ | |
376 toSubviews:subviews | |
377 atOffset:offset]; | |
378 | |
359 offset += kYMargin; | 379 offset += kYMargin; |
360 | 380 |
361 NSRect contentFrame = NSMakeRect(kFramePadding, 0, kWindowWidth, offset); | 381 NSRect contentFrame = NSMakeRect(kFramePadding, 0, kWindowWidth, offset); |
362 if(self = [super initWithFrame:contentFrame]) | 382 if(self = [super initWithFrame:contentFrame]) |
363 [self setSubviews: subviews]; | 383 [self setSubviews: subviews]; |
364 | 384 |
365 controller_ = controller; | 385 controller_ = controller; |
366 return self; | 386 return self; |
367 } | 387 } |
368 | 388 |
389 - (void)updateSuggestionLabelForModel:(WebIntentPickerModel*)model { | |
390 DCHECK(suggestionLabel_.get()); | |
391 string16 labelText; | |
392 | |
393 if (model->GetInstalledServiceCount()!=0) { | |
Nico
2012/05/16 22:02:45
spaces around !=
groby-ooo-7-16
2012/05/16 23:27:02
Done.
| |
394 if (model->GetInstalledServiceCount() == 0) | |
Nico
2012/05/16 22:02:45
wait what? isn't that the negation of the previous
groby-ooo-7-16
2012/05/16 23:27:02
No, it won't. The first condition was wrong. Fixed
| |
395 labelText = l10n_util::GetStringUTF16( | |
396 IDS_INTENT_PICKER_GET_MORE_SERVICES_NONE_INSTALLED); | |
397 else | |
398 labelText = l10n_util::GetStringUTF16( | |
399 IDS_INTENT_PICKER_GET_MORE_SERVICES); | |
400 } | |
401 | |
402 if (labelText.empty()) { | |
403 [suggestionLabel_ setHidden:TRUE]; | |
404 } else { | |
405 NSRect textFrame = [suggestionLabel_ frame]; | |
406 | |
407 [suggestionLabel_ setHidden:FALSE]; | |
408 [suggestionLabel_ setStringValue:base::SysUTF16ToNSString(labelText)]; | |
409 textFrame.size.height += | |
410 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: | |
411 suggestionLabel_]; | |
412 [suggestionLabel_ setFrame: textFrame]; | |
413 } | |
414 } | |
415 | |
369 - (void)startThrobberForRow:(NSInteger)index { | 416 - (void)startThrobberForRow:(NSInteger)index { |
370 for (SingleSuggestionView* row in [self subviews]) { | 417 for (SingleSuggestionView* row in [self subviews]) { |
371 [row setEnabled:NO]; | 418 [row setEnabled:NO]; |
372 if ([row tag] == index) | 419 if ([row tag] == index) |
373 [row startThrobber]; | 420 [row startThrobber]; |
374 } | 421 } |
375 } | 422 } |
376 | 423 |
377 - (void)stopThrobber { | 424 - (void)stopThrobber { |
378 for (SingleSuggestionView* row in [self subviews]) { | 425 for (SingleSuggestionView* row in [self subviews]) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
484 picker_->OnExtensionInstallRequested(UTF16ToUTF8(extension.id)); | 531 picker_->OnExtensionInstallRequested(UTF16ToUTF8(extension.id)); |
485 } | 532 } |
486 } | 533 } |
487 | 534 |
488 - (void)setIntentButtonsEnabled:(BOOL)enabled { | 535 - (void)setIntentButtonsEnabled:(BOOL)enabled { |
489 for (NSButton* button in intentButtons_.get()) { | 536 for (NSButton* button in intentButtons_.get()) { |
490 [button setEnabled:enabled]; | 537 [button setEnabled:enabled]; |
491 } | 538 } |
492 } | 539 } |
493 | 540 |
494 // Sets properties on the given |field| to act as title or description labels. | |
495 - (void)configureTextFieldAsLabel:(NSTextField*)field { | |
496 [field setEditable:NO]; | |
497 [field setSelectable:YES]; | |
498 [field setDrawsBackground:NO]; | |
499 [field setBezeled:NO]; | |
500 } | |
501 | |
502 - (CGFloat)addStackedView:(NSView*)view | 541 - (CGFloat)addStackedView:(NSView*)view |
503 toSubviews:(NSMutableArray*)subviews | 542 toSubviews:(NSMutableArray*)subviews |
504 atOffset:(CGFloat)offset { | 543 atOffset:(CGFloat)offset { |
505 if (view == nil) | 544 if (view == nil) |
506 return 0.0; | 545 return 0.0; |
507 | 546 |
508 NSPoint frameOrigin = [view frame].origin; | 547 NSPoint frameOrigin = [view frame].origin; |
509 frameOrigin.y = offset; | 548 frameOrigin.y = offset; |
510 [view setFrameOrigin:frameOrigin]; | 549 [view setFrameOrigin:frameOrigin]; |
511 [subviews addObject:view]; | 550 [subviews addObject:view]; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 [NSArray arrayWithObject:contentView]]; | 725 [NSArray arrayWithObject:contentView]]; |
687 } | 726 } |
688 | 727 |
689 - (void)setActionString:(NSString*)actionString { | 728 - (void)setActionString:(NSString*)actionString { |
690 NSRect textFrame; | 729 NSRect textFrame; |
691 if (!actionTextField_.get()) { | 730 if (!actionTextField_.get()) { |
692 textFrame = NSMakeRect(kFramePadding, 0, | 731 textFrame = NSMakeRect(kFramePadding, 0, |
693 kTextWidth, 1); | 732 kTextWidth, 1); |
694 | 733 |
695 actionTextField_.reset([[NSTextField alloc] initWithFrame:textFrame]); | 734 actionTextField_.reset([[NSTextField alloc] initWithFrame:textFrame]); |
696 [self configureTextFieldAsLabel:actionTextField_.get()]; | 735 [WebIntentPickerSheetController configureTextFieldAsLabel: |
736 actionTextField_]; | |
697 [actionTextField_ setFont:[NSFont systemFontOfSize:kHeaderFontSize]]; | 737 [actionTextField_ setFont:[NSFont systemFontOfSize:kHeaderFontSize]]; |
698 } else { | 738 } else { |
699 textFrame = [actionTextField_ frame]; | 739 textFrame = [actionTextField_ frame]; |
700 } | 740 } |
701 | 741 |
702 [actionTextField_ setStringValue:actionString]; | 742 [actionTextField_ setStringValue:actionString]; |
703 textFrame.size.height += | 743 textFrame.size.height += |
704 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: | 744 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField: |
705 actionTextField_]; | 745 actionTextField_]; |
706 [actionTextField_ setFrame: textFrame]; | 746 [actionTextField_ setFrame: textFrame]; |
707 } | 747 } |
708 | 748 |
709 - (void)stopThrobber { | 749 - (void)stopThrobber { |
710 [closeButton_ setEnabled:YES]; | 750 [closeButton_ setEnabled:YES]; |
711 [self setIntentButtonsEnabled:YES]; | 751 [self setIntentButtonsEnabled:YES]; |
712 [suggestionView_ stopThrobber]; | 752 [suggestionView_ stopThrobber]; |
713 } | 753 } |
714 | 754 |
715 - (void)closeSheet { | 755 - (void)closeSheet { |
716 [NSApp endSheet:[self window]]; | 756 [NSApp endSheet:[self window]]; |
717 } | 757 } |
718 @end // WebIntentPickerSheetController | 758 @end // WebIntentPickerSheetController |
OLD | NEW |