Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 69a2563

Browse files
committed
Implement animations for progress bars
1 parent a97e505 commit 69a2563

File tree

4 files changed

+154
-3
lines changed

4 files changed

+154
-3
lines changed

src/components/views/elements/ProgressBar.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2020 The Matrix.org Foundation C.I.C.
2+
Copyright 2020,2022 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,13 +16,20 @@ limitations under the License.
1616

1717
import React from "react";
1818

19+
import { useSmoothAnimation } from "../../../hooks/useSmoothAnimation";
20+
1921
interface IProps {
2022
value: number;
2123
max: number;
24+
animated?: boolean;
2225
}
2326

24-
const ProgressBar: React.FC<IProps> = ({ value, max }) => {
25-
return <progress className="mx_ProgressBar" max={max} value={value} />;
27+
const PROGRESS_BAR_ANIMATION_DURATION = 300;
28+
const ProgressBar: React.FC<IProps> = ({ value, max, animated }) => {
29+
// Animating progress bars via CSS transition isn’t possible in all of our supported browsers yet.
30+
// As workaround, we’re using animations through JS requestAnimationFrame
31+
const currentValue = useSmoothAnimation(0, value, PROGRESS_BAR_ANIMATION_DURATION, animated);
32+
return <progress className="mx_ProgressBar" max={max} value={currentValue} />;
2633
};
2734

2835
export default ProgressBar;

src/hooks/useAnimation.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { logger } from "matrix-js-sdk/src/logger";
18+
import { useCallback, useEffect, useRef } from "react";
19+
20+
import SettingsStore from "../settings/SettingsStore";
21+
22+
const debuglog = (...args: any[]) => {
23+
if (SettingsStore.getValue("debug_animation")) {
24+
logger.log.call(console, "Animation debuglog:", ...args);
25+
}
26+
};
27+
28+
export function useAnimation(enabled: boolean, callback: (timestamp: DOMHighResTimeStamp) => boolean) {
29+
const handle = useRef<number | null>(null);
30+
31+
const handler = useCallback(
32+
(timestamp: DOMHighResTimeStamp) => {
33+
if (callback(timestamp)) {
34+
handle.current = requestAnimationFrame(handler);
35+
} else {
36+
debuglog("Finished animation!");
37+
}
38+
},
39+
[callback],
40+
);
41+
42+
useEffect(() => {
43+
debuglog("Started animation!");
44+
if (enabled) {
45+
handle.current = requestAnimationFrame(handler);
46+
}
47+
return () => {
48+
if (handle.current) {
49+
debuglog("Aborted animation!");
50+
cancelAnimationFrame(handle.current);
51+
handle.current = null;
52+
}
53+
};
54+
}, [enabled, handler]);
55+
}

src/hooks/useSmoothAnimation.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { logger } from "matrix-js-sdk/src/logger";
18+
import { useCallback, useEffect, useRef, useState } from "react";
19+
20+
import SettingsStore from "../settings/SettingsStore";
21+
import { useAnimation } from "./useAnimation";
22+
23+
const debuglog = (...args: any[]) => {
24+
if (SettingsStore.getValue("debug_animation")) {
25+
logger.log.call(console, "Animation debuglog:", ...args);
26+
}
27+
};
28+
29+
/**
30+
* Utility function to smoothly animate to a certain target value
31+
* @param initialValue Initial value to be used as initial starting point
32+
* @param targetValue Desired value to animate to (can be changed repeatedly to whatever is current at that time)
33+
* @param duration Duration that each animation should take
34+
* @param enabled Whether the animation should run or not
35+
*/
36+
export function useSmoothAnimation(
37+
initialValue: number,
38+
targetValue: number,
39+
duration: number,
40+
enabled: boolean,
41+
): number {
42+
const state = useRef<{ timestamp: DOMHighResTimeStamp | null, value: number }>({
43+
timestamp: null,
44+
value: initialValue,
45+
});
46+
const [currentValue, setCurrentValue] = useState<number>(initialValue);
47+
const [currentStepSize, setCurrentStepSize] = useState<number>(0);
48+
49+
useEffect(() => {
50+
const totalDelta = targetValue - state.current.value;
51+
setCurrentStepSize(totalDelta / duration);
52+
state.current = { ...state.current, timestamp: null };
53+
}, [duration, targetValue]);
54+
55+
const update = useCallback(
56+
(timestamp: DOMHighResTimeStamp): boolean => {
57+
if (!state.current.timestamp) {
58+
state.current = { ...state.current, timestamp };
59+
return true;
60+
}
61+
62+
if (Math.abs(currentStepSize) < Number.EPSILON) {
63+
return false;
64+
}
65+
66+
const timeDelta = timestamp - state.current.timestamp;
67+
const valueDelta = currentStepSize * timeDelta;
68+
const maxValueDelta = targetValue - state.current.value;
69+
const clampedValueDelta = Math.sign(valueDelta) * Math.min(Math.abs(maxValueDelta), Math.abs(valueDelta));
70+
const value = state.current.value + clampedValueDelta;
71+
72+
debuglog(`Animating to ${targetValue} at ${value} timeDelta=${timeDelta}, valueDelta=${valueDelta}`);
73+
74+
setCurrentValue(value);
75+
state.current = { value, timestamp };
76+
77+
return Math.abs(maxValueDelta) > Number.EPSILON;
78+
},
79+
[currentStepSize, targetValue],
80+
);
81+
82+
useAnimation(enabled, update);
83+
84+
return currentValue;
85+
}

src/settings/Settings.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,10 @@ export const SETTINGS: {[setting: string]: ISetting} = {
952952
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS,
953953
default: false,
954954
},
955+
"debug_animation": {
956+
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS,
957+
default: false,
958+
},
955959
"audioInputMuted": {
956960
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS,
957961
default: false,

0 commit comments

Comments
 (0)