Add support for react-native Windows Cpp/WinRT (#1893)
This also deprecates the old react-native windows implementation
This commit is contained in:
110
windows/ReactNativeVideoCPP/ReactVideoViewManager.cpp
Normal file
110
windows/ReactNativeVideoCPP/ReactVideoViewManager.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "pch.h"
|
||||
#include "ReactVideoViewManager.h"
|
||||
#include "NativeModules.h"
|
||||
#include "ReactVideoView.h"
|
||||
|
||||
namespace winrt::ReactNativeVideoCPP::implementation {
|
||||
|
||||
ReactVideoViewManager::ReactVideoViewManager() {}
|
||||
|
||||
// IViewManager
|
||||
hstring ReactVideoViewManager::Name() noexcept {
|
||||
return L"RCTVideo";
|
||||
}
|
||||
|
||||
FrameworkElement ReactVideoViewManager::CreateView() noexcept {
|
||||
auto reactVideoView = winrt::ReactNativeVideoCPP::ReactVideoView(m_reactContext);
|
||||
return reactVideoView;
|
||||
}
|
||||
|
||||
// IViewManagerWithReactContext
|
||||
IReactContext ReactVideoViewManager::ReactContext() noexcept {
|
||||
return m_reactContext;
|
||||
}
|
||||
|
||||
void ReactVideoViewManager::ReactContext(IReactContext reactContext) noexcept {
|
||||
m_reactContext = reactContext;
|
||||
}
|
||||
|
||||
// IViewManagerWithExportedViewConstants
|
||||
winrt::Microsoft::ReactNative::ConstantProviderDelegate ReactVideoViewManager::ExportedViewConstants() noexcept {
|
||||
return [](winrt::Microsoft::ReactNative::IJSValueWriter const &constantWriter) {
|
||||
WriteProperty(constantWriter, L"ScaleNone", to_hstring(std::to_string((int)Stretch::None)));
|
||||
WriteProperty(constantWriter, L"ScaleToFill", to_hstring(std::to_string((int)Stretch::UniformToFill)));
|
||||
WriteProperty(constantWriter, L"ScaleAspectFit", to_hstring(std::to_string((int)Stretch::Uniform)));
|
||||
WriteProperty(constantWriter, L"ScaleAspectFill", to_hstring(std::to_string((int)Stretch::Fill)));
|
||||
};
|
||||
}
|
||||
|
||||
// IViewManagerWithNativeProperties
|
||||
IMapView<hstring, ViewManagerPropertyType> ReactVideoViewManager::NativeProps() noexcept {
|
||||
auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
|
||||
nativeProps.Insert(L"src", ViewManagerPropertyType::Map);
|
||||
nativeProps.Insert(L"resizeMode", ViewManagerPropertyType::String);
|
||||
nativeProps.Insert(L"repeat", ViewManagerPropertyType::Boolean);
|
||||
nativeProps.Insert(L"paused", ViewManagerPropertyType::Boolean);
|
||||
nativeProps.Insert(L"muted", ViewManagerPropertyType::Boolean);
|
||||
nativeProps.Insert(L"volume", ViewManagerPropertyType::Number);
|
||||
nativeProps.Insert(L"seek", ViewManagerPropertyType::Number);
|
||||
nativeProps.Insert(L"controls", ViewManagerPropertyType::Boolean);
|
||||
nativeProps.Insert(L"fullscreen", ViewManagerPropertyType::Boolean);
|
||||
nativeProps.Insert(L"progressUpdateInterval", ViewManagerPropertyType::Number);
|
||||
|
||||
return nativeProps.GetView();
|
||||
}
|
||||
|
||||
void ReactVideoViewManager::UpdateProperties(
|
||||
FrameworkElement const &view,
|
||||
IJSValueReader const &propertyMapReader) noexcept {
|
||||
if (auto reactVideoView = view.try_as<winrt::ReactNativeVideoCPP::ReactVideoView>()) {
|
||||
const JSValueObject &propertyMap = JSValue::ReadObjectFrom(propertyMapReader);
|
||||
|
||||
for (auto const &pair : propertyMap) {
|
||||
auto const &propertyName = pair.first;
|
||||
auto const &propertyValue = pair.second;
|
||||
if (!propertyValue.IsNull()) {
|
||||
if (propertyName == "src") {
|
||||
auto const &srcMap = propertyValue.Object();
|
||||
auto const &uri = srcMap.at("uri");
|
||||
reactVideoView.Set_UriString(to_hstring(uri.String()));
|
||||
} else if (propertyName == "resizeMode") {
|
||||
reactVideoView.Stretch(static_cast<Stretch>(std::stoul(propertyValue.String())));
|
||||
} else if (propertyName == "repeat") {
|
||||
reactVideoView.Set_IsLoopingEnabled(propertyValue.Boolean());
|
||||
} else if (propertyName == "paused") {
|
||||
m_paused = propertyValue.Boolean();
|
||||
reactVideoView.Set_Paused(m_paused);
|
||||
} else if (propertyName == "muted") {
|
||||
reactVideoView.Set_Muted(propertyValue.Boolean());
|
||||
} else if (propertyName == "volume") {
|
||||
reactVideoView.Set_Volume(propertyValue.Double());
|
||||
} else if (propertyName == "seek") {
|
||||
reactVideoView.Set_Position(propertyValue.Double());
|
||||
} else if (propertyName == "controls") {
|
||||
reactVideoView.Set_Controls(propertyValue.Boolean());
|
||||
} else if (propertyName == "fullscreen") {
|
||||
reactVideoView.Set_FullScreen(propertyValue.Boolean());
|
||||
} else if (propertyName == "progressUpdateInterval") {
|
||||
reactVideoView.Set_ProgressUpdateInterval(propertyValue.Int64());
|
||||
}
|
||||
}
|
||||
}
|
||||
reactVideoView.Set_AutoPlay(!m_paused); // auto play on pause false or not set.
|
||||
}
|
||||
}
|
||||
|
||||
// IViewManagerWithExportedEventTypeConstants
|
||||
ConstantProviderDelegate ReactVideoViewManager::ExportedCustomBubblingEventTypeConstants() noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ConstantProviderDelegate ReactVideoViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
|
||||
return [](winrt::Microsoft::ReactNative::IJSValueWriter const &constantWriter) {
|
||||
WriteCustomDirectEventTypeConstant(constantWriter, "Load");
|
||||
WriteCustomDirectEventTypeConstant(constantWriter, "End");
|
||||
WriteCustomDirectEventTypeConstant(constantWriter, "Seek");
|
||||
WriteCustomDirectEventTypeConstant(constantWriter, "Progress");
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace winrt::ReactNativeVideoCPP::implementation
|
||||
Reference in New Issue
Block a user