JS: Add submenu module (#3601)

* JS: Add submenu module
* Using view_holder instead of view_dispatcher

Co-authored-by: nminaylov <nm29719@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
WillyJL 2024-05-16 18:55:08 +01:00 committed by GitHub
parent 7c63bf7574
commit 63403bbae2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 192 additions and 2 deletions

View File

@ -16,6 +16,7 @@ App(
"elements.h",
"view_dispatcher.h",
"view_stack.h",
"view_holder.h",
"modules/button_menu.h",
"modules/byte_input.h",
"modules/popup.h",

View File

@ -46,3 +46,11 @@ App(
requires=["js_app"],
sources=["modules/js_serial.c"],
)
App(
appid="js_submenu",
apptype=FlipperAppType.PLUGIN,
entry_point="js_submenu_ep",
requires=["js_app"],
sources=["modules/js_submenu.c"],
)

View File

@ -0,0 +1,11 @@
let submenu = require("submenu");
submenu.addItem("Item 1", 0);
submenu.addItem("Item 2", 1);
submenu.addItem("Item 3", 2);
submenu.setHeader("Select an option:");
let result = submenu.show();
// Returns undefined when pressing back
print("Result:", result);

View File

@ -0,0 +1,148 @@
#include <gui/modules/submenu.h>
#include <gui/view_holder.h>
#include <gui/view.h>
#include <toolbox/api_lock.h>
#include "../js_modules.h"
typedef struct {
Submenu* submenu;
ViewHolder* view_holder;
FuriApiLock lock;
uint32_t result;
bool accepted;
} JsSubmenuInst;
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* submenu = mjs_get_ptr(mjs, obj_inst);
furi_assert(submenu);
return submenu;
}
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);
}
static bool check_arg_count(struct mjs* mjs, size_t count) {
size_t num_args = mjs_nargs(mjs);
if(num_args != count) {
ret_bad_args(mjs, "Wrong argument count");
return false;
}
return true;
}
static void submenu_callback(void* context, uint32_t id) {
JsSubmenuInst* submenu = context;
submenu->result = id;
submenu->accepted = true;
api_lock_unlock(submenu->lock);
}
static void submenu_exit(void* context) {
JsSubmenuInst* submenu = context;
submenu->result = 0;
submenu->accepted = false;
api_lock_unlock(submenu->lock);
}
static void js_submenu_add_item(struct mjs* mjs) {
JsSubmenuInst* submenu = get_this_ctx(mjs);
if(!check_arg_count(mjs, 2)) return;
mjs_val_t label_arg = mjs_arg(mjs, 0);
const char* label = mjs_get_string(mjs, &label_arg, NULL);
if(!label) {
ret_bad_args(mjs, "Label must be a string");
return;
}
mjs_val_t id_arg = mjs_arg(mjs, 1);
if(!mjs_is_number(id_arg)) {
ret_bad_args(mjs, "Id must be a number");
return;
}
int32_t id = mjs_get_int32(mjs, id_arg);
submenu_add_item(submenu->submenu, label, id, submenu_callback, submenu);
mjs_return(mjs, MJS_UNDEFINED);
}
static void js_submenu_set_header(struct mjs* mjs) {
JsSubmenuInst* submenu = get_this_ctx(mjs);
if(!check_arg_count(mjs, 1)) return;
mjs_val_t header_arg = mjs_arg(mjs, 0);
const char* header = mjs_get_string(mjs, &header_arg, NULL);
if(!header) {
ret_bad_args(mjs, "Header must be a string");
return;
}
submenu_set_header(submenu->submenu, header);
mjs_return(mjs, MJS_UNDEFINED);
}
static void js_submenu_show(struct mjs* mjs) {
JsSubmenuInst* submenu = get_this_ctx(mjs);
if(!check_arg_count(mjs, 0)) return;
submenu->lock = api_lock_alloc_locked();
Gui* gui = furi_record_open(RECORD_GUI);
submenu->view_holder = view_holder_alloc();
view_holder_attach_to_gui(submenu->view_holder, gui);
view_holder_set_back_callback(submenu->view_holder, submenu_exit, submenu);
view_holder_set_view(submenu->view_holder, submenu_get_view(submenu->submenu));
view_holder_start(submenu->view_holder);
api_lock_wait_unlock(submenu->lock);
view_holder_stop(submenu->view_holder);
view_holder_free(submenu->view_holder);
furi_record_close(RECORD_GUI);
api_lock_free(submenu->lock);
submenu_reset(submenu->submenu);
if(submenu->accepted) {
mjs_return(mjs, mjs_mk_number(mjs, submenu->result));
} else {
mjs_return(mjs, MJS_UNDEFINED);
}
}
static void* js_submenu_create(struct mjs* mjs, mjs_val_t* object) {
JsSubmenuInst* submenu = malloc(sizeof(JsSubmenuInst));
mjs_val_t submenu_obj = mjs_mk_object(mjs);
mjs_set(mjs, submenu_obj, INST_PROP_NAME, ~0, mjs_mk_foreign(mjs, submenu));
mjs_set(mjs, submenu_obj, "addItem", ~0, MJS_MK_FN(js_submenu_add_item));
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();
*object = submenu_obj;
return submenu;
}
static void js_submenu_destroy(void* inst) {
JsSubmenuInst* submenu = inst;
submenu_free(submenu->submenu);
free(submenu);
}
static const JsModuleDescriptor js_submenu_desc = {
"submenu",
js_submenu_create,
js_submenu_destroy,
};
static const FlipperAppPluginDescriptor submenu_plugin_descriptor = {
.appid = PLUGIN_APP_ID,
.ep_api_version = PLUGIN_API_VERSION,
.entry_point = &js_submenu_desc,
};
const FlipperAppPluginDescriptor* js_submenu_ep(void) {
return &submenu_plugin_descriptor;
}

View File

@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,61.3,,
Version,+,61.4,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
@ -27,6 +27,7 @@ Header,+,applications/services/gui/modules/variable_item_list.h,,
Header,+,applications/services/gui/modules/widget.h,,
Header,+,applications/services/gui/modules/widget_elements/widget_element.h,,
Header,+,applications/services/gui/view_dispatcher.h,,
Header,+,applications/services/gui/view_holder.h,,
Header,+,applications/services/gui/view_stack.h,,
Header,+,applications/services/input/input.h,,
Header,+,applications/services/loader/firmware_api/firmware_api.h,,
@ -2683,6 +2684,16 @@ Function,+,view_dispatcher_switch_to_view,void,"ViewDispatcher*, uint32_t"
Function,+,view_free,void,View*
Function,+,view_free_model,void,View*
Function,+,view_get_model,void*,View*
Function,+,view_holder_alloc,ViewHolder*,
Function,+,view_holder_attach_to_gui,void,"ViewHolder*, Gui*"
Function,+,view_holder_free,void,ViewHolder*
Function,+,view_holder_get_free_context,void*,ViewHolder*
Function,+,view_holder_set_back_callback,void,"ViewHolder*, BackCallback, void*"
Function,+,view_holder_set_free_callback,void,"ViewHolder*, FreeCallback, void*"
Function,+,view_holder_set_view,void,"ViewHolder*, View*"
Function,+,view_holder_start,void,ViewHolder*
Function,+,view_holder_stop,void,ViewHolder*
Function,+,view_holder_update,void,"View*, void*"
Function,+,view_port_alloc,ViewPort*,
Function,+,view_port_draw_callback_set,void,"ViewPort*, ViewPortDrawCallback, void*"
Function,+,view_port_enabled_set,void,"ViewPort*, _Bool"

1 entry status name type params
2 Version + 61.3 61.4
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/cli/cli.h
5 Header + applications/services/cli/cli_vcp.h
27 Header + applications/services/gui/modules/widget.h
28 Header + applications/services/gui/modules/widget_elements/widget_element.h
29 Header + applications/services/gui/view_dispatcher.h
30 Header + applications/services/gui/view_holder.h
31 Header + applications/services/gui/view_stack.h
32 Header + applications/services/input/input.h
33 Header + applications/services/loader/firmware_api/firmware_api.h
2684 Function + view_free void View*
2685 Function + view_free_model void View*
2686 Function + view_get_model void* View*
2687 Function + view_holder_alloc ViewHolder*
2688 Function + view_holder_attach_to_gui void ViewHolder*, Gui*
2689 Function + view_holder_free void ViewHolder*
2690 Function + view_holder_get_free_context void* ViewHolder*
2691 Function + view_holder_set_back_callback void ViewHolder*, BackCallback, void*
2692 Function + view_holder_set_free_callback void ViewHolder*, FreeCallback, void*
2693 Function + view_holder_set_view void ViewHolder*, View*
2694 Function + view_holder_start void ViewHolder*
2695 Function + view_holder_stop void ViewHolder*
2696 Function + view_holder_update void View*, void*
2697 Function + view_port_alloc ViewPort*
2698 Function + view_port_draw_callback_set void ViewPort*, ViewPortDrawCallback, void*
2699 Function + view_port_enabled_set void ViewPort*, _Bool

View File

@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,61.3,,
Version,+,61.4,,
Header,+,applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
@ -28,6 +28,7 @@ Header,+,applications/services/gui/modules/variable_item_list.h,,
Header,+,applications/services/gui/modules/widget.h,,
Header,+,applications/services/gui/modules/widget_elements/widget_element.h,,
Header,+,applications/services/gui/view_dispatcher.h,,
Header,+,applications/services/gui/view_holder.h,,
Header,+,applications/services/gui/view_stack.h,,
Header,+,applications/services/input/input.h,,
Header,+,applications/services/loader/firmware_api/firmware_api.h,,
@ -3483,6 +3484,16 @@ Function,+,view_dispatcher_switch_to_view,void,"ViewDispatcher*, uint32_t"
Function,+,view_free,void,View*
Function,+,view_free_model,void,View*
Function,+,view_get_model,void*,View*
Function,+,view_holder_alloc,ViewHolder*,
Function,+,view_holder_attach_to_gui,void,"ViewHolder*, Gui*"
Function,+,view_holder_free,void,ViewHolder*
Function,+,view_holder_get_free_context,void*,ViewHolder*
Function,+,view_holder_set_back_callback,void,"ViewHolder*, BackCallback, void*"
Function,+,view_holder_set_free_callback,void,"ViewHolder*, FreeCallback, void*"
Function,+,view_holder_set_view,void,"ViewHolder*, View*"
Function,+,view_holder_start,void,ViewHolder*
Function,+,view_holder_stop,void,ViewHolder*
Function,+,view_holder_update,void,"View*, void*"
Function,+,view_port_alloc,ViewPort*,
Function,+,view_port_draw_callback_set,void,"ViewPort*, ViewPortDrawCallback, void*"
Function,+,view_port_enabled_set,void,"ViewPort*, _Bool"

1 entry status name type params
2 Version + 61.3 61.4
3 Header + applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h
4 Header + applications/services/bt/bt_service/bt.h
5 Header + applications/services/cli/cli.h
28 Header + applications/services/gui/modules/widget.h
29 Header + applications/services/gui/modules/widget_elements/widget_element.h
30 Header + applications/services/gui/view_dispatcher.h
31 Header + applications/services/gui/view_holder.h
32 Header + applications/services/gui/view_stack.h
33 Header + applications/services/input/input.h
34 Header + applications/services/loader/firmware_api/firmware_api.h
3484 Function + view_free void View*
3485 Function + view_free_model void View*
3486 Function + view_get_model void* View*
3487 Function + view_holder_alloc ViewHolder*
3488 Function + view_holder_attach_to_gui void ViewHolder*, Gui*
3489 Function + view_holder_free void ViewHolder*
3490 Function + view_holder_get_free_context void* ViewHolder*
3491 Function + view_holder_set_back_callback void ViewHolder*, BackCallback, void*
3492 Function + view_holder_set_free_callback void ViewHolder*, FreeCallback, void*
3493 Function + view_holder_set_view void ViewHolder*, View*
3494 Function + view_holder_start void ViewHolder*
3495 Function + view_holder_stop void ViewHolder*
3496 Function + view_holder_update void View*, void*
3497 Function + view_port_alloc ViewPort*
3498 Function + view_port_draw_callback_set void ViewPort*, ViewPortDrawCallback, void*
3499 Function + view_port_enabled_set void ViewPort*, _Bool