@sdcx/overlay 0.4.0 → 1.0.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 http://www.sdcx.tech
3
+ Copyright (c) 2025 listenzz@163.com
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  `Overlay` 是一个 React Native 原生 UI 基础设施,它漂浮在你的 React Native 应用之上,可用于实现 Modal, Alert, Toast, Popover, Notification, Hoverball 等顶层 UI。
4
4
 
5
+ > 这个库属于实验性质,请谨慎使用
6
+
7
+ ## 版本兼容
8
+
9
+ | 版本 | RN 版本 | RN 架构 |
10
+ | ---- | ------- | ------- |
11
+ | 0.x | < 0.82 | 旧架构 |
12
+ | 1.x | >= 0.82 | 新架构 |
13
+
5
14
  ## Installation
6
15
 
7
16
  ```bash
@@ -24,7 +33,7 @@ module.exports = {
24
33
  },
25
34
  },
26
35
  },
27
- }
36
+ };
28
37
  ```
29
38
 
30
39
  ## Usage
package/RNOverlay.podspec CHANGED
@@ -6,13 +6,13 @@ Pod::Spec.new do |s|
6
6
  s.name = "RNOverlay"
7
7
  s.version = package["version"]
8
8
  s.summary = package["description"]
9
-
9
+
10
10
  s.homepage = package["homepage"]
11
11
  s.license = package["license"]
12
12
  s.authors = package["author"]
13
- s.platforms = { :ios => "10.0" }
13
+ s.platforms = { :ios => min_ios_version_supported }
14
14
  s.source = { :git => "https://github.com/github-account/overlay.git", :tag => "#{s.version}" }
15
15
 
16
16
  s.source_files = "ios/Overlay/**/*.{h,m,mm}"
17
- s.dependency "React-Core"
18
- end
17
+ install_modules_dependencies(s)
18
+ end
@@ -1,35 +1,57 @@
1
- // android/build.gradle
1
+ buildscript {
2
+ repositories {
3
+ mavenCentral()
4
+ google()
5
+ }
2
6
 
3
- def safeExtGet(prop, fallback) {
4
- rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
7
+ dependencies {
8
+ classpath "com.android.tools.build:gradle:8.7.2"
9
+ }
5
10
  }
6
11
 
7
12
  apply plugin: 'com.android.library'
13
+ apply plugin: 'com.facebook.react'
14
+
15
+ def safeExtGet(prop, fallback) {
16
+ rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
17
+ }
8
18
 
9
19
  android {
10
- compileOptions {
11
- sourceCompatibility JavaVersion.VERSION_1_8
12
- targetCompatibility JavaVersion.VERSION_1_8
13
- }
14
-
15
- compileSdkVersion safeExtGet('compileSdkVersion', 30)
16
- buildToolsVersion safeExtGet('buildToolsVersion', '30.0.2')
17
-
18
- defaultConfig {
19
- minSdkVersion safeExtGet('minSdkVersion', 21)
20
- targetSdkVersion safeExtGet('targetSdkVersion', 30)
21
- versionCode 1
22
- versionName "1.0.0"
23
- }
24
-
25
- buildTypes {
26
- release {
27
- consumerProguardFiles 'proguard-rules.pro'
28
- }
29
- }
20
+
21
+ def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
22
+ // Check AGP version for backward compatibility w/react-native versions still on gradle plugin 6
23
+ def major = agpVersion[0].toInteger()
24
+ def minor = agpVersion[1].toInteger()
25
+ if ((major == 7 && minor >= 3) || major >= 8) {
26
+ namespace "com.reactnative.overlay"
27
+ buildFeatures {
28
+ buildConfig true
29
+ }
30
+ }
31
+
32
+ compileSdkVersion safeExtGet('compileSdkVersion', 35)
33
+ buildToolsVersion safeExtGet('buildToolsVersion', '35.0.0')
34
+
35
+ defaultConfig {
36
+ minSdkVersion safeExtGet('minSdkVersion', 24)
37
+ targetSdkVersion safeExtGet('targetSdkVersion', 35)
38
+ }
39
+
40
+ sourceSets {
41
+ main {
42
+ java.srcDirs += [
43
+ "generated/java",
44
+ "generated/jni"
45
+ ]
46
+ }
47
+ }
48
+ }
49
+
50
+ repositories {
51
+ google()
52
+ mavenCentral()
30
53
  }
31
54
 
32
55
  dependencies {
33
- implementation fileTree(dir: 'libs', include: ['*.jar'])
34
- implementation 'com.facebook.react:react-native:+'
56
+ api 'com.facebook.react:react-native:+'
35
57
  }
@@ -3,98 +3,84 @@ package com.reactnative.overlay;
3
3
  import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
4
4
 
5
5
  import android.app.Activity;
6
- import android.os.Bundle;
7
- import android.view.Gravity;
8
6
  import android.view.ViewGroup;
9
7
  import android.view.Window;
10
- import android.widget.FrameLayout;
11
8
 
12
9
  import androidx.annotation.NonNull;
13
10
  import androidx.annotation.UiThread;
14
11
 
15
- import com.facebook.react.ReactInstanceManager;
12
+ import com.facebook.react.ReactHost;
16
13
  import com.facebook.react.bridge.Arguments;
17
- import com.facebook.react.bridge.ReactContext;
18
14
  import com.facebook.react.bridge.ReadableMap;
15
+ import com.facebook.react.interfaces.fabric.ReactSurface;
19
16
 
20
17
  @UiThread
21
18
  public class Overlay {
22
19
 
23
- final Activity activity;
24
- final String moduleName;
25
-
26
- final ReactInstanceManager reactInstanceManager;
27
-
28
- OverlayRootView rootView;
29
- ViewGroup decorView;
30
-
31
- public Overlay(@NonNull Activity activity, String moduleName, ReactInstanceManager reactInstanceManager) {
32
- this.activity = activity;
33
- this.moduleName = moduleName;
34
- this.reactInstanceManager = reactInstanceManager;
35
- }
36
-
37
- public void show(ReadableMap props, ReadableMap options) {
38
- OverlayRootView reactRootView = createReactRootView();
39
-
40
- if (options.hasKey("passThroughTouches")) {
41
- reactRootView.setShouldConsumeTouchEvent(!options.getBoolean("passThroughTouches"));
42
- }
43
-
44
- this.rootView = reactRootView;
45
- startReactApplication(reactRootView, Arguments.toBundle(props));
46
- decorView = getDecorView();
47
- if (decorView != null) {
48
- decorView.addView(reactRootView);
49
- }
50
- }
51
-
52
- public void hide() {
53
- if (decorView != null) {
54
- decorView.removeView(rootView);
55
- decorView = null;
56
- }
57
-
58
- unmountReactView();
59
- }
60
-
61
- public void update() {
62
- ViewGroup decorView = getDecorView();
63
- if (decorView != null && decorView != this.decorView) {
64
- this.decorView.removeView(rootView);
65
- this.decorView = decorView;
66
- decorView.addView(rootView);
67
- }
68
- }
69
-
70
- private void unmountReactView() {
71
- ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
72
- if (reactContext == null || !reactContext.hasActiveCatalystInstance()) {
73
- return;
74
- }
75
-
76
- if (rootView != null) {
77
- rootView.unmountReactApplication();
78
- rootView = null;
79
- }
80
- }
81
-
82
- private void startReactApplication(OverlayRootView reactRootView, Bundle props) {
83
- reactRootView.startReactApplication(reactInstanceManager, moduleName, props);
84
- }
85
-
86
- private OverlayRootView createReactRootView() {
87
- OverlayRootView reactRootView = new OverlayRootView(activity);
88
- reactRootView.setLayoutParams(new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.CENTER));
89
- return reactRootView;
90
- }
91
-
92
- private ViewGroup getDecorView() {
93
- Window window = activity.getWindow();
94
- if (window == null) {
95
- return null;
20
+ final Activity activity;
21
+ final String moduleName;
22
+ final ReactHost reactHost;
23
+
24
+ ViewGroup rootView;
25
+ ViewGroup decorView;
26
+ ReactSurface reactSurface;
27
+
28
+ public Overlay(@NonNull Activity activity, String moduleName, ReactHost reactHost) {
29
+ this.activity = activity;
30
+ this.moduleName = moduleName;
31
+ this.reactHost = reactHost;
32
+ }
33
+
34
+ public void show(ReadableMap props, ReadableMap options) {
35
+
36
+ decorView = getDecorView();
37
+ if (decorView == null) {
38
+ return;
39
+ }
40
+
41
+ ReactSurface reactSurface = reactHost.createSurface(activity, moduleName, Arguments.toBundle(props));
42
+ this.reactSurface = reactSurface;
43
+ ViewGroup view = reactSurface.getView();
44
+ ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);
45
+ OverlayRootView root = new OverlayRootView(activity);
46
+ root.addView(view, layoutParams);
47
+ this.rootView = root;
48
+ decorView.addView(root, layoutParams);
49
+ if (options.hasKey("passThroughTouches")) {
50
+ root.setPassThroughTouches(options.getBoolean("passThroughTouches"));
96
51
  }
97
- return (ViewGroup) window.getDecorView();
98
- }
52
+ reactSurface.start();
53
+ }
54
+
55
+ public void hide() {
56
+ if (decorView != null) {
57
+ decorView.removeView(rootView);
58
+ decorView = null;
59
+ }
60
+ unmountReactView();
61
+ }
62
+
63
+ public void update() {
64
+ ViewGroup decorView = getDecorView();
65
+ if (decorView != null && decorView != this.decorView) {
66
+ this.decorView.removeView(rootView);
67
+ this.decorView = decorView;
68
+ decorView.addView(rootView);
69
+ }
70
+ }
71
+
72
+ private void unmountReactView() {
73
+ if (reactSurface != null) {
74
+ reactSurface.stop();
75
+ }
76
+ }
77
+
78
+ private ViewGroup getDecorView() {
79
+ Window window = activity.getWindow();
80
+ if (window == null) {
81
+ return null;
82
+ }
83
+ return (ViewGroup) window.getDecorView();
84
+ }
99
85
 
100
86
  }
@@ -3,109 +3,130 @@ package com.reactnative.overlay;
3
3
  import android.app.Activity;
4
4
 
5
5
  import androidx.annotation.NonNull;
6
+ import androidx.core.graphics.Insets;
7
+ import androidx.core.view.ViewCompat;
8
+ import androidx.core.view.WindowInsetsCompat;
6
9
 
7
10
  import com.facebook.common.logging.FLog;
11
+ import com.facebook.react.ReactHost;
8
12
  import com.facebook.react.ReactNativeHost;
9
- import com.facebook.react.bridge.Arguments;
13
+ import com.facebook.react.bridge.JavaOnlyMap;
10
14
  import com.facebook.react.bridge.LifecycleEventListener;
11
15
  import com.facebook.react.bridge.ReactApplicationContext;
12
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
13
- import com.facebook.react.bridge.ReactMethod;
14
16
  import com.facebook.react.bridge.ReadableMap;
15
17
  import com.facebook.react.bridge.UiThreadUtil;
16
18
  import com.facebook.react.bridge.WritableMap;
19
+ import com.facebook.react.defaults.DefaultReactHost;
20
+ import com.facebook.react.uimanager.PixelUtil;
17
21
 
18
22
  import java.util.HashMap;
19
- public class OverlayModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
20
-
21
- private final HashMap<String, Overlay> overlays = new HashMap<>();
22
- private final ReactApplicationContext reactContext;
23
-
24
- private final ReactNativeHost reactNativeHost;
25
-
26
- public OverlayModule(ReactApplicationContext reactContext, ReactNativeHost reactNativeHost) {
27
- super(reactContext);
28
- this.reactContext = reactContext;
29
- this.reactNativeHost = reactNativeHost;
30
- reactContext.addLifecycleEventListener(this);
31
- }
32
-
33
- @Override
34
- public void invalidate() {
35
- reactContext.removeLifecycleEventListener(this);
36
- final Activity activity = getCurrentActivity();
37
- if (activity == null || activity.isFinishing()) {
38
- return;
39
- }
40
- UiThreadUtil.runOnUiThread(this::handleDestroy);
41
- }
42
-
43
- private void handleDestroy() {
44
- for (String key : overlays.keySet()) {
45
- Overlay overlay = overlays.get(key);
46
- overlay.hide();
47
- }
48
- overlays.clear();
49
- }
50
-
51
- @NonNull
52
- @Override
53
- public String getName() {
54
- return "OverlayHost";
55
- }
56
-
57
- @ReactMethod
58
- public void show(final String moduleName, final ReadableMap options) {
59
- UiThreadUtil.runOnUiThread(() -> {
60
- final Activity activity = getCurrentActivity();
61
- if (activity == null || activity.isFinishing()) {
62
- return;
63
- }
64
-
65
- Overlay overlay = overlays.get(moduleName);
66
- if (overlay != null) {
67
- overlay.update();
68
- return;
69
- }
70
-
71
- int id = options.getInt("id");
72
- WritableMap props = Arguments.createMap();
73
- props.putInt("id", id);
74
- overlay = new Overlay(activity, moduleName, reactNativeHost.getReactInstanceManager());
75
- overlay.show(props, options);
76
- overlays.put(genOverlayKey(moduleName, id), overlay);
77
- });
78
- }
79
-
80
- @ReactMethod
81
- public void hide(String moduleName, int id) {
82
- UiThreadUtil.runOnUiThread(() -> {
83
- Overlay overlay = overlays.get(genOverlayKey(moduleName, id));
84
- if (overlay == null) {
85
- return;
86
- }
87
- overlays.remove(genOverlayKey(moduleName, id));
88
- overlay.hide();
89
- });
90
- }
91
-
92
- @Override
93
- public void onHostResume() {
94
- //
95
- }
96
-
97
- @Override
98
- public void onHostPause() {
99
- //
100
- }
101
-
102
- @Override
103
- public void onHostDestroy() {
104
- FLog.i("OverlayModule", "onHostDestroy");
105
- handleDestroy();
106
- }
107
-
108
- private String genOverlayKey(String moduleName, int id) {
109
- return moduleName + "-" + id;
110
- }
23
+
24
+ public class OverlayModule extends NativeOverlaySpec implements LifecycleEventListener {
25
+
26
+ private final HashMap<String, Overlay> overlays = new HashMap<>();
27
+ private final ReactApplicationContext reactContext;
28
+ private final ReactHost reactHost;
29
+
30
+ public OverlayModule(ReactApplicationContext reactContext, ReactNativeHost reactNativeHost) {
31
+ super(reactContext);
32
+ this.reactContext = reactContext;
33
+ this.reactHost = DefaultReactHost.getDefaultReactHost(reactContext, reactNativeHost, null);
34
+ reactContext.addLifecycleEventListener(this);
35
+ }
36
+
37
+ @NonNull
38
+ @Override
39
+ public String getName() {
40
+ return "OverlayHost";
41
+ }
42
+
43
+ @Override
44
+ public void invalidate() {
45
+ reactContext.removeLifecycleEventListener(this);
46
+ final Activity activity = getCurrentActivity();
47
+ if (activity == null || activity.isFinishing()) {
48
+ return;
49
+ }
50
+ UiThreadUtil.runOnUiThread(this::handleDestroy);
51
+ }
52
+
53
+ private void handleDestroy() {
54
+ for (String key : overlays.keySet()) {
55
+ Overlay overlay = overlays.get(key);
56
+ overlay.hide();
57
+ }
58
+ overlays.clear();
59
+ }
60
+
61
+ public void show(final String moduleName, final ReadableMap options) {
62
+ UiThreadUtil.runOnUiThread(() -> {
63
+ final Activity activity = getCurrentActivity();
64
+ if (activity == null || activity.isFinishing()) {
65
+ return;
66
+ }
67
+
68
+ Overlay overlay = overlays.get(moduleName);
69
+ if (overlay != null) {
70
+ overlay.update();
71
+ return;
72
+ }
73
+
74
+ int id = (int) options.getDouble("overlayId");
75
+ WritableMap props = JavaOnlyMap.deepClone(options);
76
+ props.putMap("insets", getInsets(activity));
77
+ overlay = new Overlay(activity, moduleName, reactHost);
78
+ overlay.show(props, options);
79
+ overlays.put(genOverlayKey(moduleName, id), overlay);
80
+ });
81
+ }
82
+
83
+ @Override
84
+ public void hide(String moduleName, double overlayId) {
85
+ hide(moduleName, (int) overlayId);
86
+ }
87
+
88
+ public void hide(String moduleName, int id) {
89
+ UiThreadUtil.runOnUiThread(() -> {
90
+ Overlay overlay = overlays.get(genOverlayKey(moduleName, id));
91
+ if (overlay == null) {
92
+ return;
93
+ }
94
+ overlays.remove(genOverlayKey(moduleName, id));
95
+ overlay.hide();
96
+ });
97
+ }
98
+
99
+ @Override
100
+ public void onHostResume() {
101
+ //
102
+ }
103
+
104
+ @Override
105
+ public void onHostPause() {
106
+ //
107
+ }
108
+
109
+ @Override
110
+ public void onHostDestroy() {
111
+ FLog.i("OverlayModule", "onHostDestroy");
112
+ handleDestroy();
113
+ }
114
+
115
+ private String genOverlayKey(String moduleName, int id) {
116
+ return moduleName + "-" + id;
117
+ }
118
+
119
+ private ReadableMap getInsets(Activity activity) {
120
+ WindowInsetsCompat windowInsets = ViewCompat.getRootWindowInsets(activity.getWindow().getDecorView());
121
+ assert windowInsets != null;
122
+ Insets navigationBarInsets = windowInsets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.navigationBars());
123
+ Insets statusBarInsets = windowInsets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.statusBars());
124
+ Insets displayCutoutInsets = windowInsets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.displayCutout());
125
+ WritableMap insets = new JavaOnlyMap();
126
+ insets.putDouble("left", Math.floor(PixelUtil.toDIPFromPixel(Math.max(navigationBarInsets.left, displayCutoutInsets.left) + 0.5f)));
127
+ insets.putDouble("top", Math.floor(PixelUtil.toDIPFromPixel(statusBarInsets.top + 0.5f)));
128
+ insets.putDouble("right", Math.floor(PixelUtil.toDIPFromPixel(Math.max(navigationBarInsets.right, displayCutoutInsets.right) + 0.5f)));
129
+ insets.putDouble("bottom", Math.floor(PixelUtil.toDIPFromPixel(navigationBarInsets.bottom) + 0.5f));
130
+ return insets;
131
+ }
111
132
  }
@@ -2,7 +2,6 @@ package com.reactnative.overlay;
2
2
 
3
3
  import androidx.annotation.NonNull;
4
4
 
5
- import com.facebook.react.ReactInstanceManager;
6
5
  import com.facebook.react.ReactNativeHost;
7
6
  import com.facebook.react.ReactPackage;
8
7
  import com.facebook.react.bridge.NativeModule;
@@ -14,21 +13,21 @@ import java.util.List;
14
13
 
15
14
  public class OverlayPackage implements ReactPackage {
16
15
 
17
- private final ReactNativeHost reactNativeHost;
16
+ private final ReactNativeHost reactNativeHost;
18
17
 
19
- public OverlayPackage(ReactNativeHost reactNativeHost) {
20
- this.reactNativeHost = reactNativeHost;
21
- }
18
+ public OverlayPackage(ReactNativeHost reactNativeHost) {
19
+ this.reactNativeHost = reactNativeHost;
20
+ }
22
21
 
23
- @NonNull
24
- @Override
25
- public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
26
- return Collections.singletonList(new OverlayModule(reactContext, reactNativeHost));
27
- }
22
+ @NonNull
23
+ @Override
24
+ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
25
+ return Collections.singletonList(new OverlayModule(reactContext, reactNativeHost));
26
+ }
28
27
 
29
- @NonNull
30
- @Override
31
- public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
32
- return Collections.emptyList();
33
- }
28
+ @NonNull
29
+ @Override
30
+ public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
31
+ return Collections.emptyList();
32
+ }
34
33
  }