Skip to content
Open
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": "Add localization infrastructure for context menu strings in TextInput and Text components",
"packageName": "react-native-windows",
"email": "74712637+iamAbhi-916@users.noreply.github.com",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions vnext/Desktop.DLL/React.Windows.Desktop.DLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
</ResourceCompile>
<ResourceCompile Include="..\Microsoft.ReactNative\Resources\Strings.rc" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="boost" Version="1.83.0.0" />
Expand Down
1 change: 1 addition & 0 deletions vnext/Desktop/React.Windows.Desktop.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
<ClCompile Include="..\Microsoft.ReactNative\Modules\Animated\ValueAnimatedNode.cpp" />
<ClCompile Include="..\Microsoft.ReactNative\Modules\ImageViewManagerModule.cpp" />
<ClCompile Include="..\Microsoft.ReactNative\Utils\KeyboardUtils.cpp" />
<ClCompile Include="..\Microsoft.ReactNative\Utils\LocalizedStrings.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ABI\MessageQueueShim.h">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

#include <AutoDraw.h>
#include <Fabric/ReactTaggedView.h>
#include <Resources/StringResourceIds.h>
#include <Utils/IcuUtils.h>
#include <Utils/LocalizedStrings.h>
#include <Utils/ValueUtils.h>
#include <react/renderer/components/text/ParagraphShadowNode.h>
#include <react/renderer/components/text/ParagraphState.h>
Expand Down Expand Up @@ -829,7 +831,7 @@ void ParagraphComponentView::SetSelection(int32_t start, int32_t end) noexcept {
}

void ParagraphComponentView::ShowContextMenu() noexcept {
HMENU menu = CreatePopupMenu();
std::unique_ptr<std::remove_pointer_t<HMENU>, decltype(&DestroyMenu)> menu(CreatePopupMenu(), &DestroyMenu);
if (!menu) {
return;
}
Expand All @@ -838,28 +840,26 @@ void ParagraphComponentView::ShowContextMenu() noexcept {
const std::wstring utf16Text{facebook::react::WindowsTextLayoutManager::GetTransformedText(m_attributedStringBox)};
const bool hasText = !utf16Text.empty();

// Add menu items (1 = Copy, 2 = Select All)
AppendMenuW(menu, MF_STRING | (hasSelection ? 0 : MF_GRAYED), 1, L"Copy");
AppendMenuW(menu, MF_STRING | (hasText ? 0 : MF_GRAYED), 2, L"Select All");
auto copyStr = ::Microsoft::ReactNative::GetLocalizedString(IDS_CONTEXT_MENU_COPY);
auto selectAllStr = ::Microsoft::ReactNative::GetLocalizedString(IDS_CONTEXT_MENU_SELECT_ALL);

AppendMenuW(menu.get(), MF_STRING | (hasSelection ? 0 : MF_GRAYED), 1, copyStr.c_str());
AppendMenuW(menu.get(), MF_STRING | (hasText ? 0 : MF_GRAYED), 2, selectAllStr.c_str());

// Get cursor position for menu placement
POINT cursorPos;
GetCursorPos(&cursorPos);

const HWND hwnd = GetActiveWindow();

const int cmd = TrackPopupMenu(
menu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_NONOTIFY, cursorPos.x, cursorPos.y, 0, hwnd, NULL);
menu.get(), TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_NONOTIFY, cursorPos.x, cursorPos.y, 0, hwnd, NULL);

if (cmd == 1) {
// Copy
CopySelectionToClipboard();
} else if (cmd == 2) {
SetSelection(0, static_cast<int32_t>(utf16Text.length()));
DrawText();
}

DestroyMenu(menu);
}

void ParagraphComponentView::OnKeyDown(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <AutoDraw.h>
#include <Fabric/Composition/UiaHelpers.h>
#include <Fabric/platform/react/renderer/graphics/PlatformColorUtils.h>
#include <Resources/StringResourceIds.h>
#include <Utils/LocalizedStrings.h>
#include <Utils/ThemeUtils.h>
#include <Utils/ValueUtils.h>
#include <react/renderer/components/textinput/TextInputState.h>
Expand Down Expand Up @@ -1900,7 +1902,7 @@ void WindowsTextInputComponentView::OnContextMenuKey(
}

void WindowsTextInputComponentView::ShowContextMenu(const winrt::Windows::Foundation::Point &position) noexcept {
HMENU menu = CreatePopupMenu();
std::unique_ptr<std::remove_pointer_t<HMENU>, decltype(&DestroyMenu)> menu(CreatePopupMenu(), &DestroyMenu);
if (!menu)
return;

Expand All @@ -1913,35 +1915,38 @@ void WindowsTextInputComponentView::ShowContextMenu(const winrt::Windows::Founda
bool isReadOnly = windowsTextInputProps().editable == false;
bool canPaste = !isReadOnly && IsClipboardFormatAvailable(CF_UNICODETEXT);

AppendMenuW(menu, MF_STRING | (hasSelection && !isReadOnly ? 0 : MF_GRAYED), 1, L"Cut");
AppendMenuW(menu, MF_STRING | (hasSelection ? 0 : MF_GRAYED), 2, L"Copy");
AppendMenuW(menu, MF_STRING | (canPaste ? 0 : MF_GRAYED), 3, L"Paste");
AppendMenuW(menu, MF_STRING | (!isEmpty && !isReadOnly ? 0 : MF_GRAYED), 4, L"Select All");
auto cutStr = ::Microsoft::ReactNative::GetLocalizedString(IDS_CONTEXT_MENU_CUT);
auto copyStr = ::Microsoft::ReactNative::GetLocalizedString(IDS_CONTEXT_MENU_COPY);
auto pasteStr = ::Microsoft::ReactNative::GetLocalizedString(IDS_CONTEXT_MENU_PASTE);
auto selectAllStr = ::Microsoft::ReactNative::GetLocalizedString(IDS_CONTEXT_MENU_SELECT_ALL);

AppendMenuW(menu.get(), MF_STRING | (hasSelection && !isReadOnly ? 0 : MF_GRAYED), 1, cutStr.c_str());
AppendMenuW(menu.get(), MF_STRING | (hasSelection ? 0 : MF_GRAYED), 2, copyStr.c_str());
AppendMenuW(menu.get(), MF_STRING | (canPaste ? 0 : MF_GRAYED), 3, pasteStr.c_str());
AppendMenuW(menu.get(), MF_STRING | (!isEmpty && !isReadOnly ? 0 : MF_GRAYED), 4, selectAllStr.c_str());

HWND hwnd = GetActiveWindow();

int cmd = TrackPopupMenu(
menu,
menu.get(),
TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_NONOTIFY,
static_cast<int>(position.X),
static_cast<int>(position.Y),
0,
hwnd,
NULL);

if (cmd == 1) { // Cut
if (cmd == 1) {
m_textServices->TxSendMessage(WM_CUT, 0, 0, &res);
OnTextUpdated();
} else if (cmd == 2) { // Copy
} else if (cmd == 2) {
m_textServices->TxSendMessage(WM_COPY, 0, 0, &res);
} else if (cmd == 3) { // Paste
} else if (cmd == 3) {
m_textServices->TxSendMessage(WM_PASTE, 0, 0, &res);
OnTextUpdated();
} else if (cmd == 4) { // Select All
} else if (cmd == 4) {
m_textServices->TxSendMessage(EM_SETSEL, 0, -1, &res);
}

DestroyMenu(menu);
}

} // namespace winrt::Microsoft::ReactNative::Composition::implementation
6 changes: 6 additions & 0 deletions vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
<ClInclude Include="Utils\UwpPreparedScriptStore.h" />
<ClInclude Include="Utils\UwpScriptStore.h" />
<ClInclude Include="Utils\IcuUtils.h" />
<ClInclude Include="Utils\LocalizedStrings.h" />
<ClInclude Include="Utils\ValueUtils.h" />
<ClInclude Include="Utils\XamlIslandUtils.h" />
<ClInclude Include="Utils\XamlUtils.h" />
Expand Down Expand Up @@ -372,6 +373,7 @@
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
<ClCompile Include="Utils\KeyboardUtils.cpp" />
<ClCompile Include="Utils\LocalBundleReader.cpp" />
<ClCompile Include="Utils\LocalizedStrings.cpp" />
<ClCompile Include="Utils\UwpPreparedScriptStore.cpp" />
<ClCompile Include="Utils\UwpScriptStore.cpp" />
<ClCompile Include="Utils\ValueUtils.cpp" />
Expand Down Expand Up @@ -410,6 +412,10 @@
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
</ResourceCompile>
<ResourceCompile Include="Resources\Strings.rc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Resources\StringResourceIds.h" />
</ItemGroup>
<ItemGroup>
<Natvis Include="$(ReactNativeWindowsDir)Folly\Folly.natvis" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
<ClCompile Include="Utils\LocalBundleReader.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="Utils\LocalizedStrings.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="Utils\UwpPreparedScriptStore.cpp">
<Filter>Utils</Filter>
</ClCompile>
Expand Down Expand Up @@ -368,6 +371,12 @@
<ClInclude Include="Utils\LocalBundleReader.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="Utils\LocalizedStrings.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="Resources\StringResourceIds.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="Utils\PropertyHandlerUtils.h">
<Filter>Utils</Filter>
</ClInclude>
Expand Down Expand Up @@ -520,6 +529,7 @@
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Version.rc" />
<ResourceCompile Include="Resources\Strings.rc" />
</ItemGroup>
<ItemGroup>
<Page Include="DevMenuControl.xaml" />
Expand Down
9 changes: 9 additions & 0 deletions vnext/Microsoft.ReactNative/Resources/StringResourceIds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#define IDS_CONTEXT_MENU_CUT 100
#define IDS_CONTEXT_MENU_COPY 101
#define IDS_CONTEXT_MENU_PASTE 102
#define IDS_CONTEXT_MENU_SELECT_ALL 103
12 changes: 12 additions & 0 deletions vnext/Microsoft.ReactNative/Resources/Strings.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "StringResourceIds.h"

STRINGTABLE
BEGIN
IDS_CONTEXT_MENU_CUT "Cut"
IDS_CONTEXT_MENU_COPY "Copy"
IDS_CONTEXT_MENU_PASTE "Paste"
IDS_CONTEXT_MENU_SELECT_ALL "Select All"
END
34 changes: 34 additions & 0 deletions vnext/Microsoft.ReactNative/Utils/LocalizedStrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "LocalizedStrings.h"
#include <windows.h>

namespace Microsoft::ReactNative {

namespace {

HMODULE GetCurrentModule() noexcept {
static HMODULE s_module = [] {
HMODULE hmod = nullptr;
GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&GetCurrentModule),
&hmod);
return hmod;
}();
return s_module;
}

} // namespace

std::wstring GetLocalizedString(UINT stringId) noexcept {
LPCWSTR buffer = nullptr;
int length = LoadStringW(GetCurrentModule(), stringId, reinterpret_cast<LPWSTR>(&buffer), 0);
if (length > 0 && buffer) {
return std::wstring(buffer, static_cast<size_t>(length));
}
return {};
}

} // namespace Microsoft::ReactNative
13 changes: 13 additions & 0 deletions vnext/Microsoft.ReactNative/Utils/LocalizedStrings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include <windows.h>
#include <string>

namespace Microsoft::ReactNative {

std::wstring GetLocalizedString(UINT stringId) noexcept;

} // namespace Microsoft::ReactNative
Loading