react-native-windows 0.74.23 → 0.74.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/Microsoft.ReactNative/CompositionComponentView.idl +2 -1
  2. package/Microsoft.ReactNative/Fabric/AbiComponentDescriptor.cpp +4 -1
  3. package/Microsoft.ReactNative/Fabric/AbiViewComponentDescriptor.cpp +1 -1
  4. package/Microsoft.ReactNative/Fabric/AbiViewProps.cpp +7 -0
  5. package/Microsoft.ReactNative/Fabric/AbiViewProps.h +2 -0
  6. package/Microsoft.ReactNative/Fabric/ComponentView.cpp +19 -4
  7. package/Microsoft.ReactNative/Fabric/ComponentView.h +8 -3
  8. package/Microsoft.ReactNative/Fabric/Composition/BorderPrimitive.cpp +926 -0
  9. package/Microsoft.ReactNative/Fabric/Composition/BorderPrimitive.h +76 -0
  10. package/Microsoft.ReactNative/Fabric/Composition/CompositionDynamicAutomationProvider.cpp +31 -13
  11. package/Microsoft.ReactNative/Fabric/Composition/CompositionEventHandler.cpp +27 -3
  12. package/Microsoft.ReactNative/Fabric/Composition/CompositionEventHandler.h +3 -1
  13. package/Microsoft.ReactNative/Fabric/Composition/CompositionRootAutomationProvider.cpp +8 -32
  14. package/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.cpp +193 -892
  15. package/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.h +17 -22
  16. package/Microsoft.ReactNative/Fabric/Composition/DebuggingOverlayComponentView.cpp +1 -1
  17. package/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp +130 -122
  18. package/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.h +14 -8
  19. package/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.cpp +34 -20
  20. package/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.h +5 -3
  21. package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.cpp +63 -2
  22. package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.h +12 -0
  23. package/Microsoft.ReactNative/Fabric/Composition/RootComponentView.cpp +51 -3
  24. package/Microsoft.ReactNative/Fabric/Composition/RootComponentView.h +3 -0
  25. package/Microsoft.ReactNative/Fabric/Composition/ScrollViewComponentView.cpp +8 -8
  26. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp +41 -5
  27. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.h +4 -0
  28. package/Microsoft.ReactNative/Fabric/Composition/Theme.cpp +9 -3
  29. package/Microsoft.ReactNative/Fabric/Composition/UiaHelpers.cpp +11 -0
  30. package/Microsoft.ReactNative/Fabric/Composition/UiaHelpers.h +2 -0
  31. package/Microsoft.ReactNative/IReactCompositionViewComponentBuilder.idl +1 -0
  32. package/Microsoft.ReactNative/IReactViewComponentBuilder.idl +1 -1
  33. package/Microsoft.ReactNative/ReactNativeAppBuilder.cpp +25 -129
  34. package/Microsoft.ReactNative/ReactNativeAppBuilder.h +5 -13
  35. package/Microsoft.ReactNative/ReactNativeAppBuilder.idl +13 -34
  36. package/Microsoft.ReactNative/ReactNativeIsland.idl +3 -2
  37. package/Microsoft.ReactNative/ReactNativeWin32App.cpp +129 -18
  38. package/Microsoft.ReactNative/ReactNativeWin32App.h +14 -5
  39. package/Microsoft.ReactNative/ViewProps.idl +2 -0
  40. package/Microsoft.ReactNative/packages.lock.json +55 -83
  41. package/Microsoft.ReactNative.Cxx/ComponentView.Experimental.interop.h +14 -0
  42. package/Microsoft.ReactNative.Managed/packages.lock.json +107 -107
  43. package/PropertySheets/CppAppConsumeCSharpModule.props +2 -0
  44. package/PropertySheets/External/Microsoft.ReactNative.CSharp.Dependencies.props +1 -1
  45. package/PropertySheets/Generated/PackageVersion.g.props +3 -3
  46. package/ReactCommon/packages.lock.json +2 -2
  47. package/Shared/Shared.vcxitems +3 -10
  48. package/Shared/Shared.vcxitems.filters +1 -0
  49. package/package.json +3 -3
  50. package/templates/cpp-app/windows/MyApp/MyApp.cpp +46 -130
  51. package/Microsoft.ReactNative/ReactInstanceSettingsBuilder.cpp +0 -59
  52. package/Microsoft.ReactNative/ReactInstanceSettingsBuilder.h +0 -23
@@ -1,43 +1,17 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
1
4
  #include "pch.h"
2
5
  #include "ReactNativeAppBuilder.h"
3
6
  #include "ReactNativeAppBuilder.g.cpp"
4
- #include "IReactDispatcher.h"
5
- #include "ReactNativeHost.h"
7
+
6
8
  #include "ReactNativeWin32App.h"
9
+
10
+ #include "winrt/Microsoft.ReactNative.h"
7
11
  #include "winrt/Microsoft.UI.Composition.h"
8
12
  #include "winrt/Microsoft.UI.Dispatching.h"
13
+ #include "winrt/Microsoft.UI.Interop.h"
9
14
  #include "winrt/Microsoft.UI.Windowing.h"
10
- #include "winrt/microsoft.UI.Interop.h"
11
-
12
- // Scaling factor for the window's content based on the DPI of the display where the window is located.
13
- float ScaleFactor(HWND hwnd) noexcept {
14
- return GetDpiForWindow(hwnd) / static_cast<float>(USER_DEFAULT_SCREEN_DPI);
15
- }
16
-
17
- void UpdateRootViewSizeToAppWindow(
18
- winrt::Microsoft::ReactNative::ReactNativeIsland const &rootView,
19
- winrt::Microsoft::UI::Windowing::AppWindow const &window) {
20
- auto hwnd = winrt::Microsoft::UI::GetWindowFromWindowId(window.Id());
21
- auto scaleFactor = ScaleFactor(hwnd);
22
- winrt::Windows::Foundation::Size size{
23
- window.ClientSize().Width / scaleFactor, window.ClientSize().Height / scaleFactor};
24
- // Do not relayout when minimized
25
- if (window.Presenter().as<winrt::Microsoft::UI::Windowing::OverlappedPresenter>().State() !=
26
- winrt::Microsoft::UI::Windowing::OverlappedPresenterState::Minimized) {
27
- winrt::Microsoft::ReactNative::LayoutConstraints constraints;
28
- constraints.LayoutDirection = winrt::Microsoft::ReactNative::LayoutDirection::Undefined;
29
- constraints.MaximumSize = constraints.MinimumSize = size;
30
- rootView.Arrange(constraints, {0, 0});
31
- }
32
- }
33
-
34
- namespace winrt::ReactNative {
35
- using namespace winrt::Microsoft::ReactNative;
36
- }
37
-
38
- namespace winrt::UI {
39
- using namespace winrt::Microsoft::UI;
40
- }
41
15
 
42
16
  namespace winrt::Microsoft::ReactNative::implementation {
43
17
  ReactNativeAppBuilder::ReactNativeAppBuilder() {
@@ -46,133 +20,55 @@ ReactNativeAppBuilder::ReactNativeAppBuilder() {
46
20
 
47
21
  ReactNativeAppBuilder::~ReactNativeAppBuilder() {}
48
22
 
49
- winrt::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::AddPackageProviders(
50
- winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::ReactNative::IReactPackageProvider> const
51
- &packageProviders) {
52
- for (auto const &provider : packageProviders) {
53
- m_reactNativeWin32App.ReactNativeHost().PackageProviders().Append(provider);
54
- }
55
-
56
- return *this;
57
- }
58
-
59
- winrt::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetReactInstanceSettings(
60
- winrt::Microsoft::ReactNative::ReactInstanceSettings const &settings) {
61
- m_reactNativeWin32App.ReactNativeHost().InstanceSettings(settings);
62
-
23
+ winrt::Microsoft::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetDispatcherQueueController(
24
+ winrt::Microsoft::UI::Dispatching::DispatcherQueueController const &dispatcherQueueController) {
25
+ m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->DispatcherQueueController(
26
+ dispatcherQueueController);
63
27
  return *this;
64
28
  }
65
29
 
66
- winrt::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetCompositor(
30
+ winrt::Microsoft::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetCompositor(
67
31
  winrt::Microsoft::UI::Composition::Compositor const &compositor) {
68
32
  m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->Compositor(compositor);
69
33
  return *this;
70
34
  }
71
35
 
72
- winrt::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetAppWindow(
36
+ winrt::Microsoft::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetAppWindow(
73
37
  winrt::Microsoft::UI::Windowing::AppWindow const &appWindow) {
74
38
  m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->AppWindow(appWindow);
75
39
 
76
40
  return *this;
77
41
  }
78
42
 
79
- winrt::Microsoft::ReactNative::ReactNativeAppBuilder ReactNativeAppBuilder::SetReactViewOptions(
80
- winrt::Microsoft::ReactNative::ReactViewOptions const &reactViewOptions) {
81
- m_reactViewOptions = reactViewOptions;
43
+ winrt::Microsoft::ReactNative::ReactNativeWin32App ReactNativeAppBuilder::Build() {
44
+ // Create the DispatcherQueueController if the app developer doesn't provide one
45
+ if (m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->DispatcherQueueController() == nullptr) {
46
+ assert(m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->Compositor() == nullptr);
82
47
 
83
- return *this;
84
- }
85
-
86
- winrt::ReactNative::ReactNativeWin32App ReactNativeAppBuilder::Build() {
87
- if (m_reactNativeWin32App.Compositor() == nullptr) {
88
- // Create a DispatcherQueue for this thread. This is needed for Composition, Content, and
89
- // Input APIs.
48
+ // Create a DispatcherQueue for this thread. This is needed for Composition, Content, and Input APIs.
90
49
  auto dispatcherQueueController =
91
50
  winrt::Microsoft::UI::Dispatching::DispatcherQueueController::CreateOnCurrentThread();
92
51
 
93
- m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->DispatchQueueController(
52
+ m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->DispatcherQueueController(
94
53
  dispatcherQueueController);
54
+ }
95
55
 
96
- // Create the compositor on behalf of the App Developer
56
+ // Create the Compositor if the app developer doesn't provide one
57
+ if (m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->Compositor() == nullptr) {
58
+ // Create the compositor on behalf of the App Developer.
97
59
  auto compositor = winrt::Microsoft::UI::Composition::Compositor();
98
60
  m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->Compositor(compositor);
99
61
  }
100
62
 
101
- // Create the AppWindow if the developer doesn't provide one
63
+ // Create the AppWindow if the app developer doesn't provide one
102
64
  if (m_reactNativeWin32App.AppWindow() == nullptr) {
103
65
  auto appWindow = winrt::Microsoft::UI::Windowing::AppWindow::Create();
104
- appWindow.Title(L"SampleApplication");
66
+ appWindow.Title(L"ReactNativeWin32App");
105
67
  appWindow.Resize({1000, 1000});
106
- appWindow.Show();
107
68
 
108
69
  m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->AppWindow(appWindow);
109
70
  }
110
71
 
111
- // Currently set the property to use current thread dispatcher as a default UI dispatcher.
112
- // TODO: Provision for setting dispatcher based on the thread dispatcherQueueController is created.
113
- m_reactNativeWin32App.ReactNativeHost().InstanceSettings().Properties().Set(
114
- ReactDispatcherHelper::UIDispatcherProperty(), ReactDispatcherHelper::UIThreadDispatcher());
115
-
116
- auto hwnd{winrt::UI::GetWindowFromWindowId(m_reactNativeWin32App.AppWindow().Id())};
117
-
118
- winrt::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
119
- m_reactNativeWin32App.ReactNativeHost().InstanceSettings().Properties(), reinterpret_cast<uint64_t>(hwnd));
120
-
121
- winrt::ReactNative::Composition::CompositionUIService::SetCompositor(
122
- m_reactNativeWin32App.ReactNativeHost().InstanceSettings(), m_reactNativeWin32App.Compositor());
123
-
124
- // Start the react-native instance, which will create a JavaScript runtime and load the applications bundle.
125
- m_reactNativeWin32App.ReactNativeHost().ReloadInstance();
126
-
127
- // Create a RootView which will present a react-native component
128
- auto reactNativeIsland = winrt::Microsoft::ReactNative::ReactNativeIsland(m_reactNativeWin32App.Compositor());
129
- reactNativeIsland.ReactViewHost(winrt::Microsoft::ReactNative::ReactCoreInjection::MakeViewHost(
130
- m_reactNativeWin32App.ReactNativeHost(), m_reactViewOptions));
131
-
132
- m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->ReactNativeIsland(
133
- std::move(reactNativeIsland));
134
-
135
- // Update the size of the RootView when the AppWindow changes size
136
- m_reactNativeWin32App.AppWindow().Changed(
137
- [wkRootView = winrt::make_weak(m_reactNativeWin32App.ReactNativeIsland())](
138
- winrt::Microsoft::UI::Windowing::AppWindow const &window,
139
- winrt::Microsoft::UI::Windowing::AppWindowChangedEventArgs const &args) {
140
- if (args.DidSizeChange() || args.DidVisibilityChange()) {
141
- if (auto rootView = wkRootView.get()) {
142
- UpdateRootViewSizeToAppWindow(rootView, window);
143
- }
144
- }
145
- });
146
-
147
- // Quit application when main window is closed
148
- m_reactNativeWin32App.AppWindow().Destroying([this](
149
- winrt::Microsoft::UI::Windowing::AppWindow const &window,
150
- winrt::Windows::Foundation::IInspectable const & /*args*/) {
151
- // Before we shutdown the application - unload the ReactNativeHost to give the javascript a chance to save any
152
- // state
153
- auto async = m_reactNativeWin32App.ReactNativeHost().UnloadInstance();
154
- async.Completed([this](auto asyncInfo, winrt::Windows::Foundation::AsyncStatus asyncStatus) {
155
- assert(asyncStatus == winrt::Windows::Foundation::AsyncStatus::Completed);
156
- m_reactNativeWin32App.ReactNativeHost().InstanceSettings().UIDispatcher().Post([]() { PostQuitMessage(0); });
157
- });
158
- });
159
-
160
- // DesktopChildSiteBridge create a ContentSite that can host the RootView ContentIsland
161
- auto desktopChildSiteBridge = winrt::Microsoft::UI::Content::DesktopChildSiteBridge::Create(
162
- m_reactNativeWin32App.Compositor(), m_reactNativeWin32App.AppWindow().Id());
163
-
164
- desktopChildSiteBridge.Connect(m_reactNativeWin32App.ReactNativeIsland().Island());
165
-
166
- desktopChildSiteBridge.ResizePolicy(winrt::Microsoft::UI::Content::ContentSizePolicy::ResizeContentToParentWindow);
167
-
168
- auto scaleFactor = ScaleFactor(hwnd);
169
- m_reactNativeWin32App.ReactNativeIsland().ScaleFactor(scaleFactor);
170
-
171
- UpdateRootViewSizeToAppWindow(reactNativeIsland, m_reactNativeWin32App.AppWindow());
172
-
173
- m_reactNativeWin32App.as<implementation::ReactNativeWin32App>().get()->DesktopChildSiteBridge(
174
- std::move(desktopChildSiteBridge));
175
-
176
72
  return m_reactNativeWin32App;
177
73
  }
178
74
 
@@ -1,6 +1,8 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
1
3
  #pragma once
4
+
2
5
  #include "ReactNativeAppBuilder.g.h"
3
- #include <winrt/Microsoft.UI.Content.h>
4
6
 
5
7
  namespace winrt::Microsoft::ReactNative::implementation {
6
8
  struct ReactNativeAppBuilder : ReactNativeAppBuilderT<ReactNativeAppBuilder> {
@@ -8,25 +10,15 @@ struct ReactNativeAppBuilder : ReactNativeAppBuilderT<ReactNativeAppBuilder> {
8
10
 
9
11
  ~ReactNativeAppBuilder();
10
12
 
11
- winrt::Microsoft::ReactNative::ReactNativeAppBuilder AddPackageProviders(
12
- winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::ReactNative::IReactPackageProvider> const
13
- &packageProviders);
14
- winrt::Microsoft::ReactNative::ReactNativeAppBuilder SetReactInstanceSettings(
15
- winrt::Microsoft::ReactNative::ReactInstanceSettings const &settings);
16
-
17
- // TODO: Currently, SetCompositor API is not exposed to the developer.
18
- // Compositor depends on the DispatcherQueue created by DispatcherQueueController on a current thread
19
- // or dedicated thread. So we also have to make a provision for setting DispatcherQueueController.
13
+ winrt::Microsoft::ReactNative::ReactNativeAppBuilder SetDispatcherQueueController(
14
+ winrt::Microsoft::UI::Dispatching::DispatcherQueueController const &dispatcherQueueController);
20
15
  winrt::Microsoft::ReactNative::ReactNativeAppBuilder SetCompositor(
21
16
  winrt::Microsoft::UI::Composition::Compositor const &compositor);
22
17
  winrt::Microsoft::ReactNative::ReactNativeAppBuilder SetAppWindow(
23
18
  winrt::Microsoft::UI::Windowing::AppWindow const &appWindow);
24
- winrt::Microsoft::ReactNative::ReactNativeAppBuilder SetReactViewOptions(
25
- winrt::Microsoft::ReactNative::ReactViewOptions const &reactViewOptions);
26
19
  winrt::Microsoft::ReactNative::ReactNativeWin32App Build();
27
20
 
28
21
  private:
29
- winrt::Microsoft::ReactNative::ReactViewOptions m_reactViewOptions{};
30
22
  winrt::Microsoft::ReactNative::ReactNativeWin32App m_reactNativeWin32App{nullptr};
31
23
  };
32
24
  } // namespace winrt::Microsoft::ReactNative::implementation
@@ -9,60 +9,39 @@ import "IReactPackageProvider.idl";
9
9
  namespace Microsoft.ReactNative {
10
10
 
11
11
  [experimental]
12
- DOC_STRING("ReactNativeWin32App is used to manage resources appropriately to be able to host ReactNative component in a contentIsland.")
12
+ DOC_STRING("ReactNativeWin32App sets up the infrastructure for the default experience of a ReactNative application filling a WinAppSDK window.")
13
13
  runtimeclass ReactNativeWin32App {
14
14
  // Properties
15
15
  Microsoft.UI.Windowing.AppWindow AppWindow {get;};
16
16
 
17
- Microsoft.UI.Composition.Compositor Compositor {get;};
18
-
19
- Microsoft.UI.Content.DesktopChildSiteBridge DesktopChildSiteBridge {get;};
20
-
21
17
  ReactNativeHost ReactNativeHost {get;};
22
18
 
23
- ReactNativeIsland ReactNativeIsland {get;};
19
+ ReactViewOptions ReactViewOptions {get;};
20
+
21
+ // TODO: Hide these APIs for now until we're sure we need to expose them and can do so safely
22
+ // Microsoft.UI.Composition.Compositor Compositor {get;};
23
+ // Microsoft.UI.Content.DesktopChildSiteBridge DesktopChildSiteBridge {get;};
24
+ // Microsoft.UI.Dispatching DispatcherQueueController {get;};
25
+ // ReactNativeIsland ReactNativeIsland {get;};
24
26
 
25
27
  // Methods
26
28
  void Start();
27
29
  }
28
30
 
29
31
  [experimental]
30
- DOC_STRING("This is the builder for creating ReactInstanceSettings.")
31
- runtimeclass ReactInstanceSettingsBuilder
32
- {
33
- ReactInstanceSettingsBuilder();
34
-
35
- // Properties
36
- ReactInstanceSettings ReactInstanceSettings {get;};
37
-
38
- // Methods
39
- ReactInstanceSettingsBuilder UseDirectDebugger(Boolean state);
40
-
41
- ReactInstanceSettingsBuilder UseDeveloperSupport(Boolean state);
42
-
43
- ReactInstanceSettingsBuilder BundleRootPath(String path);
44
-
45
- ReactInstanceSettingsBuilder DebugBundlePath(String path);
46
-
47
- ReactInstanceSettingsBuilder JavaScriptBundleFile(String file);
48
-
49
- ReactInstanceSettingsBuilder UseFastRefresh(Boolean state);
50
- }
51
-
52
- [experimental]
53
- DOC_STRING("ReactNativeAppBuilder initializes all the required infrastructure for a Win32 Fabric Application.")
32
+ DOC_STRING("ReactNativeAppBuilder builds a ReactNativeWin32App with the base WinAppSDK infrastructure.")
54
33
  runtimeclass ReactNativeAppBuilder
55
34
  {
56
35
  ReactNativeAppBuilder();
57
36
 
58
37
  // Methods
59
- ReactNativeAppBuilder AddPackageProviders(Windows.Foundation.Collections.IVector<Microsoft.ReactNative.IReactPackageProvider> packageProviders);
60
-
61
- ReactNativeAppBuilder SetReactInstanceSettings(ReactInstanceSettings settings);
62
38
 
63
39
  ReactNativeAppBuilder SetAppWindow(Microsoft.UI.Windowing.AppWindow appWindow);
64
40
 
65
- ReactNativeAppBuilder SetReactViewOptions(Microsoft.ReactNative.ReactViewOptions reactViewOptions);
41
+ // TODO: Hide these APIs for now until we're sure we need to expose them and can do so safely
42
+ // Compositor depends on the DispatcherQueue created by DispatcherQueueController
43
+ // ReactNativeAppBuilder SetCompositor(Microsoft.UI.Composition.Compositor compositor);
44
+ // ReactNativeAppBuilder SetDispatcherQueueController(Microsoft.UI.Dispatching DispatcherQueueController);
66
45
 
67
46
  ReactNativeWin32App Build();
68
47
  }
@@ -78,11 +78,12 @@ namespace Microsoft.ReactNative
78
78
  DOC_STRING("A windows composition component that hosts React Native UI elements.")
79
79
  runtimeclass ReactNativeIsland
80
80
  {
81
- DOC_STRING("Creates a new instance of @ReactNativeIsland.")
81
+ DOC_STRING("Creates a new instance of @ReactNativeIsland. Can be implemented with a ReactViewHost or a ComponentView with reactContext")
82
82
  ReactNativeIsland();
83
83
 
84
84
  #ifdef USE_WINUI3
85
85
  ReactNativeIsland(Microsoft.UI.Composition.Compositor compositor);
86
+ ReactNativeIsland(Microsoft.UI.Composition.Compositor compositor, Microsoft.ReactNative.IReactContext context, Microsoft.ReactNative.ComponentView componentView);
86
87
  #endif
87
88
 
88
89
  DOC_STRING(
@@ -97,7 +98,7 @@ namespace Microsoft.ReactNative
97
98
 
98
99
  DOC_STRING("ScaleFactor for this windows (DPI/96)")
99
100
  Single ScaleFactor {get; set;};
100
-
101
+
101
102
  Single FontSizeMultiplier { get; };
102
103
 
103
104
  DOC_STRING("Move focus to this @ReactNativeIsland")
@@ -1,12 +1,42 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
1
4
  #include "pch.h"
2
5
  #include "ReactNativeWin32App.h"
3
6
  #include "ReactNativeWin32App.g.cpp"
7
+
8
+ #include "IReactDispatcher.h"
9
+ #include "ReactCoreInjection.h"
4
10
  #include "ReactNativeHost.h"
11
+
5
12
  #include "winrt/Microsoft.UI.Composition.h"
6
13
  #include "winrt/Microsoft.UI.Content.h"
14
+ #include "winrt/Microsoft.UI.Dispatching.h"
7
15
  #include "winrt/Microsoft.UI.Interop.h"
8
16
  #include "winrt/Microsoft.UI.Windowing.h"
9
17
 
18
+ // Scaling factor for the window's content based on the DPI of the display where the window is located.
19
+ float ScaleFactor(HWND hwnd) noexcept {
20
+ return GetDpiForWindow(hwnd) / static_cast<float>(USER_DEFAULT_SCREEN_DPI);
21
+ }
22
+
23
+ void UpdateRootViewSizeToAppWindow(
24
+ winrt::Microsoft::ReactNative::ReactNativeIsland const &rootView,
25
+ winrt::Microsoft::UI::Windowing::AppWindow const &window) {
26
+ auto hwnd = winrt::Microsoft::UI::GetWindowFromWindowId(window.Id());
27
+ auto scaleFactor = ScaleFactor(hwnd);
28
+ winrt::Windows::Foundation::Size size{
29
+ window.ClientSize().Width / scaleFactor, window.ClientSize().Height / scaleFactor};
30
+ // Do not relayout when minimized
31
+ if (window.Presenter().as<winrt::Microsoft::UI::Windowing::OverlappedPresenter>().State() !=
32
+ winrt::Microsoft::UI::Windowing::OverlappedPresenterState::Minimized) {
33
+ winrt::Microsoft::ReactNative::LayoutConstraints constraints;
34
+ constraints.LayoutDirection = winrt::Microsoft::ReactNative::LayoutDirection::Undefined;
35
+ constraints.MaximumSize = constraints.MinimumSize = size;
36
+ rootView.Arrange(constraints, {0, 0});
37
+ }
38
+ }
39
+
10
40
  namespace winrt::Microsoft::ReactNative::implementation {
11
41
  ReactNativeWin32App::ReactNativeWin32App() {}
12
42
 
@@ -14,41 +44,44 @@ ReactNativeWin32App::~ReactNativeWin32App() {
14
44
  m_desktopChildSiteBridge = nullptr;
15
45
 
16
46
  // Destroy all Composition objects
17
- m_compositor.Close();
18
- m_compositor = nullptr;
19
- }
20
-
21
- void ReactNativeWin32App::AppWindow(winrt::Microsoft::UI::Windowing::AppWindow const &appWindow) {
22
- m_appWindow = appWindow;
47
+ if (m_compositor != nullptr) {
48
+ m_compositor.Close();
49
+ m_compositor = nullptr;
50
+ }
23
51
  }
24
52
 
25
53
  winrt::Microsoft::UI::Windowing::AppWindow ReactNativeWin32App::AppWindow() {
26
54
  return m_appWindow;
27
55
  }
28
56
 
29
- void ReactNativeWin32App::Compositor(winrt::Microsoft::UI::Composition::Compositor const &compositor) {
30
- m_compositor = compositor;
57
+ void ReactNativeWin32App::AppWindow(winrt::Microsoft::UI::Windowing::AppWindow const &appWindow) {
58
+ m_appWindow = appWindow;
31
59
  }
32
60
 
33
61
  winrt::Microsoft::UI::Composition::Compositor ReactNativeWin32App::Compositor() {
34
62
  return m_compositor;
35
63
  }
36
64
 
37
- winrt::Microsoft::ReactNative::ReactNativeIsland ReactNativeWin32App::ReactNativeIsland() {
38
- return m_reactNativeIsland;
65
+ void ReactNativeWin32App::Compositor(winrt::Microsoft::UI::Composition::Compositor const &compositor) {
66
+ m_compositor = compositor;
39
67
  }
40
68
 
41
- void ReactNativeWin32App::ReactNativeIsland(winrt::Microsoft::ReactNative::ReactNativeIsland const &reactNativeIsland) {
42
- m_reactNativeIsland = reactNativeIsland;
69
+ void ReactNativeWin32App::DesktopChildSiteBridge(
70
+ winrt::Microsoft::UI::Content::DesktopChildSiteBridge const &desktopChildSiteBridge) {
71
+ m_desktopChildSiteBridge = desktopChildSiteBridge;
43
72
  }
44
73
 
45
74
  winrt::Microsoft::UI::Content::DesktopChildSiteBridge ReactNativeWin32App::DesktopChildSiteBridge() {
46
75
  return m_desktopChildSiteBridge;
47
76
  }
48
77
 
49
- void ReactNativeWin32App::DesktopChildSiteBridge(
50
- winrt::Microsoft::UI::Content::DesktopChildSiteBridge const &desktopChildSiteBridge) {
51
- m_desktopChildSiteBridge = desktopChildSiteBridge;
78
+ winrt::Microsoft::UI::Dispatching::DispatcherQueueController ReactNativeWin32App::DispatcherQueueController() {
79
+ return m_dispatcherQueueController;
80
+ }
81
+
82
+ void ReactNativeWin32App::DispatcherQueueController(
83
+ winrt::Microsoft::UI::Dispatching::DispatcherQueueController const &dispatcherQueueController) {
84
+ m_dispatcherQueueController = dispatcherQueueController;
52
85
  }
53
86
 
54
87
  winrt::Microsoft::ReactNative::ReactNativeHost ReactNativeWin32App::ReactNativeHost() {
@@ -62,12 +95,90 @@ void ReactNativeWin32App::ReactNativeHost(winrt::Microsoft::ReactNative::ReactNa
62
95
  m_host = host;
63
96
  }
64
97
 
65
- void ReactNativeWin32App::DispatchQueueController(
66
- winrt::Microsoft::UI::Dispatching::DispatcherQueueController const &dispatcherQueueController) {
67
- m_dispatcherQueueController = dispatcherQueueController;
98
+ winrt::Microsoft::ReactNative::ReactNativeIsland ReactNativeWin32App::ReactNativeIsland() {
99
+ return m_reactNativeIsland;
100
+ }
101
+
102
+ void ReactNativeWin32App::ReactNativeIsland(winrt::Microsoft::ReactNative::ReactNativeIsland const &reactNativeIsland) {
103
+ m_reactNativeIsland = reactNativeIsland;
104
+ }
105
+
106
+ winrt::Microsoft::ReactNative::ReactViewOptions ReactNativeWin32App::ReactViewOptions() {
107
+ if (m_reactViewOptions == nullptr) {
108
+ m_reactViewOptions = winrt::make<winrt::Microsoft::ReactNative::implementation::ReactViewOptions>();
109
+ }
110
+ return m_reactViewOptions;
111
+ }
112
+
113
+ void ReactNativeWin32App::ReactViewOptions(winrt::Microsoft::ReactNative::ReactViewOptions const &viewOptions) {
114
+ m_reactViewOptions = viewOptions;
68
115
  }
69
116
 
70
117
  void ReactNativeWin32App::Start() {
118
+ // Show the hosting AppWindow
119
+ m_appWindow.Show();
120
+
121
+ // Currently set the property to use current thread dispatcher as a default UI dispatcher.
122
+ // TODO: Use the correct dispatcher from a developer-provided DispatcherQueueController
123
+ ReactNativeHost().InstanceSettings().Properties().Set(
124
+ ReactDispatcherHelper::UIDispatcherProperty(), ReactDispatcherHelper::UIThreadDispatcher());
125
+
126
+ auto hwnd{winrt::Microsoft::UI::GetWindowFromWindowId(m_appWindow.Id())};
127
+
128
+ winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
129
+ ReactNativeHost().InstanceSettings().Properties(), reinterpret_cast<uint64_t>(hwnd));
130
+
131
+ winrt::Microsoft::ReactNative::Composition::CompositionUIService::SetCompositor(
132
+ ReactNativeHost().InstanceSettings(), m_compositor);
133
+
134
+ // Start the react-native instance, which will create a JavaScript runtime and load the applications bundle.
135
+ ReactNativeHost().ReloadInstance();
136
+
137
+ // Create a RootView which will present a react-native component
138
+ if (m_reactNativeIsland == nullptr) {
139
+ m_reactNativeIsland = winrt::Microsoft::ReactNative::ReactNativeIsland(m_compositor);
140
+ }
141
+
142
+ m_reactNativeIsland.ReactViewHost(
143
+ winrt::Microsoft::ReactNative::ReactCoreInjection::MakeViewHost(ReactNativeHost(), ReactViewOptions()));
144
+
145
+ // Update the size of the RootView when the AppWindow changes size
146
+ m_appWindow.Changed([wkRootView = winrt::make_weak(m_reactNativeIsland)](
147
+ winrt::Microsoft::UI::Windowing::AppWindow const &window,
148
+ winrt::Microsoft::UI::Windowing::AppWindowChangedEventArgs const &args) {
149
+ if (args.DidSizeChange() || args.DidVisibilityChange()) {
150
+ if (auto rootView = wkRootView.get()) {
151
+ UpdateRootViewSizeToAppWindow(rootView, window);
152
+ }
153
+ }
154
+ });
155
+
156
+ // Quit application when main window is closed
157
+ m_appWindow.Destroying([this](
158
+ winrt::Microsoft::UI::Windowing::AppWindow const &window,
159
+ winrt::Windows::Foundation::IInspectable const & /*args*/) {
160
+ // Before we shutdown the application - unload the ReactNativeHost to give the javascript a chance to save any
161
+ // state
162
+ auto async = ReactNativeHost().UnloadInstance();
163
+ async.Completed([this](auto asyncInfo, winrt::Windows::Foundation::AsyncStatus asyncStatus) {
164
+ assert(asyncStatus == winrt::Windows::Foundation::AsyncStatus::Completed);
165
+ ReactNativeHost().InstanceSettings().UIDispatcher().Post([]() { PostQuitMessage(0); });
166
+ });
167
+ });
168
+
169
+ // DesktopChildSiteBridge create a ContentSite that can host the RootView ContentIsland
170
+ m_desktopChildSiteBridge =
171
+ winrt::Microsoft::UI::Content::DesktopChildSiteBridge::Create(m_compositor, m_appWindow.Id());
172
+
173
+ m_desktopChildSiteBridge.Connect(m_reactNativeIsland.Island());
174
+
175
+ m_desktopChildSiteBridge.ResizePolicy(winrt::Microsoft::UI::Content::ContentSizePolicy::ResizeContentToParentWindow);
176
+
177
+ auto scaleFactor = ScaleFactor(hwnd);
178
+ m_reactNativeIsland.ScaleFactor(scaleFactor);
179
+
180
+ UpdateRootViewSizeToAppWindow(m_reactNativeIsland, m_appWindow);
181
+
71
182
  m_desktopChildSiteBridge.Show();
72
183
 
73
184
  // Run the main application event loop
@@ -1,5 +1,9 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
1
3
  #pragma once
4
+
2
5
  #include "ReactNativeWin32App.g.h"
6
+
3
7
  #include "winrt/Microsoft.UI.Dispatching.h"
4
8
 
5
9
  namespace winrt::Microsoft::ReactNative::implementation {
@@ -13,26 +17,31 @@ struct ReactNativeWin32App : ReactNativeWin32AppT<ReactNativeWin32App> {
13
17
  winrt::Microsoft::UI::Composition::Compositor Compositor();
14
18
  void Compositor(winrt::Microsoft::UI::Composition::Compositor const &compositor);
15
19
 
16
- winrt::Microsoft::ReactNative::ReactNativeIsland ReactNativeIsland();
17
- void ReactNativeIsland(winrt::Microsoft::ReactNative::ReactNativeIsland const &reactNativeIsland);
18
-
19
20
  winrt::Microsoft::UI::Content::DesktopChildSiteBridge DesktopChildSiteBridge();
20
21
  void DesktopChildSiteBridge(winrt::Microsoft::UI::Content::DesktopChildSiteBridge const &desktopChildSiteBridge);
21
22
 
22
- void DispatchQueueController(
23
+ winrt::Microsoft::UI::Dispatching::DispatcherQueueController DispatcherQueueController();
24
+ void DispatcherQueueController(
23
25
  winrt::Microsoft::UI::Dispatching::DispatcherQueueController const &dispatcherQueueController);
24
26
 
25
27
  winrt::Microsoft::ReactNative::ReactNativeHost ReactNativeHost();
26
28
  void ReactNativeHost(winrt::Microsoft::ReactNative::ReactNativeHost const &host);
27
29
 
30
+ winrt::Microsoft::ReactNative::ReactNativeIsland ReactNativeIsland();
31
+ void ReactNativeIsland(winrt::Microsoft::ReactNative::ReactNativeIsland const &reactNativeIsland);
32
+
33
+ winrt::Microsoft::ReactNative::ReactViewOptions ReactViewOptions();
34
+ void ReactViewOptions(winrt::Microsoft::ReactNative::ReactViewOptions const &reactViewOptions);
35
+
28
36
  void Start();
29
37
 
30
38
  private:
31
39
  winrt::Microsoft::UI::Windowing::AppWindow m_appWindow{nullptr};
32
40
  winrt::Microsoft::UI::Composition::Compositor m_compositor{nullptr};
41
+ winrt::Microsoft::UI::Content::DesktopChildSiteBridge m_desktopChildSiteBridge{nullptr};
33
42
  winrt::Microsoft::UI::Dispatching::DispatcherQueueController m_dispatcherQueueController{nullptr};
34
43
  winrt::Microsoft::ReactNative::ReactNativeHost m_host{nullptr};
35
44
  winrt::Microsoft::ReactNative::ReactNativeIsland m_reactNativeIsland{nullptr};
36
- winrt::Microsoft::UI::Content::DesktopChildSiteBridge m_desktopChildSiteBridge{nullptr};
45
+ winrt::Microsoft::ReactNative::ReactViewOptions m_reactViewOptions{nullptr};
37
46
  };
38
47
  } // namespace winrt::Microsoft::ReactNative::implementation
@@ -24,6 +24,8 @@ namespace Microsoft.ReactNative {
24
24
  Microsoft.UI.Composition.CompositionBrush AsBrush(Microsoft.ReactNative.Composition.Theme theme);
25
25
  #endif
26
26
 
27
+ Boolean Equals(Color color);
28
+
27
29
  static Color Black();
28
30
  static Color Transparent();
29
31
  static Color ReadValue(IJSValueReader reader);