Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): lean the view over from a 3D button, after Google Maps

Elian Doran1f1eaf4b5642
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 31a96c2876..af2d1ebbdd 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2749,6 +2749,8 @@
"webgl-unavailable": "This map can't be drawn because WebGL isn't available. Check that hardware acceleration is enabled and that your graphics drivers are up to date.",
"zoom-in": "Zoom in",
"zoom-out": "Zoom out",
+ "enter-3d": "Tilt into a 3D view — holding Ctrl and dragging tilts it freely",
+ "exit-3d": "Lay the map flat again",
"fullscreen": "Fullscreen",
"exit-fullscreen": "Exit fullscreen",
"close-details": "Close",
diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.css b/apps/client/src/widgets/collections/geomap/MapToolbar.css
index 0f38e81fda..03d9528549 100644
--- a/apps/client/src/widgets/collections/geomap/MapToolbar.css
+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.css
@@ -28,3 +28,10 @@
--opaque-icon-button-hover-color: var(--geo-map-panel-hover-text-color);
}
+/* The tilt wears text — 3D or 2D, a word the icon set has no glyph for — but it is one action, so
+ it keeps an icon button's width rather than the readout's roomier chip. */
+.geo-map-container .geo-map-toolbar .geo-map-tilt-button {
+ min-width: 0;
+ width: 32px;
+}
+
diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx b/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx
index c6dfa0fc4e..b1ed894975 100644
--- a/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.spec.tsx
@@ -14,36 +14,57 @@ import { renderInto } from "../../../test/render";
import { ParentMap } from "./map";
import MapToolbar from "./MapToolbar";
-/** The order the group lays its buttons out in — the image viewer's, with the screen at the end. */
-const ZOOM_OUT = 0;
-const ZOOM_LEVEL = 1;
-const ZOOM_IN = 2;
-const FULLSCREEN = 3;
-
-/** A map that zooms, says so, and stands somewhere — all these controls ask of one. */
-function fakeMap({ zoom = 5, minZoom = 2, maxZoom = 22 } = {}) {
- const listeners = new Set<() => void>();
+/** The order the group lays its buttons out in — the image viewer's, with the tilt leading and the
+ * screen at the end. */
+const TILT = 0;
+const ZOOM_OUT = 1;
+const ZOOM_LEVEL = 2;
+const ZOOM_IN = 3;
+const FULLSCREEN = 4;
+
+/** A map that zooms and tilts, says so, and stands somewhere — all these controls ask of one. */
+function fakeMap({ zoom = 5, minZoom = 2, maxZoom = 22, pitch = 0 } = {}) {
+ const listeners = new Map<string, Set<() => void>>();
+ const fire = (event: string) => {
+ for (const listener of listeners.get(event) ?? []) listener();
+ };
const container = document.createElement("div");
container.requestFullscreen = vi.fn(async () => {});
document.body.appendChild(container);
const setZoom = (value: number) => {
zoom = Math.min(Math.max(value, minZoom), maxZoom);
- for (const listener of listeners) listener();
+ fire("zoom");
+ };
+ const setPitch = (value: number) => {
+ pitch = value;
+ fire("pitch");
};
return {
getZoom: () => zoom,
getMinZoom: () => minZoom,
getMaxZoom: () => maxZoom,
+ getPitch: () => pitch,
getContainer: () => container,
zoomIn: vi.fn(() => setZoom(zoom + 1)),
zoomOut: vi.fn(() => setZoom(zoom - 1)),
/** The map being zoomed by something other than these buttons — the wheel, or a pinch. */
zoomTo: setZoom,
- on: (event: string, listener: () => void) => { if (event === "zoom") listeners.add(listener); },
- off: (event: string, listener: () => void) => { if (event === "zoom") listeners.delete(listener); },
- get listenerCount() { return listeners.size; }
+ /** The view being leaned over by hand — Ctrl and a drag, which MapLibre honours itself. */
+ tiltTo: setPitch,
+ easeTo: vi.fn(({ pitch: leanedTo }: { pitch?: number }) => {
+ if (leanedTo !== undefined) setPitch(leanedTo);
+ }),
+ on: (event: string, listener: () => void) => {
+ listeners.set(event, (listeners.get(event) ?? new Set()).add(listener));
+ },
+ off: (event: string, listener: () => void) => { listeners.get(event)?.delete(listener); },
+ get listenerCount() {
+ let count = 0;
+ for (const held of listeners.values()) count += held.size;
+ return count;
+ }
};
}
@@ -85,6 +106,7 @@ describe("geo map MapToolbar", () => {
const container = renderToolbar(fakeMap());
expect(buttons(container).map((button) => button.className)).toEqual([
+ expect.stringContaining("geo-map-tilt-button"),
expect.stringContaining("bx-minus-circle"),
expect.stringContaining("tn-overlay-text-button"),
expect.stringContaining("bx-plus-circle"),
@@ -92,6 +114,34 @@ describe("geo map MapToolbar", () => {
]);
});
+ it("leans the view over and lays it flat again, its face naming the view it offers", () => {
+ const map = fakeMap();
+ const container = renderToolbar(map);
+ expect(buttons(container)[TILT].textContent).toBe("3D");
+
+ press(container, TILT);
+
+ // Leaned over — by an eased flight rather than a jump — and now offering the way back.
+ expect(map.easeTo).toHaveBeenCalled();
+ expect(map.getPitch()).toBeGreaterThan(0);
+ expect(buttons(container)[TILT].textContent).toBe("2D");
+
+ press(container, TILT);
+
+ expect(map.getPitch()).toBe(0);
+ expect(buttons(container)[TILT].textContent).toBe("3D");
+ });
+
+ it("counts a tilt dragged in with Ctrl as 3D, the button never having been pressed", () => {
+ const map = fakeMap();
+ const container = renderToolbar(map);
+
+ // As MapLibre's own Ctrl-drag would, without the button hearing of it directly.
+ act(() => map.tiltTo(30));
+
+ expect(buttons(container)[TILT].textContent).toBe("2D");
+ });
+
it("stands aside until there is a map to zoom", () => {
const container = renderToolbar(null);
@@ -173,7 +223,8 @@ describe("geo map MapToolbar", () => {
it("stops listening to a map it is taken off", () => {
const map = fakeMap();
const container = renderToolbar(map);
- expect(map.listenerCount).toBe(1);
+ // One listener for the zoom, one for the pitch.
+ expect(map.listenerCount).toBe(2);
// A map torn down under controls that stayed behind would go on being reported to.
act(() => { render(null, container); });
diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.tsx b/apps/client/src/widgets/collections/geomap/MapToolbar.tsx
index aa5653424c..5e9ade4875 100644
--- a/apps/client/src/widgets/collections/geomap/MapToolbar.tsx
+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.tsx
@@ -18,20 +18,38 @@ import { ParentMap } from "./map";
* see {@link ImageViewer}): a step out, a step in, and between them a readout saying how close in
* the map is. The readout only says it — a map has no fitted view to be reset to the way an image
* has, and the whole world is a place worth going back to only rarely.
+ *
+ * At the group's leading end stands the tilt, after Google Maps's own button: 3D leans the view
+ * over, 2D lays it flat again. Which of the two it offers is read off the view itself rather than
+ * remembered from the last press — MapLibre tilts for Ctrl and a drag as well, and a button that
+ * only watched itself would go on offering a 3D the reader is already in.
*/
+
+/** How far the button leans the view over — a lean rather than the horizon, as Google Maps takes
+ * it. Ctrl and a drag go further, all the way to MapLibre's own limit. */
+const TILTED_PITCH = 45;
export default function MapToolbar() {
const map = useContext(ParentMap);
const zoom = useMapZoom(map);
+ const pitch = useMapPitch(map);
// The map itself rather than the whole view: what is around it is the note's own chrome, and
// everything the bar above the map offers is on the map's right-click menu as well.
const [ isFullscreen, toggleFullscreen ] = useFullscreen(map?.getContainer());
+ const tiltRef = useRef<HTMLButtonElement>(null);
const zoomOutRef = useRef<HTMLButtonElement>(null);
const zoomInRef = useRef<HTMLButtonElement>(null);
const fullscreenRef = useRef<HTMLButtonElement>(null);
+ // Whether the reader is in a 3D view however they got there — the button, or Ctrl and a drag.
+ const isTilted = (pitch ?? map?.getPitch() ?? 0) > 0;
+
// The group stands at the foot of the map, so its tooltips open away from that edge, where
// they would otherwise fall off.
+ useStaticTooltip(tiltRef, {
+ title: isTilted ? t("geo-map.exit-3d") : t("geo-map.enter-3d"),
+ placement: "top"
+ });
useStaticTooltip(zoomOutRef, { title: t("geo-map.zoom-out"), placement: "top" });
useStaticTooltip(zoomInRef, { title: t("geo-map.zoom-in"), placement: "top" });
useStaticTooltip(fullscreenRef, {
@@ -51,6 +69,16 @@ export default function MapToolbar() {
otherwise take it for the start of a drag. */
onMouseDown={(e) => e.stopPropagation()}
>
+ {/* Its face names the view it offers, not the one in force — Google Maps's way round. */}
+ <button
+ ref={tiltRef}
+ type="button"
+ className="tn-overlay-text-button geo-map-tilt-button"
+ aria-label={isTilted ? t("geo-map.exit-3d") : t("geo-map.enter-3d")}
+ onClick={() => map.easeTo({ pitch: isTilted ? 0 : TILTED_PITCH })}
+ >
+ {isTilted ? "2D" : "3D"}
+ </button>
<button
ref={zoomOutRef}
type="button"
@@ -119,3 +147,25 @@ function useMapZoom(map: MapLibreGLMap | null) {
return zoom;
}
+
+/**
+ * How far the view is leaned over, followed as it changes — by the tilt button, or by Ctrl and a
+ * drag, which MapLibre honours without being asked. Read for which view the button should offer:
+ * a tilt entered by hand is as much a 3D view as one the button gave.
+ */
+function useMapPitch(map: MapLibreGLMap | null) {
+ const [ pitch, setPitch ] = useState<number | null>(null);
+
+ useEffect(() => {
+ if (!map) return;
+
+ const report = () => setPitch(map.getPitch());
+ // The map may have been tilted between being built and being listened to.
+ report();
+
+ map.on("pitch", report);
+ return () => { map.off("pitch", report); };
+ }, [ map ]);
+
+ return pitch;
+}