[the-board] [ui] Use Clutter's click/drag actions instead of custom code



commit 85b4b7b789d9767763c2bfe4ea67b6c45c968ace
Author: Lucas Rocha <lucasr gnome org>
Date:   Thu Nov 11 23:14:26 2010 +0000

    [ui] Use Clutter's click/drag actions instead of custom code

 src/js/ui/thing.js |  182 ++++++++++++++++++----------------------------------
 1 files changed, 62 insertions(+), 120 deletions(-)
---
diff --git a/src/js/ui/thing.js b/src/js/ui/thing.js
index 5d88476..cd4f59e 100644
--- a/src/js/ui/thing.js
+++ b/src/js/ui/thing.js
@@ -5,6 +5,7 @@ const Tweener = imports.tweener.tweener;
 
 // gi imports
 const Clutter = imports.gi.Clutter;
+const GObject = imports.gi.GObject;
 const Mx = imports.gi.Mx;
 const Tb = imports.gi.Tb;
 
@@ -63,14 +64,7 @@ Thing.prototype = {
             this._canResize = true;
         }
 
-        this._parentActorButtonReleaseEventId =
-            this._parentActor.connect("button-release-event",
-                                      Lang.bind(this,
-                                                this._onParentActorButtonReleaseEvent));
-
-        this._dragging = false;
-        this._dragType = DragType.NONE;
-        this._activateOnRelease = false;
+        this._activateOnClick = true;
         this._content = null;
 
         this._createMainBox();
@@ -89,11 +83,26 @@ Thing.prototype = {
                          reactive: true,
                          name: "thing-main-box" });
 
-        this._mainBox.connect("button-press-event",
-                              Lang.bind(this, this._onMainBoxButtonPressEvent));
+        let clickAction = new Clutter.ClickAction();
+
+        clickAction.connect("clicked",
+                            Lang.bind(this, this._onMainBoxClicked));
+
+        this._mainBox.add_action(clickAction);
+
+        let dragAction = new Clutter.DragAction();
 
-        this._mainBox.connect("button-release-event",
-                              Lang.bind(this, this._onMainBoxButtonReleaseEvent));
+        dragAction.set_drag_threshold(_DRAGGING_THRESHOLD,
+                                      _DRAGGING_THRESHOLD);
+
+        dragAction.connect("drag-motion",
+                           Lang.bind(this, this._onMainBoxDragMotion));
+        dragAction.connect("drag-begin",
+                           Lang.bind(this, this._onMainBoxDragBegin));
+        dragAction.connect("drag-end",
+                           Lang.bind(this, this._onMainBoxDragEnd));
+
+        this._mainBox.add_action(dragAction);
 
         this._mainBox.connect("enter-event",
                               Lang.bind(this, this._onMainBoxEnterEvent));
@@ -133,13 +142,22 @@ Thing.prototype = {
         this._resizeButton =
             new Mx.Button({ name: "thing-resize-button" });
 
+        let dragAction = new Clutter.DragAction();
+
+        dragAction.set_drag_threshold(_DRAGGING_THRESHOLD,
+                                      _DRAGGING_THRESHOLD);
+
+        dragAction.connect("drag-motion",
+                           Lang.bind(this, this._onResizeButtonDragMotion));
+        dragAction.connect("drag-begin",
+                           Lang.bind(this, this._onResizeButtonDragBegin));
+
+        this._resizeButton.add_action(dragAction);
+
         this._resizeButton.opacity = 0;
         this._resizeButton.anchorX = -_SHOW_CONTROLS_LENGTH;
         this._resizeButton.anchorY = -_SHOW_CONTROLS_LENGTH;
 
-        this._resizeButton.connect("button-press-event",
-                                   Lang.bind(this, this._onResizeButtonPressEvent));
-
         this._mainBox.append(this._resizeButton,
                              Tb.BoxPackFlags.FIXED);
 
@@ -148,41 +166,6 @@ Thing.prototype = {
                                             Tb.BoxAlignment.END);
     },
 
-    _startDragging : function(dragType) {
-        this._dragging = true;
-        this._dragType = dragType;
-
-        Clutter.grab_pointer(this._parentActor);
-
-        this._parentActorMotionEventId =
-            this._parentActor.connect("motion-event",
-                                      Lang.bind(this,
-                                                this._onParentActorMotionEvent));
-
-        this.emit("dragging-start");
-    },
-
-    _stopDragging : function() {
-        this._dragging = false;
-        this._dragType = DragType.NONE;
-
-        Clutter.ungrab_pointer();
-
-        if (this._parentActorMotionEventId) {
-            this._parentActor.disconnect(this._parentActorMotionEventId);
-            delete this._parentActorMotionEventId;
-        }
-
-        this.emit("dragging-stop");
-        this.emit('save');
-    },
-
-    _maybeActivateOnRelease : function() {
-        if (this._activateOnRelease) {
-            this.emit("activate");
-        }
-    },
-
     _updatePosition : function(newX, newY, fromState) {
         if (!fromState) {
             newX = MathUtil.clamp(newX,
@@ -196,11 +179,6 @@ Thing.prototype = {
                                   this._mainBox.height);
         }
 
-        if (Math.abs(this._mainBox.x - newX) >= _DRAGGING_THRESHOLD ||
-            Math.abs(this._mainBox.y - newY) >= _DRAGGING_THRESHOLD) {
-            this._activateOnRelease = false;
-        }
-
         this._mainBox.x = newX;
         this._mainBox.y = newY;
     },
@@ -243,30 +221,33 @@ Thing.prototype = {
         }
     },
 
-    _onMainBoxButtonPressEvent : function(o, event) {
-        let [transformedX, transformedY] =
-            this._parentActor.get_transformed_position();
+    _onMainBoxClicked : function(action) {
+        if (this._activateOnClick) {
+            this.emit("activate");
+        }
 
-        let [eventX, eventY] = event.get_coords();
+        this._activateOnClick = true;
+    },
 
-        this._pointerXInBox = eventX - transformedX -
-                              this._mainBox.x;
+    _onMainBoxDragMotion : function(action, actor, deltaX, deltaY) {
+        Tb.signal_stop_emission_by_name(action, "drag-motion");
 
-        this._pointerYInBox = eventY - transformedY -
-                              this._mainBox.y;
+        let newX = this._mainBox.x + deltaX;
+        let newY = this._mainBox.y + deltaY;
 
-        this._activateOnRelease = true;
+        this._updatePosition(newX, newY,
+                             false /* not from state */);
+    },
 
-        this._startDragging(DragType.MOVE);
+    _onMainBoxDragBegin : function(action, actor, eventX, eventY, modifiers) {
+        action.dragHandle = actor;
+        this._activateOnClick = false;
 
-        return true;
+        this.emit("dragging-start");
     },
 
-    _onMainBoxButtonReleaseEvent : function() {
-        this._stopDragging();
-        this._maybeActivateOnRelease();
-
-        return true;
+    _onMainBoxDragEnd : function(action, actor, eventX, eventY, modifiers) {
+        this.emit("dragging-end");
     },
 
     _onMainBoxEnterEvent : function() {
@@ -277,61 +258,22 @@ Thing.prototype = {
         this.leave();
     },
 
-    _onParentActorButtonReleaseEvent : function(o, event) {
-        if (!this._dragging) {
-            return false;
-        }
-
-        this._stopDragging();
-        this._maybeActivateOnRelease();
-
-        return true;
+    _onRemoveButtonClicked : function() {
+        this.emit("remove");
     },
 
-    _onParentActorMotionEvent : function(o, event) {
-        if (!this._dragging) {
-            return;
-        }
+    _onResizeButtonDragMotion : function(action, actor, deltaX, deltaY) {
+        Tb.signal_stop_emission_by_name(action, "drag-motion");
 
-        let [transformedX, transformedY] =
-            this._parentActor.get_transformed_position();
+        let newWidth = this._mainBox.width + deltaX;
+        let newHeight = this._mainBox.height + deltaY;
 
-        let [eventX, eventY] = event.get_coords();
-
-        switch(this._dragType) {
-        case DragType.MOVE:
-            let newX = eventX - transformedX -
-                       this._pointerXInBox;
-
-            let newY = eventY - transformedY -
-                       this._pointerYInBox;
-
-            this._updatePosition(newX, newY,
-                                 false /* not from state */);
-            break;
-
-        case DragType.RESIZE:
-            let x = eventX - transformedX;
-            let y = eventY - transformedY;
-
-            let newWidth = x - this._mainBox.x;
-            let newHeight = y - this._mainBox.y;
-
-            this._updateSize(newWidth, newHeight,
-                             false /* not from state */);
-            break;
-
-        default:
-            break;
-        }
-    },
-
-    _onRemoveButtonClicked : function() {
-        this.emit("remove");
+        this._updateSize(newWidth, newHeight,
+                         false /* not from state */);
     },
 
-    _onResizeButtonPressEvent : function() {
-        this._startDragging(DragType.RESIZE);
+    _onResizeButtonDragBegin : function(action, actor, eventX, eventY, modifiers) {
+        action.dragHandle = actor;
     },
 
     enter : function() {



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