Skip to content

Commit 8fc0f91

Browse files
authored
[Fabric] Add switch's 'trackColor' (#11234)
* initial solution * restructure code * Change files * address feedback * format
1 parent f314377 commit 8fc0f91

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
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": "add switch 'trackColor' for Fabric",
4+
"packageName": "react-native-windows",
5+
"email": "tatianakapos@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/SwitchComponentView.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,29 @@ void SwitchComponentView::Draw() noexcept {
143143
d2dDeviceContext->GetDpi(&oldDpiX, &oldDpiY);
144144
d2dDeviceContext->SetDpi(dpi, dpi);
145145

146+
// switch track - outline
147+
D2D1_ROUNDED_RECT track = D2D1::RoundedRect(trackRect, trackCornerRadius, trackCornerRadius);
148+
if ((!switchProps->onTintColor && switchProps->value) || (!switchProps->tintColor && !switchProps->value)) {
149+
d2dDeviceContext->DrawRoundedRectangle(track, defaultBrush.get());
150+
}
151+
152+
// switch track - fill
153+
winrt::com_ptr<ID2D1SolidColorBrush> trackBrush;
154+
if (!switchProps->disabled && switchProps->onTintColor && switchProps->value) {
155+
winrt::check_hresult(
156+
d2dDeviceContext->CreateSolidColorBrush(switchProps->onTintColor.AsD2DColor(), trackBrush.put()));
157+
d2dDeviceContext->FillRoundedRectangle(track, trackBrush.get());
158+
} else if (!switchProps->disabled && switchProps->tintColor && !switchProps->value) {
159+
winrt::check_hresult(
160+
d2dDeviceContext->CreateSolidColorBrush(switchProps->tintColor.AsD2DColor(), trackBrush.put()));
161+
d2dDeviceContext->FillRoundedRectangle(track, trackBrush.get());
162+
}
163+
146164
// switch thumb
147165
D2D1_POINT_2F thumbCenter = D2D1 ::Point2F(thumbX, (trackRect.top + trackRect.bottom) / 2);
148166
D2D1_ELLIPSE thumb = D2D1::Ellipse(thumbCenter, thumbRadius, thumbRadius);
149167
d2dDeviceContext->FillEllipse(thumb, thumbBrush.get());
150168

151-
// switch track
152-
D2D1_ROUNDED_RECT track = D2D1::RoundedRect(trackRect, trackCornerRadius, trackCornerRadius);
153-
d2dDeviceContext->DrawRoundedRectangle(track, defaultBrush.get());
154-
155169
// Restore old dpi setting
156170
d2dDeviceContext->SetDpi(oldDpiX, oldDpiY);
157171

@@ -237,4 +251,4 @@ bool SwitchComponentView::focusable() const noexcept {
237251
return m_props->focusable;
238252
}
239253

240-
} // namespace Microsoft::ReactNative
254+
} // namespace Microsoft::ReactNative

0 commit comments

Comments
 (0)