cog/Utils/SideViewController.m

184 lines
5.2 KiB
Matlab
Raw Normal View History

2009-03-07 21:31:44 +00:00
//
// SplitViewController.m
// Cog
//
// Created by Vincent Spader on 6/20/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "SideViewController.h"
#import "Logging.h"
2009-03-07 21:31:44 +00:00
@implementation SideViewController
- (NSString *)showSideViewDefaultsKey {
2009-03-07 21:31:44 +00:00
return [NSString stringWithFormat:@"%@ShowSideView", [self nibName]];
}
- (NSString *)sideViewVerticalDefaultsKey {
2009-03-07 21:31:44 +00:00
return [NSString stringWithFormat:@"%@SideViewVertical", [self nibName]];
}
- (NSString *)sideViewDividerPositionDefaultsKey {
2009-03-07 21:31:44 +00:00
return [NSString stringWithFormat:@"%@SideViewDividerPosition", [self nibName]];
}
- (void)registerDefaults {
2009-03-07 21:31:44 +00:00
NSMutableDictionary *userDefaultsValuesDict = [NSMutableDictionary dictionary];
2009-03-07 22:35:39 +00:00
[userDefaultsValuesDict setObject:[NSNumber numberWithBool:YES] forKey:[self sideViewVerticalDefaultsKey]];
[userDefaultsValuesDict setObject:[NSNumber numberWithBool:NO] forKey:[self showSideViewDefaultsKey]];
[userDefaultsValuesDict setObject:[NSNumber numberWithFloat:100.0] forKey:[self sideViewDividerPositionDefaultsKey]];
2009-03-07 21:31:44 +00:00
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
}
- (id)initWithNibName:(NSString *)nib bundle:(NSBundle *)bundle {
2009-03-07 21:31:44 +00:00
self = [super initWithNibName:nib bundle:bundle];
if(self) {
2009-03-07 21:31:44 +00:00
[self registerDefaults];
}
2009-03-07 21:31:44 +00:00
return self;
}
- (void)awakeFromNib {
[splitView setVertical:[[NSUserDefaults standardUserDefaults] boolForKey:[self sideViewVerticalDefaultsKey]]];
if([[NSUserDefaults standardUserDefaults] boolForKey:[self showSideViewDefaultsKey]]) {
2009-03-07 21:31:44 +00:00
[self showSideView];
}
}
- (IBAction)toggleSideView:(id)sender {
// Show/hide current
if([self sideViewIsHidden]) {
2009-03-07 21:31:44 +00:00
[self showSideView];
} else {
2009-03-07 21:31:44 +00:00
[self hideSideView];
}
2009-03-07 21:31:44 +00:00
[splitView adjustSubviews];
}
- (IBAction)toggleVertical:(id)sender {
2009-03-07 21:31:44 +00:00
[splitView setVertical:![splitView isVertical]];
if(![self sideViewIsHidden]) {
2009-03-07 21:31:44 +00:00
[self showSideView];
}
2009-03-07 21:31:44 +00:00
[splitView adjustSubviews];
2009-03-07 21:31:44 +00:00
[[NSUserDefaults standardUserDefaults] setBool:[splitView isVertical] forKey:[self sideViewVerticalDefaultsKey]];
}
- (void)showSideView {
if([splitView isVertical]) {
[splitView setSubviews:@[[self view], mainView]];
} else {
[splitView setSubviews:@[mainView, [self view]]];
2009-03-07 21:31:44 +00:00
}
[self setDividerPosition:[[NSUserDefaults standardUserDefaults] floatForKey:[self sideViewDividerPositionDefaultsKey]]];
2009-03-07 21:31:44 +00:00
[[[self view] window] makeFirstResponder:firstResponder];
2009-03-07 21:31:44 +00:00
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:[self showSideViewDefaultsKey]];
}
- (void)hideSideView {
[splitView setSubviews:@[mainView]];
2009-03-07 21:31:44 +00:00
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:[self showSideViewDefaultsKey]];
2009-03-07 21:31:44 +00:00
[[mainView window] makeFirstResponder:mainView];
}
- (BOOL)sideViewIsHidden {
2009-03-07 21:31:44 +00:00
return ([[splitView subviews] count] == 1);
}
- (BOOL)splitView:(NSSplitView *)aSplitView canCollapseSubview:(NSView *)subview {
2009-03-07 21:31:44 +00:00
return (subview != mainView);
}
- (BOOL)splitView:(NSSplitView *)aSplitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex {
2009-03-07 21:31:44 +00:00
return (subview != mainView);
}
- (void)splitViewDidResizeSubviews:(NSNotification *)aNotification {
// Update default
if(![self sideViewIsHidden]) {
2009-03-07 21:31:44 +00:00
[[NSUserDefaults standardUserDefaults] setFloat:[self dividerPosition] forKey:[self sideViewDividerPositionDefaultsKey]];
DLog(@"DIVIDER POSITION: %f", [self dividerPosition]);
2009-03-07 21:31:44 +00:00
}
}
- (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize {
if([self sideViewIsHidden]) {
2009-03-07 21:31:44 +00:00
[splitView adjustSubviews];
} else {
2009-03-07 21:31:44 +00:00
CGFloat dividerThickness = [splitView dividerThickness];
2009-03-07 21:31:44 +00:00
NSRect sideRect = [[self view] frame];
NSRect mainRect = [mainView frame];
2009-03-07 21:31:44 +00:00
NSRect newFrame = [splitView frame];
if([splitView isVertical]) {
2009-03-07 21:31:44 +00:00
sideRect.size.width = [[NSUserDefaults standardUserDefaults] floatForKey:[self sideViewDividerPositionDefaultsKey]];
sideRect.size.height = newFrame.size.height;
sideRect.origin = NSMakePoint(0, 0);
2009-03-07 21:31:44 +00:00
mainRect.size.width = newFrame.size.width - sideRect.size.width - dividerThickness;
mainRect.size.height = newFrame.size.height;
mainRect.origin.x = sideRect.size.width + dividerThickness;
} else {
2009-03-07 21:31:44 +00:00
sideRect.size.height = [[NSUserDefaults standardUserDefaults] floatForKey:[self sideViewDividerPositionDefaultsKey]];
sideRect.size.width = newFrame.size.width;
2009-03-07 21:31:44 +00:00
mainRect.origin = NSMakePoint(0, 0);
mainRect.size.width = newFrame.size.width;
mainRect.size.height = newFrame.size.height - sideRect.size.height - dividerThickness;
2009-03-07 21:31:44 +00:00
sideRect.origin.y = mainRect.size.height + dividerThickness;
}
2009-03-07 21:31:44 +00:00
[[self view] setFrame:sideRect];
[mainView setFrame:mainRect];
}
}
- (float)dividerPosition {
if([splitView isVertical]) {
2009-03-07 21:31:44 +00:00
return [[self view] frame].size.width;
}
2009-03-07 21:31:44 +00:00
return [[self view] frame].size.height;
}
- (void)setDividerPosition:(float)position {
2009-03-07 21:31:44 +00:00
float actualPosition = position;
if(![splitView isVertical]) {
2009-03-07 21:31:44 +00:00
actualPosition = ([splitView frame].size.height - position);
}
2009-03-07 21:31:44 +00:00
[splitView adjustSubviews];
[splitView setPosition:actualPosition ofDividerAtIndex:0];
2009-03-07 21:31:44 +00:00
[[NSUserDefaults standardUserDefaults] setFloat:position forKey:[self sideViewDividerPositionDefaultsKey]];
}
2018-06-28 10:59:59 +00:00
// placeholders, implemented by subclass
- (void)doAddToPlaylist:(NSArray *)urls origin:(URLOrigin)origin {
2018-06-28 10:59:59 +00:00
}
- (void)clear:(id)sender {
2018-06-28 10:59:59 +00:00
}
- (void)playPauseResume:(id)sender {
2018-06-28 10:59:59 +00:00
}
2009-03-07 21:31:44 +00:00
@end