ui.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. /* =========================== Subview handling ================================
  5. * Note that these are not the Flipper subviews, but the subview system
  6. * implemented inside ProtoView.
  7. * ========================================================================== */
  8. /* Return the ID of the currently selected subview, of the current
  9. * view. */
  10. int get_current_subview(ProtoViewApp *app) {
  11. return app->current_subview[app->current_view];
  12. }
  13. /* Called by view rendering callback that has subviews, to show small triangles
  14. * facing down/up if there are other subviews the user can access with up
  15. * and down. */
  16. void show_available_subviews(Canvas *canvas, ProtoViewApp *app,
  17. int last_subview)
  18. {
  19. int subview = get_current_subview(app);
  20. if (subview != 0)
  21. canvas_draw_triangle(canvas,120,5,8,5,CanvasDirectionBottomToTop);
  22. if (subview != last_subview-1)
  23. canvas_draw_triangle(canvas,120,59,8,5,CanvasDirectionTopToBottom);
  24. }
  25. /* Handle up/down keys when we are in a subview. If the function catched
  26. * such keypress, it returns true, so that the actual view input callback
  27. * knows it can just return ASAP without doing anything. */
  28. bool process_subview_updown(ProtoViewApp *app, InputEvent input, int last_subview) {
  29. int subview = get_current_subview(app);
  30. if (input.type == InputTypePress) {
  31. if (input.key == InputKeyUp) {
  32. if (subview != 0)
  33. app->current_subview[app->current_view]--;
  34. return true;
  35. } else if (input.key == InputKeyDown) {
  36. if (subview != last_subview-1)
  37. app->current_subview[app->current_view]++;
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. /* ============================= Text input ====================================
  44. * Normally we just use our own private UI widgets. However for the text input
  45. * widget, that is quite complex, visualizes a keyboard and must be standardized
  46. * for user coherent experience, we use the one provided by the Flipper
  47. * framework. The following two functions allow to show the keyboard to get
  48. * text and later dismiss it.
  49. * ========================================================================== */
  50. /* Show the keyboard, take the user input and store it into the specified
  51. * 'buffer' of 'buflen' total bytes. When the user is done, the done_callback
  52. * is called passing the application context to it. Such callback needs
  53. * to do whatever it wants with the input buffer and dismissi the keyboard
  54. * calling: dismiss_keyboard(app); */
  55. void show_keyboard(ProtoViewApp *app, char *buffer, uint32_t buflen,
  56. void (*done_callback)(void*))
  57. {
  58. app->show_text_input = true;
  59. app->text_input_buffer = buffer;
  60. app->text_input_buffer_len = buflen;
  61. app->text_input_done_callback = done_callback;
  62. }
  63. void dismiss_keyboard(ProtoViewApp *app) {
  64. view_dispatcher_stop(app->view_dispatcher);
  65. }
  66. /* =========================== Canvas extensions ============================ */
  67. void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color)
  68. {
  69. struct {
  70. uint8_t x; uint8_t y;
  71. } dir[8] = {
  72. {-1,-1},
  73. {0,-1},
  74. {1,-1},
  75. {1,0},
  76. {1,1},
  77. {0,1},
  78. {-1,1},
  79. {-1,0}
  80. };
  81. /* Rotate in all the directions writing the same string to create a
  82. * border, then write the actual string in the other color in the
  83. * middle. */
  84. canvas_set_color(canvas, border_color);
  85. for (int j = 0; j < 8; j++)
  86. canvas_draw_str(canvas,x+dir[j].x,y+dir[j].y,str);
  87. canvas_set_color(canvas, text_color);
  88. canvas_draw_str(canvas,x,y,str);
  89. canvas_set_color(canvas, ColorBlack);
  90. }