bae7b530d8f18458d19d861e0e27f189ba97c621
[ITKit.git] / ITTransientStatusWindow.m
1 #import "ITTransientStatusWindow.h"
2 #import "ITWindowEffect.h"
3 #import <CoreGraphics/CoreGraphics.h>
4 #import "ITCoreGraphicsHacks.h"
5 #import "ITTextField.h"
6 #import "ITGrayRoundedView.h"
7
8 #define EFFECT_FPS 30.0
9
10
11 /*************************************************************************/
12 #pragma mark -
13 #pragma mark PRIVATE METHOD DECLARATIONS
14 /*************************************************************************/
15
16 @interface ITTransientStatusWindow (Private)
17 - (id)initWithContentView:(NSView *)contentView
18                  exitMode:(ITTransientStatusWindowExitMode)exitMode
19            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType;
20 - (void)rebuildWindow;
21 - (void)startVanishTimer;
22 - (void)stopVanishTimer;
23 @end
24
25
26 /*************************************************************************/
27 #pragma mark -
28 #pragma mark IMPLEMENTATION
29 /*************************************************************************/
30
31 @implementation ITTransientStatusWindow
32
33
34 /*************************************************************************/
35 #pragma mark -
36 #pragma mark SHARED STATIC OBJECTS
37 /*************************************************************************/
38
39 static ITTransientStatusWindow *staticWindow = nil;
40
41
42 /*************************************************************************/
43 #pragma mark -
44 #pragma mark INITIALIZATION METHODS
45 /*************************************************************************/
46
47 + (ITTransientStatusWindow *)sharedWindow
48 {
49     if ( ! (staticWindow) ) {
50         staticWindow = [[self alloc] initWithContentView:nil
51                                                 exitMode:ITTransientStatusWindowExitAfterDelay
52                                           backgroundType:ITTransientStatusWindowRounded];
53     }
54     return staticWindow;
55 }
56
57 - (id)initWithContentView:(NSView *)contentView
58                  exitMode:(ITTransientStatusWindowExitMode)exitMode
59            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
60 {
61     NSRect contentRect;
62     
63     // If no Content View was provided, use a generic view.
64     if ( ! (contentView) ) {
65         contentView = [[[NSView alloc] initWithFrame:
66             NSMakeRect(100.0, 100.0, 200.0, 200.0)] autorelease];
67     }
68     
69     // Set the content rect to the frame of the content view, now guaranteed to exist.
70     contentRect = [contentView frame];
71     
72     if ( ( self = [super initWithContentRect:contentRect
73                                    styleMask:NSBorderlessWindowMask
74                                      backing:NSBackingStoreBuffered
75                                        defer:NO] ) ) {
76                                     
77         _visibilityState     = ITWindowHiddenState;
78         _exitMode            = exitMode;
79         _exitDelay           = DEFAULT_EXIT_DELAY;
80         _backgroundType      = backgroundType;
81         _verticalPosition    = ITWindowPositionBottom;
82         _horizontalPosition  = ITWindowPositionLeft;
83         _screenPadding       = 32.0;
84         _screenNumber        = 0;
85         _entryEffect         = nil;
86         _exitEffect          = nil;
87         _reallyIgnoresEvents = YES;
88         _exitTimer           = nil;
89
90 //      if ( _backgroundType == ITTransientStatusWindowRounded ) {
91 //          _contentSubView = contentView;
92 //      } else {
93 //          [self setContentView:contentView];
94 //      }
95
96         [self setIgnoresMouseEvents:YES];
97         [self setLevel:NSScreenSaverWindowLevel];
98         [self setContentView:contentView];
99         [self rebuildWindow];
100     }
101     return self;
102 }
103
104
105 /*************************************************************************/
106 #pragma mark -
107 #pragma mark INSTANCE METHODS
108 /*************************************************************************/
109
110 - (BOOL)ignoresMouseEvents
111 {
112     return _reallyIgnoresEvents;
113 }
114
115 - (void)setIgnoresMouseEvents:(BOOL)flag
116 {
117     CGSValueObj          key;
118     CGSValueObj          ignore;
119
120     key = CGSCreateCString("IgnoreForEvents");
121     ignore = CGSCreateBoolean( (flag ? kCGSTrue : kCGSFalse) );
122     CGSSetWindowProperty([NSApp contextID], (CGSWindowID)[self windowNumber], key, ignore);
123     CGSReleaseObj(key);
124     CGSReleaseObj(ignore);
125
126     _reallyIgnoresEvents = flag;
127 }
128
129
130 /*
131
132 - (id)contentView
133 {
134     if ( _backgroundType == ITTransientStatusWindowRounded ) {
135         return _contentSubView;
136     } else {
137         return [super contentView];
138     }
139 }
140
141 - (void)setContentView:(NSView *)aView
142 {
143     if ( _backgroundType == ITTransientStatusWindowRounded ) {
144        [_contentSubView removeFromSuperview];
145         _contentSubView = aView;
146         [_contentSubView setFrame:[[super contentView] frame]];
147         [[super contentView] addSubview:_contentSubView];
148         [_contentSubView setNextResponder:self];
149     } else {
150         [super setContentView:aView];
151     }
152 }
153
154 */
155
156 - (IBAction)appear:(id)sender
157 {
158     if ( _visibilityState == ITWindowHiddenState ) {
159          // Window is hidden.  Appear as normal, and start the timer.
160         [_entryEffect performAppear];
161     } else if ( _visibilityState == ITWindowVisibleState ) {
162          // Window is completely visible.  Simply reset the timer.
163         [self startVanishTimer];
164     } else if ( _visibilityState == ITWindowAppearingState ) {
165          // Window is on its way in.  Do nothing.
166     } else if ( _visibilityState == ITWindowVanishingState ) {
167         // Window is on its way out.  Cancel the vanish.
168         [_exitEffect cancelVanish];
169     }
170 }
171
172 - (IBAction)vanish:(id)sender
173 {
174     if ( _visibilityState == ITWindowVisibleState ) {
175         // Window is totally visible.  Perform exit effect.
176         [_exitEffect performVanish];
177     } else if ( _visibilityState == ITWindowHiddenState ) {
178         // Window is hidden.  Do nothing.
179     } else if ( _visibilityState == ITWindowAppearingState ) {
180         // Window is on its way in.  Cancel appear.
181         [_entryEffect cancelAppear];
182     } else if ( _visibilityState == ITWindowVanishingState ) {
183         // Window is on its way out.  Do nothing.
184     }
185 }
186
187 - (ITWindowVisibilityState)visibilityState
188 {
189     return _visibilityState;
190 }
191
192 - (void)setVisibilityState:(ITWindowVisibilityState)newState
193 {
194     _visibilityState = newState;
195     
196     if ( _visibilityState == ITWindowVisibleState ) {
197         [self startVanishTimer];
198     }
199 }
200
201 - (ITTransientStatusWindowExitMode)exitMode
202 {
203     return _exitMode;
204 }
205
206 - (void)setExitMode:(ITTransientStatusWindowExitMode)newMode
207 {
208     _exitMode = newMode;
209     
210     if ( _visibilityState == ITWindowVisibleState ) {
211         if ( _exitMode == ITTransientStatusWindowExitOnCommand ) {
212             [self stopVanishTimer];
213         } else if ( _exitMode == ITTransientStatusWindowExitAfterDelay ) {
214             [self startVanishTimer];
215         }
216     }
217 }
218
219 - (float)exitDelay
220 {
221     return _exitDelay;
222 }
223
224 - (void)setExitDelay:(float)seconds
225 {
226     _exitDelay = seconds;
227 }
228
229 - (ITTransientStatusWindowBackgroundType)backgroundType
230 {
231 //  return _backgroundType;
232     return ITTransientStatusWindowRounded;
233 }
234
235 - (void)setBackgroundType:(ITTransientStatusWindowBackgroundType)newType
236 {
237 //  setBackgroundType: is currently ignored.  Defaults to ITTransientStatusWindowRounded.
238 //  _backgroundType = newType;
239     _backgroundType = ITTransientStatusWindowRounded;
240 }
241
242 - (ITVerticalWindowPosition)verticalPosition;
243 {
244     return _verticalPosition;
245 }
246
247 - (void)setVerticalPosition:(ITVerticalWindowPosition)newPosition;
248 {
249     _verticalPosition = newPosition;
250 }
251
252 - (ITHorizontalWindowPosition)horizontalPosition;
253 {
254     return _horizontalPosition;
255 }
256
257 - (void)setHorizontalPosition:(ITHorizontalWindowPosition)newPosition;
258 {
259     _horizontalPosition = newPosition;
260 }
261
262 - (float)effectProgress
263 {
264     return _effectProgress;
265 }
266
267 - (void)setEffectProgress:(float)newProgress
268 {
269     _effectProgress = newProgress;
270 }
271
272 - (float)screenPadding
273 {
274     return _screenPadding;
275 }
276
277 - (void)setScreenPadding:(float)newPadding
278 {
279     _screenPadding = newPadding;
280 }
281
282 - (int)screenNumber
283 {
284     return _screenNumber;
285 }
286
287 - (void)setScreenNumber:(int)newNumber
288 {
289     _screenNumber = newNumber;
290 }
291
292 - (ITWindowEffect *)entryEffect
293 {
294     return _entryEffect;
295 }
296
297 - (void)setEntryEffect:(ITWindowEffect *)newEffect
298 {
299     [_entryEffect releaseWhenIdle];
300     _entryEffect = [newEffect retain];
301 }
302
303 - (ITWindowEffect *)exitEffect
304 {
305     return _exitEffect;
306 }
307
308 - (void)setExitEffect:(ITWindowEffect *)newEffect
309 {
310     [_exitEffect releaseWhenIdle];
311     _exitEffect = [newEffect retain];
312 }
313
314
315 /*************************************************************************/
316 #pragma mark -
317 #pragma mark PRIVATE METHODS
318 /*************************************************************************/
319
320 - (void)rebuildWindow;
321 {
322     if ( _backgroundType == ITTransientStatusWindowRounded ) {
323         ITGrayRoundedView *roundedView = [[[ITGrayRoundedView alloc] initWithFrame:[self frame]] autorelease];
324
325         [self setBackgroundColor:[NSColor clearColor]];
326         [self setHasShadow:NO];
327         [self setOpaque:NO];
328         
329         [self setContentView:roundedView];
330 //      [super setContentView:roundedView];
331 //      [_contentSubView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
332 //      [self setContentView:_contentSubView];
333     } else {
334         // YUO == CLWONBAOT
335     }
336 }
337
338 - (void)startVanishTimer
339 {
340     if ( _exitMode == ITTransientStatusWindowExitAfterDelay) {
341         [self stopVanishTimer];
342         _exitTimer = [NSTimer scheduledTimerWithTimeInterval:_exitDelay
343                                                       target:self
344                                                     selector:@selector(doDelayedExit)
345                                                     userInfo:nil
346                                                      repeats:NO];
347     }
348 }
349
350 - (void)doDelayedExit
351 {
352     [self vanish:self];
353     _exitTimer = nil;
354 }
355
356 - (void)stopVanishTimer
357 {
358     if ( _exitTimer ) {
359         [_exitTimer invalidate];
360         _exitTimer = nil;
361     }
362 }
363
364 @end