[gnome-shell/gbsneto/random-cleanups-pt1] environment: Add Math.clamp



commit 8abadc9db3404f6eeb0755c2b49a1b3bd35df60e
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Jun 2 00:41:43 2020 -0300

    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

 js/ui/appDisplay.js      |  6 +-----
 js/ui/environment.js     |  4 ++++
 js/ui/iconGrid.js        |  8 ++------
 js/ui/status/location.js |  9 ++-------
 js/ui/swipeTracker.js    | 10 +++-------
 5 files changed, 12 insertions(+), 25 deletions(-)
---
diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js
index 5ab871f07d..8931ca4a16 100644
--- a/js/ui/appDisplay.js
+++ b/js/ui/appDisplay.js
@@ -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 &&
diff --git a/js/ui/environment.js b/js/ui/environment.js
index a0b884eb3e..e0e588d539 100644
--- a/js/ui/environment.js
+++ b/js/ui/environment.js
@@ -362,6 +362,10 @@ function init() {
     const Format = imports.format;
 
     String.prototype.format = Format.format;
+
+    Math.prototype.clamp = function (x, lower, upper) {
+        return Math.min(Math.max(x, lower), upper);
+    };
 }
 
 // adjustAnimationTime:
diff --git a/js/ui/iconGrid.js b/js/ui/iconGrid.js
index c0c8af3726..f67a70ed13 100644
--- a/js/ui/iconGrid.js
+++ b/js/ui/iconGrid.js
@@ -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,
diff --git a/js/ui/status/location.js b/js/ui/status/location.js
index e5c8368e4c..4250ed0fe9 100644
--- a/js/ui/status/location.js
+++ b/js/ui/status/location.js
@@ -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();
 
diff --git a/js/ui/swipeTracker.js b/js/ui/swipeTracker.js
index 818d101938..a638d78019 100644
--- a/js/ui/swipeTracker.js
+++ b/js/ui/swipeTracker.js
@@ -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);
         }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]