RCTFabricSurface.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTPrimitives.h>
  8. #import <React/RCTSurfaceStage.h>
  9. #import <react/mounting/MountingCoordinator.h>
  10. NS_ASSUME_NONNULL_BEGIN
  11. @class RCTBridge;
  12. @class RCTSurfaceView;
  13. @class RCTSurfacePresenter;
  14. @protocol RCTSurfaceDelegate;
  15. /**
  16. * (This is Fabric-compatible RCTSurface implementation.)
  17. *
  18. * RCTSurface instance represents React Native-powered piece of a user interface
  19. * which can be a full-screen app, separate modal view controller,
  20. * or even small widget.
  21. * It is called "Surface".
  22. *
  23. * The RCTSurface instance is completely thread-safe by design;
  24. * it can be created on any thread, and any its method can be called from
  25. * any thread (if the opposite is not mentioned explicitly).
  26. *
  27. * The primary goals of the RCTSurface are:
  28. * * ability to measure and layout the surface in a thread-safe
  29. * and synchronous manner;
  30. * * ability to create a UIView instance on demand (later);
  31. * * ability to communicate the current stage of the surface granularly.
  32. */
  33. @interface RCTFabricSurface : NSObject
  34. @property (atomic, readonly) RCTSurfaceStage stage;
  35. @property (atomic, readonly) NSString *moduleName;
  36. @property (atomic, readonly) ReactTag rootTag;
  37. @property (atomic, readwrite, weak, nullable) id<RCTSurfaceDelegate> delegate;
  38. @property (atomic, copy, readwrite) NSDictionary *properties;
  39. - (instancetype)initWithSurfacePresenter:(RCTSurfacePresenter *)surfacePresenter
  40. moduleName:(NSString *)moduleName
  41. initialProperties:(NSDictionary *)initialProperties;
  42. #pragma mark - Dealing with UIView representation, the Main thread only access
  43. /**
  44. * Creates (if needed) and returns `UIView` instance which represents the Surface.
  45. * The Surface will cache and *retain* this object.
  46. * Returning the UIView instance does not mean that the Surface is ready
  47. * to execute and layout. It can be just a handler which Surface will use later
  48. * to mount the actual views.
  49. * RCTSurface does not control (or influence in any way) the size or origin
  50. * of this view. Some superview (or another owner) must use other methods
  51. * of this class to setup proper layout and interop interactions with UIKit
  52. * or another UI framework.
  53. * This method must be called only from the main queue.
  54. */
  55. - (RCTSurfaceView *)view;
  56. #pragma mark - Start & Stop
  57. /**
  58. * Starts or stops the Surface.
  59. * A Surface object can be stopped and then restarted.
  60. * The starting process includes initializing all underlying React Native
  61. * infrastructure and running React app.
  62. * Just initialized Surface object starts automatically, there is no need
  63. * to call `start` explicitly. Surface also stops itself on deallocation
  64. * automatically.
  65. * Returns YES in case of success. Returns NO if the Surface is already
  66. * started or stopped.
  67. */
  68. - (BOOL)start;
  69. - (BOOL)stop;
  70. #pragma mark - Layout: Setting the size constrains
  71. /**
  72. * Sets `minimumSize` and `maximumSize` layout constraints for the Surface.
  73. */
  74. - (void)setMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize;
  75. /**
  76. * Previously set `minimumSize` layout constraint.
  77. * Defaults to `{0, 0}`.
  78. */
  79. @property (atomic, assign, readonly) CGSize minimumSize;
  80. /**
  81. * Previously set `maximumSize` layout constraint.
  82. * Defaults to `{CGFLOAT_MAX, CGFLOAT_MAX}`.
  83. */
  84. @property (atomic, assign, readonly) CGSize maximumSize;
  85. /**
  86. * Simple shortcut to `-[RCTSurface setMinimumSize:size maximumSize:size]`.
  87. */
  88. - (void)setSize:(CGSize)size;
  89. #pragma mark - Layout: Measuring
  90. /**
  91. * Measures the Surface with given constraints.
  92. * This method does not cause any side effects on the surface object.
  93. */
  94. - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize;
  95. /**
  96. * Return the current size of the root view based on (but not clamp by) current
  97. * size constraints.
  98. */
  99. @property (atomic, assign, readonly) CGSize intrinsicSize;
  100. #pragma mark - Synchronous waiting
  101. /**
  102. * Synchronously blocks the current thread up to given `timeout` until
  103. * the Surface is rendered.
  104. */
  105. - (BOOL)synchronouslyWaitFor:(NSTimeInterval)timeout;
  106. @end
  107. @interface RCTFabricSurface (Internal)
  108. /**
  109. * Sets and clears given stage flags (bitmask).
  110. * Returns `YES` if the actual state was changed.
  111. */
  112. - (BOOL)_setStage:(RCTSurfaceStage)stage;
  113. - (BOOL)_unsetStage:(RCTSurfaceStage)stage;
  114. @end
  115. @interface RCTFabricSurface (Deprecated)
  116. /**
  117. * Deprecated. Use `initWithSurfacePresenter:moduleName:initialProperties` instead.
  118. */
  119. - (instancetype)initWithBridge:(RCTBridge *)bridge
  120. moduleName:(NSString *)moduleName
  121. initialProperties:(NSDictionary *)initialProperties;
  122. /**
  123. * Deprecated. Use `rootTag` instead.
  124. */
  125. @property (atomic, readonly) NSNumber *rootViewTag;
  126. @end
  127. NS_ASSUME_NONNULL_END