Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont use CoreWindow to determine key state #12998

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix PaperUIManager (port from windows)",
"packageName": "@office-iss/react-native-win32",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Dont use CoreWindow to determine key state",
"packageName": "react-native-windows",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ const UIManagerJS: UIManagerJSInterface = {
};

// [Windows The spread operator doesn't work on JSI turbomodules, so use this instead
for (const propName of Object.getOwnPropertyNames(NativeUIManager)) {
for (const propName of Object.getOwnPropertyNames(
Object.getPrototypeOf(NativeUIManager),
)) {
// $FlowFixMe
UIManagerJS[propName] = NativeUIManager[propName];
}
Expand Down
1 change: 1 addition & 0 deletions vnext/Desktop.DLL/react-native-win32.x64.def
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ YGConfigGetUseWebDefaults
YGConfigIsExperimentalFeatureEnabled
YGConfigNew
YGConfigSetContext
YGConfigSetErrata
YGConfigSetExperimentalFeatureEnabled
YGConfigSetLogger
YGConfigSetPointScaleFactor
Expand Down
1 change: 1 addition & 0 deletions vnext/Desktop.DLL/react-native-win32.x86.def
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ _YGConfigGetUseWebDefaults@4
_YGConfigIsExperimentalFeatureEnabled@8
_YGConfigNew@0
_YGConfigSetContext@8
_YGConfigSetErrata@8
_YGConfigSetExperimentalFeatureEnabled@12
_YGConfigSetLogger@8
_YGConfigSetPointScaleFactor@8
Expand Down
6 changes: 6 additions & 0 deletions vnext/Desktop/module.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void* winrt_make_Microsoft_ReactNative_ReactNotificationServiceHelper();
void* winrt_make_Microsoft_ReactNative_ReactPropertyBagHelper();
void* winrt_make_Microsoft_ReactNative_ReactViewOptions();
void* winrt_make_Microsoft_ReactNative_RedBoxHelper();
void* winrt_make_Microsoft_ReactNative_QuirkSettings();
void* winrt_make_facebook_react_NativeLogEventSource();
void* winrt_make_facebook_react_NativeTraceEventSource();

Expand Down Expand Up @@ -105,6 +106,11 @@ void* __stdcall winrt_get_activation_factory([[maybe_unused]] std::wstring_view
return winrt_make_Microsoft_ReactNative_RedBoxHelper();
}

if (requal(name, L"Microsoft.ReactNative.QuirkSettings"))
{
return winrt_make_Microsoft_ReactNative_QuirkSettings();
}

if (requal(name, L"facebook.react.NativeLogEventSource"))
{
return winrt_make_facebook_react_NativeLogEventSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1589,10 +1589,43 @@ facebook::react::Tag ViewComponentView::hitTest(
return -1;
}

inline winrt::Windows::System::VirtualKey GetLeftOrRightModifiedKey(
const winrt::Microsoft::ReactNative::Composition::Input::KeyboardSource &source,
winrt::Windows::System::VirtualKey leftKey,
winrt::Windows::System::VirtualKey rightKey) {
return (source.GetKeyState(leftKey) == winrt::Windows::UI::Core::CoreVirtualKeyStates::Down) ? leftKey : rightKey;
}

std::string CodeFromVirtualKey(
const winrt::Microsoft::ReactNative::Composition::Input::KeyboardSource &source,
winrt::Windows::System::VirtualKey virtualKey) {
int key = static_cast<int>(virtualKey);

if (isdigit(key)) {
return "Digit" + std::string(1, static_cast<char>(key));
} else if (isupper(key)) {
return "Key" + std::string(1, static_cast<char>(key));
} else {
// Override the virtual key if it's modified key of Control, Shift or Menu
if (virtualKey == winrt::Windows::System::VirtualKey::Control) {
virtualKey = GetLeftOrRightModifiedKey(
source, winrt::Windows::System::VirtualKey::LeftControl, winrt::Windows::System::VirtualKey::RightControl);
} else if (virtualKey == winrt::Windows::System::VirtualKey::Shift) {
virtualKey = GetLeftOrRightModifiedKey(
source, winrt::Windows::System::VirtualKey::LeftShift, winrt::Windows::System::VirtualKey::RightShift);
} else if (virtualKey == winrt::Windows::System::VirtualKey::Menu) {
virtualKey = GetLeftOrRightModifiedKey(
source, winrt::Windows::System::VirtualKey::LeftMenu, winrt::Windows::System::VirtualKey::RightMenu);
}
}

return ::Microsoft::ReactNative::GetOrUnidentifiedCode(virtualKey);
}

void ViewComponentView::OnKeyDown(
const winrt::Microsoft::ReactNative::Composition::Input::KeyboardSource &source,
const winrt::Microsoft::ReactNative::Composition::Input::KeyRoutedEventArgs &args) noexcept {
auto eventCode = ::Microsoft::ReactNative::CodeFromVirtualKey(args.Key());
auto eventCode = CodeFromVirtualKey(source, args.Key());
bool fShift = source.GetKeyState(winrt::Windows::System::VirtualKey::Shift) !=
winrt::Windows::UI::Core::CoreVirtualKeyStates::None;
bool fAlt = source.GetKeyState(winrt::Windows::System::VirtualKey::Menu) !=
Expand Down Expand Up @@ -1631,7 +1664,7 @@ void ViewComponentView::OnKeyDown(
void ViewComponentView::OnKeyUp(
const winrt::Microsoft::ReactNative::Composition::Input::KeyboardSource &source,
const winrt::Microsoft::ReactNative::Composition::Input::KeyRoutedEventArgs &args) noexcept {
auto eventCode = ::Microsoft::ReactNative::CodeFromVirtualKey(args.Key());
auto eventCode = CodeFromVirtualKey(source, args.Key());
bool fShift = source.GetKeyState(winrt::Windows::System::VirtualKey::Shift) !=
winrt::Windows::UI::Core::CoreVirtualKeyStates::None;
bool fAlt = source.GetKeyState(winrt::Windows::System::VirtualKey::Menu) !=
Expand Down
12 changes: 10 additions & 2 deletions vnext/Microsoft.ReactNative/Utils/KeyboardUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ static const std::string GetOrUnidentified(
return "Unidentified";
}

const std::string GetOrUnidentifiedCode(winrt::Windows::System::VirtualKey virtualKey) {
return GetOrUnidentified(virtualKey, g_virtualKeyToCode);
}

const std::string GetOrUnidentifiedKey(winrt::Windows::System::VirtualKey virtualKey) {
return GetOrUnidentified(virtualKey, g_virtualKeyToKey);
}

std::string FromVirtualKey(winrt::Windows::System::VirtualKey virtualKey, bool fShift, bool fCaps) {
int vk = static_cast<int>(virtualKey);

Expand All @@ -362,7 +370,7 @@ std::string FromVirtualKey(winrt::Windows::System::VirtualKey virtualKey, bool f
return std::string{c};
}

return GetOrUnidentified(virtualKey, g_virtualKeyToKey);
return GetOrUnidentifiedKey(virtualKey);
}

bool IsModifiedKeyPressed(winrt::CoreWindow const &coreWindow, winrt::Windows::System::VirtualKey virtualKey) {
Expand Down Expand Up @@ -410,7 +418,7 @@ std::string CodeFromVirtualKey(winrt::Windows::System::VirtualKey virtualKey) {
}
}

return GetOrUnidentified(virtualKey, g_virtualKeyToCode);
return GetOrUnidentifiedCode(virtualKey);
}

} // namespace Microsoft::ReactNative
5 changes: 4 additions & 1 deletion vnext/Microsoft.ReactNative/Utils/KeyboardUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ bool IsModifiedKeyPressed(winrt::CoreWindow const &coreWindow, winrt::Windows::S
std::string FromVirtualKey(winrt::Windows::System::VirtualKey virtualKey, bool fShift, bool fCaps);
std::string CodeFromVirtualKey(winrt::Windows::System::VirtualKey virtualKey);

} // namespace Microsoft::ReactNative
const std::string GetOrUnidentifiedCode(winrt::Windows::System::VirtualKey virtualKey);
const std::string GetOrUnidentifiedKey(winrt::Windows::System::VirtualKey virtualKey);

} // namespace Microsoft::ReactNative
Loading