Intermediary checkin, just to mark my place and back up, in case Panther decides...
[ITKit.git] / ITPivotWindowEffect.m
1 #import "ITPivotWindowEffect.h"
2 #import "ITCoreGraphicsHacks.h"
3 #import "ITTransientStatusWindow.h"
4
5
6 @interface ITPivotWindowEffect (Private)
7 - (void)setPivot:(float)angle;
8 - (void)performAppearFromProgress:(float)progress effectTime:(float)time;
9 - (void)appearStep;
10 - (void)appearFinish;
11 - (void)performVanishFromProgress:(float)progress effectTime:(float)time;
12 - (void)vanishStep;
13 - (void)vanishFinish;
14 @end
15
16
17 @implementation ITPivotWindowEffect
18
19
20 + (NSString *)effectName
21 {
22     return @"Pivot";
23 }
24
25 + (NSDictionary *)supportedPositions
26 {
27     return [NSDictionary dictionaryWithObjectsAndKeys:
28         [NSDictionary dictionaryWithObjectsAndKeys:
29             [NSNumber numberWithBool:YES], @"Left",
30             [NSNumber numberWithBool:NO], @"Center",
31             [NSNumber numberWithBool:YES], @"Right", nil] , @"Top" ,
32         [NSDictionary dictionaryWithObjectsAndKeys:
33             [NSNumber numberWithBool:NO], @"Left",
34             [NSNumber numberWithBool:NO], @"Center",
35             [NSNumber numberWithBool:NO], @"Right", nil] , @"Middle" ,
36         [NSDictionary dictionaryWithObjectsAndKeys:
37             [NSNumber numberWithBool:YES], @"Left",
38             [NSNumber numberWithBool:NO], @"Center",
39             [NSNumber numberWithBool:YES], @"Right", nil] , @"Bottom" , nil];
40 }
41
42
43 /*************************************************************************/
44 #pragma mark -
45 #pragma mark APPEAR METHODS
46 /*************************************************************************/
47
48 - (void)performAppear
49 {
50     __idle = NO;
51     
52     [self setWindowVisibility:ITWindowAppearingState];
53     [self performAppearFromProgress:0.0 effectTime:_effectTime];
54 }
55
56 - (void)performAppearFromProgress:(float)progress effectTime:(float)time
57 {
58     [_window setEffectProgress:progress];
59     _effectSpeed = (1.0 / (EFFECT_FPS * time));
60     
61     if ( progress == 0.0 ) {
62         [self setPivot:315.0];
63         [_window setAlphaValue:0.0];
64     }
65
66     [_window orderFront:self];
67     _effectTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / EFFECT_FPS)
68                                                     target:self
69                                                   selector:@selector(appearStep)
70                                                   userInfo:nil
71                                                    repeats:YES];
72 }
73
74 - (void)appearStep
75 {
76     float interPivot = 0.0;
77     [_window setEffectProgress:([_window effectProgress] + _effectSpeed)];
78     [_window setEffectProgress:( ([_window effectProgress] < 1.0) ? [_window effectProgress] : 1.0)];
79     interPivot = (( sin(([_window effectProgress] * pi) - (pi / 2)) + 1 ) / 2);
80     [self setPivot:((interPivot * 45) + 315)];
81     [_window setAlphaValue:interPivot];
82
83     if ( [_window effectProgress] >= 1.0 ) {
84         [self appearFinish];
85     }
86 }
87
88 - (void)appearFinish
89 {
90     [_effectTimer invalidate];
91     _effectTimer = nil;
92     [self setWindowVisibility:ITWindowVisibleState];
93
94     __idle = YES;
95
96     if ( __shouldReleaseWhenIdle ) {
97         [self release];
98     }
99 }
100
101 - (void)cancelAppear
102 {
103     [self setWindowVisibility:ITWindowVanishingState];
104
105     [_effectTimer invalidate];
106     _effectTimer = nil;
107
108     [self performVanishFromProgress:[_window effectProgress] effectTime:(_effectTime / 3.5)];
109 }
110
111
112 /*************************************************************************/
113 #pragma mark -
114 #pragma mark VANISH METHODS
115 /*************************************************************************/
116
117 - (void)performVanish
118 {
119     __idle = NO;
120     
121     [self setWindowVisibility:ITWindowVanishingState];
122     [self performVanishFromProgress:1.0 effectTime:_effectTime];
123 }
124
125 - (void)performVanishFromProgress:(float)progress effectTime:(float)time
126 {
127     [_window setEffectProgress:progress];
128     _effectSpeed = (1.0 / (EFFECT_FPS * time));
129     if ( progress == 1.0 ) {
130         [self setPivot:0.0];
131         [_window setAlphaValue:1.0];
132     }
133
134     [_window orderFront:self];
135     _effectTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / EFFECT_FPS)
136                                                     target:self
137                                                   selector:@selector(vanishStep)
138                                                   userInfo:nil
139                                                    repeats:YES];
140 }
141
142 - (void)vanishStep
143 {
144     float interPivot = 1.0;
145     [_window setEffectProgress:([_window effectProgress] - _effectSpeed)];
146     [_window setEffectProgress:( ([_window effectProgress] > 0.0) ? [_window effectProgress] : 0.0)];
147     interPivot = (( sin(([_window effectProgress] * pi) - (pi / 2)) + 1 ) / 2);
148     [self setPivot:((interPivot * 45) + 315)];
149     [_window setAlphaValue:interPivot];
150
151     if ( [_window effectProgress] <= 0.0 ) {
152         [self vanishFinish];
153     }
154 }
155
156 - (void)vanishFinish
157 {
158     [_effectTimer invalidate];
159     _effectTimer = nil;
160     [_window orderOut:self];
161     [_window setAlphaValue:1.0];
162     [self setPivot:0.0];
163     [self setWindowVisibility:ITWindowHiddenState];
164
165     __idle = YES;
166
167     if ( __shouldReleaseWhenIdle ) {
168         [self release];
169     }
170 }
171
172 - (void)cancelVanish
173 {
174     [self setWindowVisibility:ITWindowAppearingState];
175
176     [_effectTimer invalidate];
177     _effectTimer = nil;
178
179     [self performAppearFromProgress:[_window effectProgress] effectTime:(_effectTime / 3.5)];
180 }
181
182
183 /*************************************************************************/
184 #pragma mark -
185 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
186 /*************************************************************************/
187
188 - (void)setPivot:(float)angle
189 {
190     float degAngle;
191     NSPoint appearPoint;
192     CGAffineTransform transform;
193     
194     if ( [(ITTransientStatusWindow *)_window horizontalPosition] == ITWindowPositionLeft ) {
195         if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionBottom ) {
196             degAngle = (angle * (pi / 180));
197         } else if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionTop ) {
198             degAngle = (-angle * (pi / 180));
199         }
200     } else if ( [(ITTransientStatusWindow *)_window horizontalPosition] == ITWindowPositionRight ) {
201         if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionBottom ) {
202             degAngle = (angle * (pi / 180));
203         } else if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionTop ) {
204             degAngle = (angle * (pi / 180));
205         }
206     }
207     
208     transform = CGAffineTransformMakeRotation(degAngle);
209     
210  // Set pivot rotation point
211     //transform.tx = -( 32.0 + [[_window screen] visibleFrame].origin.x );
212     transform.ty = ( [_window frame].size.height + 32.0 + [[_window screen] visibleFrame].origin.y );
213     
214     if ( [(ITTransientStatusWindow *)_window horizontalPosition] == ITWindowPositionLeft ) {
215         appearPoint.x = -( 32.0 + [[_window screen] visibleFrame].origin.x );
216         transform.tx = -( 32.0 + [[_window screen] visibleFrame].origin.x );
217     } else if ( [(ITTransientStatusWindow *)_window horizontalPosition] == ITWindowPositionRight ) {
218         transform.tx = -( 32.0 + [[_window screen] visibleFrame].origin.x ) + [_window frame].size.width;
219         appearPoint.x = -(([[_window screen] visibleFrame].size.width + [[_window screen] visibleFrame].origin.x) - 64.0);
220     } else if ( [(ITTransientStatusWindow *)_window horizontalPosition] == ITWindowPositionCenter ) {
221         appearPoint.x = ( [_window frame].size.width - [[_window screen] visibleFrame].size.width ) / 2;
222     }
223     
224     if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionTop ) {
225         appearPoint.y = ( [_window frame].size.height - [[_window screen] visibleFrame].size.height) / 2;
226     } else if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionBottom ) {
227         appearPoint.y = -( [[_window screen] frame].size.height - ([_window frame].origin.y) + 32.0 + [[_window screen] visibleFrame].origin.y) ;
228     }/* else if ( [(ITTransientStatusWindow *)_window verticalPosition] == ITWindowPositionMiddle ) {
229         appearPoint.y = ( [_window frame].size.height - [[_window screen] visibleFrame].size.height) / 2;
230     }*/
231     CGSSetWindowTransform([NSApp contextID],
232                           (CGSWindowID)[_window windowNumber],
233                           CGAffineTransformTranslate( transform,
234                                                      appearPoint.x,
235                                                      appearPoint.y ) );
236 }
237
238 @end