View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport;
35  
36  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryCallback;
37  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper;
38  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.ShellState;
39  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.TransitionDelegate.BaseTransitionDelegate;
40  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.FadeAnimation;
41  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.ZoomAnimation;
42  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.widget.AppsViewportWidget;
43  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.widget.ViewportWidget;
44  
45  import java.util.logging.Logger;
46  
47  import com.google.gwt.dom.client.Style;
48  import com.google.gwt.dom.client.Style.Visibility;
49  import com.google.gwt.user.client.Element;
50  import com.google.gwt.user.client.ui.Widget;
51  import com.vaadin.client.Util;
52  
53  /**
54   * The AppsTransitionDelegate provides custom transition logic when launching, closing an app, or
55   * switching between apps.
56   */
57  public class AppsTransitionDelegate extends BaseTransitionDelegate {
58  
59      private static final Logger log = Logger.getLogger(AppsTransitionDelegate.class.getName());
60  
61      private static final double CURTAIN_ALPHA = 0.9;
62  
63      private static final int CURTAIN_FADE_IN_DURATION = 500;
64  
65      private static final int CURTAIN_FADE_OUT_DURATION = 800;
66  
67      private static final int CURTAIN_FADE_OUT_DELAY = 200;
68  
69      private static final int ZOOM_DURATION = 500;
70  
71      private AppsViewportWidget viewport;
72  
73      /**
74       * Since we reveal the already loaded apps instantly - we could use a lock
75       * that ensures the zooming animation is distracted with Vaadin layout.
76       */
77      private Object lock = new Object();
78  
79      private ZoomAnimation zoomOutAnimation = new ZoomAnimation(false) {
80          @Override
81          protected void onComplete() {
82              super.onComplete();
83              viewport.removeChildNoTransition(Util.<Widget>findWidget((Element) getElement(), null));
84          }
85      };
86  
87      private ZoomAnimation zoomInAnimation = new ZoomAnimation(true) {
88          @Override
89          protected void onStart() {
90              super.onStart();
91              setCurtainAttached(false);
92              Util.findConnectorFor(viewport).getConnection().suspendReponseHandling(lock);
93          }
94  
95          @Override
96          protected void onComplete() {
97              super.onComplete();
98              ShellState.get().setAppStarted();
99              log.warning("Switching to 'APP STARTED' state after zoom-in animation");
100             Util.findConnectorFor(viewport).getConnection().resumeResponseHandling(lock);
101         }
102     };
103 
104     private FadeAnimation curtainFadeOutAnimation = new FadeAnimation(0, true);
105     private FadeAnimation curtainFadeInAnimation = new FadeAnimation(CURTAIN_ALPHA, true) {
106         @Override
107         protected void onStart() {
108             super.onStart();
109             getJQueryWrapper().get(0).getStyle().setOpacity(0d);
110         }
111     };
112 
113     public AppsTransitionDelegate(final AppsViewportWidget viewport) {
114         this.viewport = viewport;
115         curtainFadeOutAnimation.addCallback(new JQueryCallback() {
116             @Override
117             public void execute(JQueryWrapper jq) {
118                 setCurtainAttached(false);
119             }
120         });
121     }
122 
123     /**
124      * Zoom-in if switching to a different running app, from apps-launcher only
125      * closing an app doesn't zoom-in the next app, running apps are all hidden explicitly except current one.
126      */
127     @Override
128     public void setVisibleChild(final ViewportWidget viewport, final Widget app) {
129         if (!((AppsViewportWidget)viewport).isAppClosing() && isWidgetVisibilityHidden(app)) {
130             viewport.showChildNoTransition(app);
131             zoomInAnimation.run(ZOOM_DURATION, app.getElement());
132         } else {
133             viewport.showChildNoTransition(app);
134             ShellState.get().setAppStarted();
135             log.warning("Switching to 'APP STARTED' state without app transition");
136         }
137     }
138 
139     public void setCurtainVisible(boolean visible) {
140         final Element curtain = viewport.getCurtain();
141         if (visible) {
142             setCurtainAttached(true);
143             curtainFadeOutAnimation.cancel();
144             curtainFadeInAnimation.run(CURTAIN_FADE_IN_DURATION, curtain);
145         } else {
146             curtainFadeInAnimation.cancel();
147             curtainFadeOutAnimation.run(CURTAIN_FADE_OUT_DURATION + CURTAIN_FADE_OUT_DELAY, curtain);
148         }
149     }
150 
151     public void removeWidget(Widget w) {
152         zoomOutAnimation.run(ZOOM_DURATION, w.getElement());
153     }
154 
155     private boolean isWidgetVisibilityHidden(final Widget app) {
156         return Visibility.HIDDEN.getCssName().equals(app.getElement().getStyle().getVisibility()) ||
157                 Style.Display.NONE.getCssName().equals(app.getElement().getStyle().getDisplay());
158     }
159 
160     public void setCurtainAttached(boolean visible) {
161         Element viewportElement = viewport.getElement();
162         Element curtain = viewport.getCurtain();
163         if (visible) {
164             viewportElement.appendChild(curtain);
165         } else if (viewportElement.isOrHasChild(curtain)) {
166             viewportElement.removeChild(curtain);
167         }
168     }
169 
170     @Override
171     public boolean inProgress() {
172         return zoomInAnimation.isRunning() || zoomOutAnimation.isRunning();
173     }
174 }