Skip to content

Commit 04329dc

Browse files
authored
[Fabric] Add DeviceInfoModule (#12450)
* First pass at Fabric DeviceInfo * remove DeviceInfoModule stub * Change files * formatting * ForwardWindowMessage * limit ForwardWindowMessage to WM_WINDOWPOSCHANGED * formatting * First pass at Fabric DeviceInfo * remove DeviceInfoModule stub * Change files * formatting * ForwardWindowMessage * limit ForwardWindowMessage to WM_WINDOWPOSCHANGED * formatting * gatekeep InitDeviceInfoHolder
1 parent 1582219 commit 04329dc

File tree

5 files changed

+81
-85
lines changed

5 files changed

+81
-85
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": "[Fabric] Add DeviceInfoModule",
4+
"packageName": "react-native-windows",
5+
"email": "email not defined",
6+
"dependentChangeType": "patch"
7+
}

packages/playground/windows/playground-composition/DeviceInfoModule.cpp

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

packages/playground/windows/playground-composition/Playground-Composition.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <winrt/Windows.UI.Composition.Desktop.h>
2323
#include <winrt/Windows.UI.Composition.h>
2424

25+
#include <DesktopWindowBridge.h>
2526
#include "NativeModules.h"
2627
#include "ReactPropertyBag.h"
2728

@@ -153,7 +154,6 @@ struct WindowData {
153154
std::wstring(L"file:").append(workingDir).append(L"\\Bundle\\").c_str());
154155
host.InstanceSettings().UseDeveloperSupport(true);
155156

156-
host.PackageProviders().Append(CreateStubDeviceInfoPackageProvider());
157157
host.PackageProviders().Append(winrt::make<CompReactPackageProvider>());
158158
winrt::Microsoft::ReactNative::ReactCoreInjection::SetTopLevelWindowId(
159159
host.InstanceSettings().Properties(), reinterpret_cast<uint64_t>(hwnd));
@@ -480,6 +480,9 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
480480
case WM_WINDOWPOSCHANGED: {
481481
auto windowData = WindowData::GetFromWindow(hwnd);
482482
windowData->UpdateSize(hwnd);
483+
484+
winrt::Microsoft::ReactNative::ReactNotificationService rns(windowData->InstanceSettings().Notifications());
485+
winrt::Microsoft::ReactNative::ForwardWindowMessage(rns, hwnd, message, wparam, lparam);
483486
break;
484487
}
485488
}

packages/playground/windows/playground-composition/Playground-Composition.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
<PrecompiledHeader>Create</PrecompiledHeader>
117117
</ClCompile>
118118
<ClCompile Include="CustomComponent.cpp" />
119-
<ClCompile Include="DeviceInfoModule.cpp" />
120119
<ClCompile Include="Playground-Composition.cpp" />
121120
</ItemGroup>
122121
<ItemGroup>

vnext/Microsoft.ReactNative/Modules/DeviceInfoModule.cpp

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include <winrt/Windows.UI.Core.h>
1717
#include <winrt/Windows.UI.ViewManagement.h>
1818

19+
#ifdef USE_FABRIC
20+
#include <ReactCoreInjection.h>
21+
#endif
22+
1923
using namespace winrt::Microsoft::ReactNative;
2024

2125
namespace Microsoft::ReactNative {
@@ -32,42 +36,54 @@ DeviceInfoHolder::DeviceInfoHolder(const Mso::React::IReactContext &context) : m
3236
}
3337

3438
void DeviceInfoHolder::InitDeviceInfoHolder(const Mso::React::IReactContext &context) noexcept {
35-
if (xaml::TryGetCurrentApplication()) {
39+
if (xaml::TryGetCurrentApplication() || IsFabricEnabled(context.Properties())) {
3640
auto deviceInfoHolder = std::make_shared<DeviceInfoHolder>(context);
3741

3842
deviceInfoHolder->updateDeviceInfo();
3943
winrt::Microsoft::ReactNative::ReactPropertyBag pb{context.Properties()};
4044
pb.Set(DeviceInfoHolderPropertyId(), std::move(deviceInfoHolder));
4145

42-
if (auto window = xaml::Window::Current()) {
43-
auto const &coreWindow = window.CoreWindow();
46+
uint64_t hwnd = 0;
4447

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());
5651
}
52+
#endif
5753

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();
6157

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) {
7187
deviceInfoHolder->m_wmSubscription = SubscribeToWindowMessage(
7288
ReactNotificationService(context.Notifications()),
7389
WM_WINDOWPOSCHANGED,
@@ -146,11 +162,22 @@ void DeviceInfoHolder::updateDeviceInfo() noexcept {
146162

147163
m_windowWidth = window.Bounds().Width;
148164
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+
}
154181
}
155182
}
156183

@@ -165,14 +192,20 @@ void DeviceInfoHolder::updateDeviceInfo() noexcept {
165192
notifyChanged();
166193
} else {
167194
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)) {
169206
m_screenWidth = static_cast<uint32_t>(desktopRect.right - desktopRect.left);
170207
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)));
176209
}
177210
}
178211
}

0 commit comments

Comments
 (0)