RCTSurface.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <Foundation/Foundation.h>
  8. #import <React/RCTSurfaceStage.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. @class RCTBridge;
  11. @class RCTSurfaceView;
  12. @protocol RCTSurfaceDelegate;
  13. /**
  14. * RCTSurface instance represents React Native-powered piece of a user interface
  15. * which can be a full-screen app, separate modal view controller,
  16. * or even small widget.
  17. * It is called "Surface".
  18. *
  19. * The RCTSurface instance is completely thread-safe by design;
  20. * it can be created on any thread, and any its method can be called from
  21. * any thread (if the opposite is not mentioned explicitly).
  22. *
  23. * The primary goals of the RCTSurface are:
  24. * * ability to measure and layout the surface in a thread-safe
  25. * and synchronous manner;
  26. * * ability to create a UIView instance on demand (later);
  27. * * ability to communicate the current stage of the surface granularly.
  28. */
  29. @interface RCTSurface : NSObject
  30. @property (atomic, readonly) RCTSurfaceStage stage;
  31. @property (atomic, readonly) NSString *moduleName;
  32. @property (atomic, readonly) NSNumber *rootViewTag;
  33. @property (atomic, readwrite, weak, nullable) id<RCTSurfaceDelegate> delegate;
  34. @property (atomic, copy, readwrite) NSDictionary *properties;
  35. - (instancetype)initWithBridge:(RCTBridge *)bridge
  36. moduleName:(NSString *)moduleName
  37. initialProperties:(NSDictionary *)initialProperties;
  38. #pragma mark - Dealing with UIView representation, the Main thread only access
  39. /**
  40. * Creates (if needed) and returns `UIView` instance which represents the Surface.
  41. * The Surface will cache and *retain* this object.
  42. * Returning the UIView instance does not mean that the Surface is ready
  43. * to execute and layout. It can be just a handler which Surface will use later
  44. * to mount the actual views.
  45. * RCTSurface does not control (or influence in any way) the size or origin
  46. * of this view. Some superview (or another owner) must use other methods
  47. * of this class to setup proper layout and interop interactions with UIKit
  48. * or another UI framework.
  49. * This method must be called only from the main queue.
  50. */
  51. - (RCTSurfaceView *)view;
  52. #pragma mark - Layout: Setting the size constrains
  53. /**
  54. * Sets `minimumSize` and `maximumSize` layout constraints for the Surface.
  55. */
  56. - (void)setMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize;
  57. /**
  58. * Previously set `minimumSize` layout constraint.
  59. * Defaults to `{0, 0}`.
  60. */
  61. @property (atomic, assign, readonly) CGSize minimumSize;
  62. /**
  63. * Previously set `maximumSize` layout constraint.
  64. * Defaults to `{CGFLOAT_MAX, CGFLOAT_MAX}`.
  65. */
  66. @property (atomic, assign, readonly) CGSize maximumSize;
  67. /**
  68. * Simple shortcut to `-[RCTSurface setMinimumSize:size maximumSize:size]`.
  69. */
  70. - (void)setSize:(CGSize)size;
  71. #pragma mark - Layout: Measuring
  72. /**
  73. * Measures the Surface with given constraints.
  74. * This method does not cause any side effects on the surface object.
  75. */
  76. - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize;
  77. /**
  78. * Return the current size of the root view based on (but not clamp by) current
  79. * size constraints.
  80. */
  81. @property (atomic, assign, readonly) CGSize intrinsicSize;
  82. #pragma mark - Synchronous waiting
  83. /**
  84. * Synchronously blocks the current thread up to given `timeout` until
  85. * the Surface reaches `stage`.
  86. * Limitations:
  87. * - Do nothing, if called on `UIManager` queue.
  88. * - Calling on the main queue with `RCTSurfaceStageSurfaceDidInitialMounting`
  89. * stage temporary is not supported; in this case the stage will be
  90. * downgraded to `RCTSurfaceStageSurfaceDidInitialLayout`.
  91. */
  92. - (BOOL)synchronouslyWaitForStage:(RCTSurfaceStage)stage timeout:(NSTimeInterval)timeout;
  93. #pragma mark - Start & Stop
  94. /**
  95. * Starts or stops the Surface.
  96. * Those methods are a no-op for regular RCTSurface (for now), but all call sites must call them appropriately.
  97. */
  98. - (BOOL)start;
  99. - (BOOL)stop;
  100. #pragma mark - Mounting/Unmounting of React components
  101. /**
  102. * Mount the React component specified by the given moduleName. This is typically
  103. * calling runApplication.js from the native side.
  104. */
  105. - (void)mountReactComponentWithBridge:(RCTBridge *)bridge
  106. moduleName:(NSString *)moduleName
  107. params:(NSDictionary *)params;
  108. /**
  109. * Unmount the React component specified by the given rootViewTag, called from native.
  110. */
  111. - (void)unmountReactComponentWithBridge:(RCTBridge *)bridge rootViewTag:(NSNumber *)rootViewTag;
  112. @end
  113. NS_ASSUME_NONNULL_END