cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [2/3] git commit: Make view shrink to reveal in-app-menu
Date Fri, 30 May 2014 20:22:03 GMT
Make view shrink to reveal in-app-menu


Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/f1a8fab7
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/f1a8fab7
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/f1a8fab7

Branch: refs/heads/master
Commit: f1a8fab759e20d937d88ffb64c346d2cbfbf7b16
Parents: 3c31ce4
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Fri May 30 16:18:26 2014 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Fri May 30 16:18:26 2014 -0400

----------------------------------------------------------------------
 AppHarnessUI/AppHarnessUI.java | 55 +++++++++++++++++++++++++++++--------
 AppHarnessUI/AppHarnessUI.m    | 50 +++++++++++++++++++++++++++------
 www/cdvah/js/InAppMenuCtrl.js  |  2 ++
 3 files changed, 86 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/f1a8fab7/AppHarnessUI/AppHarnessUI.java
----------------------------------------------------------------------
diff --git a/AppHarnessUI/AppHarnessUI.java b/AppHarnessUI/AppHarnessUI.java
index 9e6eb82..624a159 100644
--- a/AppHarnessUI/AppHarnessUI.java
+++ b/AppHarnessUI/AppHarnessUI.java
@@ -31,13 +31,18 @@ import org.apache.cordova.PluginResult;
 import org.json.JSONException;
 
 import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
 import android.content.Context;
 import android.os.Build;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewConfiguration;
 import android.view.ViewGroup;
+import android.view.ViewPropertyAnimator;
+import android.view.animation.DecelerateInterpolator;
+import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 
 public class AppHarnessUI extends CordovaPlugin {
@@ -117,7 +122,12 @@ public class AppHarnessUI extends CordovaPlugin {
                 slaveWebView.setOverScrollMode(CordovaWebView.OVER_SCROLL_NEVER);
             }
             slaveWebView.loadUrl(url);
-            setSlaveVisible(true, null);
+            View newView = (View)slaveWebView.getParent();
+            contentView.addView(newView);
+            slaveVisible = true;
+            // Back button capturing breaks without these:
+            newView.requestFocus();
+
         }
         callbackContext.success();
     }
@@ -126,9 +136,11 @@ public class AppHarnessUI extends CordovaPlugin {
         if (slaveWebView == null) {
             Log.w(LOG_TAG, "destroy: already destroyed");
         } else {
-            setSlaveVisible(false, null);
+            contentView.removeView((View)slaveWebView.getParent());
             slaveWebView.destroy();
+            origMainView.requestFocus();
             slaveWebView = null;
+            slaveVisible = false;
             sendEvent("destroyed");
         }
         if (eventsCallback != null) {
@@ -138,6 +150,7 @@ public class AppHarnessUI extends CordovaPlugin {
         callbackContext.success();
     }
 
+    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
     private void setSlaveVisible(boolean value, CallbackContext callbackContext) {
         if (value == slaveVisible) {
             return;
@@ -146,19 +159,24 @@ public class AppHarnessUI extends CordovaPlugin {
             Log.w(LOG_TAG, "setSlaveVisible: slave not created");
         } else {
             slaveVisible = value;
-            contentView.removeAllViews();
-            View newView = value ? (View)slaveWebView.getParent() : origMainView;
-            // Back button capturing breaks without these:
-            contentView.addView(newView);
-            newView.requestFocus();
-
+            ViewPropertyAnimator anim = slaveWebView.animate();
+            // Note: Pivot is set in onSizeChanged.
+            if (value) {
+                anim.scaleX(1.0f).scaleY(1.0f);
+                ((View)slaveWebView.getParent()).requestFocus();
+            } else {
+                anim.scaleX(.25f).scaleY(.25f);
+                origMainView.requestFocus();
+            }
+            slaveWebView.stealTapEvents = !value;
+            anim.setDuration(300).setInterpolator(new DecelerateInterpolator(2.0f)).start();
         }
         if (callbackContext != null) {
             callbackContext.success();
         }
     }
 
-    private void initWebView(CordovaWebView newWebView) {
+    private void initWebView(final CordovaWebView newWebView) {
         CordovaActivity activity = (CordovaActivity)cordova.getActivity();
         if (contentView == null) {
             contentView = (ViewGroup)activity.findViewById(android.R.id.content);
@@ -167,10 +185,9 @@ public class AppHarnessUI extends CordovaPlugin {
 
         LinearLayoutSoftKeyboardDetect layoutView = new LinearLayoutSoftKeyboardDetect(activity,
contentView.getWidth(), contentView.getHeight());
         layoutView.setOrientation(LinearLayout.VERTICAL);
-//        layoutView.setBackground(origRootView.getBackground());
-        layoutView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
-                ViewGroup.LayoutParams.MATCH_PARENT, 0.0F));
 
+//        layoutView.setBackground(origRootView.getBackground());
+        layoutView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, Gravity.BOTTOM | Gravity.LEFT));
 
         if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
             newWebView.setWebViewClient(new CordovaWebViewClient(cordova, newWebView));
@@ -228,6 +245,7 @@ public class AppHarnessUI extends CordovaPlugin {
 
     private class CustomCordovaWebView extends CordovaWebView {
         TwoFingerDoubleTapGestureDetector twoFingerTapDetector;
+        boolean stealTapEvents;
 
         public CustomCordovaWebView(Context context) {
             super(context);
@@ -236,8 +254,21 @@ public class AppHarnessUI extends CordovaPlugin {
 
         @Override
         public boolean onTouchEvent(MotionEvent e) {
+            if (stealTapEvents) {
+                if (e.getAction() == MotionEvent.ACTION_UP) {
+                    sendEvent("hideMenu");
+                }
+                return true;
+            }
             twoFingerTapDetector.onTouchEvent(e);
             return super.onTouchEvent(e);
         }
+
+        @SuppressLint("NewApi")
+        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+            super.onSizeChanged(w, h, oldw, oldh);
+            // Needed for the view to stay in the bottom when rotating.
+            setPivotY(h);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/f1a8fab7/AppHarnessUI/AppHarnessUI.m
----------------------------------------------------------------------
diff --git a/AppHarnessUI/AppHarnessUI.m b/AppHarnessUI/AppHarnessUI.m
index 00bcb78..3d7dafc 100644
--- a/AppHarnessUI/AppHarnessUI.m
+++ b/AppHarnessUI/AppHarnessUI.m
@@ -26,6 +26,7 @@
 @interface AHUIViewController : CDVViewController<UIGestureRecognizerDelegate> {
 @public
     __weak AppHarnessUI* _parentPlugin;
+    UITapGestureRecognizer* _singleTapRecognizer;
 }
 @end
 
@@ -50,9 +51,20 @@
     tapRecognizer.numberOfTapsRequired = 2;
     tapRecognizer.numberOfTouchesRequired = 2;
     tapRecognizer.delegate = self;
- 
-    // Add the tap gesture recognizer to the view
     [self.view addGestureRecognizer:tapRecognizer];
+
+    // Single-tap
+    _singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
+
+}
+
+- (void)setCaptureSingleTap:(BOOL)value {
+    [self.webView setUserInteractionEnabled:!value];
+    if (value) {
+        [self.view addGestureRecognizer:_singleTapRecognizer];
+    } else {
+        [self.view removeGestureRecognizer:_singleTapRecognizer];
+    }
 }
 
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer {
@@ -60,6 +72,10 @@
     return YES;
 }
 
+- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
+    [_parentPlugin sendEvent:@"hideMenu"];
+}
+
 - (void)handleTap:(UITapGestureRecognizer *)recognizer {
     [_parentPlugin sendEvent:@"showMenu"];
 }
@@ -97,7 +113,14 @@
         _slaveCordovaViewController = [[AHUIViewController alloc] init];
         _slaveCordovaViewController.startPage = url;
         _slaveCordovaViewController->_parentPlugin = self;
-        [self setVisibleHelper:YES];
+        UIView* newView = _slaveCordovaViewController.view;
+        UIView* superView = self.viewController.view;
+        [self.viewController addChildViewController:_slaveCordovaViewController];
+        [newView layer].anchorPoint = CGPointMake(0.0f, 1.0f);
+        [newView setFrame:superView.bounds];
+        [superView addSubview:newView];
+        [_slaveCordovaViewController didMoveToParentViewController:self.viewController];
+        _slaveVisible = YES;
     } else {
         NSLog(@"AppHarnessUI.create: already exists");
     }
@@ -109,8 +132,10 @@
     if (_slaveCordovaViewController == nil) {
         NSLog(@"AppHarnessUI.destroy: already destroyed.");
     } else {
-        [self setVisibleHelper:NO];
+        [_slaveCordovaViewController removeFromParentViewController];
+        [_slaveCordovaViewController.view removeFromSuperview];
         _slaveCordovaViewController = nil;
+        _slaveVisible = NO;
         [self sendEvent:@"destroyed"];
     }
     CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
@@ -126,11 +151,18 @@
     if (_slaveCordovaViewController == nil) {
         NSLog(@"AppHarnessUI.setVisible: slave doesn't exist");
     } else {
-        UIView* newView = value ? _slaveCordovaViewController.view : self.viewController.view;
-        UIView* oldView = !value ? _slaveCordovaViewController.view : self.viewController.view;
-        UIView* superView = oldView.superview;
-        [oldView removeFromSuperview];
-        [superView addSubview:newView];
+        [UIView animateWithDuration:.3
+                              delay:0
+                            options:UIViewAnimationOptionCurveEaseOut
+                         animations:^{
+             [_slaveCordovaViewController setCaptureSingleTap:!value];
+             UIView* view = _slaveCordovaViewController.view;
+             if (value) {
+                 [view setTransform:CGAffineTransformIdentity];
+             } else {
+                 [view setTransform:CGAffineTransformMakeScale(.25, .25)];
+             }
+         } completion:nil];
     }
 }
 

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/f1a8fab7/www/cdvah/js/InAppMenuCtrl.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/InAppMenuCtrl.js b/www/cdvah/js/InAppMenuCtrl.js
index bbcab49..ee21620 100644
--- a/www/cdvah/js/InAppMenuCtrl.js
+++ b/www/cdvah/js/InAppMenuCtrl.js
@@ -51,6 +51,8 @@
         AppHarnessUI.setEventHandler(function(eventName) {
             if (eventName == 'showMenu') {
                 AppHarnessUI.setVisible(false);
+            } else if (eventName == 'hideMenu') {
+                AppHarnessUI.setVisible(true);
             } else if (eventName == 'destroyed') {
                 $window.history.back();
             } else {


Mime
View raw message