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

Commit c09d4f4

Browse files
authored
Merge pull request #5904 from SimonBrandner/normalize-wheel
Add a WheelEvent normalization function
2 parents 06726d3 + 2e6397d commit c09d4f4

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/components/views/elements/ImageView.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ import dis from '../../../dispatcher/dispatcher';
3232
import {replaceableComponent} from "../../../utils/replaceableComponent";
3333
import {RoomPermalinkCreator} from "../../../utils/permalinks/Permalinks"
3434
import {MatrixEvent} from "matrix-js-sdk/src/models/event";
35+
import {normalizeWheelEvent} from "../../../utils/Mouse";
3536

3637
const MIN_ZOOM = 100;
3738
const MAX_ZOOM = 300;
3839
// This is used for the buttons
3940
const ZOOM_STEP = 10;
4041
// This is used for mouse wheel events
41-
const ZOOM_COEFFICIENT = 7.5;
42+
const ZOOM_COEFFICIENT = 0.5;
4243
// If we have moved only this much we can zoom
4344
const ZOOM_DISTANCE = 10;
4445

@@ -115,7 +116,9 @@ export default class ImageView extends React.Component<IProps, IState> {
115116
private onWheel = (ev: WheelEvent) => {
116117
ev.stopPropagation();
117118
ev.preventDefault();
118-
const newZoom = this.state.zoom - (ev.deltaY * ZOOM_COEFFICIENT);
119+
120+
const {deltaY} = normalizeWheelEvent(ev);
121+
const newZoom = this.state.zoom - (deltaY * ZOOM_COEFFICIENT);
119122

120123
if (newZoom <= MIN_ZOOM) {
121124
this.setState({

src/utils/Mouse.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2021 Šimon Brandner <[email protected]>
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+
/**
18+
* Different browsers use different deltaModes. This causes different behaviour.
19+
* To avoid that we use this function to convert any event to pixels.
20+
* @param {WheelEvent} event to normalize
21+
* @returns {WheelEvent} normalized event event
22+
*/
23+
export function normalizeWheelEvent(event: WheelEvent): WheelEvent {
24+
const LINE_HEIGHT = 18;
25+
26+
let deltaX;
27+
let deltaY;
28+
let deltaZ;
29+
30+
if (event.deltaMode === 1) { // Units are lines
31+
deltaX = (event.deltaX * LINE_HEIGHT);
32+
deltaY = (event.deltaY * LINE_HEIGHT);
33+
deltaZ = (event.deltaZ * LINE_HEIGHT);
34+
} else {
35+
deltaX = event.deltaX;
36+
deltaY = event.deltaY;
37+
deltaZ = event.deltaZ;
38+
}
39+
40+
return new WheelEvent(
41+
"syntheticWheel",
42+
{
43+
deltaMode: 0,
44+
deltaY: deltaY,
45+
deltaX: deltaX,
46+
deltaZ: deltaZ,
47+
...event,
48+
},
49+
);
50+
}

0 commit comments

Comments
 (0)