view.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #include <input/input.h>
  3. #include "canvas.h"
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* Hides drawing view_port */
  10. #define VIEW_NONE 0xFFFFFFFF
  11. /* Ignore navigation event */
  12. #define VIEW_IGNORE 0xFFFFFFFE
  13. /* Deatch from gui, deallocate Views and ViewDispatcher
  14. * BE SUPER CAREFUL, deallocation happens automatically on GUI thread
  15. * You ARE NOT owning ViewDispatcher and Views instances
  16. */
  17. #define VIEW_DESTROY 0xFFFFFFFA
  18. /* View Draw callback
  19. * @param canvas, pointer to canvas
  20. * @param view_model, pointer to context
  21. * @warning called from GUI thread
  22. */
  23. typedef void (*ViewDrawCallback)(Canvas* canvas, void* model);
  24. /* View Input callback
  25. * @param event, pointer to input event data
  26. * @param context, pointer to context
  27. * @return true if event handled, false if event ignored
  28. * @warning called from GUI thread
  29. */
  30. typedef bool (*ViewInputCallback)(InputEvent* event, void* context);
  31. /* View navigation callback
  32. * @param context, pointer to context
  33. * @return next view id
  34. * @warning called from GUI thread
  35. */
  36. typedef uint32_t (*ViewNavigationCallback)(void* context);
  37. /* View callback
  38. * @param context, pointer to context
  39. * @warning called from GUI thread
  40. */
  41. typedef void (*ViewCallback)(void* context);
  42. /* View model types */
  43. typedef enum {
  44. /* Model is not allocated */
  45. ViewModelTypeNone,
  46. /* Model consist of atomic types and/or partial update is not critical for rendering.
  47. * Lock free.
  48. */
  49. ViewModelTypeLockFree,
  50. /* Model access is guarded with mutex.
  51. * Locking gui thread.
  52. */
  53. ViewModelTypeLocking,
  54. } ViewModelType;
  55. typedef struct View View;
  56. /* Allocate and init View
  57. * @return pointer to View
  58. */
  59. View* view_alloc();
  60. /* Free View
  61. * @param pointer to View
  62. */
  63. void view_free(View* view);
  64. /* Set View Draw callback
  65. * @param view, pointer to View
  66. * @param callback, draw callback
  67. */
  68. void view_set_draw_callback(View* view, ViewDrawCallback callback);
  69. /* Set View Draw callback
  70. * @param view, pointer to View
  71. * @param callback, input callback
  72. */
  73. void view_set_input_callback(View* view, ViewInputCallback callback);
  74. /* Set Navigation Previous callback
  75. * @param view, pointer to View
  76. * @param callback, input callback
  77. */
  78. void view_set_previous_callback(View* view, ViewNavigationCallback callback);
  79. /* Set Navigation Next callback
  80. * @param view, pointer to View
  81. * @param callback, input callback
  82. */
  83. void view_set_next_callback(View* view, ViewNavigationCallback callback);
  84. /* Set Enter callback
  85. * @param view, pointer to View
  86. * @param callback, callback
  87. */
  88. void view_set_enter_callback(View* view, ViewCallback callback);
  89. /* Set Exit callback
  90. * @param view, pointer to View
  91. * @param callback, callback
  92. */
  93. void view_set_exit_callback(View* view, ViewCallback callback);
  94. /* Set View Draw callback
  95. * @param view, pointer to View
  96. * @param context, context for callbacks
  97. */
  98. void view_set_context(View* view, void* context);
  99. /* Allocate view model.
  100. * @param view, pointer to View
  101. * @param type, View Model Type
  102. * @param size, size
  103. */
  104. void view_allocate_model(View* view, ViewModelType type, size_t size);
  105. /* Free view model data memory.
  106. * @param view, pointer to View
  107. */
  108. void view_free_model(View* view);
  109. /* Get view model data
  110. * @param view, pointer to View
  111. * @return pointer to model data
  112. * @warning Don't forget to commit model changes
  113. */
  114. void* view_get_model(View* view);
  115. /* Commit view model
  116. * @param view, pointer to View
  117. * @param update, true if you want to emit view update, false otherwise
  118. */
  119. void view_commit_model(View* view, bool update);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #ifdef __cplusplus
  124. #define with_view_model_cpp(view, type, var, function_body) \
  125. { \
  126. type* p = static_cast<type*>(view_get_model(view)); \
  127. bool update = [&](type * var) function_body(p); \
  128. view_commit_model(view, update); \
  129. }
  130. #else
  131. /*
  132. * With clause for view model
  133. * @param view, View instance pointer
  134. * @param function_body a (){} lambda declaration, executed within you parent function context
  135. * @return true if you want to emit view update, false otherwise
  136. */
  137. #define with_view_model(view, function_body) \
  138. { \
  139. void* p = view_get_model(view); \
  140. bool update = ({ bool __fn__ function_body __fn__; })(p); \
  141. view_commit_model(view, update); \
  142. }
  143. #endif