From 4bd511c21403f0ccabdd6f92d5cbb6cb97d028d5 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 19 Mar 2024 01:54:59 +0000 Subject: [PATCH] JS: Fix view dispatcher and header text bugs --- .../system/js_app/modules/js_keyboard.c | 81 +++++++++++-------- .../system/js_app/modules/js_submenu.c | 32 +++----- 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/applications/system/js_app/modules/js_keyboard.c b/applications/system/js_app/modules/js_keyboard.c index 345fdbc9b6..16c2ab7f56 100644 --- a/applications/system/js_app/modules/js_keyboard.c +++ b/applications/system/js_app/modules/js_keyboard.c @@ -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); @@ -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); } @@ -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 { @@ -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 { @@ -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); } diff --git a/applications/system/js_app/modules/js_submenu.c b/applications/system/js_app/modules/js_submenu.c index b3b2ca4a99..1d2f5db554 100644 --- a/applications/system/js_app/modules/js_submenu.c +++ b/applications/system/js_app/modules/js_submenu.c @@ -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); @@ -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 { @@ -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); }