1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-13 13:09:49 +04:00

merge ofw dev missing parts

This commit is contained in:
MX
2024-05-19 03:54:21 +03:00
parent fb6070f29d
commit 21abcb56fd
5 changed files with 292 additions and 230 deletions

View File

@@ -1,13 +1,12 @@
#include <gui/modules/text_box.h>
#include <gui/view_dispatcher.h>
#include <gui/view.h>
#include <gui/view_holder.h>
#include "../js_modules.h"
typedef struct {
TextBox* text_box;
ViewDispatcher* view_dispatcher;
FuriThread* thread;
ViewHolder* view_holder;
FuriString* text;
bool is_shown;
} JsTextboxInst;
static JsTextboxInst* get_this_ctx(struct mjs* mjs) {
@@ -87,6 +86,9 @@ static void js_textbox_add_text(struct mjs* mjs) {
return;
}
// Avoid condition race between GUI and JS thread
text_box_set_text(textbox->text_box, "");
size_t new_len = furi_string_size(textbox->text) + text_len;
if(new_len >= 4096) {
furi_string_right(textbox->text, new_len / 2);
@@ -99,10 +101,13 @@ static void js_textbox_add_text(struct mjs* mjs) {
mjs_return(mjs, MJS_UNDEFINED);
}
static void js_textbox_empty_text(struct mjs* mjs) {
static void js_textbox_clear_text(struct mjs* mjs) {
JsTextboxInst* textbox = get_this_ctx(mjs);
if(!check_arg_count(mjs, 0)) return;
// Avoid condition race between GUI and JS thread
text_box_set_text(textbox->text_box, "");
furi_string_reset(textbox->text);
text_box_set_text(textbox->text_box, furi_string_get_cstr(textbox->text));
@@ -114,58 +119,34 @@ static void js_textbox_is_open(struct mjs* mjs) {
JsTextboxInst* textbox = get_this_ctx(mjs);
if(!check_arg_count(mjs, 0)) return;
mjs_return(mjs, mjs_mk_boolean(mjs, !!textbox->thread));
}
static void textbox_deinit(void* context) {
JsTextboxInst* textbox = context;
furi_thread_join(textbox->thread);
furi_thread_free(textbox->thread);
textbox->thread = NULL;
view_dispatcher_remove_view(textbox->view_dispatcher, 0);
view_dispatcher_free(textbox->view_dispatcher);
textbox->view_dispatcher = NULL;
furi_record_close(RECORD_GUI);
text_box_reset(textbox->text_box);
furi_string_reset(textbox->text);
mjs_return(mjs, mjs_mk_boolean(mjs, textbox->is_shown));
}
static void textbox_callback(void* context, uint32_t arg) {
UNUSED(arg);
textbox_deinit(context);
}
static bool textbox_exit(void* context) {
JsTextboxInst* textbox = context;
view_dispatcher_stop(textbox->view_dispatcher);
furi_timer_pending_callback(textbox_callback, textbox, 0);
return true;
view_holder_stop(textbox->view_holder);
textbox->is_shown = false;
}
static int32_t textbox_thread(void* context) {
ViewDispatcher* view_dispatcher = context;
view_dispatcher_run(view_dispatcher);
return 0;
static void textbox_exit(void* context) {
JsTextboxInst* textbox = context;
// Using timer to schedule view_holder stop, will not work under high CPU load
furi_timer_pending_callback(textbox_callback, textbox, 0);
}
static void js_textbox_show(struct mjs* mjs) {
JsTextboxInst* textbox = get_this_ctx(mjs);
if(!check_arg_count(mjs, 0)) return;
Gui* gui = furi_record_open(RECORD_GUI);
textbox->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(textbox->view_dispatcher);
view_dispatcher_add_view(textbox->view_dispatcher, 0, text_box_get_view(textbox->text_box));
view_dispatcher_set_event_callback_context(textbox->view_dispatcher, textbox);
view_dispatcher_set_navigation_event_callback(textbox->view_dispatcher, textbox_exit);
view_dispatcher_attach_to_gui(textbox->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
view_dispatcher_switch_to_view(textbox->view_dispatcher, 0);
if(textbox->is_shown) {
mjs_prepend_errorf(mjs, MJS_INTERNAL_ERROR, "Textbox is already shown");
mjs_return(mjs, MJS_UNDEFINED);
return;
}
textbox->thread =
furi_thread_alloc_ex("JsTextbox", 1024, textbox_thread, textbox->view_dispatcher);
furi_thread_start(textbox->thread);
view_holder_start(textbox->view_holder);
textbox->is_shown = true;
mjs_return(mjs, MJS_UNDEFINED);
}
@@ -174,36 +155,49 @@ static void js_textbox_close(struct mjs* mjs) {
JsTextboxInst* textbox = get_this_ctx(mjs);
if(!check_arg_count(mjs, 0)) return;
if(textbox->thread) {
view_dispatcher_stop(textbox->view_dispatcher);
textbox_deinit(textbox);
}
view_holder_stop(textbox->view_holder);
textbox->is_shown = false;
mjs_return(mjs, MJS_UNDEFINED);
}
static void* js_textbox_create(struct mjs* mjs, mjs_val_t* object) {
JsTextboxInst* textbox = malloc(sizeof(JsTextboxInst));
mjs_val_t textbox_obj = mjs_mk_object(mjs);
mjs_set(mjs, textbox_obj, INST_PROP_NAME, ~0, mjs_mk_foreign(mjs, textbox));
mjs_set(mjs, textbox_obj, "setConfig", ~0, MJS_MK_FN(js_textbox_set_config));
mjs_set(mjs, textbox_obj, "addText", ~0, MJS_MK_FN(js_textbox_add_text));
mjs_set(mjs, textbox_obj, "emptyText", ~0, MJS_MK_FN(js_textbox_empty_text));
mjs_set(mjs, textbox_obj, "clearText", ~0, MJS_MK_FN(js_textbox_clear_text));
mjs_set(mjs, textbox_obj, "isOpen", ~0, MJS_MK_FN(js_textbox_is_open));
mjs_set(mjs, textbox_obj, "show", ~0, MJS_MK_FN(js_textbox_show));
mjs_set(mjs, textbox_obj, "close", ~0, MJS_MK_FN(js_textbox_close));
textbox->text_box = text_box_alloc();
textbox->text = furi_string_alloc();
textbox->text_box = text_box_alloc();
Gui* gui = furi_record_open(RECORD_GUI);
textbox->view_holder = view_holder_alloc();
view_holder_attach_to_gui(textbox->view_holder, gui);
view_holder_set_back_callback(textbox->view_holder, textbox_exit, textbox);
view_holder_set_view(textbox->view_holder, text_box_get_view(textbox->text_box));
*object = textbox_obj;
return textbox;
}
static void js_textbox_destroy(void* inst) {
JsTextboxInst* textbox = inst;
if(textbox->thread) {
view_dispatcher_stop(textbox->view_dispatcher);
textbox_deinit(textbox);
}
view_holder_stop(textbox->view_holder);
view_holder_free(textbox->view_holder);
textbox->view_holder = NULL;
furi_record_close(RECORD_GUI);
text_box_reset(textbox->text_box);
furi_string_reset(textbox->text);
text_box_free(textbox->text_box);
furi_string_free(textbox->text);
free(textbox);
@@ -223,4 +217,4 @@ static const FlipperAppPluginDescriptor textbox_plugin_descriptor = {
const FlipperAppPluginDescriptor* js_textbox_ep(void) {
return &textbox_plugin_descriptor;
}
}