1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 10:17:59 +01:00

Add util.Math.constrain.

This commit is contained in:
Doug Kearns
2009-05-30 20:29:23 +10:00
parent 3e984c776d
commit 95dffaf95a
5 changed files with 27 additions and 14 deletions

View File

@@ -72,8 +72,7 @@ function Buffer() //{{{
{
let values = ZoomManager.zoomValues;
let cur = values.indexOf(ZoomManager.snap(ZoomManager.zoom));
let i = cur + steps;
i = Math.max(0, Math.min(values.length - 1, i));
let i = util.Math.constrain(cur + steps, 0, values.length - 1);
if (i == cur && fullZoom == ZoomManager.useFullZoom)
liberator.beep();
@@ -361,7 +360,7 @@ function Buffer() //{{{
if (elements.length > 0)
{
count = Math.min(Math.max(count, 1), elements.length);
count = util.Math.constrain(count, 1, elements.length);
buffer.focusElement(elements[count - 1]);
}
else
@@ -649,10 +648,7 @@ function Buffer() //{{{
level = buffer.textZoom + parseInt(arg, 10);
// relative args shouldn't take us out of range
if (level < ZOOM_MIN)
level = ZOOM_MIN;
if (level > ZOOM_MAX)
level = ZOOM_MAX;
level = util.Math.constrain(level, ZOOM_MIN, ZOOM_MAX);
}
else
return void liberator.echoerr("E488: Trailing characters");

View File

@@ -154,7 +154,7 @@ function CommandLine() //{{{
this.index += diff;
if (this.index < 0 || this.index > this.store.length)
{
this.index = Math.max(0, Math.min(this.store.length, this.index));
this.index = util.Math.constrain(this.index, 0, this.store.length);
liberator.beep();
// I don't know why this kludge is needed. It
// prevents the caret from moving to the end of
@@ -373,7 +373,7 @@ function CommandLine() //{{{
idx = null;
break;
default:
idx = Math.max(0, Math.min(this.items.length - 1, idx));
idx = util.Math.constrain(idx, 0, this.items.length - 1);
break;
}
@@ -479,7 +479,7 @@ function CommandLine() //{{{
if (this.type.list)
completionList.show();
this.wildIndex = Math.max(0, Math.min(this.wildtypes.length - 1, this.wildIndex + 1));
this.wildIndex = util.Math.constrain(this.wildIndex + 1, 0, this.wildtypes.length - 1);
this.preview();
statusTimer.tell();
@@ -1831,7 +1831,7 @@ function ItemList(id) //{{{
let end = startIndex + options["maxitems"];
function getRows(context)
{
function fix(n) Math.max(0, Math.min(len, n));
function fix(n) util.Math.constrain(n, 0, len);
end -= !!context.message + context.incomplete;
let len = context.items.length;
let start = off;

View File

@@ -387,6 +387,23 @@ const util = { //{{{
return ary;
},
/**
* Math utility methods.
* @singleton
*/
Math: {
/**
* Returns the specified <b>value</b> constrained to the range <b>min</b> -
* <b>max</b>.
*
* @param {number} value The value to constrain.
* @param {number} min The minimum constraint.
* @param {number} max The maximum constraint.
* @returns {number}
*/
constrain: function constrain(value, min, max) Math.min(Math.max(min, value), max),
},
/**
* Converts a URI string into a URI object.
*

View File

@@ -831,7 +831,7 @@ function History() //{{{
liberator.beep();
else
{
let index = Math.max(start, Math.min(end, current + steps));
let index = util.Math.constrain(current + steps, start, end);
window.getWebNavigation().gotoIndex(index);
}
},

View File

@@ -402,7 +402,7 @@ function Player() // {{{
if (/^[+-]/.test(arg))
level = player.volume + level;
player.volume = Math.min(Math.max(level, 0), 1);
player.volume = util.Math.constrain(level, 0, 1);
},
{ argCount: "1" });
@@ -528,7 +528,7 @@ function Player() // {{{
let min = 0;
let max = gMM.playbackControl.duration - 5000; // TODO: 5s buffer like cmus desirable?
gMM.playbackControl.position = Math.min(Math.max(position, min), max);
gMM.playbackControl.position = util.Math.constrain(position, min, max);
},
// FIXME: 10% ?