forked from andreberg/gitx
-
Notifications
You must be signed in to change notification settings - Fork 79
/
PBGitHistoryController.m
816 lines (665 loc) · 25.2 KB
/
PBGitHistoryController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//
// PBGitHistoryView.m
// GitX
//
// Created by Pieter de Bie on 19-09-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitHistoryController.h"
#import "PBWebHistoryController.h"
#import "CWQuickLook.h"
#import "PBGitGrapher.h"
#import "PBGitRevisionCell.h"
#import "PBCommitList.h"
#import "PBCreateBranchSheet.h"
#import "PBCreateTagSheet.h"
#import "PBAddRemoteSheet.h"
#import "PBGitSidebarController.h"
#import "PBGitGradientBarView.h"
#import "PBDiffWindowController.h"
#import "PBGitDefaults.h"
#import "PBGitRevList.h"
#import "PBHistorySearchController.h"
#define QLPreviewPanel NSClassFromString(@"QLPreviewPanel")
#import "PBQLTextView.h"
#import "GLFileView.h"
#define kHistorySelectedDetailIndexKey @"PBHistorySelectedDetailIndex"
#define kHistoryDetailViewIndex 0
#define kHistoryTreeViewIndex 1
#define kHistorySplitViewPositionDefault @"History SplitView Position"
@interface PBGitHistoryController ()
- (void) updateBranchFilterMatrix;
- (void) restoreFileBrowserSelection;
- (void) saveFileBrowserSelection;
- (void)saveSplitViewPosition;
@end
@implementation PBGitHistoryController
@synthesize selectedCommitDetailsIndex, webCommit, gitTree, commitController, refController;
@synthesize searchController;
@synthesize commitList;
@synthesize treeController;
- (void)awakeFromNib
{
self.selectedCommitDetailsIndex = [[NSUserDefaults standardUserDefaults] integerForKey:kHistorySelectedDetailIndexKey];
[commitController addObserver:self forKeyPath:@"selection" options:0 context:@"commitChange"];
[commitController addObserver:self forKeyPath:@"arrangedObjects.@count" options:NSKeyValueObservingOptionInitial context:@"updateCommitCount"];
[treeController addObserver:self forKeyPath:@"selection" options:0 context:@"treeChange"];
[repository.revisionList addObserver:self forKeyPath:@"isUpdating" options:0 context:@"revisionListUpdating"];
[repository addObserver:self forKeyPath:@"currentBranch" options:0 context:@"branchChange"];
[repository addObserver:self forKeyPath:@"refs" options:0 context:@"updateRefs"];
[repository addObserver:self forKeyPath:@"currentBranchFilter" options:0 context:@"branchFilterChange"];
forceSelectionUpdate = YES;
NSSize cellSpacing = [commitList intercellSpacing];
cellSpacing.height = 0;
[commitList setIntercellSpacing:cellSpacing];
[fileBrowser setTarget:self];
[fileBrowser setDoubleAction:@selector(openSelectedFile:)];
if (!repository.currentBranch) {
[repository reloadRefs];
[repository readCurrentBranch];
}
else
[repository lazyReload];
// Set a sort descriptor for the subject column in the history list, as
// It can't be sorted by default (because it's bound to a PBGitCommit)
[[commitList tableColumnWithIdentifier:@"SubjectColumn"] setSortDescriptorPrototype:[[NSSortDescriptor alloc] initWithKey:@"subject" ascending:YES]];
// Add a menu that allows a user to select which columns to view
[[commitList headerView] setMenu:[self tableColumnMenu]];
[historySplitView setTopMin:58.0 andBottomMin:100.0];
[historySplitView setHidden:YES];
[self performSelector:@selector(restoreSplitViewPositiion) withObject:nil afterDelay:0];
[upperToolbarView setTopShade:237/255.0 bottomShade:216/255.0];
[scopeBarView setTopColor:[NSColor colorWithCalibratedHue:0.579 saturation:0.068 brightness:0.898 alpha:1.000]
bottomColor:[NSColor colorWithCalibratedHue:0.579 saturation:0.119 brightness:0.765 alpha:1.000]];
[self updateBranchFilterMatrix];
[super awakeFromNib];
}
- (void)updateKeys
{
PBGitCommit *lastObject = [[commitController selectedObjects] lastObject];
if (lastObject) {
if (![selectedCommit isEqual:lastObject]) {
selectedCommit = lastObject;
BOOL isOnHeadBranch = [selectedCommit isOnHeadBranch];
[mergeButton setEnabled:!isOnHeadBranch];
[cherryPickButton setEnabled:!isOnHeadBranch];
[rebaseButton setEnabled:!isOnHeadBranch];
}
}
else {
[mergeButton setEnabled:NO];
[cherryPickButton setEnabled:NO];
[rebaseButton setEnabled:NO];
}
if (self.selectedCommitDetailsIndex == kHistoryTreeViewIndex) {
self.gitTree = selectedCommit.tree;
[self restoreFileBrowserSelection];
}
else {
// kHistoryDetailViewIndex
if (![self.webCommit isEqual:selectedCommit])
self.webCommit = selectedCommit;
}
}
- (void) updateBranchFilterMatrix
{
if ([repository.currentBranch isSimpleRef]) {
[allBranchesFilterItem setEnabled:YES];
[localRemoteBranchesFilterItem setEnabled:YES];
NSInteger filter = repository.currentBranchFilter;
[allBranchesFilterItem setState:(filter == kGitXAllBranchesFilter)];
[localRemoteBranchesFilterItem setState:(filter == kGitXLocalRemoteBranchesFilter)];
[selectedBranchFilterItem setState:(filter == kGitXSelectedBranchFilter)];
}
else {
[allBranchesFilterItem setState:NO];
[localRemoteBranchesFilterItem setState:NO];
[allBranchesFilterItem setEnabled:NO];
[localRemoteBranchesFilterItem setEnabled:NO];
[selectedBranchFilterItem setState:YES];
}
[selectedBranchFilterItem setTitle:[repository.currentBranch title]];
[selectedBranchFilterItem sizeToFit];
[localRemoteBranchesFilterItem setTitle:[[repository.currentBranch ref] isRemote] ? @"Remote" : @"Local"];
}
- (PBGitCommit *) firstCommit
{
NSArray *arrangedObjects = [commitController arrangedObjects];
if ([arrangedObjects count] > 0)
return [arrangedObjects objectAtIndex:0];
return nil;
}
- (BOOL)isCommitSelected
{
return [selectedCommit isEqual:[[commitController selectedObjects] lastObject]];
}
- (void) setSelectedCommitDetailsIndex:(int)detailsIndex
{
if (selectedCommitDetailsIndex == detailsIndex)
return;
selectedCommitDetailsIndex = detailsIndex;
[[NSUserDefaults standardUserDefaults] setInteger:selectedCommitDetailsIndex forKey:kHistorySelectedDetailIndexKey];
forceSelectionUpdate = YES;
[self updateKeys];
}
- (void) updateStatus
{
self.isBusy = repository.revisionList.isUpdating;
self.status = [NSString stringWithFormat:@"%d commits loaded", [[commitController arrangedObjects] count]];
}
- (void) restoreFileBrowserSelection
{
if (self.selectedCommitDetailsIndex != kHistoryTreeViewIndex)
return;
NSArray *children = [treeController content];
if ([children count] == 0)
return;
NSIndexPath *path = [[NSIndexPath alloc] init];
if ([currentFileBrowserSelectionPath count] == 0)
path = [path indexPathByAddingIndex:0];
else {
for (NSString *pathComponent in currentFileBrowserSelectionPath) {
PBGitTree *child = nil;
NSUInteger childIndex = 0;
for (child in children) {
if ([child.path isEqualToString:pathComponent]) {
path = [path indexPathByAddingIndex:childIndex];
children = child.children;
break;
}
childIndex++;
}
if (!child)
return;
}
}
[treeController setSelectionIndexPath:path];
}
- (void) saveFileBrowserSelection
{
NSArray *objects = [treeController selectedObjects];
NSArray *content = [treeController content];
if ([objects count] && [content count]) {
PBGitTree *treeItem = [objects objectAtIndex:0];
currentFileBrowserSelectionPath = [treeItem.fullPath componentsSeparatedByString:@"/"];
}
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([(NSString *)context isEqualToString: @"commitChange"]) {
[self updateKeys];
[self restoreFileBrowserSelection];
return;
}
if ([(NSString *)context isEqualToString: @"treeChange"]) {
[self updateQuicklookForce: NO];
[self saveFileBrowserSelection];
return;
}
if([(NSString *)context isEqualToString:@"branchChange"]) {
// Reset the sorting
if ([[commitController sortDescriptors] count])
[commitController setSortDescriptors:[NSArray array]];
[self updateBranchFilterMatrix];
return;
}
if([(NSString *)context isEqualToString:@"updateRefs"]) {
[commitController rearrangeObjects];
return;
}
if ([(NSString *)context isEqualToString:@"branchFilterChange"]) {
[PBGitDefaults setBranchFilter:repository.currentBranchFilter];
[self updateBranchFilterMatrix];
return;
}
if([(NSString *)context isEqualToString:@"updateCommitCount"] || [(NSString *)context isEqualToString:@"revisionListUpdating"]) {
[self updateStatus];
if ([repository.currentBranch isSimpleRef])
[self selectCommit:[repository shaForRef:[repository.currentBranch ref]]];
else
[self selectCommit:[[self firstCommit] sha]];
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (IBAction) openSelectedFile:(id)sender
{
NSArray* selectedFiles = [treeController selectedObjects];
if ([selectedFiles count] == 0)
return;
PBGitTree* tree = [selectedFiles objectAtIndex:0];
NSString* name = [tree tmpFileNameForContents];
[[NSWorkspace sharedWorkspace] openTempFile:name];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if ([menuItem action] == @selector(setDetailedView:)) {
[menuItem setState:(self.selectedCommitDetailsIndex == kHistoryDetailViewIndex) ? NSOnState : NSOffState];
} else if ([menuItem action] == @selector(setTreeView:)) {
[menuItem setState:(self.selectedCommitDetailsIndex == kHistoryTreeViewIndex) ? NSOnState : NSOffState];
}
return YES;
}
- (IBAction) setDetailedView:(id)sender
{
self.selectedCommitDetailsIndex = kHistoryDetailViewIndex;
forceSelectionUpdate = YES;
}
- (IBAction) setTreeView:(id)sender
{
self.selectedCommitDetailsIndex = kHistoryTreeViewIndex;
forceSelectionUpdate = YES;
}
- (IBAction) setBranchFilter:(id)sender
{
repository.currentBranchFilter = [sender tag];
[PBGitDefaults setBranchFilter:repository.currentBranchFilter];
[self updateBranchFilterMatrix];
forceSelectionUpdate = YES;
}
- (void)keyDown:(NSEvent*)event
{
if ([[event charactersIgnoringModifiers] isEqualToString: @"f"] && [event modifierFlags] & NSAlternateKeyMask && [event modifierFlags] & NSCommandKeyMask)
[superController.window makeFirstResponder: searchField];
else
[super keyDown: event];
}
// NSSearchField (actually textfields in general) prevent the normal Find operations from working. Setup custom actions for the
// next and previous menuitems (in MainMenu.nib) so they will work when the search field is active. When searching for text in
// a file make sure to call the Find panel's action method instead.
- (IBAction)selectNext:(id)sender
{
NSResponder *firstResponder = [[[self view] window] firstResponder];
if ([firstResponder isKindOfClass:[PBQLTextView class]]) {
[(PBQLTextView *)firstResponder performFindPanelAction:sender];
return;
}
[searchController selectNextResult];
}
- (IBAction)selectPrevious:(id)sender
{
NSResponder *firstResponder = [[[self view] window] firstResponder];
if ([firstResponder isKindOfClass:[PBQLTextView class]]) {
[(PBQLTextView *)firstResponder performFindPanelAction:sender];
return;
}
[searchController selectPreviousResult];
}
- (void) copyCommitInfo
{
PBGitCommit *commit = [[commitController selectedObjects] objectAtIndex:0];
if (!commit)
return;
NSString *info = [NSString stringWithFormat:@"%@ (%@)", [[commit realSha] substringToIndex:10], [commit subject]];
NSPasteboard *a =[NSPasteboard generalPasteboard];
[a declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[a setString:info forType: NSStringPboardType];
}
- (void) copyCommitSHA
{
PBGitCommit *commit = [[commitController selectedObjects] objectAtIndex:0];
if (!commit)
return;
NSString *info = [[commit realSha] substringWithRange:NSMakeRange(0, 7)];
NSPasteboard *a =[NSPasteboard generalPasteboard];
[a declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[a setString:info forType: NSStringPboardType];
}
- (IBAction) toggleQLPreviewPanel:(id)sender
{
if ([[QLPreviewPanel sharedPreviewPanel] respondsToSelector:@selector(setDataSource:)]) {
// Public QL API
if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible])
[[QLPreviewPanel sharedPreviewPanel] orderOut:nil];
else
[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];
}
else {
// Private QL API (10.5 only)
if ([[QLPreviewPanel sharedPreviewPanel] isOpen])
[[QLPreviewPanel sharedPreviewPanel] closePanel];
else {
[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFrontWithEffect:1];
[self updateQuicklookForce:YES];
}
}
}
- (void) updateQuicklookForce:(BOOL)force
{
if (!force && ![[QLPreviewPanel sharedPreviewPanel] isOpen])
return;
if ([[QLPreviewPanel sharedPreviewPanel] respondsToSelector:@selector(setDataSource:)]) {
// Public QL API
[previewPanel reloadData];
}
else {
// Private QL API (10.5 only)
NSArray *selectedFiles = [treeController selectedObjects];
NSMutableArray *fileNames = [NSMutableArray array];
for (PBGitTree *tree in selectedFiles) {
NSString *filePath = [tree tmpFileNameForContents];
if (filePath)
[fileNames addObject:[NSURL fileURLWithPath:filePath]];
}
if ([fileNames count])
[[QLPreviewPanel sharedPreviewPanel] setURLs:fileNames currentIndex:0 preservingDisplayState:YES];
}
}
- (IBAction) refresh:(id)sender
{
[repository forceUpdateRevisions];
}
- (void) updateView
{
[self updateKeys];
}
- (NSResponder *)firstResponder;
{
return commitList;
}
- (void) scrollSelectionToTopOfViewFrom:(NSInteger)oldIndex
{
if (oldIndex == NSNotFound)
oldIndex = 0;
NSInteger newIndex = [[commitController selectionIndexes] firstIndex];
if (newIndex > oldIndex) {
CGFloat sviewHeight = [[commitList superview] bounds].size.height;
CGFloat rowHeight = [commitList rowHeight];
NSInteger visibleRows = roundf(sviewHeight / rowHeight );
newIndex += (visibleRows - 1);
if (newIndex >= [[commitController content] count])
newIndex = [[commitController content] count] - 1;
}
if (newIndex != oldIndex) {
commitList.useAdjustScroll = YES;
}
[commitList scrollRowToVisible:newIndex];
commitList.useAdjustScroll = NO;
}
- (NSArray *) selectedObjectsForSHA:(PBGitSHA *)commitSHA
{
NSPredicate *selection = [NSPredicate predicateWithFormat:@"sha == %@", commitSHA];
NSArray *selectedCommits = [[commitController content] filteredArrayUsingPredicate:selection];
if (([selectedCommits count] == 0) && [self firstCommit])
selectedCommits = [NSArray arrayWithObject:[self firstCommit]];
return selectedCommits;
}
- (void)selectCommit:(PBGitSHA *)commitSHA
{
if (!forceSelectionUpdate && [[[[commitController selectedObjects] lastObject] sha] isEqual:commitSHA])
return;
NSInteger oldIndex = [[commitController selectionIndexes] firstIndex];
NSArray *selectedCommits = [self selectedObjectsForSHA:commitSHA];
[commitController setSelectedObjects:selectedCommits];
[self scrollSelectionToTopOfViewFrom:oldIndex];
forceSelectionUpdate = NO;
}
- (BOOL) hasNonlinearPath
{
return [commitController filterPredicate] || [[commitController sortDescriptors] count] > 0;
}
- (void)closeView
{
[self saveSplitViewPosition];
if (commitController) {
[commitController removeObserver:self forKeyPath:@"selection"];
[commitController removeObserver:self forKeyPath:@"arrangedObjects.@count"];
[treeController removeObserver:self forKeyPath:@"selection"];
[repository.revisionList removeObserver:self forKeyPath:@"isUpdating"];
[repository removeObserver:self forKeyPath:@"currentBranch"];
[repository removeObserver:self forKeyPath:@"refs"];
[repository removeObserver:self forKeyPath:@"currentBranchFilter"];
}
[webHistoryController closeView];
[fileView closeView];
[super closeView];
}
#pragma mark Table Column Methods
- (NSMenu *)tableColumnMenu
{
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Table columns menu"];
for (NSTableColumn *column in [commitList tableColumns]) {
NSMenuItem *item = [[NSMenuItem alloc] init];
[item setTitle:[[column headerCell] stringValue]];
[item bind:@"value"
toObject:column
withKeyPath:@"hidden"
options:[NSDictionary dictionaryWithObject:@"NSNegateBoolean" forKey:NSValueTransformerNameBindingOption]];
[menu addItem:item];
}
return menu;
}
#pragma mark Tree Context Menu Methods
- (void)showCommitsFromTree:(id)sender
{
NSString *searchString = [(NSArray *)[sender representedObject] componentsJoinedByString:@" "];
[searchController setHistorySearch:searchString mode:kGitXPathSearchMode];
}
- (void)showInFinderAction:(id)sender
{
NSString *workingDirectory = [[repository workingDirectory] stringByAppendingString:@"/"];
NSString *path;
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
for (NSString *filePath in [sender representedObject]) {
path = [workingDirectory stringByAppendingPathComponent:filePath];
[ws selectFile: path inFileViewerRootedAtPath:path];
}
}
- (void)openFilesAction:(id)sender
{
NSString *workingDirectory = [[repository workingDirectory] stringByAppendingString:@"/"];
NSString *path;
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
for (NSString *filePath in [sender representedObject]) {
path = [workingDirectory stringByAppendingPathComponent:filePath];
[ws openFile:path];
}
}
- (void) checkoutFiles:(id)sender
{
NSMutableArray *files = [NSMutableArray array];
for (NSString *filePath in [sender representedObject])
[files addObject:[filePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
[repository checkoutFiles:files fromRefish:selectedCommit];
}
- (void) diffFilesAction:(id)sender
{
[PBDiffWindowController showDiffWindowWithFiles:[sender representedObject] fromCommit:selectedCommit diffCommit:nil];
}
- (NSMenu *)contextMenuForTreeView
{
NSArray *filePaths = [[treeController selectedObjects] valueForKey:@"fullPath"];
NSMenu *menu = [[NSMenu alloc] init];
for (NSMenuItem *item in [self menuItemsForPaths:filePaths])
[menu addItem:item];
return menu;
}
- (NSArray *)menuItemsForPaths:(NSArray *)paths
{
NSMutableArray *filePaths = [NSMutableArray array];
for (NSString *filePath in paths)
[filePaths addObject:[filePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
BOOL multiple = [filePaths count] != 1;
NSMenuItem *historyItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Show history of files" : @"Show history of file"
action:@selector(showCommitsFromTree:)
keyEquivalent:@""];
PBGitRef *headRef = [[repository headRef] ref];
NSString *headRefName = [headRef shortName];
NSString *diffTitle = [NSString stringWithFormat:@"Diff %@ with %@", multiple ? @"files" : @"file", headRefName];
BOOL isHead = [[selectedCommit sha] isEqual:[repository headSHA]];
NSMenuItem *diffItem = [[NSMenuItem alloc] initWithTitle:diffTitle
action:isHead ? nil : @selector(diffFilesAction:)
keyEquivalent:@""];
NSMenuItem *checkoutItem = [[NSMenuItem alloc] initWithTitle:multiple ? @"Checkout files" : @"Checkout file"
action:@selector(checkoutFiles:)
keyEquivalent:@""];
NSMenuItem *finderItem = [[NSMenuItem alloc] initWithTitle:@"Show in Finder"
action:@selector(showInFinderAction:)
keyEquivalent:@""];
NSMenuItem *openFilesItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Open Files" : @"Open File"
action:@selector(openFilesAction:)
keyEquivalent:@""];
NSArray *menuItems = [NSArray arrayWithObjects:historyItem, diffItem, checkoutItem, finderItem, openFilesItem, nil];
for (NSMenuItem *item in menuItems) {
[item setTarget:self];
[item setRepresentedObject:filePaths];
}
return menuItems;
}
#pragma mark NSSplitView delegate methods
- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
{
return TRUE;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex
{
NSUInteger index = [[splitView subviews] indexOfObject:subview];
// this method (and canCollapse) are called by the splitView to decide how to collapse on double-click
// we compare our two subviews, so that always the smaller one is collapsed.
if([[[splitView subviews] objectAtIndex:index] frame].size.height < [[[splitView subviews] objectAtIndex:((index+1)%2)] frame].size.height) {
return TRUE;
}
return FALSE;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
{
return historySplitView.topViewMin;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
{
return [splitView frame].size.height - [splitView dividerThickness] - historySplitView.bottomViewMin;
}
// while the user resizes the window keep the upper (history) view constant and just resize the lower view
// unless the lower view gets too small
- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize
{
NSRect newFrame = [splitView frame];
float dividerThickness = [splitView dividerThickness];
NSView *upperView = [[splitView subviews] objectAtIndex:0];
NSRect upperFrame = [upperView frame];
upperFrame.size.width = newFrame.size.width;
if ((newFrame.size.height - upperFrame.size.height - dividerThickness) < historySplitView.bottomViewMin) {
upperFrame.size.height = newFrame.size.height - historySplitView.bottomViewMin - dividerThickness;
}
NSView *lowerView = [[splitView subviews] objectAtIndex:1];
NSRect lowerFrame = [lowerView frame];
lowerFrame.origin.y = upperFrame.size.height + dividerThickness;
lowerFrame.size.height = newFrame.size.height - lowerFrame.origin.y;
lowerFrame.size.width = newFrame.size.width;
[upperView setFrame:upperFrame];
[lowerView setFrame:lowerFrame];
}
// NSSplitView does not save and restore the position of the SplitView correctly so do it manually
- (void)saveSplitViewPosition
{
float position = [[[historySplitView subviews] objectAtIndex:0] frame].size.height;
[[NSUserDefaults standardUserDefaults] setFloat:position forKey:kHistorySplitViewPositionDefault];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// make sure this happens after awakeFromNib
- (void)restoreSplitViewPositiion
{
float position = [[NSUserDefaults standardUserDefaults] floatForKey:kHistorySplitViewPositionDefault];
if (position < 1.0)
position = 175;
[historySplitView setPosition:position ofDividerAtIndex:0];
[historySplitView setHidden:NO];
}
#pragma mark Repository Methods
- (IBAction) createBranch:(id)sender
{
PBGitRef *currentRef = [repository.currentBranch ref];
if (!selectedCommit || [selectedCommit hasRef:currentRef])
[PBCreateBranchSheet beginCreateBranchSheetAtRefish:currentRef inRepository:self.repository];
else
[PBCreateBranchSheet beginCreateBranchSheetAtRefish:selectedCommit inRepository:self.repository];
}
- (IBAction) createTag:(id)sender
{
if (!selectedCommit)
[PBCreateTagSheet beginCreateTagSheetAtRefish:[repository.currentBranch ref] inRepository:repository];
else
[PBCreateTagSheet beginCreateTagSheetAtRefish:selectedCommit inRepository:repository];
}
- (IBAction) showAddRemoteSheet:(id)sender
{
[PBAddRemoteSheet beginAddRemoteSheetForRepository:self.repository];
}
- (IBAction) merge:(id)sender
{
if (selectedCommit)
[repository mergeWithRefish:selectedCommit];
}
- (IBAction) cherryPick:(id)sender
{
if (selectedCommit)
[repository cherryPickRefish:selectedCommit];
}
- (IBAction) rebase:(id)sender
{
if (selectedCommit)
[repository rebaseBranch:nil onRefish:selectedCommit];
}
#pragma mark -
#pragma mark Quick Look Public API support
@protocol QLPreviewItem;
#pragma mark (QLPreviewPanelController)
- (BOOL) acceptsPreviewPanelControl:(id)panel
{
return YES;
}
- (void)beginPreviewPanelControl:(id)panel
{
// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.
previewPanel = panel;
[previewPanel setDelegate:self];
[previewPanel setDataSource:self];
}
- (void)endPreviewPanelControl:(id)panel
{
// This document loses its responsisibility on the preview panel
// Until the next call to -beginPreviewPanelControl: it must not
// change the panel's delegate, data source or refresh it.
previewPanel = nil;
}
#pragma mark <QLPreviewPanelDataSource>
- (NSInteger)numberOfPreviewItemsInPreviewPanel:(id)panel
{
return [[fileBrowser selectedRowIndexes] count];
}
- (id <QLPreviewItem>)previewPanel:(id)panel previewItemAtIndex:(NSInteger)index
{
PBGitTree *treeItem = (PBGitTree *)[[treeController selectedObjects] objectAtIndex:index];
NSURL *previewURL = [NSURL fileURLWithPath:[treeItem tmpFileNameForContents]];
return (id <QLPreviewItem>)previewURL;
}
#pragma mark <QLPreviewPanelDelegate>
- (BOOL)previewPanel:(id)panel handleEvent:(NSEvent *)event
{
// redirect all key down events to the table view
if ([event type] == NSKeyDown) {
[fileBrowser keyDown:event];
return YES;
}
return NO;
}
// This delegate method provides the rect on screen from which the panel will zoom.
- (NSRect)previewPanel:(id)panel sourceFrameOnScreenForPreviewItem:(id <QLPreviewItem>)item
{
NSInteger index = [fileBrowser rowForItem:[[treeController selectedNodes] objectAtIndex:0]];
if (index == NSNotFound) {
return NSZeroRect;
}
NSRect iconRect = [fileBrowser frameOfCellAtColumn:0 row:index];
// check that the icon rect is visible on screen
NSRect visibleRect = [fileBrowser visibleRect];
if (!NSIntersectsRect(visibleRect, iconRect)) {
return NSZeroRect;
}
// convert icon rect to screen coordinates
iconRect = [fileBrowser convertRectToBase:iconRect];
iconRect.origin = [[fileBrowser window] convertBaseToScreen:iconRect.origin];
return iconRect;
}
@end