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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 const CGFloat kSeparatorHeight = 1.0f; | 57 const CGFloat kSeparatorHeight = 1.0f; |
58 | 58 |
59 // Distance between two views inside the table row view. | 59 // Distance between two views inside the table row view. |
60 const CGFloat kTableRowViewHorizontalPadding = 5.0f; | 60 const CGFloat kTableRowViewHorizontalPadding = 5.0f; |
61 const CGFloat kTableRowViewVerticalPadding = 1.0f; | 61 const CGFloat kTableRowViewVerticalPadding = 1.0f; |
62 | 62 |
63 // The lookup table for signal strength level image. | 63 // The lookup table for signal strength level image. |
64 const int kSignalStrengthLevelImageIds[5] = {IDR_SIGNAL_0_BAR, IDR_SIGNAL_1_BAR, | 64 const int kSignalStrengthLevelImageIds[5] = {IDR_SIGNAL_0_BAR, IDR_SIGNAL_1_BAR, |
65 IDR_SIGNAL_2_BAR, IDR_SIGNAL_3_BAR, | 65 IDR_SIGNAL_2_BAR, IDR_SIGNAL_3_BAR, |
66 IDR_SIGNAL_4_BAR}; | 66 IDR_SIGNAL_4_BAR}; |
| 67 const int kSignalStrengthLevelImageSelectedIds[5] = { |
| 68 IDR_SIGNAL_0_BAR_SELECTED, IDR_SIGNAL_1_BAR_SELECTED, |
| 69 IDR_SIGNAL_2_BAR_SELECTED, IDR_SIGNAL_3_BAR_SELECTED, |
| 70 IDR_SIGNAL_4_BAR_SELECTED}; |
67 | 71 |
68 // Creates a label with |text|. | 72 // Creates a label with |text|. |
69 base::scoped_nsobject<NSTextField> CreateLabel(NSString* text) { | 73 base::scoped_nsobject<NSTextField> CreateLabel(NSString* text) { |
70 base::scoped_nsobject<NSTextField> label( | 74 base::scoped_nsobject<NSTextField> label( |
71 [[NSTextField alloc] initWithFrame:NSZeroRect]); | 75 [[NSTextField alloc] initWithFrame:NSZeroRect]); |
72 [label setDrawsBackground:NO]; | 76 [label setDrawsBackground:NO]; |
73 [label setBezeled:NO]; | 77 [label setBezeled:NO]; |
74 [label setEditable:NO]; | 78 [label setEditable:NO]; |
75 [label setSelectable:NO]; | 79 [label setSelectable:NO]; |
76 [label setStringValue:text]; | 80 [label setStringValue:text]; |
77 [label setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; | 81 [label setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; |
| 82 [label setTextColor:[NSColor blackColor]]; |
78 [label sizeToFit]; | 83 [label sizeToFit]; |
79 return label; | 84 return label; |
80 } | 85 } |
81 | 86 |
82 } // namespace | 87 } // namespace |
83 | 88 |
84 // A table row view that contains one line of text, and optionally contains an | 89 // A table row view that contains one line of text, and optionally contains an |
85 // image in front of the text. | 90 // image in front of the text. |
86 @interface ChooserContentTableRowView : NSView { | 91 @interface ChooserContentTableRowView : NSView { |
87 @private | 92 @private |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 } | 296 } |
292 | 297 |
293 void ChooserContentViewController::OnOptionRemoved(size_t index) { | 298 void ChooserContentViewController::OnOptionRemoved(size_t index) { |
294 // |table_view_| will automatically select the removed item's next item. | 299 // |table_view_| will automatically select the removed item's next item. |
295 // So here it tracks if the removed item is the item that was currently | 300 // So here it tracks if the removed item is the item that was currently |
296 // selected, if so, deselect it. Also if the removed item is before the | 301 // selected, if so, deselect it. Also if the removed item is before the |
297 // currently selected item, the currently selected item's index needs to | 302 // currently selected item, the currently selected item's index needs to |
298 // be adjusted by one. | 303 // be adjusted by one. |
299 NSInteger idx = static_cast<NSInteger>(index); | 304 NSInteger idx = static_cast<NSInteger>(index); |
300 NSInteger selected_row = [table_view_ selectedRow]; | 305 NSInteger selected_row = [table_view_ selectedRow]; |
301 if (selected_row == idx) | 306 if (selected_row == idx) { |
302 [table_view_ deselectRow:idx]; | 307 [table_view_ deselectRow:idx]; |
303 else if (selected_row > idx) | 308 } else if (selected_row > idx) { |
304 [table_view_ | 309 [table_view_ |
305 selectRowIndexes:[NSIndexSet indexSetWithIndex:selected_row - 1] | 310 selectRowIndexes:[NSIndexSet indexSetWithIndex:selected_row - 1] |
306 byExtendingSelection:NO]; | 311 byExtendingSelection:NO]; |
| 312 } |
307 | 313 |
308 UpdateTableView(); | 314 UpdateTableView(); |
309 } | 315 } |
310 | 316 |
311 void ChooserContentViewController::OnOptionUpdated(size_t index) { | 317 void ChooserContentViewController::OnOptionUpdated(size_t index) { |
312 UpdateTableView(); | 318 UpdateTableView(); |
313 } | 319 } |
314 | 320 |
315 void ChooserContentViewController::OnAdapterEnabledChanged(bool enabled) { | 321 void ChooserContentViewController::OnAdapterEnabledChanged(bool enabled) { |
316 // No row is selected since the adapter status has changed. | 322 // No row is selected since the adapter status has changed. |
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 } | 804 } |
799 | 805 |
800 - (void)onRescan:(id)sender { | 806 - (void)onRescan:(id)sender { |
801 chooserController_->RefreshOptions(); | 807 chooserController_->RefreshOptions(); |
802 } | 808 } |
803 | 809 |
804 - (void)onHelpPressed:(id)sender { | 810 - (void)onHelpPressed:(id)sender { |
805 chooserController_->OpenHelpCenterUrl(); | 811 chooserController_->OpenHelpCenterUrl(); |
806 } | 812 } |
807 | 813 |
| 814 - (void)updateContentRowColor { |
| 815 NSInteger selectedRow = [tableView_ selectedRow]; |
| 816 NSInteger numOptions = |
| 817 base::checked_cast<NSInteger>(chooserController_->NumOptions()); |
| 818 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 819 for (NSInteger rowIndex = 0; rowIndex < numOptions; ++rowIndex) { |
| 820 // Update the color of the text. |
| 821 [[self tableRowViewText:rowIndex] |
| 822 setTextColor:(rowIndex == selectedRow ? [NSColor whiteColor] |
| 823 : [NSColor blackColor])]; |
| 824 |
| 825 // Update the color of the image. |
| 826 if (chooserController_->ShouldShowIconBeforeText()) { |
| 827 if (chooserController_->IsConnected(rowIndex)) { |
| 828 [[self tableRowViewImage:rowIndex] |
| 829 setImage:gfx::NSImageFromImageSkia(gfx::CreateVectorIcon( |
| 830 gfx::VectorIconId::BLUETOOTH_CONNECTED, |
| 831 rowIndex == selectedRow ? SK_ColorWHITE |
| 832 : gfx::kChromeIconGrey))]; |
| 833 } else { |
| 834 int signalStrengthLevel = |
| 835 chooserController_->GetSignalStrengthLevel(rowIndex); |
| 836 if (signalStrengthLevel != -1) { |
| 837 int imageId = |
| 838 rowIndex == selectedRow |
| 839 ? kSignalStrengthLevelImageSelectedIds[signalStrengthLevel] |
| 840 : kSignalStrengthLevelImageIds[signalStrengthLevel]; |
| 841 [[self tableRowViewImage:rowIndex] |
| 842 setImage:rb.GetNativeImageNamed(imageId).ToNSImage()]; |
| 843 } |
| 844 } |
| 845 } |
| 846 |
| 847 // Update the color of paired status. |
| 848 NSTextField* pairedStatusText = [self tableRowViewPairedStatus:rowIndex]; |
| 849 if (pairedStatusText) { |
| 850 [pairedStatusText |
| 851 setTextColor:(skia::SkColorToCalibratedNSColor( |
| 852 rowIndex == selectedRow ? gfx::kGoogleGreen300 |
| 853 : gfx::kGoogleGreen700))]; |
| 854 } |
| 855 } |
| 856 } |
| 857 |
808 - (NSImageView*)tableRowViewImage:(NSInteger)row { | 858 - (NSImageView*)tableRowViewImage:(NSInteger)row { |
809 ChooserContentTableRowView* tableRowView = | 859 ChooserContentTableRowView* tableRowView = |
810 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; | 860 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; |
811 return [tableRowView image]; | 861 return [tableRowView image]; |
812 } | 862 } |
813 | 863 |
814 - (NSTextField*)tableRowViewText:(NSInteger)row { | 864 - (NSTextField*)tableRowViewText:(NSInteger)row { |
815 ChooserContentTableRowView* tableRowView = | 865 ChooserContentTableRowView* tableRowView = |
816 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; | 866 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; |
817 return [tableRowView text]; | 867 return [tableRowView text]; |
818 } | 868 } |
819 | 869 |
820 - (NSTextField*)tableRowViewPairedStatus:(NSInteger)row { | 870 - (NSTextField*)tableRowViewPairedStatus:(NSInteger)row { |
821 ChooserContentTableRowView* tableRowView = | 871 ChooserContentTableRowView* tableRowView = |
822 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; | 872 [tableView_ viewAtColumn:0 row:row makeIfNecessary:YES]; |
823 return [tableRowView pairedStatus]; | 873 return [tableRowView pairedStatus]; |
824 } | 874 } |
825 | 875 |
826 @end | 876 @end |
OLD | NEW |