mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-20 15:57:57 +01:00
Add util.Math.constrain.
This commit is contained in:
@@ -72,8 +72,7 @@ function Buffer() //{{{
|
|||||||
{
|
{
|
||||||
let values = ZoomManager.zoomValues;
|
let values = ZoomManager.zoomValues;
|
||||||
let cur = values.indexOf(ZoomManager.snap(ZoomManager.zoom));
|
let cur = values.indexOf(ZoomManager.snap(ZoomManager.zoom));
|
||||||
let i = cur + steps;
|
let i = util.Math.constrain(cur + steps, 0, values.length - 1);
|
||||||
i = Math.max(0, Math.min(values.length - 1, i));
|
|
||||||
|
|
||||||
if (i == cur && fullZoom == ZoomManager.useFullZoom)
|
if (i == cur && fullZoom == ZoomManager.useFullZoom)
|
||||||
liberator.beep();
|
liberator.beep();
|
||||||
@@ -361,7 +360,7 @@ function Buffer() //{{{
|
|||||||
|
|
||||||
if (elements.length > 0)
|
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]);
|
buffer.focusElement(elements[count - 1]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -649,10 +648,7 @@ function Buffer() //{{{
|
|||||||
level = buffer.textZoom + parseInt(arg, 10);
|
level = buffer.textZoom + parseInt(arg, 10);
|
||||||
|
|
||||||
// relative args shouldn't take us out of range
|
// relative args shouldn't take us out of range
|
||||||
if (level < ZOOM_MIN)
|
level = util.Math.constrain(level, ZOOM_MIN, ZOOM_MAX);
|
||||||
level = ZOOM_MIN;
|
|
||||||
if (level > ZOOM_MAX)
|
|
||||||
level = ZOOM_MAX;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return void liberator.echoerr("E488: Trailing characters");
|
return void liberator.echoerr("E488: Trailing characters");
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ function CommandLine() //{{{
|
|||||||
this.index += diff;
|
this.index += diff;
|
||||||
if (this.index < 0 || this.index > this.store.length)
|
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();
|
liberator.beep();
|
||||||
// I don't know why this kludge is needed. It
|
// I don't know why this kludge is needed. It
|
||||||
// prevents the caret from moving to the end of
|
// prevents the caret from moving to the end of
|
||||||
@@ -373,7 +373,7 @@ function CommandLine() //{{{
|
|||||||
idx = null;
|
idx = null;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
idx = Math.max(0, Math.min(this.items.length - 1, idx));
|
idx = util.Math.constrain(idx, 0, this.items.length - 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,7 +479,7 @@ function CommandLine() //{{{
|
|||||||
if (this.type.list)
|
if (this.type.list)
|
||||||
completionList.show();
|
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();
|
this.preview();
|
||||||
|
|
||||||
statusTimer.tell();
|
statusTimer.tell();
|
||||||
@@ -1831,7 +1831,7 @@ function ItemList(id) //{{{
|
|||||||
let end = startIndex + options["maxitems"];
|
let end = startIndex + options["maxitems"];
|
||||||
function getRows(context)
|
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;
|
end -= !!context.message + context.incomplete;
|
||||||
let len = context.items.length;
|
let len = context.items.length;
|
||||||
let start = off;
|
let start = off;
|
||||||
|
|||||||
@@ -387,6 +387,23 @@ const util = { //{{{
|
|||||||
return ary;
|
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.
|
* Converts a URI string into a URI object.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -831,7 +831,7 @@ function History() //{{{
|
|||||||
liberator.beep();
|
liberator.beep();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
let index = Math.max(start, Math.min(end, current + steps));
|
let index = util.Math.constrain(current + steps, start, end);
|
||||||
window.getWebNavigation().gotoIndex(index);
|
window.getWebNavigation().gotoIndex(index);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -402,7 +402,7 @@ function Player() // {{{
|
|||||||
if (/^[+-]/.test(arg))
|
if (/^[+-]/.test(arg))
|
||||||
level = player.volume + level;
|
level = player.volume + level;
|
||||||
|
|
||||||
player.volume = Math.min(Math.max(level, 0), 1);
|
player.volume = util.Math.constrain(level, 0, 1);
|
||||||
},
|
},
|
||||||
{ argCount: "1" });
|
{ argCount: "1" });
|
||||||
|
|
||||||
@@ -528,7 +528,7 @@ function Player() // {{{
|
|||||||
let min = 0;
|
let min = 0;
|
||||||
let max = gMM.playbackControl.duration - 5000; // TODO: 5s buffer like cmus desirable?
|
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% ?
|
// FIXME: 10% ?
|
||||||
|
|||||||
Reference in New Issue
Block a user