view.h 6.0 KB

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