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