From 41154e8b41528536529d7f1083169214879e3f4e Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 21 Jan 2013 17:05:02 +0000 Subject: Window controller tidy up. --- Source/SPWindowController.m | 221 ++++++++++++++++++++++++++------------------ 1 file changed, 132 insertions(+), 89 deletions(-) (limited to 'Source') diff --git a/Source/SPWindowController.m b/Source/SPWindowController.m index cb7f0573..f7bd0aba 100644 --- a/Source/SPWindowController.m +++ b/Source/SPWindowController.m @@ -30,15 +30,6 @@ // // More info at -// Forward-declare for 10.7 compatibility -#if !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 -enum { - NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7, - NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8, - NSFullScreenWindowMask = 1 << 14 -}; -#endif - #import "SPWindowController.h" #import "SPDatabaseDocument.h" #import "SPDatabaseViewController.h" @@ -48,8 +39,18 @@ enum { #import #import +// Forward-declare for 10.7 compatibility +#if !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 +enum { + NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7, + NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8, + NSFullScreenWindowMask = 1 << 14 +}; +#endif + @interface SPWindowController () +- (void)_setUpTabBar; - (void)_updateProgressIndicatorForItem:(NSTabViewItem *)theItem; - (void)_createTitleBarLineHidingView; - (void)_updateLineHidingViewState; @@ -58,13 +59,17 @@ enum { @implementation SPWindowController +#pragma mark - +#pragma mark Initialisation + /** * awakeFromNib */ - (void)awakeFromNib { - selectedTableDocument = nil; systemVersion = 0; + selectedTableDocument = nil; + Gestalt(gestaltSystemVersion, &systemVersion); [[self window] setCollectionBehavior:[[self window] collectionBehavior] | NSWindowCollectionBehaviorFullScreenPrimary]; @@ -79,27 +84,7 @@ enum { // Initialise the managed database connections array managedDatabaseConnections = [[NSMutableArray alloc] init]; - // Set up the tab bar - [tabBar setStyleNamed:@"SequelPro"]; - [tabBar setCanCloseOnlyTab:NO]; - [tabBar setHideForSingleTab:![[NSUserDefaults standardUserDefaults] boolForKey:SPAlwaysShowWindowTabBar]]; - [tabBar setShowAddTabButton:YES]; - [tabBar setSizeCellsToFit:NO]; - [tabBar setCellMinWidth:100]; - [tabBar setCellMaxWidth:250]; - [tabBar setCellOptimumWidth:250]; - [tabBar setSelectsTabsOnMouseDown:YES]; - [tabBar setCreatesTabOnDoubleClick:YES]; - [tabBar setTearOffStyle:PSMTabBarTearOffAlphaWindow]; - [tabBar setUsesSafariStyleDragging:YES]; - - // Hook up add tab button - [tabBar setCreateNewTabTarget:self]; - [tabBar setCreateNewTabAction:@selector(addNewConnection:)]; - - // Set the double click target and action - [tabBar setDoubleClickTarget:self]; - [tabBar setDoubleClickAction:@selector(openDatabaseInNewTab)]; + [self _setUpTabBar]; // Retrieve references to the 'Close Window' and 'Close Tab' menus. These are updated as window focus changes. closeWindowMenuItem = [[[[NSApp mainMenu] itemWithTag:SPMainMenuFile] submenu] itemWithTag:1003]; @@ -111,22 +96,6 @@ enum { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateLineHidingViewState) name:SPWindowToolbarDidToggleNotification object:nil]; } -/** - * Deallocation - */ -- (void) dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [NSObject cancelPreviousPerformRequestsWithTarget:self]; - - // Tear down the animations on the tab bar to stop redraws - [tabBar destroyAnimations]; - - [managedDatabaseConnections release]; - - [super dealloc]; -} - #pragma mark - #pragma mark Database connection management @@ -137,11 +106,13 @@ enum { { // Create a new database connection view SPDatabaseDocument *newTableDocument = [[SPDatabaseDocument alloc] init]; + [newTableDocument setParentWindowController:self]; [newTableDocument setParentWindow:[self window]]; // Set up a new tab with the connection view as the identifier, add the view, and add it to the tab view NSTabViewItem *newItem = [[[NSTabViewItem alloc] initWithIdentifier:newTableDocument] autorelease]; + [newItem setView:[newTableDocument databaseView]]; [tabView addTabViewItem:newItem]; [tabView selectTabViewItem:newItem]; @@ -160,7 +131,7 @@ enum { /** * Retrieve the currently connection view in the window. */ -- (SPDatabaseDocument *) selectedTableDocument +- (SPDatabaseDocument *)selectedTableDocument { return selectedTableDocument; } @@ -168,9 +139,10 @@ enum { /** * Update the currently selected connection view */ -- (void) updateSelectedTableDocument +- (void)updateSelectedTableDocument { selectedTableDocument = [[tabView selectedTabViewItem] identifier]; + [selectedTableDocument didBecomeActiveTabInWindow]; } @@ -180,19 +152,22 @@ enum { * within each tab may require other tabs to update their titles. * If the sender is a tab, that tab is skipped when updating titles. */ -- (void) updateAllTabTitles:(id)sender +- (void)updateAllTabTitles:(id)sender { - for (NSTabViewItem *eachItem in [tabView tabViewItems]) { + for (NSTabViewItem *eachItem in [tabView tabViewItems]) + { SPDatabaseDocument *eachDocument = [eachItem identifier]; - if (eachDocument != sender) [eachDocument updateWindowTitle:self]; + + if (eachDocument != sender) { + [eachDocument updateWindowTitle:self]; + } } } - /** * Close the current tab, or if it's the last in the window, the window. */ -- (IBAction) closeTab:(id)sender +- (IBAction)closeTab:(id)sender { // Return if the selected tab shouldn't be closed if (![selectedTableDocument parentTabShouldClose]) return; @@ -211,10 +186,12 @@ enum { */ - (IBAction) selectNextDocumentTab:(id)sender { - if([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == [tabView numberOfTabViewItems] - 1) + if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == [tabView numberOfTabViewItems] - 1) { [tabView selectFirstTabViewItem:nil]; - else + } + else { [tabView selectNextTabViewItem:nil]; + } } /** @@ -222,10 +199,12 @@ enum { */ - (IBAction) selectPreviousDocumentTab:(id)sender { - if([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0) + if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0) { [tabView selectLastTabViewItem:nil]; - else + } + else { [tabView selectPreviousTabViewItem:nil]; + } } /** @@ -299,9 +278,8 @@ enum { } /** - * Toggle Tab Bar Visibility + * Toggle the tab bar's visibility. */ - - (IBAction)toggleTabBarShown:(id)sender { [tabBar setHideForSingleTab:![tabBar hideForSingleTab]]; @@ -309,15 +287,14 @@ enum { } /** - * Menu validation + * Menu item validation. */ -- (BOOL) validateMenuItem:(NSMenuItem *)menuItem +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - // Select Next/Previous/Move Tab - if ( [menuItem action] == @selector(selectPreviousDocumentTab:) - || [menuItem action] == @selector(selectNextDocumentTab:) - || [menuItem action] == @selector(moveSelectedTabInNewWindow:)) + if ([menuItem action] == @selector(selectPreviousDocumentTab:) || + [menuItem action] == @selector(selectNextDocumentTab:) || + [menuItem action] == @selector(moveSelectedTabInNewWindow:)) { return ([tabView numberOfTabViewItems] != 1); } @@ -325,6 +302,7 @@ enum { // Show/hide Tab bar if ([menuItem action] == @selector(toggleTabBarShown:)) { [menuItem setTitle:(![tabBar isTabBarHidden] ? NSLocalizedString(@"Hide Tab Bar", @"hide tab bar") : NSLocalizedString(@"Show Tab Bar", @"show tab bar"))]; + return [[tabBar cells] count] <= 1; } @@ -351,12 +329,12 @@ enum { */ - (void)selectTabAtIndex:(NSInteger)index { - if([[tabBar cells] count] > 0 && [[tabBar cells] count] > (NSUInteger)index) { + if ([[tabBar cells] count] > 0 && [[tabBar cells] count] > (NSUInteger)index) { [tabView selectTabViewItemAtIndex:index]; - } else if([[tabBar cells] count]) { + } + else if ([[tabBar cells] count]) { [tabView selectTabViewItemAtIndex:0]; } - } - (void)setHideForSingleTab:(BOOL)hide @@ -382,10 +360,14 @@ enum { * of NSInvocation (see forwardInvocation: docs for background). Must be paired * with methodSignationForSelector:. */ -- (void) forwardInvocation:(NSInvocation *)theInvocation +- (void)forwardInvocation:(NSInvocation *)theInvocation { SEL theSelector = [theInvocation selector]; - if (![selectedTableDocument respondsToSelector:theSelector]) [self doesNotRecognizeSelector:theSelector]; + + if (![selectedTableDocument respondsToSelector:theSelector]) { + [self doesNotRecognizeSelector:theSelector]; + } + [theInvocation invokeWithTarget:selectedTableDocument]; } @@ -393,19 +375,18 @@ enum { * Return the correct method signatures for the selected table document if * NSObject doesn't implement the requested methods. */ -- (NSMethodSignature *) methodSignatureForSelector:(SEL)theSelector +- (NSMethodSignature *)methodSignatureForSelector:(SEL)theSelector { NSMethodSignature *defaultSignature = [super methodSignatureForSelector:theSelector]; - if (defaultSignature) return defaultSignature; - - return [selectedTableDocument methodSignatureForSelector:theSelector]; + + return defaultSignature ? defaultSignature : [selectedTableDocument methodSignatureForSelector:theSelector]; } /** * Override the default repondsToSelector:, returning true if either NSObject * or the selected table document supports the selector. */ -- (BOOL) respondsToSelector:(SEL)theSelector +- (BOOL)respondsToSelector:(SEL)theSelector { return ([super respondsToSelector:theSelector] || (selectedTableDocument && [selectedTableDocument respondsToSelector:theSelector])); } @@ -414,22 +395,32 @@ enum { * Override the default performSelector:, again either using NSObject defaults * or performing the selector on the selected table document. */ -- (id) performSelector:(SEL)theSelector +- (id)performSelector:(SEL)theSelector { - if ([super respondsToSelector:theSelector]) return [super performSelector:theSelector]; + if ([super respondsToSelector:theSelector]) { + return [super performSelector:theSelector]; + } - if (![selectedTableDocument respondsToSelector:theSelector]) [self doesNotRecognizeSelector:theSelector]; + if (![selectedTableDocument respondsToSelector:theSelector]) { + [self doesNotRecognizeSelector:theSelector]; + } + return [selectedTableDocument performSelector:theSelector]; } /** * Override the default performSelector:withObject: - see performSelector: */ -- (id) performSelector:(SEL)theSelector withObject:(id)theObject +- (id)performSelector:(SEL)theSelector withObject:(id)theObject { - if ([super respondsToSelector:theSelector]) return [super performSelector:theSelector withObject:theObject]; + if ([super respondsToSelector:theSelector]) { + return [super performSelector:theSelector withObject:theObject]; + } - if (![selectedTableDocument respondsToSelector:theSelector]) [self doesNotRecognizeSelector:theSelector]; + if (![selectedTableDocument respondsToSelector:theSelector]) { + [self doesNotRecognizeSelector:theSelector]; + } + return [selectedTableDocument performSelector:theSelector withObject:theObject]; } @@ -442,21 +433,56 @@ enum { [tabBar update]; } +#pragma mark - +#pragma mark Private API + /** - * Binds a tab bar item's progress indicator to the represented - * tableDocument. + * Set up the window's tab bar. + */ +- (void)_setUpTabBar +{ + [tabBar setStyleNamed:@"SequelPro"]; + [tabBar setCanCloseOnlyTab:NO]; + [tabBar setHideForSingleTab:![[NSUserDefaults standardUserDefaults] boolForKey:SPAlwaysShowWindowTabBar]]; + [tabBar setShowAddTabButton:YES]; + [tabBar setSizeCellsToFit:NO]; + [tabBar setCellMinWidth:100]; + [tabBar setCellMaxWidth:250]; + [tabBar setCellOptimumWidth:250]; + [tabBar setSelectsTabsOnMouseDown:YES]; + [tabBar setCreatesTabOnDoubleClick:YES]; + [tabBar setTearOffStyle:PSMTabBarTearOffAlphaWindow]; + [tabBar setUsesSafariStyleDragging:YES]; + + // Hook up add tab button + [tabBar setCreateNewTabTarget:self]; + [tabBar setCreateNewTabAction:@selector(addNewConnection:)]; + + // Set the double click target and action + [tabBar setDoubleClickTarget:self]; + [tabBar setDoubleClickAction:@selector(openDatabaseInNewTab)]; +} + +/** + * Binds a tab bar item's progress indicator to the represented tableDocument. */ - (void)_updateProgressIndicatorForItem:(NSTabViewItem *)theItem { PSMTabBarCell *theCell = [[tabBar cells] objectAtIndex:[tabView indexOfTabViewItem:theItem]]; + [[theCell indicator] setControlSize:NSSmallControlSize]; + SPDatabaseDocument *theDocument = [theItem identifier]; [[theCell indicator] setHidden:NO]; + NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary]; + [bindingOptions setObject:NSNegateBooleanTransformerName forKey:@"NSValueTransformerName"]; + [[theCell indicator] bind:@"animate" toObject:theDocument withKeyPath:@"isProcessing" options:nil]; [[theCell indicator] bind:@"hidden" toObject:theDocument withKeyPath:@"isProcessing" options:bindingOptions]; + [theDocument addObserver:self forKeyPath:@"isProcessing" options:0 context:nil]; } @@ -486,20 +512,37 @@ enum { */ - (void)_updateLineHidingViewState { - // Set the background colour to match the titlebar window state if ((([[self window] isMainWindow] || [[[self window] attachedSheet] isMainWindow]) && [NSApp isActive])) { - [titleBarLineHidingView setBackgroundColor:[NSColor colorWithCalibratedWhite:(systemVersion >= 0x1070)?0.66f:0.63f alpha:1.0]]; - } else { - [titleBarLineHidingView setBackgroundColor:[NSColor colorWithCalibratedWhite:(systemVersion >= 0x1070)?0.87f:0.84f alpha:1.0]]; + [titleBarLineHidingView setBackgroundColor:[NSColor colorWithCalibratedWhite:(systemVersion >= 0x1070) ? 0.66f : 0.63f alpha:1.0]]; + } + else { + [titleBarLineHidingView setBackgroundColor:[NSColor colorWithCalibratedWhite:(systemVersion >= 0x1070) ? 0.87f : 0.84f alpha:1.0]]; } // If the window is fullscreen or the toolbar is showing, hide the view; otherwise show it if (([[self window] styleMask] & NSFullScreenWindowMask) || [[[self window] toolbar] isVisible] || ![[self window] toolbar]) { [titleBarLineHidingView setHidden:YES]; - } else { + } + else { [titleBarLineHidingView setHidden:NO]; } } +#pragma mark - + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [NSObject cancelPreviousPerformRequestsWithTarget:self]; + + // Tear down the animations on the tab bar to stop redraws + [tabBar destroyAnimations]; + + [managedDatabaseConnections release]; + + [super dealloc]; +} + @end -- cgit v1.2.3