@@ -21,8 +21,10 @@ type SwipeGesture =
2121 bounds : SwipeBounds ;
2222 origin : EventTarget | undefined ;
2323 }
24- // Not a swipe, or one already decided: the rest of its events are ignored.
25- | { kind : "ignored" } ;
24+ // Not a swipe: the rest of its events are ignored.
25+ | { kind : "ignored" }
26+ // A decided history swipe. Its momentum is ignored, but content can still claim it.
27+ | { kind : "released" ; origin : EventTarget | undefined } ;
2628type SwipeIndicatorState = {
2729 direction : HistoryDirection ;
2830 progress : number ;
@@ -53,7 +55,7 @@ function isInHorizontalScroller(path: readonly EventTarget[]): boolean {
5355function lockSwipeAxis (
5456 pending : Extract < SwipeGesture , { kind : "pending" } > ,
5557 origin : EventTarget | undefined ,
56- ) : Exclude < SwipeGesture , { kind : "pending " } > {
58+ ) : Extract < SwipeGesture , { kind : "ignored" | "tracking " } > {
5759 const { path, sumX, sumY } = pending ;
5860 if ( Math . abs ( sumX ) <= Math . abs ( sumY ) ) return { kind : "ignored" } ;
5961 if ( path . some ( ( node ) => node instanceof Element && node . matches ( '[role="dialog"]' ) ) ) {
@@ -123,19 +125,17 @@ export function HistorySwipeNavigation() {
123125 if ( ! [ "Alt" , "Control" , "Meta" , "Shift" ] . includes ( event . key ) ) resetContext ( ) ;
124126 } ;
125127 // The fingers lifted: navigate if the swipe went far enough, then ignore its momentum.
126- const release = ( ) => {
127- if ( gesture ?. kind === "tracking" && gesture . distance >= SWIPE_TRIGGER_DISTANCE ) {
128- navigateHistory ( gesture . direction ) ;
129- }
130- gesture = { kind : "ignored" } ;
128+ const release = ( swipe : Extract < SwipeGesture , { kind : "tracking" } > ) => {
129+ if ( swipe . distance >= SWIPE_TRIGGER_DISTANCE ) navigateHistory ( swipe . direction ) ;
130+ gesture = { kind : "released" , origin : swipe . origin } ;
131131 hideIndicator ( ) ;
132132 } ;
133133 // Chromium brackets each trackpad scroll, momentum included, with these.
134134 const unsubscribeScrollBegin = window . desktopBridge ?. onTrackpadScrollBegin ( ( ) => {
135- if ( gesture ?. kind === "ignored" ) gesture = null ;
135+ if ( gesture ?. kind === "ignored" || gesture ?. kind === "released" ) gesture = null ;
136136 } ) ;
137137 const unsubscribeScrollEnd = window . desktopBridge ?. onTrackpadScrollEnd ( ( ) => {
138- if ( gesture ?. kind === "tracking" ) release ( ) ;
138+ if ( gesture ?. kind === "tracking" ) release ( gesture ) ;
139139 gesture = null ;
140140 } ) ;
141141 const onWheel = ( event : WheelEvent ) => {
@@ -152,16 +152,22 @@ export function HistorySwipeNavigation() {
152152 : { kind : "pending" , path : event . composedPath ( ) , y : event . clientY , sumX : 0 , sumY : 0 } ;
153153 }
154154 if ( gesture . kind === "ignored" ) return ;
155- if ( gesture . kind === "tracking" ) {
156- const delta = gesture . direction === "back" ? - event . deltaX : event . deltaX ;
157- if ( event . deltaX === 0 && event . deltaY === 0 ) return release ( ) ;
158- // Content can appear under a still pointer while a stale trailing wheel is tracking.
155+ if ( gesture . kind !== "pending" ) {
156+ if ( event . deltaX === 0 && event . deltaY === 0 ) {
157+ if ( gesture . kind === "tracking" ) release ( gesture ) ;
158+ return ;
159+ }
160+ // Content can appear under a still pointer before a history swipe's native end.
159161 // Only a new wheel target is classified, so overflow gained mid-swipe stays history.
160162 const path = event . composedPath ( ) ;
161163 if ( path [ 0 ] !== gesture . origin ) {
162164 if ( isInHorizontalScroller ( path ) ) return claimContent ( ) ;
163165 gesture . origin = path [ 0 ] ;
164166 }
167+ }
168+ if ( gesture . kind === "released" ) return ;
169+ if ( gesture . kind === "tracking" ) {
170+ const delta = gesture . direction === "back" ? - event . deltaX : event . deltaX ;
165171 const { direction, distance, y } = gesture ;
166172 // Past its start the swipe picks a direction again: a stale trailing wheel may
167173 // have opened it, and the next swipe's begin can arrive after its first wheels.
@@ -176,7 +182,7 @@ export function HistorySwipeNavigation() {
176182 hideIndicator ( ) ;
177183 } else {
178184 gesture . deltas = [ ...gesture . deltas . slice ( 1 - MOMENTUM_SAMPLE_SIZE ) , delta ] ;
179- if ( isMomentumTail ( gesture . deltas ) ) return release ( ) ;
185+ if ( isMomentumTail ( gesture . deltas ) ) return release ( gesture ) ;
180186 gesture . distance = Math . max ( 0 , distance + delta ) ;
181187 }
182188 }
0 commit comments