Skip to content

Commit e1b07c7

Browse files
authored
Cleanup ReactNativeAppBuilder and ReactNativeWin32App (#13983)
## Description This PR simplifies and scopes down the API for `ReactNativeAppBuilder` and `ReactNativeWin32App`. ### Type of Change - Bug fix (non-breaking change which fixes an issue) ### Why `ReactNativeAppBuilder`'s API surface made it too easy to call incorrectly and not realize it. Resolves #13946 ### What There are a variety of changes to the API surface: * `ReactInstanceSettingsBuilder` deleted: not only are there are simply too many APIs that would need to be exposed to be useful, the very act of creating and replacing the app's `ReactNativeHost`'s `ReactInstanceSettings` with a new one is what caused the bug in #13946 in the first place * `ReactNativeAppBuilder` now only exposes APIs to specify the intial, non-ReactNative, WinAppSDK types, (i.e. `DispatcherQueueController`, `Compositor`, and `AppWindow`), objects the app developer may already have created for their existing app, and otherwise is only responsible for building a `ReactNativeWin32App` with those types properly pre-made * `ReactNativeWin32App::Start()` is now more responsible for the stitching together all of the relevant types to make a working Win32 fabric app * All WinRT APIs without an immediate use-case have been commented out until we are sure they are necessary and that it is safe to expose them * The template has been updated to follow the pattern of: * Use `ReactNativeAppBuilder` to get a `ReactNativeWin32` app with the base WinAppSDK types ready * Get and modify the types as necessary from the created app object (like the `ReactInstanceSettings` and the `AppWindow`) * Call `app.Start()` ## Screenshots N/A ## Testing Verified new apps and example apps in libraryes build and run properly. ## Changelog Should this change be included in the release notes: _yes_ Cleanup ReactNativeAppBuilder and ReactNativeWin32App
1 parent 1346dc5 commit e1b07c7

File tree

11 files changed

+279
-370
lines changed

11 files changed

+279
-370
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Cleanup ReactNativeAppBuilder and ReactNativeWin32App",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/sample-app-fabric/windows/SampleAppFabric/SampleAppFabric.cpp

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "NativeModules.h"
1010

11+
// A PackageProvider containing any turbo modules you define within this app project
1112
struct CompReactPackageProvider
1213
: winrt::implements<CompReactPackageProvider, winrt::Microsoft::ReactNative::IReactPackageProvider> {
1314
public: // IReactPackageProvider
@@ -16,65 +17,66 @@ struct CompReactPackageProvider
1617
}
1718
};
1819

19-
// Global Variables:
20-
constexpr PCWSTR windowTitle = L"sample_app_fabric";
21-
constexpr PCWSTR mainComponentName = L"sample_app_fabric";
22-
20+
// The entry point of the Win32 application
2321
_Use_decl_annotations_ int CALLBACK WinMain(HINSTANCE instance, HINSTANCE, PSTR /* commandLine */, int showCmd) {
24-
// Initialize WinRT.
22+
// Initialize WinRT
2523
winrt::init_apartment(winrt::apartment_type::single_threaded);
2624

2725
// Enable per monitor DPI scaling
2826
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
2927

30-
// Create a top-level window.
31-
auto window = winrt::Microsoft::UI::Windowing::AppWindow::Create();
32-
window.Title(windowTitle);
33-
window.Resize({1000, 1000});
34-
window.Show();
35-
28+
// Find the path hosting the app exe file
3629
WCHAR appDirectory[MAX_PATH];
3730
GetModuleFileNameW(NULL, appDirectory, MAX_PATH);
3831
PathCchRemoveFileSpec(appDirectory, MAX_PATH);
3932

40-
// DebugBundlePath is used when loading from a bundle server such as metro. If this parameter
41-
// is not provided fallback to a combination of JavaScriptBundleFile and BundleRootPath
42-
auto reactInstanceSettingsBuilder {
43-
winrt::Microsoft::ReactNative::ReactInstanceSettingsBuilder()
44-
.DebugBundlePath(L"index")
45-
.JavaScriptBundleFile(L"index.windows")
46-
.BundleRootPath(std::wstring(L"file://").append(appDirectory).append(L"\\Bundle\\").c_str())
33+
// Create a ReactNativeWin32App with the ReactNativeAppBuilder
34+
auto reactNativeWin32App{winrt::Microsoft::ReactNative::ReactNativeAppBuilder().Build()};
35+
36+
// Configure the initial InstanceSettings for the app's ReactNativeHost
37+
auto settings{reactNativeWin32App.ReactNativeHost().InstanceSettings()};
38+
// Register any autolinked native modules
39+
RegisterAutolinkedNativeModulePackages(settings.PackageProviders());
40+
// Register any native modules defined within this app project
41+
settings.PackageProviders().Append(winrt::make<CompReactPackageProvider>());
42+
4743
#if BUNDLE
48-
.UseFastRefresh(false)
44+
// Load the JS bundle from a file (not Metro):
45+
// Set the path (on disk) where the .bundle file is located
46+
settings.BundleRootPath(std::wstring(L"file://").append(appDirectory).append(L"\\Bundle\\").c_str());
47+
// Set the name of the bundle file (without the .bundle extension)
48+
settings.JavaScriptBundleFile(L"index.windows");
49+
// Disable hot reload
50+
settings.UseFastRefresh(false);
4951
#else
50-
.UseFastRefresh(true)
52+
// Load the JS bundle from Metro
53+
settings.JavaScriptBundleFile(L"index");
54+
// Enable hot reload
55+
settings.UseFastRefresh(true);
5156
#endif
5257
#if _DEBUG
53-
.UseDirectDebugger(true)
54-
.UseDeveloperSupport(true)
58+
// For Debug builds
59+
// Enable Direct Debugging of JS
60+
settings.UseDirectDebugger(true);
61+
// Enable the Developer Menu
62+
settings.UseDeveloperSupport(true);
5563
#else
56-
.UseDirectDebugger(false)
57-
.UseDeveloperSupport(false)
64+
// For Release builds:
65+
// Disable Direct Debugging of JS
66+
settings.UseDirectDebugger(false);
67+
// Disable the Developer Menu
68+
settings.UseDeveloperSupport(false);
5869
#endif
59-
};
60-
61-
auto packageProviders{winrt::single_threaded_vector<winrt::Microsoft::ReactNative::IReactPackageProvider>()};
62-
63-
RegisterAutolinkedNativeModulePackages(packageProviders);
64-
packageProviders.Append(winrt::make<CompReactPackageProvider>());
65-
66-
winrt::Microsoft::ReactNative::ReactViewOptions viewOptions;
67-
viewOptions.ComponentName(mainComponentName);
6870

69-
// Initialize and Manage the ReactNativeHost
70-
auto reactNativeAppBuilder{winrt::Microsoft::ReactNative::ReactNativeAppBuilder()
71-
.AddPackageProviders(packageProviders)
72-
.SetReactInstanceSettings(reactInstanceSettingsBuilder.ReactInstanceSettings())
73-
.SetAppWindow(window)
74-
.SetReactViewOptions(viewOptions)};
71+
// Get the AppWindow so we can configure its initial title and size
72+
auto appWindow{reactNativeWin32App.AppWindow()};
73+
appWindow.Title(L"sample_app_fabric");
74+
appWindow.Resize({1000, 1000});
7575

76-
// Start the react-native instance by creating a javascript runtime and load the bundle.
77-
auto reactNativeWin32App{reactNativeAppBuilder.Build()};
76+
// Get the ReactViewOptions so we can set the initial RN component to load
77+
auto viewOptions{reactNativeWin32App.ReactViewOptions()};
78+
viewOptions.ComponentName(L"sample_app_fabric");
7879

80+
// Start the app
7981
reactNativeWin32App.Start();
8082
}

vnext/Microsoft.ReactNative/ReactInstanceSettingsBuilder.cpp

Lines changed: 0 additions & 59 deletions
This file was deleted.

vnext/Microsoft.ReactNative/ReactInstanceSettingsBuilder.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)