Fairly large ITTSW checkin.
[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 /*************************************************************************/
21 #pragma mark -
22 #pragma mark APPEAR METHODS
23 /*************************************************************************/
24
25 - (void)performAppear
26 {
27     [self setWindowVisibility:ITTransientStatusWindowAppearingState];
28     [self performAppearFromProgress:0.0 effectTime:_effectTime];
29 }
30
31 - (void)performAppearFromProgress:(float)progress effectTime:(float)time
32 {
33     [_window setEffectProgress:progress];
34     _effectSpeed = (1.0 / (EFFECT_FPS * time));
35     
36     if ( progress == 0.0 ) {
37         [self setPivot:315.0];
38         [_window setAlphaValue:0.0];
39     }
40
41     [_window orderFront:self];
42     _effectTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / EFFECT_FPS)
43                                                     target:self
44                                                   selector:@selector(appearStep)
45                                                   userInfo:nil
46                                                    repeats:YES];
47 }
48
49 - (void)appearStep
50 {
51     float interPivot = 0.0;
52     [_window setEffectProgress:([_window effectProgress] + _effectSpeed)];
53     [_window setEffectProgress:( ([_window effectProgress] < 1.0) ? [_window effectProgress] : 1.0)];
54     interPivot = (( sin(([_window effectProgress] * pi) - (pi / 2)) + 1 ) / 2);
55     [self setPivot:((interPivot * 45) + 315)];
56     [_window setAlphaValue:interPivot];
57
58     if ( [_window effectProgress] >= 1.0 ) {
59         [self appearFinish];
60     }
61 }
62
63 - (void)appearFinish
64 {
65     [_effectTimer invalidate];
66     _effectTimer = nil;
67     [self setWindowVisibility:ITTransientStatusWindowVisibleState];
68 }
69
70 - (void)cancelAppear
71 {
72     [self setWindowVisibility:ITTransientStatusWindowVanishingState];
73
74     [_effectTimer invalidate];
75     _effectTimer = nil;
76
77     [self performVanishFromProgress:[_window effectProgress] effectTime:(_effectTime / 3.5)];
78 }
79
80
81 /*************************************************************************/
82 #pragma mark -
83 #pragma mark VANISH METHODS
84 /*************************************************************************/
85
86 - (void)performVanish
87 {
88     [self setWindowVisibility:ITTransientStatusWindowVanishingState];
89     [self performVanishFromProgress:1.0 effectTime:_effectTime];
90 }
91
92 - (void)performVanishFromProgress:(float)progress effectTime:(float)time
93 {
94     [_window setEffectProgress:progress];
95     _effectSpeed = (1.0 / (EFFECT_FPS * time));
96     if ( progress == 1.0 ) {
97         [self setPivot:0.0];
98         [_window setAlphaValue:1.0];
99     }
100
101     [_window orderFront:self];
102     _effectTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / EFFECT_FPS)
103                                                     target:self
104                                                   selector:@selector(vanishStep)
105                                                   userInfo:nil
106                                                    repeats:YES];
107 }
108
109 - (void)vanishStep
110 {
111     float interPivot = 1.0;
112     [_window setEffectProgress:([_window effectProgress] - _effectSpeed)];
113     [_window setEffectProgress:( ([_window effectProgress] > 0.0) ? [_window effectProgress] : 0.0)];
114     interPivot = (( sin(([_window effectProgress] * pi) - (pi / 2)) + 1 ) / 2);
115     [self setPivot:((interPivot * 45) + 315)];
116     [_window setAlphaValue:interPivot];
117
118     if ( [_window effectProgress] <= 0.0 ) {
119         [self vanishFinish];
120     }
121 }
122
123 - (void)vanishFinish
124 {
125     [_effectTimer invalidate];
126     _effectTimer = nil;
127     [_window orderOut:self];
128     [_window setAlphaValue:1.0];
129     [self setWindowVisibility:ITTransientStatusWindowHiddenState];
130 }
131
132 - (void)cancelVanish
133 {
134     [self setWindowVisibility:ITTransientStatusWindowAppearingState];
135
136     [_effectTimer invalidate];
137     _effectTimer = nil;
138
139     [self performAppearFromProgress:[_window effectProgress] effectTime:(_effectTime / 3.5)];
140 }
141
142
143 /*************************************************************************/
144 #pragma mark -
145 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
146 /*************************************************************************/
147
148 - (void)setPivot:(float)angle
149 {
150     float degAngle = (angle * (pi / 180));
151
152     CGAffineTransform transform = CGAffineTransformMakeRotation(degAngle);
153     
154  // Set pivot rotation point
155     transform.tx = -32.0;
156     transform.ty = [_window frame].size.height + 32.0;
157
158     CGSSetWindowTransform([NSApp contextID],
159                           (CGSWindowID)[_window windowNumber],
160                           CGAffineTransformTranslate(transform,
161                                                      (([_window frame].origin.x - 32.0) * -1),
162                                                      (([[_window screen] frame].size.height - ([_window frame].origin.y) + 32.0) * -1) ));
163 }
164
165
166 @end