2012-11-30 10:16:48 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2019-02-08 22:21:36 -05:00
|
|
|
const { Clutter, GLib, GObject, Meta, St } = imports.gi;
|
2012-11-30 10:16:48 -05:00
|
|
|
const Mainloop = imports.mainloop;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const Tweener = imports.ui.tweener;
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var POPUP_DELAY_TIMEOUT = 150; // milliseconds
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var POPUP_SCROLL_TIME = 0.10; // seconds
|
|
|
|
var POPUP_FADE_OUT_TIME = 0.1; // seconds
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var DISABLE_HOVER_TIMEOUT = 500; // milliseconds
|
2017-06-08 08:20:56 -04:00
|
|
|
var NO_MODS_TIMEOUT = 1500; // milliseconds
|
2012-11-30 10:16:48 -05: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;
|
|
|
|
}
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var SwitcherPopup = GObject.registerClass(
|
|
|
|
class SwitcherPopup extends St.Widget {
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(items) {
|
2019-04-18 16:50:49 -04:00
|
|
|
if (this.constructor.name === SwitcherPopup.prototype.constructor.name)
|
|
|
|
throw new TypeError(`Cannot instantiate abstract class ${this.constructor.name}`);
|
2017-10-30 21:23:39 -04:00
|
|
|
|
|
|
|
super._init({ style_class: 'switcher-popup',
|
2018-08-13 19:14:55 -04:00
|
|
|
reactive: true,
|
|
|
|
visible: false });
|
|
|
|
|
2012-11-30 10:16:48 -05:00
|
|
|
this._switcherList = null;
|
|
|
|
|
|
|
|
this._items = items || [];
|
|
|
|
this._selectedIndex = 0;
|
|
|
|
|
2018-08-13 19:14:55 -04:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-08-13 19:14:55 -04:00
|
|
|
Main.uiGroup.add_actor(this);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
this._haveModal = false;
|
|
|
|
this._modifierMask = 0;
|
|
|
|
|
|
|
|
this._motionTimeoutId = 0;
|
|
|
|
this._initialDelayTimeoutId = 0;
|
2017-06-08 08:20:56 -04:00
|
|
|
this._noModsTimeoutId = 0;
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-10-10 16:46:26 -04:00
|
|
|
this.add_constraint(new Clutter.BindConstraint({
|
|
|
|
source: global.stage,
|
|
|
|
coordinate: Clutter.BindCoordinate.ALL,
|
|
|
|
}));
|
2018-08-23 12:35:43 -04:00
|
|
|
|
2012-11-30 10:16:48 -05:00
|
|
|
// Initially disable hover so we ignore the enter-event if
|
|
|
|
// the switcher appears underneath the current pointer location
|
|
|
|
this._disableHover();
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-08-13 19:14:55 -04:00
|
|
|
vfunc_allocate(box, flags) {
|
|
|
|
this.set_allocation(box, flags);
|
|
|
|
|
2012-11-30 10:16:48 -05:00
|
|
|
let childBox = new Clutter.ActorBox();
|
|
|
|
let primary = Main.layoutManager.primaryMonitor;
|
|
|
|
|
2018-08-13 19:14:55 -04: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 10:16:48 -05:00
|
|
|
let hPadding = leftPadding + rightPadding;
|
|
|
|
|
|
|
|
// Allocate the switcherList
|
|
|
|
// We select a size based on an icon size that does not overflow the screen
|
2018-06-28 11:34:01 -04:00
|
|
|
let [childMinHeight, childNaturalHeight] = this._switcherList.get_preferred_height(primary.width - hPadding);
|
|
|
|
let [childMinWidth, childNaturalWidth] = this._switcherList.get_preferred_width(childNaturalHeight);
|
2012-11-30 10:16:48 -05: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;
|
2018-06-28 11:34:01 -04:00
|
|
|
this._switcherList.allocate(childBox, flags);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_initialSelection(backward, binding) {
|
2014-09-03 11:17:26 -04:00
|
|
|
if (backward)
|
|
|
|
this._select(this._items.length - 1);
|
|
|
|
else if (this._items.length == 1)
|
|
|
|
this._select(0);
|
|
|
|
else
|
|
|
|
this._select(1);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
show(backward, binding, mask) {
|
2014-09-03 11:15:31 -04:00
|
|
|
if (this._items.length == 0)
|
2012-11-30 10:16:48 -05:00
|
|
|
return false;
|
|
|
|
|
2018-08-13 19:14:55 -04:00
|
|
|
if (!Main.pushModal(this)) {
|
2012-11-30 10:16:48 -05:00
|
|
|
// Probably someone else has a pointer grab, try again with keyboard only
|
2018-08-13 19:14:55 -04:00
|
|
|
if (!Main.pushModal(this, { options: Meta.ModalOptions.POINTER_ALREADY_GRABBED }))
|
2012-11-30 10:16:48 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._haveModal = true;
|
|
|
|
this._modifierMask = primaryModifier(mask);
|
|
|
|
|
2018-08-13 19:14:55 -04:00
|
|
|
this.connect('key-press-event', this._keyPressEvent.bind(this));
|
|
|
|
this.connect('key-release-event', this._keyReleaseEvent.bind(this));
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-08-13 19:14:55 -04:00
|
|
|
this.connect('button-press-event', this._clickedOutside.bind(this));
|
|
|
|
this.connect('scroll-event', this._scrollEvent.bind(this));
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
this.add_actor(this._switcherList);
|
2017-12-01 19:27:35 -05: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 10:16:48 -05:00
|
|
|
|
|
|
|
// Need to force an allocation so we can figure out whether we
|
|
|
|
// need to scroll when selecting
|
2018-08-13 19:14:55 -04:00
|
|
|
this.opacity = 0;
|
|
|
|
this.visible = true;
|
|
|
|
this.get_allocation_box();
|
2012-11-30 10:16:48 -05: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 08:20:56 -04:00
|
|
|
if (this._modifierMask) {
|
|
|
|
let [x, y, mods] = global.get_pointer();
|
|
|
|
if (!(mods & this._modifierMask)) {
|
|
|
|
this._finish(global.get_current_time());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._resetNoModsTimeout();
|
2012-11-30 10:16:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We delay showing the popup so that fast Alt+Tab users aren't
|
|
|
|
// disturbed by the popup briefly flashing.
|
|
|
|
this._initialDelayTimeoutId = Mainloop.timeout_add(POPUP_DELAY_TIMEOUT,
|
2017-10-30 20:38:18 -04:00
|
|
|
() => {
|
2014-04-17 04:23:30 -04:00
|
|
|
Main.osdWindowManager.hideAll();
|
2018-08-13 19:14:55 -04:00
|
|
|
this.opacity = 255;
|
2012-11-30 10:16:48 -05:00
|
|
|
this._initialDelayTimeoutId = 0;
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(this._initialDelayTimeoutId, '[gnome-shell] Main.osdWindow.cancel');
|
2012-11-30 10:16:48 -05:00
|
|
|
return true;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_next() {
|
2012-11-30 10:16:48 -05:00
|
|
|
return mod(this._selectedIndex + 1, this._items.length);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_previous() {
|
2012-11-30 10:16:48 -05:00
|
|
|
return mod(this._selectedIndex - 1, this._items.length);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_keyPressHandler(keysym, action) {
|
2012-11-30 10:16:48 -05:00
|
|
|
throw new Error('Not implemented');
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_keyPressEvent(actor, event) {
|
2012-11-30 10:16:48 -05:00
|
|
|
let keysym = event.get_key_symbol();
|
2014-08-12 11:55:22 -04:00
|
|
|
let action = global.display.get_keybinding_action(event.get_key_code(), event.get_state());
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
this._disableHover();
|
|
|
|
|
2014-08-12 11:55:22 -04:00
|
|
|
if (this._keyPressHandler(keysym, action) != Clutter.EVENT_PROPAGATE)
|
2014-05-26 09:27:16 -04:00
|
|
|
return Clutter.EVENT_STOP;
|
|
|
|
|
2018-05-29 00:52:24 -04: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
|
|
|
|
if (keysym == Clutter.Escape || keysym == Clutter.Tab)
|
2018-08-13 19:05:50 -04:00
|
|
|
this.fadeAndDestroy();
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_keyReleaseEvent(actor, event) {
|
2017-06-08 08:20:56 -04:00
|
|
|
if (this._modifierMask) {
|
|
|
|
let [x, y, mods] = global.get_pointer();
|
|
|
|
let state = mods & this._modifierMask;
|
|
|
|
|
|
|
|
if (state == 0)
|
|
|
|
this._finish(event.get_time());
|
|
|
|
} else {
|
|
|
|
this._resetNoModsTimeout();
|
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_STOP;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_clickedOutside(actor, event) {
|
2018-08-13 19:05:50 -04:00
|
|
|
this.fadeAndDestroy();
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_scrollHandler(direction) {
|
2012-11-30 10:16:48 -05:00
|
|
|
if (direction == Clutter.ScrollDirection.UP)
|
|
|
|
this._select(this._previous());
|
|
|
|
else if (direction == Clutter.ScrollDirection.DOWN)
|
|
|
|
this._select(this._next());
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_scrollEvent(actor, event) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._scrollHandler(event.get_scroll_direction());
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemActivatedHandler(n) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._select(n);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemActivated(switcher, n) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._itemActivatedHandler(n);
|
2012-12-04 13:45:52 -05:00
|
|
|
this._finish(global.get_current_time());
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemEnteredHandler(n) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._select(n);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemEntered(switcher, n) {
|
2012-11-30 10:16:48 -05:00
|
|
|
if (!this.mouseActive)
|
|
|
|
return;
|
|
|
|
this._itemEnteredHandler(n);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemRemovedHandler(n) {
|
2018-02-08 06:11:11 -05:00
|
|
|
if (this._items.length > 0) {
|
|
|
|
let newIndex = Math.min(n, this._items.length - 1);
|
|
|
|
this._select(newIndex);
|
|
|
|
} else {
|
2018-08-13 19:05:50 -04:00
|
|
|
this.fadeAndDestroy();
|
2018-02-08 06:11:11 -05:00
|
|
|
}
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2018-02-08 06:11:11 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemRemoved(switcher, n) {
|
2018-02-08 06:11:11 -05:00
|
|
|
this._itemRemovedHandler(n);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2018-02-08 06:11:11 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_disableHover() {
|
2012-11-30 10:16:48 -05:00
|
|
|
this.mouseActive = false;
|
|
|
|
|
|
|
|
if (this._motionTimeoutId != 0)
|
|
|
|
Mainloop.source_remove(this._motionTimeoutId);
|
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this._motionTimeoutId = Mainloop.timeout_add(DISABLE_HOVER_TIMEOUT, this._mouseTimedOut.bind(this));
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(this._motionTimeoutId, '[gnome-shell] this._mouseTimedOut');
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_mouseTimedOut() {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._motionTimeoutId = 0;
|
|
|
|
this.mouseActive = true;
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_resetNoModsTimeout() {
|
2017-06-08 08:20:56 -04:00
|
|
|
if (this._noModsTimeoutId != 0)
|
|
|
|
Mainloop.source_remove(this._noModsTimeoutId);
|
|
|
|
|
|
|
|
this._noModsTimeoutId = Mainloop.timeout_add(NO_MODS_TIMEOUT,
|
2017-10-30 20:38:18 -04:00
|
|
|
() => {
|
2017-06-08 08:20:56 -04:00
|
|
|
this._finish(global.get_current_time());
|
|
|
|
this._noModsTimeoutId = 0;
|
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-30 20:38:18 -04:00
|
|
|
});
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2017-06-08 08:20:56 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_popModal() {
|
2012-11-30 10:16:48 -05:00
|
|
|
if (this._haveModal) {
|
2018-08-13 19:14:55 -04:00
|
|
|
Main.popModal(this);
|
2012-11-30 10:16:48 -05:00
|
|
|
this._haveModal = false;
|
|
|
|
}
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-08-13 19:05:50 -04:00
|
|
|
fadeAndDestroy() {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._popModal();
|
2018-08-13 19:14:55 -04:00
|
|
|
if (this.visible) {
|
|
|
|
Tweener.addTween(this,
|
2012-11-30 10:16:48 -05:00
|
|
|
{ opacity: 0,
|
|
|
|
time: POPUP_FADE_OUT_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2017-10-30 20:38:18 -04:00
|
|
|
onComplete: () => {
|
2018-08-13 19:14:55 -04:00
|
|
|
this.destroy();
|
2017-10-30 20:38:18 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
});
|
2018-08-13 19:14:55 -04:00
|
|
|
} else {
|
|
|
|
this.destroy();
|
|
|
|
}
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_finish(timestamp) {
|
2018-08-13 19:05:50 -04:00
|
|
|
this.fadeAndDestroy();
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDestroy() {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._popModal();
|
|
|
|
|
|
|
|
if (this._motionTimeoutId != 0)
|
|
|
|
Mainloop.source_remove(this._motionTimeoutId);
|
|
|
|
if (this._initialDelayTimeoutId != 0)
|
|
|
|
Mainloop.source_remove(this._initialDelayTimeoutId);
|
2017-06-08 08:20:56 -04:00
|
|
|
if (this._noModsTimeoutId != 0)
|
|
|
|
Mainloop.source_remove(this._noModsTimeoutId);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_select(num) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._selectedIndex = num;
|
|
|
|
this._switcherList.highlight(num);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var SwitcherButton = GObject.registerClass(
|
|
|
|
class SwitcherButton extends St.Button {
|
2018-06-28 11:34:01 -04:00
|
|
|
_init(square) {
|
2017-10-30 21:23:39 -04:00
|
|
|
super._init({ style_class: 'item-box',
|
2018-06-28 11:34:01 -04:00
|
|
|
reactive: true });
|
|
|
|
|
|
|
|
this._square = square;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2018-06-28 11:34:01 -04:00
|
|
|
|
|
|
|
vfunc_get_preferred_width(forHeight) {
|
|
|
|
if (this._square)
|
|
|
|
return this.get_preferred_height(-1);
|
|
|
|
else
|
2017-10-30 21:23:39 -04:00
|
|
|
return super.vfunc_get_preferred_width(forHeight);
|
2018-06-28 11:34:01 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var SwitcherList = GObject.registerClass({
|
2018-06-28 11:34:01 -04: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-30 21:23:39 -04:00
|
|
|
}, class SwitcherList extends St.Widget {
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(squareItems) {
|
2017-10-30 21:23:39 -04:00
|
|
|
super._init({ style_class: 'switcher-list' });
|
2018-06-28 11:34:01 -04:00
|
|
|
|
|
|
|
this._list = new St.BoxLayout({ style_class: 'switcher-list-item-container',
|
|
|
|
vertical: false,
|
|
|
|
x_expand: true,
|
|
|
|
y_expand: true });
|
|
|
|
|
|
|
|
let layoutManager = this._list.get_layout_manager();
|
|
|
|
|
2012-11-30 10:16:48 -05:00
|
|
|
this._list.spacing = 0;
|
2017-10-30 20:38:18 -04:00
|
|
|
this._list.connect('style-changed', () => {
|
|
|
|
this._list.spacing = this._list.get_theme_node().get_length('spacing');
|
|
|
|
});
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
this._scrollView = new St.ScrollView({ style_class: 'hfade',
|
|
|
|
enable_mouse_scrolling: false });
|
2018-11-27 07:45:36 -05:00
|
|
|
this._scrollView.set_policy(St.PolicyType.NEVER, St.PolicyType.NEVER);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
this._scrollView.add_actor(this._list);
|
|
|
|
this.add_actor(this._scrollView);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
// Those arrows indicate whether scrolling in one direction is possible
|
|
|
|
this._leftArrow = new St.DrawingArea({ style_class: 'switcher-arrow',
|
|
|
|
pseudo_class: 'highlighted' });
|
2017-10-30 20:38:18 -04:00
|
|
|
this._leftArrow.connect('repaint', () => {
|
|
|
|
drawArrow(this._leftArrow, St.Side.LEFT);
|
|
|
|
});
|
2012-11-30 10:16:48 -05:00
|
|
|
this._rightArrow = new St.DrawingArea({ style_class: 'switcher-arrow',
|
|
|
|
pseudo_class: 'highlighted' });
|
2017-10-30 20:38:18 -04:00
|
|
|
this._rightArrow.connect('repaint', () => {
|
|
|
|
drawArrow(this._rightArrow, St.Side.RIGHT);
|
|
|
|
});
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
this.add_actor(this._leftArrow);
|
|
|
|
this.add_actor(this._rightArrow);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
this._items = [];
|
|
|
|
this._highlighted = -1;
|
|
|
|
this._squareItems = squareItems;
|
|
|
|
this._scrollableRight = true;
|
|
|
|
this._scrollableLeft = false;
|
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
layoutManager.homogeneous = squareItems;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
addItem(item, label) {
|
2018-06-28 11:34:01 -04:00
|
|
|
let bbox = new SwitcherButton(this._squareItems);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
bbox.set_child(item);
|
|
|
|
this._list.add_actor(bbox);
|
|
|
|
|
|
|
|
let n = this._items.length;
|
2017-10-30 20:38:18 -04:00
|
|
|
bbox.connect('clicked', () => { this._onItemClicked(n); });
|
|
|
|
bbox.connect('motion-event', () => this._onItemEnter(n));
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
bbox.label_actor = label;
|
|
|
|
|
|
|
|
this._items.push(bbox);
|
|
|
|
|
|
|
|
return bbox;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
removeItem(index) {
|
2018-02-08 05:38:04 -05:00
|
|
|
let item = this._items.splice(index, 1);
|
|
|
|
item[0].destroy();
|
2018-02-08 06:11:11 -05:00
|
|
|
this.emit('item-removed', index);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2018-02-08 05:38:04 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onItemClicked(index) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._itemActivated(index);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onItemEnter(index) {
|
2017-02-28 07:35:57 -05:00
|
|
|
// Avoid reentrancy
|
|
|
|
if (index != this._currentItemEntered) {
|
|
|
|
this._currentItemEntered = index;
|
|
|
|
this._itemEntered(index);
|
|
|
|
}
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
highlight(index, justOutline) {
|
2018-02-08 05:38:04 -05:00
|
|
|
if (this._items[this._highlighted]) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this._items[this._highlighted].remove_style_pseudo_class('outlined');
|
|
|
|
this._items[this._highlighted].remove_style_pseudo_class('selected');
|
|
|
|
}
|
|
|
|
|
2018-02-08 06:11:11 -05:00
|
|
|
if (this._items[index]) {
|
2012-11-30 10:16:48 -05:00
|
|
|
if (justOutline)
|
2018-02-08 06:11:11 -05:00
|
|
|
this._items[index].add_style_pseudo_class('outlined');
|
2012-11-30 10:16:48 -05:00
|
|
|
else
|
2018-02-08 06:11:11 -05:00
|
|
|
this._items[index].add_style_pseudo_class('selected');
|
2012-11-30 10:16:48 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 06:11:11 -05:00
|
|
|
this._highlighted = index;
|
|
|
|
|
2012-11-30 10:16:48 -05:00
|
|
|
let adjustment = this._scrollView.hscroll.adjustment;
|
|
|
|
let [value, lower, upper, stepIncrement, pageIncrement, pageSize] = adjustment.get_values();
|
|
|
|
let [absItemX, absItemY] = this._items[index].get_transformed_position();
|
2018-06-28 11:34:01 -04:00
|
|
|
let [result, posX, posY] = this.transform_stage_point(absItemX, 0);
|
|
|
|
let [containerWidth, containerHeight] = this.get_transformed_size();
|
2012-11-30 10:16:48 -05:00
|
|
|
if (posX + this._items[index].get_width() > containerWidth)
|
|
|
|
this._scrollToRight();
|
|
|
|
else if (this._items[index].allocation.x1 - value < 0)
|
|
|
|
this._scrollToLeft();
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_scrollToLeft() {
|
2012-11-30 10:16:48 -05:00
|
|
|
let adjustment = this._scrollView.hscroll.adjustment;
|
|
|
|
let [value, lower, upper, stepIncrement, pageIncrement, pageSize] = adjustment.get_values();
|
|
|
|
|
|
|
|
let item = this._items[this._highlighted];
|
|
|
|
|
|
|
|
if (item.allocation.x1 < value)
|
|
|
|
value = Math.min(0, item.allocation.x1);
|
|
|
|
else if (item.allocation.x2 > value + pageSize)
|
|
|
|
value = Math.max(upper, item.allocation.x2 - pageSize);
|
|
|
|
|
|
|
|
this._scrollableRight = true;
|
|
|
|
Tweener.addTween(adjustment,
|
|
|
|
{ value: value,
|
|
|
|
time: POPUP_SCROLL_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2017-10-30 20:38:18 -04:00
|
|
|
onComplete: () => {
|
2014-01-13 12:43:01 -05:00
|
|
|
if (this._highlighted == 0)
|
2012-11-30 10:16:48 -05:00
|
|
|
this._scrollableLeft = false;
|
2018-06-28 11:34:01 -04:00
|
|
|
this.queue_relayout();
|
2017-10-30 20:38:18 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
});
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_scrollToRight() {
|
2012-11-30 10:16:48 -05:00
|
|
|
let adjustment = this._scrollView.hscroll.adjustment;
|
|
|
|
let [value, lower, upper, stepIncrement, pageIncrement, pageSize] = adjustment.get_values();
|
|
|
|
|
|
|
|
let item = this._items[this._highlighted];
|
|
|
|
|
|
|
|
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;
|
|
|
|
Tweener.addTween(adjustment,
|
|
|
|
{ value: value,
|
|
|
|
time: POPUP_SCROLL_TIME,
|
|
|
|
transition: 'easeOutQuad',
|
2017-10-30 20:38:18 -04:00
|
|
|
onComplete: () => {
|
2014-01-13 12:43:01 -05:00
|
|
|
if (this._highlighted == this._items.length - 1)
|
2012-11-30 10:16:48 -05:00
|
|
|
this._scrollableRight = false;
|
2018-06-28 11:34:01 -04:00
|
|
|
this.queue_relayout();
|
2017-10-30 20:38:18 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
});
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemActivated(n) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this.emit('item-activated', n);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_itemEntered(n) {
|
2012-11-30 10:16:48 -05:00
|
|
|
this.emit('item-entered', n);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_maxChildWidth(forHeight) {
|
2012-11-30 10:16:48 -05: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) {
|
|
|
|
let [childMin, childNat] = this._items[i].get_preferred_height(-1);
|
|
|
|
maxChildMin = Math.max(childMin, maxChildMin);
|
|
|
|
maxChildNat = Math.max(childNat, maxChildNat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [maxChildMin, maxChildNat];
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
vfunc_get_preferred_width(forHeight) {
|
|
|
|
let themeNode = this.get_theme_node();
|
|
|
|
let [maxChildMin, ] = this._maxChildWidth(forHeight);
|
|
|
|
let [minListWidth, ] = this._list.get_preferred_width(forHeight);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
return themeNode.adjust_preferred_width(maxChildMin, minListWidth);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
vfunc_get_preferred_height(forWidth) {
|
2012-11-30 10:16:48 -05: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) {
|
|
|
|
let [childMin, childNat] = this._maxChildWidth(-1);
|
|
|
|
maxChildMin = Math.max(childMin, maxChildMin);
|
|
|
|
maxChildNat = maxChildMin;
|
|
|
|
}
|
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
let themeNode = this.get_theme_node();
|
|
|
|
return themeNode.adjust_preferred_height(maxChildMin, maxChildNat);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
vfunc_allocate(box, flags) {
|
|
|
|
this.set_allocation(box, flags);
|
|
|
|
|
|
|
|
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 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04: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 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
let [, natScrollViewWidth] = this._scrollView.get_preferred_width(height);
|
2012-11-30 10:16:48 -05:00
|
|
|
|
|
|
|
let childBox = new Clutter.ActorBox();
|
2018-06-28 11:34:01 -04:00
|
|
|
let scrollable = natScrollViewWidth > width;
|
2012-11-30 10:16:48 -05:00
|
|
|
|
2018-06-28 11:34:01 -04:00
|
|
|
this._scrollView.allocate(contentBox, flags);
|
|
|
|
|
|
|
|
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;
|
|
|
|
this._leftArrow.allocate(childBox, flags);
|
|
|
|
this._leftArrow.opacity = (this._scrollableLeft && scrollable) ? 255 : 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
this._rightArrow.allocate(childBox, flags);
|
|
|
|
this._rightArrow.opacity = (this._scrollableRight && scrollable) ? 255 : 0;
|
2012-11-30 10:16:48 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function drawArrow(area, side) {
|
|
|
|
let themeNode = area.get_theme_node();
|
|
|
|
let borderColor = themeNode.get_border_color(side);
|
|
|
|
let bodyColor = themeNode.get_foreground_color();
|
|
|
|
|
|
|
|
let [width, height] = area.get_surface_size ();
|
|
|
|
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 15:07:40 -05:00
|
|
|
cr.$dispose();
|
2012-11-30 10:16:48 -05:00
|
|
|
}
|
|
|
|
|