Skip to content

Commit 2f7e8f3

Browse files
authored
Implements backgroundColor for Fabric's TextInput (#12079)
* Render background color on Text Applies a fill rect to the composition visual Fixes #11763 * Change files * Protect color conversions from empty values * Apply scale factor to fill rect * Remove unneded ViewProps alias * Render background color on Text Applies a fill rect to the composition visual Fixes #11763 * Change files * Protect color conversions from empty values * Apply scale factor to fill rect * Remove unneded ViewProps alias * Use isColorMeaningful
1 parent 9364315 commit 2f7e8f3

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
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": "Render background color on Text Applies a fill rect to the composition visual Fixes #11763",
4+
"packageName": "react-native-windows",
5+
"email": "26607885+chrisglein@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ struct CompTextHost : public winrt::implements<CompTextHost, ITextHost> {
340340

341341
//@cmember Get the background (either opaque or transparent)
342342
HRESULT TxGetBackStyle(TXTBACKSTYLE *pstyle) override {
343+
// We draw the background color as part of the composition visual, not the text
343344
*pstyle = TXTBACK_TRANSPARENT;
344-
//*pstyle = TXTBACK_OPAQUE;
345345
return S_OK;
346346
}
347347

@@ -643,6 +643,10 @@ void WindowsTextInputComponentView::updateProps(
643643
m_needsRedraw = true;
644644
}
645645

646+
if (oldTextInputProps.backgroundColor != newTextInputProps.backgroundColor) {
647+
m_needsRedraw = true;
648+
}
649+
646650
/*
647651
if (oldTextInputProps.textAttributes.foregroundColor != newTextInputProps.textAttributes.foregroundColor) {
648652
if (newTextInputProps.textAttributes.foregroundColor)
@@ -693,16 +697,6 @@ void WindowsTextInputComponentView::updateProps(
693697
m_element.CharacterCasing(xaml::Controls::CharacterCasing::Normal);
694698
}
695699
}
696-
697-
if (oldViewProps.backgroundColor != newViewProps.backgroundColor) {
698-
auto color = *newViewProps.backgroundColor;
699-
700-
if (newViewProps.backgroundColor) {
701-
m_element.ViewBackground(SolidColorBrushFrom(newViewProps.backgroundColor));
702-
} else {
703-
m_element.ClearValue(winrt::Microsoft::ReactNative::ViewPanel::ViewBackgroundProperty());
704-
}
705-
}
706700
*/
707701

708702
m_props = std::static_pointer_cast<facebook::react::WindowsTextInputProps const>(props);
@@ -1053,6 +1047,18 @@ void WindowsTextInputComponentView::DrawText() noexcept {
10531047

10541048
winrt::check_hresult(m_textServices->OnTxInPlaceActivate(&rcClient));
10551049

1050+
if (facebook::react::isColorMeaningful(m_props->backgroundColor)) {
1051+
auto backgroundColor = m_props->backgroundColor.AsD2DColor();
1052+
winrt::com_ptr<ID2D1SolidColorBrush> backgroundBrush;
1053+
winrt::check_hresult(d2dDeviceContext->CreateSolidColorBrush(backgroundColor, backgroundBrush.put()));
1054+
const D2D1_RECT_F fillRect = {
1055+
static_cast<float>(rcClient.left) / m_layoutMetrics.pointScaleFactor,
1056+
static_cast<float>(rcClient.top) / m_layoutMetrics.pointScaleFactor,
1057+
static_cast<float>(rcClient.right) / m_layoutMetrics.pointScaleFactor,
1058+
static_cast<float>(rcClient.bottom) / m_layoutMetrics.pointScaleFactor};
1059+
d2dDeviceContext->FillRectangle(fillRect, backgroundBrush.get());
1060+
}
1061+
10561062
// TODO keep track of proper invalid rect
10571063
auto hrDraw = m_textServices->TxDrawD2D(d2dDeviceContext.get(), &rc, nullptr, TXTVIEW_ACTIVE);
10581064
winrt::check_hresult(hrDraw);

vnext/Microsoft.ReactNative/Fabric/platform/react/renderer/graphics/Color.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,17 @@ winrt::Windows::UI::Color ResolvePlatformColor(Color const *const color) {
144144
}
145145

146146
D2D1::ColorF SharedColor::AsD2DColor() const {
147+
if (!m_color) {
148+
return D2D1::ColorF{0, 0.0};
149+
}
147150
winrt::Windows::UI::Color color = ResolvePlatformColor(m_color.get());
148151
return {color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f};
149152
}
150153

151154
winrt::Windows::UI::Color SharedColor::AsWindowsColor() const {
155+
if (!m_color) {
156+
return winrt::Windows::UI::Color{0, 0, 0, 0};
157+
}
152158
return ResolvePlatformColor(m_color.get());
153159
}
154160

0 commit comments

Comments
 (0)