Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): stop tab swipes at the first and last tab

Elian Doran4690fdcaea47
diff --git a/apps/client/src/widgets/containers/split_note_container.spec.tsx b/apps/client/src/widgets/containers/split_note_container.spec.tsx
index e6fc8dfb2e..71013a807e 100644
--- a/apps/client/src/widgets/containers/split_note_container.spec.tsx
+++ b/apps/client/src/widgets/containers/split_note_container.spec.tsx
@@ -168,7 +168,7 @@ describe("SplitNoteContainer", () => {
try {
const container = new SplitNoteContainer(() => new NoteWrapperWidget().child(<Probe />));
container.render();
- // The active tab, and three in the background; the last is before the first, around the end.
+ // The first tab is active: the second is next to it, and the last is not, around the end.
const tabs = [ "first", "second", "third", "fourth" ].map((ntxId) => new NoteContext(ntxId));
await act(async () => {
for (const noteContext of tabs) {
@@ -194,14 +194,13 @@ describe("SplitNoteContainer", () => {
await act(async () => {
await vi.advanceTimersByTimeAsync(1000);
});
- expect(readProbes()).toEqual([ "first-note", "second-note", "", "fourth-note" ]);
// Still hidden: only the active tab is shown.
expect(container.$widget.find("[data-ntx-id=second]").hasClass("hidden-ext")).toBe(true);
await act(async () => {
await vi.advanceTimersByTimeAsync(5000);
});
- expect(readProbes()[2]).toBe("");
+ expect(readProbes()).toEqual([ "first-note", "second-note", "", "" ]);
} finally {
vi.useRealTimers();
window.glob.device = device;
diff --git a/apps/client/src/widgets/containers/split_note_container.ts b/apps/client/src/widgets/containers/split_note_container.ts
index 8cda34cc36..eb95f04bb6 100644
--- a/apps/client/src/widgets/containers/split_note_container.ts
+++ b/apps/client/src/widgets/containers/split_note_container.ts
@@ -4,6 +4,7 @@ import NoteContext from "../../components/note_context.js";
import splitService from "../../services/resizer.js";
import { isMobile } from "../../services/utils.js";
import type BasicWidget from "../basic_widget.js";
+import { getTabToward } from "../mobile_widgets/tab_swipe.js";
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import FlexContainer from "./flex_container.js";
@@ -399,13 +400,7 @@ function recursiveCleanup(widget: Component) {
}
}
-/** The tabs before and after the active one, around the ends. */
+/** The tabs before and after the active one, which a swipe on mobile leads to. */
function getNeighbourTabs() {
- const tabs = appContext.tabManager.getMainNoteContexts();
- const index = tabs.findIndex((tab) => tab.ntxId === appContext.tabManager.getActiveMainContext()?.ntxId);
- if (index < 0 || tabs.length < 2) return [];
-
- const next = tabs[(index + 1) % tabs.length];
- const previous = tabs[(index - 1 + tabs.length) % tabs.length];
- return next === previous ? [ next ] : [ next, previous ];
+ return [ getTabToward("next"), getTabToward("previous") ].filter((tab) => !!tab);
}
diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx b/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx
index 3c5cc4af09..3263cdd177 100644
--- a/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx
+++ b/apps/client/src/widgets/mobile_widgets/TabSwitcher.spec.tsx
@@ -125,9 +125,11 @@ describe("tab swipe", () => {
it("switches tabs on a swipe anywhere on a launch bar that fits, whose click then is not a tap", () => {
const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockResolvedValue(undefined);
const tabManager = appContext.tabManager;
- const fakeContext = (ntxId: string) => ({ ntxId }) as NoteContext;
+ const tabs = [ "a", "b", "c" ].map((ntxId) => ({ ntxId }) as NoteContext);
+ let activeNtxId = "b";
appContext.tabManager = {
- getMainNoteContexts: () => [ fakeContext("a"), fakeContext("b") ]
+ getMainNoteContexts: () => tabs,
+ getActiveMainContext: () => tabs.find((tab) => tab.ntxId === activeNtxId)
} as unknown as typeof appContext.tabManager;
const onTap = vi.fn();
function LaunchBar() {
@@ -173,6 +175,13 @@ describe("tab swipe", () => {
press(button, 600, []);
expect(onTap).toHaveBeenCalledTimes(1);
+ // The first tab has no tab before it, and the swipe toward one is not a tap either.
+ activeNtxId = "a";
+ press(button, 900, [ [ 910, 200 ], [ 960, 400 ] ]);
+ expect(triggerCommand).toHaveBeenCalledTimes(3);
+ expect(onTap).toHaveBeenCalledTimes(1);
+ activeNtxId = "b";
+
// A bar with more launchers than fit scrolls sideways instead.
const scroller = button.parentElement;
if (!scroller) throw new Error("No container");
@@ -189,8 +198,10 @@ describe("tab swipe", () => {
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;
+ const tabs = [ { ntxId: "a" }, { ntxId: "b" } ];
appContext.tabManager = {
- getMainNoteContexts: () => [ { ntxId: "a" }, { ntxId: "b" } ]
+ getMainNoteContexts: () => tabs,
+ getActiveMainContext: () => tabs[0]
} as unknown as typeof appContext.tabManager;
function LaunchBar() {
const containerRef = useRef<HTMLDivElement>(null);
diff --git a/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx b/apps/client/src/widgets/mobile_widgets/TabSwitcher.tsx
index b35c0d49a3..34a5db4e73 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 { isTabSwipeActive, type SwipeDirection, TabSwipe } from "./tab_swipe";
+import { getTabToward, isTabSwipeActive, type SwipeDirection, TabSwipe } from "./tab_swipe";
const VIEW_MODE_ICON_MAPPINGS: Record<Exclude<ViewMode, "default">, string> = {
source: "bx bx-code",
@@ -174,6 +174,12 @@ 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.ts
index 63bd4a0b7b..da6a158ec2 100644
--- a/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts
+++ b/apps/client/src/widgets/mobile_widgets/tab_swipe.spec.ts
@@ -195,6 +195,8 @@ describe("TabSwipe", () => {
const { tabManager } = appContext;
let startViewTransition: ReturnType<typeof vi.fn>;
let splits: HTMLElement;
+ let tabs: NoteContext[];
+ let active: NoteContext | undefined;
beforeEach(() => {
vi.spyOn(window, "matchMedia").mockImplementation((query) => ({
@@ -209,9 +211,10 @@ describe("TabSwipe", () => {
noteContext.getSubContexts = () => [ noteContext ];
return noteContext;
};
- const tabs = [ tab("first"), tab("second") ];
+ tabs = [ tab("first"), tab("second"), tab("third") ];
+ active = tabs[0];
appContext.tabManager = {
- getActiveMainContext: () => tabs[0],
+ getActiveMainContext: () => active,
getMainNoteContexts: () => tabs
} as unknown as typeof tabManager;
@@ -256,6 +259,7 @@ describe("TabSwipe", () => {
});
it("runs back without switching tabs when released short, and slides the other way for the previous tab", async () => {
+ active = tabs[2];
const swipe = new TabSwipe("previous");
expect(document.documentElement.dataset.tabDrag).toBe("right");
swipe.move(0.1);
diff --git a/apps/client/src/widgets/mobile_widgets/tab_swipe.ts b/apps/client/src/widgets/mobile_widgets/tab_swipe.ts
index 0640b445f7..62e8267bb5 100644
--- a/apps/client/src/widgets/mobile_widgets/tab_swipe.ts
+++ b/apps/client/src/widgets/mobile_widgets/tab_swipe.ts
@@ -211,6 +211,14 @@ export function isTabSwipeActive() {
return !!activeTransition || !!activeSplitSlide;
}
+/** The tab next to the active one toward `direction`, if the active one is not the last that way. */
+export function getTabToward(direction: SwipeDirection) {
+ const tabs = appContext.tabManager.getMainNoteContexts();
+ const index = tabs.findIndex((tab) => tab.ntxId === appContext.tabManager.getActiveMainContext()?.ntxId);
+ if (index < 0) return;
+ return tabs[index + (direction === "next" ? 1 : -1)];
+}
+
/**
* 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.
@@ -218,12 +226,9 @@ export function isTabSwipeActive() {
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;
+ const target = getTabToward(direction);
+ if (!current || !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");