text_box.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "text_box.h"
  2. #include "gui/canvas.h"
  3. #include <furi.h>
  4. #include <gui/elements.h>
  5. #include <stdint.h>
  6. struct TextBox {
  7. View* view;
  8. };
  9. typedef struct {
  10. const char* text;
  11. char* text_pos;
  12. FuriString* text_formatted;
  13. int32_t scroll_pos;
  14. int32_t scroll_num;
  15. TextBoxFont font;
  16. TextBoxFocus focus;
  17. bool formatted;
  18. } TextBoxModel;
  19. static void text_box_process_down(TextBox* text_box) {
  20. with_view_model(
  21. text_box->view,
  22. TextBoxModel * model,
  23. {
  24. if(model->scroll_pos < model->scroll_num - 1) {
  25. model->scroll_pos++;
  26. // Search next line start
  27. while(*model->text_pos++ != '\n')
  28. ;
  29. }
  30. },
  31. true);
  32. }
  33. static void text_box_process_up(TextBox* text_box) {
  34. with_view_model(
  35. text_box->view,
  36. TextBoxModel * model,
  37. {
  38. if(model->scroll_pos > 0) {
  39. model->scroll_pos--;
  40. // Reach last symbol of previous line
  41. model->text_pos--;
  42. // Search prevous line start
  43. while((model->text_pos != model->text) && (*(--model->text_pos) != '\n'))
  44. ;
  45. if(*model->text_pos == '\n') {
  46. model->text_pos++;
  47. }
  48. }
  49. },
  50. true);
  51. }
  52. static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
  53. size_t i = 0;
  54. size_t line_width = 0;
  55. const char* str = model->text;
  56. size_t line_num = 0;
  57. const size_t text_width = 120;
  58. while(str[i] != '\0') {
  59. char symb = str[i++];
  60. if(symb != '\n') {
  61. size_t glyph_width = canvas_glyph_width(canvas, symb);
  62. if(line_width + glyph_width > text_width) {
  63. line_num++;
  64. line_width = 0;
  65. furi_string_push_back(model->text_formatted, '\n');
  66. }
  67. line_width += glyph_width;
  68. } else {
  69. line_num++;
  70. line_width = 0;
  71. }
  72. furi_string_push_back(model->text_formatted, symb);
  73. }
  74. line_num++;
  75. model->text = furi_string_get_cstr(model->text_formatted);
  76. model->text_pos = (char*)model->text;
  77. if(model->focus == TextBoxFocusEnd && line_num > 5) {
  78. // Set text position to 5th line from the end
  79. for(uint8_t i = 0; i < line_num - 5; i++) {
  80. while(*model->text_pos++ != '\n') {
  81. };
  82. }
  83. model->scroll_num = line_num - 4;
  84. model->scroll_pos = line_num - 5;
  85. } else {
  86. model->scroll_num = MAX(line_num - 4, 0u);
  87. model->scroll_pos = 0;
  88. }
  89. }
  90. static void text_box_view_draw_callback(Canvas* canvas, void* _model) {
  91. TextBoxModel* model = _model;
  92. canvas_clear(canvas);
  93. if(model->font == TextBoxFontText) {
  94. canvas_set_font(canvas, FontSecondary);
  95. } else if(model->font == TextBoxFontHex) {
  96. canvas_set_font(canvas, FontKeyboard);
  97. }
  98. if(!model->formatted) {
  99. text_box_insert_endline(canvas, model);
  100. model->formatted = true;
  101. }
  102. elements_slightly_rounded_frame(canvas, 0, 0, 124, 64);
  103. elements_multiline_text(canvas, 3, 11, model->text_pos);
  104. elements_scrollbar(canvas, model->scroll_pos, model->scroll_num);
  105. }
  106. static bool text_box_view_input_callback(InputEvent* event, void* context) {
  107. furi_assert(context);
  108. TextBox* text_box = context;
  109. bool consumed = false;
  110. if(event->type == InputTypeShort) {
  111. if(event->key == InputKeyDown) {
  112. text_box_process_down(text_box);
  113. consumed = true;
  114. } else if(event->key == InputKeyUp) {
  115. text_box_process_up(text_box);
  116. consumed = true;
  117. }
  118. }
  119. return consumed;
  120. }
  121. TextBox* text_box_alloc() {
  122. TextBox* text_box = malloc(sizeof(TextBox));
  123. text_box->view = view_alloc();
  124. view_set_context(text_box->view, text_box);
  125. view_allocate_model(text_box->view, ViewModelTypeLocking, sizeof(TextBoxModel));
  126. view_set_draw_callback(text_box->view, text_box_view_draw_callback);
  127. view_set_input_callback(text_box->view, text_box_view_input_callback);
  128. with_view_model(
  129. text_box->view,
  130. TextBoxModel * model,
  131. {
  132. model->text = NULL;
  133. model->text_formatted = furi_string_alloc_set("");
  134. model->formatted = false;
  135. model->font = TextBoxFontText;
  136. },
  137. true);
  138. return text_box;
  139. }
  140. void text_box_free(TextBox* text_box) {
  141. furi_assert(text_box);
  142. with_view_model(
  143. text_box->view, TextBoxModel * model, { furi_string_free(model->text_formatted); }, true);
  144. view_free(text_box->view);
  145. free(text_box);
  146. }
  147. View* text_box_get_view(TextBox* text_box) {
  148. furi_assert(text_box);
  149. return text_box->view;
  150. }
  151. void text_box_reset(TextBox* text_box) {
  152. furi_assert(text_box);
  153. with_view_model(
  154. text_box->view,
  155. TextBoxModel * model,
  156. {
  157. model->text = NULL;
  158. furi_string_set(model->text_formatted, "");
  159. model->font = TextBoxFontText;
  160. model->focus = TextBoxFocusStart;
  161. },
  162. true);
  163. }
  164. void text_box_set_text(TextBox* text_box, const char* text) {
  165. furi_assert(text_box);
  166. furi_assert(text);
  167. with_view_model(
  168. text_box->view,
  169. TextBoxModel * model,
  170. {
  171. model->text = text;
  172. furi_string_reset(model->text_formatted);
  173. furi_string_reserve(model->text_formatted, strlen(text));
  174. model->formatted = false;
  175. },
  176. true);
  177. }
  178. void text_box_set_font(TextBox* text_box, TextBoxFont font) {
  179. furi_assert(text_box);
  180. with_view_model(
  181. text_box->view, TextBoxModel * model, { model->font = font; }, true);
  182. }
  183. void text_box_set_focus(TextBox* text_box, TextBoxFocus focus) {
  184. furi_assert(text_box);
  185. with_view_model(
  186. text_box->view, TextBoxModel * model, { model->focus = focus; }, true);
  187. }