ui.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. *
  56. * Note: if the buffer is not a null-termined zero string, what it contains will
  57. * be used as initial input for the user. */
  58. void show_keyboard(ProtoViewApp *app, char *buffer, uint32_t buflen,
  59. void (*done_callback)(void*))
  60. {
  61. app->show_text_input = true;
  62. app->text_input_buffer = buffer;
  63. app->text_input_buffer_len = buflen;
  64. app->text_input_done_callback = done_callback;
  65. }
  66. void dismiss_keyboard(ProtoViewApp *app) {
  67. view_dispatcher_stop(app->view_dispatcher);
  68. }
  69. /* =========================== Canvas extensions ============================ */
  70. void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color)
  71. {
  72. struct {
  73. uint8_t x; uint8_t y;
  74. } dir[8] = {
  75. {-1,-1},
  76. {0,-1},
  77. {1,-1},
  78. {1,0},
  79. {1,1},
  80. {0,1},
  81. {-1,1},
  82. {-1,0}
  83. };
  84. /* Rotate in all the directions writing the same string to create a
  85. * border, then write the actual string in the other color in the
  86. * middle. */
  87. canvas_set_color(canvas, border_color);
  88. for (int j = 0; j < 8; j++)
  89. canvas_draw_str(canvas,x+dir[j].x,y+dir[j].y,str);
  90. canvas_set_color(canvas, text_color);
  91. canvas_draw_str(canvas,x,y,str);
  92. canvas_set_color(canvas, ColorBlack);
  93. }