Skip to content

Commit

Permalink
[Fabric] Fix OnSelectionChanged running multiple times (#12903)
Browse files Browse the repository at this point in the history
* remove extra method calls

* add check for call coming from state

* Change files

* update snapshot
  • Loading branch information
TatianaKapos authored Apr 2, 2024
1 parent 926121b commit 187f3a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix OnSelectionChanged running multiple times",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ onKeyPress key: c
onChange text: ab
onSelectionChange range: 2,2
onKeyPress key: b
onSelectionChange range: 1,1
onChange text: a
onSelectionChange range: 0,0
onChange text: a
onSelectionChange range: 1,1
onKeyPress key: a
Expand All @@ -125,7 +123,7 @@ onFocus
"Opacity": 1,
"Size": [
998,
858,
820,
],
"Visual Type": "SpriteVisual",
},
Expand All @@ -147,9 +145,7 @@ onKeyPress key: c
onChange text: ab
onSelectionChange range: 2,2
onKeyPress key: b
onSelectionChange range: 1,1
onChange text: a
onSelectionChange range: 0,0
onChange text: a
onSelectionChange range: 1,1
onKeyPress key: a
Expand All @@ -168,7 +164,7 @@ onFocus
"Opacity": 1,
"Size": [
998,
299,
262,
],
"Visual Type": "SpriteVisual",
},
Expand Down Expand Up @@ -213,9 +209,7 @@ onKeyPress key: c
onChange text: ab
onSelectionChange range: 2,2
onKeyPress key: b
onSelectionChange range: 1,1
onChange text: a
onSelectionChange range: 0,0
onChange text: a
onSelectionChange range: 1,1
onKeyPress key: a
Expand All @@ -234,7 +228,7 @@ onFocus
"Opacity": 1,
"Size": [
998,
727,
690,
],
"Visual Type": "SpriteVisual",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,6 @@ void WindowsTextInputComponentView::updateState(
return;
}

auto data = m_state->getData();

if (!oldState) {
m_mostRecentEventCount = m_state->getData().mostRecentEventCount;
}
Expand All @@ -1123,19 +1121,11 @@ void WindowsTextInputComponentView::updateState(
m_comingFromState = true;
// Only handle single/empty fragments right now -- ignore the other fragments

LRESULT res;
CHARRANGE cr;
cr.cpMin = cr.cpMax = 0;
winrt::check_hresult(m_textServices->TxSendMessage(EM_EXGETSEL, 0, reinterpret_cast<LPARAM>(&cr), &res));

UpdateText(
m_state->getData().attributedString.getFragments().size()
? m_state->getData().attributedString.getFragments()[0].string
: "");

winrt::check_hresult(
m_textServices->TxSendMessage(EM_SETSEL, static_cast<WPARAM>(cr.cpMin), static_cast<LPARAM>(cr.cpMax), &res));

m_comingFromState = false;
}
}
Expand All @@ -1154,7 +1144,8 @@ void WindowsTextInputComponentView::UpdateText(const std::string &str) noexcept
winrt::check_hresult(m_textServices->TxSendMessage(
EM_SETTEXTEX, reinterpret_cast<WPARAM>(&stt), reinterpret_cast<LPARAM>(str.c_str()), &res));

winrt::check_hresult(m_textServices->TxSendMessage(EM_EXGETSEL, 0, reinterpret_cast<LPARAM>(&cr), &res));
winrt::check_hresult(
m_textServices->TxSendMessage(EM_SETSEL, static_cast<WPARAM>(cr.cpMin), static_cast<LPARAM>(cr.cpMax), &res));

// enable colored emojis
winrt::check_hresult(
Expand Down Expand Up @@ -1225,7 +1216,7 @@ void WindowsTextInputComponentView::OnTextUpdated() noexcept {
}

void WindowsTextInputComponentView::OnSelectionChanged(LONG start, LONG end) noexcept {
if (m_eventEmitter /* && !m_comingFromJS ?? */) {
if (m_eventEmitter && !m_comingFromState /* && !m_comingFromJS ?? */) {
auto emitter = std::static_pointer_cast<const facebook::react::WindowsTextInputEventEmitter>(m_eventEmitter);
facebook::react::WindowsTextInputEventEmitter::OnSelectionChange onSelectionChangeArgs;
onSelectionChangeArgs.selection.start = start;
Expand Down

0 comments on commit 187f3a6

Please sign in to comment.