Skip to content

Commit bd5cbf4

Browse files
committed
Normalize wheel zoom for cross-platform smoothness
1 parent 7a341cd commit bd5cbf4

File tree

1 file changed

+20
-3
lines changed
  • ui/packages/shared/profile/src/ProfileFlameGraph/FlameGraphArrow

1 file changed

+20
-3
lines changed

ui/packages/shared/profile/src/ProfileFlameGraph/FlameGraphArrow/useZoom.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
import {useCallback, useEffect, useRef, useState} from 'react';
14+
import { useCallback, useEffect, useRef, useState } from 'react';
15+
16+
17+
18+
import { flushSync } from 'react-dom';
19+
20+
21+
1522

16-
import {flushSync} from 'react-dom';
1723

1824
const MIN_ZOOM = 1.0;
1925
const MAX_ZOOM = 20.0;
@@ -91,7 +97,18 @@ export const useZoom = (containerRef: React.RefObject<HTMLDivElement | null>): U
9197
const handleWheel = (e: WheelEvent): void => {
9298
if (!e.ctrlKey && !e.metaKey) return;
9399
e.preventDefault();
94-
const newZoom = clampZoom(zoomLevelRef.current * (1 - e.deltaY * WHEEL_ZOOM_SENSITIVITY));
100+
101+
let delta = e.deltaY;
102+
if (e.deltaMode === 1) {
103+
delta *= 20;
104+
}
105+
106+
// Limiting the max zoom step per event to 15%, so to fix the huge jumps in Linux OS.
107+
const MAX_FACTOR = 0.15;
108+
const rawFactor = -delta * WHEEL_ZOOM_SENSITIVITY;
109+
const zoomFactor = 1 + Math.max(-MAX_FACTOR, Math.min(MAX_FACTOR, rawFactor));
110+
111+
const newZoom = clampZoom(zoomLevelRef.current * zoomFactor);
95112
applyZoom(newZoom, e.clientX - container.getBoundingClientRect().left);
96113
};
97114

0 commit comments

Comments
 (0)