Skip to content

Commit

Permalink
feat: Improve textEdit API
Browse files Browse the repository at this point in the history
It now returns string only if new input has occurred
  • Loading branch information
warxander committed Feb 3, 2024
1 parent b21d557 commit f29660f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 39 deletions.
6 changes: 0 additions & 6 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Frame } from './frame';
import { Color, Image } from './types';

export function wait(ms: number): Promise<unknown> {
return new Promise(function (res: TimerHandler) {
setTimeout(res, ms);
});
}

export function drawItemBackground(frame: Frame, selector: string, w: number, h: number) {
const style = Frame.getStyle();
const painter = frame.getPainter();
Expand Down
44 changes: 11 additions & 33 deletions src/items/text_edit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { Frame, getFrameChecked } from '../core/frame';
import { getDefaultStyleSelectorState, wait } from '../core/utils';
import { getDefaultStyleSelectorState } from '../core/utils';
import { Color } from '../core/types';
import { TextData } from '../core/painter';

const KEYBOARD_TITLE_ENTRY = 'VEIN_EDIT_KEYBOARD_TITLE';

/**
* @category Items
*/
export async function textEdit(
export function textEdit(
text: string,
keyboardTitle: string,
maxTextLength: number,
isSecretMode = false,
placeholderText: string | null = null
): Promise<string> {
): string | null {
const frame = getFrameChecked();

const painter = frame.getPainter();
Expand All @@ -39,24 +37,9 @@ export async function textEdit(

frame.beginItem(w, h);

let keyboardResultText = null;

if (frame.isItemClicked()) {
AddTextEntry(KEYBOARD_TITLE_ENTRY, keyboardTitle);
DisplayOnscreenKeyboard(1, KEYBOARD_TITLE_ENTRY, '', text, '', '', '', maxTextLength);

while (true) {
await wait(0);

DisableAllControlActions(0);

const status = UpdateOnscreenKeyboard();
if (status === 1) {
keyboardResultText = GetOnscreenKeyboardResult();
break;
} else if (status === 2) break;
}
}
let resultText: string | null = null;
if (frame.isKeyboardOnScreen()) resultText = frame.tryGetOnScreenKeyboardResult();
else if (frame.isItemClicked()) frame.showOnScreenKeyboard(keyboardTitle, text, maxTextLength);

selector = frame.buildStyleSelector('text-edit', getDefaultStyleSelectorState(frame));

Expand All @@ -65,22 +48,17 @@ export async function textEdit(

painter.move(style.textEdit.padding, (h - painter.getFontSize(font, scale)) / 2 + style.item.textOffset);

const inputText = keyboardResultText ?? text;
const hasInputText = inputText.length !== 0;
const hasText = text.length !== 0;
const hasPlaceholderText = placeholderText !== null;

if (hasInputText || hasPlaceholderText) {
painter.setColor(style.getPropertyAs<Color>(selector, hasInputText ? 'color' : 'placeholder-color'));
if (hasText || hasPlaceholderText) {
painter.setColor(style.getPropertyAs<Color>(selector, hasText ? 'color' : 'placeholder-color'));
painter.drawText(
new TextData(
hasPlaceholderText ? placeholderText : isSecretMode ? inputText.replace(/./g, '*') : inputText,
font,
scale
)
new TextData(hasText ? (isSecretMode ? text.replace(/./g, '*') : text) : placeholderText!, font, scale)
);
}

frame.endItem();

return inputText;
return resultText;
}

0 comments on commit f29660f

Please sign in to comment.