view.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma once
  2. #include <input/input.h>
  3. #include "icon_animation.h"
  4. #include "canvas.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /** Hides drawing view_port */
  11. #define VIEW_NONE 0xFFFFFFFF
  12. /** Ignore navigation event */
  13. #define VIEW_IGNORE 0xFFFFFFFE
  14. typedef enum {
  15. ViewOrientationHorizontal,
  16. ViewOrientationVertical,
  17. } ViewOrientation;
  18. /** View, anonymous type */
  19. typedef struct View View;
  20. /** View Draw callback
  21. * @param canvas, pointer to canvas
  22. * @param view_model, pointer to context
  23. * @warning called from GUI thread
  24. */
  25. typedef void (*ViewDrawCallback)(Canvas* canvas, void* model);
  26. /** View Input callback
  27. * @param event, pointer to input event data
  28. * @param context, pointer to context
  29. * @return true if event handled, false if event ignored
  30. * @warning called from GUI thread
  31. */
  32. typedef bool (*ViewInputCallback)(InputEvent* event, void* context);
  33. /** View Custom callback
  34. * @param event, number of custom event
  35. * @param context, pointer to context
  36. * @return true if event handled, false if event ignored
  37. */
  38. typedef bool (*ViewCustomCallback)(uint32_t event, void* context);
  39. /** View navigation callback
  40. * @param context, pointer to context
  41. * @return next view id
  42. * @warning called from GUI thread
  43. */
  44. typedef uint32_t (*ViewNavigationCallback)(void* context);
  45. /** View callback
  46. * @param context, pointer to context
  47. * @warning called from GUI thread
  48. */
  49. typedef void (*ViewCallback)(void* context);
  50. /** View Update Callback
  51. * Called upon model change, need to be propagated to GUI throw ViewPort update
  52. * @param view, pointer to view
  53. * @param context, pointer to context
  54. * @warning called from GUI thread
  55. */
  56. typedef void (*ViewUpdateCallback)(View* view, void* context);
  57. /** View model types */
  58. typedef enum {
  59. /* Model is not allocated */
  60. ViewModelTypeNone,
  61. /* Model consist of atomic types and/or partial update is not critical for rendering.
  62. * Lock free.
  63. */
  64. ViewModelTypeLockFree,
  65. /* Model access is guarded with mutex.
  66. * Locking gui thread.
  67. */
  68. ViewModelTypeLocking,
  69. } ViewModelType;
  70. /** Allocate and init View
  71. * @return View instance
  72. */
  73. View* view_alloc();
  74. /** Free View
  75. * @param View instance
  76. */
  77. void view_free(View* view);
  78. /** Tie IconAnimation with View
  79. * @param view, View instance
  80. * @param icon_animation, IconAnimation instance
  81. */
  82. void view_tie_icon_animation(View* view, IconAnimation* icon_animation);
  83. /** Set View Draw callback
  84. * @param view, View instance
  85. * @param callback, draw callback
  86. */
  87. void view_set_draw_callback(View* view, ViewDrawCallback callback);
  88. /** Set View Input callback
  89. * @param view, View instance
  90. * @param callback, input callback
  91. */
  92. void view_set_input_callback(View* view, ViewInputCallback callback);
  93. /** Set View Custom callback
  94. * @param view, View instance
  95. * @param callback, input callback
  96. */
  97. void view_set_custom_callback(View* view, ViewCustomCallback callback);
  98. /** Set Navigation Previous callback
  99. * @param view, View instance
  100. * @param callback, input callback
  101. */
  102. void view_set_previous_callback(View* view, ViewNavigationCallback callback);
  103. /** Set Enter callback
  104. * @param view, View instance
  105. * @param callback, callback
  106. */
  107. void view_set_enter_callback(View* view, ViewCallback callback);
  108. /** Set Exit callback
  109. * @param view, View instance
  110. * @param callback, callback
  111. */
  112. void view_set_exit_callback(View* view, ViewCallback callback);
  113. /** Set Update callback
  114. * @param view, View instance
  115. * @param callback, callback
  116. */
  117. void view_set_update_callback(View* view, ViewUpdateCallback callback);
  118. /** Set View Draw callback
  119. * @param view, View instance
  120. * @param context, context for callbacks
  121. */
  122. void view_set_update_callback_context(View* view, void* context);
  123. /** Set View Draw callback
  124. * @param view, View instance
  125. * @param context, context for callbacks
  126. */
  127. void view_set_context(View* view, void* context);
  128. /** Set View Orientation
  129. * @param view, View instance
  130. * @param orientation, either vertical or horizontal
  131. */
  132. void view_set_orientation(View* view, ViewOrientation orientation);
  133. /** Allocate view model.
  134. * @param view, View instance
  135. * @param type, View Model Type
  136. * @param size, size
  137. */
  138. void view_allocate_model(View* view, ViewModelType type, size_t size);
  139. /** Free view model data memory.
  140. * @param view, View instance
  141. */
  142. void view_free_model(View* view);
  143. /** Get view model data
  144. * @param view, View instance
  145. * @return pointer to model data
  146. * @warning Don't forget to commit model changes
  147. */
  148. void* view_get_model(View* view);
  149. /** Commit view model
  150. * @param view, View instance
  151. * @param update, true if you want to emit view update, false otherwise
  152. */
  153. void view_commit_model(View* view, bool update);
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #ifdef __cplusplus
  158. #define with_view_model_cpp(view, type, var, function_body) \
  159. { \
  160. type* p = static_cast<type*>(view_get_model(view)); \
  161. bool update = [&](type * var) function_body(p); \
  162. view_commit_model(view, update); \
  163. }
  164. #else
  165. /**
  166. * With clause for view model
  167. * @param view, View instance pointer
  168. * @param function_body a (){} lambda declaration, executed within you parent function context
  169. * @return true if you want to emit view update, false otherwise
  170. */
  171. #define with_view_model(view, function_body) \
  172. { \
  173. void* p = view_get_model(view); \
  174. bool update = ({ bool __fn__ function_body __fn__; })(p); \
  175. view_commit_model(view, update); \
  176. }
  177. #endif