| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
#import "ESFloater.h" |
|---|
| 8 |
|
|---|
| 9 |
#define WINDOW_FADE_FPS 24.0 |
|---|
| 10 |
#define WINDOW_FADE_STEP 0.3 |
|---|
| 11 |
#define WINDOW_FADE_SLOW_STEP 0.1 |
|---|
| 12 |
#define WINDOW_FADE_MAX 1.0 |
|---|
| 13 |
#define WINDOW_FADE_MIN 0.0 |
|---|
| 14 |
#define WINDOW_FADE_SNAP 0.05 //How close to min/max we must get before fade is finished |
|---|
| 15 |
|
|---|
| 16 |
@interface ESFloater (PRIVATE) |
|---|
| 17 |
- (id)initWithImage:(NSImage *)inImage styleMask:(unsigned int)styleMask; |
|---|
| 18 |
- (void)_setWindowOpacity:(float)opacity; |
|---|
| 19 |
@end |
|---|
| 20 |
|
|---|
| 21 |
@implementation ESFloater |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
+ (id)floaterWithImage:(NSImage *)inImage styleMask:(unsigned int)styleMask title:(NSString *) title |
|---|
| 25 |
{ |
|---|
| 26 |
return([[self alloc] initWithImage:inImage styleMask:styleMask title:title]); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
- (id)initWithImage:(NSImage *)inImage styleMask:(unsigned int)styleMask title:(NSString *) title |
|---|
| 31 |
{ |
|---|
| 32 |
NSRect frame; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
[super init]; |
|---|
| 36 |
windowIsVisible = NO; |
|---|
| 37 |
visibilityTimer = nil; |
|---|
| 38 |
maxOpacity = WINDOW_FADE_MAX; |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
frame = NSMakeRect(0, 0, [inImage size].width, [inImage size].height); |
|---|
| 42 |
panel = [[NSPanel alloc] initWithContentRect:frame |
|---|
| 43 |
styleMask:styleMask |
|---|
| 44 |
backing:NSBackingStoreBuffered |
|---|
| 45 |
defer:NO]; |
|---|
| 46 |
if( title) [panel setTitle:title]; |
|---|
| 47 |
[panel setHidesOnDeactivate:NO]; |
|---|
| 48 |
[panel setIgnoresMouseEvents:YES]; |
|---|
| 49 |
[panel setLevel:NSStatusWindowLevel]; |
|---|
| 50 |
[self _setWindowOpacity:WINDOW_FADE_MIN]; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
staticView = [[NSImageView alloc] initWithFrame:frame]; |
|---|
| 54 |
[staticView setImage:inImage]; |
|---|
| 55 |
[staticView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; |
|---|
| 56 |
[[panel contentView] addSubview:[staticView autorelease]]; |
|---|
| 57 |
|
|---|
| 58 |
return(self); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
- (void)moveFloaterToPoint:(NSPoint)inPoint |
|---|
| 63 |
{ |
|---|
| 64 |
[panel setFrameOrigin:inPoint]; |
|---|
| 65 |
[panel orderFront:nil]; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
- (void)setImage:(NSImage *)inImage |
|---|
| 70 |
{ |
|---|
| 71 |
NSRect frame = [panel frame]; |
|---|
| 72 |
frame.size = NSMakeSize([inImage size].width, [inImage size].height); |
|---|
| 73 |
[staticView setImage:inImage]; |
|---|
| 74 |
[panel setFrame:frame display:YES animate:NO]; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
- (NSImage *)image |
|---|
| 79 |
{ |
|---|
| 80 |
return [staticView image]; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
- (void)endFloater |
|---|
| 85 |
{ |
|---|
| 86 |
[self close:nil]; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
- (IBAction)close:(id)sender |
|---|
| 91 |
{ |
|---|
| 92 |
[visibilityTimer invalidate]; [visibilityTimer release]; visibilityTimer = nil; |
|---|
| 93 |
[panel orderOut:nil]; |
|---|
| 94 |
[panel release]; panel = nil; |
|---|
| 95 |
|
|---|
| 96 |
[self release]; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
- (void)setMaxOpacity:(float)inMaxOpacity |
|---|
| 101 |
{ |
|---|
| 102 |
maxOpacity = inMaxOpacity; |
|---|
| 103 |
if(windowIsVisible) [self _setWindowOpacity:maxOpacity]; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
- (void)setVisible:(BOOL)inVisible animate:(BOOL)animate |
|---|
| 109 |
{ |
|---|
| 110 |
if(inVisible != windowIsVisible){ |
|---|
| 111 |
windowIsVisible = inVisible; |
|---|
| 112 |
|
|---|
| 113 |
if(animate){ |
|---|
| 114 |
if(!visibilityTimer){ |
|---|
| 115 |
visibilityTimer = [[NSTimer scheduledTimerWithTimeInterval:(1.0/WINDOW_FADE_FPS) target:self selector:@selector(_updateWindowVisiblityTimer:) userInfo:nil repeats:YES] retain]; |
|---|
| 116 |
} |
|---|
| 117 |
}else{ |
|---|
| 118 |
[self _setWindowOpacity:(windowIsVisible ? maxOpacity : WINDOW_FADE_MIN)]; |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
- (void)_updateWindowVisiblityTimer:(NSTimer *)inTimer |
|---|
| 125 |
{ |
|---|
| 126 |
float alphaValue = [panel alphaValue]; |
|---|
| 127 |
|
|---|
| 128 |
if(windowIsVisible){ |
|---|
| 129 |
alphaValue += (maxOpacity - alphaValue) * (( [[[NSApplication sharedApplication] currentEvent] modifierFlags] & NSShiftKeyMask ) ? WINDOW_FADE_SLOW_STEP : WINDOW_FADE_STEP); |
|---|
| 130 |
if(alphaValue > maxOpacity - WINDOW_FADE_SNAP) alphaValue = maxOpacity; |
|---|
| 131 |
}else{ |
|---|
| 132 |
alphaValue -= (alphaValue - WINDOW_FADE_MIN) * (( [[[NSApplication sharedApplication] currentEvent] modifierFlags] & NSShiftKeyMask ) ? WINDOW_FADE_SLOW_STEP : WINDOW_FADE_STEP); |
|---|
| 133 |
if(alphaValue < WINDOW_FADE_MIN + WINDOW_FADE_SNAP) alphaValue = WINDOW_FADE_MIN; |
|---|
| 134 |
} |
|---|
| 135 |
[self _setWindowOpacity:alphaValue]; |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
if(alphaValue == maxOpacity || alphaValue == WINDOW_FADE_MIN){ |
|---|
| 139 |
[visibilityTimer invalidate]; [visibilityTimer release]; visibilityTimer = nil; |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
- (void)_setWindowOpacity:(float)opacity |
|---|
| 144 |
{ |
|---|
| 145 |
[panel setAlphaValue:opacity]; |
|---|
| 146 |
[panel setOpaque:(opacity == 1.0)]; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
@end |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|