mirror of
https://github.com/zoriya/react-native-omni.git
synced 2026-08-16 02:44:51 +00:00
Init using create-nitro-module
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
///
|
||||
/// HybridOmniSpec.cpp
|
||||
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
||||
/// https://github.com/mrousavy/nitro
|
||||
/// Copyright © Marc Rousavy @ Margelo
|
||||
///
|
||||
|
||||
#include "HybridOmniSpec.hpp"
|
||||
|
||||
namespace margelo::nitro::omni {
|
||||
|
||||
void HybridOmniSpec::loadHybridMethods() {
|
||||
// load base methods/properties
|
||||
HybridObject::loadHybridMethods();
|
||||
// load custom methods/properties
|
||||
registerHybrids(this, [](Prototype& prototype) {
|
||||
prototype.registerHybridGetter("isRed", &HybridOmniSpec::getIsRed);
|
||||
prototype.registerHybridSetter("isRed", &HybridOmniSpec::setIsRed);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace margelo::nitro::omni
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
///
|
||||
/// HybridOmniSpec.hpp
|
||||
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
||||
/// https://github.com/mrousavy/nitro
|
||||
/// Copyright © Marc Rousavy @ Margelo
|
||||
///
|
||||
|
||||
#pragma once
|
||||
|
||||
#if __has_include(<NitroModules/HybridObject.hpp>)
|
||||
#include <NitroModules/HybridObject.hpp>
|
||||
#else
|
||||
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace margelo::nitro::omni {
|
||||
|
||||
using namespace margelo::nitro;
|
||||
|
||||
/**
|
||||
* An abstract base class for `Omni`
|
||||
* Inherit this class to create instances of `HybridOmniSpec` in C++.
|
||||
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
||||
* @example
|
||||
* ```cpp
|
||||
* class HybridOmni: public HybridOmniSpec {
|
||||
* public:
|
||||
* HybridOmni(...): HybridObject(TAG) { ... }
|
||||
* // ...
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
class HybridOmniSpec: public virtual HybridObject {
|
||||
public:
|
||||
// Constructor
|
||||
explicit HybridOmniSpec(): HybridObject(TAG) { }
|
||||
|
||||
// Destructor
|
||||
~HybridOmniSpec() override = default;
|
||||
|
||||
public:
|
||||
// Properties
|
||||
virtual bool getIsRed() = 0;
|
||||
virtual void setIsRed(bool isRed) = 0;
|
||||
|
||||
public:
|
||||
// Methods
|
||||
|
||||
|
||||
protected:
|
||||
// Hybrid Setup
|
||||
void loadHybridMethods() override;
|
||||
|
||||
protected:
|
||||
// Tag for logging
|
||||
static constexpr auto TAG = "Omni";
|
||||
};
|
||||
|
||||
} // namespace margelo::nitro::omni
|
||||
@@ -0,0 +1,83 @@
|
||||
///
|
||||
/// HybridOmniComponent.cpp
|
||||
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
||||
/// https://github.com/mrousavy/nitro
|
||||
/// Copyright © Marc Rousavy @ Margelo
|
||||
///
|
||||
|
||||
#include "HybridOmniComponent.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <exception>
|
||||
#include <utility>
|
||||
#include <NitroModules/NitroDefines.hpp>
|
||||
#include <NitroModules/JSIConverter.hpp>
|
||||
#include <NitroModules/PropNameIDCache.hpp>
|
||||
#include <react/renderer/core/RawValue.h>
|
||||
#include <react/renderer/core/ShadowNode.h>
|
||||
#include <react/renderer/core/ComponentDescriptor.h>
|
||||
#include <react/renderer/components/view/ViewProps.h>
|
||||
|
||||
namespace margelo::nitro::omni::views {
|
||||
|
||||
extern const char HybridOmniComponentName[] = "Omni";
|
||||
|
||||
HybridOmniProps::HybridOmniProps(const react::PropsParserContext& context,
|
||||
const HybridOmniProps& sourceProps,
|
||||
const react::RawProps& rawProps):
|
||||
react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
|
||||
isRed([&]() -> CachedProp<bool> {
|
||||
try {
|
||||
const react::RawValue* rawValue = rawProps.at("isRed", nullptr, nullptr);
|
||||
if (rawValue == nullptr) return sourceProps.isRed;
|
||||
const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
|
||||
return CachedProp<bool>::fromRawValue(*runtime, value, sourceProps.isRed);
|
||||
} catch (const std::exception& exc) {
|
||||
throw std::runtime_error(std::string("Omni.isRed: ") + exc.what());
|
||||
}
|
||||
}()),
|
||||
hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridOmniSpec>& /* ref */)>>> {
|
||||
try {
|
||||
const react::RawValue* rawValue = rawProps.at("hybridRef", nullptr, nullptr);
|
||||
if (rawValue == nullptr) return sourceProps.hybridRef;
|
||||
const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
|
||||
return CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridOmniSpec>& /* ref */)>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, PropNameIDCache::get(*runtime, "f")), sourceProps.hybridRef);
|
||||
} catch (const std::exception& exc) {
|
||||
throw std::runtime_error(std::string("Omni.hybridRef: ") + exc.what());
|
||||
}
|
||||
}()) { }
|
||||
|
||||
bool HybridOmniProps::filterObjectKeys(const std::string& propName) {
|
||||
switch (hashString(propName)) {
|
||||
case hashString("isRed"): return true;
|
||||
case hashString("hybridRef"): return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
HybridOmniComponentDescriptor::HybridOmniComponentDescriptor(const react::ComponentDescriptorParameters& parameters)
|
||||
: ConcreteComponentDescriptor(parameters,
|
||||
react::RawPropsParser(/* enableJsiParser */ true)) {}
|
||||
|
||||
std::shared_ptr<const react::Props> HybridOmniComponentDescriptor::cloneProps(const react::PropsParserContext& context,
|
||||
const std::shared_ptr<const react::Props>& props,
|
||||
react::RawProps rawProps) const {
|
||||
// 1. Prepare raw props parser
|
||||
rawProps.parse(rawPropsParser_);
|
||||
// 2. Copy props with Nitro's cached copy constructor
|
||||
return HybridOmniShadowNode::Props(context, /* & */ rawProps, props);
|
||||
}
|
||||
|
||||
#ifdef ANDROID
|
||||
void HybridOmniComponentDescriptor::adopt(react::ShadowNode& shadowNode) const {
|
||||
// This is called immediately after `ShadowNode` is created, cloned or in progress.
|
||||
// On Android, we need to wrap props in our state, which gets routed through Java and later unwrapped in JNI/C++.
|
||||
auto& concreteShadowNode = static_cast<HybridOmniShadowNode&>(shadowNode);
|
||||
const std::shared_ptr<const HybridOmniProps>& constProps = concreteShadowNode.getConcreteSharedProps();
|
||||
const std::shared_ptr<HybridOmniProps>& props = std::const_pointer_cast<HybridOmniProps>(constProps);
|
||||
HybridOmniState state{props};
|
||||
concreteShadowNode.setStateData(std::move(state));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace margelo::nitro::omni::views
|
||||
@@ -0,0 +1,110 @@
|
||||
///
|
||||
/// HybridOmniComponent.hpp
|
||||
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
||||
/// https://github.com/mrousavy/nitro
|
||||
/// Copyright © Marc Rousavy @ Margelo
|
||||
///
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <NitroModules/NitroDefines.hpp>
|
||||
#include <NitroModules/NitroHash.hpp>
|
||||
#include <NitroModules/CachedProp.hpp>
|
||||
#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>
|
||||
|
||||
#include <memory>
|
||||
#include "HybridOmniSpec.hpp"
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
namespace margelo::nitro::omni::views {
|
||||
|
||||
using namespace facebook;
|
||||
|
||||
/**
|
||||
* The name of the actual native View.
|
||||
*/
|
||||
extern const char HybridOmniComponentName[];
|
||||
|
||||
/**
|
||||
* Props for the "Omni" View.
|
||||
*/
|
||||
class HybridOmniProps final: public react::ViewProps {
|
||||
public:
|
||||
HybridOmniProps() = default;
|
||||
HybridOmniProps(const react::PropsParserContext& context,
|
||||
const HybridOmniProps& sourceProps,
|
||||
const react::RawProps& rawProps);
|
||||
|
||||
public:
|
||||
CachedProp<bool> isRed;
|
||||
CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridOmniSpec>& /* ref */)>>> hybridRef;
|
||||
|
||||
private:
|
||||
static bool filterObjectKeys(const std::string& propName);
|
||||
};
|
||||
|
||||
/**
|
||||
* State for the "Omni" View.
|
||||
*/
|
||||
class HybridOmniState final {
|
||||
public:
|
||||
HybridOmniState() = default;
|
||||
explicit HybridOmniState(const std::shared_ptr<HybridOmniProps>& props):
|
||||
_props(props) {}
|
||||
|
||||
public:
|
||||
[[nodiscard]]
|
||||
const std::shared_ptr<HybridOmniProps>& getProps() const {
|
||||
return _props;
|
||||
}
|
||||
|
||||
public:
|
||||
#ifdef ANDROID
|
||||
HybridOmniState(const HybridOmniState& /* previousState */, folly::dynamic /* data */) {}
|
||||
folly::dynamic getDynamic() const {
|
||||
throw std::runtime_error("HybridOmniState does not support folly!");
|
||||
}
|
||||
react::MapBuffer getMapBuffer() const {
|
||||
throw std::runtime_error("HybridOmniState does not support MapBuffer!");
|
||||
};
|
||||
#endif
|
||||
|
||||
private:
|
||||
std::shared_ptr<HybridOmniProps> _props;
|
||||
};
|
||||
|
||||
/**
|
||||
* The Shadow Node for the "Omni" View.
|
||||
*/
|
||||
using HybridOmniShadowNode = react::ConcreteViewShadowNode<HybridOmniComponentName /* "HybridOmni" */,
|
||||
HybridOmniProps /* custom props */,
|
||||
react::ViewEventEmitter /* default */,
|
||||
HybridOmniState /* custom state */>;
|
||||
|
||||
/**
|
||||
* The Component Descriptor for the "Omni" View.
|
||||
*/
|
||||
class HybridOmniComponentDescriptor final: public react::ConcreteComponentDescriptor<HybridOmniShadowNode> {
|
||||
public:
|
||||
explicit HybridOmniComponentDescriptor(const react::ComponentDescriptorParameters& parameters);
|
||||
|
||||
public:
|
||||
/**
|
||||
* A faster path for cloning props - reuses the caching logic from `HybridOmniProps`.
|
||||
*/
|
||||
std::shared_ptr<const react::Props> cloneProps(const react::PropsParserContext& context,
|
||||
const std::shared_ptr<const react::Props>& props,
|
||||
react::RawProps rawProps) const override;
|
||||
#ifdef ANDROID
|
||||
void adopt(react::ShadowNode& shadowNode) const override;
|
||||
#endif
|
||||
};
|
||||
|
||||
/* The actual view for "Omni" needs to be implemented in platform-specific code. */
|
||||
|
||||
} // namespace margelo::nitro::omni::views
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"uiViewClassName": "Omni",
|
||||
"supportsRawText": false,
|
||||
"bubblingEventTypes": {},
|
||||
"directEventTypes": {},
|
||||
"validAttributes": {
|
||||
"isRed": true,
|
||||
"hybridRef": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user