Skip to content

Commit

Permalink
refactor: TraceUpdates native component -> DebuggingOverlay (#41562)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #41562

Changelog: [Internal]

We will use this native component as a single layer for drawing debugging information: both for trace updates and inspected components from React DevTools.

Reviewed By: javache

Differential Revision: D51470789

fbshipit-source-id: 6c4633d2b70c2c2635a2bbfcd7adf1c727b73585
  • Loading branch information
hoxyq authored and facebook-github-bot committed Nov 23, 2023
1 parent 758e59f commit ed1056e
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +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 View from '../View/View';
import TraceUpdateOverlayNativeComponent, {
Commands,
} from './TraceUpdateOverlayNativeComponent';
import * as React from 'react';

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

type Props = {
reactDevToolsAgent: ReactDevToolsAgent,
Expand Down Expand Up @@ -111,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
Expand Up @@ -11,6 +11,6 @@

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

@interface RCTTraceUpdateOverlayComponentView : RCTViewComponentView <RCTTraceUpdateOverlayViewProtocol>
@interface RCTDebuggingOverlayComponentView : RCTViewComponentView <RCTDebuggingOverlayViewProtocol>

@end
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/

#import "RCTTraceUpdateOverlayComponentView.h"
#import "RCTDebuggingOverlayComponentView.h"

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

#import <react/renderer/components/rncore/ComponentDescriptors.h>
#import <react/renderer/components/rncore/EventEmitters.h>
Expand All @@ -20,17 +20,17 @@

using namespace facebook::react;

@implementation RCTTraceUpdateOverlayComponentView {
RCTTraceUpdateOverlay *_overlay;
@implementation RCTDebuggingOverlayComponentView {
RCTDebuggingOverlay *_overlay;
}

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

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

self.contentView = _overlay;
}
Expand All @@ -42,14 +42,14 @@ - (instancetype)initWithFrame:(CGRect)frame

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

#pragma mark - Native commands

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

- (void)draw:(NSString *)overlays
Expand All @@ -59,7 +59,7 @@ - (void)draw:(NSString *)overlays

@end

Class<RCTComponentViewProtocol> RCTTraceUpdateOverlayCls(void)
Class<RCTComponentViewProtocol> RCTDebuggingOverlayCls(void)
{
return RCTTraceUpdateOverlayComponentView.class;
return RCTDebuggingOverlayComponentView.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ 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));
Class<RCTComponentViewProtocol> RCTSafeAreaViewCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTScrollViewCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTSwitchCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTTextInputCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTTraceUpdateOverlayCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTUnimplementedNativeViewCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTViewCls(void) __attribute__((used));
Class<RCTComponentViewProtocol> RCTImageCls(void) __attribute__((used));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
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},
{"SafeAreaView", RCTSafeAreaViewCls},
{"ScrollView", RCTScrollViewCls},
{"Switch", RCTSwitchCls},
{"TextInput", RCTTextInputCls},
{"TraceUpdateOverlay", RCTTraceUpdateOverlayCls},
{"UnimplementedNativeView", RCTUnimplementedNativeViewCls},
{"View", RCTViewCls},
{"Image", RCTImageCls},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#import <React/RCTView.h>

@interface RCTTraceUpdateOverlay : RCTView
@interface RCTDebuggingOverlay : RCTView

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

#import "RCTTraceUpdateOverlay.h"
#import "RCTDebuggingOverlay.h"

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

@implementation RCTTraceUpdateOverlay
@implementation RCTDebuggingOverlay

- (void)draw:(NSString *)serializedNodes
{
Expand All @@ -24,7 +24,7 @@ - (void)draw:(NSString *)serializedNodes
id deserializedNodes = RCTJSONParse(serializedNodes, &error);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

#import <React/RCTViewManager.h>

@interface RCTTraceUpdateOverlayManager : RCTViewManager
@interface RCTDebuggingOverlayManager : RCTViewManager

@end
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@
* LICENSE file in the root directory of this source tree.
*/

#import "RCTTraceUpdateOverlayManager.h"
#import "RCTTraceUpdateOverlay.h"
#import "RCTDebuggingOverlayManager.h"
#import "RCTDebuggingOverlay.h"

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

#import "RCTBridge.h"

@implementation RCTTraceUpdateOverlayManager
@implementation RCTDebuggingOverlayManager

RCT_EXPORT_MODULE(TraceUpdateOverlay)
RCT_EXPORT_MODULE(DebuggingOverlay)

- (UIView *)view
{
return [RCTTraceUpdateOverlay new];
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:[RCTTraceUpdateOverlay class]]) {
[(RCTTraceUpdateOverlay *)view draw:serializedNodes];
if ([view isKindOfClass:[RCTDebuggingOverlay class]]) {
[(RCTDebuggingOverlay *)view draw:serializedNodes];
} else {
RCTLogError(@"Expected view to be RCTTraceUpdateOverlay, got %@", NSStringFromClass([view class]));
RCTLogError(@"Expected view to be RCTDebuggingOverlay, got %@", NSStringFromClass([view class]));
}
}];
}
Expand Down
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
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.graphics.RectF;
import androidx.annotation.Nullable;
Expand All @@ -16,22 +16,22 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.views.traceupdateoverlay.TraceUpdateOverlay.Overlay;
import com.facebook.react.views.debuggingoverlay.DebuggingOverlay.Overlay;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@ReactModule(name = TraceUpdateOverlayManager.REACT_CLASS)
public class TraceUpdateOverlayManager extends SimpleViewManager<TraceUpdateOverlay> {
public static final String REACT_CLASS = "TraceUpdateOverlay";
@ReactModule(name = DebuggingOverlayManager.REACT_CLASS)
public class DebuggingOverlayManager extends SimpleViewManager<DebuggingOverlay> {
public static final String REACT_CLASS = "DebuggingOverlay";

public TraceUpdateOverlayManager() {}
public DebuggingOverlayManager() {}

@Override
public void receiveCommand(
TraceUpdateOverlay view, String commandId, @Nullable ReadableArray args) {
DebuggingOverlay view, String commandId, @Nullable ReadableArray args) {
switch (commandId) {
case "draw":
if (args == null) {
Expand Down Expand Up @@ -67,13 +67,13 @@ public void receiveCommand(
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
new ReactNoCrashSoftException(
"Received unexpected command in TraceUpdateOverlayManager"));
"Received unexpected command in DebuggingOverlayManager"));
}
}

@Override
public TraceUpdateOverlay createViewInstance(ThemedReactContext context) {
return new TraceUpdateOverlay(context);
public DebuggingOverlay createViewInstance(ThemedReactContext context) {
return new DebuggingOverlay(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CoreComponentsRegistry::sharedProviderRegistry() {
providerRegistry->add(concreteComponentDescriptorProvider<
AndroidDrawerLayoutComponentDescriptor>());
providerRegistry->add(concreteComponentDescriptorProvider<
TraceUpdateOverlayComponentDescriptor>());
DebuggingOverlayComponentDescriptor>());

return providerRegistry;
}();
Expand Down

0 comments on commit ed1056e

Please sign in to comment.