wifi_marauder_text_input.c 23 KB

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