Revert to simple math for now, fix LockScreen warnings
This commit is contained in:
parent
a71cfb7676
commit
4c0ab4aec7
3 changed files with 49 additions and 150 deletions
|
|
@ -1,120 +0,0 @@
|
|||
// Math helper functions for calculator functionality
|
||||
var MathHelper = {
|
||||
// Basic arithmetic operations
|
||||
add: (a, b) => a + b,
|
||||
subtract: (a, b) => a - b,
|
||||
multiply: (a, b) => a * b,
|
||||
divide: (a, b) => b !== 0 ? a / b : NaN,
|
||||
|
||||
// Power and roots
|
||||
pow: (base, exponent) => Math.pow(base, exponent),
|
||||
sqrt: (x) => x >= 0 ? Math.sqrt(x) : NaN,
|
||||
cbrt: (x) => Math.cbrt(x),
|
||||
|
||||
// Trigonometric functions (in radians)
|
||||
sin: (x) => Math.sin(x),
|
||||
cos: (x) => Math.cos(x),
|
||||
tan: (x) => Math.tan(x),
|
||||
asin: (x) => Math.asin(x),
|
||||
acos: (x) => Math.acos(x),
|
||||
atan: (x) => Math.atan(x),
|
||||
|
||||
// Logarithmic functions
|
||||
log: (x) => x > 0 ? Math.log(x) : NaN,
|
||||
log10: (x) => x > 0 ? Math.log10(x) : NaN,
|
||||
log2: (x) => x > 0 ? Math.log2(x) : NaN,
|
||||
|
||||
// Other mathematical functions
|
||||
abs: (x) => Math.abs(x),
|
||||
floor: (x) => Math.floor(x),
|
||||
ceil: (x) => Math.ceil(x),
|
||||
round: (x) => Math.round(x),
|
||||
min: (...args) => Math.min(...args),
|
||||
max: (...args) => Math.max(...args),
|
||||
|
||||
// Constants
|
||||
PI: Math.PI,
|
||||
E: Math.E,
|
||||
|
||||
// Factorial
|
||||
factorial: (n) => {
|
||||
if (n < 0 || n !== Math.floor(n)) return NaN;
|
||||
if (n === 0 || n === 1) return 1;
|
||||
let result = 1;
|
||||
for (let i = 2; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
// Percentage
|
||||
percent: (value, total) => (value / total) * 100,
|
||||
|
||||
// Degrees to radians and vice versa
|
||||
toRadians: (degrees) => degrees * (Math.PI / 180),
|
||||
toDegrees: (radians) => radians * (180 / Math.PI),
|
||||
|
||||
// Safe evaluation with math functions
|
||||
evaluate: (expression) => {
|
||||
try {
|
||||
// Replace common math functions with MathHelper equivalents
|
||||
let processedExpr = expression
|
||||
.replace(/\bpi\b/gi, 'MathHelper.PI')
|
||||
.replace(/\be\b/gi, 'MathHelper.E')
|
||||
.replace(/\bsin\b/gi, 'MathHelper.sin')
|
||||
.replace(/\bcos\b/gi, 'MathHelper.cos')
|
||||
.replace(/\btan\b/gi, 'MathHelper.tan')
|
||||
.replace(/\basin\b/gi, 'MathHelper.asin')
|
||||
.replace(/\bacos\b/gi, 'MathHelper.acos')
|
||||
.replace(/\batan\b/gi, 'MathHelper.atan')
|
||||
.replace(/\blog\b/gi, 'MathHelper.log')
|
||||
.replace(/\blog10\b/gi, 'MathHelper.log10')
|
||||
.replace(/\blog2\b/gi, 'MathHelper.log2')
|
||||
.replace(/\bsqrt\b/gi, 'MathHelper.sqrt')
|
||||
.replace(/\bcbrt\b/gi, 'MathHelper.cbrt')
|
||||
.replace(/\bpow\b/gi, 'MathHelper.pow')
|
||||
.replace(/\babs\b/gi, 'MathHelper.abs')
|
||||
.replace(/\bfloor\b/gi, 'MathHelper.floor')
|
||||
.replace(/\bceil\b/gi, 'MathHelper.ceil')
|
||||
.replace(/\bround\b/gi, 'MathHelper.round')
|
||||
.replace(/\bmin\b/gi, 'MathHelper.min')
|
||||
.replace(/\bmax\b/gi, 'MathHelper.max')
|
||||
.replace(/\bfactorial\b/gi, 'MathHelper.factorial')
|
||||
.replace(/\bpercent\b/gi, 'MathHelper.percent')
|
||||
.replace(/\btoRadians\b/gi, 'MathHelper.toRadians')
|
||||
.replace(/\btoDegrees\b/gi, 'MathHelper.toDegrees');
|
||||
|
||||
// Evaluate the expression
|
||||
const result = Function('MathHelper', 'return ' + processedExpr)(MathHelper);
|
||||
|
||||
// Check if result is valid
|
||||
if (isNaN(result) || !isFinite(result)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
// Format result for display
|
||||
formatResult: (result) => {
|
||||
if (result === null || isNaN(result) || !isFinite(result)) {
|
||||
return "Error";
|
||||
}
|
||||
|
||||
// For very large or small numbers, use scientific notation
|
||||
if (Math.abs(result) >= 1e10 || (Math.abs(result) < 1e-10 && result !== 0)) {
|
||||
return result.toExponential(6);
|
||||
}
|
||||
|
||||
// For integers, don't show decimal places
|
||||
if (Number.isInteger(result)) {
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
// For decimals, limit to 8 significant digits
|
||||
return parseFloat(result.toPrecision(8)).toString();
|
||||
}
|
||||
};
|
||||
|
|
@ -11,7 +11,6 @@ import qs.Services
|
|||
import qs.Widgets
|
||||
|
||||
import "../../Helpers/FuzzySort.js" as Fuzzysort
|
||||
import "../../Helpers/MathHelper.js" as MathHelper
|
||||
|
||||
NLoader {
|
||||
id: appLauncher
|
||||
|
|
@ -188,26 +187,54 @@ NLoader {
|
|||
// Handle calculator
|
||||
if (query.startsWith(">calc")) {
|
||||
var expr = searchText.slice(5).trim()
|
||||
if (expr && isMathExpression(expr)) {
|
||||
var value = safeEval(expr)
|
||||
if (value !== null && value !== undefined && value !== "") {
|
||||
var formattedResult = MathHelper.MathHelper.formatResult(value)
|
||||
if (expr && expr !== "") {
|
||||
try {
|
||||
// Simple evaluation - only allow basic math operations
|
||||
var sanitizedExpr = expr.replace(/[^0-9+\-*/().\s]/g, '')
|
||||
var result = eval(sanitizedExpr)
|
||||
|
||||
if (isFinite(result) && !isNaN(result)) {
|
||||
var displayResult = Number.isInteger(result) ? result.toString() : result.toFixed(6).replace(/\.?0+$/, '')
|
||||
results.push({
|
||||
"isCalculator": true,
|
||||
"name": `${expr} = ${displayResult}`,
|
||||
"result": result,
|
||||
"expr": expr,
|
||||
"icon": "calculate",
|
||||
"execute": function () {
|
||||
Quickshell.clipboardText = displayResult
|
||||
copyText(displayResult)
|
||||
Quickshell.execDetached(["notify-send", "Calculator", `${expr} = ${displayResult} (copied to clipboard)`])
|
||||
}
|
||||
})
|
||||
} else {
|
||||
results.push({
|
||||
"isCalculator": true,
|
||||
"name": "Invalid expression",
|
||||
"content": "Please enter a valid mathematical expression",
|
||||
"icon": "calculate",
|
||||
"execute": function () {}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
results.push({
|
||||
"isCalculator": true,
|
||||
"name": `Calculator: ${expr} = ${formattedResult}`,
|
||||
"result": value,
|
||||
"expr": expr,
|
||||
"icon": "calculate",
|
||||
"execute": function () {
|
||||
Quickshell.clipboardText = String(formattedResult)
|
||||
clipboardTextCopyProcess.copyText(String(formattedResult))
|
||||
Quickshell.execDetached(
|
||||
["notify-send", "Calculator Result", `${expr} = ${formattedResult} (copied to clipboard)`])
|
||||
}
|
||||
})
|
||||
"isCalculator": true,
|
||||
"name": "Invalid expression",
|
||||
"content": "Please enter a valid mathematical expression",
|
||||
"icon": "calculate",
|
||||
"execute": function () {}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Show placeholder when just ">calc" is entered
|
||||
results.push({
|
||||
"isCalculator": true,
|
||||
"name": "Calculator",
|
||||
"content": "Enter a mathematical expression (e.g., 5+5, 2*3, 10/2)",
|
||||
"icon": "calculate",
|
||||
"execute": function () {}
|
||||
})
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
|
|
@ -240,14 +267,7 @@ NLoader {
|
|||
updateClipboardHistory()
|
||||
}
|
||||
|
||||
function isMathExpression(str) {
|
||||
// Allow more characters for enhanced math functions
|
||||
return /^[-+*/().0-9\s\w]+$/.test(str)
|
||||
}
|
||||
|
||||
function safeEval(expr) {
|
||||
return MathHelper.MathHelper.evaluate(expr)
|
||||
}
|
||||
|
||||
// Main content container
|
||||
Rectangle {
|
||||
|
|
|
|||
|
|
@ -551,9 +551,8 @@ WlSessionLock {
|
|||
height: 20 * scaling
|
||||
color: Color.mPrimary
|
||||
visible: passwordInput.activeFocus
|
||||
anchors.left: asterisksText.right
|
||||
anchors.leftMargin: Style.marginTiniest * scaling
|
||||
anchors.verticalCenter: asterisksText.verticalCenter
|
||||
Layout.leftMargin: asterisksText.width + Style.marginTiniest * scaling
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
|
||||
SequentialAnimation on opacity {
|
||||
loops: Animation.Infinite
|
||||
|
|
@ -619,7 +618,7 @@ WlSessionLock {
|
|||
onClicked: lock.unlockAttempt()
|
||||
|
||||
SequentialAnimation on scale {
|
||||
running: containsMouse
|
||||
running: executeButtonArea.containsMouse
|
||||
NumberAnimation {
|
||||
to: 1.05
|
||||
duration: Style.animationFast
|
||||
|
|
@ -628,7 +627,7 @@ WlSessionLock {
|
|||
}
|
||||
|
||||
SequentialAnimation on scale {
|
||||
running: !containsMouse
|
||||
running: !executeButtonArea.containsMouse
|
||||
NumberAnimation {
|
||||
to: 1.0
|
||||
duration: Style.animationFast
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue