2013-02-16 17:41:11 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2012-02-28 17:16:12 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Meta = imports.gi.Meta;
|
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Params = imports.misc.params;
|
|
|
|
|
2013-08-17 00:11:36 +00:00
|
|
|
let _capturedEventId = 0;
|
|
|
|
let _grabHelperStack = [];
|
|
|
|
function _onCapturedEvent(actor, event) {
|
|
|
|
let grabHelper = _grabHelperStack[_grabHelperStack.length - 1];
|
|
|
|
return grabHelper.onCapturedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _pushGrabHelper(grabHelper) {
|
|
|
|
_grabHelperStack.push(grabHelper);
|
|
|
|
|
|
|
|
if (_capturedEventId == 0)
|
|
|
|
_capturedEventId = global.stage.connect('captured-event', _onCapturedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _popGrabHelper(grabHelper) {
|
|
|
|
let poppedHelper = _grabHelperStack.pop();
|
|
|
|
if (poppedHelper != grabHelper)
|
|
|
|
throw new Error("incorrect grab helper pop");
|
|
|
|
|
|
|
|
if (_grabHelperStack.length == 0) {
|
|
|
|
global.stage.disconnect(_capturedEventId);
|
|
|
|
_capturedEventId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-28 17:16:12 +00:00
|
|
|
// GrabHelper:
|
|
|
|
// @owner: the actor that owns the GrabHelper
|
2012-08-20 20:05:00 +00:00
|
|
|
// @params: optional parameters to pass to Main.pushModal()
|
2012-02-28 17:16:12 +00:00
|
|
|
//
|
|
|
|
// Creates a new GrabHelper object, for dealing with keyboard and pointer grabs
|
|
|
|
// associated with a set of actors.
|
|
|
|
//
|
|
|
|
// Note that the grab can be automatically dropped at any time by the user, and
|
|
|
|
// your code just needs to deal with it; you shouldn't adjust behavior directly
|
|
|
|
// after you call ungrab(), but instead pass an 'onUngrab' callback when you
|
|
|
|
// call grab().
|
|
|
|
const GrabHelper = new Lang.Class({
|
|
|
|
Name: 'GrabHelper',
|
|
|
|
|
2012-08-20 20:05:00 +00:00
|
|
|
_init: function(owner, params) {
|
2012-02-28 17:16:12 +00:00
|
|
|
this._owner = owner;
|
2012-08-20 20:05:00 +00:00
|
|
|
this._modalParams = params;
|
2012-02-28 17:16:12 +00:00
|
|
|
|
|
|
|
this._grabStack = [];
|
|
|
|
|
|
|
|
this._actors = [];
|
|
|
|
this._ignoreRelease = false;
|
|
|
|
|
|
|
|
this._modalCount = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// addActor:
|
|
|
|
// @actor: an actor
|
|
|
|
//
|
|
|
|
// Adds @actor to the set of actors that are allowed to process events
|
|
|
|
// during a grab.
|
|
|
|
addActor: function(actor) {
|
|
|
|
actor.__grabHelperDestroyId = actor.connect('destroy', Lang.bind(this, function() { this.removeActor(actor); }));
|
|
|
|
this._actors.push(actor);
|
|
|
|
},
|
|
|
|
|
|
|
|
// removeActor:
|
|
|
|
// @actor: an actor
|
|
|
|
//
|
|
|
|
// Removes @actor from the set of actors that are allowed to
|
|
|
|
// process events during a grab.
|
|
|
|
removeActor: function(actor) {
|
|
|
|
let index = this._actors.indexOf(actor);
|
|
|
|
if (index != -1)
|
|
|
|
this._actors.splice(index, 1);
|
|
|
|
if (actor.__grabHelperDestroyId) {
|
|
|
|
actor.disconnect(actor.__grabHelperDestroyId);
|
|
|
|
delete actor.__grabHelperDestroyId;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_isWithinGrabbedActor: function(actor) {
|
2013-02-17 04:27:23 +00:00
|
|
|
let currentActor = this.currentGrab.actor;
|
2012-02-28 17:16:12 +00:00
|
|
|
while (actor) {
|
|
|
|
if (this._actors.indexOf(actor) != -1)
|
|
|
|
return true;
|
2012-11-26 19:47:59 +00:00
|
|
|
if (actor == currentActor)
|
|
|
|
return true;
|
2012-02-28 17:16:12 +00:00
|
|
|
actor = actor.get_parent();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
get currentGrab() {
|
2012-09-15 17:14:52 +00:00
|
|
|
return this._grabStack[this._grabStack.length - 1] || {};
|
2012-02-28 17:16:12 +00:00
|
|
|
},
|
|
|
|
|
2012-03-01 00:09:41 +00:00
|
|
|
get grabbed() {
|
|
|
|
return this._grabStack.length > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
get grabStack() {
|
|
|
|
return this._grabStack;
|
|
|
|
},
|
|
|
|
|
2012-02-28 17:16:12 +00:00
|
|
|
_findStackIndex: function(actor) {
|
|
|
|
if (!actor)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._grabStack.length; i++) {
|
|
|
|
if (this._grabStack[i].actor === actor)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
2012-11-26 19:55:03 +00:00
|
|
|
_actorInGrabStack: function(actor) {
|
|
|
|
while (actor) {
|
|
|
|
let idx = this._findStackIndex(actor);
|
|
|
|
if (idx >= 0)
|
|
|
|
return idx;
|
|
|
|
actor = actor.get_parent();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
2012-02-28 17:16:12 +00:00
|
|
|
isActorGrabbed: function(actor) {
|
|
|
|
return this._findStackIndex(actor) >= 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// grab:
|
|
|
|
// @params: A bunch of parameters, see below
|
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// The general effect of a "grab" is to ensure that the passed in actor
|
|
|
|
// and all actors inside the grab get exclusive control of the mouse and
|
|
|
|
// keyboard, with the grab automatically being dropped if the user tries
|
|
|
|
// to dismiss it. The actor is passed in through @params.actor.
|
2012-02-28 17:16:12 +00:00
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// grab() can be called multiple times, with the scope of the grab being
|
|
|
|
// changed to a different actor every time. A nested grab does not have
|
|
|
|
// to have its grabbed actor inside the parent grab actors.
|
2012-02-28 17:16:12 +00:00
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// Grabs can be automatically dropped if the user tries to dismiss it
|
|
|
|
// in one of two ways: the user clicking outside the currently grabbed
|
|
|
|
// actor, or the user typing the Escape key.
|
2012-02-28 17:16:12 +00:00
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// If the user clicks outside the grabbed actors, and the clicked on
|
|
|
|
// actor is part of a previous grab in the stack, grabs will be popped
|
|
|
|
// until that grab is active. However, the click event will not be
|
|
|
|
// replayed to the actor.
|
2012-02-28 17:16:12 +00:00
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// If the user types the Escape key, one grab from the grab stack will
|
|
|
|
// be popped.
|
|
|
|
//
|
|
|
|
// When a grab is popped by user interacting as described above, if you
|
|
|
|
// pass a callback as @params.onUngrab, it will be called with %true.
|
|
|
|
//
|
|
|
|
// If @params.focus is not null, we'll set the key focus directly
|
|
|
|
// to that actor instead of navigating in @params.actor. This is for
|
|
|
|
// use cases like menus, where we want to grab the menu actor, but keep
|
|
|
|
// focus on the clicked on menu item.
|
2012-02-28 17:16:12 +00:00
|
|
|
grab: function(params) {
|
|
|
|
params = Params.parse(params, { actor: null,
|
2012-03-01 00:09:41 +00:00
|
|
|
focus: null,
|
2012-02-28 17:16:12 +00:00
|
|
|
onUngrab: null });
|
|
|
|
|
|
|
|
let focus = global.stage.key_focus;
|
|
|
|
let hadFocus = focus && this._isWithinGrabbedActor(focus);
|
2012-08-16 17:40:51 +00:00
|
|
|
let newFocus = params.actor;
|
2012-02-28 17:16:12 +00:00
|
|
|
|
|
|
|
if (this.isActorGrabbed(params.actor))
|
2012-08-21 23:40:11 +00:00
|
|
|
return true;
|
2012-02-28 17:16:12 +00:00
|
|
|
|
|
|
|
params.savedFocus = focus;
|
|
|
|
|
2013-05-23 21:20:30 +00:00
|
|
|
if (!this._takeModalGrab())
|
2012-09-19 02:04:57 +00:00
|
|
|
return false;
|
2012-02-28 17:16:12 +00:00
|
|
|
|
2013-02-16 17:52:37 +00:00
|
|
|
this._grabStack.push(params);
|
|
|
|
|
2013-02-11 16:52:09 +00:00
|
|
|
if (params.focus) {
|
2012-03-01 00:09:41 +00:00
|
|
|
params.focus.grab_key_focus();
|
2013-05-23 21:11:40 +00:00
|
|
|
} else if (newFocus && hadFocus) {
|
2013-02-11 16:52:09 +00:00
|
|
|
if (!newFocus.navigate_focus(null, Gtk.DirectionType.TAB_FORWARD, false))
|
|
|
|
newFocus.grab_key_focus();
|
|
|
|
}
|
2012-08-21 23:40:11 +00:00
|
|
|
|
|
|
|
return true;
|
2012-02-28 17:16:12 +00:00
|
|
|
},
|
|
|
|
|
2012-09-15 20:41:18 +00:00
|
|
|
_takeModalGrab: function() {
|
2012-09-15 05:38:40 +00:00
|
|
|
let firstGrab = (this._modalCount == 0);
|
2012-09-19 02:04:57 +00:00
|
|
|
if (firstGrab) {
|
2012-08-20 20:05:00 +00:00
|
|
|
if (!Main.pushModal(this._owner, this._modalParams))
|
2012-09-19 02:04:57 +00:00
|
|
|
return false;
|
2013-05-23 21:20:30 +00:00
|
|
|
|
2013-08-17 00:11:36 +00:00
|
|
|
_pushGrabHelper(this);
|
2012-09-19 02:04:57 +00:00
|
|
|
}
|
2012-09-15 05:38:40 +00:00
|
|
|
|
2012-09-19 02:04:57 +00:00
|
|
|
this._modalCount++;
|
|
|
|
return true;
|
2012-09-15 05:38:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_releaseModalGrab: function() {
|
|
|
|
this._modalCount--;
|
|
|
|
if (this._modalCount > 0)
|
|
|
|
return;
|
|
|
|
|
2013-08-17 00:11:36 +00:00
|
|
|
_popGrabHelper(this);
|
2013-05-23 21:20:30 +00:00
|
|
|
|
|
|
|
this._ignoreRelease = false;
|
|
|
|
|
2012-09-15 05:38:40 +00:00
|
|
|
Main.popModal(this._owner);
|
|
|
|
global.sync_pointer();
|
|
|
|
},
|
|
|
|
|
2012-02-28 17:16:12 +00:00
|
|
|
// ignoreRelease:
|
|
|
|
//
|
|
|
|
// Make sure that the next button release event evaluated by the
|
|
|
|
// capture event handler returns false. This is designed for things
|
|
|
|
// like the ComboBoxMenu that go away on press, but need to eat
|
|
|
|
// the next release event.
|
|
|
|
ignoreRelease: function() {
|
|
|
|
this._ignoreRelease = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ungrab:
|
|
|
|
// @params: The parameters for the grab; see below.
|
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// Pops @params.actor from the grab stack, potentially dropping
|
|
|
|
// the grab. If the actor is not on the grab stack, this call is
|
|
|
|
// ignored with no ill effects.
|
2012-02-28 17:16:12 +00:00
|
|
|
//
|
2013-05-23 18:52:43 +00:00
|
|
|
// If the actor is not at the top of the grab stack, grabs are
|
|
|
|
// popped until the grabbed actor is at the top of the grab stack.
|
|
|
|
// The onUngrab callback for every grab is called for every popped
|
|
|
|
// grab with the parameter %false.
|
2012-02-28 17:16:12 +00:00
|
|
|
ungrab: function(params) {
|
2012-12-10 08:55:14 +00:00
|
|
|
params = Params.parse(params, { actor: this.currentGrab.actor,
|
|
|
|
isUser: false });
|
2012-02-28 17:16:12 +00:00
|
|
|
|
|
|
|
let grabStackIndex = this._findStackIndex(params.actor);
|
|
|
|
if (grabStackIndex < 0)
|
|
|
|
return;
|
|
|
|
|
2012-09-15 05:38:40 +00:00
|
|
|
let focus = global.stage.key_focus;
|
|
|
|
let hadFocus = focus && this._isWithinGrabbedActor(focus);
|
|
|
|
|
2012-08-16 17:48:22 +00:00
|
|
|
let poppedGrabs = this._grabStack.slice(grabStackIndex);
|
2012-08-20 01:12:46 +00:00
|
|
|
// "Pop" all newly ungrabbed actors off the grab stack
|
|
|
|
// by truncating the array.
|
2012-02-28 17:16:12 +00:00
|
|
|
this._grabStack.length = grabStackIndex;
|
|
|
|
|
2012-08-20 01:12:46 +00:00
|
|
|
for (let i = poppedGrabs.length - 1; i >= 0; i--) {
|
|
|
|
let poppedGrab = poppedGrabs[i];
|
2012-02-28 17:16:12 +00:00
|
|
|
|
2012-08-20 01:12:46 +00:00
|
|
|
if (poppedGrab.onUngrab)
|
2012-12-10 08:55:14 +00:00
|
|
|
poppedGrab.onUngrab(params.isUser);
|
2012-08-20 01:12:46 +00:00
|
|
|
|
2013-05-23 21:20:30 +00:00
|
|
|
this._releaseModalGrab();
|
2012-12-30 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
2012-09-15 05:38:40 +00:00
|
|
|
if (hadFocus) {
|
|
|
|
let poppedGrab = poppedGrabs[0];
|
2013-02-09 22:05:43 +00:00
|
|
|
if (poppedGrab.savedFocus)
|
|
|
|
poppedGrab.savedFocus.grab_key_focus();
|
2012-02-28 17:16:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-17 00:11:36 +00:00
|
|
|
onCapturedEvent: function(event) {
|
2012-02-28 17:16:12 +00:00
|
|
|
let type = event.type();
|
2012-12-30 17:32:41 +00:00
|
|
|
|
|
|
|
if (type == Clutter.EventType.KEY_PRESS &&
|
|
|
|
event.get_key_symbol() == Clutter.KEY_Escape) {
|
|
|
|
this.ungrab({ isUser: true });
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2012-12-30 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 17:16:12 +00:00
|
|
|
let press = type == Clutter.EventType.BUTTON_PRESS;
|
|
|
|
let release = type == Clutter.EventType.BUTTON_RELEASE;
|
|
|
|
let button = press || release;
|
|
|
|
|
|
|
|
if (release && this._ignoreRelease) {
|
|
|
|
this._ignoreRelease = false;
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2012-02-28 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._isWithinGrabbedActor(event.get_source()))
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2012-02-28 17:16:12 +00:00
|
|
|
|
2012-09-21 14:58:03 +00:00
|
|
|
if (Main.keyboard.shouldTakeEvent(event))
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2012-09-21 14:58:03 +00:00
|
|
|
|
2012-02-28 17:16:12 +00:00
|
|
|
if (button) {
|
|
|
|
// If we have a press event, ignore the next event,
|
|
|
|
// which should be a release event.
|
|
|
|
if (press)
|
|
|
|
this._ignoreRelease = true;
|
2012-11-26 19:55:03 +00:00
|
|
|
let i = this._actorInGrabStack(event.get_source()) + 1;
|
2012-12-10 08:55:14 +00:00
|
|
|
this.ungrab({ actor: this._grabStack[i].actor, isUser: true });
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2012-02-28 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2012-02-28 17:16:12 +00:00
|
|
|
},
|
|
|
|
});
|