2012-11-30 15:16:48 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported SwitcherPopup, SwitcherList */
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2021-11-25 09:49:42 +00:00
|
|
|
const { Clutter, GLib, GObject, St } = imports.gi;
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var POPUP_DELAY_TIMEOUT = 150; // milliseconds
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-08-01 23:13:10 +00:00
|
|
|
var POPUP_SCROLL_TIME = 100; // milliseconds
|
|
|
|
var POPUP_FADE_OUT_TIME = 100; // milliseconds
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var DISABLE_HOVER_TIMEOUT = 500; // milliseconds
|
2017-06-08 12:20:56 +00:00
|
|
|
var NO_MODS_TIMEOUT = 1500; // milliseconds
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
function mod(a, b) {
|
|
|
|
return (a + b) % b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function primaryModifier(mask) {
|
|
|
|
if (mask == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
let primary = 1;
|
|
|
|
while (mask > 1) {
|
|
|
|
mask >>= 1;
|
|
|
|
primary <<= 1;
|
|
|
|
}
|
|
|
|
return primary;
|
|
|
|
}
|
|
|
|
|
2019-04-27 18:45:14 +00:00
|
|
|
var SwitcherPopup = GObject.registerClass({
|
2019-08-20 21:43:54 +00:00
|
|
|
GTypeFlags: GObject.TypeFlags.ABSTRACT,
|
2019-04-27 18:45:14 +00:00
|
|
|
}, class SwitcherPopup extends St.Widget {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(items) {
|
2020-03-29 21:51:13 +00:00
|
|
|
super._init({
|
|
|
|
style_class: 'switcher-popup',
|
|
|
|
reactive: true,
|
|
|
|
visible: false,
|
|
|
|
});
|
2018-08-13 23:14:55 +00:00
|
|
|
|
2012-11-30 15:16:48 +00:00
|
|
|
this._switcherList = null;
|
|
|
|
|
|
|
|
this._items = items || [];
|
|
|
|
this._selectedIndex = 0;
|
|
|
|
|
2018-08-13 23:14:55 +00:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-08-13 23:14:55 +00:00
|
|
|
Main.uiGroup.add_actor(this);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2021-08-15 22:36:59 +00:00
|
|
|
Main.layoutManager.connectObject(
|
|
|
|
'system-modal-opened', () => this.destroy(), this);
|
2019-12-06 12:56:24 +00:00
|
|
|
|
2012-11-30 15:16:48 +00:00
|
|
|
this._haveModal = false;
|
|
|
|
this._modifierMask = 0;
|
|
|
|
|
|
|
|
this._motionTimeoutId = 0;
|
|
|
|
this._initialDelayTimeoutId = 0;
|
2017-06-08 12:20:56 +00:00
|
|
|
this._noModsTimeoutId = 0;
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-10-10 20:46:26 +00:00
|
|
|
this.add_constraint(new Clutter.BindConstraint({
|
|
|
|
source: global.stage,
|
|
|
|
coordinate: Clutter.BindCoordinate.ALL,
|
|
|
|
}));
|
2018-08-23 16:35:43 +00:00
|
|
|
|
2012-11-30 15:16:48 +00:00
|
|
|
// Initially disable hover so we ignore the enter-event if
|
|
|
|
// the switcher appears underneath the current pointer location
|
|
|
|
this._disableHover();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2020-05-09 19:30:26 +00:00
|
|
|
vfunc_allocate(box) {
|
|
|
|
this.set_allocation(box);
|
2018-08-13 23:14:55 +00:00
|
|
|
|
2012-11-30 15:16:48 +00:00
|
|
|
let childBox = new Clutter.ActorBox();
|
|
|
|
let primary = Main.layoutManager.primaryMonitor;
|
|
|
|
|
2018-08-13 23:14:55 +00:00
|
|
|
let leftPadding = this.get_theme_node().get_padding(St.Side.LEFT);
|
|
|
|
let rightPadding = this.get_theme_node().get_padding(St.Side.RIGHT);
|
2012-11-30 15:16:48 +00:00
|
|
|
let hPadding = leftPadding + rightPadding;
|
|
|
|
|
|
|
|
// Allocate the switcherList
|
|
|
|
// We select a size based on an icon size that does not overflow the screen
|
2019-02-01 13:41:55 +00:00
|
|
|
let [, childNaturalHeight] = this._switcherList.get_preferred_height(primary.width - hPadding);
|
|
|
|
let [, childNaturalWidth] = this._switcherList.get_preferred_width(childNaturalHeight);
|
2012-11-30 15:16:48 +00:00
|
|
|
childBox.x1 = Math.max(primary.x + leftPadding, primary.x + Math.floor((primary.width - childNaturalWidth) / 2));
|
|
|
|
childBox.x2 = Math.min(primary.x + primary.width - rightPadding, childBox.x1 + childNaturalWidth);
|
|
|
|
childBox.y1 = primary.y + Math.floor((primary.height - childNaturalHeight) / 2);
|
|
|
|
childBox.y2 = childBox.y1 + childNaturalHeight;
|
2020-05-09 19:30:26 +00:00
|
|
|
this._switcherList.allocate(childBox);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
_initialSelection(backward, _binding) {
|
2014-09-03 15:17:26 +00:00
|
|
|
if (backward)
|
|
|
|
this._select(this._items.length - 1);
|
|
|
|
else if (this._items.length == 1)
|
|
|
|
this._select(0);
|
|
|
|
else
|
|
|
|
this._select(1);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
show(backward, binding, mask) {
|
2014-09-03 15:15:31 +00:00
|
|
|
if (this._items.length == 0)
|
2012-11-30 15:16:48 +00:00
|
|
|
return false;
|
|
|
|
|
2021-11-25 09:49:42 +00:00
|
|
|
let grab = Main.pushModal(this);
|
|
|
|
// We expect at least a keyboard grab here
|
|
|
|
if ((grab.get_seat_state() & Clutter.GrabState.KEYBOARD) === 0) {
|
|
|
|
Main.popModal(grab);
|
|
|
|
return false;
|
2012-11-30 15:16:48 +00:00
|
|
|
}
|
2021-11-25 09:49:42 +00:00
|
|
|
this._grab = grab;
|
2012-11-30 15:16:48 +00:00
|
|
|
this._haveModal = true;
|
|
|
|
this._modifierMask = primaryModifier(mask);
|
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
this.add_actor(this._switcherList);
|
2017-12-02 00:27:35 +00:00
|
|
|
this._switcherList.connect('item-activated', this._itemActivated.bind(this));
|
|
|
|
this._switcherList.connect('item-entered', this._itemEntered.bind(this));
|
|
|
|
this._switcherList.connect('item-removed', this._itemRemoved.bind(this));
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
// Need to force an allocation so we can figure out whether we
|
|
|
|
// need to scroll when selecting
|
2018-08-13 23:14:55 +00:00
|
|
|
this.opacity = 0;
|
|
|
|
this.visible = true;
|
|
|
|
this.get_allocation_box();
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
this._initialSelection(backward, binding);
|
|
|
|
|
|
|
|
// There's a race condition; if the user released Alt before
|
|
|
|
// we got the grab, then we won't be notified. (See
|
|
|
|
// https://bugzilla.gnome.org/show_bug.cgi?id=596695 for
|
|
|
|
// details.) So we check now. (Have to do this after updating
|
|
|
|
// selection.)
|
2017-06-08 12:20:56 +00:00
|
|
|
if (this._modifierMask) {
|
2019-01-31 14:08:00 +00:00
|
|
|
let [x_, y_, mods] = global.get_pointer();
|
2017-06-08 12:20:56 +00:00
|
|
|
if (!(mods & this._modifierMask)) {
|
|
|
|
this._finish(global.get_current_time());
|
2018-12-06 17:05:39 +00:00
|
|
|
return true;
|
2017-06-08 12:20:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._resetNoModsTimeout();
|
2012-11-30 15:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We delay showing the popup so that fast Alt+Tab users aren't
|
|
|
|
// disturbed by the popup briefly flashing.
|
2019-08-19 18:50:33 +00:00
|
|
|
this._initialDelayTimeoutId = GLib.timeout_add(
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
|
|
|
POPUP_DELAY_TIMEOUT,
|
|
|
|
() => {
|
2019-11-21 22:40:02 +00:00
|
|
|
this._showImmediately();
|
2019-08-19 18:50:33 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._initialDelayTimeoutId, '[gnome-shell] Main.osdWindow.cancel');
|
2012-11-30 15:16:48 +00:00
|
|
|
return true;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-11-21 22:40:02 +00:00
|
|
|
_showImmediately() {
|
|
|
|
if (this._initialDelayTimeoutId === 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GLib.source_remove(this._initialDelayTimeoutId);
|
|
|
|
this._initialDelayTimeoutId = 0;
|
|
|
|
|
|
|
|
Main.osdWindowManager.hideAll();
|
|
|
|
this.opacity = 255;
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_next() {
|
2012-11-30 15:16:48 +00:00
|
|
|
return mod(this._selectedIndex + 1, this._items.length);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_previous() {
|
2012-11-30 15:16:48 +00:00
|
|
|
return mod(this._selectedIndex - 1, this._items.length);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
_keyPressHandler(_keysym, _action) {
|
2019-05-21 20:28:07 +00:00
|
|
|
throw new GObject.NotImplementedError(`_keyPressHandler in ${this.constructor.name}`);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_key_press_event(keyEvent) {
|
|
|
|
let keysym = keyEvent.keyval;
|
|
|
|
let action = global.display.get_keybinding_action(
|
|
|
|
keyEvent.hardware_keycode, keyEvent.modifier_state);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
this._disableHover();
|
|
|
|
|
2019-11-21 22:40:02 +00:00
|
|
|
if (this._keyPressHandler(keysym, action) != Clutter.EVENT_PROPAGATE) {
|
|
|
|
this._showImmediately();
|
2014-05-26 13:27:16 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2019-11-21 22:40:02 +00:00
|
|
|
}
|
2014-05-26 13:27:16 +00:00
|
|
|
|
2018-05-29 04:52:24 +00:00
|
|
|
// Note: pressing one of the below keys will destroy the popup only if
|
|
|
|
// that key is not used by the active popup's keyboard shortcut
|
2019-11-05 19:37:28 +00:00
|
|
|
if (keysym === Clutter.KEY_Escape || keysym === Clutter.KEY_Tab)
|
2018-08-13 23:05:50 +00:00
|
|
|
this.fadeAndDestroy();
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-11-11 21:00:01 +00:00
|
|
|
// Allow to explicitly select the current item; this is particularly
|
|
|
|
// useful for no-modifier popups
|
|
|
|
if (keysym === Clutter.KEY_space ||
|
|
|
|
keysym === Clutter.KEY_Return ||
|
|
|
|
keysym === Clutter.KEY_KP_Enter ||
|
|
|
|
keysym === Clutter.KEY_ISO_Enter)
|
|
|
|
this._finish(keyEvent.time);
|
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_key_release_event(keyEvent) {
|
2017-06-08 12:20:56 +00:00
|
|
|
if (this._modifierMask) {
|
2019-01-31 14:08:00 +00:00
|
|
|
let [x_, y_, mods] = global.get_pointer();
|
2017-06-08 12:20:56 +00:00
|
|
|
let state = mods & this._modifierMask;
|
|
|
|
|
|
|
|
if (state == 0)
|
2019-09-10 05:42:48 +00:00
|
|
|
this._finish(keyEvent.time);
|
2017-06-08 12:20:56 +00:00
|
|
|
} else {
|
|
|
|
this._resetNoModsTimeout();
|
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_STOP;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_button_press_event() {
|
|
|
|
/* We clicked outside */
|
2018-08-13 23:05:50 +00:00
|
|
|
this.fadeAndDestroy();
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_scrollHandler(direction) {
|
2012-11-30 15:16:48 +00:00
|
|
|
if (direction == Clutter.ScrollDirection.UP)
|
|
|
|
this._select(this._previous());
|
|
|
|
else if (direction == Clutter.ScrollDirection.DOWN)
|
|
|
|
this._select(this._next());
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-09-10 05:42:48 +00:00
|
|
|
vfunc_scroll_event(scrollEvent) {
|
2018-05-06 20:49:52 +00:00
|
|
|
this._disableHover();
|
|
|
|
|
2020-01-15 16:04:07 +00:00
|
|
|
this._scrollHandler(scrollEvent.direction);
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemActivatedHandler(n) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._select(n);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemActivated(switcher, n) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._itemActivatedHandler(n);
|
2012-12-04 18:45:52 +00:00
|
|
|
this._finish(global.get_current_time());
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemEnteredHandler(n) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._select(n);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemEntered(switcher, n) {
|
2012-11-30 15:16:48 +00:00
|
|
|
if (!this.mouseActive)
|
|
|
|
return;
|
|
|
|
this._itemEnteredHandler(n);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemRemovedHandler(n) {
|
2018-02-08 11:11:11 +00:00
|
|
|
if (this._items.length > 0) {
|
2018-04-25 22:32:57 +00:00
|
|
|
let newIndex;
|
|
|
|
|
|
|
|
if (n < this._selectedIndex)
|
|
|
|
newIndex = this._selectedIndex - 1;
|
|
|
|
else if (n === this._selectedIndex)
|
|
|
|
newIndex = Math.min(n, this._items.length - 1);
|
|
|
|
else if (n > this._selectedIndex)
|
|
|
|
return; // No need to select something new in this case
|
|
|
|
|
2018-02-08 11:11:11 +00:00
|
|
|
this._select(newIndex);
|
|
|
|
} else {
|
2018-08-13 23:05:50 +00:00
|
|
|
this.fadeAndDestroy();
|
2018-02-08 11:11:11 +00:00
|
|
|
}
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2018-02-08 11:11:11 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemRemoved(switcher, n) {
|
2018-02-08 11:11:11 +00:00
|
|
|
this._itemRemovedHandler(n);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2018-02-08 11:11:11 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_disableHover() {
|
2012-11-30 15:16:48 +00:00
|
|
|
this.mouseActive = false;
|
|
|
|
|
|
|
|
if (this._motionTimeoutId != 0)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._motionTimeoutId);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-08-19 18:50:33 +00:00
|
|
|
this._motionTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, DISABLE_HOVER_TIMEOUT, this._mouseTimedOut.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._motionTimeoutId, '[gnome-shell] this._mouseTimedOut');
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_mouseTimedOut() {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._motionTimeoutId = 0;
|
|
|
|
this.mouseActive = true;
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_resetNoModsTimeout() {
|
2017-06-08 12:20:56 +00:00
|
|
|
if (this._noModsTimeoutId != 0)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._noModsTimeoutId);
|
|
|
|
|
|
|
|
this._noModsTimeoutId = GLib.timeout_add(
|
|
|
|
GLib.PRIORITY_DEFAULT,
|
|
|
|
NO_MODS_TIMEOUT,
|
|
|
|
() => {
|
2019-11-22 19:56:30 +00:00
|
|
|
this._finish(global.display.get_current_time_roundtrip());
|
2019-08-19 18:50:33 +00:00
|
|
|
this._noModsTimeoutId = 0;
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-06-08 12:20:56 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_popModal() {
|
2012-11-30 15:16:48 +00:00
|
|
|
if (this._haveModal) {
|
2021-11-25 09:49:42 +00:00
|
|
|
Main.popModal(this._grab);
|
|
|
|
this._grab = null;
|
2012-11-30 15:16:48 +00:00
|
|
|
this._haveModal = false;
|
|
|
|
}
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-08-13 23:05:50 +00:00
|
|
|
fadeAndDestroy() {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._popModal();
|
2018-07-21 03:20:35 +00:00
|
|
|
if (this.opacity > 0) {
|
2018-07-20 19:46:19 +00:00
|
|
|
this.ease({
|
|
|
|
opacity: 0,
|
|
|
|
duration: POPUP_FADE_OUT_TIME,
|
2020-04-15 09:16:32 +00:00
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
2019-08-20 21:43:54 +00:00
|
|
|
onComplete: () => this.destroy(),
|
2018-07-20 19:46:19 +00:00
|
|
|
});
|
2018-08-13 23:14:55 +00:00
|
|
|
} else {
|
|
|
|
this.destroy();
|
|
|
|
}
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
_finish(_timestamp) {
|
2018-08-13 23:05:50 +00:00
|
|
|
this.fadeAndDestroy();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDestroy() {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._popModal();
|
|
|
|
|
|
|
|
if (this._motionTimeoutId != 0)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._motionTimeoutId);
|
2012-11-30 15:16:48 +00:00
|
|
|
if (this._initialDelayTimeoutId != 0)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._initialDelayTimeoutId);
|
2017-06-08 12:20:56 +00:00
|
|
|
if (this._noModsTimeoutId != 0)
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._noModsTimeoutId);
|
altTab: Don't return from _init() if there are no windows/apps
If a new SwitcherPopup is created and there are no windows or apps to
switch through found, instead of returning from _init(), still
initialize the SwitcherPopup and let the check in SwitcherPopup.show()
return false to terminate the popup.
In both cases, with or without the return statements,
WindowManager._startSwitcher() will call SwicherPopup.destroy(), which
will try to disconnect signal handlers, destroy actors etc. Now if the
constructor can't finish creating the popup, some of the functions
called from _onDestroy() will fail and throw errors.
One of those cases is when window-switcher is limited to the current
workspace, and a WindowCyclerPopup is initiated on an empty workspace.
Because this._highlight hasn't been created, _onDestroy() will fail when
trying to destroy the actor of this._highlight.
Also, the actor of this._switcherList will not get destroyed in case
show() returns because this._items is empty. For example, this will
happen when a new AppSwitcherPopup is initialized with at least 1
running app, but 0 windows on the active workspace.
https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/168
2018-05-06 15:17:33 +00:00
|
|
|
|
|
|
|
// Make sure the SwitcherList is always destroyed, it may not be
|
|
|
|
// a child of the actor at this point.
|
|
|
|
if (this._switcherList)
|
|
|
|
this._switcherList.destroy();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_select(num) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._selectedIndex = num;
|
|
|
|
this._switcherList.highlight(num);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
var SwitcherButton = GObject.registerClass(
|
|
|
|
class SwitcherButton extends St.Button {
|
2018-06-28 15:34:01 +00:00
|
|
|
_init(square) {
|
2020-03-29 21:51:13 +00:00
|
|
|
super._init({
|
|
|
|
style_class: 'item-box',
|
|
|
|
reactive: true,
|
|
|
|
});
|
2018-06-28 15:34:01 +00:00
|
|
|
|
|
|
|
this._square = square;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2018-06-28 15:34:01 +00:00
|
|
|
|
|
|
|
vfunc_get_preferred_width(forHeight) {
|
|
|
|
if (this._square)
|
|
|
|
return this.get_preferred_height(-1);
|
|
|
|
else
|
2017-10-31 01:23:39 +00:00
|
|
|
return super.vfunc_get_preferred_width(forHeight);
|
2018-06-28 15:34:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
var SwitcherList = GObject.registerClass({
|
2020-03-29 21:51:13 +00:00
|
|
|
Signals: {
|
|
|
|
'item-activated': { param_types: [GObject.TYPE_INT] },
|
|
|
|
'item-entered': { param_types: [GObject.TYPE_INT] },
|
|
|
|
'item-removed': { param_types: [GObject.TYPE_INT] },
|
|
|
|
},
|
2017-10-31 01:23:39 +00:00
|
|
|
}, class SwitcherList extends St.Widget {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(squareItems) {
|
2017-10-31 01:23:39 +00:00
|
|
|
super._init({ style_class: 'switcher-list' });
|
2018-06-28 15:34:01 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._list = new St.BoxLayout({
|
|
|
|
style_class: 'switcher-list-item-container',
|
|
|
|
vertical: false,
|
|
|
|
x_expand: true,
|
|
|
|
y_expand: true,
|
|
|
|
});
|
2018-06-28 15:34:01 +00:00
|
|
|
|
|
|
|
let layoutManager = this._list.get_layout_manager();
|
|
|
|
|
2012-11-30 15:16:48 +00:00
|
|
|
this._list.spacing = 0;
|
2017-10-31 00:38:18 +00:00
|
|
|
this._list.connect('style-changed', () => {
|
|
|
|
this._list.spacing = this._list.get_theme_node().get_length('spacing');
|
|
|
|
});
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._scrollView = new St.ScrollView({
|
|
|
|
style_class: 'hfade',
|
|
|
|
enable_mouse_scrolling: false,
|
|
|
|
});
|
2018-11-27 12:45:36 +00:00
|
|
|
this._scrollView.set_policy(St.PolicyType.NEVER, St.PolicyType.NEVER);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
this._scrollView.add_actor(this._list);
|
|
|
|
this.add_actor(this._scrollView);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
// Those arrows indicate whether scrolling in one direction is possible
|
2020-03-29 21:51:13 +00:00
|
|
|
this._leftArrow = new St.DrawingArea({
|
|
|
|
style_class: 'switcher-arrow',
|
|
|
|
pseudo_class: 'highlighted',
|
|
|
|
});
|
2017-10-31 00:38:18 +00:00
|
|
|
this._leftArrow.connect('repaint', () => {
|
|
|
|
drawArrow(this._leftArrow, St.Side.LEFT);
|
|
|
|
});
|
2020-03-29 21:51:13 +00:00
|
|
|
this._rightArrow = new St.DrawingArea({
|
|
|
|
style_class: 'switcher-arrow',
|
|
|
|
pseudo_class: 'highlighted',
|
|
|
|
});
|
2017-10-31 00:38:18 +00:00
|
|
|
this._rightArrow.connect('repaint', () => {
|
|
|
|
drawArrow(this._rightArrow, St.Side.RIGHT);
|
|
|
|
});
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
this.add_actor(this._leftArrow);
|
|
|
|
this.add_actor(this._rightArrow);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
this._items = [];
|
|
|
|
this._highlighted = -1;
|
|
|
|
this._squareItems = squareItems;
|
|
|
|
this._scrollableRight = true;
|
|
|
|
this._scrollableLeft = false;
|
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
layoutManager.homogeneous = squareItems;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
addItem(item, label) {
|
2018-06-28 15:34:01 +00:00
|
|
|
let bbox = new SwitcherButton(this._squareItems);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
bbox.set_child(item);
|
|
|
|
this._list.add_actor(bbox);
|
|
|
|
|
2020-01-15 15:19:41 +00:00
|
|
|
bbox.connect('clicked', () => this._onItemClicked(bbox));
|
2022-01-12 10:36:41 +00:00
|
|
|
bbox.connect('motion-event', () => this._onItemMotion(bbox));
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
bbox.label_actor = label;
|
|
|
|
|
|
|
|
this._items.push(bbox);
|
|
|
|
|
|
|
|
return bbox;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
removeItem(index) {
|
2018-02-08 10:38:04 +00:00
|
|
|
let item = this._items.splice(index, 1);
|
|
|
|
item[0].destroy();
|
2018-02-08 11:11:11 +00:00
|
|
|
this.emit('item-removed', index);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2018-02-08 10:38:04 +00:00
|
|
|
|
2018-04-26 09:19:40 +00:00
|
|
|
addAccessibleState(index, state) {
|
|
|
|
this._items[index].add_accessible_state(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAccessibleState(index, state) {
|
|
|
|
this._items[index].remove_accessible_state(state);
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:19:41 +00:00
|
|
|
_onItemClicked(item) {
|
|
|
|
this._itemActivated(this._items.indexOf(item));
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2022-01-12 10:36:41 +00:00
|
|
|
_onItemMotion(item) {
|
2017-02-28 12:35:57 +00:00
|
|
|
// Avoid reentrancy
|
2020-01-15 15:19:41 +00:00
|
|
|
if (item !== this._items[this._highlighted])
|
|
|
|
this._itemEntered(this._items.indexOf(item));
|
2018-04-25 20:14:57 +00:00
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
highlight(index, justOutline) {
|
2018-02-08 10:38:04 +00:00
|
|
|
if (this._items[this._highlighted]) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this._items[this._highlighted].remove_style_pseudo_class('outlined');
|
|
|
|
this._items[this._highlighted].remove_style_pseudo_class('selected');
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:11:11 +00:00
|
|
|
if (this._items[index]) {
|
2012-11-30 15:16:48 +00:00
|
|
|
if (justOutline)
|
2018-02-08 11:11:11 +00:00
|
|
|
this._items[index].add_style_pseudo_class('outlined');
|
2012-11-30 15:16:48 +00:00
|
|
|
else
|
2018-02-08 11:11:11 +00:00
|
|
|
this._items[index].add_style_pseudo_class('selected');
|
2012-11-30 15:16:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:11:11 +00:00
|
|
|
this._highlighted = index;
|
|
|
|
|
2012-11-30 15:16:48 +00:00
|
|
|
let adjustment = this._scrollView.hscroll.adjustment;
|
2019-02-01 13:41:55 +00:00
|
|
|
let [value] = adjustment.get_values();
|
|
|
|
let [absItemX] = this._items[index].get_transformed_position();
|
2019-01-31 14:08:00 +00:00
|
|
|
let [result_, posX, posY_] = this.transform_stage_point(absItemX, 0);
|
2019-02-01 13:41:55 +00:00
|
|
|
let [containerWidth] = this.get_transformed_size();
|
2012-11-30 15:16:48 +00:00
|
|
|
if (posX + this._items[index].get_width() > containerWidth)
|
2018-04-25 21:24:05 +00:00
|
|
|
this._scrollToRight(index);
|
2012-11-30 15:16:48 +00:00
|
|
|
else if (this._items[index].allocation.x1 - value < 0)
|
2018-04-25 21:24:05 +00:00
|
|
|
this._scrollToLeft(index);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-04-25 21:24:05 +00:00
|
|
|
_scrollToLeft(index) {
|
2012-11-30 15:16:48 +00:00
|
|
|
let adjustment = this._scrollView.hscroll.adjustment;
|
2019-01-31 14:08:00 +00:00
|
|
|
let [value, lower_, upper, stepIncrement_, pageIncrement_, pageSize] = adjustment.get_values();
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-04-25 21:24:05 +00:00
|
|
|
let item = this._items[index];
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
if (item.allocation.x1 < value)
|
2018-04-25 21:56:29 +00:00
|
|
|
value = Math.max(0, item.allocation.x1);
|
2012-11-30 15:16:48 +00:00
|
|
|
else if (item.allocation.x2 > value + pageSize)
|
2018-04-25 21:56:29 +00:00
|
|
|
value = Math.min(upper, item.allocation.x2 - pageSize);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
this._scrollableRight = true;
|
2019-07-24 19:03:17 +00:00
|
|
|
adjustment.ease(value, {
|
|
|
|
progress_mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
|
|
|
duration: POPUP_SCROLL_TIME,
|
|
|
|
onComplete: () => {
|
2018-04-25 21:24:05 +00:00
|
|
|
if (index === 0)
|
2019-07-24 19:03:17 +00:00
|
|
|
this._scrollableLeft = false;
|
|
|
|
this.queue_relayout();
|
2019-08-20 21:43:54 +00:00
|
|
|
},
|
2019-07-24 19:03:17 +00:00
|
|
|
});
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-04-25 21:24:05 +00:00
|
|
|
_scrollToRight(index) {
|
2012-11-30 15:16:48 +00:00
|
|
|
let adjustment = this._scrollView.hscroll.adjustment;
|
2019-01-31 14:08:00 +00:00
|
|
|
let [value, lower_, upper, stepIncrement_, pageIncrement_, pageSize] = adjustment.get_values();
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-04-25 21:24:05 +00:00
|
|
|
let item = this._items[index];
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
if (item.allocation.x1 < value)
|
|
|
|
value = Math.max(0, item.allocation.x1);
|
|
|
|
else if (item.allocation.x2 > value + pageSize)
|
|
|
|
value = Math.min(upper, item.allocation.x2 - pageSize);
|
|
|
|
|
|
|
|
this._scrollableLeft = true;
|
2019-07-24 19:03:17 +00:00
|
|
|
adjustment.ease(value, {
|
|
|
|
progress_mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
|
|
|
duration: POPUP_SCROLL_TIME,
|
|
|
|
onComplete: () => {
|
2018-04-25 21:24:05 +00:00
|
|
|
if (index === this._items.length - 1)
|
2019-07-24 19:03:17 +00:00
|
|
|
this._scrollableRight = false;
|
|
|
|
this.queue_relayout();
|
2019-08-20 21:43:54 +00:00
|
|
|
},
|
2019-07-24 19:03:17 +00:00
|
|
|
});
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemActivated(n) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this.emit('item-activated', n);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_itemEntered(n) {
|
2012-11-30 15:16:48 +00:00
|
|
|
this.emit('item-entered', n);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_maxChildWidth(forHeight) {
|
2012-11-30 15:16:48 +00:00
|
|
|
let maxChildMin = 0;
|
|
|
|
let maxChildNat = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._items.length; i++) {
|
|
|
|
let [childMin, childNat] = this._items[i].get_preferred_width(forHeight);
|
|
|
|
maxChildMin = Math.max(childMin, maxChildMin);
|
|
|
|
maxChildNat = Math.max(childNat, maxChildNat);
|
|
|
|
|
|
|
|
if (this._squareItems) {
|
2019-08-20 00:20:08 +00:00
|
|
|
[childMin, childNat] = this._items[i].get_preferred_height(-1);
|
2012-11-30 15:16:48 +00:00
|
|
|
maxChildMin = Math.max(childMin, maxChildMin);
|
|
|
|
maxChildNat = Math.max(childNat, maxChildNat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [maxChildMin, maxChildNat];
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
vfunc_get_preferred_width(forHeight) {
|
|
|
|
let themeNode = this.get_theme_node();
|
2019-01-29 19:20:09 +00:00
|
|
|
let [maxChildMin] = this._maxChildWidth(forHeight);
|
|
|
|
let [minListWidth] = this._list.get_preferred_width(forHeight);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
return themeNode.adjust_preferred_width(maxChildMin, minListWidth);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-01-31 14:08:10 +00:00
|
|
|
vfunc_get_preferred_height(_forWidth) {
|
2012-11-30 15:16:48 +00:00
|
|
|
let maxChildMin = 0;
|
|
|
|
let maxChildNat = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._items.length; i++) {
|
|
|
|
let [childMin, childNat] = this._items[i].get_preferred_height(-1);
|
|
|
|
maxChildMin = Math.max(childMin, maxChildMin);
|
|
|
|
maxChildNat = Math.max(childNat, maxChildNat);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._squareItems) {
|
2019-02-01 13:41:55 +00:00
|
|
|
let [childMin] = this._maxChildWidth(-1);
|
2012-11-30 15:16:48 +00:00
|
|
|
maxChildMin = Math.max(childMin, maxChildMin);
|
|
|
|
maxChildNat = maxChildMin;
|
|
|
|
}
|
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
let themeNode = this.get_theme_node();
|
|
|
|
return themeNode.adjust_preferred_height(maxChildMin, maxChildNat);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2020-05-09 19:30:26 +00:00
|
|
|
vfunc_allocate(box) {
|
|
|
|
this.set_allocation(box);
|
2018-06-28 15:34:01 +00:00
|
|
|
|
|
|
|
let contentBox = this.get_theme_node().get_content_box(box);
|
|
|
|
let width = contentBox.x2 - contentBox.x1;
|
|
|
|
let height = contentBox.y2 - contentBox.y1;
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2018-06-28 15:34:01 +00:00
|
|
|
let leftPadding = this.get_theme_node().get_padding(St.Side.LEFT);
|
|
|
|
let rightPadding = this.get_theme_node().get_padding(St.Side.RIGHT);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2019-11-29 00:11:16 +00:00
|
|
|
let [minListWidth] = this._list.get_preferred_width(height);
|
2012-11-30 15:16:48 +00:00
|
|
|
|
|
|
|
let childBox = new Clutter.ActorBox();
|
2019-11-29 00:11:16 +00:00
|
|
|
let scrollable = minListWidth > width;
|
2012-11-30 15:16:48 +00:00
|
|
|
|
2020-05-09 19:30:26 +00:00
|
|
|
this._scrollView.allocate(contentBox);
|
2018-06-28 15:34:01 +00:00
|
|
|
|
|
|
|
let arrowWidth = Math.floor(leftPadding / 3);
|
|
|
|
let arrowHeight = arrowWidth * 2;
|
|
|
|
childBox.x1 = leftPadding / 2;
|
|
|
|
childBox.y1 = this.height / 2 - arrowWidth;
|
|
|
|
childBox.x2 = childBox.x1 + arrowWidth;
|
|
|
|
childBox.y2 = childBox.y1 + arrowHeight;
|
2020-05-09 19:30:26 +00:00
|
|
|
this._leftArrow.allocate(childBox);
|
2019-08-19 19:38:51 +00:00
|
|
|
this._leftArrow.opacity = this._scrollableLeft && scrollable ? 255 : 0;
|
2018-06-28 15:34:01 +00:00
|
|
|
|
|
|
|
arrowWidth = Math.floor(rightPadding / 3);
|
|
|
|
arrowHeight = arrowWidth * 2;
|
|
|
|
childBox.x1 = this.width - arrowWidth - rightPadding / 2;
|
|
|
|
childBox.y1 = this.height / 2 - arrowWidth;
|
|
|
|
childBox.x2 = childBox.x1 + arrowWidth;
|
|
|
|
childBox.y2 = childBox.y1 + arrowHeight;
|
2020-05-09 19:30:26 +00:00
|
|
|
this._rightArrow.allocate(childBox);
|
2019-08-19 19:38:51 +00:00
|
|
|
this._rightArrow.opacity = this._scrollableRight && scrollable ? 255 : 0;
|
2012-11-30 15:16:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function drawArrow(area, side) {
|
|
|
|
let themeNode = area.get_theme_node();
|
|
|
|
let borderColor = themeNode.get_border_color(side);
|
|
|
|
let bodyColor = themeNode.get_foreground_color();
|
|
|
|
|
2019-08-19 17:55:49 +00:00
|
|
|
let [width, height] = area.get_surface_size();
|
2012-11-30 15:16:48 +00:00
|
|
|
let cr = area.get_context();
|
|
|
|
|
|
|
|
cr.setLineWidth(1.0);
|
|
|
|
Clutter.cairo_set_source_color(cr, borderColor);
|
|
|
|
|
|
|
|
switch (side) {
|
|
|
|
case St.Side.TOP:
|
|
|
|
cr.moveTo(0, height);
|
|
|
|
cr.lineTo(Math.floor(width * 0.5), 0);
|
|
|
|
cr.lineTo(width, height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case St.Side.BOTTOM:
|
|
|
|
cr.moveTo(width, 0);
|
|
|
|
cr.lineTo(Math.floor(width * 0.5), height);
|
|
|
|
cr.lineTo(0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case St.Side.LEFT:
|
|
|
|
cr.moveTo(width, height);
|
|
|
|
cr.lineTo(0, Math.floor(height * 0.5));
|
|
|
|
cr.lineTo(width, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case St.Side.RIGHT:
|
|
|
|
cr.moveTo(0, 0);
|
|
|
|
cr.lineTo(width, Math.floor(height * 0.5));
|
|
|
|
cr.lineTo(0, height);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cr.strokePreserve();
|
|
|
|
|
|
|
|
Clutter.cairo_set_source_color(cr, bodyColor);
|
|
|
|
cr.fill();
|
2013-01-07 20:07:40 +00:00
|
|
|
cr.$dispose();
|
2012-11-30 15:16:48 +00:00
|
|
|
}
|
|
|
|
|