environment: Add Math.clamp

The good old clamp function, now part of the Math family.
Clamp is happy after so many years of loneliness.

This is a strict implementation of the draft ECMAScript
proposal.

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1295
This commit is contained in:
Georges Basile Stavracas Neto 2020-06-02 00:41:43 -03:00
parent 5569090d1c
commit 8154728d09
5 changed files with 12 additions and 25 deletions

View File

@ -72,10 +72,6 @@ function _getFolderName(folder) {
return name;
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function _getViewFromIcon(icon) {
for (let parent = icon.get_parent(); parent; parent = parent.get_parent()) {
if (parent instanceof BaseAppView)
@ -627,7 +623,7 @@ class AppDisplay extends BaseAppView {
}
goToPage(pageNumber, animate = true) {
pageNumber = clamp(pageNumber, 0, this._grid.nPages() - 1);
pageNumber = Math.clamp(pageNumber, 0, this._grid.nPages() - 1);
if (this._grid.currentPage === pageNumber &&
this._displayingDialog &&

View File

@ -362,6 +362,10 @@ function init() {
const Format = imports.format;
String.prototype.format = Format.format;
Math.clamp = function (x, lower, upper) {
return Math.min(Math.max(x, lower), upper);
};
}
// adjustAnimationTime:

View File

@ -155,10 +155,6 @@ class BaseIcon extends St.Bin {
}
});
function clamp(value, min, max) {
return Math.max(Math.min(value, max), min);
}
function zoomOutActor(actor) {
let [x, y] = actor.get_transformed_position();
zoomOutActorAtPos(actor, x, y);
@ -182,8 +178,8 @@ function zoomOutActorAtPos(actor, x, y) {
let scaledHeight = height * APPICON_ANIMATION_OUT_SCALE;
let scaledX = x - (scaledWidth - width) / 2;
let scaledY = y - (scaledHeight - height) / 2;
let containedX = clamp(scaledX, monitor.x, monitor.x + monitor.width - scaledWidth);
let containedY = clamp(scaledY, monitor.y, monitor.y + monitor.height - scaledHeight);
let containedX = Math.clamp(scaledX, monitor.x, monitor.x + monitor.width - scaledWidth);
let containedY = Math.clamp(scaledY, monitor.y, monitor.y + monitor.height - scaledHeight);
actorClone.ease({
scale_x: APPICON_ANIMATION_OUT_SCALE,

View File

@ -225,10 +225,6 @@ class Indicator extends PanelMenu.SystemIndicator {
}
});
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
var AppAuthorizer = class {
constructor(desktopId, reqAccuracyLevel, permStoreProxy, maxAccuracyLevel) {
this.desktopId = desktopId;
@ -313,9 +309,8 @@ var AppAuthorizer = class {
_completeAuth() {
if (this._accuracyLevel != GeoclueAccuracyLevel.NONE) {
this._accuracyLevel = clamp(this._accuracyLevel,
0,
this._maxAccuracyLevel);
this._accuracyLevel = Math.clamp(this._accuracyLevel,
0, this._maxAccuracyLevel);
}
this._saveToPermissionStore();

View File

@ -28,10 +28,6 @@ const State = {
SCROLLING: 1,
};
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
const TouchpadSwipeGesture = GObject.registerClass({
Properties: {
'enabled': GObject.ParamSpec.boolean(
@ -558,8 +554,8 @@ var SwipeTracker = GObject.registerClass({
let firstPoint = this._snapPoints[0];
let lastPoint = this._snapPoints[this._snapPoints.length - 1];
this._progress = clamp(this._progress, firstPoint, lastPoint);
this._progress = clamp(this._progress,
this._progress = Math.clamp(this._progress, firstPoint, lastPoint);
this._progress = Math.clamp(this._progress,
this._initialProgress - 1, this._initialProgress + 1);
this.emit('update', this._progress);
@ -606,7 +602,7 @@ var SwipeTracker = GObject.registerClass({
let duration = Math.abs((this._progress - endProgress) / velocity * DURATION_MULTIPLIER);
if (duration > 0) {
duration = clamp(duration,
duration = Math.clamp(duration,
MIN_ANIMATION_DURATION, MAX_ANIMATION_DURATION);
}