text_input.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. #include "text_input.h"
  2. #include <gui/elements.h>
  3. #if __has_include(<assets_icons.h>)
  4. #include <assets_icons.h>
  5. #else
  6. extern const Icon I_KeySaveSelected_22x11;
  7. extern const Icon I_KeySave_22x11;
  8. extern const Icon I_KeyKeyboardSelected_10x11;
  9. extern const Icon I_KeyKeyboard_10x11;
  10. extern const Icon I_KeyBackspaceSelected_17x11;
  11. extern const Icon I_KeyBackspace_17x11;
  12. extern const Icon I_WarningDolphin_45x42;
  13. #endif
  14. #include <furi.h>
  15. struct TextInput {
  16. View* view;
  17. FuriTimer* timer;
  18. };
  19. typedef struct {
  20. const char text;
  21. const uint8_t x;
  22. const uint8_t y;
  23. } TextInputKey;
  24. typedef struct {
  25. const TextInputKey* rows[3];
  26. const uint8_t keyboard_index;
  27. } Keyboard;
  28. typedef struct {
  29. const char* header;
  30. char* text_buffer;
  31. size_t text_buffer_size;
  32. size_t minimum_length;
  33. bool clear_default_text;
  34. TextInputCallback callback;
  35. void* callback_context;
  36. uint8_t selected_row;
  37. uint8_t selected_column;
  38. TextInputValidatorCallback validator_callback;
  39. void* validator_callback_context;
  40. FuriString* validator_text;
  41. bool validator_message_visible;
  42. bool illegal_symbols;
  43. bool cursor_select;
  44. uint8_t selected_keyboard;
  45. size_t cursor_pos;
  46. } TextInputModel;
  47. static const uint8_t keyboard_origin_x = 1;
  48. static const uint8_t keyboard_origin_y = 29;
  49. static const uint8_t keyboard_row_count = 3;
  50. static const uint8_t keyboard_count = 2;
  51. #define ENTER_KEY '\r'
  52. #define BACKSPACE_KEY '\b'
  53. #define SWITCH_KEYBOARD_KEY '\t'
  54. static const TextInputKey keyboard_keys_row_1[] = {
  55. {'q', 1, 8},
  56. {'w', 10, 8},
  57. {'e', 19, 8},
  58. {'r', 28, 8},
  59. {'t', 37, 8},
  60. {'y', 46, 8},
  61. {'u', 55, 8},
  62. {'i', 64, 8},
  63. {'o', 73, 8},
  64. {'p', 82, 8},
  65. {'0', 92, 8},
  66. {'1', 102, 8},
  67. {'2', 111, 8},
  68. {'3', 120, 8},
  69. };
  70. static const TextInputKey keyboard_keys_row_2[] = {
  71. {'a', 1, 20},
  72. {'s', 10, 20},
  73. {'d', 19, 20},
  74. {'f', 28, 20},
  75. {'g', 37, 20},
  76. {'h', 46, 20},
  77. {'j', 55, 20},
  78. {'k', 64, 20},
  79. {'l', 73, 20},
  80. {BACKSPACE_KEY, 82, 11},
  81. {'4', 102, 20},
  82. {'5', 111, 20},
  83. {'6', 120, 20},
  84. };
  85. static const TextInputKey keyboard_keys_row_3[] = {
  86. {SWITCH_KEYBOARD_KEY, 0, 23},
  87. {'z', 13, 32},
  88. {'x', 21, 32},
  89. {'c', 29, 32},
  90. {'v', 37, 32},
  91. {'b', 45, 32},
  92. {'n', 53, 32},
  93. {'m', 61, 32},
  94. {'_', 69, 32},
  95. {ENTER_KEY, 77, 23},
  96. {'7', 102, 32},
  97. {'8', 111, 32},
  98. {'9', 120, 32},
  99. };
  100. static const TextInputKey symbol_keyboard_keys_row_1[] = {
  101. {'!', 2, 8},
  102. {'@', 12, 8},
  103. {'#', 22, 8},
  104. {'$', 32, 8},
  105. {'%', 42, 8},
  106. {'^', 52, 8},
  107. {'&', 62, 8},
  108. {'(', 71, 8},
  109. {')', 81, 8},
  110. {'0', 92, 8},
  111. {'1', 102, 8},
  112. {'2', 111, 8},
  113. {'3', 120, 8},
  114. };
  115. static const TextInputKey symbol_keyboard_keys_row_2[] = {
  116. {'~', 2, 20},
  117. {'+', 12, 20},
  118. {'-', 22, 20},
  119. {'=', 32, 20},
  120. {'[', 42, 20},
  121. {']', 52, 20},
  122. {'{', 62, 20},
  123. {'}', 72, 20},
  124. {BACKSPACE_KEY, 82, 11},
  125. {'4', 102, 20},
  126. {'5', 111, 20},
  127. {'6', 120, 20},
  128. };
  129. static const TextInputKey symbol_keyboard_keys_row_3[] = {
  130. {SWITCH_KEYBOARD_KEY, 0, 23},
  131. {'.', 15, 32},
  132. {',', 29, 32},
  133. {';', 41, 32},
  134. {'`', 53, 32},
  135. {'\'', 65, 32},
  136. {ENTER_KEY, 77, 23},
  137. {'7', 102, 32},
  138. {'8', 111, 32},
  139. {'9', 120, 32},
  140. };
  141. static const Keyboard keyboard = {
  142. .rows =
  143. {
  144. keyboard_keys_row_1,
  145. keyboard_keys_row_2,
  146. keyboard_keys_row_3,
  147. },
  148. .keyboard_index = 0,
  149. };
  150. static const Keyboard symbol_keyboard = {
  151. .rows =
  152. {
  153. symbol_keyboard_keys_row_1,
  154. symbol_keyboard_keys_row_2,
  155. symbol_keyboard_keys_row_3,
  156. },
  157. .keyboard_index = 1,
  158. };
  159. static const Keyboard* keyboards[] = {
  160. &keyboard,
  161. &symbol_keyboard,
  162. };
  163. static void switch_keyboard(TextInputModel* model) {
  164. model->selected_keyboard = (model->selected_keyboard + 1) % keyboard_count;
  165. }
  166. static uint8_t get_row_size(const Keyboard* keyboard, uint8_t row_index) {
  167. uint8_t row_size = 0;
  168. if(keyboard == &symbol_keyboard) {
  169. switch(row_index + 1) {
  170. case 1:
  171. row_size = COUNT_OF(symbol_keyboard_keys_row_1);
  172. break;
  173. case 2:
  174. row_size = COUNT_OF(symbol_keyboard_keys_row_2);
  175. break;
  176. case 3:
  177. row_size = COUNT_OF(symbol_keyboard_keys_row_3);
  178. break;
  179. default:
  180. furi_crash();
  181. }
  182. } else {
  183. switch(row_index + 1) {
  184. case 1:
  185. row_size = COUNT_OF(keyboard_keys_row_1);
  186. break;
  187. case 2:
  188. row_size = COUNT_OF(keyboard_keys_row_2);
  189. break;
  190. case 3:
  191. row_size = COUNT_OF(keyboard_keys_row_3);
  192. break;
  193. default:
  194. furi_crash();
  195. }
  196. }
  197. return row_size;
  198. }
  199. static const TextInputKey* get_row(const Keyboard* keyboard, uint8_t row_index) {
  200. const TextInputKey* row = NULL;
  201. if(row_index < 3) {
  202. row = keyboard->rows[row_index];
  203. } else {
  204. furi_crash();
  205. }
  206. return row;
  207. }
  208. static char get_selected_char(TextInputModel* model) {
  209. return get_row(
  210. keyboards[model->selected_keyboard], model->selected_row)[model->selected_column]
  211. .text;
  212. }
  213. static bool char_is_lowercase(char letter) {
  214. return letter >= 0x61 && letter <= 0x7A;
  215. }
  216. static char char_to_uppercase(const char letter) {
  217. if(letter == '_') {
  218. return 0x20;
  219. } else if(char_is_lowercase(letter)) {
  220. return letter - 0x20;
  221. } else {
  222. return letter;
  223. }
  224. }
  225. static char char_to_illegal_symbol(char original) {
  226. switch(original) {
  227. default:
  228. return original;
  229. case '0':
  230. return '_';
  231. case '1':
  232. return '<';
  233. case '2':
  234. return '>';
  235. case '3':
  236. return ':';
  237. case '4':
  238. return '"';
  239. case '5':
  240. return '/';
  241. case '6':
  242. return '\\';
  243. case '7':
  244. return '|';
  245. case '8':
  246. return '?';
  247. case '9':
  248. return '*';
  249. }
  250. }
  251. static void text_input_backspace_cb(TextInputModel* model) {
  252. if(model->clear_default_text) {
  253. model->text_buffer[0] = 0;
  254. model->cursor_pos = 0;
  255. } else if(model->cursor_pos > 0) {
  256. char* move = model->text_buffer + model->cursor_pos;
  257. memmove(move - 1, move, strlen(move) + 1);
  258. model->cursor_pos--;
  259. }
  260. }
  261. static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
  262. TextInputModel* model = _model;
  263. uint8_t text_length = model->text_buffer ? strlen(model->text_buffer) : 0;
  264. uint8_t needed_string_width = canvas_width(canvas) - 8;
  265. uint8_t start_pos = 4;
  266. model->cursor_pos = model->cursor_pos > text_length ? text_length : model->cursor_pos;
  267. size_t cursor_pos = model->cursor_pos;
  268. canvas_clear(canvas);
  269. canvas_set_color(canvas, ColorBlack);
  270. canvas_draw_str(canvas, 2, 8, model->header);
  271. elements_slightly_rounded_frame(canvas, 1, 12, 126, 15);
  272. char buf[text_length + 1];
  273. if(model->text_buffer) {
  274. strlcpy(buf, model->text_buffer, sizeof(buf));
  275. }
  276. char* str = buf;
  277. if(model->clear_default_text) {
  278. elements_slightly_rounded_box(
  279. canvas, start_pos - 1, 14, canvas_string_width(canvas, str) + 2, 10);
  280. canvas_set_color(canvas, ColorWhite);
  281. } else {
  282. char* move = str + cursor_pos;
  283. memmove(move + 1, move, strlen(move) + 1);
  284. str[cursor_pos] = '|';
  285. }
  286. if(cursor_pos > 0 && canvas_string_width(canvas, str) > needed_string_width) {
  287. canvas_draw_str(canvas, start_pos, 22, "...");
  288. start_pos += 6;
  289. needed_string_width -= 8;
  290. for(uint32_t off = 0;
  291. strlen(str) && canvas_string_width(canvas, str) > needed_string_width &&
  292. off < cursor_pos;
  293. off++) {
  294. str++;
  295. }
  296. }
  297. if(canvas_string_width(canvas, str) > needed_string_width) {
  298. needed_string_width -= 4;
  299. size_t len = strlen(str);
  300. while(len && canvas_string_width(canvas, str) > needed_string_width) {
  301. str[len--] = '\0';
  302. }
  303. strlcat(str, "...", sizeof(buf) - (str - buf));
  304. }
  305. canvas_draw_str(canvas, start_pos, 22, str);
  306. canvas_set_font(canvas, FontKeyboard);
  307. bool uppercase = model->clear_default_text || text_length == 0;
  308. bool symbols = model->selected_keyboard == symbol_keyboard.keyboard_index;
  309. for(uint8_t row = 0; row < keyboard_row_count; row++) {
  310. const uint8_t column_count = get_row_size(keyboards[model->selected_keyboard], row);
  311. const TextInputKey* keys = get_row(keyboards[model->selected_keyboard], row);
  312. for(size_t column = 0; column < column_count; column++) {
  313. bool selected = !model->cursor_select && model->selected_row == row &&
  314. model->selected_column == column;
  315. const Icon* icon = NULL;
  316. if(keys[column].text == ENTER_KEY) {
  317. icon = selected ? &I_KeySaveSelected_22x11 : &I_KeySave_22x11;
  318. } else if(keys[column].text == SWITCH_KEYBOARD_KEY) {
  319. icon = selected ? &I_KeyKeyboardSelected_10x11 : &I_KeyKeyboard_10x11;
  320. } else if(keys[column].text == BACKSPACE_KEY) {
  321. icon = selected ? &I_KeyBackspaceSelected_17x11 : &I_KeyBackspace_17x11;
  322. }
  323. canvas_set_color(canvas, ColorBlack);
  324. if(icon != NULL) {
  325. canvas_draw_icon(
  326. canvas,
  327. keyboard_origin_x + keys[column].x,
  328. keyboard_origin_y + keys[column].y,
  329. icon);
  330. } else {
  331. if(selected) {
  332. elements_slightly_rounded_box(
  333. canvas,
  334. keyboard_origin_x + keys[column].x - 2,
  335. keyboard_origin_y + keys[column].y - 9,
  336. 9,
  337. 11);
  338. canvas_set_color(canvas, ColorWhite);
  339. }
  340. char glyph = keys[column].text;
  341. if(uppercase && !symbols) {
  342. canvas_draw_glyph(
  343. canvas,
  344. keyboard_origin_x + keys[column].x,
  345. keyboard_origin_y + keys[column].y,
  346. char_to_uppercase(glyph));
  347. } else {
  348. canvas_draw_glyph(
  349. canvas,
  350. keyboard_origin_x + keys[column].x,
  351. keyboard_origin_y + keys[column].y -
  352. (glyph == '_' || char_is_lowercase(glyph)),
  353. (symbols && model->illegal_symbols) ? char_to_illegal_symbol(glyph) :
  354. glyph);
  355. }
  356. }
  357. }
  358. }
  359. if(model->validator_message_visible) {
  360. canvas_set_font(canvas, FontSecondary);
  361. canvas_set_color(canvas, ColorWhite);
  362. canvas_draw_box(canvas, 8, 10, 110, 48);
  363. canvas_set_color(canvas, ColorBlack);
  364. canvas_draw_icon(canvas, 10, 14, &I_WarningDolphin_45x42);
  365. canvas_draw_rframe(canvas, 8, 8, 112, 50, 3);
  366. canvas_draw_rframe(canvas, 9, 9, 110, 48, 2);
  367. elements_multiline_text(canvas, 62, 20, furi_string_get_cstr(model->validator_text));
  368. canvas_set_font(canvas, FontKeyboard);
  369. }
  370. }
  371. static void text_input_handle_up(TextInput* text_input, TextInputModel* model) {
  372. UNUSED(text_input);
  373. if(model->selected_row > 0) {
  374. model->selected_row--;
  375. if(model->selected_row == 0 &&
  376. model->selected_column >
  377. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 6) {
  378. model->selected_column = model->selected_column + 1;
  379. }
  380. if(model->selected_row == 1 &&
  381. model->selected_keyboard == symbol_keyboard.keyboard_index) {
  382. if(model->selected_column > 5)
  383. model->selected_column += 2;
  384. else if(model->selected_column > 1)
  385. model->selected_column += 1;
  386. }
  387. } else {
  388. model->cursor_select = true;
  389. model->clear_default_text = false;
  390. }
  391. }
  392. static void text_input_handle_down(TextInput* text_input, TextInputModel* model) {
  393. UNUSED(text_input);
  394. if(model->cursor_select) {
  395. model->cursor_select = false;
  396. } else if(model->selected_row < keyboard_row_count - 1) {
  397. model->selected_row++;
  398. if(model->selected_row == 1 &&
  399. model->selected_column >
  400. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 4) {
  401. model->selected_column = model->selected_column - 1;
  402. }
  403. if(model->selected_row == 2 &&
  404. model->selected_keyboard == symbol_keyboard.keyboard_index) {
  405. if(model->selected_column > 6)
  406. model->selected_column -= 2;
  407. else if(model->selected_column > 1)
  408. model->selected_column -= 1;
  409. }
  410. }
  411. }
  412. static void text_input_handle_left(TextInput* text_input, TextInputModel* model) {
  413. UNUSED(text_input);
  414. if(model->cursor_select) {
  415. model->clear_default_text = false;
  416. if(model->cursor_pos > 0) {
  417. model->cursor_pos = CLAMP(model->cursor_pos - 1, strlen(model->text_buffer), 0u);
  418. }
  419. } else if(model->selected_column > 0) {
  420. model->selected_column--;
  421. } else {
  422. model->selected_column =
  423. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 1;
  424. }
  425. }
  426. static void text_input_handle_right(TextInput* text_input, TextInputModel* model) {
  427. UNUSED(text_input);
  428. if(model->cursor_select) {
  429. model->clear_default_text = false;
  430. model->cursor_pos = CLAMP(model->cursor_pos + 1, strlen(model->text_buffer), 0u);
  431. } else if(
  432. model->selected_column <
  433. get_row_size(keyboards[model->selected_keyboard], model->selected_row) - 1) {
  434. model->selected_column++;
  435. } else {
  436. model->selected_column = 0;
  437. }
  438. }
  439. static void text_input_handle_ok(TextInput* text_input, TextInputModel* model, InputType type) {
  440. if(model->cursor_select) {
  441. model->clear_default_text = !model->clear_default_text;
  442. return;
  443. }
  444. bool shift = type == InputTypeLong;
  445. bool repeat = type == InputTypeRepeat;
  446. char selected = get_selected_char(model);
  447. size_t text_length = strlen(model->text_buffer);
  448. if(selected == ENTER_KEY) {
  449. if(model->validator_callback &&
  450. (!model->validator_callback(
  451. model->text_buffer, model->validator_text, model->validator_callback_context))) {
  452. model->validator_message_visible = true;
  453. furi_timer_start(text_input->timer, furi_kernel_get_tick_frequency() * 4);
  454. } else if(model->callback != 0 && text_length >= model->minimum_length) {
  455. model->callback(model->callback_context);
  456. }
  457. } else if(selected == SWITCH_KEYBOARD_KEY) {
  458. switch_keyboard(model);
  459. } else {
  460. if(selected == BACKSPACE_KEY) {
  461. text_input_backspace_cb(model);
  462. } else if(!repeat) {
  463. if(model->clear_default_text) {
  464. text_length = 0;
  465. }
  466. if(text_length < (model->text_buffer_size - 1)) {
  467. if(shift != (text_length == 0) &&
  468. model->selected_keyboard != symbol_keyboard.keyboard_index) {
  469. selected = char_to_uppercase(selected);
  470. }
  471. if(model->selected_keyboard == symbol_keyboard.keyboard_index &&
  472. model->illegal_symbols) {
  473. selected = char_to_illegal_symbol(selected);
  474. }
  475. if(model->clear_default_text) {
  476. model->text_buffer[0] = selected;
  477. model->text_buffer[1] = '\0';
  478. model->cursor_pos = 1;
  479. } else {
  480. char* move = model->text_buffer + model->cursor_pos;
  481. memmove(move + 1, move, strlen(move) + 1);
  482. model->text_buffer[model->cursor_pos] = selected;
  483. model->cursor_pos++;
  484. }
  485. }
  486. }
  487. model->clear_default_text = false;
  488. }
  489. }
  490. static bool text_input_view_input_callback(InputEvent* event, void* context) {
  491. TextInput* text_input = context;
  492. furi_assert(text_input);
  493. bool consumed = false;
  494. // Acquire model
  495. TextInputModel* model = view_get_model(text_input->view);
  496. if((!(event->type == InputTypePress) && !(event->type == InputTypeRelease)) &&
  497. model->validator_message_visible) {
  498. model->validator_message_visible = false;
  499. consumed = true;
  500. } else if(event->type == InputTypeShort) {
  501. consumed = true;
  502. switch(event->key) {
  503. case InputKeyUp:
  504. text_input_handle_up(text_input, model);
  505. break;
  506. case InputKeyDown:
  507. text_input_handle_down(text_input, model);
  508. break;
  509. case InputKeyLeft:
  510. text_input_handle_left(text_input, model);
  511. break;
  512. case InputKeyRight:
  513. text_input_handle_right(text_input, model);
  514. break;
  515. case InputKeyOk:
  516. text_input_handle_ok(text_input, model, event->type);
  517. break;
  518. default:
  519. consumed = false;
  520. break;
  521. }
  522. } else if(event->type == InputTypeLong) {
  523. consumed = true;
  524. switch(event->key) {
  525. case InputKeyUp:
  526. text_input_handle_up(text_input, model);
  527. break;
  528. case InputKeyDown:
  529. text_input_handle_down(text_input, model);
  530. break;
  531. case InputKeyLeft:
  532. text_input_handle_left(text_input, model);
  533. break;
  534. case InputKeyRight:
  535. text_input_handle_right(text_input, model);
  536. break;
  537. case InputKeyOk:
  538. text_input_handle_ok(text_input, model, event->type);
  539. break;
  540. case InputKeyBack:
  541. text_input_backspace_cb(model);
  542. break;
  543. default:
  544. consumed = false;
  545. break;
  546. }
  547. } else if(event->type == InputTypeRepeat) {
  548. consumed = true;
  549. switch(event->key) {
  550. case InputKeyUp:
  551. text_input_handle_up(text_input, model);
  552. break;
  553. case InputKeyDown:
  554. text_input_handle_down(text_input, model);
  555. break;
  556. case InputKeyLeft:
  557. text_input_handle_left(text_input, model);
  558. break;
  559. case InputKeyRight:
  560. text_input_handle_right(text_input, model);
  561. break;
  562. case InputKeyOk:
  563. text_input_handle_ok(text_input, model, event->type);
  564. break;
  565. case InputKeyBack:
  566. text_input_backspace_cb(model);
  567. break;
  568. default:
  569. consumed = false;
  570. break;
  571. }
  572. }
  573. // Commit model
  574. view_commit_model(text_input->view, consumed);
  575. return consumed;
  576. }
  577. void text_input_timer_callback(void* context) {
  578. furi_assert(context);
  579. TextInput* text_input = context;
  580. with_view_model(
  581. text_input->view,
  582. TextInputModel * model,
  583. { model->validator_message_visible = false; },
  584. true);
  585. }
  586. TextInput* text_input_alloc(void) {
  587. TextInput* text_input = malloc(sizeof(TextInput));
  588. text_input->view = view_alloc();
  589. view_set_context(text_input->view, text_input);
  590. view_allocate_model(text_input->view, ViewModelTypeLocking, sizeof(TextInputModel));
  591. view_set_draw_callback(text_input->view, text_input_view_draw_callback);
  592. view_set_input_callback(text_input->view, text_input_view_input_callback);
  593. text_input->timer = furi_timer_alloc(text_input_timer_callback, FuriTimerTypeOnce, text_input);
  594. with_view_model(
  595. text_input->view,
  596. TextInputModel * model,
  597. {
  598. model->validator_text = furi_string_alloc();
  599. model->minimum_length = 1;
  600. model->illegal_symbols = false;
  601. model->cursor_pos = 0;
  602. model->cursor_select = false;
  603. },
  604. false);
  605. text_input_reset(text_input);
  606. return text_input;
  607. }
  608. void text_input_free(TextInput* text_input) {
  609. furi_check(text_input);
  610. with_view_model(
  611. text_input->view,
  612. TextInputModel * model,
  613. { furi_string_free(model->validator_text); },
  614. false);
  615. // Send stop command
  616. furi_timer_stop(text_input->timer);
  617. // Release allocated memory
  618. furi_timer_free(text_input->timer);
  619. view_free(text_input->view);
  620. free(text_input);
  621. }
  622. void text_input_reset(TextInput* text_input) {
  623. furi_check(text_input);
  624. with_view_model(
  625. text_input->view,
  626. TextInputModel * model,
  627. {
  628. model->header = "";
  629. model->selected_row = 0;
  630. model->selected_column = 0;
  631. model->selected_keyboard = 0;
  632. model->minimum_length = 1;
  633. model->illegal_symbols = false;
  634. model->clear_default_text = false;
  635. model->cursor_pos = 0;
  636. model->cursor_select = false;
  637. model->text_buffer = NULL;
  638. model->text_buffer_size = 0;
  639. model->callback = NULL;
  640. model->callback_context = NULL;
  641. model->validator_callback = NULL;
  642. model->validator_callback_context = NULL;
  643. furi_string_reset(model->validator_text);
  644. model->validator_message_visible = false;
  645. },
  646. true);
  647. }
  648. View* text_input_get_view(TextInput* text_input) {
  649. furi_check(text_input);
  650. return text_input->view;
  651. }
  652. void text_input_set_result_callback(
  653. TextInput* text_input,
  654. TextInputCallback callback,
  655. void* callback_context,
  656. char* text_buffer,
  657. size_t text_buffer_size,
  658. bool clear_default_text) {
  659. furi_check(text_input);
  660. with_view_model(
  661. text_input->view,
  662. TextInputModel * model,
  663. {
  664. model->callback = callback;
  665. model->callback_context = callback_context;
  666. model->text_buffer = text_buffer;
  667. model->text_buffer_size = text_buffer_size;
  668. model->clear_default_text = clear_default_text;
  669. model->cursor_select = false;
  670. if(text_buffer && text_buffer[0] != '\0') {
  671. model->cursor_pos = strlen(text_buffer);
  672. // Set focus on Save
  673. model->selected_row = 2;
  674. model->selected_column = 9;
  675. model->selected_keyboard = 0;
  676. } else {
  677. model->cursor_pos = 0;
  678. }
  679. },
  680. true);
  681. }
  682. void text_input_set_minimum_length(TextInput* text_input, size_t minimum_length) {
  683. furi_check(text_input);
  684. with_view_model(
  685. text_input->view,
  686. TextInputModel * model,
  687. { model->minimum_length = minimum_length; },
  688. true);
  689. }
  690. void text_input_show_illegal_symbols(TextInput* text_input, bool show) {
  691. furi_check(text_input);
  692. with_view_model(
  693. text_input->view, TextInputModel * model, { model->illegal_symbols = show; }, true);
  694. }
  695. void text_input_set_validator(
  696. TextInput* text_input,
  697. TextInputValidatorCallback callback,
  698. void* callback_context) {
  699. furi_check(text_input);
  700. with_view_model(
  701. text_input->view,
  702. TextInputModel * model,
  703. {
  704. model->validator_callback = callback;
  705. model->validator_callback_context = callback_context;
  706. },
  707. true);
  708. }
  709. TextInputValidatorCallback text_input_get_validator_callback(TextInput* text_input) {
  710. furi_check(text_input);
  711. TextInputValidatorCallback validator_callback = NULL;
  712. with_view_model(
  713. text_input->view,
  714. TextInputModel * model,
  715. { validator_callback = model->validator_callback; },
  716. false);
  717. return validator_callback;
  718. }
  719. void* text_input_get_validator_callback_context(TextInput* text_input) {
  720. furi_check(text_input);
  721. void* validator_callback_context = NULL;
  722. with_view_model(
  723. text_input->view,
  724. TextInputModel * model,
  725. { validator_callback_context = model->validator_callback_context; },
  726. false);
  727. return validator_callback_context;
  728. }
  729. void text_input_set_header_text(TextInput* text_input, const char* text) {
  730. furi_check(text_input);
  731. with_view_model(text_input->view, TextInputModel * model, { model->header = text; }, true);
  732. }