Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve mobile sidebar swipe gestures https://github.com/siyuan-note/siyuan/issues/19644

Daniel9ae975a54dc8
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index 3454d0d2a2..cbeb700877 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -16,6 +16,7 @@ import {Menus} from "../menus";
import {addBaseURL, parseSiYuanUriInfo, setNoteBook} from "../util/pathName";
import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";
import {
+ handleTouchCancel,
handleTouchEnd,
handleTouchMove,
handleTouchSelectionChange,
@@ -240,7 +241,7 @@ class App {
document.addEventListener("touchstart", handleTouchStart, false);
document.addEventListener("touchmove", handleTouchMove, false);
document.addEventListener("touchend", handleTouchEnd, false);
- document.addEventListener("touchcancel", handleTouchEnd, false);
+ document.addEventListener("touchcancel", handleTouchCancel, false);
document.addEventListener("selectionchange", handleTouchSelectionChange, true);
window.addEventListener("nativePhysicalTouchUp", handleTouchUp, false);
window.addEventListener("keyup", () => {
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index 86babeee1e..824b151848 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -25,13 +25,14 @@ import {getMobileBlockSelectionElement} from "./blockSelection";
import {updateMultiSelectToolbar} from "./multiSelectToolbar";
import {
getOpeningSidebar,
- getOpenSidebarReleaseAction,
getSidebarClosingOffset,
getSidebarOpeningOffset,
+ MOBILE_SIDEBAR_SWIPE_ACTIVATION_DISTANCE,
type MobileSidebarSide,
type MobileSwipeDirection,
setSidebarSwipeState,
shouldCloseGlobalMenu,
+ shouldCommitSidebarSwipe,
shouldDragOpenSidebar,
} from "./touchPanelGesture";
@@ -45,6 +46,7 @@ let firstXY: "x" | "y";
let lastClientX: number; // 和起始方向不一致时,记录最后一次的 clientX
let scrollBlock: boolean;
let isFirstMove = true;
+let swipeStartSidebar: MobileSidebarSide;
// 长按进入多选的定时器
let longPressTimer: number;
let longPressBlockElement: HTMLElement;
@@ -225,6 +227,9 @@ export const handleTouchEnd = (event: TouchEvent) => {
window.siyuan.mobile.editor.protyle.contentElement.style.overflow = "";
}
+ const finalXDiff = Math.floor(clientX - event.changedTouches[0].clientX);
+ const finalYDiff = Math.floor(clientY - event.changedTouches[0].clientY);
+
// 有些事件不经过 touchstart 和 touchmove,因此需设置为 null 不再继续执行
clientX = null;
// 有些事件不经过 touchmove
@@ -266,15 +271,15 @@ export const handleTouchEnd = (event: TouchEvent) => {
return;
}
- let scrollEnable = false;
- if (Date.now() - time < 1000) {
- scrollEnable = true;
- } else if (Math.abs(xDiff) > window.innerWidth / 3) {
- scrollEnable = true;
- }
+ const commitSidebarSwipe = Math.abs(finalXDiff) > Math.abs(finalYDiff) && shouldCommitSidebarSwipe(
+ firstDirection,
+ finalXDiff,
+ currentTime - time,
+ window.innerWidth,
+ );
if (targetSidebar) {
- if (isXScroll && getOpenSidebarReleaseAction(targetSidebar, firstDirection, reversing) === "close") {
+ if (commitSidebarSwipe) {
closePanel();
} else {
popSidebar(targetSidebar, false);
@@ -284,16 +289,39 @@ export const handleTouchEnd = (event: TouchEvent) => {
if (!getMobileSidebarConfig().sidebarSwipe) {
return;
}
- if (!scrollEnable || !isXScroll) {
+ if (!commitSidebarSwipe) {
closePanel();
return;
}
- if (reversing) {
- closePanel();
- } else {
- popSidebar(getOpeningSidebar(firstDirection));
+ popSidebar(getOpeningSidebar(firstDirection));
+};
+
+export const handleTouchCancel = () => {
+ updateSidebarSwipeState();
+ if (!isFirstMove) {
+ if (swipeStartSidebar) {
+ popSidebar(swipeStartSidebar, false);
+ } else {
+ closePanel();
+ }
+ }
+ if (window.siyuan.mobile.editor) {
+ window.siyuan.mobile.editor.protyle.contentElement.style.overflow = "";
}
+ isFirstMove = true;
+ clientX = null;
+ clientY = null;
+ xDiff = undefined;
+ yDiff = undefined;
+ firstDirection = undefined;
+ firstXY = undefined;
+ lastClientX = undefined;
+ previousClientX = undefined;
+ swipeStartSidebar = undefined;
+ scrollBlock = false;
+ clearLongPress();
+ handleTouchUp();
};
export const handleTouchStart = (event: TouchEvent) => {
@@ -302,6 +330,7 @@ export const handleTouchStart = (event: TouchEvent) => {
longPressBlockElement = undefined;
longPressTouchRange = undefined;
const target = event.touches[0].target as HTMLElement;
+ swipeStartSidebar = getTargetSidebar(target);
if (0 < event.touches.length && (target.tagName === "VIDEO" || target.tagName === "AUDIO")) {
// https://github.com/siyuan-note/siyuan/issues/14569
activeBlur();
@@ -448,7 +477,10 @@ export const handleTouchMove = (event: TouchEvent) => {
yDiff = Math.floor(clientY - event.touches[0].clientY);
// 上下滚动防止左右滑动
if (!firstXY) {
- firstXY = getTouchAxis(xDiff, yDiff, Constants.SIZE_DRAG_THRESHOLD);
+ const sidebarGesture = !hasClosestByAttribute(target, "id", "model", true) &&
+ !hasClosestByAttribute(target, "id", "menu", true);
+ firstXY = getTouchAxis(xDiff, yDiff, sidebarGesture ?
+ MOBILE_SIDEBAR_SWIPE_ACTIVATION_DISTANCE : Constants.SIZE_DRAG_THRESHOLD);
if (!firstXY) {
return;
}
diff --git a/app/src/mobile/util/touchPanelGesture.test.ts b/app/src/mobile/util/touchPanelGesture.test.ts
index 887228dc28..ed6534fffc 100644
--- a/app/src/mobile/util/touchPanelGesture.test.ts
+++ b/app/src/mobile/util/touchPanelGesture.test.ts
@@ -1,15 +1,17 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
+import {getTouchAxis} from "./touchGesture";
import {
getOpeningSidebar,
- getOpenSidebarReleaseAction,
getSidebarClosingDirection,
getSidebarClosingOffset,
getSidebarOpeningOffset,
+ MOBILE_SIDEBAR_SWIPE_ACTIVATION_DISTANCE,
MOBILE_SIDEBAR_MASK_SWIPING_CLASS,
MOBILE_SIDEBAR_SWIPING_CLASS,
setSidebarSwipeState,
shouldCloseGlobalMenu,
+ shouldCommitSidebarSwipe,
shouldDragOpenSidebar,
} from "./touchPanelGesture";
@@ -45,13 +47,6 @@ describe("mobile sidebar touch gesture", () => {
assert.equal(shouldDragOpenSidebar("right", "toLeft"), false);
});
- it("reopens a sidebar when a closing drag is pulled back", () => {
- assert.equal(getOpenSidebarReleaseAction("left", "toLeft", false), "close");
- assert.equal(getOpenSidebarReleaseAction("left", "toLeft", true), "open");
- assert.equal(getOpenSidebarReleaseAction("right", "toRight", false), "close");
- assert.equal(getOpenSidebarReleaseAction("right", "toRight", true), "open");
- });
-
it("closes the global menu only for an unreversed swipe to the right", () => {
assert.equal(shouldCloseGlobalMenu("toRight", false), true);
assert.equal(shouldCloseGlobalMenu("toRight", true), false);
@@ -76,6 +71,30 @@ describe("mobile sidebar touch gesture", () => {
assert.equal(getSidebarOpeningOffset("right", 400, 300), 0);
});
+ it("uses a sidebar-specific activation dead zone", () => {
+ assert.equal(getTouchAxis(11, 0, MOBILE_SIDEBAR_SWIPE_ACTIVATION_DISTANCE), undefined);
+ assert.equal(getTouchAxis(12, 0, MOBILE_SIDEBAR_SWIPE_ACTIVATION_DISTANCE), "x");
+ });
+
+ it("ignores short or slow sidebar swipes", () => {
+ assert.equal(shouldCommitSidebarSwipe("toRight", -31, 50, 360), false);
+ assert.equal(shouldCommitSidebarSwipe("toLeft", 31, 50, 360), false);
+ assert.equal(shouldCommitSidebarSwipe("toRight", -32, 200, 360), false);
+ assert.equal(shouldCommitSidebarSwipe("toLeft", 32, 200, 360), false);
+ });
+
+ it("commits fast sidebar flicks in either direction", () => {
+ assert.equal(shouldCommitSidebarSwipe("toRight", -32, 100, 360), true);
+ assert.equal(shouldCommitSidebarSwipe("toLeft", 32, 100, 360), true);
+ });
+
+ it("commits long drags and rejects movement opposite to the locked direction", () => {
+ assert.equal(shouldCommitSidebarSwipe("toRight", -120, 1000, 360), true);
+ assert.equal(shouldCommitSidebarSwipe("toLeft", 120, 1000, 360), true);
+ assert.equal(shouldCommitSidebarSwipe("toRight", 120, 100, 360), false);
+ assert.equal(shouldCommitSidebarSwipe("toLeft", -120, 100, 360), false);
+ });
+
it("keeps only the active sidebar in the swiping state", () => {
const left = createClassTarget();
const right = createClassTarget();
diff --git a/app/src/mobile/util/touchPanelGesture.ts b/app/src/mobile/util/touchPanelGesture.ts
index eee91ae028..788ff1a320 100644
--- a/app/src/mobile/util/touchPanelGesture.ts
+++ b/app/src/mobile/util/touchPanelGesture.ts
@@ -1,6 +1,10 @@
export type MobileSidebarSide = "left" | "right";
export type MobileSwipeDirection = "toLeft" | "toRight";
-export type MobileSidebarReleaseAction = "close" | "open";
+
+export const MOBILE_SIDEBAR_SWIPE_ACTIVATION_DISTANCE = 12;
+export const MOBILE_SIDEBAR_SWIPE_MIN_FLING_DISTANCE = 32;
+export const MOBILE_SIDEBAR_SWIPE_MIN_FLING_VELOCITY = 0.3;
+export const MOBILE_SIDEBAR_SWIPE_SETTLE_RATIO = 1 / 3;
export const MOBILE_SIDEBAR_SWIPING_CLASS = "side-panel--swiping";
export const MOBILE_SIDEBAR_MASK_SWIPING_CLASS = "side-mask--swiping";
@@ -43,15 +47,6 @@ export const shouldCloseGlobalMenu = (direction: MobileSwipeDirection, reversing
return direction === "toRight" && !reversing;
};
-export const getOpenSidebarReleaseAction = (
- side: MobileSidebarSide,
- firstDirection: MobileSwipeDirection,
- reversing: boolean,
-): MobileSidebarReleaseAction => {
- const closing = shouldDragOpenSidebar(side, firstDirection);
- return closing !== reversing ? "close" : "open";
-};
-
export const getSidebarClosingOffset = (side: MobileSidebarSide, xDiff: number, width: number) => {
if (side === "left") {
return Math.max(Math.min(-xDiff, 0), -width);
@@ -65,3 +60,21 @@ export const getSidebarOpeningOffset = (side: MobileSidebarSide, xDiff: number,
}
return Math.max(Math.min(width - xDiff, width), 0);
};
+
+export const shouldCommitSidebarSwipe = (
+ direction: MobileSwipeDirection,
+ xDiff: number,
+ duration: number,
+ width: number,
+) => {
+ const distance = direction === "toRight" ? -xDiff : xDiff;
+ if (distance <= 0) {
+ return false;
+ }
+ if (distance >= width * MOBILE_SIDEBAR_SWIPE_SETTLE_RATIO) {
+ return true;
+ }
+ const velocity = duration > 0 ? distance / duration : Number.POSITIVE_INFINITY;
+ return distance >= MOBILE_SIDEBAR_SWIPE_MIN_FLING_DISTANCE &&
+ velocity >= MOBILE_SIDEBAR_SWIPE_MIN_FLING_VELOCITY;
+};