2010-05-19 17:26:41 +00:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
2009-02-10 16:15:59 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
2010-03-27 01:24:50 +00:00
|
|
|
const St = imports.gi.St;
|
2009-03-11 19:21:45 +00:00
|
|
|
const Lang = imports.lang;
|
2010-09-10 02:00:28 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
2009-03-11 19:21:45 +00:00
|
|
|
const Signals = imports.signals;
|
2009-02-10 16:15:59 +00:00
|
|
|
const Tweener = imports.ui.tweener;
|
2010-05-06 21:18:10 +00:00
|
|
|
const Main = imports.ui.main;
|
2009-02-10 16:15:59 +00:00
|
|
|
|
2010-03-22 19:25:43 +00:00
|
|
|
const Params = imports.misc.params;
|
|
|
|
|
2010-03-22 20:29:13 +00:00
|
|
|
// Time to scale down to maxDragActorSize
|
|
|
|
const SCALE_ANIMATION_TIME = 0.25;
|
|
|
|
// Time to animate to original position on cancel
|
2009-02-10 16:15:59 +00:00
|
|
|
const SNAP_BACK_ANIMATION_TIME = 0.25;
|
2010-05-20 14:44:45 +00:00
|
|
|
// Time to animate to original position on success
|
|
|
|
const REVERT_ANIMATION_TIME = 0.75;
|
2009-02-10 16:15:59 +00:00
|
|
|
|
2010-06-06 14:22:27 +00:00
|
|
|
const DragMotionResult = {
|
|
|
|
NO_DROP: 0,
|
|
|
|
COPY_DROP: 1,
|
|
|
|
MOVE_DROP: 2,
|
|
|
|
CONTINUE: 3
|
|
|
|
};
|
|
|
|
|
2010-09-10 02:00:28 +00:00
|
|
|
const DRAG_CURSOR_MAP = {
|
2010-11-23 23:27:47 +00:00
|
|
|
0: Shell.Cursor.DND_UNSUPPORTED_TARGET,
|
|
|
|
1: Shell.Cursor.DND_COPY,
|
|
|
|
2: Shell.Cursor.DND_MOVE
|
2010-09-10 02:00:28 +00:00
|
|
|
};
|
|
|
|
|
2010-06-06 14:22:27 +00:00
|
|
|
const DragDropResult = {
|
|
|
|
FAILURE: 0,
|
|
|
|
SUCCESS: 1,
|
|
|
|
CONTINUE: 2
|
|
|
|
};
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
let eventHandlerActor = null;
|
|
|
|
let currentDraggable = null;
|
2010-06-06 14:22:27 +00:00
|
|
|
let dragMonitors = [];
|
2009-09-22 20:15:28 +00:00
|
|
|
|
|
|
|
function _getEventHandlerActor() {
|
|
|
|
if (!eventHandlerActor) {
|
|
|
|
eventHandlerActor = new Clutter.Rectangle();
|
|
|
|
eventHandlerActor.width = 0;
|
|
|
|
eventHandlerActor.height = 0;
|
2010-05-06 21:18:10 +00:00
|
|
|
Main.uiGroup.add_actor(eventHandlerActor);
|
2009-09-22 20:15:28 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-06-06 14:22:27 +00:00
|
|
|
function addDragMonitor(monitor) {
|
|
|
|
dragMonitors.push(monitor);
|
|
|
|
}
|
|
|
|
|
2011-06-27 16:59:56 +00:00
|
|
|
function removeDragMonitor(monitor) {
|
2010-06-06 14:22:27 +00:00
|
|
|
for (let i = 0; i < dragMonitors.length; i++)
|
|
|
|
if (dragMonitors[i] == monitor) {
|
|
|
|
dragMonitors.splice(i, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-22 19:25:43 +00:00
|
|
|
function _Draggable(actor, params) {
|
|
|
|
this._init(actor, params);
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Draggable.prototype = {
|
2010-03-22 19:25:43 +00:00
|
|
|
_init : function(actor, params) {
|
2010-03-22 20:29:13 +00:00
|
|
|
params = Params.parse(params, { manualMode: false,
|
2010-05-20 14:44:45 +00:00
|
|
|
restoreOnSuccess: false,
|
2010-03-22 20:29:13 +00:00
|
|
|
dragActorMaxSize: undefined,
|
|
|
|
dragActorOpacity: undefined });
|
2010-03-22 19:25:43 +00:00
|
|
|
|
2009-02-10 16:15:59 +00:00
|
|
|
this.actor = actor;
|
2010-03-22 19:25:43 +00:00
|
|
|
if (!params.manualMode)
|
2009-09-01 18:07:31 +00:00
|
|
|
this.actor.connect('button-press-event',
|
|
|
|
Lang.bind(this, this._onButtonPress));
|
2010-03-17 22:23:32 +00:00
|
|
|
|
|
|
|
this.actor.connect('destroy', Lang.bind(this, function() {
|
2010-10-24 01:45:54 +00:00
|
|
|
this._actorDestroyed = true;
|
2011-01-30 22:39:50 +00:00
|
|
|
// If the drag actor is destroyed and we were going to fix
|
|
|
|
// up its hover state, fix up the parent hover state instead
|
|
|
|
if (this.actor == this._firstLeaveActor)
|
|
|
|
this._firstLeaveActor = this._dragOrigParent;
|
2010-10-24 01:45:54 +00:00
|
|
|
if (this._dragInProgress)
|
|
|
|
this._cancelDrag(global.get_current_time());
|
2010-03-17 22:23:32 +00:00
|
|
|
this.disconnectAll();
|
|
|
|
}));
|
2009-09-22 20:15:28 +00:00
|
|
|
this._onEventId = null;
|
|
|
|
|
2010-05-20 14:44:45 +00:00
|
|
|
this._restoreOnSuccess = params.restoreOnSuccess;
|
2010-03-22 20:29:13 +00:00
|
|
|
this._dragActorMaxSize = params.dragActorMaxSize;
|
|
|
|
this._dragActorOpacity = params.dragActorOpacity;
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
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.
|
2010-05-20 14:44:45 +00:00
|
|
|
this._animationInProgress = false; // The drag is over and the item is in the process of animating to its original position (snapping back or reverting).
|
2010-11-19 16:09:48 +00:00
|
|
|
|
2011-01-30 22:39:50 +00:00
|
|
|
// During the drag, we eat enter/leave events so that actors don't prelight or show
|
|
|
|
// tooltips. But we remember the actors that we first left/last entered so we can
|
|
|
|
// fix up the hover state after the drag ends.
|
|
|
|
this._firstLeaveActor = null;
|
|
|
|
this._lastEnterActor = null;
|
|
|
|
|
2010-11-19 16:09:48 +00:00
|
|
|
this._eventsGrabbed = false;
|
2009-02-10 16:15:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onButtonPress : function (actor, event) {
|
2010-03-27 01:24:50 +00:00
|
|
|
if (event.get_button() != 1)
|
|
|
|
return false;
|
|
|
|
|
2009-02-10 16:15:59 +00:00
|
|
|
if (Tweener.getTweenCount(actor))
|
|
|
|
return false;
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._buttonDown = true;
|
2011-01-25 21:22:00 +00:00
|
|
|
// special case St.Button: grabbing the pointer would mess up the
|
2010-03-27 01:24:50 +00:00
|
|
|
// internal state, so we start the drag manually on hover change
|
2011-01-25 21:22:00 +00:00
|
|
|
if (this.actor instanceof St.Button)
|
2010-03-27 01:24:50 +00:00
|
|
|
this.actor.connect('notify::hover',
|
2011-01-25 21:22:00 +00:00
|
|
|
Lang.bind(this, this._onButtonHoverChanged));
|
2010-03-27 01:24:50 +00:00
|
|
|
else
|
|
|
|
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
|
|
|
|
2011-01-25 21:22:00 +00:00
|
|
|
_onButtonHoverChanged: function(button) {
|
|
|
|
if (button.hover || !button.pressed)
|
2010-03-27 01:24:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
button.fake_release();
|
|
|
|
this.startDrag(this._dragStartX, this._dragStartY,
|
|
|
|
global.get_current_time());
|
|
|
|
},
|
|
|
|
|
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();
|
2011-02-28 21:34:19 +00:00
|
|
|
if (!this._onEventId)
|
|
|
|
return;
|
2009-09-22 20:15:28 +00:00
|
|
|
this.actor.disconnect(this._onEventId);
|
|
|
|
this._onEventId = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_grabEvents: function() {
|
2010-11-19 16:09:48 +00:00
|
|
|
if (!this._eventsGrabbed) {
|
|
|
|
Clutter.grab_pointer(_getEventHandlerActor());
|
|
|
|
Clutter.grab_keyboard(_getEventHandlerActor());
|
|
|
|
this._eventsGrabbed = true;
|
|
|
|
}
|
2009-02-10 16:15:59 +00:00
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_ungrabEvents: function() {
|
2010-11-19 16:09:48 +00:00
|
|
|
if (this._eventsGrabbed) {
|
|
|
|
Clutter.ungrab_pointer();
|
|
|
|
Clutter.ungrab_keyboard();
|
|
|
|
this._eventsGrabbed = false;
|
|
|
|
}
|
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);
|
2010-05-20 14:44:45 +00:00
|
|
|
} else if (this._dragActor != null && !this._animationInProgress) {
|
2009-09-22 20:15:28 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2011-01-30 22:39:50 +00:00
|
|
|
} else if (event.type() == Clutter.EventType.LEAVE) {
|
|
|
|
if (this._firstLeaveActor == null)
|
|
|
|
this._firstLeaveActor = event.get_source();
|
|
|
|
} else if (event.type() == Clutter.EventType.ENTER) {
|
|
|
|
this._lastEnterActor = event.get_source();
|
2009-09-22 20:15:28 +00:00
|
|
|
}
|
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();
|
2010-11-23 23:27:47 +00:00
|
|
|
global.set_cursor(Shell.Cursor.DND_IN_DRAG);
|
2009-09-01 18:07:31 +00:00
|
|
|
|
2010-03-22 20:29:13 +00:00
|
|
|
this._dragX = this._dragStartX = stageX;
|
|
|
|
this._dragY = this._dragStartY = stageY;
|
2009-09-01 18:07:31 +00:00
|
|
|
|
|
|
|
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();
|
2010-11-17 10:00:35 +00:00
|
|
|
Shell.util_set_hidden_from_pick(this._dragActor, true);
|
2010-03-22 20:29:13 +00:00
|
|
|
|
|
|
|
this._dragOrigOpacity = this._dragActor.opacity;
|
|
|
|
if (this._dragActorOpacity != undefined)
|
|
|
|
this._dragActor.opacity = this._dragActorOpacity;
|
|
|
|
|
|
|
|
this._snapBackX = this._dragStartX + this._dragOffsetX;
|
|
|
|
this._snapBackY = this._dragStartY + this._dragOffsetY;
|
|
|
|
this._snapBackScale = this._dragActor.scale_x;
|
|
|
|
|
|
|
|
if (this._dragActorMaxSize != undefined) {
|
|
|
|
let [scaledWidth, scaledHeight] = this._dragActor.get_transformed_size();
|
|
|
|
let currentSize = Math.max(scaledWidth, scaledHeight);
|
|
|
|
if (currentSize > this._dragActorMaxSize) {
|
|
|
|
let scale = this._dragActorMaxSize / currentSize;
|
|
|
|
let origScale = this._dragActor.scale_x;
|
|
|
|
let origDragOffsetX = this._dragOffsetX;
|
|
|
|
let origDragOffsetY = this._dragOffsetY;
|
|
|
|
|
|
|
|
// The position of the actor changes as we scale
|
|
|
|
// around the drag position, but we can't just tween
|
|
|
|
// to the final position because that tween would
|
|
|
|
// fight with updates as the user continues dragging
|
|
|
|
// the mouse; instead we do the position computations in
|
|
|
|
// an onUpdate() function.
|
|
|
|
Tweener.addTween(this._dragActor,
|
|
|
|
{ scale_x: scale * origScale,
|
|
|
|
scale_y: scale * origScale,
|
|
|
|
time: SCALE_ANIMATION_TIME,
|
2010-05-13 19:46:04 +00:00
|
|
|
transition: 'easeOutQuad',
|
2010-03-22 20:29:13 +00:00
|
|
|
onUpdate: function() {
|
|
|
|
let currentScale = this._dragActor.scale_x / origScale;
|
|
|
|
this._dragOffsetX = currentScale * origDragOffsetX;
|
|
|
|
this._dragOffsetY = currentScale * origDragOffsetY;
|
|
|
|
this._dragActor.set_position(this._dragX + this._dragOffsetX,
|
|
|
|
this._dragY + this._dragOffsetY);
|
|
|
|
},
|
|
|
|
onUpdateScope: this });
|
|
|
|
}
|
|
|
|
}
|
2009-09-01 18:07:31 +00:00
|
|
|
},
|
|
|
|
|
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();
|
2010-03-22 20:29:13 +00:00
|
|
|
this._dragX = stageX;
|
|
|
|
this._dragY = stageY;
|
2009-09-22 20:15:28 +00:00
|
|
|
|
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);
|
2010-06-06 14:22:27 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
let target = this._dragActor.get_stage().get_actor_at_pos(Clutter.PickMode.ALL,
|
2010-03-08 22:50:49 +00:00
|
|
|
stageX, stageY);
|
2010-06-06 14:22:27 +00:00
|
|
|
|
|
|
|
// We call observers only once per motion with the innermost
|
|
|
|
// target actor. If necessary, the observer can walk the
|
|
|
|
// parent itself.
|
|
|
|
let dragEvent = {
|
|
|
|
x: stageX,
|
|
|
|
y: stageY,
|
|
|
|
dragActor: this._dragActor,
|
2010-09-10 02:00:28 +00:00
|
|
|
source: this.actor._delegate,
|
2010-06-06 14:22:27 +00:00
|
|
|
targetActor: target
|
|
|
|
};
|
|
|
|
for (let i = 0; i < dragMonitors.length; i++) {
|
|
|
|
let motionFunc = dragMonitors[i].dragMotion;
|
2010-09-10 02:00:28 +00:00
|
|
|
if (motionFunc) {
|
|
|
|
let result = motionFunc(dragEvent);
|
|
|
|
if (result != DragMotionResult.CONTINUE) {
|
|
|
|
global.set_cursor(DRAG_CURSOR_MAP[result]);
|
|
|
|
return true;
|
2010-06-06 14:22:27 +00:00
|
|
|
}
|
2010-09-10 02:00:28 +00:00
|
|
|
}
|
2010-06-06 14:22:27 +00:00
|
|
|
}
|
2009-03-11 19:21:45 +00:00
|
|
|
while (target) {
|
|
|
|
if (target._delegate && target._delegate.handleDragOver) {
|
2010-05-20 14:13:27 +00:00
|
|
|
let [r, targX, targY] = target.transform_stage_point(stageX, stageY);
|
2009-03-11 19:21:45 +00:00
|
|
|
// 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.
|
2010-09-10 02:00:28 +00:00
|
|
|
let result = target._delegate.handleDragOver(this.actor._delegate,
|
|
|
|
this._dragActor,
|
|
|
|
targX,
|
|
|
|
targY,
|
|
|
|
event.get_time());
|
|
|
|
if (result != DragMotionResult.CONTINUE) {
|
|
|
|
global.set_cursor(DRAG_CURSOR_MAP[result]);
|
|
|
|
return true;
|
|
|
|
}
|
2009-03-11 19:21:45 +00:00
|
|
|
}
|
|
|
|
target = target.get_parent();
|
2009-09-01 18:07:31 +00:00
|
|
|
}
|
2010-11-23 23:27:47 +00:00
|
|
|
global.set_cursor(Shell.Cursor.DND_IN_DRAG);
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_dragActorDropped: function(event) {
|
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);
|
2010-06-06 14:22:27 +00:00
|
|
|
|
|
|
|
// We call observers only once per motion with the innermost
|
|
|
|
// target actor. If necessary, the observer can walk the
|
|
|
|
// parent itself.
|
|
|
|
let dropEvent = {
|
|
|
|
dropActor: this._dragActor,
|
|
|
|
targetActor: target,
|
|
|
|
clutterEvent: event
|
|
|
|
};
|
|
|
|
for (let i = 0; i < dragMonitors.length; i++) {
|
|
|
|
let dropFunc = dragMonitors[i].dragDrop;
|
|
|
|
if (dropFunc)
|
|
|
|
switch (dropFunc(dropEvent)) {
|
|
|
|
case DragDropResult.FAILURE:
|
|
|
|
case DragDropResult.SUCCESS:
|
|
|
|
return true;
|
|
|
|
case DragDropResult.CONTINUE:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 16:15:59 +00:00
|
|
|
while (target) {
|
2010-01-20 19:01:37 +00:00
|
|
|
if (target._delegate && target._delegate.acceptDrop) {
|
2010-05-20 14:13:27 +00:00
|
|
|
let [r, targX, targY] = target.transform_stage_point(dropX, dropY);
|
|
|
|
if (target._delegate.acceptDrop(this.actor._delegate,
|
|
|
|
this._dragActor,
|
|
|
|
targX,
|
|
|
|
targY,
|
2009-02-10 16:15:59 +00:00
|
|
|
event.get_time())) {
|
2010-10-24 01:45:54 +00:00
|
|
|
if (this._actorDestroyed)
|
|
|
|
return true;
|
2009-02-10 16:15:59 +00:00
|
|
|
// If it accepted the drop without taking the actor,
|
2010-05-20 14:44:45 +00:00
|
|
|
// handle it ourselves.
|
|
|
|
if (this._dragActor.get_parent() == this._dragActor.get_stage()) {
|
|
|
|
if (this._restoreOnSuccess) {
|
|
|
|
this._restoreDragActor(event.get_time());
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
this._dragActor.destroy();
|
|
|
|
}
|
2009-02-10 16:15:59 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragInProgress = false;
|
2010-09-10 02:00:28 +00:00
|
|
|
global.unset_cursor();
|
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;
|
|
|
|
},
|
|
|
|
|
2010-05-20 14:44:45 +00:00
|
|
|
_getRestoreLocation: function() {
|
2010-11-19 15:18:14 +00:00
|
|
|
let x, y, scale;
|
|
|
|
|
|
|
|
if (this._dragActorSource && this._dragActorSource.visible) {
|
|
|
|
// Snap the clone back to its source
|
|
|
|
[x, y] = this._dragActorSource.get_transformed_position();
|
|
|
|
let [sourceScaledWidth, sourceScaledHeight] = this._dragActorSource.get_transformed_size();
|
|
|
|
scale = this._dragActor.width / sourceScaledWidth;
|
|
|
|
} else if (this._dragOrigParent) {
|
|
|
|
// Snap the actor back to its original position within
|
|
|
|
// its parent, adjusting for the fact that the parent
|
|
|
|
// may have been moved or scaled
|
|
|
|
let [parentX, parentY] = this._dragOrigParent.get_transformed_position();
|
|
|
|
let [parentWidth, parentHeight] = this._dragOrigParent.get_size();
|
|
|
|
let [parentScaledWidth, parentScaledHeight] = this._dragOrigParent.get_transformed_size();
|
2011-03-04 13:21:17 +00:00
|
|
|
let parentScale = 1.0;
|
|
|
|
if (parentWidth != 0)
|
|
|
|
parentScale = parentScaledWidth / parentWidth;
|
2011-02-11 17:19:12 +00:00
|
|
|
|
|
|
|
x = parentX + parentScale * this._dragOrigX;
|
|
|
|
y = parentY + parentScale * this._dragOrigY;
|
2010-11-19 15:18:14 +00:00
|
|
|
scale = this._dragOrigScale * parentScale;
|
|
|
|
} else {
|
|
|
|
// Snap back actor to its original stage position
|
|
|
|
x = this._snapBackX;
|
|
|
|
y = this._snapBackY;
|
|
|
|
scale = this._snapBackScale;
|
|
|
|
}
|
2010-05-20 14:44:45 +00:00
|
|
|
|
2010-11-19 15:18:14 +00:00
|
|
|
return [x, y, scale];
|
2010-05-20 14:44:45 +00:00
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_cancelDrag: function(eventTime) {
|
2011-03-08 13:44:47 +00:00
|
|
|
this.emit('drag-cancelled', eventTime);
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragInProgress = false;
|
2010-11-19 15:18:14 +00:00
|
|
|
let [snapBackX, snapBackY, snapBackScale] = this._getRestoreLocation();
|
2009-03-11 19:21:45 +00:00
|
|
|
|
2010-10-24 01:45:54 +00:00
|
|
|
if (this._actorDestroyed) {
|
|
|
|
global.unset_cursor();
|
2010-11-19 16:09:48 +00:00
|
|
|
if (!this._buttonDown)
|
2011-01-30 22:39:50 +00:00
|
|
|
this._dragComplete();
|
2010-10-24 01:45:54 +00:00
|
|
|
this.emit('drag-end', eventTime, false);
|
2011-01-30 23:04:56 +00:00
|
|
|
if (!this._dragOrigParent)
|
|
|
|
this._dragActor.destroy();
|
|
|
|
|
2010-10-24 01:45:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-20 14:44:45 +00:00
|
|
|
this._animationInProgress = 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,
|
2010-11-19 15:18:14 +00:00
|
|
|
scale_x: snapBackScale,
|
|
|
|
scale_y: snapBackScale,
|
2010-03-22 20:29:13 +00:00
|
|
|
opacity: this._dragOrigOpacity,
|
2009-02-10 16:15:59 +00:00
|
|
|
time: SNAP_BACK_ANIMATION_TIME,
|
2010-05-13 19:46:04 +00:00
|
|
|
transition: 'easeOutQuad',
|
2010-05-20 14:44:45 +00:00
|
|
|
onComplete: this._onAnimationComplete,
|
|
|
|
onCompleteScope: this,
|
|
|
|
onCompleteParams: [this._dragActor, eventTime]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_restoreDragActor: function(eventTime) {
|
|
|
|
this._dragInProgress = false;
|
2010-11-19 15:18:14 +00:00
|
|
|
[restoreX, restoreY, restoreScale] = this._getRestoreLocation();
|
2010-05-20 14:44:45 +00:00
|
|
|
|
|
|
|
// fade the actor back in at its original location
|
|
|
|
this._dragActor.set_position(restoreX, restoreY);
|
2010-11-19 15:18:14 +00:00
|
|
|
this._dragActor.set_scale(restoreScale, restoreScale);
|
2010-05-20 14:44:45 +00:00
|
|
|
this._dragActor.opacity = 0;
|
|
|
|
|
|
|
|
this._animationInProgress = true;
|
|
|
|
Tweener.addTween(this._dragActor,
|
|
|
|
{ opacity: this._dragOrigOpacity,
|
|
|
|
time: REVERT_ANIMATION_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
|
|
|
onComplete: this._onAnimationComplete,
|
2009-02-10 16:15:59 +00:00
|
|
|
onCompleteScope: this,
|
2009-09-22 20:15:28 +00:00
|
|
|
onCompleteParams: [this._dragActor, eventTime]
|
2009-02-10 16:15:59 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2010-05-20 14:44:45 +00:00
|
|
|
_onAnimationComplete : 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
|
|
|
}
|
2010-09-10 02:00:28 +00:00
|
|
|
global.unset_cursor();
|
2009-07-23 21:34:01 +00:00
|
|
|
this.emit('drag-end', eventTime, false);
|
2009-09-22 20:15:28 +00:00
|
|
|
|
2010-05-20 14:44:45 +00:00
|
|
|
this._animationInProgress = false;
|
2009-09-22 20:15:28 +00:00
|
|
|
if (!this._buttonDown)
|
|
|
|
this._dragComplete();
|
|
|
|
},
|
|
|
|
|
2011-01-30 22:39:50 +00:00
|
|
|
// Actor is an actor we have entered or left during the drag; call
|
|
|
|
// st_widget_sync_hover on all StWidget ancestors
|
|
|
|
_syncHover: function(actor) {
|
|
|
|
while (actor) {
|
|
|
|
let parent = actor.get_parent();
|
|
|
|
if (actor instanceof St.Widget)
|
|
|
|
actor.sync_hover();
|
|
|
|
|
|
|
|
actor = parent;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
_dragComplete: function() {
|
2011-01-30 22:39:50 +00:00
|
|
|
if (!this._actorDestroyed)
|
|
|
|
Shell.util_set_hidden_from_pick(this._dragActor, false);
|
|
|
|
|
|
|
|
this._ungrabEvents();
|
|
|
|
|
|
|
|
if (this._firstLeaveActor) {
|
|
|
|
this._syncHover(this._firstLeaveActor);
|
|
|
|
this._firstLeaveActor = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._lastEnterActor) {
|
|
|
|
this._syncHover(this._lastEnterActor);
|
|
|
|
this._lastEnterActor = null;
|
|
|
|
}
|
2010-11-17 10:00:35 +00:00
|
|
|
|
2009-09-22 20:15:28 +00:00
|
|
|
this._dragActor = undefined;
|
|
|
|
currentDraggable = null;
|
2009-02-10 16:15:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Signals.addSignalMethods(_Draggable.prototype);
|
|
|
|
|
2009-09-01 18:07:31 +00:00
|
|
|
/**
|
|
|
|
* makeDraggable:
|
|
|
|
* @actor: Source actor
|
2010-03-22 19:25:43 +00:00
|
|
|
* @params: (optional) Additional parameters
|
2009-09-01 18:07:31 +00:00
|
|
|
*
|
|
|
|
* Create an object which controls drag and drop for the given actor.
|
2010-03-22 19:25:43 +00:00
|
|
|
*
|
|
|
|
* If %manualMode is %true in @params, do not automatically start
|
|
|
|
* drag and drop on click
|
2010-03-22 20:29:13 +00:00
|
|
|
*
|
|
|
|
* If %dragActorMaxSize is present in @params, the drag actor will
|
|
|
|
* be scaled down to be no larger than that size in pixels.
|
|
|
|
*
|
|
|
|
* If %dragActorOpacity is present in @params, the drag actor will
|
|
|
|
* will be set to have that opacity during the drag.
|
|
|
|
*
|
|
|
|
* Note that when the drag actor is the source actor and the drop
|
|
|
|
* succeeds, the actor scale and opacity aren't reset; if the drop
|
|
|
|
* target wants to reuse the actor, it's up to the drop target to
|
|
|
|
* reset these values.
|
2009-09-01 18:07:31 +00:00
|
|
|
*/
|
2010-03-22 19:25:43 +00:00
|
|
|
function makeDraggable(actor, params) {
|
|
|
|
return new _Draggable(actor, params);
|
2009-03-11 19:21:45 +00:00
|
|
|
}
|