Skip to content

Commit 4757284

Browse files
authored
Fabric : Added support for selectionColor in TextInput component (#14743)
* Added support for selectionColor for textInput * yarn fix and format * Change files
1 parent 6916370 commit 4757284

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "Added support for selectionColor for textInput",
4+
"packageName": "react-native-windows",
5+
"email": "74712637+iamAbhi-916@users.noreply.github.com",
6+
"dependentChangeType": "none"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,20 +320,30 @@ struct CompTextHost : public winrt::implements<CompTextHost, ITextHost> {
320320
return (*m_outer->windowsTextInputProps().textAttributes.foregroundColor).AsColorRefNoAlpha();
321321
// cr = 0x000000FF;
322322
break;
323-
324323
case COLOR_WINDOW:
325324
if (m_outer->viewProps()->backgroundColor)
326325
return (*m_outer->viewProps()->backgroundColor).AsColorRefNoAlpha();
327326
break;
328-
// case COLOR_HIGHLIGHT:
329-
// cr = RGB(0, 0, 255);
330-
// cr = 0x0000ffFF;
331-
// break;
332327

333-
// case COLOR_HIGHLIGHTTEXT:
334-
// cr = RGB(255, 0, 0);
335-
// cr = 0xFFFFFFFF;
336-
// break;
328+
case COLOR_HIGHLIGHT:
329+
if (m_outer->windowsTextInputProps().selectionColor)
330+
return (*m_outer->windowsTextInputProps().selectionColor).AsColorRefNoAlpha();
331+
break;
332+
333+
case COLOR_HIGHLIGHTTEXT:
334+
// For selected text color, we use the same color as the selection background
335+
// or the text color if selection color is not specified
336+
if (m_outer->windowsTextInputProps().selectionColor) {
337+
// Calculate appropriate text color based on selection background
338+
auto selectionColor = (*m_outer->windowsTextInputProps().selectionColor).AsColorRefNoAlpha();
339+
// Use white text for dark selection, black text for light selection
340+
int r = GetRValue(selectionColor);
341+
int g = GetGValue(selectionColor);
342+
int b = GetBValue(selectionColor);
343+
int brightness = (r * 299 + g * 587 + b * 114) / 1000;
344+
return brightness > 125 ? RGB(0, 0, 0) : RGB(255, 255, 255);
345+
}
346+
break;
337347

338348
// case COLOR_GRAYTEXT:
339349
// cr = RGB(128, 128, 128);
@@ -1126,11 +1136,14 @@ void WindowsTextInputComponentView::updateProps(
11261136
bool effectiveSpellCheck = newTextInputProps.spellCheck || newTextInputProps.autoCorrect;
11271137
updateSpellCheck(effectiveSpellCheck);
11281138
}
1129-
11301139
if (!oldProps || oldTextInputProps.autoCorrect != newTextInputProps.autoCorrect) {
11311140
updateAutoCorrect(newTextInputProps.autoCorrect);
11321141
}
11331142

1143+
if (oldTextInputProps.selectionColor != newTextInputProps.selectionColor) {
1144+
m_needsRedraw = true;
1145+
}
1146+
11341147
UpdatePropertyBits();
11351148
}
11361149

0 commit comments

Comments
 (0)