Fixed a crash when Core Image was available.
[ITKit.git] / ITSplashScreen.m
1 #import "ITSplashScreen.h"
2 #import "ITSplashWindow.h"
3 #import "ITSplashView.h"
4
5 static ITSplashScreen *_sharedScreen;
6
7 @implementation ITSplashScreen
8
9 + (ITSplashScreen *)sharedController {
10         if (!_sharedScreen) {
11                 _sharedScreen = [[ITSplashScreen alloc] init];
12         }
13         return _sharedScreen;
14 }
15
16 - (id)init {
17         if ((self = [super init])) {
18                 _window = [[ITSplashWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200) styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
19                 _view = [[ITSplashView alloc] initWithFrame:NSMakeRect(0, 0, 200, 200)];
20                 [_window setLevel:NSStatusWindowLevel];
21                 [_window setContentView:[_view autorelease]];
22         }
23         return self;
24 }
25
26 - (void)dealloc {
27         [_window release];
28         [super dealloc];
29 }
30
31 - (double)progressValue {
32         return ([[_view progressIndicator] doubleValue] / 100.0);
33 }
34
35 - (void)setProgressValue:(double)progress {
36         if (progress >= 1.0) {
37                 [[_view progressIndicator] setIndeterminate:YES];
38         } else {
39                 [[_view progressIndicator] setDoubleValue:(progress * 100.0)];
40         }
41 }
42
43 - (NSImage *)image {
44         return [_view image];
45 }
46
47 - (void)setImage:(NSImage *)image {
48         NSRect rect = NSZeroRect, newRect = [_window frame];
49         rect.size = [image size];
50         newRect.size = rect.size;
51         [_window setFrame:newRect display:NO];
52         newRect.origin.x = 0;
53         newRect.origin.y = 0;
54         [_view setFrame:newRect];
55         [_view setImage:image];
56         [_window center];
57 }
58
59 - (NSString *)string {
60         return [_view string];
61 }
62
63 - (void)setString:(NSString *)string {
64         [_view setString:string];
65 }
66
67 - (void)setSettingsPath:(NSString *)path {
68         [_view loadControlsFromPath:path];
69 }
70
71 - (void)showSplashWindow {
72         //[_window setAlphaValue:0.0];
73         [_window makeKeyAndOrderFront:nil];
74         //_fadeTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 30.0) target:self selector:@selector(showStep:) userInfo:nil repeats:YES];
75 }
76
77 - (void)showStep:(NSTimer *)timer {
78         [_window setAlphaValue:([_window alphaValue] + 0.05)];
79         if ([_window alphaValue] >= 1.0) {
80                 [timer invalidate];
81                 _fadeTimer = nil;
82         }
83 }
84
85 - (void)closeSplashWindow {
86         if (_fadeTimer) {
87                 [_fadeTimer invalidate];
88         }
89         _fadeTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 30.0) target:self selector:@selector(closeStep:) userInfo:nil repeats:YES];
90 }
91
92 - (void)closeStep:(NSTimer *)timer {
93         [_window setAlphaValue:([_window alphaValue] - 0.05)];
94         if ([_window alphaValue] <= 0.0) {
95                 [timer invalidate];
96                 _fadeTimer = nil;
97                 [_window orderOut:nil];
98                 [_view stopAnimation];
99         }
100 }
101
102 @end