Source

BWToolkit / BWSplitViewInspector.m

Diff from to

File BWSplitViewInspector.m

 #import "NSView+BWAdditions.h"
 
 @interface BWSplitViewInspector (BWSVIPrivate)
-- (void)updateControls;
+- (void)updateSizeInputFields;
 - (BOOL)toggleDividerCheckboxVisibilityWithAnimation:(BOOL)shouldAnimate;
 - (void)updateSizeLabels;
+- (void)updateUnitPopupSelections;
 @end
 
 @implementation BWSplitViewInspector
 
-@synthesize subviewPopupSelection, subviewPopupContent, collapsiblePopupSelection, collapsiblePopupContent, minUnitPopupSelection, maxUnitPopupSelection, splitView, dividerCheckboxCollapsed;
+@synthesize subviewPopupSelection, subviewPopupContent, collapsiblePopupContent, minUnitPopupSelection, maxUnitPopupSelection, splitView, dividerCheckboxCollapsed;
 
 - (NSString *)viewNibName 
 {
 
 - (void)awakeFromNib
 {
-	[minField setDelegate:self];
-	[maxField setDelegate:self];
-	
-	[[NSNotificationCenter defaultCenter] addObserver:self 
-											 selector:@selector(dividerThicknessChanged:)
-												 name:@"BWSplitViewDividerThicknessChanged"
-											   object:splitView];
-	
-	[[NSNotificationCenter defaultCenter] addObserver:self 
-											 selector:@selector(orientationChanged:)
-												 name:@"BWSplitViewOrientationChanged"
-											   object:splitView];
-}
-
-- (void)dividerThicknessChanged:(NSNotification *)notification
-{
-	[self toggleDividerCheckboxVisibilityWithAnimation:YES];
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidEndEditing:) name:NSControlTextDidEndEditingNotification object:minField];
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidEndEditing:) name:NSControlTextDidEndEditingNotification object:maxField];
 }
 
 - (void)updateSizeLabels
-{
+{	
 	if ([splitView isVertical])
 	{
 		[maxLabel setStringValue:@"Max Width"];
 	}
 }
 
-- (void)orientationChanged:(NSNotification *)notification
-{
-	[self updateSizeLabels];
-	[self toggleDividerCheckboxVisibilityWithAnimation:YES];
-}
-
-- (void)setCollapsiblePopupSelection:(int)index
-{
-	collapsiblePopupSelection = index;
-	
-	[splitView setCollapsiblePopupSelection:index];
-	[self toggleDividerCheckboxVisibilityWithAnimation:YES];
-}
-
 - (void)setSplitView:(BWSplitView *)aSplitView
 {
     if (splitView != aSplitView) 
 		
 		[self toggleDividerCheckboxVisibilityWithAnimation:NO];
     }
+	else
+	{
+		[self toggleDividerCheckboxVisibilityWithAnimation:YES];
+	}
 }
 
 - (void)setDividerCheckboxWantsLayer:(NSString *)flag
 	[super refresh];
 
 	if ([[self inspectedObjects] count] > 0)
-	{
+	{	
+		[autosizingView setSplitView:[[self inspectedObjects] objectAtIndex:0]];
+		[autosizingView layoutButtons];
+
 		[self setSplitView:[[self inspectedObjects] objectAtIndex:0]];
 		
 		// Populate the subview popup button
 			[self setCollapsiblePopupContent:[NSMutableArray arrayWithObjects:@"None", @"Left Pane", @"Right Pane",nil]];
 		else
 			[self setCollapsiblePopupContent:[NSMutableArray arrayWithObjects:@"None", @"Top Pane", @"Bottom Pane",nil]];
+		
+		[self updateSizeLabels];
+		[self updateSizeInputFields];
+		[self updateUnitPopupSelections];
 	}
-	
-	// Refresh autosizing view
-	[autosizingView setSplitView:splitView];
-	[autosizingView layoutButtons];
-	
-	[self updateSizeLabels];
-	[self updateControls];
 }
 
 + (BOOL)supportsMultipleObjectInspection
 	[splitView setMaxUnits:tempMaxUnits];
 }
 
-- (void)controlTextDidChange:(NSNotification *)aNotification
+- (void)updateUnitPopupSelections
 {
-	if ([aNotification object] == minField)
-	{
+	minUnitPopupSelection = [[[splitView minUnits] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]] intValue];
+	maxUnitPopupSelection = [[[splitView maxUnits] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]] intValue];
+}
+
+- (void)controlTextDidEndEditing:(NSNotification *)aNotification
+{	
+	if ([aNotification object] == minField || aNotification == nil)
+	{	
 		if ([minField stringValue] != nil && [[minField stringValue] isEqualToString:@""] == NO && [[minField stringValue] isEqualToString:@" "] == NO)
 		{
 			NSNumber *minValue = [NSNumber numberWithInt:[minField intValue]];
 			[splitView setMinValues:tempMinValues];
 		}
 	}
-	else if ([aNotification object] == maxField)
-	{
+	
+	if ([aNotification object] == maxField || aNotification == nil)
+	{	
 		if ([maxField stringValue] != nil && [[maxField stringValue] isEqualToString:@""] == NO && [[maxField stringValue] isEqualToString:@" "] == NO)
 		{
 			NSNumber *maxValue = [NSNumber numberWithInt:[maxField intValue]];
 	}
 }
 
-- (int)collapsiblePopupSelection
-{
-	return [splitView collapsiblePopupSelection];
+- (void)setSubviewPopupSelection:(int)index
+{ 
+	// If someone types into the text field and chooses a different subview without hitting return or clicking out of the field,
+	// the controlTextDidEndEditing notification won't fire and the value won't be saved. So we fire it manually here. 
+	[self controlTextDidEndEditing:nil];
+	
+	subviewPopupSelection = index;
+	
+	// Update the input text fields with the values in the new subview
+	[self updateSizeInputFields];
 }
 
-- (void)setSubviewPopupSelection:(int)index
-{
-	subviewPopupSelection = index;
-	
-	[self updateControls];
-}
-
-- (void)updateControls
+- (void)updateSizeInputFields
 {
 	[minField setObjectValue:[[splitView minValues] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]]];
 	[maxField setObjectValue:[[splitView maxValues] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]]];