Skip to content

Commit

Permalink
[Fabric] Add switch's 'trackColor' (#11234)
Browse files Browse the repository at this point in the history
* initial solution

* restructure code

* Change files

* address feedback

* format
  • Loading branch information
TatianaKapos committed Mar 28, 2023
1 parent f314377 commit 8fc0f91
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "add switch 'trackColor' for Fabric",
"packageName": "react-native-windows",
"email": "tatianakapos@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,29 @@ void SwitchComponentView::Draw() noexcept {
d2dDeviceContext->GetDpi(&oldDpiX, &oldDpiY);
d2dDeviceContext->SetDpi(dpi, dpi);

// switch track - outline
D2D1_ROUNDED_RECT track = D2D1::RoundedRect(trackRect, trackCornerRadius, trackCornerRadius);
if ((!switchProps->onTintColor && switchProps->value) || (!switchProps->tintColor && !switchProps->value)) {
d2dDeviceContext->DrawRoundedRectangle(track, defaultBrush.get());
}

// switch track - fill
winrt::com_ptr<ID2D1SolidColorBrush> trackBrush;
if (!switchProps->disabled && switchProps->onTintColor && switchProps->value) {
winrt::check_hresult(
d2dDeviceContext->CreateSolidColorBrush(switchProps->onTintColor.AsD2DColor(), trackBrush.put()));
d2dDeviceContext->FillRoundedRectangle(track, trackBrush.get());
} else if (!switchProps->disabled && switchProps->tintColor && !switchProps->value) {
winrt::check_hresult(
d2dDeviceContext->CreateSolidColorBrush(switchProps->tintColor.AsD2DColor(), trackBrush.put()));
d2dDeviceContext->FillRoundedRectangle(track, trackBrush.get());
}

// switch thumb
D2D1_POINT_2F thumbCenter = D2D1 ::Point2F(thumbX, (trackRect.top + trackRect.bottom) / 2);
D2D1_ELLIPSE thumb = D2D1::Ellipse(thumbCenter, thumbRadius, thumbRadius);
d2dDeviceContext->FillEllipse(thumb, thumbBrush.get());

// switch track
D2D1_ROUNDED_RECT track = D2D1::RoundedRect(trackRect, trackCornerRadius, trackCornerRadius);
d2dDeviceContext->DrawRoundedRectangle(track, defaultBrush.get());

// Restore old dpi setting
d2dDeviceContext->SetDpi(oldDpiX, oldDpiY);

Expand Down Expand Up @@ -237,4 +251,4 @@ bool SwitchComponentView::focusable() const noexcept {
return m_props->focusable;
}

} // namespace Microsoft::ReactNative
} // namespace Microsoft::ReactNative

0 comments on commit 8fc0f91

Please sign in to comment.