| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #import "config.h" | |
| 27 | |
| 28 #import "WebWindowAnimation.h" | |
| 29 | |
| 30 #import "FloatConversion.h" | |
| 31 #import "WebCoreSystemInterface.h" | |
| 32 #import <wtf/Assertions.h> | |
| 33 #import <wtf/MathExtras.h> | |
| 34 #import <wtf/UnusedParam.h> | |
| 35 | |
| 36 using namespace WebCore; | |
| 37 | |
| 38 static const CGFloat slowMotionFactor = 10; | |
| 39 | |
| 40 static NSTimeInterval WebWindowAnimationDurationFromDuration(NSTimeInterval dura
tion) | |
| 41 { | |
| 42 return ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) ? duration *
slowMotionFactor : duration; | |
| 43 } | |
| 44 | |
| 45 static NSRect scaledRect(NSRect _initialFrame, NSRect _finalFrame, CGFloat facto
r) | |
| 46 { | |
| 47 NSRect currentRect = _initialFrame; | |
| 48 currentRect.origin.x += (NSMinX(_finalFrame) - NSMinX(_initialFrame)) * fact
or; | |
| 49 currentRect.origin.y += (NSMinY(_finalFrame) - NSMinY(_initialFrame)) * fact
or; | |
| 50 currentRect.size.width += (NSWidth(_finalFrame) - NSWidth(_initialFrame)) *
factor; | |
| 51 currentRect.size.height += (NSHeight(_finalFrame) - NSHeight(_initialFrame))
* factor; | |
| 52 return currentRect; | |
| 53 } | |
| 54 | |
| 55 static CGFloat squaredDistance(NSPoint point1, NSPoint point2) | |
| 56 { | |
| 57 CGFloat deltaX = point1.x - point2.x; | |
| 58 CGFloat deltaY = point1.y - point2.y; | |
| 59 return deltaX * deltaX + deltaY * deltaY; | |
| 60 } | |
| 61 | |
| 62 @implementation WebWindowScaleAnimation | |
| 63 | |
| 64 - (id)init | |
| 65 { | |
| 66 self = [super init]; | |
| 67 if (!self) | |
| 68 return nil; | |
| 69 [self setAnimationBlockingMode:NSAnimationNonblockingThreaded]; | |
| 70 [self setFrameRate:60]; | |
| 71 return self; | |
| 72 } | |
| 73 | |
| 74 - (id)initWithHintedDuration:(NSTimeInterval)duration window:(NSWindow *)window
initalFrame:(NSRect)initialFrame finalFrame:(NSRect)finalFrame | |
| 75 { | |
| 76 self = [self init]; | |
| 77 if (!self) | |
| 78 return nil; | |
| 79 _hintedDuration = duration; | |
| 80 _window = window; | |
| 81 _initialFrame = initialFrame; | |
| 82 _finalFrame = finalFrame; | |
| 83 _realFrame = [window frame]; | |
| 84 return self; | |
| 85 } | |
| 86 | |
| 87 - (void) dealloc | |
| 88 { | |
| 89 [_subAnimation release]; | |
| 90 [super dealloc]; | |
| 91 } | |
| 92 | |
| 93 - (void)setDuration:(NSTimeInterval)duration | |
| 94 { | |
| 95 [super setDuration:WebWindowAnimationDurationFromDuration(duration)]; | |
| 96 } | |
| 97 | |
| 98 - (void)setWindow:(NSWindow *)window | |
| 99 { | |
| 100 _window = window; | |
| 101 } | |
| 102 | |
| 103 - (float)currentValue | |
| 104 { | |
| 105 return narrowPrecisionToFloat(0.5 - 0.5 * cos(piDouble * (1 - [self currentP
rogress]))); | |
| 106 } | |
| 107 | |
| 108 - (NSRect)currentFrame | |
| 109 { | |
| 110 return scaledRect(_finalFrame, _initialFrame, [self currentValue]); | |
| 111 } | |
| 112 | |
| 113 - (void)setCurrentProgress:(NSAnimationProgress)progress | |
| 114 { | |
| 115 if (!_window) | |
| 116 return; | |
| 117 | |
| 118 [super setCurrentProgress:progress]; | |
| 119 | |
| 120 NSRect currentRect = [self currentFrame]; | |
| 121 wkWindowSetScaledFrame(_window, currentRect, _realFrame); | |
| 122 [_subAnimation setCurrentProgress:progress]; | |
| 123 } | |
| 124 | |
| 125 - (void)setSubAnimation:(NSAnimation *)animation | |
| 126 { | |
| 127 id oldAnimation = _subAnimation; | |
| 128 _subAnimation = [animation retain]; | |
| 129 [oldAnimation release]; | |
| 130 } | |
| 131 | |
| 132 - (NSTimeInterval)additionalDurationNeededToReachFinalFrame | |
| 133 { | |
| 134 static const CGFloat maxAdditionalDuration = 1; | |
| 135 static const CGFloat speedFactor = 0.0001f; | |
| 136 | |
| 137 CGFloat maxDist = squaredDistance(_initialFrame.origin, _finalFrame.origin); | |
| 138 CGFloat dist; | |
| 139 | |
| 140 dist = squaredDistance(NSMakePoint(NSMaxX(_initialFrame), NSMinY(_initialFra
me)), NSMakePoint(NSMaxX(_finalFrame), NSMinY(_finalFrame))); | |
| 141 if (dist > maxDist) | |
| 142 maxDist = dist; | |
| 143 | |
| 144 dist = squaredDistance(NSMakePoint(NSMaxX(_initialFrame), NSMaxY(_initialFra
me)), NSMakePoint(NSMaxX(_finalFrame), NSMaxY(_finalFrame))); | |
| 145 if (dist > maxDist) | |
| 146 maxDist = dist; | |
| 147 | |
| 148 dist = squaredDistance(NSMakePoint(NSMinX(_initialFrame), NSMinY(_initialFra
me)), NSMakePoint(NSMinX(_finalFrame), NSMinY(_finalFrame))); | |
| 149 if (dist > maxDist) | |
| 150 maxDist = dist; | |
| 151 | |
| 152 return MIN(sqrt(maxDist) * speedFactor, maxAdditionalDuration); | |
| 153 } | |
| 154 | |
| 155 - (void)startAnimation | |
| 156 { | |
| 157 // Compute extra time | |
| 158 if (_hintedDuration) | |
| 159 [self setDuration:_hintedDuration + [self additionalDurationNeededToReac
hFinalFrame]]; | |
| 160 [super startAnimation]; | |
| 161 } | |
| 162 | |
| 163 - (void)stopAnimation | |
| 164 { | |
| 165 _window = nil; | |
| 166 [super stopAnimation]; | |
| 167 [_subAnimation stopAnimation]; | |
| 168 } | |
| 169 | |
| 170 @end | |
| 171 | |
| 172 @implementation WebWindowFadeAnimation | |
| 173 | |
| 174 - (id)init | |
| 175 { | |
| 176 self = [super init]; | |
| 177 if (!self) | |
| 178 return nil; | |
| 179 [self setAnimationBlockingMode:NSAnimationNonblockingThreaded]; | |
| 180 [self setFrameRate:60]; | |
| 181 [self setAnimationCurve:NSAnimationEaseInOut]; | |
| 182 return self; | |
| 183 } | |
| 184 | |
| 185 - (id)initWithDuration:(NSTimeInterval)duration window:(NSWindow *)window initia
lAlpha:(CGFloat)initialAlpha finalAlpha:(CGFloat)finalAlpha | |
| 186 { | |
| 187 self = [self init]; | |
| 188 if (!self) | |
| 189 return nil; | |
| 190 _window = window; | |
| 191 _initialAlpha = initialAlpha; | |
| 192 _finalAlpha = finalAlpha; | |
| 193 [self setDuration:duration]; | |
| 194 return self; | |
| 195 } | |
| 196 | |
| 197 - (void)setDuration:(NSTimeInterval)duration | |
| 198 { | |
| 199 [super setDuration:WebWindowAnimationDurationFromDuration(duration)]; | |
| 200 } | |
| 201 | |
| 202 - (CGFloat)currentAlpha | |
| 203 { | |
| 204 return MAX(0, MIN(1, _initialAlpha + [self currentValue] * (_finalAlpha - _i
nitialAlpha))); | |
| 205 } | |
| 206 | |
| 207 - (void)setCurrentProgress:(NSAnimationProgress)progress | |
| 208 { | |
| 209 if (_isStopped) | |
| 210 return; | |
| 211 | |
| 212 ASSERT(_window); | |
| 213 [super setCurrentProgress:progress]; | |
| 214 | |
| 215 wkWindowSetAlpha(_window, [self currentAlpha]); | |
| 216 } | |
| 217 | |
| 218 - (void)setWindow:(NSWindow*)window | |
| 219 { | |
| 220 _window = window; | |
| 221 } | |
| 222 | |
| 223 - (void)stopAnimation | |
| 224 { | |
| 225 // This is relevant when we are a sub animation of a scale animation. | |
| 226 // In this case we are hosted in the animated thread of the parent | |
| 227 // and even after [super stopAnimation], the parent might call | |
| 228 // setCurrrentProgress. | |
| 229 _isStopped = YES; | |
| 230 | |
| 231 [super stopAnimation]; | |
| 232 } | |
| 233 | |
| 234 @end | |
| 235 | |
| OLD | NEW |