Skip to content

Build your personal knowledge base with Trilium Notes

perf(mobile): slide the notes themselves on a tab swipe

Elian Dorana0b51a0c2cec
diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.css b/apps/client/src/widgets/mobile_widgets/TabSwitcher.css
index c3bfc33eb0..b14b498042 100644
--- a/apps/client/src/widgets/mobile_widgets/TabSwitcher.css
+++ b/apps/client/src/widgets/mobile_widgets/TabSwitcher.css
@@ -76,6 +76,35 @@ html[data-tab-slide]:has(body.ios:not(.backdrop-effects-disabled))::view-transit
backdrop-filter: blur(16px) saturate(1.5) brightness(1.15);
}
+html[data-tab-drag] {
+ --tab-slide-gap: 12px;
+
+ #detail-container {
+ overflow: clip;
+ }
+
+ #detail-container > .split-note-container-widget {
+ position: relative;
+ will-change: transform;
+
+ > .note-split.tab-slide-incoming {
+ display: flex !important;
+ position: absolute;
+ top: 0;
+ width: 100%;
+ height: 100%;
+ }
+ }
+}
+
+html[data-tab-drag="left"] #detail-container > .split-note-container-widget > .tab-slide-incoming {
+ left: calc(100% + var(--tab-slide-gap));
+}
+
+html[data-tab-drag="right"] #detail-container > .split-note-container-widget > .tab-slide-incoming {
+ left: calc(-100% - var(--tab-slide-gap));
+}
+
html[data-tab-slide="left"] {
&::view-transition-old(tab-content) { animation-name: tab-slide-out-left; }
&::view-transition-new(tab-content) { animation-name: tab-slide-in-from-right; }
diff --git a/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts b/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts
index d66ad13619..63bd4a0b7b 100644
--- a/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts
+++ b/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts
@@ -1,6 +1,8 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import appContext from "../../components/app_context";
+import type NoteContext from "../../components/note_context";
+import { forgetTabs, setTabScreen } from "./tab_navigation";
import { isTabSwipeActive, TabSwipe } from "./tab_swipe";
describe("TabSwipe", () => {
@@ -188,4 +190,95 @@ describe("TabSwipe", () => {
glob.isRtl = true;
expect(new TabSwipe("next").slide).toBe("right");
});
+
+ describe("on the note screen of a phone", () => {
+ const { tabManager } = appContext;
+ let startViewTransition: ReturnType<typeof vi.fn>;
+ let splits: HTMLElement;
+
+ beforeEach(() => {
+ vi.spyOn(window, "matchMedia").mockImplementation((query) => ({
+ matches: true, media: query, addEventListener: vi.fn(), removeEventListener: vi.fn()
+ }) as unknown as MediaQueryList);
+ const pending = new Promise<void>(() => {});
+ startViewTransition = vi.fn(() => ({ ready: pending, finished: pending }));
+ document.startViewTransition = startViewTransition as unknown as typeof document.startViewTransition;
+
+ const tab = (ntxId: string) => {
+ const noteContext = { ntxId } as NoteContext;
+ noteContext.getSubContexts = () => [ noteContext ];
+ return noteContext;
+ };
+ const tabs = [ tab("first"), tab("second") ];
+ appContext.tabManager = {
+ getActiveMainContext: () => tabs[0],
+ getMainNoteContexts: () => tabs
+ } as unknown as typeof tabManager;
+
+ document.body.innerHTML = `
+ <div id="mobile-rest-container">
+ <div id="mobile-sidebar-wrapper"></div>
+ <div id="detail-container"><div class="split-note-container-widget">
+ <div class="note-split visible" data-ntx-id="first"></div>
+ <div class="note-split hidden-ext" data-ntx-id="second"></div>
+ </div></div>
+ </div>`;
+ const container = document.querySelector<HTMLElement>(".split-note-container-widget");
+ if (!container) throw new Error("expected the splits");
+ splits = container;
+ });
+
+ afterEach(() => {
+ appContext.tabManager = tabManager;
+ forgetTabs([ "second" ]);
+ document.body.innerHTML = "";
+ });
+
+ const incoming = () => splits.querySelector<HTMLElement>("[data-ntx-id=second]");
+
+ it("slides the two tabs' notes, and switches tabs only once the slide settles", async () => {
+ const swipe = new TabSwipe("next");
+ expect(startViewTransition).not.toHaveBeenCalled();
+ expect(isTabSwipeActive()).toBe(true);
+ expect(document.documentElement.dataset.tabDrag).toBe("left");
+ expect(incoming()?.classList.contains("tab-slide-incoming")).toBe(true);
+
+ swipe.move(0.5);
+ expect(splits.style.transform).toBe("translateX(calc((100% + var(--tab-slide-gap)) * -0.5))");
+ expect(log).toEqual([]);
+
+ swipe.release(0.5);
+ await vi.waitFor(() => expect(isTabSwipeActive()).toBe(false));
+ expect(log).toEqual([ "activateNextTab" ]);
+ expect(splits.style.transform).toBe("");
+ expect(incoming()?.classList.contains("tab-slide-incoming")).toBe(false);
+ expect(document.documentElement.dataset.tabDrag).toBeUndefined();
+ });
+
+ it("runs back without switching tabs when released short, and slides the other way for the previous tab", async () => {
+ const swipe = new TabSwipe("previous");
+ expect(document.documentElement.dataset.tabDrag).toBe("right");
+ swipe.move(0.1);
+ expect(splits.style.transform).toBe("translateX(calc((100% + var(--tab-slide-gap)) * 0.1))");
+
+ swipe.release(0);
+ await vi.waitFor(() => expect(isTabSwipeActive()).toBe(false));
+ expect(log).toEqual([]);
+ expect(splits.style.transform).toBe("");
+ expect(incoming()?.classList.contains("tab-slide-incoming")).toBe(false);
+ });
+
+ 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");
+ expect(startViewTransition).toHaveBeenCalledTimes(1);
+ expect(incoming()?.classList.contains("tab-slide-incoming")).toBe(false);
+
+ incoming()?.classList.remove("empty-note");
+ setTabScreen("second", "tree");
+ new TabSwipe("next");
+ expect(startViewTransition).toHaveBeenCalledTimes(2);
+ expect(document.documentElement.dataset.tabDrag).toBeUndefined();
+ });
+ });
});
diff --git a/apps/client/src/widgets/mobile_widgets/tab_swipe.ts b/apps/client/src/widgets/mobile_widgets/tab_swipe.ts
index 6c870f9880..0640b445f7 100644
--- a/apps/client/src/widgets/mobile_widgets/tab_swipe.ts
+++ b/apps/client/src/widgets/mobile_widgets/tab_swipe.ts
@@ -1,4 +1,6 @@
import appContext, { type CommandNames } from "../../components/app_context";
+import { isNarrowLayout } from "../../services/utils";
+import { getTabNavigation } from "./tab_navigation";
export type SwipeDirection = "next" | "previous";
@@ -16,13 +18,23 @@ const SPRING_MAX_DURATION = 1000;
const HOLD_DURATION = 1e9;
let activeTransition: ViewTransition | undefined;
+let activeSplitSlide: SplitSlide | undefined;
+
+/** The note splits of a tab switch that slides them: their container and the incoming tab's split. */
+interface SplitSlide {
+ container: HTMLElement;
+ incoming: HTMLElement;
+}
/**
- * A tab switch whose slide follows the finger. The tab switches as the swipe starts, inside a view
- * transition whose animations stay paused while `move()` sets how far they have run. `release()`
- * carries them on to the other tab, or back to the first one and switches back to it, on a spring
- * that starts at the finger's speed. Without view
- * transitions, or with animations off, the tab switches on release instead.
+ * A tab switch whose slide follows the finger. `release()` carries the slide on to the other tab, or
+ * back to the first one, on a spring that starts at the finger's speed.
+ *
+ * Between the notes of two tabs on a phone, the slide moves the tabs' note splits themselves, and the
+ * tab switches once it settles on the other one. Otherwise the tab switches as the swipe starts,
+ * 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.
*/
export class TabSwipe {
/** Which way the content slides: toward the start for the next tab. */
@@ -36,20 +48,29 @@ export class TabSwipe {
private animations?: Animation[];
private hold?: Animation;
private skipped = false;
+ private splitSlide?: SplitSlide;
constructor(direction: SwipeDirection) {
this.direction = direction;
this.slide = (direction === "next") !== glob.isRtl ? "left" : "right";
- const canSlide = typeof document.startViewTransition === "function"
- && !document.body.classList.contains("motion-disabled");
- if (canSlide) this.#startTransition();
+ if (document.body.classList.contains("motion-disabled")) return;
+ this.splitSlide = findSplitSlide(direction);
+ if (this.splitSlide) {
+ this.#startSplitSlide(this.splitSlide);
+ } else if (typeof document.startViewTransition === "function") {
+ this.#startTransition();
+ }
}
/** 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));
- this.#seek();
+ if (this.splitSlide) {
+ this.#placeSplits(this.splitSlide);
+ } else {
+ this.#seek();
+ }
}
/**
@@ -59,7 +80,9 @@ export class TabSwipe {
release(velocity: number, room = window.innerWidth) {
this.commit = this.progress >= COMMIT_PROGRESS || velocity >= COMMIT_VELOCITY;
this.releaseVelocity = velocity / room;
- if (!this.transition) {
+ if (this.splitSlide) {
+ void this.#settleSplitSlide(this.splitSlide);
+ } else if (!this.transition) {
if (this.commit) void appContext.triggerCommand(getCommand(this.direction));
} else if (this.animations) {
void this.#settle();
@@ -68,6 +91,27 @@ export class TabSwipe {
}
}
+ #startSplitSlide(slide: SplitSlide) {
+ activeSplitSlide = slide;
+ document.documentElement.dataset.tabDrag = this.slide;
+ slide.incoming.classList.add("tab-slide-incoming");
+ }
+
+ #placeSplits({ container }: SplitSlide) {
+ const offset = (this.slide === "left" ? -1 : 1) * this.progress;
+ container.style.transform = offset ? `translateX(calc((100% + var(--tab-slide-gap)) * ${offset}))` : "";
+ }
+
+ async #settleSplitSlide(slide: SplitSlide) {
+ await this.#spring(this.commit ? 1 : 0);
+ // The incoming split stays in place until the switch shows it in its own right.
+ if (this.commit) await appContext.triggerCommand(getCommand(this.direction));
+ slide.container.style.transform = "";
+ slide.incoming.classList.remove("tab-slide-incoming");
+ delete document.documentElement.dataset.tabDrag;
+ if (activeSplitSlide === slide) activeSplitSlide = undefined;
+ }
+
#startTransition() {
const root = document.documentElement;
root.dataset.tabSlide = this.slide;
@@ -164,7 +208,28 @@ export class TabSwipe {
/** Whether a swipe's slide is still running, during which another swipe does not start. */
export function isTabSwipeActive() {
- return !!activeTransition;
+ return !!activeTransition || !!activeSplitSlide;
+}
+
+/**
+ * The splits to slide toward the tab `direction` leads to, on the note screen of a phone: both tabs
+ * must show their note in a single split, and the incoming one must have been rendered.
+ */
+function findSplitSlide(direction: SwipeDirection): SplitSlide | undefined {
+ if (!isNarrowLayout() || document.getElementById("mobile-sidebar-wrapper")?.classList.contains("show")) return;
+
+ const tabs = appContext.tabManager.getMainNoteContexts();
+ const current = appContext.tabManager.getActiveMainContext();
+ const index = tabs.findIndex((tab) => tab.ntxId === current?.ntxId);
+ if (!current || index < 0 || tabs.length < 2) return;
+ const target = tabs[(index + (direction === "next" ? 1 : -1) + tabs.length) % tabs.length];
+ if (!target.ntxId || (getTabNavigation(target.ntxId)?.screen ?? "detail") !== "detail") return;
+ if (current.getSubContexts().length !== 1 || target.getSubContexts().length !== 1) return;
+
+ const container = document.querySelector<HTMLElement>("#detail-container > .split-note-container-widget");
+ const incoming = container?.querySelector<HTMLElement>(`:scope > [data-ntx-id="${target.ntxId}"]`);
+ if (!container || !incoming?.classList.contains("note-split") || incoming.classList.contains("empty-note")) return;
+ return { container, incoming };
}
function getCommand(direction: SwipeDirection): CommandNames {