From 4562d6f570094dd439e7d5be225ffc11141c14c0 Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Wed, 23 Oct 2019 09:20:12 -0700 Subject: [PATCH 1/2] Removed MainReactNativeHost from samples and use ReactApplication properties instead. --- .../windows/SampleAppCPP/App.cpp | 25 +-- .../windows/SampleAppCPP/App.h | 20 +-- .../SampleAppCPP/MainReactNativeHost.cpp | 11 -- .../SampleAppCPP/MainReactNativeHost.h | 60 ------- .../SampleAppCPP/MainReactNativeHost.idl | 11 -- .../windows/SampleAppCPP/SampleApp.vcxproj | 11 -- .../SampleAppCPP/SampleApp.vcxproj.filters | 1 - .../windows/SampleAppCS/App.xaml.cs | 35 ++-- .../SampleAppCS/MainReactNativeHost.cs | 42 ----- .../windows/SampleAppCS/SampleAppCS.csproj | 1 - .../ReactApplication.cpp | 55 ++++-- .../Microsoft.ReactNative/ReactApplication.h | 103 ++++++++++-- .../ReactApplication.idl | 30 ++-- .../ReactApplicationDelegate.h | 1 - .../Microsoft.ReactNative/ReactNativeHost.cpp | 133 +++++++-------- vnext/Microsoft.ReactNative/ReactNativeHost.h | 157 ++++++++++++------ .../Microsoft.ReactNative/ReactNativeHost.idl | 14 +- vnext/Microsoft.ReactNative/ReactRootView.cpp | 3 +- vnext/Microsoft.ReactNative/ReactRootView.h | 1 - yarn.lock | 8 +- 20 files changed, 372 insertions(+), 350 deletions(-) delete mode 100644 packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.cpp delete mode 100644 packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.h delete mode 100644 packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.idl delete mode 100644 packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/MainReactNativeHost.cs diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.cpp b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.cpp index af177b6933b..0b506929313 100644 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.cpp +++ b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.cpp @@ -4,23 +4,26 @@ #include "pch.h" #include "App.h" +#include "ReactPackageProvider.h" +#include "winrt/SampleLibraryCPP.h" +#include "winrt/SampleLibraryCS.h" -using namespace winrt; -using namespace Windows::ApplicationModel; -using namespace Windows::ApplicationModel::Activation; -using namespace Windows::Foundation; -using namespace Windows::UI::Xaml; -using namespace Windows::UI::Xaml::Controls; -using namespace Windows::UI::Xaml::Navigation; -using namespace SampleApp; -using namespace SampleApp::implementation; +namespace winrt::SampleApp::implementation { /// /// Initializes the singleton application object. This is the first line of /// authored code executed, and as such is the logical equivalent of main() or /// WinMain(). /// -App::App() { +App::App() noexcept { + MainComponentName(L"SampleApp"); + JavaScriptMainModuleName(L"index.windows"); + + PackageProviders().Append( + make()); // Includes all modules in this project + PackageProviders().Append(winrt::SampleLibraryCPP::ReactPackageProvider()); + PackageProviders().Append(winrt::SampleLibraryCS::ReactPackageProvider()); + InitializeComponent(); // This works around a cpp/winrt bug with composable/aggregable types tracked @@ -28,3 +31,5 @@ App::App() { AddRef(); m_inner.as<::IUnknown>()->Release(); } + +} // namespace winrt::SampleApp::implementation diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.h b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.h index 0d76a712540..9caf8d1499c 100644 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.h +++ b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/App.h @@ -1,25 +1,13 @@ -#pragma once // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#pragma once #include "App.xaml.g.h" -#include "MainReactNativeHost.h" - namespace winrt::SampleApp::implementation { -struct App : AppT { - App(); - winrt::Microsoft::ReactNative::ReactNativeHost HostCore() { - if (m_host == nullptr) { - m_host = - winrt::make(); - } - - return m_host; - } - - private: - winrt::SampleApp::MainReactNativeHost m_host{}; +struct App : AppT { + App() noexcept; }; + } // namespace winrt::SampleApp::implementation diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.cpp b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.cpp deleted file mode 100644 index 0a335ba722a..00000000000 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#include "pch.h" -#include "MainReactNativeHost.h" -#include "MainReactNativeHost.g.cpp" - -using namespace winrt; -using namespace Windows::UI::Xaml; - -namespace winrt::SampleApp::implementation {} diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.h b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.h deleted file mode 100644 index 63b5db16e1c..00000000000 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -#include "MainReactNativeHost.g.h" - -#include "ReactPackageProvider.h" - -#include "winrt/Microsoft.ReactNative.Bridge.h" - -#include "winrt/SampleLibraryCPP.h" -#include "winrt/SampleLibraryCS.h" - -using namespace winrt; -using namespace Windows::Foundation::Collections; -using namespace Microsoft::ReactNative; -using namespace Microsoft::ReactNative::Bridge; -using namespace SampleLibraryCPP; - -namespace winrt::SampleApp::implementation { - -struct MainReactNativeHost : MainReactNativeHostT { - MainReactNativeHost() { - auto instanceSettings = InstanceSettings(); - instanceSettings.UseWebDebugger( - false); // Disabled temporarily because of issue #2877 - instanceSettings.UseLiveReload( - true); // true by default in debug builds already - instanceSettings.UseJsi(true); - }; - - hstring MainComponentName() { - return L"SampleApp"; - }; - - hstring JavaScriptMainModuleName() { - return L"index.windows"; - }; - - bool UseDeveloperSupport() { - return true; - }; - - IVectorView - PackageProviders() { - auto packages = single_threaded_vector< - Microsoft::ReactNative::Bridge::IReactPackageProvider>( - {make(), - winrt::SampleLibraryCPP::ReactPackageProvider(), - winrt::SampleLibraryCS::ReactPackageProvider()}); - return packages.GetView(); - }; -}; -} // namespace winrt::SampleApp::implementation - -namespace winrt::SampleApp::factory_implementation { -struct MainReactNativeHost : MainReactNativeHostT< - MainReactNativeHost, - implementation::MainReactNativeHost> {}; -} // namespace winrt::SampleApp::factory_implementation diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.idl b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.idl deleted file mode 100644 index f41780c5c6c..00000000000 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/MainReactNativeHost.idl +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -namespace SampleApp -{ - [default_interface] - runtimeclass MainReactNativeHost : Microsoft.ReactNative.ReactNativeHost - { - MainReactNativeHost(); - } -} diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj index 0560f05df7b..02d97317cef 100644 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj +++ b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj @@ -106,10 +106,6 @@ - - MainReactNativeHost.idl - Code - @@ -137,10 +133,6 @@ - - MainReactNativeHost.idl - Code - Create @@ -154,9 +146,6 @@ App.xaml - - Designer - diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj.filters b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj.filters index 8338b16d1b6..d8c41a60df1 100644 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj.filters +++ b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCPP/SampleApp.vcxproj.filters @@ -5,7 +5,6 @@ - diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/App.xaml.cs b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/App.xaml.cs index c1d42eb98a0..c5a1787d81f 100644 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/App.xaml.cs +++ b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/App.xaml.cs @@ -5,24 +5,27 @@ namespace SampleApp { - /// - /// Provides application-specific behavior to supplement the default - /// Application class. - /// - sealed partial class App : ReactApplication - { - private MainReactNativeHost _host = new MainReactNativeHost(); - /// - /// Initializes the singleton application object. This is the first line - /// of authored code executed, and as such is the logical equivalent of - /// main() or WinMain(). + /// Provides application-specific behavior to supplement the default + /// Application class. /// - public App() + sealed partial class App : ReactApplication { - this.InitializeComponent(); - } + /// + /// Initializes the singleton application object. This is the first line + /// of authored code executed, and as such is the logical equivalent of + /// main() or WinMain(). + /// + public App() + { + MainComponentName = "SampleApp"; + JavaScriptMainModuleName = "index.windows"; - protected override ReactNativeHost HostCore => _host; - } + PackageProviders.Add(new Microsoft.ReactNative.Managed.LocalPackageProvider()); // Includes any modules in this project + PackageProviders.Add(new SampleLibraryCS.ReactPackageProvider()); + PackageProviders.Add(new SampleLibraryCPP.ReactPackageProvider()); + + InitializeComponent(); + } + } } diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/MainReactNativeHost.cs b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/MainReactNativeHost.cs deleted file mode 100644 index 5df964dcf65..00000000000 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/MainReactNativeHost.cs +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System.Collections.Generic; - -using Microsoft.ReactNative; -using Microsoft.ReactNative.Bridge; - -namespace SampleApp -{ - sealed class MainReactNativeHost : ReactNativeHost - { - public MainReactNativeHost() - { - this.InstanceSettings.UseWebDebugger = false; // Disabled temporarily because of issue #2877 - this.InstanceSettings.UseLiveReload = true; // true by default in debug builds already - this.InstanceSettings.UseJsi = true; - -#if DEBUG - this.InstanceSettings.EnableDeveloperMenu = true; -#else - this.InstanceSettings.EnableDeveloperMenu = false; -#endif - - } - - protected override string MainComponentName => "SampleApp"; - protected override string JavaScriptMainModuleName => "index.windows"; - - protected override IReadOnlyList PackageProviders - { - get - { - return new IReactPackageProvider[] { - new Microsoft.ReactNative.Managed.LocalPackageProvider(), // Includes any modules in this project - new SampleLibraryCS.ReactPackageProvider(), - new SampleLibraryCPP.ReactPackageProvider(), - }; - } - } - } -} diff --git a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/SampleAppCS.csproj b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/SampleAppCS.csproj index adeb72deae6..5d000363477 100644 --- a/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/SampleAppCS.csproj +++ b/packages/microsoft-reactnative-sampleapps/windows/SampleAppCS/SampleAppCS.csproj @@ -120,7 +120,6 @@ App.xaml - diff --git a/vnext/Microsoft.ReactNative/ReactApplication.cpp b/vnext/Microsoft.ReactNative/ReactApplication.cpp index f3acc65b133..1b9d08bbdb6 100644 --- a/vnext/Microsoft.ReactNative/ReactApplication.cpp +++ b/vnext/Microsoft.ReactNative/ReactApplication.cpp @@ -3,11 +3,12 @@ #include "pch.h" #include "ReactApplication.h" -#if __has_include("ReactApplication.g.cpp") #include "ReactApplication.g.cpp" -#endif #include "ReactNativeHost.h" +#include +#include + using namespace winrt; using namespace Microsoft::ReactNative; using namespace Windows::ApplicationModel; @@ -19,8 +20,8 @@ using namespace Windows::UI::Xaml::Controls; using namespace Windows::UI::Xaml::Navigation; namespace winrt::Microsoft::ReactNative::implementation { -ReactApplication::ReactApplication() - : m_delegate(CreateReactApplicationDelegate()) { + +ReactApplication::ReactApplication() noexcept { Suspending({this, &ReactApplication::OnSuspending}); #if defined _DEBUG && \ @@ -35,17 +36,39 @@ ReactApplication::ReactApplication() #endif } -Microsoft::ReactNative::ReactNativeHost ReactApplication::Host() { - if (m_host == nullptr) { - m_host = overridable().HostCore(); +Microsoft::ReactNative::ReactInstanceSettings +ReactApplication::InstanceSettings() noexcept { + if (!m_instanceSettings) { + m_instanceSettings = make(); + m_instanceSettings.UseWebDebugger(false); + m_instanceSettings.UseLiveReload(true); + m_instanceSettings.UseJsi(true); + m_instanceSettings.EnableDeveloperMenu(REACT_DEFAULT_ENABLE_DEVELOPER_MENU); } - return m_host; + return m_instanceSettings; +} + +IVector ReactApplication::PackageProviders() noexcept { + if (!m_packageProviders) { + m_packageProviders = single_threaded_vector(); + } + + return m_packageProviders; } -Microsoft::ReactNative::ReactNativeHost ReactApplication::HostCore() { - throw hresult_not_implemented( - L"You must implement ReactApplication.HostCore."); +Microsoft::ReactNative::ReactNativeHost ReactApplication::Host() noexcept { + if (!m_host) { + m_host = make(); + m_host.InstanceSettings(InstanceSettings()); + m_host.PackageProviders(PackageProviders()); + m_host.MainComponentName(MainComponentName()); + m_host.UseDeveloperSupport(UseDeveloperSupport()); + m_host.JavaScriptMainModuleName(JavaScriptMainModuleName()); + m_host.JavaScriptBundleFile(JavaScriptBundleFile()); + } + + return m_host; } void ReactApplication::OnLaunched(LaunchActivatedEventArgs const &e) { @@ -61,6 +84,10 @@ void ReactApplication::OnLaunched(LaunchActivatedEventArgs const &e) { /// /// Details about the launch request and process. void ReactApplication::OnCreate(LaunchActivatedEventArgs const &e) { + if (!m_delegate) { + m_delegate = CreateReactApplicationDelegate(); + } + #if defined _DEBUG if (IsDebuggerPresent()) { this->DebugSettings().EnableFrameRateCounter(TRUE); @@ -145,4 +172,10 @@ void ReactApplication::OnNavigationFailed( throw hresult_error( E_FAIL, hstring(L"Failed to load Page ") + e.SourcePageType().Name); } + +ReactApplicationDelegate __stdcall ReactApplication:: + CreateReactApplicationDelegate() { + return winrt::Microsoft::ReactNative::ReactApplicationDelegate(*this); +} + } // namespace winrt::Microsoft::ReactNative::implementation diff --git a/vnext/Microsoft.ReactNative/ReactApplication.h b/vnext/Microsoft.ReactNative/ReactApplication.h index 056340e58d3..13541ec74cb 100644 --- a/vnext/Microsoft.ReactNative/ReactApplication.h +++ b/vnext/Microsoft.ReactNative/ReactApplication.h @@ -1,48 +1,123 @@ -#pragma once // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#pragma once #include "ReactApplication.g.h" #include "ReactNativeHost.h" -#include -#include - using namespace winrt; using namespace Windows::ApplicationModel; using namespace Windows::UI::Xaml::Navigation; +using namespace Microsoft::ReactNative; +using namespace Microsoft::ReactNative::Bridge; +using namespace Windows::Foundation::Collections; namespace winrt::Microsoft::ReactNative::implementation { + struct ReactApplication : ReactApplicationT { - ReactApplication(); + ReactApplication() noexcept; - Microsoft::ReactNative::ReactNativeHost Host(); + Microsoft::ReactNative::ReactInstanceSettings InstanceSettings() noexcept; + void InstanceSettings( + Microsoft::ReactNative::ReactInstanceSettings const &value) noexcept; - virtual Microsoft::ReactNative::ReactNativeHost HostCore(); + IVector PackageProviders() noexcept; + void PackageProviders(IVector const &value) noexcept; - virtual void OnLaunched(Activation::LaunchActivatedEventArgs const &); + hstring MainComponentName() noexcept; + void MainComponentName(hstring const &value) noexcept; - virtual void OnSuspending(IInspectable const &, SuspendingEventArgs const &); + bool UseDeveloperSupport() noexcept; + void UseDeveloperSupport(bool value) noexcept; + + hstring JavaScriptMainModuleName() noexcept; + void JavaScriptMainModuleName(hstring const &value) noexcept; + + hstring JavaScriptBundleFile() noexcept; + void JavaScriptBundleFile(hstring const &value) noexcept; + Microsoft::ReactNative::ReactNativeHost Host() noexcept; + + virtual void OnLaunched(Activation::LaunchActivatedEventArgs const &); + virtual void OnSuspending(IInspectable const &, SuspendingEventArgs const &); virtual void OnNavigationFailed( IInspectable const &, NavigationFailedEventArgs const &); protected: - virtual winrt::Microsoft::ReactNative:: - ReactApplicationDelegate __stdcall CreateReactApplicationDelegate() { - return winrt::Microsoft::ReactNative::ReactApplicationDelegate(*this); - } + virtual ReactApplicationDelegate __stdcall CreateReactApplicationDelegate(); private: + Microsoft::ReactNative::ReactInstanceSettings m_instanceSettings{nullptr}; + IVector m_packageProviders{nullptr}; + hstring m_mainComponentName; + bool m_useDeveloperSupport{REACT_DEFAULT_USE_DEVELOPER_SUPPORT}; + hstring m_javaScriptMainModuleName; + hstring m_javaScriptBundleFile; Microsoft::ReactNative::ReactNativeHost m_host{nullptr}; - Microsoft::ReactNative::ReactApplicationDelegate m_delegate{nullptr}; + + ReactApplicationDelegate m_delegate{nullptr}; void OnCreate(Activation::LaunchActivatedEventArgs const &e); }; + } // namespace winrt::Microsoft::ReactNative::implementation namespace winrt::Microsoft::ReactNative::factory_implementation { + struct ReactApplication : ReactApplicationT {}; + } // namespace winrt::Microsoft::ReactNative::factory_implementation + +namespace winrt::Microsoft::ReactNative::implementation { + +//============================================================================= +// ReactApplication inline implementation +//============================================================================= + +inline void ReactApplication::InstanceSettings( + Microsoft::ReactNative::ReactInstanceSettings const &value) noexcept { + m_instanceSettings = value; +} + +inline void ReactApplication::PackageProviders( + IVector const &value) noexcept { + m_packageProviders = value; +} + +inline hstring ReactApplication::MainComponentName() noexcept { + return m_mainComponentName; +} + +inline void ReactApplication::MainComponentName(hstring const &value) noexcept { + m_mainComponentName = value; +} + +inline bool ReactApplication::UseDeveloperSupport() noexcept { + return m_useDeveloperSupport; +} + +inline void ReactApplication::UseDeveloperSupport(bool value) noexcept { + m_useDeveloperSupport = value; +} + +inline hstring ReactApplication::JavaScriptMainModuleName() noexcept { + return m_javaScriptMainModuleName; +} + +inline void ReactApplication::JavaScriptMainModuleName( + hstring const &value) noexcept { + m_javaScriptMainModuleName = value; +} + +inline hstring ReactApplication::JavaScriptBundleFile() noexcept { + return m_javaScriptBundleFile; +} + +inline void ReactApplication::JavaScriptBundleFile( + hstring const &value) noexcept { + m_javaScriptBundleFile = value; +} + +} // namespace winrt::Microsoft::ReactNative::implementation diff --git a/vnext/Microsoft.ReactNative/ReactApplication.idl b/vnext/Microsoft.ReactNative/ReactApplication.idl index 0a0f4a46ba9..a778c4f7ec7 100644 --- a/vnext/Microsoft.ReactNative/ReactApplication.idl +++ b/vnext/Microsoft.ReactNative/ReactApplication.idl @@ -6,17 +6,21 @@ import "ReactApplicationDelegate.idl"; namespace Microsoft.ReactNative { - interface IReactApplication - { - ReactNativeHost Host{ get; }; - }; + [webhosthidden] + [default_interface] + unsealed runtimeclass ReactApplication : Windows.UI.Xaml.Application + { + ReactApplication(); + + ReactInstanceSettings InstanceSettings { get; set; }; + + IVector PackageProviders { get; set; }; + + String MainComponentName { get; set; }; + Boolean UseDeveloperSupport { get; set; }; + String JavaScriptMainModuleName { get; set; }; + String JavaScriptBundleFile { get; set; }; - [webhosthidden] - [default_interface] - unsealed runtimeclass ReactApplication : Windows.UI.Xaml.Application, IReactApplication - { - ReactApplication(); - - overridable ReactNativeHost HostCore{ get; }; - }; -} + ReactNativeHost Host { get; }; + }; +} // namespace Microsoft.ReactNative diff --git a/vnext/Microsoft.ReactNative/ReactApplicationDelegate.h b/vnext/Microsoft.ReactNative/ReactApplicationDelegate.h index daf04bbafbf..cf67db74c6e 100644 --- a/vnext/Microsoft.ReactNative/ReactApplicationDelegate.h +++ b/vnext/Microsoft.ReactNative/ReactApplicationDelegate.h @@ -3,7 +3,6 @@ // Licensed under the MIT License. #include "ReactApplicationDelegate.g.h" -#include "ReactApplication.h" using namespace winrt; using namespace Windows::UI::Xaml; diff --git a/vnext/Microsoft.ReactNative/ReactNativeHost.cpp b/vnext/Microsoft.ReactNative/ReactNativeHost.cpp index 0254db326a9..03e5028b191 100644 --- a/vnext/Microsoft.ReactNative/ReactNativeHost.cpp +++ b/vnext/Microsoft.ReactNative/ReactNativeHost.cpp @@ -20,12 +20,41 @@ using namespace Windows::UI::Xaml; using namespace Windows::UI::Xaml::Controls; namespace winrt::Microsoft::ReactNative::implementation { -ReactNativeHost::ReactNativeHost() { + +ReactNativeHost::ReactNativeHost() noexcept { Init(); } +void ReactNativeHost::Init() noexcept { +#if _DEBUG + facebook::react::InitializeLogging( + [](facebook::react::RCTLogLevel /*logLevel*/, const char *message) { + std::string str = std::string("ReactNative:") + message; + OutputDebugStringA(str.c_str()); + }); +#endif +} + Microsoft::ReactNative::ReactInstanceManager -ReactNativeHost::ReactInstanceManager() { +ReactNativeHost::CreateReactInstanceManager() noexcept { + auto builder = ReactInstanceManagerBuilder(); + builder.InstanceSettings(InstanceSettings()); + builder.UseDeveloperSupport(UseDeveloperSupport()); + builder.InitialLifecycleState(LifecycleState::BeforeCreate); + builder.JavaScriptBundleFile(JavaScriptBundleFile()); + builder.JavaScriptMainModuleName(JavaScriptMainModuleName()); + builder.PackageProviders(PackageProviders().GetView()); + + return builder.Build(); +} + +std::shared_ptr ReactNativeHost::CreateRootView() noexcept { + auto rootView = std::make_shared(); + return rootView; +} + +Microsoft::ReactNative::ReactInstanceManager +ReactNativeHost::ReactInstanceManager() noexcept { if (m_reactInstanceManager == nullptr) { m_reactInstanceManager = CreateReactInstanceManager(); } @@ -33,9 +62,11 @@ ReactNativeHost::ReactInstanceManager() { return m_reactInstanceManager; } -UIElement ReactNativeHost::GetOrCreateRootView(IInspectable initialProps) { - if (m_reactRootView != nullptr) +UIElement ReactNativeHost::GetOrCreateRootView( + IInspectable initialProps) noexcept { + if (m_reactRootView != nullptr) { return *m_reactRootView; + } folly::dynamic props = Microsoft::ReactNative::Bridge::ConvertToDynamic(initialProps); @@ -45,96 +76,56 @@ UIElement ReactNativeHost::GetOrCreateRootView(IInspectable initialProps) { m_reactRootView->OnCreate(*this); m_reactRootView->StartReactApplicationAsync( - ReactInstanceManager(), get_MainComponentName(), props); + ReactInstanceManager(), MainComponentName(), props); return *m_reactRootView; } -void ReactNativeHost::OnSuspend() { +auto ReactNativeHost::InstanceSettings() noexcept + -> Microsoft::ReactNative::ReactInstanceSettings { + if (!m_instanceSettings) { + m_instanceSettings = make(); + m_instanceSettings.UseWebDebugger(false); + m_instanceSettings.UseLiveReload(true); + m_instanceSettings.UseJsi(true); + m_instanceSettings.EnableDeveloperMenu(REACT_DEFAULT_ENABLE_DEVELOPER_MENU); + } + + return m_instanceSettings; +} + +auto ReactNativeHost::PackageProviders() noexcept + -> IVector { + if (!m_packageProviders) { + m_packageProviders = single_threaded_vector(); + } + + return m_packageProviders; +} + +void ReactNativeHost::OnSuspend() noexcept { if (HasInstance()) { ReactInstanceManager().OnSuspend(); } } -void ReactNativeHost::OnEnteredBackground() { +void ReactNativeHost::OnEnteredBackground() noexcept { if (HasInstance()) { ReactInstanceManager().OnEnteredBackground(); } } -void ReactNativeHost::OnLeavingBackground() { +void ReactNativeHost::OnLeavingBackground() noexcept { if (HasInstance()) { ReactInstanceManager().OnLeavingBackground(); } } void ReactNativeHost::OnResume( - Microsoft::ReactNative::OnResumeAction const &action) { + Microsoft::ReactNative::OnResumeAction const &action) noexcept { if (HasInstance()) { ReactInstanceManager().OnResume(action); } } -hstring ReactNativeHost::MainComponentName() { - throw hresult_not_implemented( - L"Must implement ReactNativeHost.MainComponentName."); -} - -hstring ReactNativeHost::JavaScriptMainModuleName() { - throw hresult_not_implemented( - L"Must implement ReactNativeHost.JavaScriptMainModuleName."); -} - -hstring ReactNativeHost::JavaScriptBundleFile() { - return {}; -} - -bool ReactNativeHost::UseDeveloperSupport() { -#if _DEBUG - return true; -#else - return false; -#endif -} - -auto ReactNativeHost::PackageProviders() -> IVectorView { - return single_threaded_vector().GetView(); -} - -auto ReactNativeHost::InstanceSettings() - -> Microsoft::ReactNative::ReactInstanceSettings { - // Return the default - if (m_instanceSettings == nullptr) - m_instanceSettings = make(); - - return m_instanceSettings; -} - -void ReactNativeHost::Init() { -#if _DEBUG - facebook::react::InitializeLogging( - [](facebook::react::RCTLogLevel /*logLevel*/, const char *message) { - std::string str = std::string("ReactNative:") + message; - OutputDebugStringA(str.c_str()); - }); -#endif -} - -Microsoft::ReactNative::ReactInstanceManager -ReactNativeHost::CreateReactInstanceManager() { - auto builder = ReactInstanceManagerBuilder(); - builder.InstanceSettings(InstanceSettings()); - builder.UseDeveloperSupport(UseDeveloperSupport()); - builder.InitialLifecycleState(LifecycleState::BeforeCreate); - builder.JavaScriptBundleFile(get_JavaScriptBundleFile()); - builder.JavaScriptMainModuleName(get_JavaScriptMainModuleName()); - builder.PackageProviders(get_PackageProviders()); - - return builder.Build(); -} - -std::shared_ptr ReactNativeHost::CreateRootView() { - auto rootView = std::make_shared(); - return rootView; -} } // namespace winrt::Microsoft::ReactNative::implementation diff --git a/vnext/Microsoft.ReactNative/ReactNativeHost.h b/vnext/Microsoft.ReactNative/ReactNativeHost.h index e11aa7fbdf4..a0d18ba5453 100644 --- a/vnext/Microsoft.ReactNative/ReactNativeHost.h +++ b/vnext/Microsoft.ReactNative/ReactNativeHost.h @@ -1,6 +1,6 @@ -#pragma once // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#pragma once #include "ReactNativeHost.g.h" @@ -17,66 +17,129 @@ using namespace Microsoft::ReactNative; using namespace Microsoft::ReactNative::Bridge; using namespace Windows::Foundation::Collections; +#ifndef REACT_DEFAULT_USE_DEVELOPER_SUPPORT +#if _DEBUG +#define REACT_DEFAULT_USE_DEVELOPER_SUPPORT true +#else +#define REACT_DEFAULT_USE_DEVELOPER_SUPPORT false +#endif // _DEBUG +#endif // REACT_DEFAULT_USE_DEVELOPER_SUPPORT + +#ifndef REACT_DEFAULT_ENABLE_DEVELOPER_MENU +#if _DEBUG +#define REACT_DEFAULT_ENABLE_DEVELOPER_MENU true +#else +#define REACT_DEFAULT_ENABLE_DEVELOPER_MENU false +#endif // _DEBUG +#endif // REACT_DEFAULT_ENABLE_DEVELOPER_MENU + namespace winrt::Microsoft::ReactNative::implementation { struct ReactNativeHost : ReactNativeHostT { - ReactNativeHost(); - - UIElement GetOrCreateRootView(IInspectable initialProps); - - Microsoft::ReactNative::ReactInstanceManager ReactInstanceManager(); - Microsoft::ReactNative::ReactInstanceSettings InstanceSettings(); - - void OnSuspend(); - void OnEnteredBackground(); - void OnLeavingBackground(); - void OnResume(OnResumeAction const &action); - - virtual hstring MainComponentName(); - virtual hstring JavaScriptMainModuleName(); - virtual hstring JavaScriptBundleFile(); - virtual bool UseDeveloperSupport(); - virtual IVectorView PackageProviders(); - - bool HasInstance() { - return m_reactInstanceManager != nullptr; - } - - hstring get_MainComponentName() { - return overridable().MainComponentName(); - } + ReactNativeHost() noexcept; + + UIElement GetOrCreateRootView(IInspectable initialProps) noexcept; + + Microsoft::ReactNative::ReactInstanceManager ReactInstanceManager() noexcept; + Microsoft::ReactNative::ReactInstanceSettings InstanceSettings() noexcept; + void InstanceSettings( + Microsoft::ReactNative::ReactInstanceSettings const &value) noexcept; + bool HasInstance() noexcept; + IVector PackageProviders() noexcept; + void PackageProviders(IVector const &value) noexcept; + hstring MainComponentName() noexcept; + void MainComponentName(hstring const &value) noexcept; + bool UseDeveloperSupport() noexcept; + void UseDeveloperSupport(bool value) noexcept; + hstring JavaScriptMainModuleName() noexcept; + void JavaScriptMainModuleName(hstring const &value) noexcept; + hstring JavaScriptBundleFile() noexcept; + void JavaScriptBundleFile(hstring const &value) noexcept; + + void OnSuspend() noexcept; + void OnEnteredBackground() noexcept; + void OnLeavingBackground() noexcept; + void OnResume(OnResumeAction const &action) noexcept; - hstring get_JavaScriptMainModuleName() { - return overridable().JavaScriptMainModuleName(); - } - - hstring get_JavaScriptBundleFile() { - return overridable().JavaScriptBundleFile(); - } - - bool get_UseDeveloperSupport() { - return overridable().UseDeveloperSupport(); - } - - IVectorView get_PackageProviders() { - return overridable().PackageProviders(); - } + private: + void Init() noexcept; + Microsoft::ReactNative::ReactInstanceManager + CreateReactInstanceManager() noexcept; + std::shared_ptr CreateRootView() noexcept; private: Microsoft::ReactNative::ReactInstanceSettings m_instanceSettings; Microsoft::ReactNative::ReactInstanceManager m_reactInstanceManager{nullptr}; - hstring m_mainComponentName{}; - hstring m_javascriptMainModuleName{}; + IVector m_packageProviders{nullptr}; + hstring m_mainComponentName; + bool m_useDeveloperSupport{REACT_DEFAULT_USE_DEVELOPER_SUPPORT}; + hstring m_javaScriptMainModuleName; + hstring m_javaScriptBundleFile; std::shared_ptr m_reactRootView{nullptr}; - - void Init(); - Microsoft::ReactNative::ReactInstanceManager CreateReactInstanceManager(); - std::shared_ptr CreateRootView(); }; + } // namespace winrt::Microsoft::ReactNative::implementation namespace winrt::Microsoft::ReactNative::factory_implementation { + struct ReactNativeHost : ReactNativeHostT {}; + } // namespace winrt::Microsoft::ReactNative::factory_implementation + +namespace winrt::Microsoft::ReactNative::implementation { + +//============================================================================= +// ReactNativeHost inline implementation +//============================================================================= + +inline void ReactNativeHost::InstanceSettings( + Microsoft::ReactNative::ReactInstanceSettings const &value) noexcept { + m_instanceSettings = value; +} + +inline bool ReactNativeHost::HasInstance() noexcept { + return m_reactInstanceManager != nullptr; +} + +inline void ReactNativeHost::PackageProviders( + IVector const &value) noexcept { + m_packageProviders = value; +} + +inline hstring ReactNativeHost::MainComponentName() noexcept { + return m_mainComponentName; +} + +inline void ReactNativeHost::MainComponentName(hstring const &value) noexcept { + m_mainComponentName = value; +} + +inline bool ReactNativeHost::UseDeveloperSupport() noexcept { + return m_useDeveloperSupport; +} + +inline void ReactNativeHost::UseDeveloperSupport(bool value) noexcept { + m_useDeveloperSupport = value; +} + +inline hstring ReactNativeHost::JavaScriptMainModuleName() noexcept { + return m_javaScriptMainModuleName; +} + +inline void ReactNativeHost::JavaScriptMainModuleName( + hstring const &value) noexcept { + m_javaScriptMainModuleName = value; +} + +inline hstring ReactNativeHost::JavaScriptBundleFile() noexcept { + return m_javaScriptBundleFile; +} + +inline void ReactNativeHost::JavaScriptBundleFile( + hstring const &value) noexcept { + m_javaScriptBundleFile = value; +} + +} // namespace winrt::Microsoft::ReactNative::implementation diff --git a/vnext/Microsoft.ReactNative/ReactNativeHost.idl b/vnext/Microsoft.ReactNative/ReactNativeHost.idl index 52f129068d7..e6edc632fb5 100644 --- a/vnext/Microsoft.ReactNative/ReactNativeHost.idl +++ b/vnext/Microsoft.ReactNative/ReactNativeHost.idl @@ -13,21 +13,21 @@ namespace Microsoft.ReactNative [webhosthidden] [default_interface] - unsealed runtimeclass ReactNativeHost + runtimeclass ReactNativeHost { ReactNativeHost(); ReactInstanceManager ReactInstanceManager{ get; }; - ReactInstanceSettings InstanceSettings { get; }; + ReactInstanceSettings InstanceSettings{ get; set; }; Boolean HasInstance{ get; }; - overridable IVectorView PackageProviders { get; }; + IVector PackageProviders { get; set; }; - overridable String MainComponentName{ get; }; - overridable Boolean UseDeveloperSupport{ get; }; - overridable String JavaScriptMainModuleName{ get; }; - overridable String JavaScriptBundleFile{ get; }; + String MainComponentName{ get; set; }; + Boolean UseDeveloperSupport{ get; set; }; + String JavaScriptMainModuleName{ get; set; }; + String JavaScriptBundleFile{ get; set; }; Windows.UI.Xaml.UIElement GetOrCreateRootView(IInspectable initialProps); diff --git a/vnext/Microsoft.ReactNative/ReactRootView.cpp b/vnext/Microsoft.ReactNative/ReactRootView.cpp index 620373911f2..12ba34f3232 100644 --- a/vnext/Microsoft.ReactNative/ReactRootView.cpp +++ b/vnext/Microsoft.ReactNative/ReactRootView.cpp @@ -2,9 +2,8 @@ // Licensed under the MIT License. #include "pch.h" #include "ReactRootView.h" -#if __has_include("ReactRootView.g.cpp") #include "ReactRootView.g.cpp" -#endif +#include using namespace winrt; using namespace Windows::UI::Core; diff --git a/vnext/Microsoft.ReactNative/ReactRootView.h b/vnext/Microsoft.ReactNative/ReactRootView.h index 5a227b10351..544bde7571a 100644 --- a/vnext/Microsoft.ReactNative/ReactRootView.h +++ b/vnext/Microsoft.ReactNative/ReactRootView.h @@ -4,7 +4,6 @@ #include "ReactRootView.g.h" -#include "ReactApplication.h" #include "ReactInstanceManager.h" #include "ReactNativeHost.h" #include "ViewManagerProvider.h" diff --git a/yarn.lock b/yarn.lock index cb2a1402f4e..237d910031b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9831,10 +9831,10 @@ react-is@^16.8.1, react-is@^16.8.4, react-is@^16.8.6: resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.9.0.tgz#21ca9561399aad0ff1a7701c01683e8ca981edcb" integrity sha512-tJBzzzIgnnRfEm046qRcURvwQnZVXmuCbscxUO5RWrGTXpon2d4c8mI0D8WE6ydVIm29JiLB6+RslkIvym9Rjw== -"react-native@https://github.com/microsoft/react-native/archive/v0.60.0-microsoft.8.tar.gz": - version "0.60.0-microsoft.8" - uid a2bd4cdb70c85ada46cea8fc3294c7e7bca5139c - resolved "https://github.com/microsoft/react-native/archive/v0.60.0-microsoft.8.tar.gz#a2bd4cdb70c85ada46cea8fc3294c7e7bca5139c" +"react-native@https://github.com/microsoft/react-native/archive/v0.60.0-microsoft.9.tar.gz": + version "0.60.0-microsoft.9" + uid e3882023fd841d13833dce99dcf88eedd63a9f79 + resolved "https://github.com/microsoft/react-native/archive/v0.60.0-microsoft.9.tar.gz#e3882023fd841d13833dce99dcf88eedd63a9f79" dependencies: "@babel/core" "^7.4.0" "@babel/generator" "^7.4.0" From dbb4fa1b7c36e8f7b240777094ac6490f939574a Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Wed, 23 Oct 2019 09:22:00 -0700 Subject: [PATCH 2/2] Change files --- ...ive-windows-2019-10-23-09-22-00-ReactApplication.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 change/react-native-windows-2019-10-23-09-22-00-ReactApplication.json diff --git a/change/react-native-windows-2019-10-23-09-22-00-ReactApplication.json b/change/react-native-windows-2019-10-23-09-22-00-ReactApplication.json new file mode 100644 index 00000000000..7a8208be482 --- /dev/null +++ b/change/react-native-windows-2019-10-23-09-22-00-ReactApplication.json @@ -0,0 +1,9 @@ +{ + "type": "none", + "comment": "Removed MainReactNativeHost from samples and use ReactApplication properties instead.", + "packageName": "react-native-windows", + "email": "vmorozov@microsoft.com", + "commit": "4562d6f570094dd439e7d5be225ffc11141c14c0", + "date": "2019-10-23T16:21:59.944Z", + "file": "c:\\git\\vmoroz\\react-native-windows\\change\\react-native-windows-2019-10-23-09-22-00-ReactApplication.json" +} \ No newline at end of file