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 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 return NO; | 528 return NO; |
529 | 529 |
530 NSUInteger oldIndex = [self selectedItemIndex]; | 530 NSUInteger oldIndex = [self selectedItemIndex]; |
531 | 531 |
532 // If nothing is currently selected, select the first item on the page. | 532 // If nothing is currently selected, select the first item on the page. |
533 if (oldIndex == NSNotFound) { | 533 if (oldIndex == NSNotFound) { |
534 [self selectItemAtIndex:visiblePage_ * kItemsPerPage]; | 534 [self selectItemAtIndex:visiblePage_ * kItemsPerPage]; |
535 return YES; | 535 return YES; |
536 } | 536 } |
537 | 537 |
538 if ((indexDelta < 0 && static_cast<NSUInteger>(-indexDelta) > oldIndex) || | 538 // Can't select a negative index. |
539 oldIndex + indexDelta >= [items_ count]) { | 539 if (indexDelta < 0 && static_cast<NSUInteger>(-indexDelta) > oldIndex) |
540 return NO; | 540 return NO; |
| 541 |
| 542 // Can't select an index greater or equal to the number of items. |
| 543 if (oldIndex + indexDelta >= [items_ count]) { |
| 544 if (visiblePage_ == [pages_ count] - 1) |
| 545 return NO; |
| 546 |
| 547 // If we're not on the last page, then select the last item. |
| 548 [self selectItemAtIndex:[items_ count] - 1]; |
| 549 return YES; |
541 } | 550 } |
542 | 551 |
543 [self selectItemAtIndex:oldIndex + indexDelta]; | 552 [self selectItemAtIndex:oldIndex + indexDelta]; |
544 return YES; | 553 return YES; |
545 } | 554 } |
546 | 555 |
547 - (void)selectItemAtIndex:(NSUInteger)index { | 556 - (void)selectItemAtIndex:(NSUInteger)index { |
548 if (index >= [items_ count]) | 557 if (index >= [items_ count]) |
549 return; | 558 return; |
550 | 559 |
551 if (index / kItemsPerPage != visiblePage_) | 560 if (index / kItemsPerPage != visiblePage_) |
552 [self scrollToPage:index / kItemsPerPage]; | 561 [self scrollToPage:index / kItemsPerPage]; |
553 | 562 |
554 [[self itemAtIndex:index] setSelected:YES]; | 563 [[self itemAtIndex:index] setSelected:YES]; |
555 } | 564 } |
556 | 565 |
557 - (BOOL)handleCommandBySelector:(SEL)command { | 566 - (BOOL)handleCommandBySelector:(SEL)command { |
558 if (command == @selector(insertNewline:) || | 567 if (command == @selector(insertNewline:) || |
559 command == @selector(insertLineBreak:)) { | 568 command == @selector(insertLineBreak:)) { |
560 [self activateSelection]; | 569 [self activateSelection]; |
561 return YES; | 570 return YES; |
562 } | 571 } |
563 | 572 |
564 if (command == @selector(moveLeft:)) | 573 NSUInteger oldIndex = [self selectedItemIndex]; |
565 return [self moveSelectionByDelta:-1]; | 574 // If nothing is currently selected, select the first item on the page. |
| 575 if (oldIndex == NSNotFound) { |
| 576 [self selectItemAtIndex:visiblePage_ * kItemsPerPage]; |
| 577 return YES; |
| 578 } |
566 | 579 |
567 if (command == @selector(moveRight:)) | 580 if (command == @selector(moveLeft:)) { |
568 return [self moveSelectionByDelta:1]; | 581 return oldIndex % kFixedColumns == 0 ? |
| 582 [self moveSelectionByDelta:-kItemsPerPage + kFixedColumns - 1] : |
| 583 [self moveSelectionByDelta:-1]; |
| 584 } |
569 | 585 |
570 if (command == @selector(moveUp:)) | 586 if (command == @selector(moveRight:)) { |
571 return [self moveSelectionByDelta:-kFixedColumns]; | 587 return oldIndex % kFixedColumns == kFixedColumns - 1 ? |
| 588 [self moveSelectionByDelta:+kItemsPerPage - kFixedColumns + 1] : |
| 589 [self moveSelectionByDelta:1]; |
| 590 } |
572 | 591 |
573 if (command == @selector(moveDown:)) | 592 if (command == @selector(moveUp:)) { |
574 return [self moveSelectionByDelta:kFixedColumns]; | 593 return oldIndex / kFixedColumns % kFixedRows == 0 ? |
| 594 NO : [self moveSelectionByDelta:-kFixedColumns]; |
| 595 } |
| 596 |
| 597 if (command == @selector(moveDown:)) { |
| 598 return oldIndex / kFixedColumns % kFixedRows == kFixedRows - 1 ? |
| 599 NO : [self moveSelectionByDelta:kFixedColumns]; |
| 600 } |
575 | 601 |
576 if (command == @selector(pageUp:) || | 602 if (command == @selector(pageUp:) || |
577 command == @selector(scrollPageUp:)) | 603 command == @selector(scrollPageUp:)) |
578 return [self moveSelectionByDelta:-kItemsPerPage]; | 604 return [self moveSelectionByDelta:-kItemsPerPage]; |
579 | 605 |
580 if (command == @selector(pageDown:) || | 606 if (command == @selector(pageDown:) || |
581 command == @selector(scrollPageDown:)) | 607 command == @selector(scrollPageDown:)) |
582 return [self moveSelectionByDelta:kItemsPerPage]; | 608 return [self moveSelectionByDelta:kItemsPerPage]; |
583 | 609 |
584 return NO; | 610 return NO; |
585 } | 611 } |
586 | 612 |
587 @end | 613 @end |
OLD | NEW |