Skip to content

Commit 5d4edf1

Browse files
authored
fix(desktop): lock horizontal webview pan (Magic Mouse side-scroll) (#1480)
Signed-off-by: Thomas Petersen <[email protected]>
1 parent 228122f commit 5d4edf1

3 files changed

Lines changed: 128 additions & 22 deletions

File tree

‎desktop/src/shared/hooks/useWebviewScrollBoundaryLock.ts‎

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ function isScrollableY(element: HTMLElement) {
2525
return element.scrollHeight > element.clientHeight + BOUNDARY_EPSILON_PX;
2626
}
2727

28+
function isScrollableX(element: HTMLElement) {
29+
if (isDocumentElement(element)) {
30+
return false;
31+
}
32+
33+
const style = window.getComputedStyle(element);
34+
if (!SCROLLABLE_OVERFLOW_VALUES.has(style.overflowX)) {
35+
return false;
36+
}
37+
38+
return element.scrollWidth > element.clientWidth + BOUNDARY_EPSILON_PX;
39+
}
40+
2841
function canScrollY(element: HTMLElement, deltaY: number) {
2942
if (deltaY < 0) {
3043
return element.scrollTop > BOUNDARY_EPSILON_PX;
@@ -34,6 +47,15 @@ function canScrollY(element: HTMLElement, deltaY: number) {
3447
return element.scrollTop < maxScrollTop - BOUNDARY_EPSILON_PX;
3548
}
3649

50+
function canScrollX(element: HTMLElement, deltaX: number) {
51+
if (deltaX < 0) {
52+
return element.scrollLeft > BOUNDARY_EPSILON_PX;
53+
}
54+
55+
const maxScrollLeft = element.scrollWidth - element.clientWidth;
56+
return element.scrollLeft < maxScrollLeft - BOUNDARY_EPSILON_PX;
57+
}
58+
3759
function isConversationScroller(element: HTMLElement) {
3860
return Boolean(element.closest(CONVERSATION_SCROLL_SELECTOR));
3961
}
@@ -44,39 +66,63 @@ function isConversationScroller(element: HTMLElement) {
4466
* Buzz is laid out as fixed-height nested panes. On macOS, a wheel/trackpad
4567
* gesture that starts over a non-scrollable pane (or over a scrollable pane at
4668
* its boundary) can still be handed to the WKWebView viewport, which rubber-
47-
* bands the entire app and reveals a blank strip above/below the UI. CSS
69+
* bands the entire app and reveals a blank strip beside the UI. CSS
4870
* `overscroll-behavior` is not enough for all of the empty/header/footer hit
4971
* targets in the webview, so this capture listener consumes only gestures that
50-
* otherwise have nowhere app-local to scroll.
72+
* otherwise have nowhere app-local to scroll. Both axes are locked: vertical
73+
* and horizontal pans are each checked against containers that can actually
74+
* move in that direction.
5175
*
5276
* Real scrolling is left alone: if any scroll container under the pointer can
53-
* move in the wheel direction, the browser handles it normally. At boundaries,
54-
* only containers marked with `data-buzz-conversation-scroll` are allowed to
55-
* receive the gesture so their own local elastic affordance can remain; every
56-
* other boundary is locked and cannot chain to the viewport.
77+
* move in the wheel direction, the browser handles it normally. At vertical
78+
* boundaries, only containers marked with `data-buzz-conversation-scroll` are
79+
* allowed to receive the gesture so their own local elastic affordance can
80+
* remain; every other boundary — including all horizontal ones — is locked and
81+
* cannot chain to the viewport.
5782
*/
5883
export function useWebviewScrollBoundaryLock() {
5984
React.useEffect(() => {
6085
function handleWheel(event: WheelEvent) {
61-
if (event.defaultPrevented || event.deltaY === 0 || event.ctrlKey) {
86+
if (event.defaultPrevented || event.ctrlKey) {
87+
return;
88+
}
89+
90+
const { deltaX, deltaY } = event;
91+
if (deltaX === 0 && deltaY === 0) {
6292
return;
6393
}
6494

6595
const path = event.composedPath();
6696
let firstScrollable: HTMLElement | null = null;
6797

6898
for (const target of path) {
69-
if (!isHTMLElement(target) || !isScrollableY(target)) {
99+
if (!isHTMLElement(target)) {
100+
continue;
101+
}
102+
103+
const scrollableY = deltaY !== 0 && isScrollableY(target);
104+
const scrollableX = deltaX !== 0 && isScrollableX(target);
105+
if (!scrollableY && !scrollableX) {
70106
continue;
71107
}
72108

73109
firstScrollable ??= target;
74-
if (canScrollY(target, event.deltaY)) {
110+
if (
111+
(scrollableY && canScrollY(target, deltaY)) ||
112+
(scrollableX && canScrollX(target, deltaX))
113+
) {
75114
return;
76115
}
77116
}
78117

79-
if (firstScrollable && isConversationScroller(firstScrollable)) {
118+
// Only the vertical elastic affordance of conversation scrollers is
119+
// preserved; a predominantly horizontal gesture must never pan the
120+
// webview, even over a conversation pane.
121+
if (
122+
firstScrollable &&
123+
isConversationScroller(firstScrollable) &&
124+
Math.abs(deltaY) >= Math.abs(deltaX)
125+
) {
80126
return;
81127
}
82128

‎desktop/src/shared/styles/globals/theme.css‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@
111111
width: 100%;
112112
}
113113

114+
/*
115+
* The app is a fixed-height shell — the document itself must never scroll
116+
* or rubber-band. `overscroll-behavior: none` stops scroll chaining from
117+
* inner containers reaching the viewport on both axes (the Magic Mouse
118+
* horizontal pan escaped through here), and `overflow: hidden` removes any
119+
* document-level scrollability. useWebviewScrollBoundaryLock handles the
120+
* wheel-event side; this is the CSS backstop.
121+
*/
122+
html,
123+
body {
124+
overflow: hidden;
125+
overscroll-behavior: none;
126+
}
127+
114128
mark {
115129
background-color: transparent;
116130
color: inherit;

‎desktop/tests/e2e/overscroll-boundary.spec.ts‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { installMockBridge } from "../helpers/bridge";
55
async function dispatchWheelPrevented(
66
page: import("@playwright/test").Page,
77
selector: string,
8-
deltaY: number,
8+
deltas: { deltaX?: number; deltaY?: number },
99
) {
1010
return page.evaluate(
11-
({ selector, deltaY }) => {
11+
({ selector, deltaX, deltaY }) => {
1212
const element = document.querySelector(selector);
1313
if (!element) {
1414
throw new Error(`Missing element for selector: ${selector}`);
@@ -17,12 +17,13 @@ async function dispatchWheelPrevented(
1717
const event = new WheelEvent("wheel", {
1818
bubbles: true,
1919
cancelable: true,
20+
deltaX,
2021
deltaY,
2122
});
2223
element.dispatchEvent(event);
2324
return event.defaultPrevented;
2425
},
25-
{ selector, deltaY },
26+
{ selector, deltaX: deltas.deltaX ?? 0, deltaY: deltas.deltaY ?? 0 },
2627
);
2728
}
2829

@@ -38,23 +39,68 @@ test("locks viewport rubber-band outside conversation scrollers", async ({
3839
await expect(page.getByTestId("message-timeline")).toBeVisible();
3940

4041
await expect(
41-
dispatchWheelPrevented(page, '[data-testid="app-top-chrome"]', -120),
42+
dispatchWheelPrevented(page, '[data-testid="app-top-chrome"]', {
43+
deltaY: -120,
44+
}),
4245
).resolves.toBe(true);
4346
await expect(
44-
dispatchWheelPrevented(page, '[data-testid="sidebar-pinned-header"]', -120),
47+
dispatchWheelPrevented(page, '[data-testid="sidebar-pinned-header"]', {
48+
deltaY: -120,
49+
}),
4550
).resolves.toBe(true);
4651
await expect(
47-
dispatchWheelPrevented(
48-
page,
49-
'[data-testid="app-sidebar-scroll-anchor"]',
50-
-120,
51-
),
52+
dispatchWheelPrevented(page, '[data-testid="app-sidebar-scroll-anchor"]', {
53+
deltaY: -120,
54+
}),
5255
).resolves.toBe(true);
5356
await expect(
54-
dispatchWheelPrevented(page, '[data-testid="chat-title"]', -120),
57+
dispatchWheelPrevented(page, '[data-testid="chat-title"]', {
58+
deltaY: -120,
59+
}),
5560
).resolves.toBe(true);
5661

5762
await expect(
58-
dispatchWheelPrevented(page, '[data-testid="message-timeline"]', -120),
63+
dispatchWheelPrevented(page, '[data-testid="message-timeline"]', {
64+
deltaY: -120,
65+
}),
66+
).resolves.toBe(false);
67+
});
68+
69+
test("locks horizontal viewport pan everywhere", async ({ page }) => {
70+
await page.goto("/");
71+
await page.getByTestId("channel-general").click();
72+
await expect(page.getByTestId("message-timeline")).toBeVisible();
73+
74+
for (const deltaX of [-120, 120]) {
75+
await expect(
76+
dispatchWheelPrevented(page, '[data-testid="app-top-chrome"]', {
77+
deltaX,
78+
}),
79+
).resolves.toBe(true);
80+
await expect(
81+
dispatchWheelPrevented(page, '[data-testid="sidebar-pinned-header"]', {
82+
deltaX,
83+
}),
84+
).resolves.toBe(true);
85+
await expect(
86+
dispatchWheelPrevented(page, '[data-testid="chat-title"]', { deltaX }),
87+
).resolves.toBe(true);
88+
89+
// Unlike vertical, horizontal pans over the conversation pane are locked
90+
// too — there is no horizontal elastic affordance.
91+
await expect(
92+
dispatchWheelPrevented(page, '[data-testid="message-timeline"]', {
93+
deltaX,
94+
}),
95+
).resolves.toBe(true);
96+
}
97+
98+
// A predominantly vertical gesture with slight horizontal drift still
99+
// reaches the conversation scroller.
100+
await expect(
101+
dispatchWheelPrevented(page, '[data-testid="message-timeline"]', {
102+
deltaX: -10,
103+
deltaY: -120,
104+
}),
59105
).resolves.toBe(false);
60106
});

0 commit comments

Comments
 (0)