hybrid-navigation 5.2.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/README.md +9 -20
  2. package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +5 -13
  3. package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
  4. package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +0 -50
  5. package/android/src/main/java/com/navigation/androidx/TabBarItem.java +0 -4
  6. package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +0 -7
  7. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +7 -43
  8. package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +5 -39
  9. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +62 -135
  10. package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +0 -20
  11. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +10 -21
  12. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +39 -87
  13. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +2 -16
  14. package/dist/NativeEvent.d.ts +1 -11
  15. package/dist/NativeNavigation.d.ts +7 -6
  16. package/dist/Navigation.d.ts +8 -22
  17. package/dist/Navigation.js +33 -175
  18. package/dist/NavigationComponentWrap.d.ts +3 -3
  19. package/dist/Navigator.d.ts +4 -5
  20. package/dist/Navigator.js +13 -37
  21. package/dist/Options.d.ts +2 -2
  22. package/dist/Route.d.ts +10 -35
  23. package/dist/handler/DispatchCommandHandler.d.ts +3 -18
  24. package/dist/handler/DispatchCommandHandler.js +17 -64
  25. package/dist/handler/LayoutCommandHandler.d.ts +2 -3
  26. package/dist/handler/LayoutCommandHandler.js +0 -3
  27. package/dist/handler/LeaveGuardHandler.d.ts +3 -3
  28. package/dist/handler/LeaveGuardHandler.js +57 -32
  29. package/dist/handler/LifecycleEventHandler.d.ts +2 -14
  30. package/dist/handler/LifecycleEventHandler.js +2 -79
  31. package/dist/handler/ResultEventHandler.d.ts +0 -1
  32. package/dist/handler/ResultEventHandler.js +0 -3
  33. package/dist/handler/SceneExitHandler.d.ts +3 -0
  34. package/dist/handler/SceneExitHandler.js +76 -0
  35. package/dist/hooks.d.ts +0 -3
  36. package/dist/hooks.js +21 -46
  37. package/dist/index.d.ts +1 -12
  38. package/dist/index.js +0 -3
  39. package/dist/router/index.d.ts +0 -1
  40. package/dist/router/index.js +43 -11
  41. package/dist/router/stack.d.ts +1 -1
  42. package/dist/sceneCoverage.d.ts +2 -0
  43. package/dist/sceneCoverage.js +25 -0
  44. package/dist/store.d.ts +0 -2
  45. package/dist/store.js +0 -4
  46. package/ios/HBDGlobalStyle.h +0 -1
  47. package/ios/HBDGlobalStyle.mm +0 -7
  48. package/ios/HybridNavigation.h +0 -1
  49. package/ios/Navigator/HBDScreenNavigator.mm +89 -237
  50. package/ios/Navigator/HBDStackNavigator.mm +1 -1
  51. package/ios/Navigator/HBDTabNavigator.mm +15 -30
  52. package/ios/React/HBDNativeNavigation.mm +4 -31
  53. package/ios/React/HBDReactBridgeManager.h +0 -5
  54. package/ios/React/HBDReactBridgeManager.mm +0 -17
  55. package/ios/ViewController/HBDReactViewController.h +1 -1
  56. package/ios/ViewController/HBDReactViewController.mm +34 -9
  57. package/ios/ViewController/HBDTabBarController.h +2 -11
  58. package/ios/ViewController/HBDTabBarController.mm +14 -170
  59. package/ios/ViewController/HBDViewController.h +0 -1
  60. package/ios/ViewController/HBDViewController.mm +9 -57
  61. package/ios/ViewController/UIViewController+HBD.h +2 -0
  62. package/ios/ViewController/UIViewController+HBD.mm +5 -0
  63. package/package.json +1 -3
  64. package/src/NativeEvent.ts +1 -12
  65. package/src/NativeNavigation.ts +8 -7
  66. package/src/Navigation.ts +50 -281
  67. package/src/NavigationComponentWrap.tsx +3 -11
  68. package/src/Navigator.ts +15 -46
  69. package/src/Options.ts +2 -3
  70. package/src/Route.ts +13 -56
  71. package/src/handler/DispatchCommandHandler.ts +29 -111
  72. package/src/handler/LayoutCommandHandler.ts +2 -6
  73. package/src/handler/LeaveGuardHandler.ts +65 -32
  74. package/src/handler/LifecycleEventHandler.ts +22 -122
  75. package/src/handler/ResultEventHandler.ts +0 -6
  76. package/src/handler/SceneExitHandler.ts +86 -0
  77. package/src/hooks.ts +18 -53
  78. package/src/index.ts +0 -14
  79. package/src/router/index.ts +45 -12
  80. package/src/router/stack.ts +1 -1
  81. package/src/sceneCoverage.ts +30 -0
  82. package/src/store.ts +0 -5
  83. package/dist/defineRoutes.d.ts +0 -19
  84. package/dist/defineRoutes.js +0 -6
  85. package/dist/handler/PresentationCoordinator.d.ts +0 -39
  86. package/dist/handler/PresentationCoordinator.js +0 -673
  87. package/dist/handler/TabEventHandler.d.ts +0 -8
  88. package/dist/handler/TabEventHandler.js +0 -25
  89. package/dist/reveal.d.ts +0 -46
  90. package/dist/reveal.js +0 -431
  91. package/dist/routeGraph.d.ts +0 -12
  92. package/dist/routeGraph.js +0 -68
  93. package/dist/testing.d.ts +0 -82
  94. package/dist/testing.js +0 -253
  95. package/ios/ViewController/HBDTabBarRenderer.h +0 -28
  96. package/src/defineRoutes.ts +0 -46
  97. package/src/handler/PresentationCoordinator.ts +0 -806
  98. package/src/handler/TabEventHandler.ts +0 -31
  99. package/src/reveal.ts +0 -565
  100. package/src/routeGraph.ts +0 -87
  101. package/src/testing.ts +0 -402
  102. package/testing.d.ts +0 -1
  103. package/testing.js +0 -3
package/README.md CHANGED
@@ -1,25 +1,14 @@
1
1
  # hybrid-navigation
2
2
 
3
- React Native Navigation that supports seamless navigation between Native and React.
3
+ 使用原生 Stack、Tabs 和 Drawer 管理 React Native 页面。RN 页面和原生页面可以互相跳转、传参并返回结果,支持 URL 深链。
4
4
 
5
- ## 特性
5
+ ```sh
6
+ yarn add hybrid-navigation
7
+ ```
6
8
 
7
- - 使用原生导航组件实现 React Native 页面间的导航,不仅具有更优的性能,而且使得 RN 页面具有原生质感
8
- - 原生页面和 RN 页面共享路由, 使得它们之间相互跳转和传值轻而易举
9
- - 内置 drawer, tabs, stack 标准容器,同时支持自定义容器和导航
10
- - 支持 Deep Link
9
+ 安装后需要配置原生宿主并注册页面。根据应用中负责根布局的一侧选择接入指南。
11
10
 
12
- ## Support
13
-
14
- | 版本 | RN 版本 | RN 架构 |
15
- | ---- | ------- | ------- |
16
- | 2.x | < 0.82 | 旧架构 |
17
- | 3.x | >= 0.83 | 新架构 |
18
- | 4.x | >= 0.83 | 新架构 |
19
- | 5.x | >= 0.83 | 新架构 |
20
-
21
- ## 文档与迁移
22
-
23
- - [文档首页](https://github.com/listenzz/hybrid-navigation/blob/master/docs/README.md)
24
- - [v4 -> v5 迁移指南](https://github.com/listenzz/hybrid-navigation/blob/master/docs/migration-v5.md)
25
- - [CHANGELOG](https://github.com/listenzz/hybrid-navigation/blob/master/CHANGELOG.md)
11
+ - [集成到以 RN 为主的项目](https://github.com/listenzz/hybrid-navigation/blob/master/docs/integration-react.md)
12
+ - [为原生项目添加 RN 模块](https://github.com/listenzz/hybrid-navigation/blob/master/docs/integration-native.md)
13
+ - [使用指南与版本兼容](https://github.com/listenzz/hybrid-navigation/blob/master/docs/README.md)
14
+ - [v4 → v5 迁移指南](https://github.com/listenzz/hybrid-navigation/blob/master/docs/migration-v5.md) · [更新记录](https://github.com/listenzz/hybrid-navigation/blob/master/CHANGELOG.md)
@@ -87,12 +87,6 @@ public abstract class AwesomeFragment extends Fragment {
87
87
  }
88
88
 
89
89
  mPresentationDelegate.onCreate(savedInstanceState);
90
-
91
- if (mHasPendingResult) {
92
- setResultInternal(mResultCode, mResult);
93
- } else {
94
- setResultInternal(0, null);
95
- }
96
90
  }
97
91
 
98
92
  @Override
@@ -472,16 +466,14 @@ public abstract class AwesomeFragment extends Fragment {
472
466
  private int mRequestCode;
473
467
  private int mResultCode;
474
468
  private Bundle mResult;
475
- private boolean mHasPendingResult;
476
469
 
477
- public void setResult(int resultCode, Bundle data) {
478
- mHasPendingResult = true;
479
- setResultInternal(resultCode, data);
480
- }
481
-
482
- private void setResultInternal(int resultCode, Bundle data) {
470
+ public void setLocalResult(int resultCode, Bundle data) {
483
471
  mResult = data;
484
472
  mResultCode = resultCode;
473
+ }
474
+
475
+ public void setResult(int resultCode, Bundle data) {
476
+ setLocalResult(resultCode, data);
485
477
  if (definesPresentationContext()) {
486
478
  return;
487
479
  }
@@ -60,7 +60,7 @@ public class DefaultTabBarProvider implements TabBarProvider {
60
60
  mTabBar.setTabSelectedListener(new TabBar.OnTabSelectedListener() {
61
61
  @Override
62
62
  public void onTabSelected(int position) {
63
- mTabBarFragment.selectTabByUser(position);
63
+ mTabBarFragment.setSelectedIndex(position);
64
64
  }
65
65
 
66
66
  @Override
@@ -70,7 +70,7 @@ public class DefaultTabBarProvider implements TabBarProvider {
70
70
 
71
71
  @Override
72
72
  public void onTabReselected(int position) {
73
- mTabBarFragment.selectTabByUser(position);
73
+ mTabBarFragment.setSelectedIndex(position);
74
74
  }
75
75
  });
76
76
 
@@ -194,44 +194,6 @@ public class TabBarFragment extends AwesomeFragment {
194
194
  return mFragments;
195
195
  }
196
196
 
197
- @Nullable
198
- private TabBarItem resolveTabBarItem(@NonNull AwesomeFragment fragment) {
199
- TabBarItem item = fragment.getTabBarItem();
200
- if (item != null) {
201
- return item;
202
- }
203
- if (fragment instanceof StackFragment stack) {
204
- AwesomeFragment root = stack.getRootFragment();
205
- return root != null ? root.getTabBarItem() : null;
206
- }
207
- return null;
208
- }
209
-
210
- @NonNull
211
- public ArrayList<String> getTabKeys() {
212
- ArrayList<String> keys = new ArrayList<>();
213
- if (mFragments != null) {
214
- for (AwesomeFragment fragment : mFragments) {
215
- TabBarItem item = resolveTabBarItem(fragment);
216
- keys.add(item != null && item.key != null ? item.key : "");
217
- }
218
- }
219
- return keys;
220
- }
221
-
222
- public int findIndexByTabKey(@Nullable String key) {
223
- if (key == null || key.isEmpty() || mFragments == null) {
224
- return -1;
225
- }
226
- for (int i = 0; i < mFragments.size(); i++) {
227
- TabBarItem item = resolveTabBarItem(mFragments.get(i));
228
- if (item != null && key.equals(item.key)) {
229
- return i;
230
- }
231
- }
232
- return -1;
233
- }
234
-
235
197
  public void setSelectedFragment(AwesomeFragment fragment) {
236
198
  setSelectedFragment(fragment, () -> {
237
199
  });
@@ -278,18 +240,6 @@ public class TabBarFragment extends AwesomeFragment {
278
240
  });
279
241
  }
280
242
 
281
- public void selectTabByUser(int index) {
282
- if (index < 0 || index >= mFragments.size()) {
283
- return;
284
- }
285
- final int previousIndex = mSelectedIndex;
286
- final boolean reselected = previousIndex == index;
287
- setSelectedIndex(index, () -> onTabPressed(index, previousIndex, reselected));
288
- }
289
-
290
- protected void onTabPressed(int index, int previousIndex, boolean reselected) {
291
- }
292
-
293
243
  public void setSelectedIndex(int index, @NonNull Runnable completion) {
294
244
  if (index < 0 || index >= mFragments.size()) {
295
245
  completion.run();
@@ -11,8 +11,6 @@ public class TabBarItem implements Parcelable {
11
11
 
12
12
  @NonNull
13
13
  public String title;
14
- @Nullable
15
- public String key;
16
14
  public String iconUri;
17
15
  public String unselectedIconUri;
18
16
  @DrawableRes
@@ -70,7 +68,6 @@ public class TabBarItem implements Parcelable {
70
68
  @Override
71
69
  public void writeToParcel(Parcel dest, int flags) {
72
70
  dest.writeString(this.title);
73
- dest.writeString(this.key);
74
71
  dest.writeString(this.iconUri);
75
72
  dest.writeString(this.unselectedIconUri);
76
73
  dest.writeInt(this.iconRes);
@@ -83,7 +80,6 @@ public class TabBarItem implements Parcelable {
83
80
  String title = in.readString();
84
81
  assert title != null;
85
82
  this.title = title;
86
- this.key = in.readString();
87
83
  this.iconUri = in.readString();
88
84
  this.unselectedIconUri = in.readString();
89
85
  this.iconRes = in.readInt();
@@ -24,7 +24,6 @@ public class GlobalStyle {
24
24
 
25
25
  private final Bundle options;
26
26
  private final ScreenOrientation defaultOrientation;
27
- private final int modalBackgroundColor;
28
27
  private final boolean injectInitialMetrics;
29
28
  private final boolean prewarmPush;
30
29
 
@@ -32,16 +31,10 @@ public class GlobalStyle {
32
31
  this.options = options != null ? new Bundle(options) : new Bundle();
33
32
  ScreenOrientation parsed = ScreenOrientation.fromString(this.options.getString("orientation"));
34
33
  this.defaultOrientation = parsed != null ? parsed : ScreenOrientation.PORTRAIT;
35
- String modalColor = this.options.getString("modalBackgroundColor");
36
- this.modalBackgroundColor = modalColor != null ? Color.parseColor(modalColor) : Color.TRANSPARENT;
37
34
  this.injectInitialMetrics = this.options.getBoolean("injectInitialMetrics", false);
38
35
  this.prewarmPush = this.options.getBoolean("prewarmPush", false);
39
36
  }
40
37
 
41
- public int getModalBackgroundColor() {
42
- return modalBackgroundColor;
43
- }
44
-
45
38
  public boolean isInjectInitialMetrics() {
46
39
  return injectInitialMetrics;
47
40
  }
@@ -62,34 +62,9 @@ public class HybridFragment extends AwesomeFragment {
62
62
  payload.putString("sceneId", getSceneId());
63
63
  payload.putString("state", newState.getWireValue());
64
64
  payload.putString("prevState", prevState.getWireValue());
65
- String moduleName = getModuleName();
66
- if (moduleName != null && !moduleName.isEmpty()) {
67
- payload.putString("moduleName", moduleName);
68
- }
69
- String mode = Navigator.Util.getMode(this);
70
- if (mode != null && !mode.isEmpty()) {
71
- payload.putString("mode", mode);
72
- }
73
- String presentingId = resolvePresentingSceneIdChain();
74
- if (presentingId != null && !presentingId.isEmpty()) {
75
- payload.putString("presentingId", presentingId);
76
- }
77
65
  nativeEvent.emitOnSceneStateChange(payload);
78
66
  }
79
67
 
80
- @Nullable
81
- private String resolvePresentingSceneIdChain() {
82
- AwesomeFragment current = this;
83
- while (current != null) {
84
- String presentingId = FragmentHelper.getArguments(current).getString(PresentableActivity.ARG_PRESENTING_SCENE_ID);
85
- if (presentingId != null && !presentingId.isEmpty()) {
86
- return presentingId;
87
- }
88
- current = current.getParentAwesomeFragment();
89
- }
90
- return null;
91
- }
92
-
93
68
  protected boolean isNativeManagedLifecycle() {
94
69
  return true;
95
70
  }
@@ -206,8 +181,8 @@ public class HybridFragment extends AwesomeFragment {
206
181
 
207
182
  private void setBackgroundForModal(@NonNull Style style) {
208
183
  int color = mStyle.getScreenBackgroundColor();
209
- if (getOptions().getString("screenBackgroundColor") == null || Color.alpha(color) == 255) {
210
- style.setScreenBackgroundColor(reactManager.getGlobalStyle().getModalBackgroundColor());
184
+ if (Color.alpha(color) == 255) {
185
+ style.setScreenBackgroundColor(Color.TRANSPARENT);
211
186
  }
212
187
  }
213
188
 
@@ -341,43 +316,32 @@ public class HybridFragment extends AwesomeFragment {
341
316
  if (title == null) {
342
317
  return;
343
318
  }
344
- String key = tabItem.getString("key");
345
319
 
346
320
  Bundle icon = tabItem.getBundle("icon");
347
321
  if (icon == null) {
348
- TabBarItem item = new TabBarItem(title);
349
- item.key = key;
350
- setTabBarItem(item);
322
+ setTabBarItem(new TabBarItem(title));
351
323
  return;
352
324
  }
353
325
 
354
326
  String uri = icon.getString("uri");
355
327
  if (uri == null) {
356
- TabBarItem item = new TabBarItem(title);
357
- item.key = key;
358
- setTabBarItem(item);
328
+ setTabBarItem(new TabBarItem(title));
359
329
  return;
360
330
  }
361
331
 
362
332
  Bundle unselectedIcon = tabItem.getBundle("unselectedIcon");
363
333
  if (unselectedIcon == null) {
364
- TabBarItem item = new TabBarItem(title, uri);
365
- item.key = key;
366
- setTabBarItem(item);
334
+ setTabBarItem(new TabBarItem(title, uri));
367
335
  return;
368
336
  }
369
337
 
370
338
  String unselectedUri = unselectedIcon.getString("uri");
371
339
  if (unselectedUri == null) {
372
- TabBarItem item = new TabBarItem(title, uri);
373
- item.key = key;
374
- setTabBarItem(item);
340
+ setTabBarItem(new TabBarItem(title, uri));
375
341
  return;
376
342
  }
377
343
 
378
- TabBarItem item = new TabBarItem(title, uri, unselectedUri);
379
- item.key = key;
380
- setTabBarItem(item);
344
+ setTabBarItem(new TabBarItem(title, uri, unselectedUri));
381
345
  }
382
346
 
383
347
  private Bundle props;
@@ -31,7 +31,6 @@ import com.navigation.androidx.TabBarFragment;
31
31
 
32
32
  import java.util.ArrayList;
33
33
  import java.util.HashMap;
34
- import java.util.List;
35
34
  import java.util.Map;
36
35
  import java.util.concurrent.atomic.AtomicBoolean;
37
36
  import java.util.function.Function;
@@ -296,7 +295,7 @@ public class NativeNavigation extends NativeNavigationSpec {
296
295
  if (key == null) {
297
296
  continue;
298
297
  }
299
- int resolvedIndex = tabBarFragment.findIndexByTabKey(key);
298
+ int resolvedIndex = ReactTabBarFragment.tabKeys(tabBarFragment).indexOf(key);
300
299
  if (resolvedIndex < 0) {
301
300
  continue;
302
301
  }
@@ -427,43 +426,10 @@ public class NativeNavigation extends NativeNavigationSpec {
427
426
  }
428
427
 
429
428
  @Nullable
430
- private TabBarFragment getTabBarFragment(@Nullable String sceneId) {
431
- if (sceneId != null && !sceneId.isEmpty()) {
432
- AwesomeFragment fragment = findFragmentBySceneId(sceneId);
433
- if (fragment != null) {
434
- return fragment.getTabBarFragment();
435
- }
436
- return null;
437
- }
438
- ReactAppCompatActivity activity = reactManager.getActiveActivity();
439
- if (activity == null) {
440
- return null;
441
- }
442
- return findTabBarFragment(activity.getSupportFragmentManager());
443
- }
444
-
445
- @Nullable
446
- private TabBarFragment findTabBarFragment(@NonNull FragmentManager fragmentManager) {
447
- List<AwesomeFragment> fragments = FragmentHelper.getFragments(fragmentManager);
448
- for (AwesomeFragment fragment : fragments) {
449
- TabBarFragment found = findTabBarFragmentInTree(fragment);
450
- if (found != null) {
451
- return found;
452
- }
453
- }
454
- return null;
455
- }
456
-
457
- @Nullable
458
- private TabBarFragment findTabBarFragmentInTree(@NonNull AwesomeFragment fragment) {
459
- if (fragment instanceof TabBarFragment tabBarFragment) {
460
- return tabBarFragment;
461
- }
462
- for (AwesomeFragment child : fragment.getChildAwesomeFragments()) {
463
- TabBarFragment found = findTabBarFragmentInTree(child);
464
- if (found != null) {
465
- return found;
466
- }
429
+ private TabBarFragment getTabBarFragment(String sceneId) {
430
+ AwesomeFragment fragment = findFragmentBySceneId(sceneId);
431
+ if (fragment != null) {
432
+ return fragment.getTabBarFragment();
467
433
  }
468
434
  return null;
469
435
  }
@@ -1,9 +1,7 @@
1
1
  package com.reactnative.hybridnavigation;
2
2
 
3
3
  import android.content.Context;
4
- import android.graphics.Insets;
5
4
  import android.graphics.Rect;
6
- import android.os.Build;
7
5
  import android.os.Bundle;
8
6
  import android.os.Handler;
9
7
  import android.os.Looper;
@@ -13,10 +11,12 @@ import android.view.LayoutInflater;
13
11
  import android.view.View;
14
12
  import android.view.ViewGroup;
15
13
  import android.view.Window;
16
- import android.view.WindowInsets;
17
14
 
18
15
  import androidx.annotation.NonNull;
19
16
  import androidx.annotation.Nullable;
17
+ import androidx.core.graphics.Insets;
18
+ import androidx.core.view.ViewCompat;
19
+ import androidx.core.view.WindowInsetsCompat;
20
20
  import androidx.fragment.app.FragmentActivity;
21
21
 
22
22
  import com.facebook.common.logging.FLog;
@@ -80,9 +80,11 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
80
80
  if (width <= 0 || height <= 0) {
81
81
  return false;
82
82
  }
83
- View contentView = activity.getWindow() != null
84
- ? activity.getWindow().getDecorView().findViewById(android.R.id.content)
85
- : null;
83
+ Window window = activity.getWindow();
84
+ if (window == null) {
85
+ return false;
86
+ }
87
+ View contentView = window.getDecorView().findViewById(android.R.id.content);
86
88
  if (contentView == null || contentView.getWidth() != width || contentView.getHeight() != height) {
87
89
  return false;
88
90
  }
@@ -92,14 +94,12 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
92
94
  if (stackFragment.getPresentingFragment() != null) {
93
95
  return false;
94
96
  }
95
- if (!canUseCurrentWindowGeometry(activity, stackFragment)) {
97
+ if (!canUseCurrentWindowGeometry(window, width, height)) {
96
98
  return false;
97
99
  }
98
100
 
99
- Bundle initialProps = buildInitialProps(activity, stackFragment);
100
-
101
101
  ReactHost reactHost = getReactManager().getReactHost();
102
- ReactSurface surface = reactHost.createSurface(activity, getModuleName(), initialProps);
102
+ ReactSurface surface = reactHost.createSurface(activity, getModuleName(), buildInitialProps(activity, stackFragment, container));
103
103
  View surfaceView = surface.getView();
104
104
  if (surfaceView == null) {
105
105
  return false;
@@ -190,14 +190,14 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
190
190
  View view = inflater.inflate(R.layout.nav_fragment_react, container, false);
191
191
  reactRootView = view.findViewById(R.id.react_content);
192
192
  if (isReactModuleRegisterCompleted()) {
193
- mountReactView();
193
+ mountReactView(container);
194
194
  } else {
195
- scheduleMountWhenRegistered();
195
+ scheduleMountWhenRegistered(container);
196
196
  }
197
197
  return view;
198
198
  }
199
199
 
200
- private void scheduleMountWhenRegistered() {
200
+ private void scheduleMountWhenRegistered(@Nullable ViewGroup container) {
201
201
  clearPendingRegisterListener();
202
202
  pendingRegisterListener = new ReactManager.ReactModuleRegisterListener() {
203
203
  @Override
@@ -206,7 +206,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
206
206
  if (reactRootView == null || reactSurface != null || !isAdded()) {
207
207
  return;
208
208
  }
209
- mountReactView();
209
+ mountReactView(container);
210
210
  if (isResumed() && !isResumingForRedirect()) {
211
211
  getLifecycleStateMachine().onHostResume(canEmitReactVisibility());
212
212
  }
@@ -263,8 +263,8 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
263
263
  unmountReactView();
264
264
  }
265
265
 
266
- private void mountReactView() {
267
- boolean reusedPrewarmed = initReactRootView();
266
+ private void mountReactView(@Nullable ViewGroup container) {
267
+ boolean reusedPrewarmed = initReactRootView(container);
268
268
  getReactManager().addReactBridgeReloadListener(this);
269
269
  if (!reusedPrewarmed || !getLifecycleStateMachine().isMounted()) {
270
270
  getLifecycleStateMachine().onMount();
@@ -321,7 +321,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
321
321
  }
322
322
  }
323
323
 
324
- private boolean initReactRootView() {
324
+ private boolean initReactRootView(@Nullable ViewGroup container) {
325
325
  HBDRootView rootView = reactRootView;
326
326
  if (rootView == null) {
327
327
  throw new IllegalStateException("[Navigation] HBDRootView 还没有创建。");
@@ -334,7 +334,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
334
334
  return true;
335
335
  }
336
336
  ReactHost reactHost = getReactManager().getReactHost();
337
- Bundle initialProps = buildInitialProps(getActivity(), this);
337
+ Bundle initialProps = buildInitialProps(requireActivity(), this, container);
338
338
  ReactSurface reactSurface = reactHost.createSurface(requireContext(), getModuleName(), initialProps);
339
339
  this.reactSurface = reactSurface;
340
340
 
@@ -344,36 +344,53 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
344
344
  }
345
345
 
346
346
  @NonNull
347
- private Bundle buildInitialProps(@Nullable FragmentActivity activity, @Nullable AwesomeFragment hostFragment) {
348
- Bundle initialProps = new Bundle(getProps());
349
- if (!initialProps.containsKey("initialMetrics") && getReactManager().getGlobalStyle().isInjectInitialMetrics()) {
350
- Bundle metrics = buildInitialMetrics(activity, hostFragment);
351
- if (metrics != null) {
352
- initialProps.putBundle("initialMetrics", metrics);
353
- }
347
+ private Bundle buildInitialProps(@NonNull FragmentActivity activity, @NonNull AwesomeFragment host, @Nullable View container) {
348
+ Bundle props = getProps();
349
+ if (!getReactManager().getGlobalStyle().isInjectInitialMetrics() || props.containsKey("initialMetrics")
350
+ || container == null || host.getTabBarFragment() != null || isFragmentInsideDrawerMenu(host)) {
351
+ return props;
354
352
  }
353
+
354
+ Window window = activity.getWindow();
355
+ View decor = window.getDecorView();
356
+ View content = decor.findViewById(android.R.id.content);
357
+ if (content == null || container.getRootView() != decor || container.getWidth() <= 0 || container.getHeight() <= 0
358
+ || container.getWidth() != content.getWidth() || container.getHeight() != content.getHeight()
359
+ || !canUseCurrentWindowGeometry(window, content.getWidth(), content.getHeight())) {
360
+ return props;
361
+ }
362
+
363
+ Rect frame = new Rect();
364
+ Rect contentFrame = new Rect();
365
+ WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(decor);
366
+ if (windowInsets == null || !container.getGlobalVisibleRect(frame) || !content.getGlobalVisibleRect(contentFrame)
367
+ || !frame.equals(contentFrame) || frame.width() != container.getWidth() || frame.height() != container.getHeight()) {
368
+ return props;
369
+ }
370
+
371
+ Insets system = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
372
+ Bundle insets = new Bundle();
373
+ insets.putDouble("top", PixelUtil.toDIPFromPixel(Math.max(0, system.top - frame.top)));
374
+ insets.putDouble("right", PixelUtil.toDIPFromPixel(Math.max(0, system.right - (decor.getWidth() - frame.right))));
375
+ insets.putDouble("bottom", PixelUtil.toDIPFromPixel(Math.max(0, system.bottom - (decor.getHeight() - frame.bottom))));
376
+ insets.putDouble("left", PixelUtil.toDIPFromPixel(Math.max(0, system.left - frame.left)));
377
+
378
+ Bundle bounds = new Bundle();
379
+ bounds.putDouble("x", PixelUtil.toDIPFromPixel(frame.left));
380
+ bounds.putDouble("y", PixelUtil.toDIPFromPixel(frame.top));
381
+ bounds.putDouble("width", PixelUtil.toDIPFromPixel(frame.width()));
382
+ bounds.putDouble("height", PixelUtil.toDIPFromPixel(frame.height()));
383
+
384
+ Bundle metrics = new Bundle();
385
+ metrics.putBundle("insets", insets);
386
+ metrics.putBundle("frame", bounds);
387
+ Bundle initialProps = new Bundle(props);
388
+ initialProps.putBundle("initialMetrics", metrics);
355
389
  return initialProps;
356
390
  }
357
391
 
358
- private boolean canUseCurrentWindowGeometry(@Nullable FragmentActivity activity, @Nullable AwesomeFragment hostFragment) {
359
- if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
360
- return false;
361
- }
362
- Window window = activity.getWindow();
363
- if (window == null) {
364
- return false;
365
- }
366
- View decorView = window.getDecorView();
367
- if (!(decorView instanceof ViewGroup) || !decorView.isAttachedToWindow()) {
368
- return false;
369
- }
370
- View contentView = decorView.findViewById(android.R.id.content);
371
- if (contentView == null || contentView.getWidth() <= 0 || contentView.getHeight() <= 0) {
372
- return false;
373
- }
374
- // Tab 根页与 Drawer 侧栏容器不按全屏窗口几何预热或注入。
375
- AwesomeFragment contextFragment = hostFragment != null ? hostFragment : this;
376
- if (contextFragment.getTabBarFragment() != null || isFragmentInsideDrawerMenu(contextFragment)) {
392
+ private boolean canUseCurrentWindowGeometry(@NonNull Window window, int width, int height) {
393
+ if (!window.getDecorView().isAttachedToWindow()) {
377
394
  return false;
378
395
  }
379
396
  OrientationCoordinator coordinator = getOrientationCoordinator();
@@ -383,7 +400,7 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
383
400
  ScreenOrientation targetOrientation = coordinator != null
384
401
  ? coordinator.resolveOrientation(this)
385
402
  : getReactManager().getGlobalStyle().getDefaultOrientation();
386
- boolean windowIsLandscape = contentView.getWidth() > contentView.getHeight();
403
+ boolean windowIsLandscape = width > height;
387
404
  if (targetOrientation == ScreenOrientation.LANDSCAPE && !windowIsLandscape) {
388
405
  return false;
389
406
  }
@@ -399,96 +416,6 @@ public class ReactFragment extends HybridFragment implements ReactManager.ReactB
399
416
  return true;
400
417
  }
401
418
 
402
- @Nullable
403
- private Bundle buildInitialMetrics(@Nullable FragmentActivity activity, @Nullable AwesomeFragment hostFragment) {
404
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
405
- return null;
406
- }
407
- if (activity == null || !canUseCurrentWindowGeometry(activity, hostFragment)) {
408
- return null;
409
- }
410
- Window window = activity.getWindow();
411
- if (window == null) {
412
- return null;
413
- }
414
- View decorView = window.getDecorView();
415
- ViewGroup decorGroup = (ViewGroup) decorView;
416
- View contentView = decorView.findViewById(android.R.id.content);
417
- if (contentView == null) {
418
- return null;
419
- }
420
- WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
421
- if (rootWindowInsets == null) {
422
- return null;
423
- }
424
-
425
- float rootTop;
426
- float rootRight;
427
- float rootBottom;
428
- float rootLeft;
429
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
430
- Insets systemInsets = rootWindowInsets.getInsets(
431
- WindowInsets.Type.statusBars()
432
- | WindowInsets.Type.displayCutout()
433
- | WindowInsets.Type.navigationBars()
434
- | WindowInsets.Type.captionBar()
435
- );
436
- rootTop = systemInsets.top;
437
- rootRight = systemInsets.right;
438
- rootBottom = systemInsets.bottom;
439
- rootLeft = systemInsets.left;
440
- } else {
441
- @SuppressWarnings("deprecation")
442
- int insetTop = rootWindowInsets.getSystemWindowInsetTop();
443
- @SuppressWarnings("deprecation")
444
- int insetRight = rootWindowInsets.getSystemWindowInsetRight();
445
- @SuppressWarnings("deprecation")
446
- int insetBottom = Math.min(rootWindowInsets.getSystemWindowInsetBottom(), rootWindowInsets.getStableInsetBottom());
447
- @SuppressWarnings("deprecation")
448
- int insetLeft = rootWindowInsets.getSystemWindowInsetLeft();
449
- rootTop = insetTop;
450
- rootRight = insetRight;
451
- rootBottom = insetBottom;
452
- rootLeft = insetLeft;
453
- }
454
-
455
- float windowWidth = decorView.getWidth();
456
- float windowHeight = decorView.getHeight();
457
- Rect visibleRect = new Rect();
458
- if (!contentView.getGlobalVisibleRect(visibleRect)) {
459
- return null;
460
- }
461
- float top = Math.max(rootTop - visibleRect.top, 0f);
462
- float right = Math.max(Math.min(visibleRect.left + contentView.getWidth() - windowWidth, 0f) + rootRight, 0f);
463
- float bottom = Math.max(Math.min(visibleRect.top + contentView.getHeight() - windowHeight, 0f) + rootBottom, 0f);
464
- float left = Math.max(rootLeft - visibleRect.left, 0f);
465
-
466
- Rect frameRect = new Rect();
467
- contentView.getDrawingRect(frameRect);
468
- try {
469
- decorGroup.offsetDescendantRectToMyCoords(contentView, frameRect);
470
- } catch (IllegalArgumentException ignored) {
471
- return null;
472
- }
473
-
474
- Bundle insetsBundle = new Bundle();
475
- insetsBundle.putDouble("top", PixelUtil.toDIPFromPixel(top));
476
- insetsBundle.putDouble("right", PixelUtil.toDIPFromPixel(right));
477
- insetsBundle.putDouble("bottom", PixelUtil.toDIPFromPixel(bottom));
478
- insetsBundle.putDouble("left", PixelUtil.toDIPFromPixel(left));
479
-
480
- Bundle frameBundle = new Bundle();
481
- frameBundle.putDouble("x", PixelUtil.toDIPFromPixel(frameRect.left));
482
- frameBundle.putDouble("y", PixelUtil.toDIPFromPixel(frameRect.top));
483
- frameBundle.putDouble("width", PixelUtil.toDIPFromPixel(contentView.getWidth()));
484
- frameBundle.putDouble("height", PixelUtil.toDIPFromPixel(contentView.getHeight()));
485
-
486
- Bundle metrics = new Bundle();
487
- metrics.putBundle("insets", insetsBundle);
488
- metrics.putBundle("frame", frameBundle);
489
- return metrics;
490
- }
491
-
492
419
  private boolean resolvePreferredStatusBarHidden() {
493
420
  if (isAdded()) {
494
421
  return preferredStatusBarHidden();