Resize NSSplitView Nicely

- (void) splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize {
// <a href="http://www.wodeveloper.com/omniLists/macosx-dev/2003/May/msg00261.html" >http://www.wodeveloper.com/omniLists/macosx-dev/2003/May/msg00261.html</a>

// grab the splitviews
NSView *left = [[sender subviews] objectAtIndex:0];
NSView *right = [[sender subviews] objectAtIndex:1];

float dividerThickness = [sender dividerThickness];

// get the different frames
NSRect newFrame = [sender frame];
NSRect leftFrame = [left frame];
NSRect rightFrame = [right frame];

// change in width for this redraw
int	dWidth  = newFrame.size.width - oldSize.width;

// ratio of the left frame width to the right used for resize speed when both panes are being resized
float rLeftRight = (leftFrame.size.width - MIN_LEFT_PANEL_W) / rightFrame.size.width;

// resize the height of the left
leftFrame.size.height = newFrame.size.height;
leftFrame.origin = NSMakePoint(0,0);

// resize the left & right pane equally if we are shrinking the frame
// resize the right pane only if we are increasing the frame
// when resizing lock at minimum width for the left panel
if(leftFrame.size.width <= MIN_LEFT_PANEL_W && dWidth < 0) {
rightFrame.size.width += dWidth;
} else if(dWidth > 0) {
rightFrame.size.width += dWidth;
} else {
leftFrame.size.width += dWidth * rLeftRight;
rightFrame.size.width += dWidth * (1 - rLeftRight);
}

rightFrame.size.width = newFrame.size.width - leftFrame.size.width - dividerThickness;
rightFrame.size.height = newFrame.size.height;
rightFrame.origin.x = leftFrame.size.width + dividerThickness;

[left setFrame:leftFrame];
[right setFrame:rightFrame];
}

source

Leave a Reply