-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSWindowController.m
267 lines (194 loc) · 8.33 KB
/
TSWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// TSWindowController.m
// ThisService
//
// Created by Jesper on 2011-07-11.
// Copyright 2011-2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
#import "TSWindowController.h"
#import "TSViewController.h"
#import "TSCreateServiceViewController.h"
#import "TSPackUpViewController.h"
#import "TSModeMatrix.h"
#import "TSService.h"
@implementation TSWindowController
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
return YES;
}
- (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication {
[[self window] makeKeyAndOrderFront:self];
return YES;
}
- (void)awakeFromNib {
NSArray *viewControllerClasses = [NSArray arrayWithObjects:[TSCreateServiceViewController class], [TSPackUpViewController class], nil];
NSMutableArray *newViewControllers = [NSMutableArray arrayWithCapacity:[viewControllerClasses count]];
for (Class cls in viewControllerClasses) {
NSString *str = NSStringFromClass(cls);
TSViewController *vc = [[cls alloc] initWithNibName:str bundle:nil];
[newViewControllers addObject:[vc autorelease]];
}
viewControllers = [newViewControllers copy];
[modeMatrix setContentInitialized];
}
- (TSViewController *)viewControllerForTag:(NSInteger)tag {
if (tag == NSNotFound)
return nil;
TSViewController *viewController = (TSViewController *)[viewControllers objectAtIndex:tag];
return viewController;
}
- (BOOL)modeMatrix:(TSModeMatrix *)matrix shouldSwitchFromTag:(NSInteger)previousTag toTag:(NSInteger)newTag {
TSViewController *goingViewController = [self viewControllerForTag:previousTag];
TSViewController *becomingViewController = [self viewControllerForTag:newTag];
if (goingViewController == nil) return YES;
if ([becomingViewController isEqual:goingViewController]) return NO;
BOOL okay = [becomingViewController shouldSwitchToViewController:becomingViewController];
return okay;
}
- (NSSize)contentViewSizeForViewWithSize:(NSSize)viewSize {
CGFloat heightOfModeMatrix = [modeMatrix frame].size.height;
return NSMakeSize(viewSize.width, viewSize.height + heightOfModeMatrix);
}
- (NSSize)contentViewSizeForView:(NSView *)view {
return [self contentViewSizeForViewWithSize:[view frame].size];
}
- (NSRect)frameRectForModeMatrixGivenView:(NSView *)view {
NSSize sizeContentView = [self contentViewSizeForView:view];
CGFloat heightOfModeMatrix = [modeMatrix frame].size.height;
return NSMakeRect(0, sizeContentView.height - heightOfModeMatrix, sizeContentView.width, heightOfModeMatrix);
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification {
TSViewController *vc = [self viewControllerForTag:[modeMatrix selectedTag]];
if (nil == vc) return;
[vc viewWillAppearInContainer:self];
}
- (void)resizeViewController:(TSViewController *)vc toNewContentSize:(NSSize)size animate:(BOOL)animate {
NSWindow *w = [self window];
NSView *refreshingView = vc.view;
NSView *contentView = [w contentView];
NSSize newContentViewSize = [self contentViewSizeForViewWithSize:size];
NSSize oldContentViewSize = [contentView frame].size;
CGFloat diffBetweenSize = newContentViewSize.height - oldContentViewSize.height;
NSRect newViewFrameRect = [refreshingView frame];
newViewFrameRect.size = size;
NSRect windowRect = [w frame];
NSRect newFrameRect = [w frameRectForContentRect:NSMakeRect(windowRect.origin.x, windowRect.origin.y - diffBetweenSize, newContentViewSize.width, newContentViewSize.height)];
NSSize contentSizeOfBecomingView = [refreshingView frame].size;
[refreshingView setFrame:NSMakeRect(0, 0, contentSizeOfBecomingView.width, contentSizeOfBecomingView.height)];
[subviewView setAutoresizesSubviews:YES];
if (animate) {
if (currentAnimation) {
[currentAnimation stopAnimation];
}
NSViewAnimation *anim = [[NSViewAnimation alloc] initWithViewAnimations:
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
w, NSViewAnimationTargetKey,
[NSValue valueWithRect:newFrameRect], NSViewAnimationEndFrameKey,
nil],
nil]
];
[anim setDelegate:self];
[anim setDuration:[[self window] animationResizeTime:newFrameRect]*0.85];
[anim startAnimation];
currentAnimation = anim;
} else {
[w setFrame:newFrameRect display:YES];
}
}
- (void)modeMatrix:(TSModeMatrix *)matrix switchFromTag:(NSInteger)previousTag toTag:(NSInteger)newTag animate:(BOOL)animate {
TSViewController *goingViewController = [self viewControllerForTag:previousTag];
TSViewController *becomingViewController = [self viewControllerForTag:newTag];
if (!goingViewController && !becomingViewController) return;
NSView *goingView = [goingViewController view];
NSView *becomingView = [becomingViewController view];
NSWindow *w = [self window];
NSView *contentView = [w contentView];
NSSize newContentViewSize = [self contentViewSizeForView:becomingView];
NSSize oldContentViewSize = [contentView frame].size;
CGFloat diffBetweenSize = newContentViewSize.height - oldContentViewSize.height;
NSRect windowRect = [w frame];
NSRect newFrameRect = [w frameRectForContentRect:NSMakeRect(windowRect.origin.x, windowRect.origin.y - diffBetweenSize, newContentViewSize.width, newContentViewSize.height)];
NSSize contentSizeOfBecomingView = [becomingView frame].size;
[becomingView setFrame:NSMakeRect(0, 0, contentSizeOfBecomingView.width, contentSizeOfBecomingView.height)];
[subviewView addSubview:becomingView positioned:NSWindowBelow relativeTo:modeMatrix];
[subviewView setAutoresizesSubviews:NO];
[becomingViewController viewWillAppearInContainer:self];
if (animate) {
if (currentAnimation) {
[currentAnimation stopAnimation];
}
NSViewAnimation *anim = [[NSViewAnimation alloc] initWithViewAnimations:
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
goingView, NSViewAnimationTargetKey,
NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey,
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
becomingView, NSViewAnimationTargetKey,
NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
w, NSViewAnimationTargetKey,
[NSValue valueWithRect:newFrameRect], NSViewAnimationEndFrameKey,
nil],
nil]
];
[anim setDelegate:self];
[anim setDuration:[[self window] animationResizeTime:newFrameRect]];
[anim startAnimation];
currentAnimation = anim;
} else {
[w setFrame:newFrameRect display:YES];
if (goingView != becomingView) {
[goingView setHidden:YES];
}
[becomingView setHidden:NO];
}
/* This is the code I would have liked to have used.
It starts to animate and then falls apart in spectacular ways,
including logging "unlockFocus called too many time." [sic].
The internet tells me to balance my NSGraphicsContext saves and restores,
of which I have none.
#define ANIMATE_IF_NEEDED(x) (animate ? ([x animator]) : x)
[becomingView setAlphaValue:0.0];
[becomingView setHidden:NO];
[goingView setAlphaValue:1.0];
[ANIMATE_IF_NEEDED(w) setFrame:newFrameRect display:YES];
if (goingView != becomingView) {
[ANIMATE_IF_NEEDED(goingView) setAlphaValue:0.0];
[ANIMATE_IF_NEEDED(goingView) setHidden:YES];
}
[ANIMATE_IF_NEEDED(becomingView) setAlphaValue:1.0];
[ANIMATE_IF_NEEDED(becomingView) setHidden:NO];*/
if (becomingViewController != goingViewController) {
[goingViewController viewWillDisappear];
}
}
-(void)animationDidEnd:(NSAnimation *)animation {
[animation autorelease];
if (animation != currentAnimation) return;
currentAnimation = nil;
}
- (IBAction)showPackUp:(id)sender {
[[self window] makeKeyAndOrderFront:nil];
[modeMatrix goToTag:1];
}
- (IBAction)showCreateService:(id)sender {
[[self window] makeKeyAndOrderFront:nil];
[modeMatrix goToTag:0];
}
-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
TSViewController *vc = [self viewControllerForTag:[modeMatrix selectedTag]];
if (nil == vc) return NO;
return [vc shouldOpenFileAtPath:filename];
}
- (void)dealloc {
[viewControllers release];
[currentAnimation release];
[super dealloc];
}
@end