diff --git a/applications/services/gui/modules/text_box.c b/applications/services/gui/modules/text_box.c index 0e4aae9446b..c0110806a73 100644 --- a/applications/services/gui/modules/text_box.c +++ b/applications/services/gui/modules/text_box.c @@ -4,6 +4,9 @@ #include #include +#define TEXT_BOX_MAX_SYMBOL_WIDTH (10) +#define TEXT_BOX_LINE_WIDTH (120) + struct TextBox { View* view; @@ -78,13 +81,11 @@ static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) { const char* str = model->text; size_t line_num = 0; - const size_t text_width = 120; - while(str[i] != '\0') { char symb = str[i++]; if(symb != '\n') { 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_width = 0; furi_string_push_back(model->text_formatted, '\n'); @@ -116,6 +117,10 @@ static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) { static void text_box_view_draw_callback(Canvas* canvas, void* _model) { TextBoxModel* model = _model; + if(!model->text) { + return; + } + canvas_clear(canvas); if(model->font == TextBoxFontText) { canvas_set_font(canvas, FontSecondary); @@ -211,6 +216,7 @@ void text_box_reset(TextBox* text_box) { furi_string_set(model->text_formatted, ""); model->font = TextBoxFontText; model->focus = TextBoxFocusStart; + model->formatted = false; }, true); } @@ -218,6 +224,8 @@ void text_box_reset(TextBox* text_box) { void text_box_set_text(TextBox* text_box, const char* text) { furi_assert(text_box); 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( text_box->view, @@ -225,7 +233,7 @@ void text_box_set_text(TextBox* text_box, const char* text) { { model->text = text; 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; }, true); diff --git a/furi/core/string.c b/furi/core/string.c index 682c8d40977..a09ff953f92 100644 --- a/furi/core/string.c +++ b/furi/core/string.c @@ -71,7 +71,8 @@ void furi_string_reserve(FuriString* s, size_t alloc) { } void furi_string_reset(FuriString* s) { - string_reset(s->string); + string_clear(s->string); + string_init(s->string); } void furi_string_swap(FuriString* v1, FuriString* v2) {