OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chooser_content_view_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/chooser_content_view_cocoa.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 [table_view_ setHidden:NO]; | 308 [table_view_ setHidden:NO]; |
309 [spinner_ setHidden:YES]; | 309 [spinner_ setHidden:YES]; |
310 } | 310 } |
311 | 311 |
312 void ChooserContentViewController::OnOptionRemoved(size_t index) { | 312 void ChooserContentViewController::OnOptionRemoved(size_t index) { |
313 // |table_view_| will automatically select the removed item's next item. | 313 // |table_view_| will automatically select the removed item's next item. |
314 // So here it tracks if the removed item is the item that was currently | 314 // So here it tracks if the removed item is the item that was currently |
315 // selected, if so, deselect it. Also if the removed item is before the | 315 // selected, if so, deselect it. Also if the removed item is before the |
316 // currently selected item, the currently selected item's index needs to | 316 // currently selected item, the currently selected item's index needs to |
317 // be adjusted by one. | 317 // be adjusted by one. |
318 NSInteger idx = static_cast<NSInteger>(index); | 318 NSUInteger idx = static_cast<NSUInteger>(index); |
Robert Sesek
2016/11/22 19:07:29
Is the cast necessary? size_t should be the same a
juncai
2016/11/22 23:12:06
Done.
| |
319 NSInteger selected_row = [table_view_ selectedRow]; | 319 NSIndexSet* selected_rows = [table_view_ selectedRowIndexes]; |
320 if (selected_row == idx) { | 320 NSMutableIndexSet* updated_selected_rows = [NSMutableIndexSet indexSet]; |
321 [table_view_ deselectRow:idx]; | 321 NSUInteger row = [selected_rows firstIndex]; |
322 } else if (selected_row > idx) { | 322 while (row != NSNotFound) { |
323 [table_view_ | 323 if (row < idx) |
324 selectRowIndexes:[NSIndexSet indexSetWithIndex:selected_row - 1] | 324 [updated_selected_rows addIndex:row]; |
325 byExtendingSelection:NO]; | 325 else if (row > idx) |
326 [updated_selected_rows addIndex:row - 1]; | |
327 row = [selected_rows indexGreaterThanIndex:row]; | |
326 } | 328 } |
327 | 329 |
330 [table_view_ selectRowIndexes:updated_selected_rows byExtendingSelection:NO]; | |
331 | |
328 UpdateTableView(); | 332 UpdateTableView(); |
329 } | 333 } |
330 | 334 |
331 void ChooserContentViewController::OnOptionUpdated(size_t index) { | 335 void ChooserContentViewController::OnOptionUpdated(size_t index) { |
332 UpdateTableView(); | 336 UpdateTableView(); |
333 } | 337 } |
334 | 338 |
335 void ChooserContentViewController::OnAdapterEnabledChanged(bool enabled) { | 339 void ChooserContentViewController::OnAdapterEnabledChanged(bool enabled) { |
336 // No row is selected since the adapter status has changed. | 340 // No row is selected since the adapter status has changed. |
337 // This will also disable the OK button if it was enabled because | 341 // This will also disable the OK button if it was enabled because |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 // When complete, show |word_connector_| and |rescan_button_| and hide | 376 // When complete, show |word_connector_| and |rescan_button_| and hide |
373 // |scanning_message_|. | 377 // |scanning_message_|. |
374 [scanning_message_ setHidden:refreshing ? NO : YES]; | 378 [scanning_message_ setHidden:refreshing ? NO : YES]; |
375 [word_connector_ setHidden:refreshing ? YES : NO]; | 379 [word_connector_ setHidden:refreshing ? YES : NO]; |
376 [rescan_button_ setHidden:refreshing ? YES : NO]; | 380 [rescan_button_ setHidden:refreshing ? YES : NO]; |
377 } | 381 } |
378 | 382 |
379 void ChooserContentViewController::UpdateTableView() { | 383 void ChooserContentViewController::UpdateTableView() { |
380 [table_view_ setEnabled:chooser_controller_->NumOptions() > 0]; | 384 [table_view_ setEnabled:chooser_controller_->NumOptions() > 0]; |
381 // For NSView-based table views, calling reloadData will deselect the | 385 // For NSView-based table views, calling reloadData will deselect the |
382 // currently selected row, so |selected_row| stores the currently selected | 386 // currently selected row, so |selected_rows| stores the currently selected |
383 // row in order to select it again. | 387 // rows in order to select them again. |
384 NSInteger selected_row = [table_view_ selectedRow]; | 388 NSIndexSet* selected_rows = [table_view_ selectedRowIndexes]; |
385 [table_view_ reloadData]; | 389 [table_view_ reloadData]; |
386 if (selected_row != -1) { | 390 [table_view_ selectRowIndexes:selected_rows byExtendingSelection:NO]; |
387 [table_view_ selectRowIndexes:[NSIndexSet indexSetWithIndex:selected_row] | |
388 byExtendingSelection:NO]; | |
389 } | |
390 } | 391 } |
391 | 392 |
392 @implementation ChooserContentViewCocoa | 393 @implementation ChooserContentViewCocoa |
393 | 394 |
394 // TODO(juncai): restructure this function to be some smaller methods to | 395 // TODO(juncai): restructure this function to be some smaller methods to |
395 // create the pieces for the view. By doing so, the methods that calculate | 396 // create the pieces for the view. By doing so, the methods that calculate |
396 // the frame and origins can be moved into those methods, rather than as | 397 // the frame and origins can be moved into those methods, rather than as |
397 // helper functions. | 398 // helper functions. |
398 - (instancetype)initWithChooserTitle:(NSString*)chooserTitle | 399 - (instancetype)initWithChooserTitle:(NSString*)chooserTitle |
399 chooserController: | 400 chooserController: |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
493 [scrollView_ setDrawsBackground:NO]; | 494 [scrollView_ setDrawsBackground:NO]; |
494 | 495 |
495 // TableView. | 496 // TableView. |
496 tableView_.reset([[NSTableView alloc] initWithFrame:NSZeroRect]); | 497 tableView_.reset([[NSTableView alloc] initWithFrame:NSZeroRect]); |
497 tableColumn_.reset([[NSTableColumn alloc] initWithIdentifier:@""]); | 498 tableColumn_.reset([[NSTableColumn alloc] initWithIdentifier:@""]); |
498 [tableColumn_ setWidth:(scrollViewWidth - kMarginX)]; | 499 [tableColumn_ setWidth:(scrollViewWidth - kMarginX)]; |
499 [tableView_ addTableColumn:tableColumn_]; | 500 [tableView_ addTableColumn:tableColumn_]; |
500 // Make the column title invisible. | 501 // Make the column title invisible. |
501 [tableView_ setHeaderView:nil]; | 502 [tableView_ setHeaderView:nil]; |
502 [tableView_ setFocusRingType:NSFocusRingTypeNone]; | 503 [tableView_ setFocusRingType:NSFocusRingTypeNone]; |
504 [tableView_ | |
505 setAllowsMultipleSelection:chooserController_->AllowMultipleSelection() | |
506 ? YES | |
507 : NO]; | |
503 | 508 |
504 // Spinner. | 509 // Spinner. |
505 // Set the spinner in the center of the scroll view. | 510 // Set the spinner in the center of the scroll view. |
506 CGFloat spinnerOriginX = | 511 CGFloat spinnerOriginX = |
507 scrollViewOriginX + (scrollViewWidth - kSpinnerSize) / 2; | 512 scrollViewOriginX + (scrollViewWidth - kSpinnerSize) / 2; |
508 CGFloat spinnerOriginY = | 513 CGFloat spinnerOriginY = |
509 scrollViewOriginY + (scrollViewHeight - kSpinnerSize) / 2; | 514 scrollViewOriginY + (scrollViewHeight - kSpinnerSize) / 2; |
510 spinner_.reset([[SpinnerView alloc] | 515 spinner_.reset([[SpinnerView alloc] |
511 initWithFrame:NSMakeRect(spinnerOriginX, spinnerOriginY, kSpinnerSize, | 516 initWithFrame:NSMakeRect(spinnerOriginX, spinnerOriginY, kSpinnerSize, |
512 kSpinnerSize)]); | 517 kSpinnerSize)]); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
787 | 792 |
788 return base::SysUTF16ToNSString( | 793 return base::SysUTF16ToNSString( |
789 chooserController_->GetOption(static_cast<size_t>(index))); | 794 chooserController_->GetOption(static_cast<size_t>(index))); |
790 } | 795 } |
791 | 796 |
792 - (void)updateTableView { | 797 - (void)updateTableView { |
793 chooserContentViewController_->UpdateTableView(); | 798 chooserContentViewController_->UpdateTableView(); |
794 } | 799 } |
795 | 800 |
796 - (void)accept { | 801 - (void)accept { |
797 chooserController_->Select([tableView_ selectedRow]); | 802 NSIndexSet* selectedRows = [tableView_ selectedRowIndexes]; |
803 NSUInteger index = [selectedRows firstIndex]; | |
804 while (index != NSNotFound) { | |
805 chooserController_->Select(index); | |
806 index = [selectedRows indexGreaterThanIndex:index]; | |
807 } | |
798 } | 808 } |
799 | 809 |
800 - (void)cancel { | 810 - (void)cancel { |
801 chooserController_->Cancel(); | 811 chooserController_->Cancel(); |
802 } | 812 } |
803 | 813 |
804 - (void)close { | 814 - (void)close { |
805 chooserController_->Close(); | 815 chooserController_->Close(); |
806 } | 816 } |
807 | 817 |
808 - (void)onAdapterOffHelp:(id)sender { | 818 - (void)onAdapterOffHelp:(id)sender { |
809 chooserController_->OpenAdapterOffHelpUrl(); | 819 chooserController_->OpenAdapterOffHelpUrl(); |
810 } | 820 } |
811 | 821 |
812 - (void)onRescan:(id)sender { | 822 - (void)onRescan:(id)sender { |
813 chooserController_->RefreshOptions(); | 823 chooserController_->RefreshOptions(); |
814 } | 824 } |
815 | 825 |
816 - (void)onHelpPressed:(id)sender { | 826 - (void)onHelpPressed:(id)sender { |
817 chooserController_->OpenHelpCenterUrl(); | 827 chooserController_->OpenHelpCenterUrl(); |
818 } | 828 } |
819 | 829 |
820 - (void)updateContentRowColor { | 830 - (void)updateContentRowColor { |
821 NSInteger selectedRow = [tableView_ selectedRow]; | 831 NSIndexSet* selectedRows = [tableView_ selectedRowIndexes]; |
822 NSInteger numOptions = | 832 NSInteger numOptions = |
823 base::checked_cast<NSInteger>(chooserController_->NumOptions()); | 833 base::checked_cast<NSInteger>(chooserController_->NumOptions()); |
824 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 834 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
825 for (NSInteger rowIndex = 0; rowIndex < numOptions; ++rowIndex) { | 835 for (NSInteger rowIndex = 0; rowIndex < numOptions; ++rowIndex) { |
836 BOOL isSelected = [selectedRows containsIndex:rowIndex]; | |
826 // Update the color of the text. | 837 // Update the color of the text. |
827 [[self tableRowViewText:rowIndex] | 838 [[self tableRowViewText:rowIndex] |
828 setTextColor:(rowIndex == selectedRow ? [NSColor whiteColor] | 839 setTextColor:(isSelected ? [NSColor whiteColor] |
829 : [NSColor blackColor])]; | 840 : [NSColor blackColor])]; |
830 | 841 |
831 // Update the color of the image. | 842 // Update the color of the image. |
832 if (chooserController_->ShouldShowIconBeforeText()) { | 843 if (chooserController_->ShouldShowIconBeforeText()) { |
833 if (chooserController_->IsConnected(rowIndex)) { | 844 if (chooserController_->IsConnected(rowIndex)) { |
834 [[self tableRowViewImage:rowIndex] | 845 [[self tableRowViewImage:rowIndex] |
835 setImage:gfx::NSImageFromImageSkia(gfx::CreateVectorIcon( | 846 setImage:gfx::NSImageFromImageSkia(gfx::CreateVectorIcon( |
836 gfx::VectorIconId::BLUETOOTH_CONNECTED, | 847 gfx::VectorIconId::BLUETOOTH_CONNECTED, |
837 rowIndex == selectedRow ? SK_ColorWHITE | 848 isSelected ? SK_ColorWHITE : gfx::kChromeIconGrey))]; |
838 : gfx::kChromeIconGrey))]; | |
839 } else { | 849 } else { |
840 int signalStrengthLevel = | 850 int signalStrengthLevel = |
841 chooserController_->GetSignalStrengthLevel(rowIndex); | 851 chooserController_->GetSignalStrengthLevel(rowIndex); |
842 if (signalStrengthLevel != -1) { | 852 if (signalStrengthLevel != -1) { |
843 int imageId = | 853 int imageId = |
844 rowIndex == selectedRow | 854 isSelected |
845 ? kSignalStrengthLevelImageSelectedIds[signalStrengthLevel] | 855 ? kSignalStrengthLevelImageSelectedIds[signalStrengthLevel] |
846 : kSignalStrengthLevelImageIds[signalStrengthLevel]; | 856 : kSignalStrengthLevelImageIds[signalStrengthLevel]; |
847 [[self tableRowViewImage:rowIndex] | 857 [[self tableRowViewImage:rowIndex] |
848 setImage:rb.GetNativeImageNamed(imageId).ToNSImage()]; | 858 setImage:rb.GetNativeImageNamed(imageId).ToNSImage()]; |
849 } | 859 } |
850 } | 860 } |
851 } | 861 } |
852 | 862 |
853 // Update the color of paired status. | 863 // Update the color of paired status. |
854 NSTextField* pairedStatusText = [self tableRowViewPairedStatus:rowIndex]; | 864 NSTextField* pairedStatusText = [self tableRowViewPairedStatus:rowIndex]; |
855 if (pairedStatusText) { | 865 if (pairedStatusText) { |
856 [pairedStatusText | 866 [pairedStatusText setTextColor:(skia::SkColorToCalibratedNSColor( |
857 setTextColor:(skia::SkColorToCalibratedNSColor( | 867 isSelected ? gfx::kGoogleGreen300 |
858 rowIndex == selectedRow ? gfx::kGoogleGreen300 | 868 : gfx::kGoogleGreen700))]; |
859 : gfx::kGoogleGreen700))]; | |
860 } | 869 } |
861 } | 870 } |
862 } | 871 } |
863 | 872 |
864 - (NSImageView*)tableRowViewImage:(NSInteger)row { | 873 - (NSImageView*)tableRowViewImage:(NSInteger)row { |
865 ChooserContentTableRowView* tableRowView = | 874 ChooserContentTableRowView* tableRowView = |
866 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; | 875 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; |
867 return [tableRowView image]; | 876 return [tableRowView image]; |
868 } | 877 } |
869 | 878 |
(...skipping 11 matching lines...) Expand all Loading... | |
881 | 890 |
882 - (void)drawRect:(NSRect)rect { | 891 - (void)drawRect:(NSRect)rect { |
883 [[NSColor colorWithCalibratedWhite:245.0f / 255.0f alpha:1.0f] setFill]; | 892 [[NSColor colorWithCalibratedWhite:245.0f / 255.0f alpha:1.0f] setFill]; |
884 NSRect footnoteFrame = | 893 NSRect footnoteFrame = |
885 NSMakeRect(0.0f, 0.0f, kChooserWidth, separatorOriginY_); | 894 NSMakeRect(0.0f, 0.0f, kChooserWidth, separatorOriginY_); |
886 NSRectFill(footnoteFrame); | 895 NSRectFill(footnoteFrame); |
887 [super drawRect:footnoteFrame]; | 896 [super drawRect:footnoteFrame]; |
888 } | 897 } |
889 | 898 |
890 @end | 899 @end |
OLD | NEW |