[gnome-shell/wip/paging-release2: 13/23] appDisplay: Add and rework pan action response



commit e7a89e92d504bcc1339ea92a45febf81dbb10c75
Author: Carlos Soriano <carlos soriano89 gmail com>
Date:   Mon Aug 12 16:36:45 2013 +0200

    appDisplay: Add and rework pan action response
    
    Add pan action to AllView and rework it to take
    into account the velocity the user gives to the action
    
    https://bugzilla.gnome.org/show_bug.cgi?id=706081

 js/ui/appDisplay.js |   81 +++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 72 insertions(+), 9 deletions(-)
---
diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js
index adbc057..40cec31 100644
--- a/js/ui/appDisplay.js
+++ b/js/ui/appDisplay.js
@@ -45,7 +45,9 @@ const INDICATORS_ANIMATION_TIME = 0.6;
 // at once without waits
 const INDICATORS_ANIMATION_DELAY_OFFSET_PRECENTAGE = 50;
 const INDICATOR_MOVE_OFFSET = 60;
-
+// Fraction of page height the finger or mouse must reach before
+// change page
+const PAGE_SWITCH_TRESHOLD = 0.2;
 const PAGE_SWITCH_TIME = 0.3;
 
 // Recursively load a GMenuTreeDirectory; we could put this in ShellAppSystem too
@@ -287,6 +289,13 @@ const AllView = new Lang.Class({
 
         this._pagesView.connect('scroll-event', Lang.bind(this, this._onScroll));
 
+        let panAction = new Clutter.PanAction({ interpolate: false });
+        panAction.connect('pan', Lang.bind(this, this._onPan));
+        panAction.connect('gesture-cancel', Lang.bind(this, this._onPanEnd));
+        panAction.connect('gesture-end', Lang.bind(this, this._onPanEnd));
+        this._panAction = panAction;
+        this._pagesView.add_action(panAction);
+        this._panning = false;
         this._clickAction = new Clutter.ClickAction();
         this._clickAction.connect('clicked', Lang.bind(this, function() {
             if (!this._currentPopup)
@@ -345,14 +354,47 @@ const AllView = new Lang.Class({
     },
 
     viewGoToPage: function(pageNumber, animated) {
-        this._currentPage = pageNumber;
-        let time = 0;
-        if (animated)
-            time = PAGE_SWITCH_TIME;
-        Tweener.addTween(this._verticalAdjustment,
-                         { value: this._grid.getPageY(this._currentPage),
-                           time: time,
-                           transition: 'easeOutQuad' });
+        let velocity;
+        if (!this._panning)
+            velocity = 0;
+        else
+            velocity = Math.abs(this._panAction.get_velocity(0)[2]);
+        // Tween the change between pages.
+        // If velocity is not specified (i.e. scrolling with mouse wheel),
+        // use the same speed regardless of original position
+        // if velocity is specified, it's in pixels per milliseconds
+        let diffToPage = this._diffToPage(pageNumber);
+        let childBox = this._pagesView.get_allocation_box();
+        let totalHeight = childBox.y2 - childBox.y1;
+        let time;
+        // Only take the velocity into account on page changes, otherwise
+        // return smoothly to the current page using the default velocity
+        if (this._currentPage != pageNumber) {
+            let minVelocity = totalHeight / (PAGE_SWITCH_TIME * 1000);
+            velocity = Math.max(minVelocity, velocity);
+            time = (diffToPage / velocity) / 1000;
+        } else {
+            time = PAGE_SWITCH_TIME * diffToPage / totalHeight;
+        }
+        // When changing more than one page, make sure to not take
+        // longer than PAGE_SWITCH_TIME
+        time = Math.min(time, PAGE_SWITCH_TIME);
+
+        if(!animated)
+            time = 0;
+
+        if (pageNumber < this._grid.nPages() && pageNumber >= 0) {
+            this._currentPage = pageNumber;
+            Tweener.addTween(this._verticalAdjustment,
+                             { value: this._grid.getPageY(this._currentPage),
+                               time: time,
+                               transition: 'easeOutQuad' });
+        }
+    },
+
+    _diffToPage: function (pageNumber) {
+        let currentScrollPosition = this._verticalAdjustment.value;
+        return Math.abs(currentScrollPosition - this._grid.getPageY(pageNumber));
     },
 
     _onScroll: function(actor, event) {
@@ -368,6 +410,27 @@ const AllView = new Lang.Class({
         }
     },
 
+    _onPan: function(action) {
+        this._panning = true;
+        this._clickAction.release();
+        let [dist, dx, dy] = action.get_motion_delta(0);
+        let adjustment = this._verticalAdjustment;
+        adjustment.value -= (dy / this._pagesView.height) * adjustment.page_size;
+    },
+
+    _onPanEnd: function(action) {
+        let diffCurrentPage = this._diffToPage(this._currentPage);
+        if (diffCurrentPage > this._pagesView.height * PAGE_SWITCH_TRESHOLD) {
+            if (action.get_velocity(0)[2] > 0 && this._currentPage > 0)
+                this.goToPage(this._currentPage - 1, true, true);
+            else if (this._currentPage < this._grid.nPages() - 1)
+                        this.goToPage(this._currentPage + 1, true , true);
+        } else {
+            this.goToPage(this._currentPage, true, true);
+        }
+        this._panning = false;
+    },
+
     _getItemId: function(item) {
         if (item instanceof Shell.App)
             return item.get_id();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]