-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
263 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import type { SourceFile } from '../syntax/SourceFile.js' | ||
import type { HybridObjectSpec } from '../syntax/HybridObjectSpec.js' | ||
import { createIndentation } from '../utils.js' | ||
import { createFileMetadataString } from '../syntax/helpers.js' | ||
import { NitroConfig } from '../config/NitroConfig.js' | ||
import { getHybridObjectName } from '../syntax/getHybridObjectName.js' | ||
|
||
interface ViewComponentNames { | ||
propsClassName: `${string}Props` | ||
stateClassName: `${string}State` | ||
nameVariable: `${string}ComponentName` | ||
shadowNodeClassName: `${string}ShadowNode` | ||
descriptorClassName: `${string}ComponentDescriptor` | ||
component: `${string}Component` | ||
} | ||
|
||
export function getViewComponentNames( | ||
spec: HybridObjectSpec | ||
): ViewComponentNames { | ||
const name = getHybridObjectName(spec.name) | ||
return { | ||
propsClassName: `${name.HybridT}Props`, | ||
stateClassName: `${name.HybridT}State`, | ||
nameVariable: `${name.HybridT}ComponentName`, | ||
shadowNodeClassName: `${name.HybridT}ShadowNode`, | ||
descriptorClassName: `${name.HybridT}ComponentDescriptor`, | ||
component: `${spec.name}Component`, | ||
} | ||
} | ||
|
||
export function createViewComponentShadowNodeFiles( | ||
spec: HybridObjectSpec | ||
): SourceFile[] { | ||
if (!spec.isHybridView) { | ||
throw new Error( | ||
`Cannot create View Component ShadowNode code for ${spec.name} - it's not a HybridView!` | ||
) | ||
} | ||
|
||
const name = getHybridObjectName(spec.name) | ||
const { | ||
propsClassName, | ||
stateClassName, | ||
nameVariable, | ||
shadowNodeClassName, | ||
descriptorClassName, | ||
component, | ||
} = getViewComponentNames(spec) | ||
|
||
const namespace = NitroConfig.getCxxNamespace('c++', 'views') | ||
|
||
// .hpp code | ||
const componentHeaderCode = ` | ||
${createFileMetadataString(`${component}.hpp`)} | ||
#pragma once | ||
#include "NitroDefines.hpp" | ||
#if REACT_NATIVE_VERSION >= 78 | ||
#include <react/renderer/core/ConcreteComponentDescriptor.h> | ||
#include <react/renderer/core/PropsParserContext.h> | ||
#include <react/renderer/components/view/ConcreteViewShadowNode.h> | ||
#include <react/renderer/components/view/ViewProps.h> | ||
namespace ${namespace} { | ||
using namespace facebook; | ||
class ${propsClassName}: public react::ViewProps { | ||
public: | ||
explicit ${propsClassName}() = default; | ||
${propsClassName}(const react::PropsParserContext& context, | ||
${createIndentation(propsClassName.length)} const ${propsClassName}& sourceProps, | ||
${createIndentation(propsClassName.length)} const react::RawProps& rawProps); | ||
}; | ||
class ${stateClassName} { | ||
public: | ||
explicit ${stateClassName}() = default; | ||
}; | ||
extern const char ${nameVariable}[]; | ||
using ${shadowNodeClassName} = react::ConcreteViewShadowNode<${nameVariable}, ${propsClassName}, react::ViewEventEmitter, ${stateClassName}>; | ||
class ${descriptorClassName}: public react::ConcreteComponentDescriptor<${shadowNodeClassName}> { | ||
public: | ||
${descriptorClassName}(const react::ComponentDescriptorParameters& parameters); | ||
}; | ||
// TODO: Actual RCTViewComponentView goes here... or in Swift? | ||
} // namespace ${namespace} | ||
#else | ||
#warning "View Component '${name.HybridT}' will be unavailable in React Native, because it requires React Native 78 or higher." | ||
#endif | ||
`.trim() | ||
|
||
// .cpp code | ||
const ctorIndent = createIndentation(propsClassName.length * 2) | ||
const componentCode = ` | ||
${createFileMetadataString(`${component}.cpp`)} | ||
#include "${component}.hpp" | ||
#if REACT_NATIVE_VERSION >= 78 | ||
namespace ${namespace} { | ||
${propsClassName}::${propsClassName}(const react::PropsParserContext& context, | ||
${ctorIndent} const ${propsClassName}& sourceProps, | ||
${ctorIndent} const react::RawProps& rawProps): react::ViewProps(context, sourceProps, rawProps) { | ||
if (rawProps.isEmpty()) { | ||
// TODO: idk? Hanno? | ||
return; | ||
} | ||
const RawValue* rawValue = rawProps.at("nativeProp", nullptr, nullptr); | ||
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue; | ||
// TODO: Parse runtime and value | ||
} | ||
${nameVariable} = "${name.HybridT}"; | ||
${descriptorClassName}::${descriptorClassName}(const react::ComponentDescriptorParameters& parameters) | ||
: ConcreteComponentDescriptor(parameters, | ||
std::make_unique<react::RawPropsParser>(/* enableJsiParser */ true)) {} | ||
} // namespace ${namespace} | ||
#endif | ||
`.trim() | ||
|
||
return [ | ||
{ | ||
name: `${component}.hpp`, | ||
content: componentHeaderCode, | ||
language: 'c++', | ||
platform: 'shared', | ||
subdirectory: ['views'], | ||
}, | ||
{ | ||
name: `${component}.cpp`, | ||
content: componentCode, | ||
language: 'c++', | ||
platform: 'shared', | ||
subdirectory: ['views'], | ||
}, | ||
] | ||
} |
80 changes: 32 additions & 48 deletions
80
packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/react-native-nitro-image/nitrogen/generated/ios/c++/views/TestViewComponent.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// | ||
/// TestViewComponent.mm | ||
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. | ||
/// https://github.com/mrousavy/nitro | ||
/// Copyright © 2025 Marc Rousavy @ Margelo | ||
/// | ||
|
||
#if REACT_NATIVE_VERSION >= 78 | ||
|
||
#import "TestViewComponent.hpp" | ||
#import <React/RCTComponentViewFactory.h> | ||
#import <React/UIView+ComponentViewProtocol.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
namespace margelo::nitro::image::views { | ||
|
||
@interface TestViewComponent: RCTViewComponentView | ||
@end | ||
|
||
@implementation TestViewComponent | ||
+ (void) load { | ||
// TODO: Register it! | ||
} | ||
|
||
+ (ComponentDescriptorProvider)componentDescriptorProvider { | ||
return concreteComponentDescriptorProvider<HybridTestViewComponentDescriptor>(); | ||
} | ||
|
||
- (void)updateProps:(const facebook::react::Props::Shared&)props | ||
oldProps:(const facebook::react::Props::Shared&)oldProps { | ||
// TODO: const auto& newViewProps = *std::static_pointer_cast<CustomViewProps const>(props); | ||
|
||
[super updateProps:props oldProps:oldProps]; | ||
} | ||
@end | ||
|
||
} // namespace margelo::nitro::image::views | ||
|
||
#endif |
34 changes: 34 additions & 0 deletions
34
packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/TestViewComponent.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// | ||
/// TestViewComponent.cpp | ||
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. | ||
/// https://github.com/mrousavy/nitro | ||
/// Copyright © 2025 Marc Rousavy @ Margelo | ||
/// | ||
|
||
#include "TestViewComponent.hpp" | ||
|
||
#if REACT_NATIVE_VERSION >= 78 | ||
|
||
namespace margelo::nitro::image::views { | ||
|
||
HybridTestViewProps::HybridTestViewProps(const react::PropsParserContext& context, | ||
const HybridTestViewProps& sourceProps, | ||
const react::RawProps& rawProps): react::ViewProps(context, sourceProps, rawProps) { | ||
if (rawProps.isEmpty()) { | ||
// TODO: idk? Hanno? | ||
return; | ||
} | ||
const RawValue* rawValue = rawProps.at("nativeProp", nullptr, nullptr); | ||
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue; | ||
// TODO: Parse runtime and value | ||
} | ||
|
||
HybridTestViewComponentName = "HybridTestView"; | ||
|
||
HybridTestViewComponentDescriptor::HybridTestViewComponentDescriptor(const react::ComponentDescriptorParameters& parameters) | ||
: ConcreteComponentDescriptor(parameters, | ||
std::make_unique<react::RawPropsParser>(/* enableJsiParser */ true)) {} | ||
|
||
} // namespace margelo::nitro::image::views | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters