forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBGitWindowController.m
418 lines (337 loc) · 13.8 KB
/
PBGitWindowController.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
//
// PBDetailController.m
// GitX
//
// Created by Pieter de Bie on 16-06-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitWindowController.h"
#import "PBGitHistoryController.h"
#import "PBGitCommitController.h"
#import "PBGitDefaults.h"
#import "Terminal.h"
#import "PBCloneRepsitoryToSheet.h"
#import "PBCommitHookFailedSheet.h"
#import "PBGitXMessageSheet.h"
#import "PBGitSidebarController.h"
#import "PBAddRemoteSheet.h"
#ifndef MAC_OS_X_VERSION_10_7
#define NSApplicationPresentationAutoHideToolbar 0
#define NSApplicationPresentationFullScreen 0
#endif
@implementation PBGitWindowController
@synthesize repository;
- (id)initWithRepository:(PBGitRepository*)theRepository displayDefault:(BOOL)displayDefault
{
if (!(self = [self initWithWindowNibName:@"RepositoryWindow"]))
return nil;
self.repository = theRepository;
addRemoteAfterCloneTo = NO;
remoteURL = nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cloneToInProgress:) name:@"CloneToOperationInProgress" object:Nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(successMessageSheetDidOrderOut:) name:@"SuccessMessageSheetDidOrderOut" object:Nil];
return self;
}
- (void)synchronizeWindowTitleWithDocumentName
{
[super synchronizeWindowTitleWithDocumentName];
// Point window proxy icon at project directory, not internal .git dir
NSString *workingDirectory = [self.repository workingDirectory];
[[self window] setRepresentedURL:[NSURL fileURLWithPath:workingDirectory
isDirectory:YES]];
}
- (void)windowWillClose:(NSNotification *)notification
{
//DLog(@"Window will close!");
if (sidebarController)
[sidebarController closeView];
if (contentController)
[contentController removeObserver:self forKeyPath:@"status"];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
{
return NSApplicationPresentationAutoHideToolbar | NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationAutoHideDock;
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if ([menuItem action] == @selector(showCommitView:)) {
[menuItem setState:(contentController == sidebarController.commitViewController) ? YES : NO];
return ![repository isBareRepository];
} else if ([menuItem action] == @selector(showHistoryView:)) {
[menuItem setState:(contentController != sidebarController.commitViewController) ? YES : NO];
return ![repository isBareRepository];
} else if ([menuItem action] == @selector(commit:)){
return [contentController isKindOfClass:[PBGitCommitController class]];
}
return YES;
}
- (IBAction) commit:(id) sender
{
[(PBGitCommitController *)contentController commit:sender];
}
- (void) awakeFromNib
{
[[self window] setDelegate:self];
[[self window] setAutorecalculatesContentBorderThickness:NO forEdge:NSMinYEdge];
[[self window] setContentBorderThickness:31.0f forEdge:NSMinYEdge];
sidebarController = [[PBGitSidebarController alloc] initWithRepository:repository superController:self];
[[sidebarController view] setFrame:[sourceSplitView bounds]];
[sourceSplitView addSubview:[sidebarController view]];
[sourceListControlsView addSubview:sidebarController.sourceListControlsView];
[[statusField cell] setBackgroundStyle:NSBackgroundStyleRaised];
[progressIndicator setUsesThreadedAnimation:YES];
NSImage *finderImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFinderIcon)];
[finderItem setImage:finderImage];
NSImage *terminalImage = [[NSWorkspace sharedWorkspace] iconForFile:@"/Applications/Utilities/Terminal.app/"];
[terminalItem setImage:terminalImage];
[self showWindow:nil];
[self initChangeLayout];
}
- (void) removeAllContentSubViews
{
if ([contentSplitView subviews])
while ([[contentSplitView subviews] count] > 0)
[[[contentSplitView subviews] lastObject] removeFromSuperviewWithoutNeedingDisplay];
}
- (void) changeContentController:(PBViewController *)controller
{
if (!controller || (contentController == controller))
return;
if (contentController)
[contentController removeObserver:self forKeyPath:@"status"];
[self removeAllContentSubViews];
contentController = controller;
[[contentController view] setFrame:[contentSplitView bounds]];
[contentSplitView addSubview:[contentController view]];
[self setNextResponder: contentController];
[[self window] makeFirstResponder:[contentController firstResponder]];
if ([PBGitDefaults refreshAutomatically]) {
[contentController refresh:self];
}
[contentController addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial context:@"statusChange"];
}
- (void) showCommitView:(id)sender
{
[sidebarController selectStage];
}
- (void) showHistoryView:(id)sender
{
[sidebarController selectCurrentBranch];
}
- (void)showCommitHookFailedSheet:(NSString *)messageText infoText:(NSString *)infoText commitController:(PBGitCommitController *)controller
{
[PBCommitHookFailedSheet beginMessageSheetForWindow:[self window] withMessageText:messageText infoText:infoText commitController:controller];
}
- (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText
{
[PBGitXMessageSheet beginMessageSheetForWindow:[self window] withMessageText:messageText infoText:infoText];
}
- (void)showErrorSheet:(NSError *)error
{
[[NSAlert alertWithError:error] beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(showErrorSheetAlertDidEnd:returnCode:contextInfo:)
contextInfo:Nil];
}
- (void) showErrorSheetAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)code contextInfo:(void *)info
{
[[alert window] orderOut:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ErrorMessageDidEnd" object:self];
}
- (void)windowDidBecomeKey:(NSNotification *)notification
{
if ([PBGitDefaults refreshAutomatically]) {
[contentController refresh:self];
}
if ([PBGitDefaults isUseITerm2]) {
[terminalItem setImage:[[NSWorkspace sharedWorkspace] iconForFile:[[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.googlecode.iterm2"]]];
[terminalItem setLabel:@"iTerm"];
}
else {
[terminalItem setImage:[[NSWorkspace sharedWorkspace] iconForFile:@"/Applications/Utilities/Terminal.app/"]];
[terminalItem setLabel:@"Terminal"];
}
}
- (void)showErrorSheetTitle:(NSString *)title message:(NSString *)message arguments:(NSArray *)arguments output:(NSString *)output
{
NSString *command = [arguments componentsJoinedByString:@" "];
NSString *reason = [NSString stringWithFormat:@"%@\n\ncommand: git %@\n%@", message, command, output];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
title, NSLocalizedDescriptionKey,
reason, NSLocalizedRecoverySuggestionErrorKey,
nil];
NSError *error = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
[self showErrorSheet:error];
}
- (IBAction) revealInFinder:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:[repository workingDirectory]];
}
- (IBAction) openInTerminal:(id)sender
{
NSString *workingDirectory = [[repository workingDirectory] stringByAppendingString:@"/"];
if ([PBGitDefaults isUseITerm2]) {
NSStringEncoding encoding;
NSString *resourcePath = [[[NSBundle bundleForClass:[self class]] resourcePath] stringByAppendingPathComponent:@"Start_iTerm2.applescript"];
NSString *scriptSource = [NSString stringWithContentsOfFile:resourcePath usedEncoding:&encoding error:nil];
NSString *iTerm2StartScript = [scriptSource stringByReplacingOccurrencesOfString:@"%%workDir%%" withString:workingDirectory];
NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:iTerm2StartScript];
[scriptObject executeAndReturnError:nil];
}
else {
TerminalApplication *term = [SBApplication applicationWithBundleIdentifier:@"com.apple.Terminal"];
NSString *cmd = [NSString stringWithFormat: @"cd \"%@\"; clear; echo '# Opened by GitX:'; git status", workingDirectory];
[term doScript: cmd in: nil];
[NSThread sleepForTimeInterval: 0.1];
[term activate];
}
}
- (IBAction) cloneTo:(id)sender
{
[PBCloneRepsitoryToSheet beginCloneRepsitoryToSheetForRepository:repository];
}
- (IBAction) refresh:(id)sender
{
[sidebarController.historyViewController refresh: self];
[sidebarController.commitViewController refresh: self];
}
- (void) updateStatus
{
NSString *status = contentController.status;
BOOL isBusy = contentController.isBusy;
if (!status) {
status = @"";
isBusy = NO;
}
[statusField setStringValue:status];
if (isBusy) {
[progressIndicator startAnimation:self];
[progressIndicator setHidden:NO];
[statusField setTextColor:[NSColor blueColor]];
}
else {
[progressIndicator stopAnimation:self];
[progressIndicator setHidden:YES];
[statusField setTextColor:[NSColor blackColor]];
}
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([(__bridge NSString *)context isEqualToString:@"statusChange"]) {
[self updateStatus];
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void) selectCommitForSha:(NSString *)sha
{
if (contentController != sidebarController.historyViewController)
[sidebarController selectCurrentBranch];
[sidebarController.historyViewController selectCommit:sha];
}
- (NSArray *) menuItemsForPaths:(NSArray *)paths
{
return [sidebarController.historyViewController menuItemsForPaths:paths];
}
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode
{
[sidebarController setHistorySearch:searchString mode:mode];
}
#pragma mark - SplitView changeLayout
-(void)initChangeLayout
{
splitViews=[NSArray arrayWithObjects:mainSplitView,[[sidebarController historyViewController] historySplitView], nil];
splitViewsSize=[NSMutableArray arrayWithCapacity:[splitViews count]];
for (int n=0; n<[splitViews count]; n++) {
NSSplitView *splitView=[splitViews objectAtIndex:n];
NSView *left=[[splitView subviews] objectAtIndex:0];
[splitViewsSize addObject:[NSNumber numberWithInt:[left frame].size.width]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(resizeSubviewsHandler:)
name:NSSplitViewWillResizeSubviewsNotification
object:splitView
];
}
}
- (IBAction)changeLayout:(id)sender
{
NSInteger index=[sender selectedSegment];
NSSplitView *splitView=[splitViews objectAtIndex:index];
NSView *left=[[splitView subviews] objectAtIndex:0];
CGFloat pos;
if ([splitView isSubviewCollapsed:left])
pos=[[splitViewsSize objectAtIndex:index] intValue];
else
pos=[splitView minPossiblePositionOfDividerAtIndex:0];
[splitView setPosition:pos ofDividerAtIndex:0 ];
}
- (void)resizeSubviewsHandler:(NSNotification *)notif
{
NSSplitView *splitView=[notif object];
NSInteger index=[splitViews indexOfObject:splitView];
NSView *left=[[splitView subviews] objectAtIndex:0];
NSNumber *pos;
if([splitView isVertical]){
pos=[NSNumber numberWithInt:[left frame].size.width];
}else{
pos=[NSNumber numberWithInt:[left frame].size.height];
}
[splitViewsSize removeObjectAtIndex:index];
[splitViewsSize insertObject:pos atIndex:index];
}
#pragma mark -
#pragma mark SplitView Delegates
- (BOOL)splitView:(NSSplitView *)sp canCollapseSubview:(NSView *)subview
{
return TRUE;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex
{
NSUInteger index = [[splitView subviews] indexOfObject:subview];
return index==0;
}
#pragma mark min/max widths while moving the divider
- (CGFloat)splitView:(NSSplitView *)view constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
{
if (proposedMin < kGitSplitViewMinWidth)
return kGitSplitViewMinWidth;
return proposedMin;
}
- (CGFloat)splitView:(NSSplitView *)view constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
{
if (dividerIndex == 0)
return kGitSplitViewMaxWidth;
return proposedMax;
}
#pragma mark constrain sidebar width while resizing the window
- (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
NSRect newFrame = [sender frame];
float dividerThickness = [sender dividerThickness];
NSView *sourceView = [[sender subviews] objectAtIndex:0];
NSRect sourceFrame = [sourceView frame];
sourceFrame.size.height = newFrame.size.height;
NSView *mainView = [[sender subviews] objectAtIndex:1];
NSRect mainFrame = [mainView frame];
mainFrame.origin.x = sourceFrame.size.width + dividerThickness;
mainFrame.size.width = newFrame.size.width - mainFrame.origin.x;
mainFrame.size.height = newFrame.size.height;
[sourceView setFrame:sourceFrame];
[mainView setFrame:mainFrame];
}
#pragma mark - Observer methods
- (void)cloneToInProgress:(NSNotification*)notification
{
addRemoteAfterCloneTo = YES;
remoteURL = [[notification userInfo] objectForKey:@"CloneToPath"];
}
- (void)successMessageSheetDidOrderOut:(NSNotification*)notification
{
if (addRemoteAfterCloneTo)
{
addRemoteAfterCloneTo = NO;
[PBAddRemoteSheet beginAddRemoteSheetForRepository:repository withRemoteURL:remoteURL];
}
}
@end