2 // PBDetailController.m
5 // Created by Pieter de Bie on 16-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitWindowController.h"
10 #import "PBGitHistoryController.h"
11 #import "PBGitCommitController.h"
13 @implementation PBGitWindowController
16 @synthesize repository, viewController, selectedViewIndex;
18 - (id)initWithRepository:(PBGitRepository*)theRepository displayDefault:(BOOL)displayDefault
20 if(self = [self initWithWindowNibName:@"RepositoryWindow"])
22 self.repository = theRepository;
23 [self showWindow:nil];
27 self.selectedViewIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedViewIndex"];
29 self.selectedViewIndex = -1;
35 - (void)windowWillClose:(NSNotification *)notification
37 NSLog(@"Window will close!");
38 if (historyViewController)
39 [historyViewController removeView];
40 if (commitViewController)
41 [commitViewController removeView];
44 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
46 if ([menuItem action] == @selector(showCommitView:) || [menuItem action] == @selector(showHistoryView:)) {
47 return ![repository isBareRepository];
52 - (void) setSelectedViewIndex: (int) i
54 [self changeViewController: i];
57 - (void)changeViewController:(NSInteger)whichViewTag
59 [self willChangeValueForKey:@"viewController"];
61 if (viewController != nil)
62 [[viewController view] removeFromSuperview];
64 if ([repository isBareRepository]) { // in bare repository we don't want to view commit
65 whichViewTag = 0; // even if it was selected by default
68 // Set our default here because we might have changed it (based on bare repo) before
69 selectedViewIndex = whichViewTag;
70 [[NSUserDefaults standardUserDefaults] setInteger:whichViewTag forKey:@"selectedViewIndex"];
74 case 0: // swap in the "CustomImageViewController - NSImageView"
75 if (!historyViewController)
76 historyViewController = [[PBGitHistoryController alloc] initWithRepository:repository superController:self];
78 [historyViewController updateView];
79 viewController = historyViewController;
82 if (!commitViewController)
83 commitViewController = [[PBGitCommitController alloc] initWithRepository:repository superController:self];
85 [commitViewController updateView];
87 viewController = commitViewController;
91 // make sure we automatically resize the controller's view to the current window size
92 [[viewController view] setFrame: [contentView bounds]];
94 //// embed the current view to our host view
95 [contentView addSubview: [viewController view]];
97 [self useToolbar: [viewController viewToolbar]];
99 // Allow the viewcontroller to catch actions
100 [self setNextResponder: viewController];
101 [self didChangeValueForKey:@"viewController"]; // this will trigger the NSTextField's value binding to change
107 [[self window] setDelegate:self];
108 [[self window] setAutorecalculatesContentBorderThickness:NO forEdge:NSMinYEdge];
109 [[self window] setContentBorderThickness:35.0f forEdge:NSMinYEdge];
110 [self showHistoryView:nil];
113 - (void) showCommitView:(id)sender
115 if (self.selectedViewIndex != 1)
116 self.selectedViewIndex = 1;
119 - (void) showHistoryView:(id)sender
121 if (self.selectedViewIndex != 0)
122 self.selectedViewIndex = 0;
126 #pragma mark Toolbar Delegates
128 - (void) useToolbar:(NSToolbar *)toolbar
130 NSSegmentedControl *item = nil;
131 for (NSToolbarItem *toolbarItem in [toolbar items]) {
132 if ([[toolbarItem view] isKindOfClass:[NSSegmentedControl class]]) {
133 item = (NSSegmentedControl *)[toolbarItem view];
137 [item bind:@"selectedIndex" toObject:self withKeyPath:@"selectedViewIndex" options:0];
138 [item setEnabled: ![repository isBareRepository]];
140 [self.window setToolbar:toolbar];