Sin descripción

ReactWebViewManager.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "pch.h"
  2. #include "ReactWebViewManager.h"
  3. #include "NativeModules.h"
  4. #include "ReactWebView.h"
  5. #include "JSValueXaml.h"
  6. namespace winrt {
  7. using namespace Microsoft::ReactNative;
  8. using namespace Windows::Foundation;
  9. using namespace Windows::Foundation::Collections;
  10. using namespace Windows::UI;
  11. using namespace Windows::UI::Xaml;
  12. using namespace Windows::UI::Xaml::Controls;
  13. }
  14. namespace winrt::ReactNativeWebView::implementation {
  15. ReactWebViewManager::ReactWebViewManager() {}
  16. // IViewManager
  17. winrt::hstring ReactWebViewManager::Name() noexcept {
  18. return L"RCTWebView";
  19. }
  20. winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
  21. m_reactWebView = *winrt::make_self<ReactWebView>(m_reactContext);
  22. return m_reactWebView.GetView();
  23. }
  24. // IViewManagerWithReactContext
  25. winrt::IReactContext ReactWebViewManager::ReactContext() noexcept {
  26. return m_reactContext;
  27. }
  28. void ReactWebViewManager::ReactContext(IReactContext reactContext) noexcept {
  29. m_reactContext = reactContext;
  30. }
  31. // IViewManagerWithNativeProperties
  32. IMapView<hstring, ViewManagerPropertyType> ReactWebViewManager::NativeProps() noexcept {
  33. auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
  34. nativeProps.Insert(L"source", ViewManagerPropertyType::Map);
  35. return nativeProps.GetView();
  36. }
  37. void ReactWebViewManager::UpdateProperties(
  38. FrameworkElement const& view,
  39. IJSValueReader const& propertyMapReader) noexcept {
  40. if (auto webView = view.try_as<winrt::WebView>()) {
  41. const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
  42. for (auto const& pair : propertyMap) {
  43. auto const& propertyName = pair.first;
  44. auto const& propertyValue = pair.second;
  45. if (propertyValue.IsNull()) continue;
  46. if (propertyName == "source") {
  47. auto const& srcMap = propertyValue.AsObject();
  48. if (srcMap.find("uri") != srcMap.end()) {
  49. auto uriString = srcMap.at("uri").AsString();
  50. // non-uri sources not yet supported
  51. if (uriString.length() == 0) {
  52. continue;
  53. }
  54. bool isPackagerAsset = false;
  55. if (srcMap.find("__packager_asset") != srcMap.end()) {
  56. isPackagerAsset = srcMap.at("__packager_asset").AsBoolean();
  57. }
  58. if (isPackagerAsset && uriString.find("assets") == 0) {
  59. uriString.replace(0, 6, "ms-appx://");
  60. }
  61. webView.Navigate(winrt::Uri(to_hstring(uriString)));
  62. }
  63. else if (srcMap.find("html") != srcMap.end()) {
  64. auto htmlString = srcMap.at("html").AsString();
  65. webView.NavigateToString(to_hstring(htmlString));
  66. }
  67. }
  68. else if (propertyName == "backgroundColor") {
  69. auto color = propertyValue.To<winrt::Color>();
  70. webView.DefaultBackgroundColor(color.A==0 ? winrt::Colors::Transparent() : color);
  71. }
  72. }
  73. }
  74. }
  75. // IViewManagerWithExportedEventTypeConstants
  76. ConstantProviderDelegate ReactWebViewManager::ExportedCustomBubblingEventTypeConstants() noexcept {
  77. return nullptr;
  78. }
  79. ConstantProviderDelegate ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
  80. return [](winrt::IJSValueWriter const& constantWriter) {
  81. WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingStart");
  82. WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingFinish");
  83. WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingError");
  84. WriteCustomDirectEventTypeConstant(constantWriter, "onMessage");
  85. };
  86. }
  87. // IViewManagerWithCommands
  88. IMapView<hstring, int64_t> ReactWebViewManager::Commands() noexcept {
  89. auto commands = winrt::single_threaded_map<hstring, int64_t>();
  90. commands.Insert(L"goForward", static_cast<int32_t>(WebViewCommands::GoForward));
  91. commands.Insert(L"goBack", static_cast<int32_t>(WebViewCommands::GoBack));
  92. commands.Insert(L"reload", static_cast<int32_t>(WebViewCommands::Reload));
  93. commands.Insert(L"stopLoading", static_cast<int32_t>(WebViewCommands::StopLoading));
  94. commands.Insert(L"injectJavaScript", static_cast<int32_t>(WebViewCommands::InjectJavaScript));
  95. return commands.GetView();
  96. }
  97. void ReactWebViewManager::DispatchCommand(
  98. FrameworkElement const& view,
  99. int64_t commandId,
  100. winrt::IJSValueReader const& commandArgsReader) noexcept {
  101. if (auto webView = view.try_as<winrt::WebView>()) {
  102. switch (commandId) {
  103. case static_cast<int64_t>(WebViewCommands::GoForward) :
  104. if (webView.CanGoForward()) {
  105. webView.GoForward();
  106. }
  107. break;
  108. case static_cast<int64_t>(WebViewCommands::GoBack) :
  109. if (webView.CanGoBack()) {
  110. webView.GoBack();
  111. }
  112. break;
  113. case static_cast<int64_t>(WebViewCommands::Reload) :
  114. webView.Refresh();
  115. break;
  116. case static_cast<int64_t>(WebViewCommands::StopLoading) :
  117. webView.Stop();
  118. break;
  119. case static_cast<int64_t>(WebViewCommands::InjectJavaScript) :
  120. webView.InvokeScriptAsync(L"eval", { commandArgsReader.GetString() });
  121. break;
  122. }
  123. }
  124. }
  125. } // namespace winrt::ReactWebView::implementation