custom_text_input.c 23 KB

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