Skip to content

Commit 2f58e9e

Browse files
committed
parameterize check method
1 parent a930313 commit 2f58e9e

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

inputMethods.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,26 @@ namespace microgui {
271271
kb.appendText(".")
272272
} // END OF: Decimal point
273273

274-
const __kbBehaviourNumericEnter: KeyboardBtnFn = (btn: Button, kb: IKeyboard) => { // Enter
275-
const txt = kb.getText();
276-
const len = txt.length;
277-
const lenRule = txt[len - 1] != "-";
278-
const noDecimalEnding = txt[len - 1] != "."; // Illegal: 0. , -0. , -10. Okay: -0.00.. and 0.000 (becomes 0 later)
279-
280-
if (len > 0 && lenRule && noDecimalEnding) { // Last rule could be removed, casting "1." to number is valid.
281-
// Turn -0 and -0.000... into 0 before returning
282-
const txtAsNum: number = +txt;
283-
if (txtAsNum == 0 || txtAsNum == -0)
284-
kb.setText("0")
285-
kb.nextScene()
286-
} else {
287-
kb.shakeText()
288-
}
289-
} // END OF: ENTER
274+
const get__kbBehaviourNumericEnter = (layout: KeyboardLayouts) => {
275+
const __kbBehaviourNumericEnter: KeyboardBtnFn = (btn: Button, kb: IKeyboard) => { // Enter
276+
const txt = kb.getText();
277+
const len = txt.length;
278+
const lenRule = txt[len - 1] != "-";
279+
const noDecimalEnding = txt[len - 1] != "."; // Illegal: 0. , -0. , -10. Okay: -0.00.. and 0.000 (becomes 0 later)
280+
281+
if (len > 0 && lenRule && noDecimalEnding) { // Last rule could be removed, casting "1." to number is valid.
282+
// Turn -0 and -0.000... into 0 before returning
283+
const txtAsNum: number = +txt;
284+
if (txtAsNum == 0 || txtAsNum == -0)
285+
kb.setText(layout == KeyboardLayouts.NUMERIC ? "0" : "1")
286+
kb.nextScene()
287+
} else {
288+
kb.shakeText()
289+
}
290+
return __kbBehaviourNumericDecimal
291+
} // END OF: ENTER
292+
return __kbBehaviourNumericEnter
293+
}
290294

291295
function __keyboardLayout(layout: KeyboardLayouts, del = false): KeyboardLayoutData {
292296
switch (layout) {
@@ -331,7 +335,7 @@ namespace microgui {
331335
defaultBtnBehaviour: __kbBehaviourNumericDefault,
332336
specialBtnBehaviours: [
333337
{ btnRow: 0, btnCol: 3, behaviour: (btn: Button, kb: IKeyboard) => kb.deletePriorCharacters(1) }, // Backspace
334-
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => __kbBehaviourNumericEnter(b, kb) }
338+
{ btnRow: 2, btnCol: 4, behaviour: (b: Button, kb: IKeyboard) => get__kbBehaviourNumericEnter(layout)(b, kb) }
335339
]
336340
}
337341
if (layout == KeyboardLayouts.NUMERIC) {

0 commit comments

Comments
 (0)