Bug 1190669 part 4 - Force stop transition animation when the cocoa window gets destroyed. r=smichaud
authorXidorn Quan <quanxunzhen@gmail.com>
Thu, 06 Aug 2015 15:37:48 +1000
changeset 288148 70a60348507ef8af0e6eee965cfc183b8ed0e2bf
parent 288147 e3e4870fd88547dcce694d0ad09debe8a9409045
child 288149 dde1881bc8513bc5c81d3d5cdf09c76563b8b743
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmichaud
bugs1190669
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1190669 part 4 - Force stop transition animation when the cocoa window gets destroyed. r=smichaud
widget/cocoa/nsCocoaWindow.h
widget/cocoa/nsCocoaWindow.mm
--- a/widget/cocoa/nsCocoaWindow.h
+++ b/widget/cocoa/nsCocoaWindow.h
@@ -291,16 +291,24 @@ public:
     virtual void PerformFullscreenTransition(FullscreenTransitionStage aStage,
                                              uint16_t aDuration,
                                              nsISupports* aData,
                                              nsIRunnable* aCallback) override;
     NS_IMETHOD MakeFullScreen(
       bool aFullScreen, nsIScreen* aTargetScreen = nullptr) override final;
     NS_IMETHOD MakeFullScreenWithNativeTransition(
       bool aFullScreen, nsIScreen* aTargetScreen = nullptr) override final;
+    NSAnimation* FullscreenTransitionAnimation() const { return mFullscreenTransitionAnimation; }
+    void ReleaseFullscreenTransitionAnimation()
+    {
+      MOZ_ASSERT(mFullscreenTransitionAnimation,
+                 "Should only be called when there is animation");
+      [mFullscreenTransitionAnimation release];
+      mFullscreenTransitionAnimation = nil;
+    }
 
     NS_IMETHOD              Resize(double aWidth, double aHeight, bool aRepaint) override;
     NS_IMETHOD              Resize(double aX, double aY, double aWidth, double aHeight, bool aRepaint) override;
     NS_IMETHOD              GetClientBounds(nsIntRect &aRect) override;
     NS_IMETHOD              GetScreenBounds(nsIntRect &aRect) override;
     void                    ReportMoveEvent();
     void                    ReportSizeEvent();
     NS_IMETHOD              SetCursor(nsCursor aCursor) override;
@@ -409,16 +417,19 @@ protected:
                      const IMENotification& aIMENotification) override;
 
   nsIWidget*           mParent;         // if we're a popup, this is our parent [WEAK]
   BaseWindow*          mWindow;         // our cocoa window [STRONG]
   WindowDelegate*      mDelegate;       // our delegate for processing window msgs [STRONG]
   nsRefPtr<nsMenuBarX> mMenuBar;
   NSWindow*            mSheetWindowParent; // if this is a sheet, this is the NSWindow it's attached to
   nsChildView*         mPopupContentView; // if this is a popup, this is its content widget
+  // if this is a toplevel window, and there is any ongoing fullscreen
+  // transition, it is the animation object.
+  NSAnimation*         mFullscreenTransitionAnimation;
   int32_t              mShadowStyle;
 
   CGFloat              mBackingScaleFactor;
 
   WindowAnimationType  mAnimationType;
 
   bool                 mWindowMadeHere; // true if we created the window, false for embedding
   bool                 mSheetNeedsShow; // if this is a sheet, are we waiting to be shown?
--- a/widget/cocoa/nsCocoaWindow.mm
+++ b/widget/cocoa/nsCocoaWindow.mm
@@ -98,16 +98,17 @@ static void RollUpPopups()
 }
 
 nsCocoaWindow::nsCocoaWindow()
 : mParent(nullptr)
 , mWindow(nil)
 , mDelegate(nil)
 , mSheetWindowParent(nil)
 , mPopupContentView(nil)
+, mFullscreenTransitionAnimation(nil)
 , mShadowStyle(NS_STYLE_WINDOW_SHADOW_DEFAULT)
 , mBackingScaleFactor(0.0)
 , mAnimationType(nsIWidget::eGenericWindowAnimation)
 , mWindowMadeHere(false)
 , mSheetNeedsShow(false)
 , mInFullScreenMode(false)
 , mInFullScreenTransition(false)
 , mModal(false)
@@ -506,16 +507,21 @@ NS_IMETHODIMP nsCocoaWindow::Destroy()
 
   // If we don't hide here we run into problems with panels, this is not ideal.
   // (Bug 891424)
   Show(false);
 
   if (mPopupContentView)
     mPopupContentView->Destroy();
 
+  if (mFullscreenTransitionAnimation) {
+    [mFullscreenTransitionAnimation stopAnimation];
+    ReleaseFullscreenTransitionAnimation();
+  }
+
   nsBaseWidget::Destroy();
   // nsBaseWidget::Destroy() calls GetParent()->RemoveChild(this). But we
   // don't implement GetParent(), so we need to do the equivalent here.
   if (mParent) {
     mParent->RemoveChild(this);
   }
   nsBaseWidget::OnDestroy();
 
@@ -1288,28 +1294,42 @@ private:
   }
 };
 
 NS_IMPL_ISUPPORTS0(FullscreenTransitionData)
 
 @interface FullscreenTransitionDelegate : NSObject <NSAnimationDelegate>
 {
 @public
+  nsCocoaWindow* mWindow;
   nsIRunnable* mCallback;
 }
 @end
 
 @implementation FullscreenTransitionDelegate
-- (void)animationDidEnd:(NSAnimation *)animation
+- (void)cleanupAndDispatch:(NSAnimation* )animation
 {
   [animation setDelegate:nil];
   [self autorelease];
   // The caller should have added ref for us.
   NS_DispatchToMainThread(already_AddRefed<nsIRunnable>(mCallback));
 }
+
+- (void)animationDidEnd:(NSAnimation *)animation
+{
+  MOZ_ASSERT(animation == mWindow->FullscreenTransitionAnimation(),
+             "Should be handling the only animation on the window");
+  mWindow->ReleaseFullscreenTransitionAnimation();
+  [self cleanupAndDispatch:animation];
+}
+
+- (void)animationDidStop:(NSAnimation *)animation
+{
+  [self cleanupAndDispatch:animation];
+}
 @end
 
 /* virtual */ bool
 nsCocoaWindow::PrepareForFullscreenTransition(nsISupports** aData)
 {
   nsCOMPtr<nsIScreen> widgetScreen = GetWidgetScreen();
   nsScreenCocoa* screen = static_cast<nsScreenCocoa*>(widgetScreen.get());
   NSScreen* cocoaScreen = screen->CocoaScreen();
@@ -1335,29 +1355,35 @@ nsCocoaWindow::PrepareForFullscreenTrans
 nsCocoaWindow::PerformFullscreenTransition(FullscreenTransitionStage aStage,
                                            uint16_t aDuration,
                                            nsISupports* aData,
                                            nsIRunnable* aCallback)
 {
   auto data = static_cast<FullscreenTransitionData*>(aData);
   FullscreenTransitionDelegate* delegate =
     [[FullscreenTransitionDelegate alloc] init];
+  delegate->mWindow = this;
   // Storing already_AddRefed directly could cause static checking fail.
   delegate->mCallback = nsCOMPtr<nsIRunnable>(aCallback).forget().take();
 
+  if (mFullscreenTransitionAnimation) {
+    [mFullscreenTransitionAnimation stopAnimation];
+    ReleaseFullscreenTransitionAnimation();
+  }
+
   NSDictionary* dict = @{
     NSViewAnimationTargetKey: data->mTransitionWindow,
     NSViewAnimationEffectKey: aStage == eBeforeFullscreenToggle ?
       NSViewAnimationFadeInEffect : NSViewAnimationFadeOutEffect
   };
-  NSAnimation* animation =
-    [[[NSViewAnimation alloc] initWithViewAnimations:@[dict]] autorelease];
-  [animation setDelegate:delegate];
-  [animation setDuration:aDuration / 1000.0];
-  [animation startAnimation];
+  mFullscreenTransitionAnimation =
+    [[NSViewAnimation alloc] initWithViewAnimations:@[dict]];
+  [mFullscreenTransitionAnimation setDelegate:delegate];
+  [mFullscreenTransitionAnimation setDuration:aDuration / 1000.0];
+  [mFullscreenTransitionAnimation startAnimation];
 }
 
 void nsCocoaWindow::EnteredFullScreen(bool aFullScreen, bool aNativeMode)
 {
   mInFullScreenTransition = false;
   bool wasInFullscreen = mInFullScreenMode;
   mInFullScreenMode = aFullScreen;
   if (aNativeMode || mInNativeFullScreenMode) {