text_input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include "text_input.h"
  2. #include <gui/elements.h>
  3. #include <furi.h>
  4. struct TextInput {
  5. View* view;
  6. };
  7. typedef struct {
  8. const char text;
  9. const uint8_t x;
  10. const uint8_t y;
  11. } TextInputKey;
  12. typedef struct {
  13. const char* header;
  14. char* text_buffer;
  15. size_t text_buffer_size;
  16. TextInputCallback callback;
  17. void* callback_context;
  18. uint8_t selected_row;
  19. uint8_t selected_column;
  20. } TextInputModel;
  21. static const uint8_t keyboard_origin_x = 1;
  22. static const uint8_t keyboard_origin_y = 29;
  23. static const uint8_t keyboard_row_count = 3;
  24. #define ENTER_KEY '\r'
  25. #define BACKSPACE_KEY '\b'
  26. static const TextInputKey keyboard_keys_row_1[] = {
  27. {'q', 1, 8},
  28. {'w', 10, 8},
  29. {'e', 19, 8},
  30. {'r', 28, 8},
  31. {'t', 37, 8},
  32. {'y', 46, 8},
  33. {'u', 55, 8},
  34. {'i', 64, 8},
  35. {'o', 73, 8},
  36. {'p', 82, 8},
  37. {'0', 91, 8},
  38. {'1', 100, 8},
  39. {'2', 110, 8},
  40. {'3', 120, 8},
  41. };
  42. static const TextInputKey keyboard_keys_row_2[] = {
  43. {'a', 1, 20},
  44. {'s', 10, 20},
  45. {'d', 19, 20},
  46. {'f', 28, 20},
  47. {'g', 37, 20},
  48. {'h', 46, 20},
  49. {'j', 55, 20},
  50. {'k', 64, 20},
  51. {'l', 73, 20},
  52. {BACKSPACE_KEY, 82, 12},
  53. {'4', 100, 20},
  54. {'5', 110, 20},
  55. {'6', 120, 20},
  56. };
  57. static const TextInputKey keyboard_keys_row_3[] = {
  58. {'z', 1, 32},
  59. {'x', 10, 32},
  60. {'c', 19, 32},
  61. {'v', 28, 32},
  62. {'b', 37, 32},
  63. {'n', 46, 32},
  64. {'m', 55, 32},
  65. {'_', 64, 32},
  66. {ENTER_KEY, 74, 23},
  67. {'7', 100, 32},
  68. {'8', 110, 32},
  69. {'9', 120, 32},
  70. };
  71. static uint8_t get_row_size(uint8_t row_index) {
  72. uint8_t row_size = 0;
  73. switch(row_index + 1) {
  74. case 1:
  75. row_size = sizeof(keyboard_keys_row_1) / sizeof(TextInputKey);
  76. break;
  77. case 2:
  78. row_size = sizeof(keyboard_keys_row_2) / sizeof(TextInputKey);
  79. break;
  80. case 3:
  81. row_size = sizeof(keyboard_keys_row_3) / sizeof(TextInputKey);
  82. break;
  83. }
  84. return row_size;
  85. }
  86. static const TextInputKey* get_row(uint8_t row_index) {
  87. const TextInputKey* row = NULL;
  88. switch(row_index + 1) {
  89. case 1:
  90. row = keyboard_keys_row_1;
  91. break;
  92. case 2:
  93. row = keyboard_keys_row_2;
  94. break;
  95. case 3:
  96. row = keyboard_keys_row_3;
  97. break;
  98. }
  99. return row;
  100. }
  101. static const char get_selected_char(TextInputModel* model) {
  102. return get_row(model->selected_row)[model->selected_column].text;
  103. }
  104. static const bool char_is_lowercase(char letter) {
  105. return (letter >= 0x61 && letter <= 0x7A);
  106. }
  107. static const char char_to_uppercase(const char letter) {
  108. return (letter - 0x20);
  109. }
  110. static void text_input_backspace_cb(TextInputModel* model) {
  111. uint8_t text_length = strlen(model->text_buffer);
  112. if(text_length > 0) {
  113. model->text_buffer[text_length - 1] = 0;
  114. }
  115. }
  116. static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
  117. TextInputModel* model = _model;
  118. uint8_t text_length = strlen(model->text_buffer);
  119. uint8_t needed_string_width = canvas_width(canvas) - 8;
  120. uint8_t start_pos = 4;
  121. const char* text = model->text_buffer;
  122. canvas_clear(canvas);
  123. canvas_set_color(canvas, ColorBlack);
  124. canvas_draw_str(canvas, 2, 8, model->header);
  125. elements_slightly_rounded_frame(canvas, 1, 12, 126, 15);
  126. if(canvas_string_width(canvas, text) > needed_string_width) {
  127. canvas_draw_str(canvas, start_pos, 22, "...");
  128. start_pos += 6;
  129. needed_string_width -= 8;
  130. }
  131. while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
  132. text++;
  133. }
  134. canvas_draw_str(canvas, start_pos, 22, text);
  135. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 22, "|");
  136. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 22, "|");
  137. canvas_set_font(canvas, FontKeyboard);
  138. for(uint8_t row = 0; row <= keyboard_row_count; row++) {
  139. const uint8_t column_count = get_row_size(row);
  140. const TextInputKey* keys = get_row(row);
  141. for(size_t column = 0; column < column_count; column++) {
  142. if(keys[column].text == ENTER_KEY) {
  143. canvas_set_color(canvas, ColorBlack);
  144. if(model->selected_row == row && model->selected_column == column) {
  145. canvas_draw_icon_name(
  146. canvas,
  147. keyboard_origin_x + keys[column].x,
  148. keyboard_origin_y + keys[column].y,
  149. I_KeySaveSelected_24x11);
  150. } else {
  151. canvas_draw_icon_name(
  152. canvas,
  153. keyboard_origin_x + keys[column].x,
  154. keyboard_origin_y + keys[column].y,
  155. I_KeySave_24x11);
  156. }
  157. } else if(keys[column].text == BACKSPACE_KEY) {
  158. canvas_set_color(canvas, ColorBlack);
  159. if(model->selected_row == row && model->selected_column == column) {
  160. canvas_draw_icon_name(
  161. canvas,
  162. keyboard_origin_x + keys[column].x,
  163. keyboard_origin_y + keys[column].y,
  164. I_KeyBackspaceSelected_16x9);
  165. } else {
  166. canvas_draw_icon_name(
  167. canvas,
  168. keyboard_origin_x + keys[column].x,
  169. keyboard_origin_y + keys[column].y,
  170. I_KeyBackspace_16x9);
  171. }
  172. } else {
  173. if(model->selected_row == row && model->selected_column == column) {
  174. canvas_set_color(canvas, ColorBlack);
  175. canvas_draw_box(
  176. canvas,
  177. keyboard_origin_x + keys[column].x - 1,
  178. keyboard_origin_y + keys[column].y - 8,
  179. 7,
  180. 10);
  181. canvas_set_color(canvas, ColorWhite);
  182. } else {
  183. canvas_set_color(canvas, ColorBlack);
  184. }
  185. if(text_length == 0 && char_is_lowercase(keys[column].text)) {
  186. canvas_draw_glyph(
  187. canvas,
  188. keyboard_origin_x + keys[column].x,
  189. keyboard_origin_y + keys[column].y,
  190. char_to_uppercase(keys[column].text));
  191. } else {
  192. canvas_draw_glyph(
  193. canvas,
  194. keyboard_origin_x + keys[column].x,
  195. keyboard_origin_y + keys[column].y,
  196. keys[column].text);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. static void text_input_handle_up(TextInput* text_input) {
  203. with_view_model(
  204. text_input->view, (TextInputModel * model) {
  205. if(model->selected_row > 0) {
  206. model->selected_row--;
  207. if(model->selected_column > get_row_size(model->selected_row) - 5) {
  208. model->selected_column = model->selected_column + 1;
  209. }
  210. }
  211. return true;
  212. });
  213. }
  214. static void text_input_handle_down(TextInput* text_input) {
  215. with_view_model(
  216. text_input->view, (TextInputModel * model) {
  217. if(model->selected_row < keyboard_row_count - 1) {
  218. model->selected_row++;
  219. if(model->selected_column > get_row_size(model->selected_row) - 4) {
  220. model->selected_column = model->selected_column - 1;
  221. }
  222. }
  223. return true;
  224. });
  225. }
  226. static void text_input_handle_left(TextInput* text_input) {
  227. with_view_model(
  228. text_input->view, (TextInputModel * model) {
  229. if(model->selected_column > 0) {
  230. model->selected_column--;
  231. } else {
  232. model->selected_column = get_row_size(model->selected_row) - 1;
  233. }
  234. return true;
  235. });
  236. }
  237. static void text_input_handle_right(TextInput* text_input) {
  238. with_view_model(
  239. text_input->view, (TextInputModel * model) {
  240. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  241. model->selected_column++;
  242. } else {
  243. model->selected_column = 0;
  244. }
  245. return true;
  246. });
  247. }
  248. static void text_input_handle_ok(TextInput* text_input) {
  249. with_view_model(
  250. text_input->view, (TextInputModel * model) {
  251. char selected = get_selected_char(model);
  252. uint8_t text_length = strlen(model->text_buffer);
  253. if(selected == ENTER_KEY) {
  254. if(model->callback != 0 && text_length > 0) {
  255. model->callback(model->callback_context);
  256. }
  257. } else if(selected == BACKSPACE_KEY) {
  258. text_input_backspace_cb(model);
  259. } else if(text_length < (model->text_buffer_size - 1)) {
  260. if(text_length == 0 && char_is_lowercase(selected)) {
  261. selected = char_to_uppercase(selected);
  262. }
  263. model->text_buffer[text_length] = selected;
  264. model->text_buffer[text_length + 1] = 0;
  265. }
  266. return true;
  267. });
  268. }
  269. static bool text_input_view_input_callback(InputEvent* event, void* context) {
  270. TextInput* text_input = context;
  271. furi_assert(text_input);
  272. bool consumed = false;
  273. if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
  274. switch(event->key) {
  275. case InputKeyUp:
  276. text_input_handle_up(text_input);
  277. consumed = true;
  278. break;
  279. case InputKeyDown:
  280. text_input_handle_down(text_input);
  281. consumed = true;
  282. break;
  283. case InputKeyLeft:
  284. text_input_handle_left(text_input);
  285. consumed = true;
  286. break;
  287. case InputKeyRight:
  288. text_input_handle_right(text_input);
  289. consumed = true;
  290. break;
  291. case InputKeyOk:
  292. text_input_handle_ok(text_input);
  293. consumed = true;
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. if((event->type == InputTypeLong || event->type == InputTypeRepeat) &&
  300. event->key == InputKeyBack) {
  301. with_view_model(
  302. text_input->view, (TextInputModel * model) {
  303. text_input_backspace_cb(model);
  304. return true;
  305. });
  306. consumed = true;
  307. }
  308. return consumed;
  309. }
  310. TextInput* text_input_alloc() {
  311. TextInput* text_input = furi_alloc(sizeof(TextInput));
  312. text_input->view = view_alloc();
  313. view_set_context(text_input->view, text_input);
  314. view_allocate_model(text_input->view, ViewModelTypeLocking, sizeof(TextInputModel));
  315. view_set_draw_callback(text_input->view, text_input_view_draw_callback);
  316. view_set_input_callback(text_input->view, text_input_view_input_callback);
  317. with_view_model(
  318. text_input->view, (TextInputModel * model) {
  319. model->text_buffer_size = 0;
  320. model->header = "";
  321. model->selected_row = 0;
  322. model->selected_column = 0;
  323. return true;
  324. });
  325. return text_input;
  326. }
  327. void text_input_free(TextInput* text_input) {
  328. furi_assert(text_input);
  329. view_free(text_input->view);
  330. free(text_input);
  331. }
  332. View* text_input_get_view(TextInput* text_input) {
  333. furi_assert(text_input);
  334. return text_input->view;
  335. }
  336. void text_input_set_result_callback(
  337. TextInput* text_input,
  338. TextInputCallback callback,
  339. void* callback_context,
  340. char* text_buffer,
  341. size_t text_buffer_size) {
  342. with_view_model(
  343. text_input->view, (TextInputModel * model) {
  344. model->callback = callback;
  345. model->callback_context = callback_context;
  346. model->text_buffer = text_buffer;
  347. model->text_buffer_size = text_buffer_size;
  348. return true;
  349. });
  350. }
  351. void text_input_set_header_text(TextInput* text_input, const char* text) {
  352. with_view_model(
  353. text_input->view, (TextInputModel * model) {
  354. model->header = text;
  355. return true;
  356. });
  357. }