Skip to content

Commit

Permalink
Add key navigation between index and workdir files (#921)
Browse files Browse the repository at this point in the history
* Add key navigation between index and workdir files
Navigate to index files controller when Down button pressed while on the
last file of the workdir list
Navigate to workdir files controller when Up button pressed while on the
first files of the index list

* Use NSEventModifierFlags for declared modifiers
Use more precise type for long flags
  • Loading branch information
fo2rist authored May 26, 2023
1 parent ecff963 commit cf6f49d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions GitUpKit/Interface/GIConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#import <AppKit/NSEvent.h>
#import <Foundation/Foundation.h>

typedef NS_ENUM(unsigned short, GIKeyCode) {
Expand All @@ -29,3 +30,5 @@ typedef NS_ENUM(unsigned short, GIKeyCode) {
kGIKeyCode_Return = 0x24,
kGIKeyCode_Delete = 0x33
};

static const NSEventModifierFlags kGIKeyModifiersAll = NSEventModifierFlagShift | NSEventModifierFlagControl | NSEventModifierFlagCommand | NSEventModifierFlagOption;
22 changes: 22 additions & 0 deletions GitUpKit/Views/GIAdvancedCommitViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ - (void)_diffFilesViewControllerDidPressDelete:(GIDiffFilesViewController*)contr
}

- (BOOL)diffFilesViewController:(GIDiffFilesViewController*)controller handleKeyDownEvent:(NSEvent*)event {
// Stage/Unstage files by Return/Delete
if (!(event.modifierFlags & NSEventModifierFlagDeviceIndependentFlagsMask)) {
if (event.keyCode == kGIKeyCode_Return) {
[self _diffFilesViewControllerDidPressReturn:controller];
Expand All @@ -319,6 +320,27 @@ - (BOOL)diffFilesViewController:(GIDiffFilesViewController*)controller handleKey
}
}

// Navigate beteween stated and unstaged files list by up/down arrows
NSEventModifierFlags modifiers = event.modifierFlags & kGIKeyModifiersAll;
if (controller == _workdirFilesViewController && !modifiers && event.keyCode == kGIKeyCode_Down) {
bool onlyLastFileSelected = (controller.selectedDeltas.count == 1) && (controller.selectedDelta == controller.deltas.lastObject);
bool hasIndexFiles = _indexFilesViewController.deltas.count > 0;
if (onlyLastFileSelected && hasIndexFiles) {
// move focus to next controller
[[controller.view window] selectNextKeyView:_workdirFilesViewController.view];
return YES;
}
} else if (controller == _indexFilesViewController && !modifiers && event.keyCode == kGIKeyCode_Up) {
bool onlyFirstFileSelected = (controller.selectedDeltas.count == 1) && (controller.selectedDelta == controller.deltas.firstObject);
bool hasWorkdirFiles =_workdirFilesViewController.deltas.count > 0;
if (onlyFirstFileSelected && hasWorkdirFiles) {
// move focus to previous controller
[[controller.view window] selectPreviousKeyView:_workdirFilesViewController.view];
return YES;
}
}

// Perform contextual action on a file
if (controller == _workdirFilesViewController) {
return [self handleKeyDownEvent:event forSelectedDeltas:_workdirFilesViewController.selectedDeltas withConflicts:_indexConflicts allowOpen:YES];
} else if (controller == _indexFilesViewController) {
Expand Down

0 comments on commit cf6f49d

Please sign in to comment.