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

text box: reserve correct memory size to avoid reallocation

This commit is contained in:
gornekich
2024-02-13 11:33:13 +00:00
parent 25a280c818
commit 4042852825

View File

@@ -4,6 +4,9 @@
#include <furi.h> #include <furi.h>
#include <stdint.h> #include <stdint.h>
#define TEXT_BOX_MAX_SYMBOL_WIDTH (10)
#define TEXT_BOX_LINE_WIDTH (120)
struct TextBox { struct TextBox {
View* view; View* view;
@@ -78,13 +81,11 @@ static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
const char* str = model->text; const char* str = model->text;
size_t line_num = 0; size_t line_num = 0;
const size_t text_width = 120;
while(str[i] != '\0') { while(str[i] != '\0') {
char symb = str[i++]; char symb = str[i++];
if(symb != '\n') { if(symb != '\n') {
size_t glyph_width = canvas_glyph_width(canvas, symb); size_t glyph_width = canvas_glyph_width(canvas, symb);
if(line_width + glyph_width > text_width) { if(line_width + glyph_width > TEXT_BOX_LINE_WIDTH) {
line_num++; line_num++;
line_width = 0; line_width = 0;
furi_string_push_back(model->text_formatted, '\n'); furi_string_push_back(model->text_formatted, '\n');
@@ -211,6 +212,7 @@ void text_box_reset(TextBox* text_box) {
furi_string_set(model->text_formatted, ""); furi_string_set(model->text_formatted, "");
model->font = TextBoxFontText; model->font = TextBoxFontText;
model->focus = TextBoxFocusStart; model->focus = TextBoxFocusStart;
model->formatted = false;
}, },
true); true);
} }
@@ -218,6 +220,8 @@ void text_box_reset(TextBox* text_box) {
void text_box_set_text(TextBox* text_box, const char* text) { void text_box_set_text(TextBox* text_box, const char* text) {
furi_assert(text_box); furi_assert(text_box);
furi_assert(text); furi_assert(text);
size_t str_length = strlen(text);
size_t formating_margin = str_length * TEXT_BOX_MAX_SYMBOL_WIDTH / TEXT_BOX_LINE_WIDTH;
with_view_model( with_view_model(
text_box->view, text_box->view,
@@ -225,7 +229,7 @@ void text_box_set_text(TextBox* text_box, const char* text) {
{ {
model->text = text; model->text = text;
furi_string_reset(model->text_formatted); furi_string_reset(model->text_formatted);
furi_string_reserve(model->text_formatted, strlen(text)); furi_string_reserve(model->text_formatted, str_length + formating_margin);
model->formatted = false; model->formatted = false;
}, },
true); true);