2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2020-05-19 09:43:10 -04:00
|
|
|
/* exported BaseIcon, IconGrid, IconGridLayout */
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2021-03-02 05:47:13 -05:00
|
|
|
const { Clutter, GLib, GObject, Meta, Shell, St } = imports.gi;
|
2010-07-20 22:22:19 -04:00
|
|
|
|
|
|
|
const Params = imports.misc.params;
|
2014-06-17 06:47:00 -04:00
|
|
|
const Main = imports.ui.main;
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var ICON_SIZE = 96;
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
var PAGE_SWITCH_TIME = 300;
|
2014-06-17 06:47:00 -04:00
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
var IconSize = {
|
|
|
|
LARGE: 96,
|
|
|
|
MEDIUM: 64,
|
2022-05-01 22:41:45 -04:00
|
|
|
MEDIUM_SMALL: 48,
|
2020-05-19 09:07:24 -04:00
|
|
|
SMALL: 32,
|
2022-05-01 22:41:45 -04:00
|
|
|
SMALLER: 24,
|
2020-05-19 09:07:24 -04:00
|
|
|
TINY: 16,
|
|
|
|
};
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var APPICON_ANIMATION_OUT_SCALE = 3;
|
2019-08-01 19:13:10 -04:00
|
|
|
var APPICON_ANIMATION_OUT_TIME = 250;
|
2014-06-17 15:31:53 -04:00
|
|
|
|
2020-06-24 11:54:52 -04:00
|
|
|
const ICON_POSITION_DELAY = 10;
|
2019-12-04 14:34:34 -05:00
|
|
|
|
2020-05-19 13:19:05 -04:00
|
|
|
const defaultGridModes = [
|
|
|
|
{
|
|
|
|
rows: 8,
|
|
|
|
columns: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
rows: 6,
|
|
|
|
columns: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
rows: 4,
|
|
|
|
columns: 6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
rows: 3,
|
|
|
|
columns: 8,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-05-25 14:58:39 -04:00
|
|
|
var LEFT_DIVIDER_LEEWAY = 20;
|
|
|
|
var RIGHT_DIVIDER_LEEWAY = 20;
|
|
|
|
|
|
|
|
var DragLocation = {
|
|
|
|
INVALID: 0,
|
|
|
|
START_EDGE: 1,
|
|
|
|
ON_ICON: 2,
|
|
|
|
END_EDGE: 3,
|
|
|
|
EMPTY_SPACE: 4,
|
|
|
|
};
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var BaseIcon = GObject.registerClass(
|
2021-03-02 05:47:13 -05:00
|
|
|
class BaseIcon extends Shell.SquareBin {
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(label, params) {
|
2020-10-21 11:58:29 -04:00
|
|
|
params = Params.parse(params, {
|
|
|
|
createIcon: null,
|
|
|
|
setSizeManually: false,
|
|
|
|
showLabel: true,
|
|
|
|
});
|
2013-08-31 09:05:53 -04:00
|
|
|
|
|
|
|
let styleClass = 'overview-icon';
|
|
|
|
if (params.showLabel)
|
|
|
|
styleClass += ' overview-icon-with-label';
|
|
|
|
|
2019-10-17 17:40:24 -04:00
|
|
|
super._init({ style_class: styleClass });
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2019-10-17 17:40:24 -04:00
|
|
|
this._box = new St.BoxLayout({
|
|
|
|
vertical: true,
|
|
|
|
x_expand: true,
|
|
|
|
y_expand: true,
|
|
|
|
});
|
2018-06-30 14:00:08 -04:00
|
|
|
this.set_child(this._box);
|
2010-07-20 22:22:19 -04:00
|
|
|
|
|
|
|
this.iconSize = ICON_SIZE;
|
2019-12-20 16:47:01 -05:00
|
|
|
this._iconBin = new St.Bin({ x_align: Clutter.ActorAlign.CENTER });
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2018-06-30 14:00:08 -04:00
|
|
|
this._box.add_actor(this._iconBin);
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2011-01-20 09:25:25 -05:00
|
|
|
if (params.showLabel) {
|
2011-03-08 13:33:57 -05:00
|
|
|
this.label = new St.Label({ text: label });
|
2019-07-03 12:27:21 -04:00
|
|
|
this.label.clutter_text.set({
|
|
|
|
x_align: Clutter.ActorAlign.CENTER,
|
2019-08-20 17:43:54 -04:00
|
|
|
y_align: Clutter.ActorAlign.CENTER,
|
2019-07-03 12:27:21 -04:00
|
|
|
});
|
2018-06-30 14:00:08 -04:00
|
|
|
this._box.add_actor(this.label);
|
2011-01-20 09:25:25 -05:00
|
|
|
} else {
|
2011-03-08 13:33:57 -05:00
|
|
|
this.label = null;
|
2011-01-20 09:25:25 -05:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2010-08-30 08:59:45 -04:00
|
|
|
if (params.createIcon)
|
|
|
|
this.createIcon = params.createIcon;
|
|
|
|
this._setSizeManually = params.setSizeManually;
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2011-03-02 18:25:23 -05:00
|
|
|
this.icon = null;
|
2012-11-30 20:35:04 -05:00
|
|
|
|
|
|
|
let cache = St.TextureCache.get_default();
|
2021-08-15 18:36:59 -04:00
|
|
|
cache.connectObject(
|
|
|
|
'icon-theme-changed', this._onIconThemeChanged.bind(this), this);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
|
|
|
// This can be overridden by a subclass, or by the createIcon
|
|
|
|
// parameter to _init()
|
2019-01-31 09:08:10 -05:00
|
|
|
createIcon(_size) {
|
2019-05-21 16:28:07 -04:00
|
|
|
throw new GObject.NotImplementedError(`createIcon in ${this.constructor.name}`);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
setIconSize(size) {
|
2010-08-30 08:59:45 -04:00
|
|
|
if (!this._setSizeManually)
|
|
|
|
throw new Error('setSizeManually has to be set to use setIconsize');
|
|
|
|
|
2020-10-21 11:58:29 -04:00
|
|
|
if (size === this.iconSize)
|
2010-08-30 08:59:45 -04:00
|
|
|
return;
|
|
|
|
|
2011-03-02 18:25:23 -05:00
|
|
|
this._createIconTexture(size);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2011-03-02 18:25:23 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_createIconTexture(size) {
|
2011-03-02 18:25:23 -05:00
|
|
|
if (this.icon)
|
|
|
|
this.icon.destroy();
|
2010-08-30 08:59:45 -04:00
|
|
|
this.iconSize = size;
|
|
|
|
this.icon = this.createIcon(this.iconSize);
|
2011-02-10 10:30:28 -05:00
|
|
|
|
2011-12-20 13:11:07 -05:00
|
|
|
this._iconBin.child = this.icon;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-08-30 08:59:45 -04:00
|
|
|
|
2018-06-30 14:00:08 -04:00
|
|
|
vfunc_style_changed() {
|
2019-03-27 12:50:20 -04:00
|
|
|
super.vfunc_style_changed();
|
2018-06-30 14:00:08 -04:00
|
|
|
let node = this.get_theme_node();
|
2010-08-30 08:59:36 -04:00
|
|
|
|
2011-03-02 18:25:23 -05:00
|
|
|
let size;
|
|
|
|
if (this._setSizeManually) {
|
|
|
|
size = this.iconSize;
|
|
|
|
} else {
|
2020-04-03 14:28:57 -04:00
|
|
|
const { scaleFactor } =
|
|
|
|
St.ThemeContext.get_for_stage(global.stage);
|
|
|
|
|
2011-03-02 18:25:23 -05:00
|
|
|
let [found, len] = node.lookup_length('icon-size', false);
|
2020-04-03 14:28:57 -04:00
|
|
|
size = found ? len / scaleFactor : ICON_SIZE;
|
2011-03-02 18:25:23 -05:00
|
|
|
}
|
2010-08-30 08:59:36 -04:00
|
|
|
|
2020-10-21 11:58:29 -04:00
|
|
|
if (this.iconSize === size && this._iconBin.child)
|
2012-11-30 20:35:04 -05:00
|
|
|
return;
|
|
|
|
|
2011-03-02 18:25:23 -05:00
|
|
|
this._createIconTexture(size);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2012-11-30 20:35:04 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onIconThemeChanged() {
|
2012-11-30 20:35:04 -05:00
|
|
|
this._createIconTexture(this.iconSize);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2014-06-17 15:31:53 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
animateZoomOut() {
|
2014-06-17 15:31:53 -04:00
|
|
|
// Animate only the child instead of the entire actor, so the
|
|
|
|
// styles like hover and running are not applied while
|
|
|
|
// animating.
|
2018-06-30 14:00:08 -04:00
|
|
|
zoomOutActor(this.child);
|
2010-07-20 22:22:19 -04:00
|
|
|
}
|
2019-06-28 18:49:18 -04:00
|
|
|
|
2019-09-15 05:40:27 -04:00
|
|
|
animateZoomOutAtPos(x, y) {
|
|
|
|
zoomOutActorAtPos(this.child, x, y);
|
|
|
|
}
|
|
|
|
|
2019-06-28 18:49:18 -04:00
|
|
|
update() {
|
|
|
|
this._createIconTexture(this.iconSize);
|
|
|
|
}
|
2011-11-20 11:07:14 -05:00
|
|
|
});
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2014-06-17 15:31:53 -04:00
|
|
|
function zoomOutActor(actor) {
|
2019-09-15 05:40:27 -04:00
|
|
|
let [x, y] = actor.get_transformed_position();
|
|
|
|
zoomOutActorAtPos(actor, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
function zoomOutActorAtPos(actor, x, y) {
|
2021-08-12 21:21:47 -04:00
|
|
|
const monitor = Main.layoutManager.findMonitorForActor(actor);
|
|
|
|
if (!monitor)
|
|
|
|
return;
|
|
|
|
|
2020-10-21 11:58:29 -04:00
|
|
|
const actorClone = new Clutter.Clone({
|
|
|
|
source: actor,
|
|
|
|
reactive: false,
|
|
|
|
});
|
2014-06-17 15:31:53 -04:00
|
|
|
let [width, height] = actor.get_transformed_size();
|
2019-09-15 05:40:27 -04:00
|
|
|
|
2014-06-17 15:31:53 -04:00
|
|
|
actorClone.set_size(width, height);
|
|
|
|
actorClone.set_position(x, y);
|
|
|
|
actorClone.opacity = 255;
|
|
|
|
actorClone.set_pivot_point(0.5, 0.5);
|
|
|
|
|
|
|
|
Main.uiGroup.add_actor(actorClone);
|
|
|
|
|
|
|
|
// Avoid monitor edges to not zoom outside the current monitor
|
|
|
|
let scaledWidth = width * APPICON_ANIMATION_OUT_SCALE;
|
|
|
|
let scaledHeight = height * APPICON_ANIMATION_OUT_SCALE;
|
|
|
|
let scaledX = x - (scaledWidth - width) / 2;
|
|
|
|
let scaledY = y - (scaledHeight - height) / 2;
|
2020-06-01 23:41:43 -04:00
|
|
|
let containedX = Math.clamp(scaledX, monitor.x, monitor.x + monitor.width - scaledWidth);
|
|
|
|
let containedY = Math.clamp(scaledY, monitor.y, monitor.y + monitor.height - scaledHeight);
|
2014-06-17 15:31:53 -04:00
|
|
|
|
2018-07-20 15:46:19 -04:00
|
|
|
actorClone.ease({
|
|
|
|
scale_x: APPICON_ANIMATION_OUT_SCALE,
|
|
|
|
scale_y: APPICON_ANIMATION_OUT_SCALE,
|
|
|
|
translation_x: containedX - scaledX,
|
|
|
|
translation_y: containedY - scaledY,
|
|
|
|
opacity: 0,
|
|
|
|
duration: APPICON_ANIMATION_OUT_TIME,
|
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
|
2019-08-20 17:43:54 -04:00
|
|
|
onComplete: () => actorClone.destroy(),
|
2018-07-20 15:46:19 -04:00
|
|
|
});
|
2014-06-17 15:31:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:30:26 -04:00
|
|
|
function animateIconPosition(icon, box, nChangedIcons) {
|
2020-02-07 04:23:11 -05:00
|
|
|
if (!icon.has_allocation() || icon.allocation.equal(box) || icon.opacity === 0) {
|
2020-05-09 15:30:26 -04:00
|
|
|
icon.allocate(box);
|
2019-12-04 14:34:34 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
icon.save_easing_state();
|
|
|
|
icon.set_easing_mode(Clutter.AnimationMode.EASE_OUT_QUAD);
|
|
|
|
icon.set_easing_delay(nChangedIcons * ICON_POSITION_DELAY);
|
|
|
|
|
2020-05-09 15:30:26 -04:00
|
|
|
icon.allocate(box);
|
2019-12-04 14:34:34 -05:00
|
|
|
|
|
|
|
icon.restore_easing_state();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
function swap(value, length) {
|
|
|
|
return length - value - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var IconGridLayout = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'allow-incomplete-pages': GObject.ParamSpec.boolean('allow-incomplete-pages',
|
|
|
|
'Allow incomplete pages', 'Allow incomplete pages',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
true),
|
|
|
|
'column-spacing': GObject.ParamSpec.int('column-spacing',
|
|
|
|
'Column spacing', 'Column spacing',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
0, GLib.MAXINT32, 0),
|
|
|
|
'columns-per-page': GObject.ParamSpec.int('columns-per-page',
|
|
|
|
'Columns per page', 'Columns per page',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
2021-01-29 17:42:30 -05:00
|
|
|
1, GLib.MAXINT32, 6),
|
2020-05-19 09:07:24 -04:00
|
|
|
'fixed-icon-size': GObject.ParamSpec.int('fixed-icon-size',
|
|
|
|
'Fixed icon size', 'Fixed icon size',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
-1, GLib.MAXINT32, -1),
|
|
|
|
'icon-size': GObject.ParamSpec.int('icon-size',
|
|
|
|
'Icon size', 'Icon size',
|
|
|
|
GObject.ParamFlags.READABLE,
|
|
|
|
0, GLib.MAXINT32, 0),
|
|
|
|
'last-row-align': GObject.ParamSpec.enum('last-row-align',
|
|
|
|
'Last row align', 'Last row align',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
Clutter.ActorAlign.$gtype,
|
|
|
|
Clutter.ActorAlign.FILL),
|
|
|
|
'max-column-spacing': GObject.ParamSpec.int('max-column-spacing',
|
|
|
|
'Maximum column spacing', 'Maximum column spacing',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
-1, GLib.MAXINT32, -1),
|
|
|
|
'max-row-spacing': GObject.ParamSpec.int('max-row-spacing',
|
|
|
|
'Maximum row spacing', 'Maximum row spacing',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
-1, GLib.MAXINT32, -1),
|
|
|
|
'orientation': GObject.ParamSpec.enum('orientation',
|
|
|
|
'Orientation', 'Orientation',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
Clutter.Orientation.$gtype,
|
|
|
|
Clutter.Orientation.VERTICAL),
|
|
|
|
'page-halign': GObject.ParamSpec.enum('page-halign',
|
|
|
|
'Horizontal page align',
|
|
|
|
'Horizontal page align',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
Clutter.ActorAlign.$gtype,
|
|
|
|
Clutter.ActorAlign.FILL),
|
2020-11-25 16:39:08 -05:00
|
|
|
'page-padding': GObject.ParamSpec.boxed('page-padding',
|
|
|
|
'Page padding', 'Page padding',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
Clutter.Margin.$gtype),
|
2020-05-19 09:07:24 -04:00
|
|
|
'page-valign': GObject.ParamSpec.enum('page-valign',
|
|
|
|
'Vertical page align',
|
|
|
|
'Vertical page align',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
Clutter.ActorAlign.$gtype,
|
|
|
|
Clutter.ActorAlign.FILL),
|
|
|
|
'row-spacing': GObject.ParamSpec.int('row-spacing',
|
|
|
|
'Row spacing', 'Row spacing',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
0, GLib.MAXINT32, 0),
|
|
|
|
'rows-per-page': GObject.ParamSpec.int('rows-per-page',
|
|
|
|
'Rows per page', 'Rows per page',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
2021-01-29 17:42:30 -05:00
|
|
|
1, GLib.MAXINT32, 4),
|
2020-05-19 09:07:24 -04:00
|
|
|
},
|
|
|
|
Signals: {
|
|
|
|
'pages-changed': {},
|
|
|
|
},
|
|
|
|
}, class IconGridLayout extends Clutter.LayoutManager {
|
|
|
|
_init(params = {}) {
|
2021-01-29 19:03:04 -05:00
|
|
|
this._orientation = params.orientation ?? Clutter.Orientation.VERTICAL;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
super._init(params);
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
if (!this.pagePadding)
|
|
|
|
this.pagePadding = new Clutter.Margin();
|
|
|
|
|
|
|
|
this._iconSize = this.fixedIconSize !== -1
|
|
|
|
? this.fixedIconSize
|
2020-05-19 09:07:24 -04:00
|
|
|
: IconSize.LARGE;
|
|
|
|
|
|
|
|
this._pageSizeChanged = false;
|
|
|
|
this._pageHeight = 0;
|
|
|
|
this._pageWidth = 0;
|
|
|
|
this._nPages = -1;
|
|
|
|
|
|
|
|
// [
|
|
|
|
// {
|
|
|
|
// children: [ itemData, itemData, itemData, ... ],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// children: [ itemData, itemData, itemData, ... ],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// children: [ itemData, itemData, itemData, ... ],
|
|
|
|
// },
|
|
|
|
// ]
|
|
|
|
this._pages = [];
|
|
|
|
|
|
|
|
// {
|
|
|
|
// item: {
|
|
|
|
// actor: Clutter.Actor,
|
|
|
|
// pageIndex: <index>,
|
|
|
|
// },
|
|
|
|
// item: {
|
|
|
|
// actor: Clutter.Actor,
|
|
|
|
// pageIndex: <index>,
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
this._items = new Map();
|
|
|
|
|
|
|
|
this._containerDestroyedId = 0;
|
|
|
|
this._updateIconSizesLaterId = 0;
|
2020-12-02 15:43:13 -05:00
|
|
|
|
2021-02-23 13:47:45 -05:00
|
|
|
this._childrenMaxSize = -1;
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_findBestIconSize() {
|
2021-01-29 19:03:04 -05:00
|
|
|
const nColumns = this.columnsPerPage;
|
|
|
|
const nRows = this.rowsPerPage;
|
|
|
|
const columnSpacingPerPage = this.columnSpacing * (nColumns - 1);
|
|
|
|
const rowSpacingPerPage = this.rowSpacing * (nRows - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
const [firstItem] = this._container;
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
if (this.fixedIconSize !== -1)
|
|
|
|
return this.fixedIconSize;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
const iconSizes = Object.values(IconSize).sort((a, b) => b - a);
|
|
|
|
for (const size of iconSizes) {
|
|
|
|
let usedWidth, usedHeight;
|
|
|
|
|
|
|
|
if (firstItem) {
|
|
|
|
firstItem.icon.setIconSize(size);
|
|
|
|
const [firstItemWidth, firstItemHeight] =
|
|
|
|
firstItem.get_preferred_size();
|
|
|
|
|
|
|
|
const itemSize = Math.max(firstItemWidth, firstItemHeight);
|
|
|
|
|
|
|
|
usedWidth = itemSize * nColumns;
|
|
|
|
usedHeight = itemSize * nRows;
|
|
|
|
} else {
|
|
|
|
usedWidth = size * nColumns;
|
|
|
|
usedHeight = size * nRows;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emptyHSpace =
|
2020-11-25 16:39:08 -05:00
|
|
|
this._pageWidth - usedWidth - columnSpacingPerPage -
|
2021-01-29 19:03:04 -05:00
|
|
|
this.pagePadding.left - this.pagePadding.right;
|
2020-05-19 09:07:24 -04:00
|
|
|
const emptyVSpace =
|
2020-11-25 16:39:08 -05:00
|
|
|
this._pageHeight - usedHeight - rowSpacingPerPage -
|
2021-01-29 19:03:04 -05:00
|
|
|
this.pagePadding.top - this.pagePadding.bottom;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2020-11-30 20:14:17 -05:00
|
|
|
if (emptyHSpace >= 0 && emptyVSpace > 0)
|
2020-12-01 15:58:09 -05:00
|
|
|
return size;
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:58:09 -05:00
|
|
|
return IconSize.TINY;
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_getChildrenMaxSize() {
|
2021-02-23 13:47:45 -05:00
|
|
|
if (this._childrenMaxSize === -1) {
|
|
|
|
let minWidth = 0;
|
|
|
|
let minHeight = 0;
|
|
|
|
|
2021-02-25 12:07:59 -05:00
|
|
|
const nPages = this._pages.length;
|
|
|
|
for (let pageIndex = 0; pageIndex < nPages; pageIndex++) {
|
|
|
|
const page = this._pages[pageIndex];
|
|
|
|
const nVisibleItems = page.visibleChildren.length;
|
|
|
|
for (let itemIndex = 0; itemIndex < nVisibleItems; itemIndex++) {
|
|
|
|
const item = page.visibleChildren[itemIndex];
|
|
|
|
|
|
|
|
const childMinHeight = item.get_preferred_height(-1)[0];
|
|
|
|
const childMinWidth = item.get_preferred_width(-1)[0];
|
|
|
|
|
|
|
|
minWidth = Math.max(minWidth, childMinWidth);
|
|
|
|
minHeight = Math.max(minHeight, childMinHeight);
|
|
|
|
}
|
2021-02-23 13:47:45 -05:00
|
|
|
}
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2021-02-23 13:47:45 -05:00
|
|
|
this._childrenMaxSize = Math.max(minWidth, minHeight);
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:47:45 -05:00
|
|
|
return this._childrenMaxSize;
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
_updateVisibleChildrenForPage(pageIndex) {
|
|
|
|
this._pages[pageIndex].visibleChildren =
|
|
|
|
this._pages[pageIndex].children.filter(actor => actor.visible);
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_updatePages() {
|
|
|
|
for (let i = 0; i < this._pages.length; i++)
|
|
|
|
this._relocateSurplusItems(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
_unlinkItem(item) {
|
|
|
|
const itemData = this._items.get(item);
|
|
|
|
|
|
|
|
item.disconnect(itemData.destroyId);
|
|
|
|
item.disconnect(itemData.visibleId);
|
2021-02-23 13:47:45 -05:00
|
|
|
item.disconnect(itemData.queueRelayoutId);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
this._items.delete(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
_removePage(pageIndex) {
|
|
|
|
// Make sure to not leave any icon left here
|
|
|
|
this._pages[pageIndex].children.forEach(item => {
|
|
|
|
this._unlinkItem(item);
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:58:51 -04:00
|
|
|
// Adjust the page indexes of items after this page
|
|
|
|
for (const itemData of this._items.values()) {
|
|
|
|
if (itemData.pageIndex > pageIndex)
|
|
|
|
itemData.pageIndex--;
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
this._pages.splice(pageIndex, 1);
|
|
|
|
this.emit('pages-changed');
|
|
|
|
}
|
|
|
|
|
|
|
|
_fillItemVacancies(pageIndex) {
|
|
|
|
if (pageIndex >= this._pages.length - 1)
|
|
|
|
return;
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
const visiblePageItems = this._pages[pageIndex].visibleChildren;
|
2021-01-29 19:03:04 -05:00
|
|
|
const itemsPerPage = this.columnsPerPage * this.rowsPerPage;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
// No reduce needed
|
|
|
|
if (visiblePageItems.length === itemsPerPage)
|
|
|
|
return;
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
const visibleNextPageItems = this._pages[pageIndex + 1].visibleChildren;
|
2020-05-19 09:07:24 -04:00
|
|
|
const nMissingItems = Math.min(itemsPerPage - visiblePageItems.length, visibleNextPageItems.length);
|
|
|
|
|
|
|
|
// Append to the current page the first items of the next page
|
|
|
|
for (let i = 0; i < nMissingItems; i++) {
|
|
|
|
const reducedItem = visibleNextPageItems[i];
|
|
|
|
|
|
|
|
this._removeItemData(reducedItem);
|
|
|
|
this._addItemToPage(reducedItem, pageIndex, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeItemData(item) {
|
|
|
|
const itemData = this._items.get(item);
|
|
|
|
const pageIndex = itemData.pageIndex;
|
|
|
|
const page = this._pages[pageIndex];
|
|
|
|
const itemIndex = page.children.indexOf(item);
|
|
|
|
|
|
|
|
this._unlinkItem(item);
|
|
|
|
|
|
|
|
page.children.splice(itemIndex, 1);
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
this._updateVisibleChildrenForPage(pageIndex);
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
// Delete the page if this is the last icon in it
|
2021-02-23 13:52:46 -05:00
|
|
|
const visibleItems = this._pages[pageIndex].visibleChildren;
|
2020-05-19 09:07:24 -04:00
|
|
|
if (visibleItems.length === 0)
|
|
|
|
this._removePage(pageIndex);
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
if (!this.allowIncompletePages)
|
2020-05-19 09:07:24 -04:00
|
|
|
this._fillItemVacancies(pageIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
_relocateSurplusItems(pageIndex) {
|
2021-02-23 13:52:46 -05:00
|
|
|
const visiblePageItems = this._pages[pageIndex].visibleChildren;
|
2021-01-29 19:03:04 -05:00
|
|
|
const itemsPerPage = this.columnsPerPage * this.rowsPerPage;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
// No overflow needed
|
|
|
|
if (visiblePageItems.length <= itemsPerPage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const nExtraItems = visiblePageItems.length - itemsPerPage;
|
|
|
|
for (let i = 0; i < nExtraItems; i++) {
|
|
|
|
const overflowIndex = visiblePageItems.length - i - 1;
|
|
|
|
const overflowItem = visiblePageItems[overflowIndex];
|
|
|
|
|
|
|
|
this._removeItemData(overflowItem);
|
|
|
|
this._addItemToPage(overflowItem, pageIndex + 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_appendPage() {
|
|
|
|
this._pages.push({ children: [] });
|
|
|
|
this.emit('pages-changed');
|
|
|
|
}
|
|
|
|
|
|
|
|
_addItemToPage(item, pageIndex, index) {
|
|
|
|
// Ensure we have at least one page
|
|
|
|
if (this._pages.length === 0)
|
|
|
|
this._appendPage();
|
|
|
|
|
|
|
|
// Append a new page if necessary
|
|
|
|
if (pageIndex === this._pages.length)
|
|
|
|
this._appendPage();
|
|
|
|
|
|
|
|
if (pageIndex === -1)
|
|
|
|
pageIndex = this._pages.length - 1;
|
|
|
|
|
|
|
|
if (index === -1)
|
|
|
|
index = this._pages[pageIndex].children.length;
|
|
|
|
|
|
|
|
this._items.set(item, {
|
|
|
|
actor: item,
|
|
|
|
pageIndex,
|
|
|
|
destroyId: item.connect('destroy', () => this._removeItemData(item)),
|
|
|
|
visibleId: item.connect('notify::visible', () => {
|
|
|
|
const itemData = this._items.get(item);
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
this._updateVisibleChildrenForPage(itemData.pageIndex);
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
if (item.visible)
|
|
|
|
this._relocateSurplusItems(itemData.pageIndex);
|
2021-01-29 19:03:04 -05:00
|
|
|
else if (!this.allowIncompletePages)
|
2020-05-19 09:07:24 -04:00
|
|
|
this._fillItemVacancies(itemData.pageIndex);
|
|
|
|
}),
|
2021-02-23 13:47:45 -05:00
|
|
|
queueRelayoutId: item.connect('queue-relayout', () => {
|
|
|
|
this._childrenMaxSize = -1;
|
|
|
|
}),
|
2020-05-19 09:07:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
item.icon.setIconSize(this._iconSize);
|
|
|
|
|
|
|
|
this._pages[pageIndex].children.splice(index, 0, item);
|
2021-02-23 13:52:46 -05:00
|
|
|
this._updateVisibleChildrenForPage(pageIndex);
|
2020-05-19 09:07:24 -04:00
|
|
|
this._relocateSurplusItems(pageIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
_calculateSpacing(childSize) {
|
2021-01-29 19:03:04 -05:00
|
|
|
const nColumns = this.columnsPerPage;
|
|
|
|
const nRows = this.rowsPerPage;
|
2020-05-19 09:07:24 -04:00
|
|
|
const usedWidth = childSize * nColumns;
|
|
|
|
const usedHeight = childSize * nRows;
|
2021-01-29 19:03:04 -05:00
|
|
|
const columnSpacingPerPage = this.columnSpacing * (nColumns - 1);
|
|
|
|
const rowSpacingPerPage = this.rowSpacing * (nRows - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2020-11-25 16:39:08 -05:00
|
|
|
const emptyHSpace =
|
|
|
|
this._pageWidth - usedWidth - columnSpacingPerPage -
|
2021-01-29 19:03:04 -05:00
|
|
|
this.pagePadding.left - this.pagePadding.right;
|
2020-11-25 16:39:08 -05:00
|
|
|
const emptyVSpace =
|
|
|
|
this._pageHeight - usedHeight - rowSpacingPerPage -
|
2021-01-29 19:03:04 -05:00
|
|
|
this.pagePadding.top - this.pagePadding.bottom;
|
|
|
|
let leftEmptySpace = this.pagePadding.left;
|
|
|
|
let topEmptySpace = this.pagePadding.top;
|
2020-05-19 09:07:24 -04:00
|
|
|
let hSpacing;
|
|
|
|
let vSpacing;
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
switch (this.pageHalign) {
|
2020-05-19 09:07:24 -04:00
|
|
|
case Clutter.ActorAlign.START:
|
2021-01-29 19:03:04 -05:00
|
|
|
hSpacing = this.columnSpacing;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.CENTER:
|
2020-11-25 16:39:08 -05:00
|
|
|
leftEmptySpace += Math.floor(emptyHSpace / 2);
|
2021-01-29 19:03:04 -05:00
|
|
|
hSpacing = this.columnSpacing;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.END:
|
2020-11-25 16:39:08 -05:00
|
|
|
leftEmptySpace += emptyHSpace;
|
2021-01-29 19:03:04 -05:00
|
|
|
hSpacing = this.columnSpacing;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.FILL:
|
2021-01-29 19:03:04 -05:00
|
|
|
hSpacing = this.columnSpacing + emptyHSpace / (nColumns - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
// Maybe constraint horizontal spacing
|
2021-01-29 19:03:04 -05:00
|
|
|
if (this.maxColumnSpacing !== -1 && hSpacing > this.maxColumnSpacing) {
|
2020-05-19 09:07:24 -04:00
|
|
|
const extraHSpacing =
|
2021-01-29 19:03:04 -05:00
|
|
|
(this.maxColumnSpacing - this.columnSpacing) * (nColumns - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
hSpacing = this.maxColumnSpacing;
|
2020-11-25 16:39:08 -05:00
|
|
|
leftEmptySpace +=
|
2020-05-19 09:07:24 -04:00
|
|
|
Math.max((emptyHSpace - extraHSpacing) / 2, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
switch (this.pageValign) {
|
2020-05-19 09:07:24 -04:00
|
|
|
case Clutter.ActorAlign.START:
|
2021-01-29 19:03:04 -05:00
|
|
|
vSpacing = this.rowSpacing;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.CENTER:
|
2020-11-25 16:39:08 -05:00
|
|
|
topEmptySpace += Math.floor(emptyVSpace / 2);
|
2021-01-29 19:03:04 -05:00
|
|
|
vSpacing = this.rowSpacing;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.END:
|
2020-11-25 16:39:08 -05:00
|
|
|
topEmptySpace += emptyVSpace;
|
2021-01-29 19:03:04 -05:00
|
|
|
vSpacing = this.rowSpacing;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.FILL:
|
2021-01-29 19:03:04 -05:00
|
|
|
vSpacing = this.rowSpacing + emptyVSpace / (nRows - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
// Maybe constraint vertical spacing
|
2021-01-29 19:03:04 -05:00
|
|
|
if (this.maxRowSpacing !== -1 && vSpacing > this.maxRowSpacing) {
|
2020-05-19 09:07:24 -04:00
|
|
|
const extraVSpacing =
|
2021-01-29 19:03:04 -05:00
|
|
|
(this.maxRowSpacing - this.rowSpacing) * (nRows - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
vSpacing = this.maxRowSpacing;
|
2020-11-25 16:39:08 -05:00
|
|
|
topEmptySpace +=
|
2020-05-19 09:07:24 -04:00
|
|
|
Math.max((emptyVSpace - extraVSpacing) / 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [leftEmptySpace, topEmptySpace, hSpacing, vSpacing];
|
|
|
|
}
|
|
|
|
|
2021-02-24 06:07:03 -05:00
|
|
|
_getRowPadding(align, items, itemIndex, childSize, spacing) {
|
|
|
|
if (align === Clutter.ActorAlign.START ||
|
|
|
|
align === Clutter.ActorAlign.FILL)
|
2021-02-24 06:06:19 -05:00
|
|
|
return 0;
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
const nRows = Math.ceil(items.length / this.columnsPerPage);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
let rowAlign = 0;
|
2021-01-29 19:03:04 -05:00
|
|
|
const row = Math.floor(itemIndex / this.columnsPerPage);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
// Only apply to the last row
|
|
|
|
if (row < nRows - 1)
|
|
|
|
return 0;
|
|
|
|
|
2021-01-29 19:03:04 -05:00
|
|
|
const rowStart = row * this.columnsPerPage;
|
|
|
|
const rowEnd = Math.min((row + 1) * this.columnsPerPage - 1, items.length - 1);
|
2020-05-19 09:07:24 -04:00
|
|
|
const itemsInThisRow = rowEnd - rowStart + 1;
|
2021-01-29 19:03:04 -05:00
|
|
|
const nEmpty = this.columnsPerPage - itemsInThisRow;
|
2020-05-19 09:07:24 -04:00
|
|
|
const availableWidth = nEmpty * (spacing + childSize);
|
|
|
|
|
|
|
|
const isRtl =
|
|
|
|
Clutter.get_default_text_direction() === Clutter.TextDirection.RTL;
|
|
|
|
|
2021-02-24 06:07:03 -05:00
|
|
|
switch (align) {
|
2020-05-19 09:07:24 -04:00
|
|
|
case Clutter.ActorAlign.CENTER:
|
|
|
|
rowAlign = availableWidth / 2;
|
|
|
|
break;
|
|
|
|
case Clutter.ActorAlign.END:
|
|
|
|
rowAlign = availableWidth;
|
|
|
|
break;
|
2021-02-24 06:06:19 -05:00
|
|
|
// START and FILL align are handled at the beginning of the function
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return isRtl ? rowAlign * -1 : rowAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDestroy() {
|
|
|
|
if (this._updateIconSizesLaterId >= 0) {
|
|
|
|
Meta.later_remove(this._updateIconSizesLaterId);
|
|
|
|
this._updateIconSizesLaterId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_set_container(container) {
|
2021-08-15 18:36:59 -04:00
|
|
|
this._container?.disconnectObject(this);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
this._container = container;
|
|
|
|
|
|
|
|
if (this._container)
|
2021-08-15 18:36:59 -04:00
|
|
|
this._container.connectObject('destroy', this._onDestroy.bind(this), this);
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_get_preferred_width(_container, _forHeight) {
|
|
|
|
let minWidth = -1;
|
|
|
|
let natWidth = -1;
|
|
|
|
|
|
|
|
switch (this._orientation) {
|
|
|
|
case Clutter.Orientation.VERTICAL:
|
|
|
|
minWidth = IconSize.TINY;
|
|
|
|
natWidth = this._pageWidth;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Clutter.Orientation.HORIZONTAL:
|
|
|
|
minWidth = this._pageWidth * this._pages.length;
|
|
|
|
natWidth = minWidth;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [minWidth, natWidth];
|
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_get_preferred_height(_container, _forWidth) {
|
|
|
|
let minHeight = -1;
|
|
|
|
let natHeight = -1;
|
|
|
|
|
|
|
|
switch (this._orientation) {
|
|
|
|
case Clutter.Orientation.VERTICAL:
|
|
|
|
minHeight = this._pageHeight * this._pages.length;
|
|
|
|
natHeight = minHeight;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Clutter.Orientation.HORIZONTAL:
|
|
|
|
minHeight = IconSize.TINY;
|
|
|
|
natHeight = this._pageHeight;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [minHeight, natHeight];
|
|
|
|
}
|
|
|
|
|
|
|
|
vfunc_allocate() {
|
|
|
|
if (this._pageWidth === 0 || this._pageHeight === 0)
|
|
|
|
throw new Error('IconGridLayout.adaptToSize wasn\'t called before allocation');
|
|
|
|
|
|
|
|
const isRtl =
|
|
|
|
Clutter.get_default_text_direction() === Clutter.TextDirection.RTL;
|
|
|
|
const childSize = this._getChildrenMaxSize();
|
|
|
|
|
|
|
|
const [leftEmptySpace, topEmptySpace, hSpacing, vSpacing] =
|
|
|
|
this._calculateSpacing(childSize);
|
|
|
|
|
|
|
|
const childBox = new Clutter.ActorBox();
|
|
|
|
|
|
|
|
let nChangedIcons = 0;
|
2021-02-24 06:07:03 -05:00
|
|
|
const columnsPerPage = this.columnsPerPage;
|
|
|
|
const orientation = this._orientation;
|
|
|
|
const pageWidth = this._pageWidth;
|
|
|
|
const pageHeight = this._pageHeight;
|
|
|
|
const pageSizeChanged = this._pageSizeChanged;
|
|
|
|
const lastRowAlign = this.lastRowAlign;
|
2021-02-24 05:29:46 -05:00
|
|
|
const shouldEaseItems = this._shouldEaseItems;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
this._pages.forEach((page, pageIndex) => {
|
2021-02-24 06:07:03 -05:00
|
|
|
if (isRtl && orientation === Clutter.Orientation.HORIZONTAL)
|
2020-05-19 09:07:24 -04:00
|
|
|
pageIndex = swap(pageIndex, this._pages.length);
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
page.visibleChildren.forEach((item, itemIndex) => {
|
2021-02-24 06:07:03 -05:00
|
|
|
const row = Math.floor(itemIndex / columnsPerPage);
|
|
|
|
let column = itemIndex % columnsPerPage;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
if (isRtl)
|
2021-02-24 06:07:03 -05:00
|
|
|
column = swap(column, columnsPerPage);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2021-02-24 06:07:03 -05:00
|
|
|
const rowPadding = this._getRowPadding(lastRowAlign,
|
|
|
|
page.visibleChildren, itemIndex, childSize, hSpacing);
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
// Icon position
|
|
|
|
let x = leftEmptySpace + rowPadding + column * (childSize + hSpacing);
|
|
|
|
let y = topEmptySpace + row * (childSize + vSpacing);
|
|
|
|
|
|
|
|
// Page start
|
2021-02-24 06:07:03 -05:00
|
|
|
switch (orientation) {
|
2020-05-19 09:07:24 -04:00
|
|
|
case Clutter.Orientation.HORIZONTAL:
|
2021-02-24 06:07:03 -05:00
|
|
|
x += pageIndex * pageWidth;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
case Clutter.Orientation.VERTICAL:
|
2021-02-24 06:07:03 -05:00
|
|
|
y += pageIndex * pageHeight;
|
2020-05-19 09:07:24 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:07:39 -05:00
|
|
|
childBox.set_origin(Math.floor(x), Math.floor(y));
|
2020-05-19 09:07:24 -04:00
|
|
|
|
2020-10-21 14:40:20 -04:00
|
|
|
const [,, naturalWidth, naturalHeight] = item.get_preferred_size();
|
|
|
|
childBox.set_size(
|
|
|
|
Math.max(childSize, naturalWidth),
|
|
|
|
Math.max(childSize, naturalHeight));
|
|
|
|
|
2021-02-24 05:29:46 -05:00
|
|
|
if (!shouldEaseItems || pageSizeChanged)
|
2020-05-19 09:07:24 -04:00
|
|
|
item.allocate(childBox);
|
|
|
|
else if (animateIconPosition(item, childBox, nChangedIcons))
|
|
|
|
nChangedIcons++;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this._pageSizeChanged = false;
|
2021-02-24 05:29:46 -05:00
|
|
|
this._shouldEaseItems = false;
|
2020-05-19 09:07:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* addItem:
|
|
|
|
* @param {Clutter.Actor} item: item to append to the grid
|
|
|
|
* @param {int} page: page number
|
|
|
|
* @param {int} index: position in the page
|
|
|
|
*
|
|
|
|
* Adds @item to the grid. @item must not be part of the grid.
|
|
|
|
*
|
|
|
|
* If @index exceeds the number of items per page, @item will
|
|
|
|
* be added to the next page.
|
|
|
|
*
|
|
|
|
* @page must be a number between 0 and the number of pages.
|
|
|
|
* Adding to the page after next will create a new page.
|
|
|
|
*/
|
|
|
|
addItem(item, page = -1, index = -1) {
|
|
|
|
if (this._items.has(item))
|
|
|
|
throw new Error(`Item ${item} already added to IconGridLayout`);
|
|
|
|
|
|
|
|
if (page > this._pages.length)
|
|
|
|
throw new Error(`Cannot add ${item} to page ${page}`);
|
|
|
|
|
|
|
|
if (!this._container)
|
|
|
|
return;
|
|
|
|
|
2021-02-24 05:29:46 -05:00
|
|
|
this._shouldEaseItems = true;
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
this._container.add_child(item);
|
|
|
|
this._addItemToPage(item, page, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* appendItem:
|
|
|
|
* @param {Clutter.Actor} item: item to append to the grid
|
|
|
|
*
|
|
|
|
* Appends @item to the grid. @item must not be part of the grid.
|
|
|
|
*/
|
|
|
|
appendItem(item) {
|
|
|
|
this.addItem(item);
|
|
|
|
}
|
|
|
|
|
2020-09-23 15:52:09 -04:00
|
|
|
/**
|
|
|
|
* moveItem:
|
|
|
|
* @param {Clutter.Actor} item: item to move
|
|
|
|
* @param {int} newPage: new page of the item
|
|
|
|
* @param {int} newPosition: new page of the item
|
|
|
|
*
|
|
|
|
* Moves @item to the grid. @item must be part of the grid.
|
|
|
|
*/
|
|
|
|
moveItem(item, newPage, newPosition) {
|
|
|
|
if (!this._items.has(item))
|
|
|
|
throw new Error(`Item ${item} is not part of the IconGridLayout`);
|
|
|
|
|
2021-02-24 05:29:46 -05:00
|
|
|
this._shouldEaseItems = true;
|
|
|
|
|
2020-09-23 15:52:09 -04:00
|
|
|
this._removeItemData(item);
|
|
|
|
this._addItemToPage(item, newPage, newPosition);
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
/**
|
|
|
|
* removeItem:
|
|
|
|
* @param {Clutter.Actor} item: item to remove from the grid
|
|
|
|
*
|
|
|
|
* Removes @item to the grid. @item must be part of the grid.
|
|
|
|
*/
|
|
|
|
removeItem(item) {
|
|
|
|
if (!this._items.has(item))
|
|
|
|
throw new Error(`Item ${item} is not part of the IconGridLayout`);
|
|
|
|
|
|
|
|
if (!this._container)
|
|
|
|
return;
|
|
|
|
|
2021-02-24 05:29:46 -05:00
|
|
|
this._shouldEaseItems = true;
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
this._container.remove_child(item);
|
|
|
|
this._removeItemData(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getItemsAtPage:
|
|
|
|
* @param {int} pageIndex: page index
|
|
|
|
*
|
|
|
|
* Retrieves the children at page @pageIndex. Children may be invisible.
|
|
|
|
*
|
|
|
|
* @returns {Array} an array of {Clutter.Actor}s
|
|
|
|
*/
|
|
|
|
getItemsAtPage(pageIndex) {
|
|
|
|
if (pageIndex >= this._pages.length)
|
|
|
|
throw new Error(`IconGridLayout does not have page ${pageIndex}`);
|
|
|
|
|
|
|
|
return [...this._pages[pageIndex].children];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getItemPosition:
|
|
|
|
* @param {BaseIcon} item: the item
|
|
|
|
*
|
|
|
|
* Retrieves the position of @item is its page, or -1 if @item is not
|
|
|
|
* part of the grid.
|
|
|
|
*
|
|
|
|
* @returns {[int, int]} the page and position of @item
|
|
|
|
*/
|
|
|
|
getItemPosition(item) {
|
|
|
|
if (!this._items.has(item))
|
|
|
|
return [-1, -1];
|
|
|
|
|
|
|
|
const itemData = this._items.get(item);
|
2021-02-23 13:52:46 -05:00
|
|
|
const visibleItems = this._pages[itemData.pageIndex].visibleChildren;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
return [itemData.pageIndex, visibleItems.indexOf(item)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getItemAt:
|
|
|
|
* @param {int} page: the page
|
|
|
|
* @param {int} position: the position in page
|
|
|
|
*
|
|
|
|
* Retrieves the item at @page and @position.
|
|
|
|
*
|
|
|
|
* @returns {BaseItem} the item at @page and @position, or null
|
|
|
|
*/
|
|
|
|
getItemAt(page, position) {
|
|
|
|
if (page < 0 || page >= this._pages.length)
|
|
|
|
return null;
|
|
|
|
|
2021-02-23 13:52:46 -05:00
|
|
|
const visibleItems = this._pages[page].visibleChildren;
|
2020-05-19 09:07:24 -04:00
|
|
|
|
|
|
|
if (position < 0 || position >= visibleItems.length)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return visibleItems[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getItemPage:
|
|
|
|
* @param {BaseIcon} item: the item
|
|
|
|
*
|
|
|
|
* Retrieves the page @item is in, or -1 if @item is not part of the grid.
|
|
|
|
*
|
|
|
|
* @returns {int} the page where @item is in
|
|
|
|
*/
|
|
|
|
getItemPage(item) {
|
|
|
|
if (!this._items.has(item))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
const itemData = this._items.get(item);
|
|
|
|
return itemData.pageIndex;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:43:13 -05:00
|
|
|
ensureIconSizeUpdated() {
|
|
|
|
if (this._updateIconSizesLaterId === 0)
|
|
|
|
return Promise.resolve();
|
|
|
|
|
|
|
|
return new Promise(
|
|
|
|
resolve => this._iconSizeUpdateResolveCbs.push(resolve));
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:07:24 -04:00
|
|
|
adaptToSize(pageWidth, pageHeight) {
|
|
|
|
if (this._pageWidth === pageWidth && this._pageHeight === pageHeight)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._pageWidth = pageWidth;
|
|
|
|
this._pageHeight = pageHeight;
|
|
|
|
this._pageSizeChanged = true;
|
|
|
|
|
|
|
|
if (this._updateIconSizesLaterId === 0) {
|
|
|
|
this._updateIconSizesLaterId =
|
|
|
|
Meta.later_add(Meta.LaterType.BEFORE_REDRAW, () => {
|
|
|
|
const iconSize = this._findBestIconSize();
|
|
|
|
|
|
|
|
if (this._iconSize !== iconSize) {
|
|
|
|
this._iconSize = iconSize;
|
|
|
|
|
|
|
|
for (const child of this._container)
|
|
|
|
child.icon.setIconSize(iconSize);
|
|
|
|
|
|
|
|
this.notify('icon-size');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateIconSizesLaterId = 0;
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:58:39 -04:00
|
|
|
/**
|
|
|
|
* getDropTarget:
|
|
|
|
* @param {int} x: position of the horizontal axis
|
|
|
|
* @param {int} y: position of the vertical axis
|
|
|
|
*
|
|
|
|
* Retrieves the item located at (@x, @y), as well as the drag location.
|
|
|
|
* Both @x and @y are relative to the grid.
|
|
|
|
*
|
|
|
|
* @returns {[Clutter.Actor, DragLocation]} the item and drag location
|
|
|
|
* under (@x, @y)
|
|
|
|
*/
|
|
|
|
getDropTarget(x, y) {
|
|
|
|
const childSize = this._getChildrenMaxSize();
|
|
|
|
const [leftEmptySpace, topEmptySpace, hSpacing, vSpacing] =
|
|
|
|
this._calculateSpacing(childSize);
|
|
|
|
|
|
|
|
const isRtl =
|
|
|
|
Clutter.get_default_text_direction() === Clutter.TextDirection.RTL;
|
|
|
|
|
|
|
|
let page = this._orientation === Clutter.Orientation.VERTICAL
|
|
|
|
? Math.floor(y / this._pageHeight)
|
|
|
|
: Math.floor(x / this._pageWidth);
|
|
|
|
|
|
|
|
// Out of bounds
|
|
|
|
if (page >= this._pages.length)
|
|
|
|
return [null, DragLocation.INVALID];
|
|
|
|
|
|
|
|
if (isRtl && this._orientation === Clutter.Orientation.HORIZONTAL)
|
|
|
|
page = swap(page, this._pages.length);
|
|
|
|
|
|
|
|
// Page-relative coordinates from now on
|
2021-03-14 12:32:26 -04:00
|
|
|
if (this._orientation === Clutter.Orientation.HORIZONTAL)
|
|
|
|
x %= this._pageWidth;
|
|
|
|
else
|
|
|
|
y %= this._pageHeight;
|
2020-05-25 14:58:39 -04:00
|
|
|
|
|
|
|
if (x < leftEmptySpace || y < topEmptySpace)
|
|
|
|
return [null, DragLocation.INVALID];
|
|
|
|
|
|
|
|
const gridWidth =
|
2021-01-29 19:03:04 -05:00
|
|
|
childSize * this.columnsPerPage +
|
|
|
|
hSpacing * (this.columnsPerPage - 1);
|
2020-05-25 14:58:39 -04:00
|
|
|
const gridHeight =
|
2021-01-29 19:03:04 -05:00
|
|
|
childSize * this.rowsPerPage +
|
|
|
|
vSpacing * (this.rowsPerPage - 1);
|
2020-05-25 14:58:39 -04:00
|
|
|
|
|
|
|
if (x > leftEmptySpace + gridWidth || y > topEmptySpace + gridHeight)
|
|
|
|
return [null, DragLocation.INVALID];
|
|
|
|
|
|
|
|
const halfHSpacing = hSpacing / 2;
|
|
|
|
const halfVSpacing = vSpacing / 2;
|
2021-02-23 13:52:46 -05:00
|
|
|
const visibleItems = this._pages[page].visibleChildren;
|
2020-05-25 14:58:39 -04:00
|
|
|
|
|
|
|
for (const item of visibleItems) {
|
|
|
|
const childBox = item.allocation.copy();
|
|
|
|
|
|
|
|
// Page offset
|
|
|
|
switch (this._orientation) {
|
|
|
|
case Clutter.Orientation.HORIZONTAL:
|
|
|
|
childBox.set_origin(childBox.x1 % this._pageWidth, childBox.y1);
|
|
|
|
break;
|
|
|
|
case Clutter.Orientation.VERTICAL:
|
|
|
|
childBox.set_origin(childBox.x1, childBox.y1 % this._pageHeight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outside the icon boundaries
|
|
|
|
if (x < childBox.x1 - halfHSpacing ||
|
|
|
|
x > childBox.x2 + halfHSpacing ||
|
|
|
|
y < childBox.y1 - halfVSpacing ||
|
|
|
|
y > childBox.y2 + halfVSpacing)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let dragLocation;
|
|
|
|
|
|
|
|
if (x < childBox.x1 + LEFT_DIVIDER_LEEWAY)
|
|
|
|
dragLocation = DragLocation.START_EDGE;
|
|
|
|
else if (x > childBox.x2 - RIGHT_DIVIDER_LEEWAY)
|
|
|
|
dragLocation = DragLocation.END_EDGE;
|
|
|
|
else
|
|
|
|
dragLocation = DragLocation.ON_ICON;
|
|
|
|
|
|
|
|
if (isRtl) {
|
|
|
|
if (dragLocation === DragLocation.START_EDGE)
|
|
|
|
dragLocation = DragLocation.END_EDGE;
|
|
|
|
else if (dragLocation === DragLocation.END_EDGE)
|
|
|
|
dragLocation = DragLocation.START_EDGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [item, dragLocation];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [null, DragLocation.EMPTY_SPACE];
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:03:07 -05:00
|
|
|
get iconSize() {
|
2020-05-19 09:07:24 -04:00
|
|
|
return this._iconSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
get nPages() {
|
|
|
|
return this._pages.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
get orientation() {
|
|
|
|
return this._orientation;
|
|
|
|
}
|
|
|
|
|
|
|
|
set orientation(v) {
|
|
|
|
if (this._orientation === v)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (v) {
|
|
|
|
case Clutter.Orientation.VERTICAL:
|
|
|
|
this.request_mode = Clutter.RequestMode.HEIGHT_FOR_WIDTH;
|
|
|
|
break;
|
|
|
|
case Clutter.Orientation.HORIZONTAL:
|
|
|
|
this.request_mode = Clutter.RequestMode.WIDTH_FOR_HEIGHT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._orientation = v;
|
|
|
|
this.notify('orientation');
|
|
|
|
}
|
|
|
|
|
|
|
|
get pageHeight() {
|
|
|
|
return this._pageHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
get pageWidth() {
|
|
|
|
return this._pageWidth;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var IconGrid = GObject.registerClass({
|
2020-05-19 09:43:10 -04:00
|
|
|
Signals: {
|
|
|
|
'pages-changed': {},
|
|
|
|
},
|
|
|
|
}, class IconGrid extends St.Viewport {
|
|
|
|
_init(layoutParams = {}) {
|
|
|
|
layoutParams = Params.parse(layoutParams, {
|
|
|
|
allow_incomplete_pages: false,
|
2020-05-27 22:29:16 -04:00
|
|
|
orientation: Clutter.Orientation.HORIZONTAL,
|
2020-05-19 09:43:10 -04:00
|
|
|
columns_per_page: 6,
|
|
|
|
rows_per_page: 4,
|
2020-11-25 16:50:02 -05:00
|
|
|
page_halign: Clutter.ActorAlign.FILL,
|
2020-11-25 16:42:03 -05:00
|
|
|
page_padding: new Clutter.Margin(),
|
2020-11-25 16:50:02 -05:00
|
|
|
page_valign: Clutter.ActorAlign.FILL,
|
2020-05-19 09:43:10 -04:00
|
|
|
last_row_align: Clutter.ActorAlign.START,
|
|
|
|
column_spacing: 0,
|
|
|
|
row_spacing: 0,
|
|
|
|
});
|
|
|
|
const layoutManager = new IconGridLayout(layoutParams);
|
2021-07-08 14:36:14 -04:00
|
|
|
const pagesChangedId = layoutManager.connect('pages-changed',
|
|
|
|
() => this.emit('pages-changed'));
|
2020-05-18 22:01:41 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
super._init({
|
|
|
|
style_class: 'icon-grid',
|
|
|
|
layoutManager,
|
|
|
|
x_expand: true,
|
|
|
|
y_expand: true,
|
|
|
|
});
|
2019-07-22 11:06:30 -04:00
|
|
|
|
2020-05-19 13:19:05 -04:00
|
|
|
this._gridModes = defaultGridModes;
|
2020-05-19 09:43:10 -04:00
|
|
|
this._currentPage = 0;
|
2020-05-19 13:19:05 -04:00
|
|
|
this._currentMode = -1;
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2018-07-02 10:03:07 -04:00
|
|
|
this.connect('actor-added', this._childAdded.bind(this));
|
|
|
|
this.connect('actor-removed', this._childRemoved.bind(this));
|
2021-07-08 14:36:14 -04:00
|
|
|
this.connect('destroy', () => layoutManager.disconnect(pagesChangedId));
|
2019-07-22 11:06:30 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_childAdded(grid, child) {
|
2020-05-19 14:34:11 -04:00
|
|
|
child._iconGridKeyFocusInId = child.connect('key-focus-in', () => {
|
|
|
|
this._ensureItemIsVisible(child);
|
|
|
|
});
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2014-03-20 09:48:58 -04:00
|
|
|
|
2020-05-19 14:34:11 -04:00
|
|
|
_ensureItemIsVisible(item) {
|
|
|
|
if (!this.contains(item))
|
|
|
|
throw new Error(`${item} is not a child of IconGrid`);
|
|
|
|
|
|
|
|
const itemPage = this.layout_manager.getItemPage(item);
|
|
|
|
this.goToPage(itemPage);
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:19:05 -04:00
|
|
|
_setGridMode(modeIndex) {
|
|
|
|
if (this._currentMode === modeIndex)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._currentMode = modeIndex;
|
|
|
|
|
|
|
|
if (modeIndex !== -1) {
|
|
|
|
const newMode = this._gridModes[modeIndex];
|
|
|
|
|
|
|
|
this.layout_manager.rows_per_page = newMode.rows;
|
|
|
|
this.layout_manager.columns_per_page = newMode.columns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
appDisplay: Adapt to available extra space showing icon grids
Depending on the available horizontal space, we may want to manipulate
the icon grid and scroll view spacing to result in an optimal layout
that has space left to preview prev/next pages.
The main change here is that, when adapting to the available size, the
space given to a page does not necessarily match the available space,
as we need to be able to show more than one page at a time.
With this decoupling of available and page sizes in place, we now know
how much space there is available in order to extend the padding between
pages, or the fade effect applied to the previewed pages.
Underneath, we rely a bit less on hardcoded CSS paddings, and a bit more
on the StScrollView::content-padding property.
All put together, gives us proper space management from ultra-wide
displays, to display ratios that are close to the optimal grid ratio.
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1630>
2021-02-19 10:45:57 -05:00
|
|
|
findBestModeForSize(width, height) {
|
2020-11-25 16:39:08 -05:00
|
|
|
const { pagePadding } = this.layout_manager;
|
|
|
|
width -= pagePadding.left + pagePadding.right;
|
|
|
|
height -= pagePadding.top + pagePadding.bottom;
|
|
|
|
|
2020-05-19 13:19:05 -04:00
|
|
|
const sizeRatio = width / height;
|
|
|
|
let closestRatio = Infinity;
|
|
|
|
let bestMode = -1;
|
|
|
|
|
|
|
|
for (let modeIndex in this._gridModes) {
|
|
|
|
const mode = this._gridModes[modeIndex];
|
|
|
|
const modeRatio = mode.columns / mode.rows;
|
|
|
|
|
|
|
|
if (Math.abs(sizeRatio - modeRatio) < Math.abs(sizeRatio - closestRatio)) {
|
|
|
|
closestRatio = modeRatio;
|
|
|
|
bestMode = modeIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._setGridMode(bestMode);
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_childRemoved(grid, child) {
|
2014-03-20 09:48:58 -04:00
|
|
|
child.disconnect(child._iconGridKeyFocusInId);
|
2019-09-08 12:09:23 -04:00
|
|
|
delete child._iconGridKeyFocusInId;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
vfunc_style_changed() {
|
|
|
|
super.vfunc_style_changed();
|
|
|
|
|
|
|
|
const node = this.get_theme_node();
|
|
|
|
this.layout_manager.column_spacing = node.get_length('column-spacing');
|
|
|
|
this.layout_manager.row_spacing = node.get_length('row-spacing');
|
|
|
|
|
|
|
|
let [found, value] = node.lookup_length('max-column-spacing', false);
|
|
|
|
this.layout_manager.max_column_spacing = found ? value : -1;
|
|
|
|
|
|
|
|
[found, value] = node.lookup_length('max-row-spacing', false);
|
|
|
|
this.layout_manager.max_row_spacing = found ? value : -1;
|
2020-11-25 16:42:03 -05:00
|
|
|
|
|
|
|
const padding = new Clutter.Margin();
|
|
|
|
['top', 'right', 'bottom', 'left'].forEach(side => {
|
|
|
|
padding[side] = node.get_length(`page-padding-${side}`);
|
|
|
|
});
|
|
|
|
this.layout_manager.page_padding = padding;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-12-18 14:09:58 -05:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* addItem:
|
|
|
|
* @param {Clutter.Actor} item: item to append to the grid
|
|
|
|
* @param {int} page: page number
|
|
|
|
* @param {int} index: position in the page
|
|
|
|
*
|
|
|
|
* Adds @item to the grid. @item must not be part of the grid.
|
|
|
|
*
|
|
|
|
* If @index exceeds the number of items per page, @item will
|
|
|
|
* be added to the next page.
|
|
|
|
*
|
|
|
|
* @page must be a number between 0 and the number of pages.
|
|
|
|
* Adding to the page after next will create a new page.
|
|
|
|
*/
|
|
|
|
addItem(item, page = -1, index = -1) {
|
|
|
|
if (!(item.icon instanceof BaseIcon))
|
|
|
|
throw new Error('Only items with a BaseIcon icon property can be added to IconGrid');
|
|
|
|
|
|
|
|
this.layout_manager.addItem(item, page, index);
|
2020-05-18 22:01:41 -04:00
|
|
|
}
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* appendItem:
|
|
|
|
* @param {Clutter.Actor} item: item to append to the grid
|
|
|
|
*
|
|
|
|
* Appends @item to the grid. @item must not be part of the grid.
|
|
|
|
*/
|
|
|
|
appendItem(item) {
|
|
|
|
this.layout_manager.appendItem(item);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-09-23 15:52:09 -04:00
|
|
|
/**
|
|
|
|
* moveItem:
|
|
|
|
* @param {Clutter.Actor} item: item to move
|
|
|
|
* @param {int} newPage: new page of the item
|
|
|
|
* @param {int} newPosition: new page of the item
|
|
|
|
*
|
|
|
|
* Moves @item to the grid. @item must be part of the grid.
|
|
|
|
*/
|
|
|
|
moveItem(item, newPage, newPosition) {
|
|
|
|
this.layout_manager.moveItem(item, newPage, newPosition);
|
|
|
|
this.queue_relayout();
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* removeItem:
|
|
|
|
* @param {Clutter.Actor} item: item to remove from the grid
|
|
|
|
*
|
|
|
|
* Removes @item to the grid. @item must be part of the grid.
|
|
|
|
*/
|
|
|
|
removeItem(item) {
|
|
|
|
if (!this.contains(item))
|
|
|
|
throw new Error(`Item ${item} is not part of the IconGrid`);
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
this.layout_manager.removeItem(item);
|
|
|
|
}
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* goToPage:
|
|
|
|
* @param {int} pageIndex: page index
|
|
|
|
* @param {boolean} animate: animate the page transition
|
|
|
|
*
|
|
|
|
* Moves the current page to @pageIndex. @pageIndex must be a valid page
|
|
|
|
* number.
|
|
|
|
*/
|
|
|
|
goToPage(pageIndex, animate = true) {
|
|
|
|
if (pageIndex >= this.nPages)
|
|
|
|
throw new Error(`IconGrid does not have page ${pageIndex}`);
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
let newValue;
|
|
|
|
let adjustment;
|
|
|
|
switch (this.layout_manager.orientation) {
|
|
|
|
case Clutter.Orientation.VERTICAL:
|
|
|
|
adjustment = this.vadjustment;
|
|
|
|
newValue = pageIndex * this.layout_manager.pageHeight;
|
2019-02-01 07:21:00 -05:00
|
|
|
break;
|
2020-05-19 09:43:10 -04:00
|
|
|
case Clutter.Orientation.HORIZONTAL:
|
|
|
|
adjustment = this.hadjustment;
|
|
|
|
newValue = pageIndex * this.layout_manager.pageWidth;
|
2019-02-01 07:21:00 -05:00
|
|
|
break;
|
2010-10-14 08:27:22 -04:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
this._currentPage = pageIndex;
|
2020-05-18 22:01:41 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
if (!this.mapped)
|
|
|
|
animate = false;
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
adjustment.ease(newValue, {
|
|
|
|
mode: Clutter.AnimationMode.EASE_OUT_CUBIC,
|
|
|
|
duration: animate ? PAGE_SWITCH_TIME : 0,
|
|
|
|
});
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2010-07-20 22:22:19 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* getItemPage:
|
|
|
|
* @param {BaseIcon} item: the item
|
|
|
|
*
|
|
|
|
* Retrieves the page @item is in, or -1 if @item is not part of the grid.
|
|
|
|
*
|
|
|
|
* @returns {int} the page where @item is in
|
|
|
|
*/
|
|
|
|
getItemPage(item) {
|
|
|
|
return this.layout_manager.getItemPage(item);
|
|
|
|
}
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* getItemPosition:
|
|
|
|
* @param {BaseIcon} item: the item
|
|
|
|
*
|
|
|
|
* Retrieves the position of @item is its page, or -1 if @item is not
|
|
|
|
* part of the grid.
|
|
|
|
*
|
|
|
|
* @returns {[int, int]} the page and position of @item
|
|
|
|
*/
|
|
|
|
getItemPosition(item) {
|
|
|
|
if (!this.contains(item))
|
|
|
|
return [-1, -1];
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
const layoutManager = this.layout_manager;
|
|
|
|
return layoutManager.getItemPosition(item);
|
|
|
|
}
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* getItemAt:
|
|
|
|
* @param {int} page: the page
|
|
|
|
* @param {int} position: the position in page
|
|
|
|
*
|
|
|
|
* Retrieves the item at @page and @position.
|
|
|
|
*
|
|
|
|
* @returns {BaseItem} the item at @page and @position, or null
|
|
|
|
*/
|
|
|
|
getItemAt(page, position) {
|
|
|
|
const layoutManager = this.layout_manager;
|
|
|
|
return layoutManager.getItemAt(page, position);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2018-07-02 10:03:07 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
/**
|
|
|
|
* getItemsAtPage:
|
|
|
|
* @param {int} page: the page index
|
|
|
|
*
|
|
|
|
* Retrieves the children at page @page, including invisible children.
|
|
|
|
*
|
|
|
|
* @returns {Array} an array of {Clutter.Actor}s
|
2014-06-17 06:47:00 -04:00
|
|
|
*/
|
2020-05-19 09:43:10 -04:00
|
|
|
getItemsAtPage(page) {
|
|
|
|
if (page < 0 || page > this.nPages)
|
|
|
|
throw new Error(`Page ${page} does not exist at IconGrid`);
|
2020-05-18 22:01:41 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
const layoutManager = this.layout_manager;
|
|
|
|
return layoutManager.getItemsAtPage(page);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2014-06-17 06:47:00 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
get currentPage() {
|
|
|
|
return this._currentPage;
|
2019-08-08 18:15:48 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
set currentPage(v) {
|
|
|
|
this.goToPage(v);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2014-06-17 06:47:00 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
get nPages() {
|
|
|
|
return this.layout_manager.nPages;
|
|
|
|
}
|
2014-06-17 06:47:00 -04:00
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
adaptToSize(width, height) {
|
|
|
|
this.layout_manager.adaptToSize(width, height);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2014-06-17 06:47:00 -04:00
|
|
|
|
2020-05-19 13:19:05 -04:00
|
|
|
setGridModes(modes) {
|
|
|
|
this._gridModes = modes ? modes : defaultGridModes;
|
|
|
|
this.queue_relayout();
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:58:39 -04:00
|
|
|
getDropTarget(x, y) {
|
|
|
|
const layoutManager = this.layout_manager;
|
|
|
|
return layoutManager.getDropTarget(x, y, this._currentPage);
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:43:10 -04:00
|
|
|
get itemsPerPage() {
|
|
|
|
const layoutManager = this.layout_manager;
|
|
|
|
return layoutManager.rows_per_page * layoutManager.columns_per_page;
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2011-11-20 12:56:27 -05:00
|
|
|
});
|