Skip to content

Commit

Permalink
JS: Keyboard allow pressing back, add symbols, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Mar 17, 2024
1 parent ff2aa9b commit 9d83750
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
16 changes: 10 additions & 6 deletions applications/system/js_app/examples/apps/Scripts/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ keyboard.setHeader("Example Text Input");

// Default text is optional
let text = keyboard.text(100, "Default text", true);
// Returns undefined when pressing back
print("Got text:", text);

keyboard.setHeader("Example Byte Input");

// Default data is optional
let data = keyboard.byte(6, Uint8Array([1, 2, 3, 4, 5, 6]));
data = Uint8Array(data);
let result = "0x";
for (let i = 0; i < data.byteLength; i++) {
if (data[i] < 0x10) result += "0";
result += to_hex_string(data[i]);
let result = keyboard.byte(6, Uint8Array([1, 2, 3, 4, 5, 6]));
// Returns undefined when pressing back
if (result !== undefined) {
let data = Uint8Array(result);
result = "0x";
for (let i = 0; i < data.byteLength; i++) {
if (data[i] < 0x10) result += "0";
result += to_hex_string(data[i]);
}
}
print("Got data:", result);
31 changes: 22 additions & 9 deletions applications/system/js_app/modules/js_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
#define membersof(x) (sizeof(x) / sizeof(x[0]))

typedef struct {
char* data;
TextInput* text_input;
ByteInput* byte_input;
ViewDispatcher* view_dispatcher;
uint8_t* byteinput;
bool accepted;
} JsKeyboardInst;

typedef enum {
Expand Down Expand Up @@ -55,14 +54,17 @@ static JsKeyboardInst* get_this_ctx(struct mjs* mjs) {
return storage;
}

void text_input_callback(void* context) {
static void keyboard_callback(void* context) {
JsKeyboardInst* keyboard = (JsKeyboardInst*)context;
keyboard->accepted = true;
view_dispatcher_stop(keyboard->view_dispatcher);
}

void byte_input_callback(void* context) {
static bool keyboard_exit(void* context) {
JsKeyboardInst* keyboard = (JsKeyboardInst*)context;
keyboard->accepted = false;
view_dispatcher_stop(keyboard->view_dispatcher);
return true;
}

static void js_keyboard_set_header(struct mjs* mjs) {
Expand Down Expand Up @@ -97,15 +99,21 @@ static void js_keyboard_text(struct mjs* mjs) {
furi_record_close(RECORD_GUI);

text_input_set_result_callback(
keyboard->text_input, text_input_callback, keyboard, buffer, input_length, clear_default);
keyboard->text_input, keyboard_callback, keyboard, buffer, input_length, clear_default);
text_input_add_illegal_symbols(keyboard->text_input);
text_input_set_minimum_length(keyboard->text_input, 0);

view_dispatcher_switch_to_view(keyboard->view_dispatcher, JsKeyboardViewTextInput);

view_dispatcher_run(keyboard->view_dispatcher);

text_input_reset(keyboard->text_input);

mjs_return(mjs, mjs_mk_string(mjs, buffer, ~0, true));
if(keyboard->accepted) {
mjs_return(mjs, mjs_mk_string(mjs, buffer, ~0, true));
} else {
mjs_return(mjs, MJS_UNDEFINED);
}
free(buffer);
}

Expand All @@ -131,7 +139,7 @@ static void js_keyboard_byte(struct mjs* mjs) {
furi_record_close(RECORD_GUI);

byte_input_set_result_callback(
keyboard->byte_input, byte_input_callback, NULL, keyboard, buffer, input_length);
keyboard->byte_input, keyboard_callback, NULL, keyboard, buffer, input_length);

view_dispatcher_switch_to_view(keyboard->view_dispatcher, JsKeyboardViewByteInput);

Expand All @@ -140,7 +148,11 @@ static void js_keyboard_byte(struct mjs* mjs) {
byte_input_set_result_callback(keyboard->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(keyboard->byte_input, "");

mjs_return(mjs, mjs_mk_array_buf(mjs, (char*)buffer, input_length));
if(keyboard->accepted) {
mjs_return(mjs, mjs_mk_array_buf(mjs, (char*)buffer, input_length));
} else {
mjs_return(mjs, MJS_UNDEFINED);
}
free(buffer);
}

Expand All @@ -163,6 +175,8 @@ static void* js_keyboard_create(struct mjs* mjs, mjs_val_t* object) {
keyboard->view_dispatcher,
JsKeyboardViewByteInput,
byte_input_get_view(keyboard->byte_input));
view_dispatcher_set_event_callback_context(keyboard->view_dispatcher, keyboard);
view_dispatcher_set_navigation_event_callback(keyboard->view_dispatcher, keyboard_exit);
*object = keyboard_obj;
return keyboard;
}
Expand All @@ -174,7 +188,6 @@ static void js_keyboard_destroy(void* inst) {
view_dispatcher_remove_view(keyboard->view_dispatcher, JsKeyboardViewTextInput);
text_input_free(keyboard->text_input);
view_dispatcher_free(keyboard->view_dispatcher);
free(keyboard->data);
free(keyboard);
}

Expand Down

0 comments on commit 9d83750

Please sign in to comment.