16
16
#include < winrt/Windows.UI.Core.h>
17
17
#include < winrt/Windows.UI.ViewManagement.h>
18
18
19
+ #ifdef USE_FABRIC
20
+ #include < ReactCoreInjection.h>
21
+ #endif
22
+
19
23
using namespace winrt ::Microsoft::ReactNative;
20
24
21
25
namespace Microsoft ::ReactNative {
@@ -32,42 +36,54 @@ DeviceInfoHolder::DeviceInfoHolder(const Mso::React::IReactContext &context) : m
32
36
}
33
37
34
38
void DeviceInfoHolder::InitDeviceInfoHolder (const Mso::React::IReactContext &context) noexcept {
35
- if (xaml::TryGetCurrentApplication ()) {
39
+ if (xaml::TryGetCurrentApplication () || IsFabricEnabled (context. Properties ()) ) {
36
40
auto deviceInfoHolder = std::make_shared<DeviceInfoHolder>(context);
37
41
38
42
deviceInfoHolder->updateDeviceInfo ();
39
43
winrt::Microsoft::ReactNative::ReactPropertyBag pb{context.Properties ()};
40
44
pb.Set (DeviceInfoHolderPropertyId (), std::move (deviceInfoHolder));
41
45
42
- if (auto window = xaml::Window::Current ()) {
43
- auto const &coreWindow = window.CoreWindow ();
46
+ uint64_t hwnd = 0 ;
44
47
45
- deviceInfoHolder->m_sizeChangedRevoker =
46
- coreWindow.SizeChanged (winrt::auto_revoke, [weakHolder = std::weak_ptr (deviceInfoHolder)](auto &&, auto &&) {
47
- if (auto strongHolder = weakHolder.lock ()) {
48
- strongHolder->updateDeviceInfo ();
49
- }
50
- });
51
- } else {
52
- assert (IsXamlIsland ());
53
- // This is either a WinUI 3 island or a system XAML island
54
- // system XAML islands have a CoreWindow so we want to use the GetForCurrentView APIs
55
- // For WinUI 3 islands we require the app to forward window messages as ReactNotifications
48
+ #ifdef USE_FABRIC
49
+ if (IsFabricEnabled (context.Properties ())) {
50
+ hwnd = winrt::Microsoft::ReactNative::implementation::ReactCoreInjection::GetTopLevelWindowId (pb.Handle ());
56
51
}
52
+ #endif
57
53
58
- if (! IsWinUI3Island ()) {
59
- // UWP or system XAML island
60
- auto const &displayInfo = winrt::Windows::Graphics::Display::DisplayInformation::GetForCurrentView ();
54
+ if (xaml::TryGetCurrentApplication ()) {
55
+ if ( auto window = xaml::Window::Current ()) {
56
+ auto const &coreWindow = window. CoreWindow ();
61
57
62
- deviceInfoHolder->m_dpiChangedRevoker = displayInfo.DpiChanged (
63
- winrt::auto_revoke, [weakHolder = std::weak_ptr (deviceInfoHolder)](const auto &, const auto &) {
64
- if (auto strongHolder = weakHolder.lock ()) {
65
- strongHolder->updateDeviceInfo ();
66
- }
67
- });
68
- } else if (
69
- auto hwnd =
70
- reinterpret_cast <HWND>(XamlUIService::GetIslandWindowHandle (deviceInfoHolder->m_context ->Properties ()))) {
58
+ deviceInfoHolder->m_sizeChangedRevoker = coreWindow.SizeChanged (
59
+ winrt::auto_revoke, [weakHolder = std::weak_ptr (deviceInfoHolder)](auto &&, auto &&) {
60
+ if (auto strongHolder = weakHolder.lock ()) {
61
+ strongHolder->updateDeviceInfo ();
62
+ }
63
+ });
64
+ } else {
65
+ assert (IsXamlIsland ());
66
+ // This is either a WinUI 3 island or a system XAML island
67
+ // system XAML islands have a CoreWindow so we want to use the GetForCurrentView APIs
68
+ // For WinUI 3 islands we require the app to forward window messages as ReactNotifications
69
+ }
70
+
71
+ if (!IsWinUI3Island ()) {
72
+ // UWP or system XAML island
73
+ auto const &displayInfo = winrt::Windows::Graphics::Display::DisplayInformation::GetForCurrentView ();
74
+
75
+ deviceInfoHolder->m_dpiChangedRevoker = displayInfo.DpiChanged (
76
+ winrt::auto_revoke, [weakHolder = std::weak_ptr (deviceInfoHolder)](const auto &, const auto &) {
77
+ if (auto strongHolder = weakHolder.lock ()) {
78
+ strongHolder->updateDeviceInfo ();
79
+ }
80
+ });
81
+ } else {
82
+ hwnd = XamlUIService::GetIslandWindowHandle (deviceInfoHolder->m_context ->Properties ());
83
+ }
84
+ }
85
+
86
+ if (hwnd) {
71
87
deviceInfoHolder->m_wmSubscription = SubscribeToWindowMessage (
72
88
ReactNotificationService (context.Notifications ()),
73
89
WM_WINDOWPOSCHANGED,
@@ -146,11 +162,22 @@ void DeviceInfoHolder::updateDeviceInfo() noexcept {
146
162
147
163
m_windowWidth = window.Bounds ().Width ;
148
164
m_windowHeight = window.Bounds ().Height ;
149
- } else if (auto hwnd = reinterpret_cast <HWND>(XamlUIService::GetIslandWindowHandle (m_context->Properties ()))) {
150
- RECT rect{};
151
- if (CALL_INDIRECT (L" user32.dll" , GetWindowRect, hwnd, &rect)) {
152
- m_windowWidth = (float )(rect.right - rect.left );
153
- m_windowHeight = (float )(rect.bottom - rect.top );
165
+ } else {
166
+ auto hwnd = XamlUIService::GetIslandWindowHandle (m_context->Properties ());
167
+
168
+ #ifdef USE_FABRIC
169
+ if (IsFabricEnabled (m_context->Properties ())) {
170
+ winrt::Microsoft::ReactNative::ReactPropertyBag pb{m_context->Properties ()};
171
+ hwnd = winrt::Microsoft::ReactNative::implementation::ReactCoreInjection::GetTopLevelWindowId (pb.Handle ());
172
+ }
173
+ #endif
174
+
175
+ if (hwnd) {
176
+ RECT rect{};
177
+ if (CALL_INDIRECT (L" user32.dll" , GetWindowRect, reinterpret_cast <HWND>(hwnd), &rect)) {
178
+ m_windowWidth = (float )(rect.right - rect.left );
179
+ m_windowHeight = (float )(rect.bottom - rect.top );
180
+ }
154
181
}
155
182
}
156
183
@@ -165,14 +192,20 @@ void DeviceInfoHolder::updateDeviceInfo() noexcept {
165
192
notifyChanged ();
166
193
} else {
167
194
RECT desktopRect{};
168
- if (CALL_INDIRECT (L" user32.dll" , GetWindowRect, nullptr , &desktopRect)) {
195
+
196
+ auto hwnd = XamlUIService::GetIslandWindowHandle (m_context->Properties ());
197
+
198
+ #ifdef USE_FABRIC
199
+ if (IsFabricEnabled (m_context->Properties ())) {
200
+ winrt::Microsoft::ReactNative::ReactPropertyBag pb{m_context->Properties ()};
201
+ hwnd = winrt::Microsoft::ReactNative::implementation::ReactCoreInjection::GetTopLevelWindowId (pb.Handle ());
202
+ }
203
+ #endif
204
+
205
+ if (hwnd && CALL_INDIRECT (L" user32.dll" , GetWindowRect, reinterpret_cast <HWND>(hwnd), &desktopRect)) {
169
206
m_screenWidth = static_cast <uint32_t >(desktopRect.right - desktopRect.left );
170
207
m_screenHeight = static_cast <uint32_t >(desktopRect.bottom - desktopRect.top );
171
- m_dpi = static_cast <float >(CALL_INDIRECT (
172
- L" user32.dll" ,
173
- GetDpiForWindow,
174
- reinterpret_cast <HWND>(
175
- winrt::Microsoft::ReactNative::XamlUIService::GetIslandWindowHandle (m_context->Properties ()))));
208
+ m_dpi = static_cast <float >(CALL_INDIRECT (L" user32.dll" , GetDpiForWindow, reinterpret_cast <HWND>(hwnd)));
176
209
}
177
210
}
178
211
}
0 commit comments