Skip to content

Commit

Permalink
JS: Fix view dispatcher and header text bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Mar 19, 2024
1 parent bec4947 commit 4bd511c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 53 deletions.
81 changes: 47 additions & 34 deletions applications/system/js_app/modules/js_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ typedef struct {
TextInput* text_input;
ByteInput* byte_input;
ViewDispatcher* view_dispatcher;
char* header;
bool accepted;
} JsKeyboardInst;

typedef enum {
JsKeyboardViewTextInput,
JsKeyboardViewByteInput,
} JsKeyboardView;

static void ret_bad_args(struct mjs* mjs, const char* error) {
mjs_prepend_errorf(mjs, MJS_BAD_ARGS_ERROR, "%s", error);
mjs_return(mjs, MJS_UNDEFINED);
Expand Down Expand Up @@ -52,8 +48,10 @@ static void js_keyboard_set_header(struct mjs* mjs) {
return;
}

text_input_set_header_text(keyboard->text_input, header);
byte_input_set_header_text(keyboard->byte_input, header);
if(keyboard->header) {
free(keyboard->header);
}
keyboard->header = strdup(header);

mjs_return(mjs, MJS_UNDEFINED);
}
Expand All @@ -78,21 +76,36 @@ static void js_keyboard_text(struct mjs* mjs) {
clear_default = mjs_get_bool(mjs, bool_obj);
}

view_dispatcher_attach_to_gui(
keyboard->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
furi_record_close(RECORD_GUI);

if(keyboard->header) {
text_input_set_header_text(keyboard->text_input, keyboard->header);
}
text_input_set_result_callback(
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);
Gui* gui = furi_record_open(RECORD_GUI);
keyboard->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(keyboard->view_dispatcher);
view_dispatcher_add_view(
keyboard->view_dispatcher, 0, text_input_get_view(keyboard->text_input));
view_dispatcher_set_event_callback_context(keyboard->view_dispatcher, keyboard);
view_dispatcher_set_navigation_event_callback(keyboard->view_dispatcher, keyboard_exit);
view_dispatcher_attach_to_gui(keyboard->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
view_dispatcher_switch_to_view(keyboard->view_dispatcher, 0);

view_dispatcher_run(keyboard->view_dispatcher);

text_input_reset(keyboard->text_input);
view_dispatcher_remove_view(keyboard->view_dispatcher, 0);
view_dispatcher_free(keyboard->view_dispatcher);
keyboard->view_dispatcher = NULL;
furi_record_close(RECORD_GUI);

text_input_reset(keyboard->text_input);
if(keyboard->header) {
free(keyboard->header);
keyboard->header = NULL;
}
if(keyboard->accepted) {
mjs_return(mjs, mjs_mk_string(mjs, buffer, ~0, true));
} else {
Expand Down Expand Up @@ -122,20 +135,35 @@ static void js_keyboard_byte(struct mjs* mjs) {
memcpy(buffer, (uint8_t*)default_data, MIN((size_t)input_length, default_data_len));
}

view_dispatcher_attach_to_gui(
keyboard->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
furi_record_close(RECORD_GUI);

if(keyboard->header) {
byte_input_set_header_text(keyboard->byte_input, keyboard->header);
}
byte_input_set_result_callback(
keyboard->byte_input, keyboard_callback, NULL, keyboard, buffer, input_length);

view_dispatcher_switch_to_view(keyboard->view_dispatcher, JsKeyboardViewByteInput);
Gui* gui = furi_record_open(RECORD_GUI);
keyboard->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(keyboard->view_dispatcher);
view_dispatcher_add_view(
keyboard->view_dispatcher, 0, 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);
view_dispatcher_attach_to_gui(keyboard->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
view_dispatcher_switch_to_view(keyboard->view_dispatcher, 0);

view_dispatcher_run(keyboard->view_dispatcher);

view_dispatcher_remove_view(keyboard->view_dispatcher, 0);
view_dispatcher_free(keyboard->view_dispatcher);
keyboard->view_dispatcher = NULL;
furi_record_close(RECORD_GUI);

if(keyboard->header) {
free(keyboard->header);
keyboard->header = NULL;
}
byte_input_set_result_callback(keyboard->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(keyboard->byte_input, "");

if(keyboard->accepted) {
mjs_return(mjs, mjs_mk_array_buf(mjs, (char*)buffer, input_length));
} else {
Expand All @@ -153,29 +181,14 @@ static void* js_keyboard_create(struct mjs* mjs, mjs_val_t* object) {
mjs_set(mjs, keyboard_obj, "byte", ~0, MJS_MK_FN(js_keyboard_byte));
keyboard->byte_input = byte_input_alloc();
keyboard->text_input = text_input_alloc();
keyboard->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(keyboard->view_dispatcher);
view_dispatcher_add_view(
keyboard->view_dispatcher,
JsKeyboardViewTextInput,
text_input_get_view(keyboard->text_input));
view_dispatcher_add_view(
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;
}

static void js_keyboard_destroy(void* inst) {
JsKeyboardInst* keyboard = inst;
view_dispatcher_remove_view(keyboard->view_dispatcher, JsKeyboardViewByteInput);
byte_input_free(keyboard->byte_input);
view_dispatcher_remove_view(keyboard->view_dispatcher, JsKeyboardViewTextInput);
text_input_free(keyboard->text_input);
view_dispatcher_free(keyboard->view_dispatcher);
free(keyboard);
}

Expand Down
32 changes: 13 additions & 19 deletions applications/system/js_app/modules/js_submenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ typedef struct {
bool accepted;
} JsSubmenuInst;

typedef enum {
JsSubmenuViewSubmenu,
} JsSubmenuView;

static JsSubmenuInst* get_this_ctx(struct mjs* mjs) {
mjs_val_t obj_inst = mjs_get(mjs, mjs_get_this(mjs), INST_PROP_NAME, ~0);
JsSubmenuInst* storage = mjs_get_ptr(mjs, obj_inst);
Expand Down Expand Up @@ -92,18 +88,24 @@ static void js_submenu_set_header(struct mjs* mjs) {
static void js_submenu_show(struct mjs* mjs) {
JsSubmenuInst* submenu = get_this_ctx(mjs);
if(!check_arg_count(mjs, 0)) return;
submenu->result = 0;

view_dispatcher_attach_to_gui(
submenu->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
furi_record_close(RECORD_GUI);

view_dispatcher_switch_to_view(submenu->view_dispatcher, JsSubmenuViewSubmenu);
Gui* gui = furi_record_open(RECORD_GUI);
submenu->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(submenu->view_dispatcher);
view_dispatcher_add_view(submenu->view_dispatcher, 0, submenu_get_view(submenu->submenu));
view_dispatcher_set_event_callback_context(submenu->view_dispatcher, submenu);
view_dispatcher_set_navigation_event_callback(submenu->view_dispatcher, submenu_exit);
view_dispatcher_attach_to_gui(submenu->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
view_dispatcher_switch_to_view(submenu->view_dispatcher, 0);

view_dispatcher_run(submenu->view_dispatcher);

submenu_reset(submenu->submenu);
view_dispatcher_remove_view(submenu->view_dispatcher, 0);
view_dispatcher_free(submenu->view_dispatcher);
submenu->view_dispatcher = NULL;
furi_record_close(RECORD_GUI);

submenu_reset(submenu->submenu);
if(submenu->accepted) {
mjs_return(mjs, mjs_mk_number(mjs, submenu->result));
} else {
Expand All @@ -119,21 +121,13 @@ static void* js_submenu_create(struct mjs* mjs, mjs_val_t* object) {
mjs_set(mjs, submenu_obj, "setHeader", ~0, MJS_MK_FN(js_submenu_set_header));
mjs_set(mjs, submenu_obj, "show", ~0, MJS_MK_FN(js_submenu_show));
submenu->submenu = submenu_alloc();
submenu->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(submenu->view_dispatcher);
view_dispatcher_add_view(
submenu->view_dispatcher, JsSubmenuViewSubmenu, submenu_get_view(submenu->submenu));
view_dispatcher_set_event_callback_context(submenu->view_dispatcher, submenu);
view_dispatcher_set_navigation_event_callback(submenu->view_dispatcher, submenu_exit);
*object = submenu_obj;
return submenu;
}

static void js_submenu_destroy(void* inst) {
JsSubmenuInst* submenu = inst;
view_dispatcher_remove_view(submenu->view_dispatcher, JsSubmenuViewSubmenu);
submenu_free(submenu->submenu);
view_dispatcher_free(submenu->view_dispatcher);
free(submenu);
}

Expand Down

0 comments on commit 4bd511c

Please sign in to comment.