Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@
* @format
*/

import type {Overlay} from '../../Debugging/DebuggingOverlayNativeComponent';
import type {
InstanceFromReactDevTools,
ReactDevToolsAgent,
} from '../../Types/ReactDevToolsTypes';
import type {Overlay} from './TraceUpdateOverlayNativeComponent';

import DebuggingOverlayNativeComponent, {
Commands,
} from '../../Debugging/DebuggingOverlayNativeComponent';
import UIManager from '../../ReactNative/UIManager';
import processColor from '../../StyleSheet/processColor';
import StyleSheet from '../../StyleSheet/StyleSheet';
import Platform from '../../Utilities/Platform';
import View from '../View/View';
import TraceUpdateOverlayNativeComponent, {
Commands,
} from './TraceUpdateOverlayNativeComponent';
import * as React from 'react';

const {useEffect, useRef, useState} = React;
const isNativeComponentReady =
Platform.OS === 'android' &&
UIManager.hasViewManagerConfig('TraceUpdateOverlay');
UIManager.hasViewManagerConfig('DebuggingOverlay');

type Props = {
reactDevToolsAgent: ReactDevToolsAgent,
Expand All @@ -39,13 +37,13 @@ export default function TraceUpdateOverlay({
const [overlayDisabled, setOverlayDisabled] = useState(false);

useEffect(() => {
if (!isNativeComponentReady) {
return;
}

const drawTraceUpdates = (
nodesToDraw: Array<{node: InstanceFromReactDevTools, color: string}> = [],
) => {
if (!isNativeComponentReady) {
return;
}

// If overlay is disabled before, now it's enabled.
setOverlayDisabled(false);

Expand Down Expand Up @@ -113,13 +111,13 @@ export default function TraceUpdateOverlay({
}, [reactDevToolsAgent]);

const nativeComponentRef =
useRef<?React.ElementRef<typeof TraceUpdateOverlayNativeComponent>>(null);
useRef<?React.ElementRef<typeof DebuggingOverlayNativeComponent>>(null);

return (
!overlayDisabled &&
isNativeComponentReady && (
<View pointerEvents="none" style={styles.overlay}>
<TraceUpdateOverlayNativeComponent
<DebuggingOverlayNativeComponent
ref={nativeComponentRef}
style={styles.overlay}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
* @format
*/

import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
import type {ProcessedColorValue} from '../../StyleSheet/processColor';
import type {ViewProps} from '../View/ViewPropTypes';
import type {ViewProps} from '../Components/View/ViewPropTypes';
import type {HostComponent} from '../Renderer/shims/ReactNativeTypes';
import type {ProcessedColorValue} from '../StyleSheet/processColor';

import codegenNativeCommands from '../../Utilities/codegenNativeCommands';
import codegenNativeComponent from '../../Utilities/codegenNativeComponent';
import codegenNativeCommands from '../Utilities/codegenNativeCommands';
import codegenNativeComponent from '../Utilities/codegenNativeComponent';
import * as React from 'react';

type NativeProps = $ReadOnly<{|
...ViewProps,
|}>;
export type TraceUpdateOverlayNativeComponentType = HostComponent<NativeProps>;
export type DebuggingOverlayNativeComponentType = HostComponent<NativeProps>;
export type Overlay = {
rect: {left: number, top: number, width: number, height: number},
color: ?ProcessedColorValue,
};

interface NativeCommands {
+draw: (
viewRef: React.ElementRef<TraceUpdateOverlayNativeComponentType>,
viewRef: React.ElementRef<DebuggingOverlayNativeComponentType>,
// TODO(T144046177): Ideally we can pass array of Overlay, but currently
// Array type is not supported in RN codegen for building native commands.
overlays: string,
Expand All @@ -39,5 +39,5 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
});

export default (codegenNativeComponent<NativeProps>(
'TraceUpdateOverlay',
'DebuggingOverlay',
): HostComponent<NativeProps>);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <UIKit/UIKit.h>

#import <React/RCTViewComponentView.h>

#import <react/renderer/components/rncore/RCTComponentViewHelpers.h>

@interface RCTDebuggingOverlayComponentView : RCTViewComponentView <RCTDebuggingOverlayViewProtocol>

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTDebuggingOverlayComponentView.h"

#import <React/RCTDebuggingOverlay.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>

#import <react/renderer/components/rncore/ComponentDescriptors.h>
#import <react/renderer/components/rncore/EventEmitters.h>
#import <react/renderer/components/rncore/Props.h>
#import <react/renderer/components/rncore/RCTComponentViewHelpers.h>

#import "RCTFabricComponentsPlugins.h"

using namespace facebook::react;

@implementation RCTDebuggingOverlayComponentView {
RCTDebuggingOverlay *_overlay;
}

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const DebuggingOverlayProps>();
_props = defaultProps;

_overlay = [[RCTDebuggingOverlay alloc] initWithFrame:self.bounds];

self.contentView = _overlay;
}

return self;
}

#pragma mark - RCTComponentViewProtocol

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<DebuggingOverlayComponentDescriptor>();
}

#pragma mark - Native commands

- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
{
RCTDebuggingOverlayHandleCommand(self, commandName, args);
}

- (void)draw:(NSString *)overlays
{
[_overlay draw:overlays];
}

@end

Class<RCTComponentViewProtocol> RCTDebuggingOverlayCls(void)
{
return RCTDebuggingOverlayComponentView.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Class<RCTComponentViewProtocol> RCTFabricComponentsProvider(const char *name);

// Lookup functions
Class<RCTComponentViewProtocol> RCTActivityIndicatorViewCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTDebuggingOverlayCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTInputAccessoryCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTParagraphCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTPullToRefreshViewCls(void) __attribute__((used));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Class<RCTComponentViewProtocol> RCTFabricComponentsProvider(const char *name) {
static std::unordered_map<std::string, Class (*)(void)> sFabricComponentsClassMap = {
{"ActivityIndicatorView", RCTActivityIndicatorViewCls},
{"DebuggingOverlay", RCTDebuggingOverlayCls},
{"InputAccessoryView", RCTInputAccessoryCls},
{"Paragraph", RCTParagraphCls},
{"PullToRefreshView", RCTPullToRefreshViewCls},
Expand Down
16 changes: 16 additions & 0 deletions packages/react-native/React/Views/RCTDebuggingOverlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <UIKit/UIKit.h>

#import <React/RCTView.h>

@interface RCTDebuggingOverlay : RCTView

- (void)draw:(NSString *)serializedNodes;

@end
57 changes: 57 additions & 0 deletions packages/react-native/React/Views/RCTDebuggingOverlay.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTDebuggingOverlay.h"

#import <React/RCTConvert.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>

@implementation RCTDebuggingOverlay

- (void)draw:(NSString *)serializedNodes
{
NSArray *subViewsToRemove = [self subviews];
for (UIView *v in subViewsToRemove) {
[v removeFromSuperview];
}

NSError *error = nil;
id deserializedNodes = RCTJSONParse(serializedNodes, &error);

if (error) {
RCTLogError(@"Failed to parse serialized nodes passed to RCTDebuggingOverlay");
return;
}

if (![deserializedNodes isKindOfClass:[NSArray class]]) {
RCTLogError(@"Expected to receive nodes as an array, got %@", NSStringFromClass([deserializedNodes class]));
return;
}

for (NSDictionary *node in deserializedNodes) {
NSDictionary *nodeRectangle = node[@"rect"];
NSNumber *nodeColor = node[@"color"];

NSNumber *x = nodeRectangle[@"left"];
NSNumber *y = nodeRectangle[@"top"];
NSNumber *width = nodeRectangle[@"width"];
NSNumber *height = nodeRectangle[@"height"];

CGRect rect = CGRectMake(x.doubleValue, y.doubleValue, width.doubleValue, height.doubleValue);

UIView *box = [[UIView alloc] initWithFrame:rect];
box.backgroundColor = [UIColor clearColor];

box.layer.borderWidth = 2.0f;
box.layer.borderColor = [RCTConvert UIColor:nodeColor].CGColor;

[self addSubview:box];
}
}

@end
12 changes: 12 additions & 0 deletions packages/react-native/React/Views/RCTDebuggingOverlayManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTViewManager.h>

@interface RCTDebuggingOverlayManager : RCTViewManager

@end
38 changes: 38 additions & 0 deletions packages/react-native/React/Views/RCTDebuggingOverlayManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTDebuggingOverlayManager.h"
#import "RCTDebuggingOverlay.h"

#import <React/RCTLog.h>
#import <React/RCTUIManager.h>

#import "RCTBridge.h"

@implementation RCTDebuggingOverlayManager

RCT_EXPORT_MODULE(DebuggingOverlay)

- (UIView *)view
{
return [RCTDebuggingOverlay new];
}

RCT_EXPORT_METHOD(draw : (nonnull NSNumber *)viewTag nodes : (NSString *)serializedNodes)
{
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[viewTag];

if ([view isKindOfClass:[RCTDebuggingOverlay class]]) {
[(RCTDebuggingOverlay *)view draw:serializedNodes];
} else {
RCTLogError(@"Expected view to be RCTDebuggingOverlay, got %@", NSStringFromClass([view class]));
}
}];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.traceupdateoverlay.TraceUpdateOverlayManager;
import com.facebook.react.views.debuggingoverlay.DebuggingOverlayManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -96,8 +96,7 @@ private static void appendMap(
private Map<String, ModuleSpec> getViewManagersMap() {
if (mViewManagers == null) {
Map<String, ModuleSpec> viewManagers = new HashMap<>();
appendMap(
viewManagers, TraceUpdateOverlayManager.REACT_CLASS, TraceUpdateOverlayManager::new);
appendMap(viewManagers, DebuggingOverlayManager.REACT_CLASS, DebuggingOverlayManager::new);

mViewManagers = viewManagers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.traceupdateoverlay;
package com.facebook.react.views.debuggingoverlay;

import android.content.Context;
import android.graphics.Canvas;
Expand All @@ -17,7 +17,7 @@
import java.util.ArrayList;
import java.util.List;

public class TraceUpdateOverlay extends View {
public class DebuggingOverlay extends View {
private final Paint mOverlayPaint = new Paint();
private List<Overlay> mOverlays = new ArrayList<Overlay>();

Expand All @@ -43,7 +43,7 @@ public RectF getPixelRect() {
}
}

public TraceUpdateOverlay(Context context) {
public DebuggingOverlay(Context context) {
super(context);
mOverlayPaint.setStyle(Paint.Style.STROKE);
mOverlayPaint.setStrokeWidth(6);
Expand Down
Loading