OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ui/app_list/cocoa/apps_grid_controller.h" | 5 #import "ui/app_list/cocoa/apps_grid_controller.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "ui/app_list/app_list_model.h" | 8 #include "ui/app_list/app_list_model.h" |
9 #include "ui/app_list/app_list_model_observer.h" | 9 #include "ui/app_list/app_list_model_observer.h" |
10 #include "ui/app_list/app_list_view_delegate.h" | 10 #include "ui/app_list/app_list_view_delegate.h" |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 return NO; | 596 return NO; |
597 | 597 |
598 NSUInteger oldIndex = [self selectedItemIndex]; | 598 NSUInteger oldIndex = [self selectedItemIndex]; |
599 | 599 |
600 // If nothing is currently selected, select the first item on the page. | 600 // If nothing is currently selected, select the first item on the page. |
601 if (oldIndex == NSNotFound) { | 601 if (oldIndex == NSNotFound) { |
602 [self selectItemAtIndex:visiblePage_ * kItemsPerPage]; | 602 [self selectItemAtIndex:visiblePage_ * kItemsPerPage]; |
603 return YES; | 603 return YES; |
604 } | 604 } |
605 | 605 |
606 if ((indexDelta < 0 && static_cast<NSUInteger>(-indexDelta) > oldIndex) || | 606 // Can't select a negative index. |
607 oldIndex + indexDelta >= [items_ count]) { | 607 if (indexDelta < 0 && static_cast<NSUInteger>(-indexDelta) > oldIndex) |
608 return NO; | 608 return NO; |
| 609 |
| 610 // Can't select an index greater or equal to the number of items. |
| 611 if (oldIndex + indexDelta >= [items_ count]) { |
| 612 if (visiblePage_ == [pages_ count] - 1) |
| 613 return NO; |
| 614 |
| 615 // If we're not on the last page, then select the last item. |
| 616 [self selectItemAtIndex:[items_ count] - 1]; |
| 617 return YES; |
609 } | 618 } |
610 | 619 |
611 [self selectItemAtIndex:oldIndex + indexDelta]; | 620 [self selectItemAtIndex:oldIndex + indexDelta]; |
612 return YES; | 621 return YES; |
613 } | 622 } |
614 | 623 |
615 - (void)selectItemAtIndex:(NSUInteger)index { | 624 - (void)selectItemAtIndex:(NSUInteger)index { |
616 if (index >= [items_ count]) | 625 if (index >= [items_ count]) |
617 return; | 626 return; |
618 | 627 |
619 if (index / kItemsPerPage != visiblePage_) | 628 if (index / kItemsPerPage != visiblePage_) |
620 [self scrollToPage:index / kItemsPerPage]; | 629 [self scrollToPage:index / kItemsPerPage]; |
621 | 630 |
622 [[self itemAtIndex:index] setSelected:YES]; | 631 [[self itemAtIndex:index] setSelected:YES]; |
623 } | 632 } |
624 | 633 |
625 - (BOOL)handleCommandBySelector:(SEL)command { | 634 - (BOOL)handleCommandBySelector:(SEL)command { |
626 if (command == @selector(insertNewline:) || | 635 if (command == @selector(insertNewline:) || |
627 command == @selector(insertLineBreak:)) { | 636 command == @selector(insertLineBreak:)) { |
628 [self activateSelection]; | 637 [self activateSelection]; |
629 return YES; | 638 return YES; |
630 } | 639 } |
631 | 640 |
632 if (command == @selector(moveLeft:)) | 641 NSUInteger oldIndex = [self selectedItemIndex]; |
633 return [self moveSelectionByDelta:-1]; | 642 // If nothing is currently selected, select the first item on the page. |
| 643 if (oldIndex == NSNotFound) { |
| 644 [self selectItemAtIndex:visiblePage_ * kItemsPerPage]; |
| 645 return YES; |
| 646 } |
634 | 647 |
635 if (command == @selector(moveRight:)) | 648 if (command == @selector(moveLeft:)) { |
636 return [self moveSelectionByDelta:1]; | 649 return oldIndex % kFixedColumns == 0 ? |
| 650 [self moveSelectionByDelta:-kItemsPerPage + kFixedColumns - 1] : |
| 651 [self moveSelectionByDelta:-1]; |
| 652 } |
637 | 653 |
638 if (command == @selector(moveUp:)) | 654 if (command == @selector(moveRight:)) { |
639 return [self moveSelectionByDelta:-kFixedColumns]; | 655 return oldIndex % kFixedColumns == kFixedColumns - 1 ? |
| 656 [self moveSelectionByDelta:+kItemsPerPage - kFixedColumns + 1] : |
| 657 [self moveSelectionByDelta:1]; |
| 658 } |
640 | 659 |
641 if (command == @selector(moveDown:)) | 660 if (command == @selector(moveUp:)) { |
642 return [self moveSelectionByDelta:kFixedColumns]; | 661 return oldIndex / kFixedColumns % kFixedRows == 0 ? |
| 662 NO : [self moveSelectionByDelta:-kFixedColumns]; |
| 663 } |
| 664 |
| 665 if (command == @selector(moveDown:)) { |
| 666 return oldIndex / kFixedColumns % kFixedRows == kFixedRows - 1 ? |
| 667 NO : [self moveSelectionByDelta:kFixedColumns]; |
| 668 } |
643 | 669 |
644 if (command == @selector(pageUp:) || | 670 if (command == @selector(pageUp:) || |
645 command == @selector(scrollPageUp:)) | 671 command == @selector(scrollPageUp:)) |
646 return [self moveSelectionByDelta:-kItemsPerPage]; | 672 return [self moveSelectionByDelta:-kItemsPerPage]; |
647 | 673 |
648 if (command == @selector(pageDown:) || | 674 if (command == @selector(pageDown:) || |
649 command == @selector(scrollPageDown:)) | 675 command == @selector(scrollPageDown:)) |
650 return [self moveSelectionByDelta:kItemsPerPage]; | 676 return [self moveSelectionByDelta:kItemsPerPage]; |
651 | 677 |
652 return NO; | 678 return NO; |
653 } | 679 } |
654 | 680 |
655 @end | 681 @end |
OLD | NEW |