Skip to content

Commit 6ad70ec

Browse files
committed
feat: switch dynamically between implementations
1 parent c6c47fd commit 6ad70ec

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

packages/ui/android/src/main/java/com/harnessui/UIHelperImpl.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.harnessui
33
import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.Canvas
6+
import android.graphics.Rect
67
import android.os.Handler
78
import android.os.Looper
89
import android.os.SystemClock
@@ -263,12 +264,25 @@ class UIHelperImpl(
263264
val height = targetView.height
264265

265266
if (width > 0 && height > 0) {
266-
Log.i(TAG, "Capturing view $nativeId: w=$width h=$height")
267+
val location = IntArray(2)
268+
targetView.getLocationOnScreen(location)
269+
val viewRect = Rect(location[0], location[1], location[0] + width, location[1] + height)
270+
val rootRect = Rect(0, 0, root.width, root.height)
271+
272+
val isFullyOnScreen = rootRect.contains(viewRect)
267273
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
268274
val canvas = Canvas(bitmap)
269275

270-
// Draw the specific view
271-
targetView.draw(canvas)
276+
if (isFullyOnScreen) {
277+
Log.i(TAG, "View $nativeId is fully on screen. Using screen-based screenshotting.")
278+
// Translate canvas to capture the view from the root view hierarchy
279+
canvas.translate(-location[0].toFloat(), -location[1].toFloat())
280+
root.draw(canvas)
281+
} else {
282+
Log.i(TAG, "View $nativeId is partially/fully off screen. Using direct view render.")
283+
// Draw the specific view directly
284+
targetView.draw(canvas)
285+
}
272286

273287
val outputStream = ByteArrayOutputStream()
274288
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)

packages/ui/ios/HarnessUI.mm

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,22 @@ - (void)captureScreenshot:(JS::NativeHarnessUI::ViewInfo *)bounds
446446
if (nativeId) {
447447
UIView *targetView = [ViewRegistry getView:nativeId];
448448
if (targetView) {
449-
RCTLogInfo(@"[HarnessUI] Found view for nativeId: %@. Capturing direct view render.", nativeId);
450-
pngData = [ViewQueryHelper captureScreenshotOfView:targetView];
449+
UIWindow *window = [self getActiveWindow];
450+
if (window) {
451+
CGRect frameInWindow = [targetView convertRect:targetView.bounds toView:window];
452+
BOOL isFullyOnScreen = CGRectContainsRect(window.bounds, frameInWindow);
453+
454+
if (isFullyOnScreen) {
455+
RCTLogInfo(@"[HarnessUI] View %@ is fully on screen. Using screen-based screenshotting.", nativeId);
456+
pngData = [ViewQueryHelper captureScreenshotWithBounds:frameInWindow];
457+
} else {
458+
RCTLogInfo(@"[HarnessUI] View %@ is partially/fully off screen. Using direct view render.", nativeId);
459+
pngData = [ViewQueryHelper captureScreenshotOfView:targetView];
460+
}
461+
} else {
462+
RCTLogWarn(@"[HarnessUI] No active window found for nativeId: %@. Using direct view render.", nativeId);
463+
pngData = [ViewQueryHelper captureScreenshotOfView:targetView];
464+
}
451465
} else {
452466
RCTLogWarn(@"[HarnessUI] View with nativeId %@ not found. Falling back to window bounds capture.", nativeId);
453467
pngData = [ViewQueryHelper captureScreenshotWithBounds:captureRect];

0 commit comments

Comments
 (0)