RCTSurfaceView.mm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "RCTSurfaceView.h"
  8. #import "RCTSurfaceView+Internal.h"
  9. #import "RCTDefines.h"
  10. #import "RCTSurface.h"
  11. #import "RCTSurfaceRootView.h"
  12. @implementation RCTSurfaceView {
  13. RCTSurfaceRootView *_Nullable _rootView;
  14. RCTSurfaceStage _stage;
  15. }
  16. RCT_NOT_IMPLEMENTED(-(instancetype)init)
  17. RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
  18. RCT_NOT_IMPLEMENTED(-(nullable instancetype)initWithCoder : (NSCoder *)coder)
  19. - (instancetype)initWithSurface:(RCTSurface *)surface
  20. {
  21. if (self = [super initWithFrame:CGRectZero]) {
  22. _stage = surface.stage;
  23. _surface = surface;
  24. }
  25. return self;
  26. }
  27. #pragma mark - Internal Interface
  28. - (void)setRootView:(RCTSurfaceRootView *)rootView
  29. {
  30. if (_rootView == rootView) {
  31. return;
  32. }
  33. [_rootView removeFromSuperview];
  34. _rootView = rootView;
  35. [self _updateStage];
  36. }
  37. - (RCTSurfaceRootView *)rootView
  38. {
  39. return _rootView;
  40. }
  41. #pragma mark - stage
  42. - (void)setStage:(RCTSurfaceStage)stage
  43. {
  44. if (stage == _stage) {
  45. return;
  46. }
  47. _stage = stage;
  48. [self _updateStage];
  49. }
  50. - (RCTSurfaceStage)stage
  51. {
  52. return _stage;
  53. }
  54. #pragma mark - Private
  55. - (void)_updateStage
  56. {
  57. if (RCTSurfaceStageIsRunning(_stage)) {
  58. if (_rootView.superview != self) {
  59. [self addSubview:_rootView];
  60. }
  61. } else {
  62. [_rootView removeFromSuperview];
  63. }
  64. }
  65. @end