1
mirror of https://github.com/flipperdevices/flipperzero-firmware.git synced 2025-12-13 05:19:50 +04:00

JS: gui: text_input: Default text props

Co-authored-by: xMasterX <xMasterX@users.noreply.github.com>
This commit is contained in:
Willy-JL
2024-10-17 17:49:34 +01:00
parent d50b0621cb
commit 4bbbd29b96
5 changed files with 67 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ let emptyView = require("gui/empty_screen");
let textInputView = require("gui/text_input");
let textBoxView = require("gui/text_box");
let dialogView = require("gui/dialog");
let flipper = require("flipper");
// declare view instances
let views = {
@@ -14,6 +15,9 @@ let views = {
empty: emptyView.make(),
keyboard: textInputView.makeWith({
header: "Enter your name",
defaultText: flipper.getName(),
defaultTextClear: true,
// Props for makeWith() are passed in reverse order, so maxLength must be after defaultText
minLength: 0,
maxLength: 32,
}),
@@ -57,6 +61,7 @@ eventLoop.subscribe(views.demos.chosen, function (_sub, index, gui, eventLoop, v
// say hi after keyboard input
eventLoop.subscribe(views.keyboard.input, function (_sub, name, gui, views) {
views.keyboard.set("defaultText", name); // Remember for next usage
views.helloDialog.set("text", "Hi " + name + "! :)");
gui.viewDispatcher.switchTo(views.helloDialog);
}, gui, views);

View File

@@ -215,6 +215,13 @@ static bool
}
c_value = (JsViewPropValue){.array = value};
} break;
case JsViewPropTypeBool: {
if(!mjs_is_boolean(value)) {
expected_type = "bool";
break;
}
c_value = (JsViewPropValue){.boolean = mjs_get_bool(mjs, value)};
} break;
}
if(expected_type) {

View File

@@ -9,12 +9,14 @@ typedef enum {
JsViewPropTypeString,
JsViewPropTypeNumber,
JsViewPropTypeArr,
JsViewPropTypeBool,
} JsViewPropType;
typedef union {
const char* string;
int32_t number;
mjs_val_t array;
bool boolean;
} JsViewPropValue;
/**

View File

@@ -9,6 +9,7 @@ typedef struct {
char* buffer;
size_t buffer_size;
FuriString* header;
bool default_text_clear;
FuriSemaphore* input_semaphore;
JsEventLoopContract contract;
} JsKbdContext;
@@ -56,7 +57,46 @@ static bool max_len_assign(
context,
context->buffer,
context->buffer_size,
true);
context->default_text_clear);
return true;
}
static bool default_text_assign(
struct mjs* mjs,
TextInput* input,
JsViewPropValue value,
JsKbdContext* context) {
UNUSED(mjs);
UNUSED(input);
if(value.string) {
strlcpy(context->buffer, value.string, context->buffer_size);
text_input_set_result_callback(
input,
(TextInputCallback)input_callback,
context,
context->buffer,
context->buffer_size,
context->default_text_clear);
}
return true;
}
static bool default_text_clear_assign(
struct mjs* mjs,
TextInput* input,
JsViewPropValue value,
JsKbdContext* context) {
UNUSED(mjs);
context->default_text_clear = value.boolean;
text_input_set_result_callback(
input,
(TextInputCallback)input_callback,
context,
context->buffer,
context->buffer_size,
context->default_text_clear);
return true;
}
@@ -66,6 +106,7 @@ static JsKbdContext* ctx_make(struct mjs* mjs, TextInput* input, mjs_val_t view_
.buffer_size = DEFAULT_BUF_SZ,
.buffer = malloc(DEFAULT_BUF_SZ),
.header = furi_string_alloc(),
.default_text_clear = false,
.input_semaphore = furi_semaphore_alloc(1, 0),
};
context->contract = (JsEventLoopContract){
@@ -105,7 +146,7 @@ static const JsViewDescriptor view_descriptor = {
.get_view = (JsViewGetView)text_input_get_view,
.custom_make = (JsViewCustomMake)ctx_make,
.custom_destroy = (JsViewCustomDestroy)ctx_destroy,
.prop_cnt = 3,
.prop_cnt = 5,
.props = {
(JsViewPropDescriptor){
.name = "header",
@@ -119,6 +160,14 @@ static const JsViewDescriptor view_descriptor = {
.name = "maxLength",
.type = JsViewPropTypeNumber,
.assign = (JsViewPropAssign)max_len_assign},
(JsViewPropDescriptor){
.name = "defaultText",
.type = JsViewPropTypeString,
.assign = (JsViewPropAssign)default_text_assign},
(JsViewPropDescriptor){
.name = "defaultTextClear",
.type = JsViewPropTypeBool,
.assign = (JsViewPropAssign)default_text_clear_assign},
}};
JS_GUI_VIEW_DEF(text_input, &view_descriptor);

View File

@@ -5,6 +5,8 @@ type Props = {
header: string,
minLength: number,
maxLength: number,
defaultText: string,
defaultTextClear: boolean,
}
declare class TextInput extends View<Props> {
input: Contract<string>;