@@ -92,42 +92,54 @@ - (BOOL) allSelectedCanBeIgnored:(NSArray *)selectedFiles
92
92
- (NSMenu *) menuForTable : (NSTableView *)table
93
93
{
94
94
NSMenu *menu = [[NSMenu alloc ] init ];
95
- id controller = [ table tag ] == 0 ? unstagedFilesController : stagedFilesController;
96
- NSArray *selectedFiles = [ controller selectedObjects ] ;
95
+ NSArrayController * controller = table. tag == 0 ? unstagedFilesController : stagedFilesController;
96
+ NSArray *selectedFiles = controller. selectedObjects ;
97
97
98
- if ([selectedFiles count ] == 0 )
98
+ NSUInteger numberOfSelectedFiles = selectedFiles.count ;
99
+
100
+ if (numberOfSelectedFiles == 0 )
99
101
{
100
102
return menu;
101
103
}
102
-
103
- // Unstaged changes
104
- if ([table tag ] == 0 ) {
105
- NSMenuItem *stageItem = [[NSMenuItem alloc ] initWithTitle: @" Stage Changes" action: @selector (stageFilesAction: ) keyEquivalent: @" s" ];
106
- [stageItem setTarget: self ];
104
+
105
+ // Stage/Unstage changes
106
+ if (table.tag == 0 ) {
107
+ NSString *stageTitle = numberOfSelectedFiles == 1
108
+ ? [NSString stringWithFormat: NSLocalizedString( @" Stage “%@ ”" , @" Stage single file contextual menu item" ), [self getNameOfFirstSelectedFile: selectedFiles]]
109
+ : [NSString stringWithFormat: NSLocalizedString( @" Stage %s Files" , @" Stage multiple files contextual menu item" ), numberOfSelectedFiles ];
110
+ NSMenuItem *stageItem = [[NSMenuItem alloc ] initWithTitle: stageTitle action: @selector (stageFilesAction: ) keyEquivalent: @" s" ];
111
+ stageItem.target = self;
107
112
[menu addItem: stageItem];
108
113
}
109
- else if ([table tag ] == 1 ) {
110
- NSMenuItem *unstageItem = [[NSMenuItem alloc ] initWithTitle: @" Unstage Changes" action: @selector (unstageFilesAction: ) keyEquivalent: @" u" ];
111
- [unstageItem setTarget: self ];
114
+ else if (table.tag == 1 ) {
115
+ NSString *stageTitle = numberOfSelectedFiles == 1
116
+ ? [NSString stringWithFormat: NSLocalizedString( @" Unstage “%@ ”" , @" Unstage single file contextual menu item" ), [self getNameOfFirstSelectedFile: selectedFiles]]
117
+ : [NSString stringWithFormat: NSLocalizedString( @" Unstage %s Files" , @" Unstage multiple files contextual menu item" ), numberOfSelectedFiles ];
118
+ NSMenuItem *unstageItem = [[NSMenuItem alloc ] initWithTitle: stageTitle action: @selector (unstageFilesAction: ) keyEquivalent: @" u" ];
119
+ unstageItem.target = self;
112
120
[menu addItem: unstageItem];
113
121
}
114
122
115
- NSString *title = [selectedFiles count ] == 1 ? @" Open file" : @" Open files" ;
116
- NSMenuItem *openItem = [[NSMenuItem alloc ] initWithTitle: title action: @selector (openFilesAction: ) keyEquivalent: @" " ];
117
- [openItem setTarget: self ];
123
+ NSString *openTitle = numberOfSelectedFiles == 1
124
+ ? [NSString stringWithFormat: NSLocalizedString( @" Open ”%@ “" , @" Open single file contextual menu item" ), [self getNameOfFirstSelectedFile: selectedFiles]]
125
+ : [NSString stringWithFormat: NSLocalizedString( @" Open %s Files" , @" Open multiple files contextual menu item" ), numberOfSelectedFiles ];
126
+ NSMenuItem *openItem = [[NSMenuItem alloc ] initWithTitle: openTitle action: @selector (openFilesAction: ) keyEquivalent: @" " ];
127
+ openItem.target = self;
118
128
[menu addItem: openItem];
119
129
120
130
// Attempt to ignore
121
131
if ([self allSelectedCanBeIgnored: selectedFiles]) {
122
- NSString *ignoreText = [selectedFiles count ] == 1 ? @" Ignore File" : @" Ignore Files" ;
132
+ NSString *ignoreText = numberOfSelectedFiles == 1
133
+ ? [NSString stringWithFormat: NSLocalizedString( @" Ignore ”%@ “" , @" Ignore single file contextual menu item" ), [self getNameOfFirstSelectedFile: selectedFiles]]
134
+ : [NSString stringWithFormat: NSLocalizedString( @" Ignore %s Files" , @" Ignore multiple files contextual menu item" ), numberOfSelectedFiles ];
123
135
NSMenuItem *ignoreItem = [[NSMenuItem alloc ] initWithTitle: ignoreText action: @selector (ignoreFilesAction: ) keyEquivalent: @" " ];
124
- [ ignoreItem setTarget: self ] ;
136
+ ignoreItem. target = self;
125
137
[menu addItem: ignoreItem];
126
138
}
127
139
128
- if ([selectedFiles count ] == 1 ) {
140
+ if (numberOfSelectedFiles == 1 ) {
129
141
NSMenuItem *showInFinderItem = [[NSMenuItem alloc ] initWithTitle: @" Show in Finder" action: @selector (showInFinderAction: ) keyEquivalent: @" " ];
130
- [ showInFinderItem setTarget: self ] ;
142
+ showInFinderItem. target = self;
131
143
[menu addItem: showInFinderItem];
132
144
}
133
145
@@ -186,6 +198,10 @@ - (NSMenu *) menuForTable:(NSTableView *)table
186
198
return menu;
187
199
}
188
200
201
+ - (NSString *) getNameOfFirstSelectedFile : (NSArray <PBChangedFile *> *) selectedFiles {
202
+ return selectedFiles.firstObject .path .lastPathComponent ;
203
+ }
204
+
189
205
- (BOOL )validateMenuItem : (NSMenuItem *)menuItem
190
206
{
191
207
if ([self respondsToSelector: [menuItem action ]])
@@ -197,24 +213,34 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
197
213
return [[commitController nextResponder ] validateMenuItem: menuItem];
198
214
}
199
215
200
- - ( void ) stageSelectedFiles
201
- {
202
- [commitController.index stageFiles: [unstagedFilesController selectedObjects ] for: unstagedFilesController ];
216
+
217
+ - ( IBAction ) stageFilesAction : ( id ) sender {
218
+ [self stageSelectedFiles ];
203
219
}
204
220
205
- - (void ) unstageSelectedFiles
221
+ - (IBAction ) unstageFilesAction : (id ) sender {
222
+ [self unstageSelectedFiles ];
223
+ }
224
+
225
+ - (void ) stageSelectedFiles
206
226
{
207
- [commitController.index unstageFiles: [stagedFilesController selectedObjects ] for: stagedFilesController];
227
+ [commitController.index stageFiles: unstagedFilesController.selectedObjects];
228
+ [self .class establishFutureSelection: unstagedFilesController];
208
229
}
209
230
210
- - (void ) stageFilesAction : ( id ) sender
231
+ - (void ) unstageSelectedFiles
211
232
{
212
- [commitController.index stageFiles: [sender representedObject ] for: unstagedFilesController];
233
+ [commitController.index unstageFiles: [stagedFilesController selectedObjects ]];
234
+ [self .class establishFutureSelection: stagedFilesController];
213
235
}
214
236
215
- - (void ) unstageFilesAction : ( id ) sender
237
+ + (void ) establishFutureSelection : ( NSArrayController *) controller
216
238
{
217
- [commitController.index unstageFiles: [sender representedObject ] for: stagedFilesController];
239
+ NSUInteger currentSelectionIndex = controller.selectionIndex ;
240
+ dispatch_async (dispatch_get_main_queue (), ^{
241
+ NSUInteger newSelectionIndex = MIN (currentSelectionIndex, [controller.arrangedObjects count ] - 1 );
242
+ controller.selectionIndex = newSelectionIndex;
243
+ });
218
244
}
219
245
220
246
- (void ) openFilesAction : (id ) sender
@@ -282,7 +308,7 @@ - (void) discardChangesForFilesAlertDidEnd:(NSAlert *)alert returnCode:(NSIntege
282
308
[[alert window ] orderOut: nil ];
283
309
284
310
if (returnCode == NSAlertDefaultReturn ) {
285
- [commitController.index discardChangesForFiles: (__bridge NSArray *)contextInfo for: unstagedFilesController ];
311
+ [commitController.index discardChangesForFiles: (__bridge NSArray *)contextInfo];
286
312
}
287
313
}
288
314
@@ -299,7 +325,7 @@ - (void) discardChangesForFiles:(NSArray *)files force:(BOOL)force
299
325
didEndSelector: @selector (discardChangesForFilesAlertDidEnd:returnCode:contextInfo: )
300
326
contextInfo: (__bridge_retained void *)files];
301
327
} else {
302
- [commitController.index discardChangesForFiles: files for: unstagedFilesController ];
328
+ [commitController.index discardChangesForFiles: files];
303
329
}
304
330
}
305
331
@@ -317,10 +343,10 @@ - (void) tableClicked:(NSTableView *) tableView
317
343
NSIndexSet *selectionIndexes = [tableView selectedRowIndexes ];
318
344
NSArray *files = [[controller arrangedObjects ] objectsAtIndexes: selectionIndexes];
319
345
if ([tableView tag ] == 0 ) {
320
- [commitController.index stageFiles: files for: controller ];
346
+ [commitController.index stageFiles: files];
321
347
}
322
348
else {
323
- [commitController.index unstageFiles: files for: controller ];
349
+ [commitController.index unstageFiles: files];
324
350
}
325
351
}
326
352
@@ -380,10 +406,10 @@ - (BOOL)tableView:(NSTableView *)aTableView
380
406
NSArray *files = [[controller arrangedObjects ] objectsAtIndexes: rowIndexes];
381
407
382
408
if ([aTableView tag ] == 0 ) {
383
- [commitController.index unstageFiles: files for: controller ];
409
+ [commitController.index unstageFiles: files];
384
410
}
385
411
else {
386
- [commitController.index stageFiles: files for: controller ];
412
+ [commitController.index stageFiles: files];
387
413
}
388
414
389
415
return YES ;
0 commit comments