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