Skip to content

Commit 0ab2ba1

Browse files
authored
[Fabric] Implement hidesWhenStopped in ActivityIndicator (#14615)
1 parent cb69407 commit 0ab2ba1

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
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": "[Fabric] Implement hidesWhenStopped in ActivityIndicator",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/playground/Samples/index.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export default class Bootstrap extends React.Component<
141141
switchIsOn: boolean;
142142
highlightPressed: boolean;
143143
mouseEntered: boolean;
144+
isAnimating: boolean;
144145
}
145146
> {
146147
constructor(props: {}) {
@@ -151,11 +152,18 @@ export default class Bootstrap extends React.Component<
151152
mouseEntered: false,
152153
switchIsOn: true,
153154
highlightPressed: false,
155+
isAnimating: false,
154156
};
155157
}
156158

157159
inputRef: React.RefObject<TextInput | null>;
158160

161+
toggleAnimation = () => {
162+
this.setState(prevState => ({
163+
isAnimating: !prevState.isAnimating,
164+
}));
165+
};
166+
159167
render() {
160168
return (
161169
<ScrollView>
@@ -199,7 +207,26 @@ export default class Bootstrap extends React.Component<
199207
</View>
200208
</ScrollView>
201209
<TicTacButton />
202-
<ActivityIndicator size="large" color="green" />
210+
<ActivityIndicator
211+
size="large"
212+
color="green"
213+
animating={this.state.isAnimating}
214+
hidesWhenStopped={false}
215+
/>
216+
<ActivityIndicator
217+
size="large"
218+
color="green"
219+
animating={this.state.isAnimating}
220+
hidesWhenStopped={true}
221+
/>
222+
<View>
223+
<Button
224+
title={
225+
this.state.isAnimating ? 'Stop Animation' : 'Start Animation'
226+
}
227+
onPress={this.toggleAnimation}
228+
/>
229+
</View>
203230
<Text style={{marginTop: 15}}>Big Border & Clipping Tests:</Text>
204231
<View style={{flexDirection: 'row'}}>
205232
<View style={{flexDirection: 'column', alignItems: 'center'}}>

vnext/Microsoft.ReactNative/CompositionSwitcher.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ namespace Microsoft.ReactNative.Composition.Experimental
127127
{
128128
void Size(Single value);
129129
void Brush(IBrush brush);
130+
void StartAnimation();
131+
void StopAnimation();
130132
}
131133

132134
[webhosthidden]

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,18 @@ void ActivityIndicatorComponentView::updateProps(
6767
updateProgressColor(newViewProps->color);
6868
}
6969

70-
if (newViewProps->animating != oldViewProps->animating) {
71-
m_ActivityIndicatorVisual.IsVisible(newViewProps->animating);
70+
if (newViewProps->animating != oldViewProps->animating ||
71+
newViewProps->hidesWhenStopped != oldViewProps->hidesWhenStopped) {
72+
bool setHidden = (newViewProps->hidesWhenStopped && !newViewProps->animating);
73+
m_ActivityIndicatorVisual.IsVisible(!setHidden);
74+
75+
if (!newViewProps->animating && !newViewProps->hidesWhenStopped) {
76+
m_animationStopped = true;
77+
m_ActivityIndicatorVisual.StopAnimation();
78+
} else if (m_animationStopped) {
79+
m_ActivityIndicatorVisual.StartAnimation();
80+
m_animationStopped = false;
81+
}
7282
}
7383
}
7484

vnext/Microsoft.ReactNative/Fabric/Composition/ActivityIndicatorComponentView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ struct ActivityIndicatorComponentView
5353
void updateVisualSize() noexcept;
5454
void updateProgressColor(const facebook::react::SharedColor &color) noexcept;
5555

56+
bool m_animationStopped{false};
57+
5658
winrt::Microsoft::ReactNative::Composition::Experimental::IActivityVisual m_ActivityIndicatorVisual{nullptr};
5759
};
5860

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,16 +1389,7 @@ struct CompActivityVisual : winrt::implements<
13891389
_themeProperties.InsertVector4(L"Foreground", ColorAsVector4({0, 0, 0, 0}));
13901390

13911391
visual.Children().InsertAtTop(Root());
1392-
1393-
auto easing = _c.CreateLinearEasingFunction();
1394-
const auto progressAnimation = _c.CreateScalarKeyFrameAnimation();
1395-
progressAnimation.Duration(winrt::Windows::Foundation::TimeSpan{c_durationTicks});
1396-
progressAnimation.InsertKeyFrame(0.0f, 0.0f);
1397-
progressAnimation.InsertKeyFrame(1.0f, 1.0f, easing);
1398-
progressAnimation.IterationBehavior(TTypeRedirects::AnimationIterationBehavior::Forever);
1399-
1400-
_root.Properties().StartAnimation(L"Progress", progressAnimation);
1401-
_root.StartAnimation(L"Progress", progressAnimation);
1392+
StartAnimation();
14021393
}
14031394

14041395
void Brush(winrt::Microsoft::ReactNative::Composition::Experimental::IBrush brush) noexcept {
@@ -1524,6 +1515,24 @@ struct CompActivityVisual : winrt::implements<
15241515
SetAnimationClass<TTypeRedirects>(value, m_visual);
15251516
}
15261517

1518+
void StartAnimation() noexcept {
1519+
auto easing = _c.CreateLinearEasingFunction();
1520+
const auto progressAnimation = _c.CreateScalarKeyFrameAnimation();
1521+
progressAnimation.Duration(winrt::Windows::Foundation::TimeSpan{c_durationTicks});
1522+
progressAnimation.InsertKeyFrame(0.0f, 0.0f);
1523+
progressAnimation.InsertKeyFrame(1.0f, 1.0f, easing);
1524+
progressAnimation.IterationBehavior(TTypeRedirects::AnimationIterationBehavior::Forever);
1525+
1526+
_root.Properties().StartAnimation(L"Progress", progressAnimation);
1527+
_root.StartAnimation(L"Progress", progressAnimation);
1528+
}
1529+
1530+
void StopAnimation() noexcept {
1531+
_root.Properties().InsertScalar(L"Progress", 0.7f);
1532+
_root.Properties().StopAnimation(L"Progress");
1533+
_root.StopAnimation(L"Progress");
1534+
}
1535+
15271536
private:
15281537
typename TTypeRedirects::SpriteVisual m_visual{nullptr};
15291538
typename TTypeRedirects::SpriteVisual m_contentVisual{nullptr};

0 commit comments

Comments
 (0)