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
feat(mobile): pull the screen on a swipe past the last tab
Elian Doran2edcb5f4402d
diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.css b/apps/client/src/widgets/mobile_widgets/TabSwitcher.cssindex f647f93796..10b932ed80 100644--- a/apps/client/src/widgets/mobile_widgets/TabSwitcher.css+++ b/apps/client/src/widgets/mobile_widgets/TabSwitcher.css@@ -76,6 +76,10 @@ html[data-tab-slide]:has(body.ios:not(.backdrop-effects-disabled))::view-transitbackdrop-filter: blur(16px) saturate(1.5) brightness(1.15);}+html[data-tab-overscroll] #detail-container {+ overflow: clip;+}+html[data-tab-drag] {--tab-slide-gap: 12px;diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx b/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsxindex 34a5db4e73..b35c0d49a3 100644--- a/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx+++ b/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx@@ -32,7 +32,7 @@ import LinkButton from "../react/LinkButton";import Modal from "../react/Modal";import { useNavigatorChildren } from "./MobileNoteNavigator";import { getLastSegment, getSpotlightedNoteId, getTabNavigation, type TreeStack } from "./tab_navigation";-import { getTabToward, isTabSwipeActive, type SwipeDirection, TabSwipe } from "./tab_swipe";+import { isTabSwipeActive, type SwipeDirection, TabSwipe } from "./tab_swipe";const VIEW_MODE_ICON_MAPPINGS: Record<Exclude<ViewMode, "default">, string> = {source: "bx bx-code",@@ -174,12 +174,6 @@ function useTabSwipe() {if (Math.abs(dy) > SWIPE_START) gestureRef.current = null;return;}- // Past the first or the last tab, the drag still ends in no tap.- if (!getTabToward(direction)) {- gestureRef.current = null;- swipedRef.current = true;- return;- }gesture.swipe = new TabSwipe(direction);gesture.room = getRoom(gesture.x, gesture.swipe.slide);swipedRef.current = true;diff --git a/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts b/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.tsindex da6a158ec2..73e74f97cd 100644--- a/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts+++ b/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts@@ -6,11 +6,18 @@ import { forgetTabs, setTabScreen } from "./tab_navigation";import { isTabSwipeActive, TabSwipe } from "./tab_swipe";describe("TabSwipe", () => {+ const { tabManager } = appContext;let log: string[];beforeEach(() => {glob.isRtl = false;log = [];+ // The middle one of three tabs, with a tab both ways.+ const tabs = [ "before", "middle", "after" ].map((ntxId) => ({ ntxId }) as NoteContext);+ appContext.tabManager = {+ getActiveMainContext: () => tabs[1],+ getMainNoteContexts: () => tabs+ } as unknown as typeof tabManager;vi.spyOn(appContext, "triggerCommand").mockImplementation(async (name) => {log.push(name);});@@ -25,6 +32,7 @@ describe("TabSwipe", () => {afterEach(() => {vi.restoreAllMocks();+ appContext.tabManager = tabManager;Reflect.deleteProperty(document, "startViewTransition");Reflect.deleteProperty(document, "getAnimations");Reflect.deleteProperty(document.documentElement, "animate");@@ -192,7 +200,6 @@ describe("TabSwipe", () => {});describe("on the note screen of a phone", () => {- const { tabManager } = appContext;let startViewTransition: ReturnType<typeof vi.fn>;let splits: HTMLElement;let tabs: NoteContext[];@@ -232,7 +239,6 @@ describe("TabSwipe", () => {});afterEach(() => {- appContext.tabManager = tabManager;forgetTabs([ "second" ]);document.body.innerHTML = "";});@@ -272,6 +278,46 @@ describe("TabSwipe", () => {expect(incoming()?.classList.contains("tab-slide-incoming")).toBe(false);});+ it("pulls the note past the last tab against a growing resistance, and springs it back", async () => {+ active = tabs[2];+ const pulled = (progress: number) => {+ swipe.move(progress);+ const match = /^translateX\(calc\(100% \* (-?[\d.]+)\)\)$/.exec(splits.style.transform);+ expect(match).not.toBeNull();+ return Number(match?.[1]);+ };++ const swipe = new TabSwipe("next");+ expect(startViewTransition).not.toHaveBeenCalled();+ expect(isTabSwipeActive()).toBe(true);+ expect(document.documentElement.dataset.tabOverscroll).toBe("");+ expect(splits.querySelector(".tab-slide-incoming")).toBeNull();+ const [ short, half, full ] = [ pulled(0.1), pulled(0.5), pulled(1) ];+ expect(short).toBeLessThan(0);+ expect(half).toBeLessThan(short);+ // Each stretch of the finger pulls less than the one before, up to a quarter of the width.+ expect(half - full).toBeLessThan(short - half);+ expect(full).toBeGreaterThan(-0.25);++ swipe.release(2);+ await vi.waitFor(() => expect(isTabSwipeActive()).toBe(false));+ expect(log).toEqual([]);+ expect(splits.style.transform).toBe("");+ expect(document.documentElement.dataset.tabOverscroll).toBeUndefined();++ // Before the first tab, the tree pulls the other way while it is shown.+ active = tabs[0];+ const sidebar = document.getElementById("mobile-sidebar-wrapper");+ sidebar?.classList.add("show");+ const tree = sidebar?.appendChild(document.createElement("div"));+ const back = new TabSwipe("previous");+ back.move(0.5);+ expect(tree?.style.transform).toBe(`translateX(calc(100% * ${-half}))`);+ expect(splits.style.transform).toBe("");+ back.release(0);+ await vi.waitFor(() => expect(tree?.style.transform).toBe(""));+ });+it("keeps the view transition for a tab not shown yet, or one left on its tree", () => {incoming()?.classList.add("empty-note");new TabSwipe("next");diff --git a/apps/client/src/widgets/mobile_widgets/tab_swipe.ts b/apps/client/src/widgets/mobile_widgets/tab_swipe.tsindex 62e8267bb5..3cbedd0843 100644--- a/apps/client/src/widgets/mobile_widgets/tab_swipe.ts+++ b/apps/client/src/widgets/mobile_widgets/tab_swipe.ts@@ -16,9 +16,14 @@ const SPRING_REST = 0.002;const SPRING_MAX_DURATION = 1000;/** Longer than any swipe is held; the animation holding the transition open is canceled long before. */const HOLD_DURATION = 1e9;+/** How far, as a share of its width, a swipe past the first or last tab can pull the screen at most. */+const OVERSCROLL_LIMIT = 0.25;+/** How quickly the pull past the first or last tab approaches `OVERSCROLL_LIMIT` as the finger moves. */+const OVERSCROLL_STIFFNESS = 2;let activeTransition: ViewTransition | undefined;let activeSplitSlide: SplitSlide | undefined;+let activeOverscroll: HTMLElement | undefined;/** The note splits of a tab switch that slides them: their container and the incoming tab's split. */interface SplitSlide {@@ -35,6 +40,9 @@ interface SplitSlide {* inside a view transition whose animations stay paused while `move()` sets how far they have run,* and a slide that runs back switches back. Without view transitions, or with animations off, the* tab switches on release instead.+ *+ * Past the first or the last tab, the swipe pulls the shown screen with a growing resistance and+ * springs it back on release, with no tab to switch to.*/export class TabSwipe {/** Which way the content slides: toward the start for the next tab. */@@ -49,12 +57,20 @@ export class TabSwipe {private hold?: Animation;private skipped = false;private splitSlide?: SplitSlide;+ private readonly isPastEnd: boolean;+ private overscroll?: HTMLElement;constructor(direction: SwipeDirection) {this.direction = direction;this.slide = (direction === "next") !== glob.isRtl ? "left" : "right";+ this.isPastEnd = isPastLastTab(direction);if (document.body.classList.contains("motion-disabled")) return;+ if (this.isPastEnd) {+ this.overscroll = findShownScreen();+ if (this.overscroll) this.#startOverscroll();+ return;+ }this.splitSlide = findSplitSlide(direction);if (this.splitSlide) {this.#startSplitSlide(this.splitSlide);@@ -66,7 +82,9 @@ export class TabSwipe {/** Moves the slide to `progress`, from 0 (the tab the swipe started on) to 1 (the other tab). */move(progress: number) {this.progress = Math.min(1, Math.max(0, progress));- if (this.splitSlide) {+ if (this.overscroll) {+ this.#placeOverscroll(this.overscroll);+ } else if (this.splitSlide) {this.#placeSplits(this.splitSlide);} else {this.#seek();@@ -78,6 +96,10 @@ export class TabSwipe {* and `room` how far, in px, it travels for the whole slide.*/release(velocity: number, room = window.innerWidth) {+ if (this.isPastEnd) {+ if (this.overscroll) void this.#settleOverscroll(this.overscroll);+ return;+ }this.commit = this.progress >= COMMIT_PROGRESS || velocity >= COMMIT_VELOCITY;this.releaseVelocity = velocity / room;if (this.splitSlide) {@@ -91,6 +113,25 @@ export class TabSwipe {}}+ #startOverscroll() {+ activeOverscroll = this.overscroll;+ document.documentElement.dataset.tabOverscroll = "";+ }++ #placeOverscroll(screen: HTMLElement) {+ const pull = OVERSCROLL_LIMIT * (1 - 1 / (1 + OVERSCROLL_STIFFNESS * this.progress));+ const offset = (this.slide === "left" ? -1 : 1) * pull;+ screen.style.transform = offset ? `translateX(calc(100% * ${offset}))` : "";+ }++ async #settleOverscroll(screen: HTMLElement) {+ this.commit = false;+ await this.#spring(0);+ screen.style.transform = "";+ delete document.documentElement.dataset.tabOverscroll;+ if (activeOverscroll === screen) activeOverscroll = undefined;+ }+#startSplitSlide(slide: SplitSlide) {activeSplitSlide = slide;document.documentElement.dataset.tabDrag = this.slide;@@ -208,7 +249,23 @@ export class TabSwipe {/** Whether a swipe's slide is still running, during which another swipe does not start. */export function isTabSwipeActive() {- return !!activeTransition || !!activeSplitSlide;+ return !!activeTransition || !!activeSplitSlide || !!activeOverscroll;+}++/** Whether the active tab is the last one toward `direction`. */+function isPastLastTab(direction: SwipeDirection) {+ const tabs = appContext.tabManager.getMainNoteContexts();+ return tabs.some((tab) => tab.ntxId === appContext.tabManager.getActiveMainContext()?.ntxId)+ && !getTabToward(direction);+}++/** The screen a swipe past the first or last tab pulls: the tree while a phone shows it, or the notes. */+function findShownScreen() {+ const sidebar = document.getElementById("mobile-sidebar-wrapper");+ if (isNarrowLayout() && sidebar?.classList.contains("show")) {+ return sidebar.firstElementChild instanceof HTMLElement ? sidebar.firstElementChild : undefined;+ }+ return document.querySelector<HTMLElement>("#detail-container > .split-note-container-widget") ?? undefined;}/** The tab next to the active one toward `direction`, if the active one is not the last that way. */