2009-02-10 16:15:59 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
2009-03-11 19:21:45 +00:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
2009-02-10 16:15:59 +00:00
|
|
|
const Tweener = imports.ui.tweener;
|
|
|
|
|
|
|
|
const SNAP_BACK_ANIMATION_TIME = 0.25;
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
let eventHandlerActor = null;
|
|
|
|
let currentDraggable = null;
|
|
|
|
|
|
|
|
function _getEventHandlerActor() {
|
|
|
|
if (!eventHandlerActor) {
|
|
|
|
eventHandlerActor = new Clutter.Rectangle();
|
|
|
|
eventHandlerActor.width = 0;
|
|
|
|
eventHandlerActor.height = 0;
|
|
|
|
global.stage.add_actor(eventHandlerActor);
|
|
|
|
// We connect to 'event' rather than 'captured-event' because the capturing phase doesn't happen
|
|
|
|
// when you've grabbed the pointer.
|
|
|
|
eventHandlerActor.connect('event',
|
|
|
|
function(actor, event) {
|
|
|
|
return currentDraggable._onEvent(actor, event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return eventHandlerActor;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:07:31 +00:00
|
|
|
function _Draggable(actor, manualMode) {
|
|
|
|
this._init(actor, manualMode);
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Draggable.prototype = {
|
2009-09-01 18:07:31 +00:00
|
|
|
_init : function(actor, manualMode) {
|
2009-02-10 16:15:59 +00:00
|
|
|
this.actor = actor;
|
2009-09-01 18:07:31 +00:00
|
|
|
if (!manualMode)
|
|
|
|
this.actor.connect('button-press-event',
|
|
|
|
Lang.bind(this, this._onButtonPress));
|
2009-09-22 20:15:28 +00:00
|
|
|
this._onEventId = null;
|
|
|
|
|
|
|
|
this._buttonDown = false; // The mouse button has been pressed and has not yet been released.
|
|
|
|
this._dragInProgress = false; // The drag has been started, and has not been dropped or cancelled yet.
|
|
|
|
this._snapBackInProgress = false; // The drag has been cancelled and the item is in the process of snapping back.
|
2009-02-10 16:15:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onButtonPress : function (actor, event) {
|
|
|
|
// FIXME: we should make sure it's button 1, but we can't currently
|
|
|
|
// check that from JavaScript
|
|
|
|
if (Tweener.getTweenCount(actor))
|
|
|
|
return false;
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._buttonDown = true;
|
|
|
|
this._grabActor();
|
2009-02-10 16:15:59 +00:00
|
|
|
|
|
|
|
let [stageX, stageY] = event.get_coords();
|
|
|
|
this._dragStartX = stageX;
|
|
|
|
this._dragStartY = stageY;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
2010-03-08 22:50:49 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_grabActor: function() {
|
|
|
|
Clutter.grab_pointer(this.actor);
|
|
|
|
this._onEventId = this.actor.connect('event',
|
|
|
|
Lang.bind(this, this._onEvent));
|
|
|
|
},
|
2009-02-10 16:15:59 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_ungrabActor: function() {
|
|
|
|
Clutter.ungrab_pointer();
|
|
|
|
this.actor.disconnect(this._onEventId);
|
|
|
|
this._onEventId = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_grabEvents: function() {
|
|
|
|
Clutter.grab_pointer(_getEventHandlerActor());
|
|
|
|
Clutter.grab_keyboard(_getEventHandlerActor());
|
2009-02-10 16:15:59 +00:00
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_ungrabEvents: function() {
|
2009-02-10 16:15:59 +00:00
|
|
|
Clutter.ungrab_pointer();
|
2009-09-22 20:15:28 +00:00
|
|
|
Clutter.ungrab_keyboard();
|
2009-02-10 16:15:59 +00:00
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_onEvent: function(actor, event) {
|
|
|
|
// We intercept BUTTON_RELEASE event to know that the button was released in case we
|
|
|
|
// didn't start the drag, to drop the draggable in case the drag was in progress, and
|
|
|
|
// to complete the drag and ensure that whatever happens to be under the pointer does
|
|
|
|
// not get triggered if the drag was cancelled with Esc.
|
|
|
|
if (event.type() == Clutter.EventType.BUTTON_RELEASE) {
|
|
|
|
this._buttonDown = false;
|
|
|
|
if (this._dragInProgress) {
|
|
|
|
return this._dragActorDropped(event);
|
|
|
|
} else if (this._dragActor != null && !this._snapBackInProgress) {
|
|
|
|
// Drag must have been cancelled with Esc.
|
|
|
|
this._dragComplete();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Drag has never started.
|
|
|
|
this._ungrabActor();
|
2009-02-10 16:15:59 +00:00
|
|
|
return false;
|
2009-09-22 20:15:28 +00:00
|
|
|
}
|
|
|
|
// We intercept MOTION event to figure out if the drag has started and to draw
|
|
|
|
// this._dragActor under the pointer when dragging is in progress
|
|
|
|
} else if (event.type() == Clutter.EventType.MOTION) {
|
|
|
|
if (this._dragInProgress) {
|
|
|
|
return this._updateDragPosition(event);
|
|
|
|
} else if (this._dragActor == null) {
|
|
|
|
return this._maybeStartDrag(event);
|
|
|
|
}
|
|
|
|
// We intercept KEY_PRESS event so that we can process Esc key press to cancel
|
|
|
|
// dragging and ignore all other key presses.
|
|
|
|
} else if (event.type() == Clutter.EventType.KEY_PRESS && this._dragInProgress) {
|
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
if (symbol == Clutter.Escape) {
|
|
|
|
this._cancelDrag(event.get_time());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-02-10 16:15:59 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
return false;
|
2009-02-10 16:15:59 +00:00
|
|
|
},
|
|
|
|
|
2009-09-01 18:07:31 +00:00
|
|
|
/**
|
|
|
|
* startDrag:
|
|
|
|
* @stageX: X coordinate of event
|
|
|
|
* @stageY: Y coordinate of event
|
|
|
|
* @time: Event timestamp
|
|
|
|
*
|
|
|
|
* Directly initiate a drag and drop operation from the given actor.
|
|
|
|
* This function is useful to call if you've specified manualMode
|
|
|
|
* for the draggable.
|
|
|
|
*/
|
2009-09-22 20:15:28 +00:00
|
|
|
startDrag: function (stageX, stageY, time) {
|
|
|
|
currentDraggable = this;
|
|
|
|
this._dragInProgress = true;
|
|
|
|
|
2009-09-01 18:07:31 +00:00
|
|
|
this.emit('drag-begin', time);
|
2009-09-22 20:15:28 +00:00
|
|
|
if (this._onEventId)
|
|
|
|
this._ungrabActor();
|
|
|
|
this._grabEvents();
|
2009-09-01 18:07:31 +00:00
|
|
|
|
|
|
|
this._dragStartX = stageX;
|
|
|
|
this._dragStartY = stageY;
|
|
|
|
|
|
|
|
if (this.actor._delegate && this.actor._delegate.getDragActor) {
|
|
|
|
this._dragActor = this.actor._delegate.getDragActor(this._dragStartX, this._dragStartY);
|
|
|
|
// Drag actor does not always have to be the same as actor. For example drag actor
|
|
|
|
// can be an image that's part of the actor. So to perform "snap back" correctly we need
|
|
|
|
// to know what was the drag actor source.
|
|
|
|
if (this.actor._delegate.getDragActorSource) {
|
|
|
|
this._dragActorSource = this.actor._delegate.getDragActorSource();
|
|
|
|
// If the user dragged from the source, then position
|
|
|
|
// the dragActor over it. Otherwise, center it
|
|
|
|
// around the pointer
|
|
|
|
let [sourceX, sourceY] = this._dragActorSource.get_transformed_position();
|
|
|
|
let x, y;
|
2010-03-10 00:15:43 +00:00
|
|
|
if (stageX > sourceX && stageX <= sourceX + this._dragActor.width &&
|
|
|
|
stageY > sourceY && stageY <= sourceY + this._dragActor.height) {
|
2009-09-01 18:07:31 +00:00
|
|
|
x = sourceX;
|
|
|
|
y = sourceY;
|
|
|
|
} else {
|
|
|
|
x = stageX - this._dragActor.width / 2;
|
|
|
|
y = stageY - this._dragActor.height / 2;
|
|
|
|
}
|
|
|
|
this._dragActor.set_position(x, y);
|
|
|
|
} else {
|
|
|
|
this._dragActorSource = this.actor;
|
|
|
|
}
|
|
|
|
this._dragOrigParent = undefined;
|
|
|
|
|
2010-03-05 21:57:09 +00:00
|
|
|
this._dragOffsetX = this._dragActor.x - this._dragStartX;
|
|
|
|
this._dragOffsetY = this._dragActor.y - this._dragStartY;
|
2009-09-01 18:07:31 +00:00
|
|
|
} else {
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragActor = this.actor;
|
2009-09-01 18:07:31 +00:00
|
|
|
this._dragActorSource = undefined;
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragOrigParent = this.actor.get_parent();
|
2009-09-01 18:07:31 +00:00
|
|
|
this._dragOrigX = this._dragActor.x;
|
|
|
|
this._dragOrigY = this._dragActor.y;
|
|
|
|
this._dragOrigScale = this._dragActor.scale_x;
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
let [actorStageX, actorStageY] = this.actor.get_transformed_position();
|
2010-03-05 21:57:09 +00:00
|
|
|
this._dragOffsetX = actorStageX - this._dragStartX;
|
|
|
|
this._dragOffsetY = actorStageY - this._dragStartY;
|
2009-09-01 18:07:31 +00:00
|
|
|
|
|
|
|
// Set the actor's scale such that it will keep the same
|
|
|
|
// transformed size when it's reparented to the stage
|
2009-09-22 20:15:28 +00:00
|
|
|
let [scaledWidth, scaledHeight] = this.actor.get_transformed_size();
|
|
|
|
this.actor.set_scale(scaledWidth / this.actor.width,
|
|
|
|
scaledHeight / this.actor.height);
|
2009-09-01 18:07:31 +00:00
|
|
|
}
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragActor.reparent(this.actor.get_stage());
|
2009-09-01 18:07:31 +00:00
|
|
|
this._dragActor.raise_top();
|
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_maybeStartDrag: function(event) {
|
2009-02-10 16:15:59 +00:00
|
|
|
let [stageX, stageY] = event.get_coords();
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
// See if the user has moved the mouse enough to trigger a drag
|
2009-02-10 16:15:59 +00:00
|
|
|
let threshold = Gtk.Settings.get_default().gtk_dnd_drag_threshold;
|
2009-09-22 20:15:28 +00:00
|
|
|
if ((Math.abs(stageX - this._dragStartX) > threshold ||
|
2009-02-10 16:15:59 +00:00
|
|
|
Math.abs(stageY - this._dragStartY) > threshold)) {
|
2009-09-22 20:15:28 +00:00
|
|
|
this.startDrag(stageX, stageY, event.get_time());
|
|
|
|
this._updateDragPosition(event);
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateDragPosition : function (event) {
|
|
|
|
let [stageX, stageY] = event.get_coords();
|
|
|
|
|
2009-02-10 16:15:59 +00:00
|
|
|
// If we are dragging, update the position
|
|
|
|
if (this._dragActor) {
|
|
|
|
this._dragActor.set_position(stageX + this._dragOffsetX,
|
|
|
|
stageY + this._dragOffsetY);
|
2009-03-11 19:21:45 +00:00
|
|
|
// Because we want to find out what other actor is located at the current position of this._dragActor,
|
|
|
|
// we have to temporarily hide this._dragActor.
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragActor.hide();
|
|
|
|
let target = this._dragActor.get_stage().get_actor_at_pos(Clutter.PickMode.ALL,
|
2010-03-08 22:50:49 +00:00
|
|
|
stageX, stageY);
|
2009-03-11 19:21:45 +00:00
|
|
|
this._dragActor.show();
|
|
|
|
while (target) {
|
|
|
|
if (target._delegate && target._delegate.handleDragOver) {
|
|
|
|
let [targX, targY] = target.get_transformed_position();
|
|
|
|
// We currently loop through all parents on drag-over even if one of the children has handled it.
|
|
|
|
// We can check the return value of the function and break the loop if it's true if we don't want
|
|
|
|
// to continue checking the parents.
|
2009-09-22 20:15:28 +00:00
|
|
|
target._delegate.handleDragOver(this.actor._delegate, this._dragActor,
|
2010-03-08 22:50:49 +00:00
|
|
|
(stageX - targX) / target.scale_x,
|
|
|
|
(stageY - targY) / target.scale_y,
|
2009-03-11 19:21:45 +00:00
|
|
|
event.get_time());
|
|
|
|
}
|
|
|
|
target = target.get_parent();
|
2009-09-01 18:07:31 +00:00
|
|
|
}
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_dragActorDropped: function(event) {
|
|
|
|
// Find a drop target. Because we want to find out what other actor is located at
|
|
|
|
// the current position of this._dragActor, we have to temporarily hide this._dragActor.
|
|
|
|
this._dragActor.hide();
|
2009-02-10 16:15:59 +00:00
|
|
|
let [dropX, dropY] = event.get_coords();
|
2009-09-22 20:15:28 +00:00
|
|
|
let target = this._dragActor.get_stage().get_actor_at_pos(Clutter.PickMode.ALL,
|
|
|
|
dropX, dropY);
|
|
|
|
this._dragActor.show();
|
2009-02-10 16:15:59 +00:00
|
|
|
while (target) {
|
2010-01-20 19:01:37 +00:00
|
|
|
if (target._delegate && target._delegate.acceptDrop) {
|
2009-02-10 16:15:59 +00:00
|
|
|
let [targX, targY] = target.get_transformed_position();
|
2009-09-22 20:15:28 +00:00
|
|
|
if (target._delegate.acceptDrop(this.actor._delegate, this._dragActor,
|
2009-08-06 23:34:12 +00:00
|
|
|
(dropX - targX) / target.scale_x,
|
|
|
|
(dropY - targY) / target.scale_y,
|
2009-02-10 16:15:59 +00:00
|
|
|
event.get_time())) {
|
|
|
|
// If it accepted the drop without taking the actor,
|
|
|
|
// destroy it.
|
2009-09-22 20:15:28 +00:00
|
|
|
if (this._dragActor.get_parent() == this._dragActor.get_stage())
|
|
|
|
this._dragActor.destroy();
|
2009-02-10 16:15:59 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragInProgress = false;
|
2009-07-23 21:34:01 +00:00
|
|
|
this.emit('drag-end', event.get_time(), true);
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragComplete();
|
2009-02-10 16:15:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target = target.get_parent();
|
|
|
|
}
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._cancelDrag(event.get_time());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_cancelDrag: function(eventTime) {
|
|
|
|
this._dragInProgress = false;
|
2009-03-11 19:21:45 +00:00
|
|
|
// Snap back to the actor source if the source is still around, snap back
|
|
|
|
// to the original location if the actor itself was being dragged or the
|
|
|
|
// source is no longer around.
|
|
|
|
let snapBackX = this._dragStartX + this._dragOffsetX;
|
|
|
|
let snapBackY = this._dragStartY + this._dragOffsetY;
|
|
|
|
if (this._dragActorSource && this._dragActorSource.visible) {
|
|
|
|
[snapBackX, snapBackY] = this._dragActorSource.get_transformed_position();
|
|
|
|
}
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._snapBackInProgress = true;
|
2009-02-10 16:15:59 +00:00
|
|
|
// No target, so snap back
|
2009-09-22 20:15:28 +00:00
|
|
|
Tweener.addTween(this._dragActor,
|
2009-03-11 19:21:45 +00:00
|
|
|
{ x: snapBackX,
|
|
|
|
y: snapBackY,
|
2009-02-10 16:15:59 +00:00
|
|
|
time: SNAP_BACK_ANIMATION_TIME,
|
|
|
|
transition: "easeOutQuad",
|
|
|
|
onComplete: this._onSnapBackComplete,
|
|
|
|
onCompleteScope: this,
|
2009-09-22 20:15:28 +00:00
|
|
|
onCompleteParams: [this._dragActor, eventTime]
|
2009-02-10 16:15:59 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2009-07-23 21:34:01 +00:00
|
|
|
_onSnapBackComplete : function (dragActor, eventTime) {
|
2009-02-10 16:15:59 +00:00
|
|
|
if (this._dragOrigParent) {
|
|
|
|
dragActor.reparent(this._dragOrigParent);
|
|
|
|
dragActor.set_scale(this._dragOrigScale, this._dragOrigScale);
|
|
|
|
dragActor.set_position(this._dragOrigX, this._dragOrigY);
|
2009-07-23 21:34:01 +00:00
|
|
|
} else {
|
2009-02-10 16:15:59 +00:00
|
|
|
dragActor.destroy();
|
2009-07-23 21:34:01 +00:00
|
|
|
}
|
|
|
|
this.emit('drag-end', eventTime, false);
|
2009-09-22 20:15:28 +00:00
|
|
|
|
|
|
|
this._snapBackInProgress = false;
|
|
|
|
if (!this._buttonDown)
|
|
|
|
this._dragComplete();
|
|
|
|
},
|
|
|
|
|
|
|
|
_dragComplete: function() {
|
|
|
|
this._dragActor = undefined;
|
|
|
|
currentDraggable = null;
|
|
|
|
this._ungrabEvents();
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Signals.addSignalMethods(_Draggable.prototype);
|
|
|
|
|
2009-09-01 18:07:31 +00:00
|
|
|
/**
|
|
|
|
* makeDraggable:
|
|
|
|
* @actor: Source actor
|
|
|
|
* @manualMode: If given, do not automatically start drag and drop on click
|
|
|
|
*
|
|
|
|
* Create an object which controls drag and drop for the given actor.
|
|
|
|
*/
|
|
|
|
function makeDraggable(actor, manualMode) {
|
|
|
|
return new _Draggable(actor, manualMode);
|
2009-03-11 19:21:45 +00:00
|
|
|
}
|