[Adium-commits] adium 2040:163ec6690518: Replace a fade timer with an NSViewAnim...

adium-commits at adiumx.com adium-commits at adiumx.com
Wed Apr 29 10:31:09 UTC 2009


details:	http://hg.adiumx.com/adium/rev/163ec6690518
revision:	2040:163ec6690518
author:		Peter Hosey
date:		Wed Apr 29 03:30:42 2009 -0700

Replace a fade timer with an NSViewAnimation. Also switched from `+[NSEvent(AIEventAdditions) shiftKey]` to `-[NSEvent(AIEventAdditions) shiftKey]`.

These changes together should make the fade much more efficient: The previous implementation read the keyboards on every frame, whereas this checks the current event (instead of the keyboards) only once, before starting the fade. That's on top of not reinventing the wheel.

diffstat:

 Frameworks/AIUtilities Framework/Source/AIFloater.h |   2 +-
 Frameworks/AIUtilities Framework/Source/AIFloater.m |  41 ++++++++------------
 2 files changed, 17 insertions(+), 26 deletions(-)

diffs (87 lines):

diff -r 28244ecc5cca -r 163ec6690518 Frameworks/AIUtilities Framework/Source/AIFloater.h
--- a/Frameworks/AIUtilities Framework/Source/AIFloater.h	Wed Apr 29 01:54:58 2009 -0700
+++ b/Frameworks/AIUtilities Framework/Source/AIFloater.h	Wed Apr 29 03:30:42 2009 -0700
@@ -18,7 +18,7 @@
     NSImageView			*staticView;
     NSPanel				*panel;
     BOOL                windowIsVisible;
-    NSTimer             *visibilityTimer;
+    NSViewAnimation     *fadeAnimation;
     float               maxOpacity;
 }
 
diff -r 28244ecc5cca -r 163ec6690518 Frameworks/AIUtilities Framework/Source/AIFloater.m
--- a/Frameworks/AIUtilities Framework/Source/AIFloater.m	Wed Apr 29 01:54:58 2009 -0700
+++ b/Frameworks/AIUtilities Framework/Source/AIFloater.m	Wed Apr 29 03:30:42 2009 -0700
@@ -33,7 +33,7 @@
 	if ((self = [super init])) {
 		NSRect  frame;
 		windowIsVisible = NO;
-		visibilityTimer = nil;
+		fadeAnimation = nil;
 		maxOpacity = WINDOW_FADE_MAX;
 
 		//Set up the panel
@@ -89,7 +89,7 @@
 //
 - (IBAction)close:(id)sender
 {
-    [visibilityTimer invalidate]; [visibilityTimer release]; visibilityTimer = nil;
+    [fadeAnimation stopAnimation]; [fadeAnimation release]; fadeAnimation = nil;
     [panel orderOut:nil];
     [panel release]; panel = nil;
 
@@ -111,8 +111,20 @@
         windowIsVisible = inVisible;
         
         if (animate) {
-            if (!visibilityTimer) {
-                visibilityTimer = [[NSTimer scheduledTimerWithTimeInterval:(1.0/WINDOW_FADE_FPS) target:self selector:@selector(_updateWindowVisiblityTimer:) userInfo:nil repeats:YES] retain];
+            if (!fadeAnimation) {
+				NSDictionary *animDict = [NSDictionary dictionaryWithObjectsAndKeys:
+					panel, NSViewAnimationTargetKey,
+					inVisible ? NSViewAnimationFadeInEffect : NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey,
+					nil];
+                fadeAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:animDict]];
+
+				//1.0 / FPS = duration per step
+				//1.0 / step = number of steps (e.g.: If step = 0.1, 1.0 / step = 10 steps)
+				//duration per step * number of steps = total duration
+				NSTimeInterval step = [[NSApp currentEvent] shiftKey] ? WINDOW_FADE_SLOW_STEP : WINDOW_FADE_STEP;
+				fadeAnimation.duration = (1.0 / step) * (1.0 / WINDOW_FADE_FPS);
+
+				[fadeAnimation startAnimation];
             }
         } else {
             [self _setWindowOpacity:(windowIsVisible ? maxOpacity : WINDOW_FADE_MIN)];
@@ -120,30 +132,9 @@
     }
 }
 
-//Smoothly 
-- (void)_updateWindowVisiblityTimer:(NSTimer *)inTimer
-{
-    float   alphaValue = [panel alphaValue];
-    
-    if (windowIsVisible) {
-        alphaValue += (maxOpacity - alphaValue) * ([NSEvent shiftKey] ? WINDOW_FADE_SLOW_STEP : WINDOW_FADE_STEP);
-        if (alphaValue > maxOpacity - WINDOW_FADE_SNAP) alphaValue = maxOpacity;
-    } else {
-        alphaValue -= (alphaValue - WINDOW_FADE_MIN) * ([NSEvent shiftKey] ? WINDOW_FADE_SLOW_STEP : WINDOW_FADE_STEP);
-        if (alphaValue < WINDOW_FADE_MIN + WINDOW_FADE_SNAP) alphaValue = WINDOW_FADE_MIN;
-    }
-    [self _setWindowOpacity:alphaValue];
-    
-    //
-    if (alphaValue == maxOpacity || alphaValue == WINDOW_FADE_MIN) {
-        [visibilityTimer invalidate]; [visibilityTimer release]; visibilityTimer = nil;
-    }
-}
-
 - (void)_setWindowOpacity:(float)opacity
 {
     [panel setAlphaValue:opacity];
-    [panel setOpaque:(opacity == 1.0)];
 }
 
 




More information about the commits mailing list