Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mobile): finish a tab swipe released off the launch bar
Elian Doranabc537e48fa5
diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx b/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsxindex c70aa03dae..3c5cc4af09 100644--- a/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx+++ b/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx@@ -1,3 +1,4 @@+import { render } from "preact";import { useRef } from "preact/hooks";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";@@ -184,4 +185,41 @@ describe("tab swipe", () => {appContext.tabManager = tabManager;});++ it("ends a swipe released off the launch bar, and one still running as the bar unmounts", () => {+ const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockResolvedValue(undefined);+ const tabManager = appContext.tabManager;+ appContext.tabManager = {+ getMainNoteContexts: () => [ { ntxId: "a" }, { ntxId: "b" } ]+ } as unknown as typeof appContext.tabManager;+ function LaunchBar() {+ const containerRef = useRef<HTMLDivElement>(null);+ useLaunchBarTabSwipe(containerRef, true);+ return <div id="launcher-pane"><div ref={containerRef}><button /></div></div>;+ }+ const send = (target: EventTarget, type: string, clientX: number) => {+ target.dispatchEvent(new PointerEvent(type, { bubbles: true, isPrimary: true, pointerId: 1, clientX, clientY: 10 }));+ };++ let bar = document.createElement("div");+ act(() => {+ bar = renderInto(<LaunchBar />);+ });+ const button = bar.querySelector("button");+ if (!button) throw new Error("No button");++ send(button, "pointerdown", 600);+ send(button, "pointermove", 590);+ send(document.body, "pointermove", 300);+ send(document.body, "pointerup", 300);+ expect(triggerCommand.mock.calls).toEqual([ [ "activateNextTab" ] ]);++ send(button, "pointerdown", 600);+ send(button, "pointermove", 590);+ send(button, "pointermove", 300);+ act(() => render(null, bar));+ expect(triggerCommand.mock.calls).toEqual([ [ "activateNextTab" ], [ "activateNextTab" ] ]);++ appContext.tabManager = tabManager;+ });});diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx b/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsxindex a0159af70c..b35c0d49a3 100644--- a/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx+++ b/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx@@ -86,7 +86,8 @@ interface SwipeGesture {/*** Slides to the next or the previous tab as a finger swipes sideways anywhere on the launch bar, while* the bar fits all its launchers: one that scrolls sideways keeps that drag for scrolling. The click- * that ends a swipe reaches none of the bar's buttons.+ * that ends a swipe reaches none of the bar's buttons. A swipe that starts on the bar follows the+ * pointer off it, since a mouse, unlike a finger, does not keep sending its events to the bar.*/export function useLaunchBarTabSwipe(containerRef: RefObject<HTMLElement | null>, enabled: boolean) {const swipe = useTabSwipe();@@ -123,18 +124,19 @@ export function useLaunchBarTabSwipe(containerRef: RefObject<HTMLElement | null>}updateSwipeable();bar.addEventListener("pointerdown", onPointerDown);- bar.addEventListener("pointermove", onPointerMove);- bar.addEventListener("pointerup", onPointerEnd);- bar.addEventListener("pointercancel", onPointerEnd);+ window.addEventListener("pointermove", onPointerMove);+ window.addEventListener("pointerup", onPointerEnd);+ window.addEventListener("pointercancel", onPointerEnd);bar.addEventListener("click", onClickCapture, { capture: true });return () => {observer.disconnect();bar.classList.remove("tab-swipe");bar.removeEventListener("pointerdown", onPointerDown);- bar.removeEventListener("pointermove", onPointerMove);- bar.removeEventListener("pointerup", onPointerEnd);- bar.removeEventListener("pointercancel", onPointerEnd);+ window.removeEventListener("pointermove", onPointerMove);+ window.removeEventListener("pointerup", onPointerEnd);+ window.removeEventListener("pointercancel", onPointerEnd);bar.removeEventListener("click", onClickCapture, { capture: true });+ swipeRef.current.cancel();};}, [ containerRef, enabled ]);}@@ -186,12 +188,19 @@ function useTabSwipe() {},onPointerEnd(e: PointerEvent) {const gesture = gestureRef.current;+ if (!gesture || gesture.pointerId !== e.pointerId) return;gestureRef.current = null;- if (!gesture?.swipe) return;+ if (!gesture.swipe) return;const isFlick = e.type === "pointerup" && e.timeStamp - gesture.lastTime <= FLICK_WINDOW;gesture.swipe.release(isFlick ? gesture.velocity : 0, gesture.room);},+ /** Settles a running swipe where it stands, as when the pointer lifts slowly. */+ cancel() {+ const gesture = gestureRef.current;+ gestureRef.current = null;+ gesture?.swipe?.release(0, gesture.room);+ },takeSwipe() {const swiped = swipedRef.current;swipedRef.current = false;