mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-18 04:09:33 +01:00
fix(mobile): give a sideways drag on the terminal to the session swipe alone
The terminal's touch handler and the session swipe judged every gesture separately, so a sideways flick also ended as a tap on the session being left (a link, a CLI click, a keyboard toggle) or as a vertical scroll. The terminal now locks the dominant axis past the tap slop, hands a sideways drag it cannot pan to the swipe and publishes the axis, and the swipe stands aside for scrolls, pans, a lift the terminal consumed (its selection gesture), touch selections, a second finger and surfaces with controls of their own. A touch that starts outside the terminal resets the published axis, so swiping from the phone home screen into a session keeps working after a terminal scroll. A deliberate swipe gets 500ms instead of 300ms, and vertical drift cancels it mid-gesture instead of only at the lift.
This commit is contained in:
1 parent
39077281d1
commit
63cada938d
8 files changed
+221
-28
No files matched your search
@@ -758,14 +758,24 @@ const SwipeHandler = {
|
||||
startY: 0,
|
||||
startTime: 0,
|
||||
minSwipeDistance: 80, // Minimum pixels for a valid swipe
|
||||
maxSwipeTime: 300, // Maximum ms for a swipe gesture
|
||||
maxVerticalDrift: 100, // Max vertical movement allowed
|
||||
// 300ms rejected an unhurried but deliberate swipe outright, which read as an
|
||||
// unresponsive gesture rather than as "too slow".
|
||||
maxSwipeTime: 500, // Maximum ms for a swipe gesture
|
||||
maxVerticalDrift: 50, // Max vertical movement allowed, at any point of the gesture
|
||||
|
||||
_touchStartHandler: null,
|
||||
_touchMoveHandler: null,
|
||||
_touchEndHandler: null,
|
||||
_element: null,
|
||||
_ignoreGesture: false,
|
||||
|
||||
/**
|
||||
* Surfaces inside `.main` that own their own gestures or controls: a swipe
|
||||
* there must not switch sessions. The phone overview and welcome screen are
|
||||
* deliberately NOT listed: swiping from home into a session is a feature.
|
||||
*/
|
||||
IGNORE_SELECTOR: '.tile-grid, .term-select-bar, #cjkInput, .history-trunc-bar',
|
||||
|
||||
/** Initialize swipe handling */
|
||||
init() {
|
||||
// Only on touch devices
|
||||
@@ -776,8 +786,10 @@ const SwipeHandler = {
|
||||
|
||||
this._element = terminal;
|
||||
this._touchStartHandler = (e) => this.onTouchStart(e);
|
||||
this._touchMoveHandler = (e) => this.onTouchMove(e);
|
||||
this._touchEndHandler = (e) => this.onTouchEnd(e);
|
||||
terminal.addEventListener('touchstart', this._touchStartHandler, { passive: true });
|
||||
terminal.addEventListener('touchmove', this._touchMoveHandler, { passive: true });
|
||||
terminal.addEventListener('touchend', this._touchEndHandler, { passive: true });
|
||||
},
|
||||
|
||||
@@ -785,9 +797,11 @@ const SwipeHandler = {
|
||||
cleanup() {
|
||||
if (this._element && this._touchStartHandler) {
|
||||
this._element.removeEventListener('touchstart', this._touchStartHandler);
|
||||
this._element.removeEventListener('touchmove', this._touchMoveHandler);
|
||||
this._element.removeEventListener('touchend', this._touchEndHandler);
|
||||
}
|
||||
this._touchStartHandler = null;
|
||||
this._touchMoveHandler = null;
|
||||
this._touchEndHandler = null;
|
||||
this._element = null;
|
||||
},
|
||||
@@ -797,20 +811,43 @@ const SwipeHandler = {
|
||||
// here. Swiping across the open session drawer — the natural "dismiss it"
|
||||
// gesture — would otherwise fire nextSession() and drop the user into a
|
||||
// session they never tapped.
|
||||
// The terminal's touch handler publishes the axis it locks for a gesture
|
||||
// that starts on it; a touch that starts elsewhere in `.main` (the home
|
||||
// screen) must not inherit the last one, or a terminal scroll a moment
|
||||
// earlier vetoes a swipe from home. Reset before any early return.
|
||||
if (typeof app !== 'undefined') app._terminalGestureAxis = null;
|
||||
this._ignoreGesture = !!e.target?.closest?.('.session-sidebar');
|
||||
if (this._ignoreGesture) return;
|
||||
if (!e.touches || e.touches.length !== 1) return;
|
||||
// A second finger makes this a different gesture (pinch, two-finger scroll):
|
||||
// never measure a later lift against the first finger's start. Stays set
|
||||
// until the next touchstart, so the first finger's lift cannot clear it.
|
||||
if (!e.touches || e.touches.length !== 1) {
|
||||
this._ignoreGesture = true;
|
||||
return;
|
||||
}
|
||||
this._ignoreGesture = !!e.target?.closest?.(this.IGNORE_SELECTOR);
|
||||
this.startX = e.touches[0].clientX;
|
||||
this.startY = e.touches[0].clientY;
|
||||
this.startTime = Date.now();
|
||||
},
|
||||
|
||||
onTouchMove(e) {
|
||||
if (this._ignoreGesture || !e.touches || e.touches.length !== 1) return;
|
||||
if (Math.abs(e.touches[0].clientY - this.startY) > this.maxVerticalDrift) this._ignoreGesture = true;
|
||||
},
|
||||
|
||||
onTouchEnd(e) {
|
||||
if (this._ignoreGesture) {
|
||||
this._ignoreGesture = false;
|
||||
return;
|
||||
}
|
||||
if (this._ignoreGesture) return;
|
||||
if (!e.changedTouches || e.changedTouches.length !== 1) return;
|
||||
if (typeof app === 'undefined') return;
|
||||
// The terminal's own touch handler (terminal-ui.js) runs first, on the same
|
||||
// gesture: a drag it locked as a scroll or a pan is not a session swipe,
|
||||
// and neither is a lift it consumed (its long-press selection gesture
|
||||
// preventDefaults the touchend, even on blank space where no selection
|
||||
// survives the lift). A swipe must not extend or drop a touch selection.
|
||||
if (e.defaultPrevented) return;
|
||||
if (app._terminalGestureAxis === 'y' || app._terminalGestureAxis === 'x') return;
|
||||
if (app._touchSelecting || app._touchSelectionActive) return;
|
||||
|
||||
const endX = e.changedTouches[0].clientX;
|
||||
const endY = e.changedTouches[0].clientY;
|
||||
@@ -824,13 +861,8 @@ const SwipeHandler = {
|
||||
if (deltaY > this.maxVerticalDrift) return; // Too much vertical movement
|
||||
if (Math.abs(deltaX) < this.minSwipeDistance) return; // Too short
|
||||
|
||||
// Valid swipe detected
|
||||
if (deltaX > 0) {
|
||||
// Swipe right -> previous session
|
||||
if (typeof app !== 'undefined') app.prevSession();
|
||||
} else {
|
||||
// Swipe left -> next session
|
||||
if (typeof app !== 'undefined') app.nextSession();
|
||||
}
|
||||
// Valid swipe detected. Swipe right -> previous session, left -> next.
|
||||
if (deltaX > 0) app.prevSession();
|
||||
else app.nextSession();
|
||||
},
|
||||
};
|
||||
@@ -1196,8 +1196,13 @@ Object.assign(CodemanApp.prototype, {
|
||||
let longPressStartY = 0;
|
||||
let touchStartY = 0;
|
||||
let touchStartX = 0;
|
||||
// 'x' | 'y' | null — locked on the first travel past the tap slop, so a
|
||||
// diagonal drag cannot pan and scroll at the same time.
|
||||
// 'x' | 'y' | 'swipe' | null — locked on the first travel past the tap
|
||||
// slop, so a diagonal drag cannot pan and scroll at the same time. 'swipe'
|
||||
// is a sideways drag on a terminal that cannot pan: it is the session
|
||||
// swipe's (SwipeHandler, mobile-handlers.js), so it neither scrolls nor
|
||||
// ends as a tap on the session being left. Published per gesture as
|
||||
// `this._terminalGestureAxis`, which SwipeHandler reads to stand aside
|
||||
// for a scroll or a pan.
|
||||
let panAxis = null;
|
||||
/**
|
||||
* Can this gesture pan sideways? Only while the terminal is wider than
|
||||
@@ -1242,6 +1247,7 @@ Object.assign(CodemanApp.prototype, {
|
||||
touchStartY = touchLastY;
|
||||
touchStartX = touchLastX;
|
||||
panAxis = null;
|
||||
this._terminalGestureAxis = null;
|
||||
velocity = 0;
|
||||
pixelAccum = 0;
|
||||
isTouching = true;
|
||||
@@ -1315,10 +1321,10 @@ Object.assign(CodemanApp.prototype, {
|
||||
if (!didScroll) {
|
||||
const travelY = Math.abs(touchY - touchStartY);
|
||||
const travelX = Math.abs(touchX - touchStartX);
|
||||
const sideways = canPanHorizontally() && travelX >= TAP_THRESHOLD;
|
||||
if (travelY >= TAP_THRESHOLD || sideways) {
|
||||
if (travelY >= TAP_THRESHOLD || travelX >= TAP_THRESHOLD) {
|
||||
didScroll = true;
|
||||
panAxis = sideways && travelX > travelY ? 'x' : 'y';
|
||||
panAxis = travelX > travelY ? (canPanHorizontally() ? 'x' : 'swipe') : 'y';
|
||||
this._terminalGestureAxis = panAxis;
|
||||
}
|
||||
}
|
||||
// Below the tap threshold, treat the gesture as a potential tap:
|
||||
@@ -1329,6 +1335,7 @@ Object.assign(CodemanApp.prototype, {
|
||||
// fling, so a jittery tap would both position the cursor AND scroll.
|
||||
if (!didScroll) return;
|
||||
ev.preventDefault();
|
||||
if (panAxis === 'swipe') return;
|
||||
if (panAxis === 'x') {
|
||||
// Pan the container, and touch nothing the vertical path owns —
|
||||
// no pixelAccum, no velocity, so touchend cannot turn a sideways
|
||||
|
||||
Reference in new issue
Block a user